forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
198 lines (153 loc) · 5.39 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
package field
import (
"gorm.io/gorm/clause"
)
type String Field
func (field String) Eq(value string) Expr {
return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}}
}
func (field String) Neq(value string) Expr {
return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}}
}
func (field String) Gt(value string) Expr {
return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}}
}
func (field String) Gte(value string) Expr {
return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}}
}
func (field String) Lt(value string) Expr {
return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}}
}
func (field String) Lte(value string) Expr {
return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}}
}
func (field String) Between(left string, right string) Expr {
return field.between([]interface{}{left, right})
}
func (field String) NotBetween(left string, right string) Expr {
return Not(field.Between(left, right))
}
func (field String) In(values ...string) Expr {
return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values)}}
}
func (field String) NotIn(values ...string) Expr {
return expr{e: clause.Not(field.In(values...).expression())}
}
func (field String) Like(value string) Expr {
return expr{e: clause.Like{Column: field.RawExpr(), Value: value}}
}
func (field String) NotLike(value string) Expr {
return expr{e: clause.Not(field.Like(value).expression())}
}
func (field String) Regexp(value string) Expr {
return field.regexp(value)
}
func (field String) NotRegxp(value string) Expr {
return expr{e: clause.Not(field.Regexp(value).expression())}
}
func (field String) Value(value string) AssignExpr {
return field.value(value)
}
func (field String) Zero() AssignExpr {
return field.value("")
}
func (field String) IfNull(value string) Expr {
return field.ifNull(value)
}
// FindInSet 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 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()}}}
}
func (field String) Replace(from, to string) String {
return String{expr{e: clause.Expr{SQL: "REPLACE(?,?,?)", Vars: []interface{}{field.RawExpr(), from, to}}}}
}
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
}
type Bytes String
func (field Bytes) Eq(value []byte) Expr {
return expr{e: clause.Eq{Column: field.RawExpr(), Value: value}}
}
func (field Bytes) Neq(value []byte) Expr {
return expr{e: clause.Neq{Column: field.RawExpr(), Value: value}}
}
func (field Bytes) Gt(value []byte) Expr {
return expr{e: clause.Gt{Column: field.RawExpr(), Value: value}}
}
func (field Bytes) Gte(value []byte) Expr {
return expr{e: clause.Gte{Column: field.RawExpr(), Value: value}}
}
func (field Bytes) Lt(value []byte) Expr {
return expr{e: clause.Lt{Column: field.RawExpr(), Value: value}}
}
func (field Bytes) Lte(value []byte) Expr {
return expr{e: clause.Lte{Column: field.RawExpr(), Value: value}}
}
func (field Bytes) Between(left []byte, right []byte) Expr {
return field.between([]interface{}{left, right})
}
func (field Bytes) NotBetween(left []byte, right []byte) Expr {
return Not(field.Between(left, right))
}
func (field Bytes) In(values ...[]byte) Expr {
return expr{e: clause.IN{Column: field.RawExpr(), Values: field.toSlice(values)}}
}
func (field Bytes) NotIn(values ...[]byte) Expr {
return expr{e: clause.Not(field.In(values...).expression())}
}
func (field Bytes) Like(value string) Expr {
return expr{e: clause.Like{Column: field.RawExpr(), Value: value}}
}
func (field Bytes) NotLike(value string) Expr {
return expr{e: clause.Not(field.Like(value).expression())}
}
func (field Bytes) Regexp(value string) Expr {
return field.regexp(value)
}
func (field Bytes) NotRegxp(value string) Expr {
return Not(field.Regexp(value))
}
func (field Bytes) Value(value []byte) AssignExpr {
return field.value(value)
}
func (field Bytes) Zero() AssignExpr {
return field.value([]byte{})
}
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
}