-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_server_storage.go
266 lines (240 loc) · 7.18 KB
/
sql_server_storage.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package sqlserver_storage
import (
"context"
"errors"
"fmt"
sql_based_storage "github.com/storage-lock/go-sql-based-storage"
"github.com/storage-lock/go-storage"
storage_lock "github.com/storage-lock/go-storage-lock"
"strings"
"time"
_ "github.com/denisenkom/go-mssqldb"
)
type SqlServerStorage struct {
*sql_based_storage.SqlBasedStorage
options *SqlServerStorageOptions
tableFullName string
}
var _ storage.Storage = &SqlServerStorage{}
// NewSqlServerStorage 创建一个基于SqlServer的Storage
func NewSqlServerStorage(ctx context.Context, options *SqlServerStorageOptions) (*SqlServerStorage, error) {
// 参数检查
if err := options.Check(); err != nil {
return nil, err
}
// sql storage的基础Storage
baseStorageOption := sql_based_storage.NewSqlBasedStorageOptions().
SetConnectionManager(options.ConnectionManager).
SetSqlProvider(sql_based_storage.NewSql92Provider()).
SetTableFullName(options.TableName)
baseStorage, err := sql_based_storage.NewSqlBasedStorage(baseStorageOption)
if err != nil {
return nil, err
}
s := &SqlServerStorage{
SqlBasedStorage: baseStorage,
options: options,
}
err = s.Init(ctx)
if err != nil {
return nil, err
}
return s, nil
}
const StorageName = "sql-server-storage"
func (x *SqlServerStorage) GetName() string {
return StorageName
}
func (x *SqlServerStorage) Init(ctx context.Context) (returnError error) {
db, err := x.options.ConnectionManager.Take(ctx)
if err != nil {
return err
}
defer func() {
err := x.options.ConnectionManager.Return(ctx, db)
if returnError == nil {
returnError = err
}
}()
// 创建存储锁信息需要的表
tableFullName := x.options.TableName
if tableFullName == "" {
tableFullName = storage.DefaultStorageTableName
}
// TODO 2023-8-9 00:09:08 跟驱动名称是不是有关
// 这个语法好像执行不过去
//createTableSql := `IF NOT EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='%s' AND XTYPE='U')
// CREATE TABLE %s (
// lock_id VARCHAR(255) NOT NULL PRIMARY KEY,
// version BIGINT NOT NULL,
// lock_information_json_string VARCHAR(255) NOT NULL
// )
//GO`
// 这个语法是可以的
createTableSql := `IF NOT EXISTS (
SELECT * FROM sys.tables t
JOIN sys.schemas s ON (t.schema_id = s.schema_id)
WHERE s.name = 'dbo' AND t.name = '%s')
CREATE TABLE %s (
lock_id VARCHAR(255) NOT NULL PRIMARY KEY,
owner_id VARCHAR(255) NOT NULL,
version BIGINT NOT NULL,
lock_information_json_string VARCHAR(255) NOT NULL
);`
_, err = db.ExecContext(ctx, fmt.Sprintf(createTableSql, tableFullName, tableFullName))
if err != nil {
return err
}
x.tableFullName = tableFullName
return nil
}
//
//func (x *SqlServerStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) (returnError error) {
//
// db, err := x.options.ConnectionManager.Take(ctx)
// if err != nil {
// return err
// }
// defer func() {
// err := x.options.ConnectionManager.Return(ctx, db)
// if returnError == nil {
// returnError = err
// }
// }()
//
// insertSql := fmt.Sprintf(`UPDATE %s SET version = ?, lock_information_json_string = ? WHERE lock_id = ? AND owner_id = ? AND version = ?`, x.tableFullName)
// execContext, err := db.ExecContext(ctx, insertSql, newVersion, lockInformation.ToJsonString(), lockId, lockInformation.OwnerId, exceptedVersion)
// if err != nil {
// return err
// }
// affected, err := execContext.RowsAffected()
// if err != nil {
// return err
// }
// if affected == 0 {
// return storage_lock.ErrVersionMiss
// }
// return nil
//}
func (x *SqlServerStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) (returnError error) {
returnError = x.SqlBasedStorage.CreateWithVersion(ctx, lockId, version, lockInformation)
if returnError != nil {
errorMsg := returnError.Error()
// 把SqlServer主键重复的错误转换为StorageLock框架内部的Version Miss的错误
// panic: mssql: Violation of PRIMARY KEY constraint 'PK__storage___AE35FA63FDC73D2D'. Cannot insert duplicate key in object 'dbo.storage_lock'. The duplicate key value is (35dc907fdbef4eb6b61718c6d083492d).
if strings.Contains(errorMsg, "Violation of PRIMARY KEY constraint") {
return storage_lock.ErrVersionMiss
}
}
return returnError
}
//func (x *SqlServerStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) (returnError error) {
//
// db, err := x.options.ConnectionManager.Take(ctx)
// if err != nil {
// return err
// }
// defer func() {
// err := x.options.ConnectionManager.Return(ctx, db)
// if returnError == nil {
// returnError = err
// }
// }()
//
// deleteSql := fmt.Sprintf(`DELETE FROM %s WHERE lock_id = ? AND owner_id = ? AND version = ?`, x.tableFullName)
// execContext, err := db.ExecContext(ctx, deleteSql, lockId, lockInformation.OwnerId, exceptedVersion)
// if err != nil {
// return err
// }
// affected, err := execContext.RowsAffected()
// if err != nil {
// return err
// }
// if affected == 0 {
// return storage_lock.ErrVersionMiss
// }
// return nil
//}
//
//func (x *SqlServerStorage) Get(ctx context.Context, lockId string) (lockInformationJsonString string, returnError error) {
//
// db, err := x.options.ConnectionManager.Take(ctx)
// if err != nil {
// return "", err
// }
// defer func() {
// err := x.options.ConnectionManager.Return(ctx, db)
// if returnError == nil {
// returnError = err
// }
// }()
//
// getLockSql := fmt.Sprintf("SELECT lock_information_json_string FROM %s WHERE lock_id = ?", x.tableFullName)
// rs, err := db.Query(getLockSql, lockId)
// if err != nil {
// return "", err
// }
// defer func() {
// _ = rs.Close()
// }()
// if !rs.Next() {
// return "", storage_lock.ErrLockNotFound
// }
// err = rs.Scan(&lockInformationJsonString)
// if err != nil {
// return "", err
// }
// return lockInformationJsonString, nil
//}
func (x *SqlServerStorage) GetTime(ctx context.Context) (now time.Time, returnError error) {
db, err := x.options.ConnectionManager.Take(ctx)
if err != nil {
return time.Time{}, err
}
defer func() {
err := x.options.ConnectionManager.Return(ctx, db)
if returnError == nil {
returnError = err
}
}()
var zero time.Time
rs, err := db.Query("SELECT CURRENT_TIMESTAMP")
if err != nil {
return zero, err
}
defer func() {
_ = rs.Close()
}()
if !rs.Next() {
return zero, errors.New("rs server time failed")
}
var databaseTime time.Time
err = rs.Scan(&databaseTime)
if err != nil {
return zero, err
}
return databaseTime, nil
}
//func (x *SqlServerStorage) Close(ctx context.Context) error {
// return nil
//}
//
//func (x *SqlServerStorage) List(ctx context.Context) (iterator iterator.Iterator[*storage.LockInformation], returnError error) {
//
// db, err := x.options.ConnectionManager.Take(ctx)
// if err != nil {
// return nil, err
// }
// defer func() {
// err := x.options.ConnectionManager.Return(ctx, db)
// if returnError == nil {
// returnError = err
// }
// }()
//
// rows, err := db.Query(fmt.Sprintf("SELECT * FROM %s", x.tableFullName))
// if err != nil {
// return nil, err
// }
// return storage.NewSqlRowsIterator(rows), nil
//}