Skip to content

Add support for query notifications#90

Closed
maxhauser wants to merge 1 commit into
denisenkom:masterfrom
maxhauser:query-notifications
Closed

Add support for query notifications#90
maxhauser wants to merge 1 commit into
denisenkom:masterfrom
maxhauser:query-notifications

Conversation

@maxhauser
Copy link
Copy Markdown
Contributor

Added SetQueryNotification method to MssqlStmt. Parameter semantics are the same as in new SqlNotificationRequest(userData,options,timeout).

@denisenkom
Copy link
Copy Markdown
Owner

Can you add a test for this feature?

@bbigras
Copy link
Copy Markdown
Contributor

bbigras commented Feb 9, 2016

I'm also interested to see how it works. I don't know how to use SetQueryNotification(). I don't know how to get MssqlStmt from sql.DB or sql.Stmt.

@maxhauser do you use this in production? Does it work well?

@bbigras
Copy link
Copy Markdown
Contributor

bbigras commented Feb 16, 2016

@denisenkom any idea how to use this?

@maxhauser
Copy link
Copy Markdown
Contributor Author

I used that just for a proof of concept but it did not made it into production. The use of the SetQueryNotification() method is similar to the SqlNotificationRequest described here.

To get a MssqlStmt you have to cast the Stmt that you get from Prepare() like this:

stmt, err := cn.Prepare("SELECT Name FROM dbo.Test")
if err != nil {
    panic(err)
}

sqlstmt := stmt.(*mssql.MssqlStmt)
sqlstmt.SetQueryNotification("ABC", "service=WebCacheNotifications", time.Hour)

Change notifications are then posted to the specified service broker service.

For more information about query notifications and how to set up the service broker that is a good start.

@bbigras
Copy link
Copy Markdown
Contributor

bbigras commented Feb 18, 2016

To get a MssqlStmt you have to cast the Stmt that you get from Prepare()

I tried that but I had an invalid type assertion error.

The following code give this error, I wonder if something changed in go-mssqldb or Go since the last time you tried it.

.\main.go:26: invalid type assertion: stmt.(*mssql.MssqlStmt) (non-interface type *sql.Stmt on left)`
package main

import (
    "database/sql"
    "time"

    _ "github.com/maxhauser/go-mssqldb"
)

func main() {
    cn, err := sql.Open("mssql", sqlStr)
    if err != nil {
        panic(err)
    }
    defer cn.Close()

    stmt, err := cn.Prepare("SELECT Name FROM dbo.Test")
    if err != nil {
        panic(err)
    }

    sqlstmt := stmt.(*mssql.MssqlStmt)
    sqlstmt.SetQueryNotification("ABC", "service=WebCacheNotifications", time.Hour)
}

@maxhauser
Copy link
Copy Markdown
Contributor Author

Sorry, I forgot to mention that you have to have a MssqlDriver connection. Try to open the connection directly with the mssql driver, like that:

 import mssql "github.com/maxhauser/go-mssqldb"

...

 mssqldriver := &mssql.MssqlDriver{}
 cn, err := mssqldriver.Open(connectionstring)

@bbigras
Copy link
Copy Markdown
Contributor

bbigras commented Feb 18, 2016

Thanks, it worked.

I'm not sure if I do the right thing or maybe query notifications doesn't work the way I think.

The following code executes the query, prints the output and exits. How do I get the notification? I was thinking that maybe the query would block until there's a change.

    stmt, err := cn.Prepare("SELECT test FROM dbo.test;")
    if err != nil {
        panic(err)
    }

    sqlstmt := stmt.(*mssql.MssqlStmt)
    sqlstmt.SetQueryNotification("ABC", "service=WebCacheNotifications", time.Hour)

    rows, errQuery := sqlstmt.Query(nil)
    if errQuery != nil {
        panic(errQuery)
    }
    defer rows.Close()

    result := make([]driver.Value, 1)
    if err := rows.Next(result); err != nil {
        panic(err)
    }
    fmt.Println(result)

@bbigras
Copy link
Copy Markdown
Contributor

bbigras commented Mar 7, 2016

Anyone?

@maxhauser
Copy link
Copy Markdown
Contributor Author

Change notifications are posted to the specified service broker service.
So you can listen for notifications with RECEIVE on a second connection.
For more information about query notifications and how to set up the service broker that is a good start.

@denisenkom
Copy link
Copy Markdown
Owner

merged, thanks!

@denisenkom denisenkom closed this Apr 9, 2016
odeke-em pushed a commit to orijtech/go-mssqldb that referenced this pull request Jul 26, 2020
use a non-zero exit code when no command is given
gabrielcorado pushed a commit to gravitational/go-mssqldb that referenced this pull request Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants