/
sql.go
383 lines (367 loc) · 9.19 KB
/
sql.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package weSubDatabase
import (
"fmt"
"log"
"time"
)
// 连接MySQL时的可选配置
//
// Optional configuration when connecting to MySQL
type LinkSQLO func(*Option)
// ===============
//
// 设置等待次数
// WaitCount int "等待次数"
//
// ===============
//
// Set the number of waits
// WaitCount int "Number of waits"
func OLWaitCount(WaitCount int) LinkSQLO {
return func(o *Option) {
o.WaitCount = WaitCount
}
}
// ===============
//
// 设置每次等待时间,单位毫秒
// WaitTime int "每次等待时间,单位毫秒"
//
// ===============
//
// Set the waiting time per time, in milliseconds
// WaitTime int "Waiting time per time, in milliseconds"
func OLWaitTime(WaitTime int) LinkSQLO {
return func(o *Option) {
o.WaitTime = WaitTime
}
}
// ===============
//
// 设置是输出到控制台
// IsShowPrint bool "是否输出到控制台"
//
// ===============
//
// Set whether to output to the console
// IsShowPrint bool "Whether to output to the console"
func OLIsShowPrint(IsShowPrint bool) LinkSQLO {
return func(o *Option) {
o.IsShowPrint = IsShowPrint
}
}
// 是否为主键的可选配置
//
// Optional configuration for whether it is a primary key
type IsPrimaryKeyO func(*Option)
// ===============
//
// 设置是否为主键
// IsPrimaryKey bool "是否为主键"
//
// ===============
//
// Set whether it is a primary key
// IsPrimaryKey bool "Whether it is a primary key"
func OIPKIsPrimaryKey(IsPrimaryKey bool) IsPrimaryKeyO {
return func(o *Option) {
o.IsPrimaryKey = IsPrimaryKey
}
}
// ===============
//
// 是否输出到控制台
// IsShowPrint bool "是否输出到控制台"
//
// ===============
//
// Whether to output to the console
// IsShowPrint bool "Whether to output to the console"
func OIPKIsShowPrint(IsShowPrint bool) IsPrimaryKeyO {
return func(o *Option) {
o.IsShowPrint = IsShowPrint
}
}
// 是否输出到控制台
//
// Whether to output to the console
type IsShowPrintO func(*Option)
// ===============
//
// 是否输出到控制台
// IsShowPrint bool "是否输出到控制台"
//
// ===============
//
// Whether to output to the console
// IsShowPrint bool "Whether to output to the console"
func OIsShowPrint(IsShowPrint bool) IsShowPrintO {
return func(o *Option) {
o.IsShowPrint = IsShowPrint
}
}
// ===============
//
// 连接MySQL数据库并放入连接池
// item int "配置文件中的第几个MySQL配置"
// options []LinkSQLO "配置"
// WaitCount int "等待次数"
// WaitTime int "每次等待时间,单位毫秒"
// return 1 int "连接池中的位置"
// return 2 error "错误信息"
//
// ===============
//
// Connect to MySQL database and put it into Connection pool
// item int "Which MySQL configuration in
// the configuration file"
// options []LinkSQLO "Configuration"
// WaitCount int "Number of waits"
// WaitTime int "Waiting time per time,
// in milliseconds"
// return 1 int "Position in the connection pool"
// return 2 error "Error message"
func (s *Setting) MysqlIsRun(item int, options ...LinkSQLO) (int, error) {
option := &Option{
WaitCount: 10,
WaitTime: 500,
IsShowPrint: false,
}
for _, o := range options {
o(option)
}
if s.LinkNum >= s.MaxLink {
WaitCount := 0
for {
if s.LinkNum < s.MaxLink {
break
}
if WaitCount > option.WaitCount {
return -1, fmt.Errorf("MySQL connections are full")
}
WaitCount += 1
time.Sleep(time.Duration(option.WaitTime) * time.Millisecond)
}
}
// println("==========\r\nMySQL连接中...")
wSQLdb, err := s.Link(item)
if err != nil {
tn := time.Now()
s.ConnectFailTime[item] = &tn
return -1, err
}
ii := 0
for i := 0; i < len(s.MySQLDB); i++ {
if s.MySQLDB[i] == nil {
ii = i
break
}
}
s.LinkNum += 1
s.MySQLDB[ii] = wSQLdb
if option.IsShowPrint {
println("MySQL DB", item, "connection successful!")
}
return ii, nil
}
// ===============
//
// 关闭MySQL连接
// i int "连接池中的位置"
//
// ===============
//
// Close MySQL connection
// i int "Position in the connection pool"
func (s *Setting) MysqlClose(i int, options ...IsShowPrintO) {
if i < 0 || i >= len(s.MySQLDB) {
return
}
if s.MySQLDB[i] != nil {
s.MySQLDB[i].Close()
s.MySQLDB[i] = nil
s.LinkNum -= 1
if s.LinkNum < 0 {
s.LinkNum = 0
}
option := &Option{
IsShowPrint: false,
}
for _, o := range options {
o(option)
}
if option.IsShowPrint {
println("MySQL Close Connection! Current number of connections:", s.LinkNum)
}
}
}
// ===============
//
// 根据 *MysqlDB 查询
// sqlStr string "SQL 语句"
// reqd chan []map[string]string "查询结果"
// reerr chan error "错误信息"
// Debug *log.Logger "Debug 日志对象"
//
// ===============
//
// According to *MysqlDB query
// sqlStr string "SQL statement"
// reqd chan []map[string]string "query result"
// reerr chan error "error message"
// Debug *log.Logger "Debug log object"
func (s *Setting) go_query(i int, sqlStr string, reqd chan []map[string]string, reerr chan error, IsShowPrint bool, Debug *log.Logger) {
mI, err := s.MysqlIsRun(i, OLIsShowPrint(IsShowPrint))
if err != nil {
s.MysqlClose(mI)
reqd <- nil
reerr <- err
return
}
qd, err := s.MySQLDB[mI].QueryCMD(sqlStr, Debug, OIsShowPrint(IsShowPrint))
s.MysqlClose(mI, OIsShowPrint(IsShowPrint))
reqd <- qd
reerr <- err
}
// ===============
//
// 根据 *Setting 从数据库集中调用单行SQL查询指令
// sqlStr string "SQL指令"
// Debug *log.Logger "调试输出"
// options []IsShowPrintO "配置"
// IsShowPrint bool "是否输出到控制台"
// return 1 []map[string]string "查询结果"
// return 2 []error "错误信息"
//
// ===============
//
// According to *Setting, call single row SQL query instruction from database set
// sqlStr string "SQL instruction"
// Debug *log.Logger "Debug output"
// options []IsShowPrintO "Configuration"
// IsShowPrint bool "Whether to output to the
// console"
// return 1 []map[string]string "Query result"
// return 2 []error "Error message"
func (s *MysqlDB) QueryCMD(sqlStr string, Debug *log.Logger, options ...IsShowPrintO) ([]map[string]string, error) {
option := &Option{
IsShowPrint: false,
}
for _, o := range options {
o(option)
}
if Debug != nil {
Debug.Println("[Query]", sqlStr)
}
if option.IsShowPrint {
fmt.Println("[Query]", sqlStr)
}
query, err := s.DB.Query(sqlStr)
if err != nil {
if Debug != nil {
Debug.Println("MySQL Query Error:", err)
}
return nil, err
}
return handleQD(query, Debug)
}
// ===============
//
// 根据 *MysqlDB 调用单行SQL查询指令
// sqlStr string "SQL 语句"
// reqd chan []map[string]string "查询结果"
// reerr chan error "错误信息"
// isShowPrint bool "是否输出到控制台"
// Debug *log.Logger "Debug 日志对象"
//
// ===============
//
// According to *MysqlDB query
// sqlStr string "SQL statement"
// reqd chan []map[string]string "query result"
// reerr chan error "error message"
// isShowPrint bool "Whether to output
// to the console"
// Debug *log.Logger "Debug log object"
func (s *Setting) go_exec(i int, sqlStr string, reLIid chan int64, reRA chan int64, reerr chan error, isShowPrint bool, Debug *log.Logger) {
mI, err := s.MysqlIsRun(i)
if err != nil {
s.MysqlClose(mI)
reLIid <- 0
reRA <- 0
reerr <- err
return
}
lastInsertId, rowsAffected, err := s.MySQLDB[mI].ExecCMD(sqlStr, Debug, OIsShowPrint(isShowPrint))
s.MysqlClose(mI)
if reLIid != nil {
reLIid <- lastInsertId
}
if reRA != nil {
reRA <- rowsAffected
}
reerr <- err
}
// ===============
//
// 根据 *Setting 从数据库集中调用单行SQL指令
// sqlStr string "SQL指令"
// Debug *log.Logger "调试输出"
// options []IsShowPrintO "配置"
// IsShowPrint bool "是否输出到控制台"
// return 1 int64 "插入的行数"
// return 2 int64 "影响的行数"
// return 3 []error "错误信息"
//
// ===============
//
// According to *Setting, call single row SQL instruction from database set
// sqlStr string "SQL instruction"
// Debug *log.Logger "Debug output"
// options []IsShowPrintO "Configuration"
// isShowPrint bool "Whether to output
// to the console"
// return 1 int64 "Number of rows inserted"
// return 2 int64 "Number of rows affected"
// return 3 []error "Error message"
func (s *MysqlDB) ExecCMD(sqlStr string, Debug *log.Logger, options ...IsShowPrintO) (int64, int64, error) {
option := &Option{
IsShowPrint: false,
}
for _, o := range options {
o(option)
}
if Debug != nil {
Debug.Println("[ExecCMD]", sqlStr)
}
if option.IsShowPrint {
fmt.Println("[ExecCMD]", sqlStr)
}
var (
lastInsertId int64 = 0
rowsAffected int64 = 0
err error = nil
)
result, err := s.DB.Exec(sqlStr)
if err != nil {
if Debug != nil {
Debug.Println("MySQL Query Error:", err)
}
return lastInsertId, rowsAffected, err
}
lastInsertId, err = result.LastInsertId()
if err != nil {
if Debug != nil {
Debug.Println("lastInsertId Error:", err)
}
return lastInsertId, rowsAffected, err
}
rowsAffected, err = result.RowsAffected()
if err != nil {
if Debug != nil {
Debug.Println("rowsAffected Error:", err)
}
return lastInsertId, rowsAffected, err
}
return lastInsertId, rowsAffected, err
}