-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditions.go
204 lines (161 loc) · 5.03 KB
/
conditions.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
package g
import (
"container/list"
"gorm.io/gorm"
)
/*
@File: conditions.go
@Author: khaosles
@Time: 2023/6/11 19:53
@Desc: 条件查询 支持 and or select join group 调用该结构体To方法生成带查询条件的db
*/
type Conditions struct {
queue *list.List
}
type condition struct {
query string
join int
value []any
}
const (
AND = iota
OR
ORDER
SELECT
GROUP
JOIN
)
func NewConditions() *Conditions {
queue := list.New()
return &Conditions{queue: queue}
}
func (c *Conditions) To(db *gorm.DB) *gorm.DB {
if c.queue.Len() == 0 {
return db
}
for e := c.queue.Front(); e != nil; e = e.Next() {
cd := e.Value.(*condition)
switch cd.join {
case AND:
db = db.Where(cd.query, cd.value...)
case ORDER:
db = db.Order(cd.query)
case SELECT:
db = db.Select(cd.query)
case GROUP:
db = db.Group(cd.query)
case OR:
db = db.Or(cd.query, cd.value...)
case JOIN:
db = db.Joins(cd.query)
}
}
return db
}
// join 连接方式 or and,op 运算符 field 字段 value 字段值
func (c *Conditions) sql(join int, query string, values ...any) *Conditions {
c.queue.PushBack(&condition{join: join, query: query, value: values})
return c
}
func (c *Conditions) Order(fields string) *Conditions {
return c.sql(ORDER, fields)
}
func (c *Conditions) Select(fields string) *Conditions {
return c.sql(SELECT, fields)
}
func (c *Conditions) Group(fields string) *Conditions {
return c.sql(GROUP, fields)
}
func (c *Conditions) Joins(joinCondition string) *Conditions {
return c.sql(JOIN, joinCondition)
}
func (c *Conditions) AndIn(field string, value any) *Conditions {
return c.sql(AND, field+" in (?)", value)
}
func (c *Conditions) AndNotIn(field string, value any) *Conditions {
return c.sql(AND, field+" not in (?)", value)
}
func (c *Conditions) AndEqualTo(field string, value any) *Conditions {
return c.sql(AND, field+" = ?", value)
}
func (c *Conditions) AndLessThan(field string, value any) *Conditions {
return c.sql(AND, field+" < ?", value)
}
func (c *Conditions) AndLessThanOrEqualTo(field string, value any) *Conditions {
return c.sql(AND, field+" <= ?", value)
}
func (c *Conditions) AndGreaterThan(field string, value any) *Conditions {
return c.sql(AND, field+" > ?", value)
}
func (c *Conditions) AndGreaterThanOrEqualTo(field string, value any) *Conditions {
return c.sql(AND, field+" >= ?", value)
}
func (c *Conditions) AndNotEqualTo(field string, value any) *Conditions {
return c.sql(AND, field+" <> ?", value)
}
func (c *Conditions) AndLike(field string, value any) *Conditions {
return c.sql(AND, field+" like ?", value)
}
func (c *Conditions) AndNotLike(field string, value any) *Conditions {
return c.sql(AND, field+" not like ?", value)
}
func (c *Conditions) AndILike(field string, value any) *Conditions {
return c.sql(AND, field+" ilike ?", value)
}
func (c *Conditions) AndBetween(field string, value1, value2 any) *Conditions {
return c.sql(AND, field+" between ? and ?", value1, value2)
}
func (c *Conditions) AndNotBetween(field string, value1, value2 any) *Conditions {
return c.sql(AND, field+" not between ? and ?", value1, value2)
}
func (c *Conditions) AndIsNull(field string) *Conditions {
return c.sql(AND, field+" is null")
}
func (c *Conditions) AndIsNotNull(field string) *Conditions {
return c.sql(AND, field+" is not null")
}
func (c *Conditions) OrIn(field string, value any) *Conditions {
return c.sql(OR, field+" in (?)", value)
}
func (c *Conditions) OtNotIn(field string, value any) *Conditions {
return c.sql(OR, field+" not in (?)", value)
}
func (c *Conditions) OrEqualTo(field string, value any) *Conditions {
return c.sql(OR, field+" = ?", value)
}
func (c *Conditions) OrLessThan(field string, value any) *Conditions {
return c.sql(OR, field+" < ?", value)
}
func (c *Conditions) OrLessThanOrEqualTo(field string, value any) *Conditions {
return c.sql(OR, field+" <= ?", value)
}
func (c *Conditions) OrGreaterThan(field string, value any) *Conditions {
return c.sql(OR, field+" > ?", value)
}
func (c *Conditions) OrGreaterThanOrEqualTo(field string, value any) *Conditions {
return c.sql(OR, field+" >= ?", value)
}
func (c *Conditions) OrNotEqualTo(field string, value any) *Conditions {
return c.sql(OR, field+" <> ?", value)
}
func (c *Conditions) OrLike(field string, value any) *Conditions {
return c.sql(OR, field+" like ?", value)
}
func (c *Conditions) OrNotLike(field string, value any) *Conditions {
return c.sql(OR, field+" not like ?", value)
}
func (c *Conditions) OrILike(field string, value any) *Conditions {
return c.sql(OR, field+" ilike ?", value)
}
func (c *Conditions) OrIsNull(field string) *Conditions {
return c.sql(OR, field+" is null")
}
func (c *Conditions) OrIsNotNull(field string) *Conditions {
return c.sql(OR, field+" is not null")
}
func (c *Conditions) OrBetween(field string, value1, value2 any) *Conditions {
return c.sql(OR, field+" between ? and ?", value1, value2)
}
func (c *Conditions) OrNotBetween(field string, value1, value2 any) *Conditions {
return c.sql(OR, field+" not between ? and ?", value1, value2)
}