-
Notifications
You must be signed in to change notification settings - Fork 29
/
mssql.go
91 lines (81 loc) · 2.72 KB
/
mssql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package mssql provides a Cloud SQL SQL Server driver that works with the
// database/sql package.
package mssql
import (
"context"
"database/sql"
"database/sql/driver"
"net"
"cloud.google.com/go/cloudsqlconn"
mssqldb "github.com/microsoft/go-mssqldb"
"github.com/microsoft/go-mssqldb/msdsn"
)
// RegisterDriver registers a SQL Server driver that uses the
// cloudsqlconn.Dialer configured with the provided options. The choice of name
// is entirely up to the caller and may be used to distinguish between multiple
// registrations of differently configured Dialers.
func RegisterDriver(name string, opts ...cloudsqlconn.Option) (func() error, error) {
d, err := cloudsqlconn.NewDialer(context.Background(), opts...)
if err != nil {
return func() error { return nil }, err
}
sql.Register(name, &sqlserverDriver{
d: d,
})
return func() error { return d.Close() }, nil
}
type csqlDialer struct {
driver.Conn
d *cloudsqlconn.Dialer
connName string
}
// DialContext adheres to the mssql.Dialer interface.
func (c *csqlDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
return c.d.Dial(ctx, c.connName)
}
// Close ensures the cloudsqlconn.Dialer is closed before the conncetion is
// closed.
func (c *csqlDialer) Close() error {
c.d.Close()
return c.Conn.Close()
}
type sqlserverDriver struct {
d *cloudsqlconn.Dialer
}
// Open accepts a URL, ADO, or ODBC style connection string and returns a
// connection to the database using cloudsqlconn.Dialer. The Cloud SQL instance
// connection name should be specified in a "cloudsql" parameter. For example:
//
// "sqlserver://user:password@localhost?database=mydb&cloudsql=my-proj:us-central1:my-inst"
//
// For details, see
// https://github.com/microsoft/go-mssqldb#the-connection-string-can-be-specified-in-one-of-three-formats
func (s *sqlserverDriver) Open(name string) (driver.Conn, error) {
_, res, err := msdsn.Parse(name)
if err != nil {
return nil, err
}
c, err := mssqldb.NewConnector(name)
if err != nil {
return nil, err
}
connName := res["cloudsql"]
c.Dialer = &csqlDialer{
d: s.d,
connName: connName,
}
return c.Connect(context.Background())
}