forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
236 lines (191 loc) · 6.02 KB
/
string.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
package field
import (
"gorm.io/gorm/clause"
)
// String string type field
type String Field
// Eq equal to
func (field String) Eq(value string) Expr {
return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}}
}
// Neq not equal to
func (field String) Neq(value string) Expr {
return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}}
}
// Gt greater than
func (field String) Gt(value string) Expr {
return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}}
}
// Gte greater or equal to
func (field String) Gte(value string) Expr {
return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}}
}
// Lt less than
func (field String) Lt(value string) Expr {
return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}}
}
// Lte less or equal to
func (field String) Lte(value string) Expr {
return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}}
}
// Between ...
func (field String) Between(left string, right string) Expr {
return field.between([]interface{}{left, right})
}
// NotBetween ...
func (field String) NotBetween(left string, right string) Expr {
return Not(field.Between(left, right))
}
// In ...
func (field String) In(values ...string) Expr {
return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values)}}
}
// NotIn ...
func (field String) NotIn(values ...string) Expr {
return expr{e: clause.Not(field.In(values...).expression())}
}
// Like ...
func (field String) Like(value string) Expr {
return expr{e: clause.Like{Column: field.RawExpr(), Value: value}}
}
// NotLike ...
func (field String) NotLike(value string) Expr {
return expr{e: clause.Not(field.Like(value).expression())}
}
// Regexp ...
func (field String) Regexp(value string) Expr {
return field.regexp(value)
}
// NotRegxp ...
func (field String) NotRegxp(value string) Expr {
return expr{e: clause.Not(field.Regexp(value).expression())}
}
// Value ...
func (field String) Value(value string) AssignExpr {
return field.value(value)
}
// Zero ...
func (field String) Zero() AssignExpr {
return field.value("")
}
// IfNull ...
func (field String) IfNull(value string) Expr {
return field.ifNull(value)
}
// FindInSet equal to FIND_IN_SET(field_name, input_string_list)
func (field String) FindInSet(targetList string) Expr {
return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{field.RawExpr(), targetList}}}
}
// FindInSetWith equal to FIND_IN_SET(input_string, field_name)
func (field String) FindInSetWith(target string) Expr {
return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{target, field.RawExpr()}}}
}
// Replace ...
func (field String) Replace(from, to string) String {
return String{expr{e: clause.Expr{SQL: "REPLACE(?,?,?)", Vars: []interface{}{field.RawExpr(), from, to}}}}
}
// Concat ...
func (field String) Concat(before, after string) String {
switch {
case before != "" && after != "":
return String{expr{e: clause.Expr{SQL: "CONCAT(?,?,?)", Vars: []interface{}{before, field.RawExpr(), after}}}}
case before != "":
return String{expr{e: clause.Expr{SQL: "CONCAT(?,?)", Vars: []interface{}{before, field.RawExpr()}}}}
case after != "":
return String{expr{e: clause.Expr{SQL: "CONCAT(?,?)", Vars: []interface{}{field.RawExpr(), after}}}}
default:
return field
}
}
func (field String) toSlice(values []string) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
slice[i] = v
}
return slice
}
// Bytes []byte type field
type Bytes String
// Eq equal to
func (field Bytes) Eq(value []byte) Expr {
return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}}
}
// Neq not equal to
func (field Bytes) Neq(value []byte) Expr {
return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}}
}
// Gt greater than
func (field Bytes) Gt(value []byte) Expr {
return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}}
}
// Gte greater or equal to
func (field Bytes) Gte(value []byte) Expr {
return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}}
}
// Lt less than
func (field Bytes) Lt(value []byte) Expr {
return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}}
}
// Lte less or equal to
func (field Bytes) Lte(value []byte) Expr {
return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}}
}
// Between ...
func (field Bytes) Between(left []byte, right []byte) Expr {
return field.between([]interface{}{left, right})
}
// NotBetween ...
func (field Bytes) NotBetween(left []byte, right []byte) Expr {
return Not(field.Between(left, right))
}
// In ...
func (field Bytes) In(values ...[]byte) Expr {
return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values)}}
}
// NotIn ...
func (field Bytes) NotIn(values ...[]byte) Expr {
return expr{e: clause.Not(field.In(values...).expression())}
}
// Like ...
func (field Bytes) Like(value string) Expr {
return expr{e: clause.Like{Column: field.RawExpr(), Value: value}}
}
// NotLike ...
func (field Bytes) NotLike(value string) Expr {
return expr{e: clause.Not(field.Like(value).expression())}
}
// Regexp ...
func (field Bytes) Regexp(value string) Expr {
return field.regexp(value)
}
// NotRegxp ...
func (field Bytes) NotRegxp(value string) Expr {
return Not(field.Regexp(value))
}
// Value ...
func (field Bytes) Value(value []byte) AssignExpr {
return field.value(value)
}
// Zero ...
func (field Bytes) Zero() AssignExpr {
return field.value([]byte{})
}
// IfNull ...
func (field Bytes) IfNull(value []byte) Expr {
return field.ifNull(value)
}
// FindInSet FIND_IN_SET(field_name, input_string_list)
func (field Bytes) FindInSet(targetList string) Expr {
return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{field.RawExpr(), targetList}}}
}
// FindInSetWith FIND_IN_SET(input_string, field_name)
func (field Bytes) FindInSetWith(target string) Expr {
return expr{e: clause.Expr{SQL: "FIND_IN_SET(?,?)", Vars: []interface{}{target, field.RawExpr()}}}
}
func (field Bytes) toSlice(values [][]byte) []interface{} {
slice := make([]interface{}, len(values))
for i, v := range values {
slice[i] = v
}
return slice
}