-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
99 lines (84 loc) · 2.09 KB
/
func.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
package sqlr
import (
"fmt"
"strings"
)
// Update 直接构建更新语句
func Update(table string, data *R, scope ...*R) (string, []any) {
table = fmt.Sprintf("`%s`", table)
var bs strings.Builder
bs.WriteString("UPDATE ")
bs.WriteString(table)
bs.WriteString(" SET ")
bs.WriteString(data.Update())
if len(scope) > 0 {
bs.WriteString(" WHERE ")
bs.WriteString(scope[0].Where())
var all []any
all = append(all, data.v...)
all = append(all, scope[0].v...)
return bs.String(), all
}
return bs.String(), data.V()
}
// Insert 直接构建Insert语句
func Insert(table string, data *R) (string, []any) {
table = fmt.Sprintf("`%s`", table)
var bs strings.Builder
bs.WriteString("INSERT INTO ")
bs.WriteString(table)
bs.WriteString(data.Insert())
return bs.String(), data.V()
}
// InsertBatch 批量插入 ⚠️未做异常判断,务必保证 columns的列数和rows[n]的列数相等
func InsertBatch(table string, columns []string, rows [][]any) (string, []any) {
table = fmt.Sprintf("`%s`", table)
var bs strings.Builder
var ss strings.Builder
var vs []any
bs.WriteString("INSERT INTO ")
bs.WriteString(table)
var lc = len(columns)
bs.WriteString("(`")
bs.WriteString(columns[0])
ss.WriteString("(?")
for i := 1; i < lc; i++ {
bs.WriteString("`,`")
bs.WriteString(columns[i])
ss.WriteString(" ,?")
}
bs.WriteString("`) VALUES ")
ss.WriteString(")")
var lr = len(rows)
var sss = ss.String()
if lr > 0 {
bs.WriteString(sss)
vs = append(vs, rows[0]...)
for i := 1; i < lr; i++ {
bs.WriteString(",")
bs.WriteString(sss)
vs = append(vs, rows[i]...)
}
}
return bs.String(), vs
}
// In 直接构建In语句模版
func In(filed string, count int) string {
var sb strings.Builder
sb.WriteString(filed)
sb.WriteString(" IN (?")
for i := 1; i < count; i++ {
sb.WriteString(",?")
}
sb.WriteString(")")
return sb.String()
}
// Join 把多段sql使用string builder连接起来
func Join(parts ...string) string {
var bs strings.Builder
for i := 0; i < len(parts); i++ {
bs.WriteString(parts[i])
bs.WriteString(" ")
}
return bs.String()[:bs.Len()-1]
}