-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sqlexp Messages for Query/QueryContext (#690)
* Ask for usedb and set language messages * add functional test for message queue * first basic version of msg queue * complete messages implementation * fix lint and build issues * fix test to avoid 0 rows in a query * add sqlexp to appveyor yml * fix build * fix the merge * move messageq tests to go1.9 * fix example function and support env vars * implement data type discovery * fix pipeline variables for tests
- Loading branch information
1 parent
b979f7a
commit e538731
Showing
20 changed files
with
762 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// +build go1.14 | ||
|
||
package mssql | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func BenchmarkMessageQueue(b *testing.B) { | ||
conn, logger := open(b) | ||
defer conn.Close() | ||
defer logger.StopLogging() | ||
|
||
b.Run("BlockingQuery", func(b *testing.B) { | ||
var errs, results float64 | ||
for i := 0; i < b.N; i++ { | ||
r, err := conn.Query(mixedQuery) | ||
if err != nil { | ||
b.Fatal(err.Error()) | ||
} | ||
defer r.Close() | ||
active := true | ||
first := true | ||
for active { | ||
active = r.Next() | ||
if active && first { | ||
results++ | ||
} | ||
first = false | ||
if !active { | ||
if r.Err() != nil { | ||
b.Logf("r.Err:%v", r.Err()) | ||
errs++ | ||
} | ||
active = r.NextResultSet() | ||
if active { | ||
first = true | ||
} | ||
} | ||
} | ||
} | ||
b.ReportMetric(float64(0), "msgs/op") | ||
b.ReportMetric(errs/float64(b.N), "errors/op") | ||
b.ReportMetric(results/float64(b.N), "results/op") | ||
}) | ||
b.Run("NonblockingQuery", func(b *testing.B) { | ||
var msgs, errs, results, rowcounts float64 | ||
for i := 0; i < b.N; i++ { | ||
m, e, r, rc := testMixedQuery(conn, b) | ||
msgs += float64(m) | ||
errs += float64(e) | ||
results += float64(r) | ||
rowcounts += float64(rc) | ||
if r != 4 { | ||
b.Fatalf("Got wrong results count: %d, expected 4", r) | ||
} | ||
} | ||
b.ReportMetric(msgs/float64(b.N), "msgs/op") | ||
b.ReportMetric(errs/float64(b.N), "errors/op") | ||
b.ReportMetric(results/float64(b.N), "results/op") | ||
b.ReportMetric(rowcounts/float64(b.N), "rowcounts/op") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//go:build go1.10 | ||
// +build go1.10 | ||
|
||
package mssql_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
|
||
mssql "github.com/denisenkom/go-mssqldb" | ||
"github.com/golang-sql/sqlexp" | ||
) | ||
|
||
const ( | ||
msgQuery = `select 'name' as Name | ||
PRINT N'This is a message' | ||
select 199 | ||
RAISERROR (N'Testing!' , 11, 1) | ||
select 300 | ||
` | ||
) | ||
|
||
// This example shows the usage of sqlexp/Messages | ||
func ExampleRows_usingmessages() { | ||
|
||
connString := makeConnURL().String() | ||
|
||
// Create a new connector object by calling NewConnector | ||
connector, err := mssql.NewConnector(connString) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
// Pass connector to sql.OpenDB to get a sql.DB object | ||
db := sql.OpenDB(connector) | ||
defer db.Close() | ||
retmsg := &sqlexp.ReturnMessage{} | ||
ctx := context.Background() | ||
rows, err := db.QueryContext(ctx, msgQuery, retmsg) | ||
if err != nil { | ||
log.Fatalf("QueryContext failed: %v", err) | ||
} | ||
active := true | ||
for active { | ||
msg := retmsg.Message(ctx) | ||
switch m := msg.(type) { | ||
case sqlexp.MsgNotice: | ||
fmt.Println(m.Message) | ||
case sqlexp.MsgNext: | ||
inresult := true | ||
for inresult { | ||
inresult = rows.Next() | ||
if inresult { | ||
cols, err := rows.Columns() | ||
if err != nil { | ||
log.Fatalf("Columns failed: %v", err) | ||
} | ||
fmt.Println(cols) | ||
var d interface{} | ||
if err = rows.Scan(&d); err == nil { | ||
fmt.Println(d) | ||
} | ||
} | ||
} | ||
case sqlexp.MsgNextResultSet: | ||
active = rows.NextResultSet() | ||
case sqlexp.MsgError: | ||
fmt.Println("Error:", m.Error) | ||
case sqlexp.MsgRowsAffected: | ||
fmt.Println("Rows affected:", m.Count) | ||
} | ||
} | ||
} |
Oops, something went wrong.