-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplatefunc.go
More file actions
241 lines (226 loc) · 6.16 KB
/
Copy pathtemplatefunc.go
File metadata and controls
241 lines (226 loc) · 6.16 KB
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
/*
* Copyright (c) 2023.
* all right reserved by gnodux<gnodux@gmail.com>
*/
package sqlmx
import (
"fmt"
"github.com/gnodux/sqlmx/dialect"
. "github.com/gnodux/sqlmx/meta"
"github.com/gnodux/sqlmx/utils"
"reflect"
"strings"
"text/template"
"time"
)
func MakeFuncMap(driver *dialect.Dialect) template.FuncMap {
return template.FuncMap{
"where": func(v any) string { return where(driver, v) },
"namedWhere": func(v any) string { return namedWhere(driver, v) },
"nwhere": func(v any) string { return namedWhere(driver, v) },
//"asc": func(cols []string) string { return orderByMap(driver, expr.SimpleAsc(cols...)) },
//"desc": func(cols []string) string { return orderByMap(driver, expr.SimpleDesc(cols...)) },
"v": func(v any) string { return sqlValue(driver, v) },
"n": driver.SQLNameFunc,
"sqlName": driver.SQLNameFunc,
"list": func(v []any) string { return sqlValues(driver, v) },
"columns": func(v []*Column) string { return columns(driver, v) },
"allColumns": func(v []*Column) string { return allColumns(driver, v) },
"args": func(v []*Column) string { return args(driver, v) },
"setArgs": func(v []*Column) string { return sets(v, driver) },
"orderBy": func(v map[string]string) string { return orderByMap(driver, v) },
"driver": func() string { return driver.Name },
"dialect": func() string {
return driver.Name
},
}
}
func orderByMap(driver *dialect.Dialect, order map[string]string) string {
if len(order) == 0 {
return ""
}
sb := strings.Builder{}
pre := driver.KeywordWithSpace("ORDER BY")
for k, v := range order {
sb.WriteString(pre)
sb.WriteString(driver.SQLNameFunc(driver.NameFunc(k)))
sb.WriteString(" ")
sb.WriteString(v)
pre = ","
}
if sb.Len() > 0 {
sb.WriteString(" ")
}
return sb.String()
}
func namedWhere(driver *dialect.Dialect, v any) string {
return whereWith(driver, v, driver.KeywordWithSpace("AND"), true)
}
func columns(driver *dialect.Dialect, cols []*Column) string {
sb := strings.Builder{}
pre := ""
for _, c := range cols {
if c.Ignore || c.IsPrimaryKey {
continue
}
sb.WriteString(pre)
sb.WriteString(driver.SQLNameFunc(c.ColumnName))
pre = ","
}
return sb.String()
}
func allColumns(driver *dialect.Dialect, cols []*Column) string {
sb := strings.Builder{}
pre := ""
for _, c := range cols {
sb.WriteString(pre)
sb.WriteString(driver.SQLNameFunc(c.ColumnName))
pre = ","
}
return sb.String()
}
func args(driver *dialect.Dialect, cols []*Column) string {
sb := strings.Builder{}
pre := ""
for _, c := range cols {
if c.Ignore || c.IsPrimaryKey {
continue
}
sb.WriteString(pre)
sb.WriteString(driver.NamedPrefix)
sb.WriteString(c.ColumnName)
pre = ","
}
return sb.String()
}
func sets(cols []*Column, driver *dialect.Dialect) string {
sb := &strings.Builder{}
pre := ""
for _, c := range cols {
if c.Ignore || c.IsPrimaryKey {
continue
}
sb.WriteString(pre)
sb.WriteString(driver.SQLNameFunc(c.ColumnName))
sb.WriteString("=" + driver.NamedPrefix)
sb.WriteString(c.ColumnName)
pre = ","
}
return sb.String()
}
func where(driver *dialect.Dialect, v any) string {
return whereWith(driver, v, driver.KeywordWithSpace("AND"), false)
}
func whereOr(driver *dialect.Dialect, v any) string {
return whereWith(driver, v, driver.KeywordWithSpace("OR"), false)
}
func whereWith(driver *dialect.Dialect, arg any, op string, named bool) string {
argv := reflect.ValueOf(arg)
if arg == nil {
return ""
}
if op == "" {
op = driver.KeywordWithSpace("AND")
}
if op[0] != ' ' {
op = " " + op + " "
}
// 预分配一个合理的初始容量
buf := strings.Builder{}
buf.Grow(256)
switch reflect.TypeOf(argv.Interface()).Kind() {
case reflect.Map:
comma := driver.KeywordWithSpace("WHERE")
keys := argv.MapKeys()
for i := 0; i < len(keys); i++ {
k := keys[i]
buf.WriteString(comma)
buf.WriteString(driver.SQLNameFunc(driver.NameFunc(k.String())))
value := argv.MapIndex(k)
valueKind := reflect.TypeOf(value.Interface()).Kind()
if valueKind == reflect.String {
const stringMatchers = "%.?"
if strings.ContainsAny(value.Interface().(string), stringMatchers) {
buf.WriteString(driver.KeywordWithSpace("LIKE"))
} else {
buf.WriteByte('=')
}
} else {
buf.WriteByte('=')
}
if named {
buf.WriteByte(':')
buf.WriteString(k.String())
} else {
buf.WriteString(sqlValue(driver, value.Interface()))
}
comma = op
}
case reflect.Struct:
comma := driver.KeywordWithSpace("WHERE")
typ := argv.Type()
for i := 0; i < argv.NumField(); i++ {
field := argv.Field(i)
if field.IsZero() {
continue
}
buf.WriteString(comma)
buf.WriteString(driver.SQLNameFunc(driver.NameFunc(typ.Field(i).Name)))
buf.WriteByte('=')
buf.WriteString(sqlValue(driver, field.Interface()))
comma = op
}
}
buf.WriteByte(' ')
return buf.String()
}
// sqlValues list of sqlValues
func sqlValues(driver *dialect.Dialect, v any) string {
value := reflect.ValueOf(v)
sb := &strings.Builder{}
// 预分配一个合理的初始容量
sb.Grow(64)
switch value.Kind() {
case reflect.Slice, reflect.Array:
len := value.Len()
if len > 0 {
// 第一个元素不需要逗号
sb.WriteString(sqlValue(driver, value.Index(0).Interface()))
// 后续元素添加逗号
for idx := 1; idx < len; idx++ {
sb.WriteByte(',')
sb.WriteString(sqlValue(driver, value.Index(idx).Interface()))
}
}
default:
sb.WriteString(sqlValue(driver, value.Interface()))
}
return sb.String()
}
// value sql value converter(sql inject process)
func sqlValue(driver *dialect.Dialect, arg any) string {
var ret string
switch a := arg.(type) {
case nil:
ret = driver.Keyword("NULL")
case string:
ret = "'" + utils.Escape(a) + "'"
case *string:
ret = "'" + utils.Escape(*a) + "'"
case time.Time:
ret = a.Format(driver.DateFormat)
case *time.Time:
ret = a.Format(driver.DateFormat)
case bool:
if a {
ret = driver.Keyword("TRUE")
} else {
ret = driver.Keyword("FALSE")
}
case uint, uint16, uint32, uint64, int, int16, int32, int64, float32, float64:
return fmt.Sprintf("%v", a)
default:
ret = fmt.Sprintf("'%v'", a)
}
return ret
}