-
Notifications
You must be signed in to change notification settings - Fork 2
/
spellsql_test.go
547 lines (492 loc) · 16.4 KB
/
spellsql_test.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package spellsql
import (
"fmt"
"strconv"
"strings"
"testing"
"gitee.com/xuesongtao/spellsql/test"
)
func TestHandleCusType(t *testing.T) {
type MyStr string
type MyStrAlias = string
type Tmp struct {
Name MyStr
Addr MyStrAlias
}
tmp := &Tmp{
Name: MyStr("myStr"),
Addr: MyStrAlias("alias"),
}
sqlStr := FmtSqlStr("myStr=? and alias=?", tmp.Name, tmp.Addr)
sureStr := `myStr="myStr" and alias="alias"`
if sqlStr != sureStr {
t.Error("is no ok")
}
}
// 新增
func TestNewCacheSql_INSERT(t *testing.T) {
t.Run("no have values", func(t *testing.T) {
s := NewCacheSql("INSERT INTO sys_user (username, password, name)")
// s.SetPrintLog(false)
s.SetInsertValues("xuesongtao", "123456", "阿桃")
s.SetInsertValues("xuesongtao", "123456", "阿桃")
sqlStr := s.GetSqlStr()
sureSql := `INSERT INTO sys_user (username, password, name) VALUES ("xuesongtao", "123456", "阿桃"), ("xuesongtao", "123456", "阿桃");`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("have values", func(t *testing.T) {
s := NewCacheSql("INSERT INTO sys_user (username, password, name) VALUES")
// s.SetPrintLog(false)
s.SetInsertValues("xuesongtao", "123456", "阿桃")
s.SetInsertValues("xuesongtao", "123456", "阿桃")
sqlStr := s.GetSqlStr()
sureSql := `INSERT INTO sys_user (username, password, name) VALUES ("xuesongtao", "123456", "阿桃"), ("xuesongtao", "123456", "阿桃");`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("have values", func(t *testing.T) {
s := NewCacheSql("INSERT INTO sys_user (username, password, name) VALUES (?, ?, ?)", "test", 123456, "阿涛")
// s.SetPrintLog(false)
s.SetInsertValues("xuesongtao", "123456", "阿桃")
s.SetInsertValues("xuesongtao", "123456", "阿桃")
sqlStr := s.GetSqlStr()
sureSql := `INSERT INTO sys_user (username, password, name) VALUES ("test", 123456, "阿涛"), ("xuesongtao", "123456", "阿桃"), ("xuesongtao", "123456", "阿桃");`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("key-value", func(t *testing.T) {
s := NewCacheSql("INSERT INTO sys_user (username, password)")
// s.SetPrintLog(false)
s.SetInsertValues("xue", 12)
sqlStr := s.GetSqlStr()
sureSql := `INSERT INTO sys_user (username, password) VALUES ("xue", 12);`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("insert many", func(t *testing.T) {
s := NewCacheSql("INSERT INTO sys_user (username, password)")
// s.SetPrintLog(false)
for i := 0; i < 2; i++ {
s.SetInsertValuesArgs("?, ?d", "xue", "123456")
s.SetInsertValues("xue", 123456)
}
sqlStr := s.GetSqlStr()
sureSql := `INSERT INTO sys_user (username, password) VALUES ("xue", 123456), ("xue", 123456), ("xue", 123456), ("xue", 123456);`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("duplicate update", func(t *testing.T) {
s := NewCacheSql("INSERT INTO sys_user (username, password, age)")
// s.SetPrintLog(false)
s.SetInsertValuesArgs("?, ?, ?d", "xuesongtao", "123", "20")
s.Append("ON DUPLICATE KEY UPDATE username=VALUES(?v)", "username")
sqlStr := s.GetSqlStr()
sureSql := `INSERT INTO sys_user (username, password, age) VALUES ("xuesongtao", "123", 20) ON DUPLICATE KEY UPDATE username=VALUES(username);`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
}
// 删除
func TestNewCacheSql_DELETE(t *testing.T) {
t.Run("1", func(t *testing.T) {
s := NewCacheSql("DELETE FROM sys_user WHERE id = ?", 123)
// s.SetPrintLog(false)
sqlStr := s.GetSqlStr()
sureSql := "DELETE FROM sys_user WHERE id = 123;"
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("2", func(t *testing.T) {
s := NewCacheSql("DELETE FROM sys_user WHERE id = ?", 123)
// s.SetPrintLog(false)
if true {
s.SetWhere("age", ">", 10)
}
sqlStr := s.GetSqlStr()
sureSql := "DELETE FROM sys_user WHERE id = 123 AND age > 10;"
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
}
// 修改
func TestNewCacheSql_UPDATE(t *testing.T) {
t.Run("normal", func(t *testing.T) {
s := NewCacheSql("UPDATE sys_user SET username = ?, password = ?, name = ? WHERE id = ?", "test", 123456, "阿涛", 12)
// s.SetPrintLog(false)
sqlStr := s.GetSqlStr()
sureSql := `UPDATE sys_user SET username = "test", password = 123456, name = "阿涛" WHERE id = 12;`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("key-value", func(t *testing.T) {
idsStr := []string{"1", "2", "3", "4", "5"}
s := NewCacheSql("UPDATE sys_user SET")
// s.SetPrintLog(false)
s.SetUpdateValue("name", "xue")
s.SetUpdateValueArgs("age = ?, score = ?", 18, 90.5)
s.SetWhereArgs("id IN (?d) AND age IN (?) AND name = ?", idsStr, []int{18, 20}, "xuesongtao")
sqlStr := s.GetSqlStr()
sureSql := `UPDATE sys_user SET name = "xue", age = 18, score = 90.5 WHERE id IN (1,2,3,4,5) AND age IN (18,20) AND name = "xuesongtao";`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("placeholder", func(t *testing.T) {
idsStr := []string{"1", "2", "3", "4", "5"}
s := NewCacheSql("UPDATE sys_user SET")
// s.SetPrintLog(false)
s.SetUpdateValue("name", "xue")
s.SetUpdateValueArgs("age = ?, score = ?", 18, 90.5)
s.SetWhereArgs("id IN (?d) AND name = ?", idsStr, "xuesongtao")
sqlStr := s.GetSqlStr()
sureSql := `UPDATE sys_user SET name = "xue", age = 18, score = 90.5 WHERE id IN (1,2,3,4,5) AND name = "xuesongtao";`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
}
func TestNewCacheSql_Select(t *testing.T) {
t.Run("list", func(t *testing.T) {
s := NewSql("SELECT username, password FROM sys_user WHERE money > ?", 1000.00)
// s.SetPrintLog(false)
if true {
s.SetWhereArgs("age > ?d", "12")
}
if true {
s.SetWhere("age", "=", "18 or 1=1") // 测试注入
}
if true {
s.SetWhere("age", "IN", []string{"18 or 1=1"}) // 测试注入
}
if true {
s.SetBetween("create_time", "2022-04-01 01:00:11", "2022-05-01 01:00:11")
}
if true {
s.SetOrWhere("name", "xue")
}
totalSqlStr := s.GetTotalSqlStr()
sureSql := `SELECT COUNT(*) FROM sys_user WHERE money > 1000 AND age > 12 AND age = "18 or 1=1" AND age IN ("18 or 1=1") AND (create_time BETWEEN "2022-04-01 01:00:11" AND "2022-05-01 01:00:11") OR name = "xue";`
if !test.Equal(totalSqlStr, sureSql) {
t.Error(test.NoEqErr)
}
sqlStr := s.SetOrderByStr("create_time DESC").SetLimit(1, 10).GetSqlStr()
sureSql = `SELECT username, password FROM sys_user WHERE money > 1000 AND age > 12 AND age = "18 or 1=1" AND age IN ("18 or 1=1") AND (create_time BETWEEN "2022-04-01 01:00:11" AND "2022-05-01 01:00:11") OR name = "xue" ORDER BY create_time DESC LIMIT 10 OFFSET 0;`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("son select", func(t *testing.T) {
s := NewSql("SELECT username, password FROM sys_user WHERE")
// s.SetPrintLog(false)
if true {
s.SetWhere("age", "IN", FmtSqlStr("SELECT age FROM user_info WHERE id=?", 10))
}
if true {
s.SetWhereArgs("age IN (?v)", FmtSqlStr("SELECT age FROM user_info WHERE id=?", 10))
}
sqlStr := s.GetSqlStr()
sureSql := `SELECT username, password FROM sys_user WHERE age IN (SELECT age FROM user_info WHERE id=10) AND age IN (SELECT age FROM user_info WHERE id=10);`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("list select", func(t *testing.T) {
s := NewSql("SELECT username, password FROM sys_user WHERE")
// s.SetPrintLog(false)
if true {
s.SetAllLike("name", "test")
}
if true {
s.SetLeftLike("name", "test")
}
if true {
s.SetRightLike("name", "test")
}
sqlStr := s.GetSqlStr()
sureSql := `SELECT username, password FROM sys_user WHERE name LIKE "%test%" AND name LIKE "%test" AND name LIKE "test%";`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("list select join", func(t *testing.T) {
s := NewSql("SELECT su.username, su.password FROM sys_user su").
SetJoin("user_cls uc", "su.id=uc.user_id", LJI).
SetJoin("test t", "t.user_cls_id=uc.id")
// s.SetPrintLog(false)
if true {
s.SetWhere("su.name", "test")
}
sqlStr := s.GetSqlStr()
sureSql := `SELECT su.username, su.password FROM sys_user su LEFT JOIN user_cls uc ON su.id=uc.user_id JOIN test t ON t.user_cls_id=uc.id WHERE su.name = "test";`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
t.Run("group by", func(t *testing.T) {
s := NewSql("SELECT cls_id,COUNT(*) FROM sys_user WHERE")
s.SetPrintLog(false)
sqlStr := s.SetGroupByStr("cls_id").SetHaving("sum(cls_id)>10").GetSqlStr()
sureSql := `SELECT cls_id,COUNT(*) FROM sys_user WHERE GROUP BY cls_id HAVING sum(cls_id)>10;`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
})
}
func TestGetSqlStr(t *testing.T) {
sqlStr := GetSqlStr("INSERT INTO doctor_check_record (d_id, is_accept, no_accept_reasons, no_accept_img, check_id) "+
"VALUES (?, ?, ?, ?, ?, ?d)", 1, 1, "test", "req.NoAcceptImg", 12, "1")
sureSql := `INSERT INTO doctor_check_record (d_id, is_accept, no_accept_reasons, no_accept_img, check_id) VALUES (1, 1, "test", "req.NoAcceptImg", 12, 1);`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
}
func TestFmtSqlStr(t *testing.T) {
sqlStr := FmtSqlStr("SELECT * FROM user_info WHERE id IN (?)", []int{1, 2, 3})
sureSql := `SELECT * FROM user_info WHERE id IN (1,2,3)`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
sqlStr = FmtSqlStr("SELECT * FROM user_info WHERE id IN (?d)", []string{"1", "2", "3"})
sureSql = `SELECT * FROM user_info WHERE id IN (1,2,3)`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
sqlStr = FmtSqlStr("SELECT account_id FROM (?v) tmp GROUP BY account_id HAVING COUNT(*)>=? ORDER BY NULL",
"SELECT account_id FROM test1 UNION ALL SELECT account_id FROM test2", 2)
sureSql = `SELECT account_id FROM (SELECT account_id FROM test1 UNION ALL SELECT account_id FROM test2) tmp GROUP BY account_id HAVING COUNT(*)>=2 ORDER BY NULL`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
// 组合 fmt 1
sqlObj := NewCacheSql("id=1")
if true {
sqlObj.SetWhere("name", "test")
}
if true {
sqlObj.SetWhereArgs("age>?", 1)
}
if true {
sqlObj.SetOrWhere("name", "1")
}
sqlStr = sqlObj.FmtSql()
sureSql = `id=1 AND name = "test" AND age>1 OR name = "1"`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
}
func TestFmtLikeSqlStr(t *testing.T) {
sqlStr := GetLikeSqlStr(ALK, "SELECT id, username FROM sys_user", "name", "xue")
sureSql := `SELECT id, username FROM sys_user WHERE name LIKE "%xue%"`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
sqlStr = GetLikeSqlStr(RLK, "SELECT id, username FROM sys_user", "name", "xue")
sureSql = `SELECT id, username FROM sys_user WHERE name LIKE "xue%"`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
sqlStr = GetLikeSqlStr(LLK, "SELECT id, username FROM sys_user", "name", "xue")
sureSql = `SELECT id, username FROM sys_user WHERE name LIKE "%xue"`
if !test.Equal(sqlStr, sureSql) {
t.Error(test.NoEqErr)
}
}
func TestIndexForBF(t *testing.T) {
str := "SELECT kind_id, kind_name FROM item_kind WHERE"
i := IndexForBF(true, str, "WHEREb")
if i != -1 {
t.Error(test.NoEqErr)
}
// str = "SELECT kind_id, kind_name FROM item_kind WHERE"
str = "SELECT kind_id, kind_name FROM item_kind WHERE"
i = IndexForBF(false, str, "aSELECT")
if i != -1 {
t.Error(test.NoEqErr)
}
}
// 去重
func TestDistinctIdsStr(t *testing.T) {
ids := ""
for i := 0; i < 10; i++ {
ids += fmt.Sprintf("%d,", i%2)
}
t.Log("ids: ", ids)
res := DistinctIdsStr(ids, ",")
if res != "0,1" {
t.Log("ids: ", res)
t.Error(test.NoEqErr)
}
}
func TestDistinctIds(t *testing.T) {
ids := []string{"0", "1", "2", "1", "0", "2"}
res := DistinctIds(ids)
if !test.Equal([]string{"0", "1", "2"}, res) {
t.Error(test.NoEqErr)
}
}
func BenchmarkIndexForBF1(b *testing.B) {
for i := 0; i < b.N; i++ {
str := "SELECT kind_id, kind_name FROM item_kind WHERE"
_ = IndexForBF(true, str, "WHERE")
}
}
func BenchmarkIndexForBF12(b *testing.B) {
for i := 0; i < b.N; i++ {
str := "SELECT kind_id, kind_name FROM item_kind WHERE"
_ = IndexForBF(false, str, "WHERE")
}
}
func BenchmarkIndexForBF(b *testing.B) {
for i := 0; i < b.N; i++ {
str := "GROUP BY test, test1"
_ = IndexForBF(true, str, "GROUP BY")
}
}
func BenchmarkStringIndex(b *testing.B) {
for i := 0; i < b.N; i++ {
str := "GROUP BY test, test1"
_ = strings.Index(str, "GROUP BY")
}
}
func BenchmarkFmtInt2Str(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%d", i)
}
}
func BenchmarkSqlStr_Int2Str(b *testing.B) {
s := NewSql("SELECT 12")
var i int64
for i < int64(b.N) {
s.Int2Str(i)
i++
}
}
// go test -benchmem -run=^$ -bench ^BenchmarkSqlStr_GetSql gitee.com/xuesongtao/spellsql -v -count=5
func BenchmarkSqlStr_GetSql(b *testing.B) {
b.ResetTimer()
totalSqlStr, sqlStr := "", ""
for i := 0; i < b.N; i++ {
s := NewCacheSql("SELECT u.username, u.password FROM sys_user su LEFT JOIN user u ON su.id = u.id")
s.SetWhere("u.username", "test")
s.SetWhere("u.password", "test")
s.SetWhere("u.password", "IN", "SELECT id FROM t WHERE id = 10")
s.SetLimit(0, 10)
s.SetGroupByStr("u.username, u.password")
totalSqlStr = s.SetPrintLog(false).GetTotalSqlStr()
sqlStr = s.GetSqlStr()
}
// b.Log(totalSqlStr, sqlStr)
_ = totalSqlStr
_ = sqlStr
// BenchmarkSqlStr_GetSql-8 742323 1610 ns/op 1824 B/op 19 allocs/op
// BenchmarkSqlStr_GetSql-8 721304 1610 ns/op 1824 B/op 19 allocs/op
// BenchmarkSqlStr_GetSql-8 717390 1616 ns/op 1824 B/op 19 allocs/op
// BenchmarkSqlStr_GetSql-8 714825 1613 ns/op 1824 B/op 19 allocs/op
// BenchmarkSqlStr_GetSql-8 724214 1614 ns/op 1824 B/op 19 allocs/op
}
func BenchmarkSqlStr_GetSql2(b *testing.B) {
b.ResetTimer()
totalSqlStr, sqlStr := "", ""
for i := 0; i < b.N; i++ {
s := "SELECT u.username, u.password FROM sys_user su LEFT JOIN user u ON su.id = u.id WHERE"
s1 := "SELECT count(*) FROM sys_user su LEFT JOIN user u ON su.id = u.id WHERE"
s += fmt.Sprintf(" u.username = %q AND", testMySQLEscape("test"))
s1 += fmt.Sprintf(" u.username = %q AND", testMySQLEscape("test"))
s += fmt.Sprintf(" u.password = %q AND", testMySQLEscape("test"))
s1 += fmt.Sprintf(" u.password = %q AND", testMySQLEscape("test"))
s += fmt.Sprintf(" u.password IN (%v) AND", testMySQLEscape("SELECT id FROM t WHERE id = 10"))
s1 += fmt.Sprintf(" u.password IN (%v) AND", testMySQLEscape("SELECT id FROM t WHERE id = 10"))
s += fmt.Sprintf(" LIMIT %d, %d", 0, 10)
s1 += fmt.Sprintf(" LIMIT %d, %d", 0, 10)
s += "GROUP BY u.username, u.password"
s1 += "GROUP BY u.username, u.password"
totalSqlStr = s
sqlStr = s1
}
// b.Log(totalSqlStr, sqlStr)
_ = totalSqlStr
_ = sqlStr
// BenchmarkSqlStr_GetSql2-8 714324 1643 ns/op 2320 B/op 36 allocs/op
// BenchmarkSqlStr_GetSql2-8 678813 1637 ns/op 2320 B/op 36 allocs/op
// BenchmarkSqlStr_GetSql2-8 705320 1640 ns/op 2320 B/op 36 allocs/op
// BenchmarkSqlStr_GetSql2-8 704493 1642 ns/op 2320 B/op 36 allocs/op
// BenchmarkSqlStr_GetSql2-8 697677 1665 ns/op 2320 B/op 36 allocs/op
}
func testMySQLEscape(v string) string {
var pos = 0
buf := make([]byte, 2*len(v))
for i := 0; i < len(v); i++ {
c := v[i]
switch c {
case '\x00':
buf[pos] = '\\'
buf[pos+1] = '0'
pos += 2
case '\n':
buf[pos] = '\\'
buf[pos+1] = 'n'
pos += 2
case '\r':
buf[pos] = '\\'
buf[pos+1] = 'r'
pos += 2
case '\x1a':
buf[pos] = '\\'
buf[pos+1] = 'Z'
pos += 2
case '\'':
buf[pos] = '\\'
buf[pos+1] = '\''
pos += 2
case '"':
buf[pos] = '\\'
buf[pos+1] = '"'
pos += 2
case '\\':
buf[pos] = '\\'
buf[pos+1] = '\\'
pos += 2
default:
buf[pos] = c
pos++
}
}
return string(buf[:pos])
}
func BenchmarkIntStr(b *testing.B) {
s := int64(56)
a := ""
for i := 0; i < b.N; i++ {
a = strconv.Itoa(int(s))
}
b.Log(a)
}
func BenchmarkIntStr1(b *testing.B) {
s := int64(56)
a := ""
obj := NewCacheSql("")
b.ResetTimer()
for i := 0; i < b.N; i++ {
a = obj.Int2Str(s)
}
b.Log(a)
}
func BenchmarkIntStr3(b *testing.B) {
s := int64(56)
a := ""
b.ResetTimer()
for i := 0; i < b.N; i++ {
a = Str(s)
}
b.Log(a)
}