forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
333 lines (277 loc) · 8.79 KB
/
export.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
package field
import (
"fmt"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var (
// Star a symbol of "*"
Star = NewAsterisk("")
// ALL same with Star
ALL = Star
)
// Option field option
type Option func(clause.Column) clause.Column
var (
banColumnRaw Option = func(col clause.Column) clause.Column {
col.Raw = false
return col
}
)
// ======================== generic field =======================
// NewField create new field
func NewField(table, column string, opts ...Option) Field {
return Field{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewSerializer create new field2
func NewSerializer(table, column string, opts ...Option) Serializer {
return Serializer{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewAsterisk create new * field
func NewAsterisk(table string, opts ...Option) Asterisk {
return Asterisk{asteriskExpr: asteriskExpr{expr{col: toColumn(table, "*", opts...)}}}
}
// ======================== integer =======================
// NewInt create new Int
func NewInt(table, column string, opts ...Option) Int {
return Int{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewInt8 create new Int8
func NewInt8(table, column string, opts ...Option) Int8 {
return Int8{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewInt16 ...
func NewInt16(table, column string, opts ...Option) Int16 {
return Int16{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewInt32 ...
func NewInt32(table, column string, opts ...Option) Int32 {
return Int32{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewInt64 ...
func NewInt64(table, column string, opts ...Option) Int64 {
return Int64{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewUint ...
func NewUint(table, column string, opts ...Option) Uint {
return Uint{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewUint8 ...
func NewUint8(table, column string, opts ...Option) Uint8 {
return Uint8{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewUint16 ...
func NewUint16(table, column string, opts ...Option) Uint16 {
return Uint16{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewUint32 ...
func NewUint32(table, column string, opts ...Option) Uint32 {
return Uint32{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewUint64 ...
func NewUint64(table, column string, opts ...Option) Uint64 {
return Uint64{expr: expr{col: toColumn(table, column, opts...)}}
}
// ======================== float =======================
// NewFloat32 ...
func NewFloat32(table, column string, opts ...Option) Float32 {
return Float32{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewFloat64 ...
func NewFloat64(table, column string, opts ...Option) Float64 {
return Float64{expr: expr{col: toColumn(table, column, opts...)}}
}
// ======================== string =======================
// NewString ...
func NewString(table, column string, opts ...Option) String {
return String{expr: expr{col: toColumn(table, column, opts...)}}
}
// NewBytes ...
func NewBytes(table, column string, opts ...Option) Bytes {
return Bytes{expr: expr{col: toColumn(table, column, opts...)}}
}
// ======================== bool =======================
// NewBool ...
func NewBool(table, column string, opts ...Option) Bool {
return Bool{expr: expr{col: toColumn(table, column, opts...)}}
}
// ======================== time =======================
// NewTime ...
func NewTime(table, column string, opts ...Option) Time {
return Time{expr: expr{col: toColumn(table, column, opts...)}}
}
func toColumn(table, column string, opts ...Option) clause.Column {
col := clause.Column{Table: table, Name: column}
for _, opt := range opts {
col = opt(col)
}
return banColumnRaw(col)
}
// ======================== boolean operate ========================
// Or return or condition
func Or(exprs ...Expr) Expr {
return &expr{e: clause.Or(toExpression(exprs...)...)}
}
// And return and condition
func And(exprs ...Expr) Expr {
return &expr{e: clause.And(toExpression(exprs...)...)}
}
// Not return not condition
func Not(exprs ...Expr) Expr {
return &expr{e: clause.Not(toExpression(exprs...)...)}
}
func toExpression(conds ...Expr) []clause.Expression {
exprs := make([]clause.Expression, len(conds))
for i, cond := range conds {
exprs[i] = cond.expression()
}
return exprs
}
// ======================== subquery method ========================
// ContainsSubQuery return contains subquery
// when len(columns) == 1, equal to columns[0] IN (subquery)
// when len(columns) > 1, equal to (columns[0], columns[1], ...) IN (subquery)
func ContainsSubQuery(columns []Expr, subQuery *gorm.DB) Expr {
switch len(columns) {
case 0:
return expr{e: clause.Expr{}}
case 1:
return expr{e: clause.Expr{
SQL: "? IN (?)",
Vars: []interface{}{columns[0].RawExpr(), subQuery},
}}
default: // len(columns) > 0
placeholders := make([]string, len(columns))
cols := make([]interface{}, len(columns))
for i, c := range columns {
placeholders[i], cols[i] = "?", c.RawExpr()
}
return expr{e: clause.Expr{
SQL: fmt.Sprintf("(%s) IN (?)", strings.Join(placeholders, ",")),
Vars: append(cols, subQuery),
}}
}
}
// AssignSubQuery assign with subquery
func AssignSubQuery(columns []Expr, subQuery *gorm.DB) AssignExpr {
cols := make([]string, len(columns))
for i, c := range columns {
cols[i] = string(c.BuildColumn(subQuery.Statement))
}
name := cols[0]
if len(cols) > 1 {
name = "(" + strings.Join(cols, ",") + ")"
}
return expr{e: clause.Set{{
Column: clause.Column{Name: name, Raw: true},
Value: gorm.Expr("(?)", subQuery),
}}}
}
// CompareOperator compare operator
type CompareOperator string
const (
// EqOp =
EqOp CompareOperator = " = "
// NeqOp <>
NeqOp CompareOperator = " <> "
// GtOp >
GtOp CompareOperator = " > "
// GteOp >=
GteOp CompareOperator = " >= "
// LtOp <
LtOp CompareOperator = " < "
// LteOp <=
LteOp CompareOperator = " <= "
// ExistsOp EXISTS
ExistsOp CompareOperator = "EXISTS "
)
// CompareSubQuery compare with sub query
func CompareSubQuery(op CompareOperator, column Expr, subQuery *gorm.DB) Expr {
if op == ExistsOp {
return expr{e: clause.Expr{
SQL: fmt.Sprint(op, "(?)"),
Vars: []interface{}{subQuery},
}}
}
return expr{e: clause.Expr{
SQL: fmt.Sprint("?", op, "(?)"),
Vars: []interface{}{column.RawExpr(), subQuery},
}}
}
// Value ...
type Value interface {
expr() clause.Expr
// implement Condition
BeCond() interface{}
CondError() error
}
type val clause.Expr
func (v val) expr() clause.Expr { return clause.Expr(v) }
func (v val) BeCond() interface{} { return v }
func (val) CondError() error { return nil }
// Values convert value to expression which implement Value
func Values(value interface{}) Value {
return val(clause.Expr{
SQL: "?",
Vars: []interface{}{value},
WithoutParentheses: true,
})
}
// ContainsValue return expression which compare with value
func ContainsValue(columns []Expr, value Value) Expr {
switch len(columns) {
case 0:
return expr{e: clause.Expr{}}
case 1:
return expr{e: clause.Expr{
SQL: "? IN (?)",
Vars: []interface{}{columns[0].RawExpr(), value.expr()},
}}
default: // len(columns) > 0
vars := make([]string, len(columns))
queryCols := make([]interface{}, len(columns))
for i, c := range columns {
vars[i], queryCols[i] = "?", c.RawExpr()
}
return expr{e: clause.Expr{
SQL: fmt.Sprintf("(%s) IN (?)", strings.Join(vars, ", ")),
Vars: append(queryCols, value.expr()),
}}
}
}
// EmptyExpr return a empty expression
func EmptyExpr() Expr { return expr{e: clause.Expr{}} }
// AssociationFields all association
var AssociationFields Expr = NewString("", clause.Associations).appendBuildOpts(WithoutQuote)
// Associations ...
var Associations RelationField = NewRelation(clause.Associations, "")
// NewRelation return a new Relation for association
func NewRelation(fieldName string, fieldType string, relations ...Relation) *Relation {
return &Relation{
fieldName: fieldName,
fieldPath: fieldName,
fieldType: fieldType,
childRelations: wrapPath(fieldName, relations),
}
}
// NewRelationWithType return a Relation with specified field type
func NewRelationWithType(relationship RelationshipType, fieldName string, fieldType string, relations ...Relation) *Relation {
return &Relation{
relationship: relationship,
fieldName: fieldName,
fieldType: fieldType,
fieldPath: fieldName,
childRelations: wrapPath(fieldName, relations),
}
}
// NewRelationWithModel return a Relation with specified model struct
func NewRelationWithModel(relationship RelationshipType, fieldName string, fieldType string, fieldModel interface{}, relations ...Relation) *Relation {
return &Relation{
relationship: relationship,
fieldName: fieldName,
fieldType: fieldType,
fieldPath: fieldName,
fieldModel: fieldModel,
}
}