-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for SetQueryNotification #524
base: master
Are you sure you want to change the base?
Changes from 1 commit
3659894
df965ee
947a6d7
47f1070
ebae179
6f7b03e
8638508
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,61 @@ | ||||||
package main | ||||||
|
||||||
import ( | ||||||
"flag" | ||||||
"fmt" | ||||||
"log" | ||||||
"time" | ||||||
|
||||||
mssql "github.com/denisenkom/go-mssqldb" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need to explicitly set the alias. It is
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept it since it seems to be like this in other examples. |
||||||
) | ||||||
|
||||||
var ( | ||||||
debug = flag.Bool("debug", false, "enable debugging") | ||||||
password = flag.String("password", "", "the database password") | ||||||
port *int = flag.Int("port", 1433, "the database port") | ||||||
server = flag.String("server", "", "the database server") | ||||||
user = flag.String("user", "", "the database user") | ||||||
database = flag.String("database", "", "the database name") | ||||||
) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will get redefinition errors here if this file is in |
||||||
|
||||||
func ExampleSetQueryNotification() { | ||||||
flag.Parse() | ||||||
|
||||||
if *debug { | ||||||
fmt.Printf(" password:%s\n", *password) | ||||||
fmt.Printf(" port:%d\n", *port) | ||||||
fmt.Printf(" server:%s\n", *server) | ||||||
fmt.Printf(" user:%s\n", *user) | ||||||
fmt.Printf(" database:%s\n", *database) | ||||||
} | ||||||
|
||||||
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;", *server, *user, *password, *port, *database) | ||||||
if *debug { | ||||||
fmt.Printf(" connString:%s\n", connString) | ||||||
} | ||||||
mssqldriver := &mssql.Driver{} | ||||||
cn, err := mssqldriver.Open(connString) | ||||||
if err != nil { | ||||||
log.Fatal("Open connection failed:", err.Error()) | ||||||
} | ||||||
defer cn.Close() | ||||||
conn, _ := cn.(*mssql.Conn) | ||||||
|
||||||
// Supported SELECT statements: https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms181122(v=sql.105) | ||||||
stmt, err := conn.Prepare("SELECT [myColumn] FROM [mySchema].[myTable];") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any special setup needed for the Schema and Table for query notification to work? Perhaps add comments letting user know what setup is required? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no special setup for the schema or table but query notifications must be setup on SQL Server for this example to work. |
||||||
if err != nil { | ||||||
log.Fatal("Prepare failed:", err.Error()) | ||||||
} | ||||||
defer stmt.Close() | ||||||
|
||||||
sqlstmt, _ := stmt.(*mssql.Stmt) | ||||||
defer sqlstmt.Close() | ||||||
sqlstmt.SetQueryNotification("Message", "service=myService", time.Hour) | ||||||
|
||||||
rows, err := sqlstmt.Query(nil) | ||||||
if err != nil { | ||||||
log.Fatal("Query failed:", err.Error()) | ||||||
} else { | ||||||
rows.Close() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does |
||||||
} | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add newline. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only documentation (md files) should be in the doc folder. If this example test works as a standalone test, please put it in the root folder.
Since this example is targeting the
SetQueryNotification
, you should rename is to something likesetquerynotification_example_text.go
.If you look at other *_examples_test.go files (e.g.,
datetimeoffset_example_test.go
), you will see that instead of identifying is aspackage main
, they usepackage mssql_test
. That way if this file is in the root folder along with the src files, you will not get package clash (having 2 packages in the same directory). When godoc gets this example, they'll replacepackage mssql_test
withpackage main
so it looks like a proper test application.It's also a good idea to add a
.md
file in the doc folder to show the users now this query notification feature works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the function has "Example" prefixing it's name it won't be run as a test. I have moved it from the doc folder though.