Skip to content

Commit

Permalink
Create connector in mssql
Browse files Browse the repository at this point in the history
  • Loading branch information
RudraNirvan committed May 22, 2022
1 parent 57e5ff7 commit 836fc90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"context"
"database/sql"
"flag"
"fmt"
"log"

_ "github.com/denisenkom/go-mssqldb"
"github.com/denisenkom/go-mssqldb/azuread"
mssql "github.com/denisenkom/go-mssqldb"
)

var (
Expand All @@ -32,7 +32,15 @@ func main() {
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
conn, err := sql.Open(azuread.DriverName, connString)

tokenProviderWithCtx := func(ctx context.Context) (string, error) {
return "access_token", nil
}

connector, err := mssql.NewConnectorWithAccessTokenProvider(connString, tokenProviderWithCtx)
conn := sql.OpenDB(connector)

//conn, err := sql.Open(azuread.DriverName, connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
Expand Down
14 changes: 14 additions & 0 deletions mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ func NewConnector(dsn string) (*Connector, error) {
return c, nil
}

// NewConnectorWithAccessTokenProvider creates a new connector from a DSN using the given
// access token provider. The returned connector may be used with sql.OpenDB.
func NewConnectorWithAccessTokenProvider(dsn string, tokenProvider func(ctx context.Context) (string, error)) (*Connector, error) {
params, _, err := msdsn.Parse(dsn)
if err != nil {
return nil, err
}

return NewSecurityTokenConnector(
params,
tokenProvider,
)
}

// NewConnectorConfig creates a new Connector for a DSN Config struct.
// The returned connector may be used with sql.OpenDB.
func NewConnectorConfig(config msdsn.Config) *Connector {
Expand Down

0 comments on commit 836fc90

Please sign in to comment.