-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlserver_connection_manager.go
115 lines (94 loc) · 2.39 KB
/
sqlserver_connection_manager.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package sqlserver_storage
import (
"context"
"database/sql"
"fmt"
"github.com/storage-lock/go-storage"
"sync"
)
const (
DriverNameMsSql = "mssql"
DriverNameSqlServer = "sqlserver"
)
// SqlServerConnectionManager 负责管理与Sql Server服务器的连接
type SqlServerConnectionManager struct {
// 主机的名字
Host string
// 主机的端口
Port uint
// 用户名
User string
// 密码
Passwd string
// DSN
// Example: "sqlserver://sa:[email protected]:1433"
DSN string
driverName string
// 初始化好的数据库实例
db *sql.DB
err error
once sync.Once
}
var _ storage.ConnectionManager[*sql.DB] = &SqlServerConnectionManager{}
// NewSqlServerConnectionManagerFromDsn 从DSN创建SqlServer连接
func NewSqlServerConnectionManagerFromDsn(dsn string) *SqlServerConnectionManager {
return &SqlServerConnectionManager{
DSN: dsn,
}
}
func NewSqlServerConnectionManagerFromSqlDb(db *sql.DB) *SqlServerConnectionManager {
return &SqlServerConnectionManager{
db: db,
}
}
// NewSqlServerConnectionManager 从服务器属性创建数据库连接
func NewSqlServerConnectionManager(host string, port uint, user, passwd string) *SqlServerConnectionManager {
return &SqlServerConnectionManager{
Host: host,
Port: port,
User: user,
Passwd: passwd,
}
}
func (x *SqlServerConnectionManager) SetDriverName(driverName string) *SqlServerConnectionManager {
x.driverName = driverName
return x
}
const SqlServerConnectionManagerName = "sql-server-connection-manager"
func (x *SqlServerConnectionManager) Name() string {
return SqlServerConnectionManagerName
}
func (x *SqlServerConnectionManager) GetDSN() string {
if x.DSN != "" {
return x.DSN
}
return fmt.Sprintf("sqlserver://%s:%s@%s:%d", x.User, x.Passwd, x.Host, x.Port)
}
// Take 获取到数据库的连接
func (x *SqlServerConnectionManager) Take(ctx context.Context) (*sql.DB, error) {
x.once.Do(func() {
if x.db != nil {
return
}
driverName := x.driverName
if driverName == "" {
driverName = DriverNameMsSql
}
db, err := sql.Open(driverName, x.GetDSN())
if err != nil {
x.err = err
return
}
x.db = db
})
return x.db, x.err
}
func (x *SqlServerConnectionManager) Return(ctx context.Context, db *sql.DB) error {
return nil
}
func (x *SqlServerConnectionManager) Shutdown(ctx context.Context) error {
if x.db != nil {
return x.db.Close()
}
return nil
}