/
query.go
205 lines (171 loc) · 4.39 KB
/
query.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
package teadb
import (
"github.com/TeaWeb/code/teaconfigs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"time"
)
type Query struct {
table string
offset int
size int
operandMap OperandMap // field => operands
sortFields []*SortField
debug bool
timeout time.Duration
resultFields []string
}
func NewQuery(table string) *Query {
query := &Query{
table: table,
}
query.Init()
return query
}
func (this *Query) Init() *Query {
this.offset = -1
this.size = -1
this.operandMap = OperandMap{}
return this
}
func (this *Query) Table(table string) *Query {
this.table = table
return this
}
func (this *Query) Debug() *Query {
this.debug = true
return this
}
func (this *Query) Timeout(timeout time.Duration) *Query {
this.timeout = timeout
return this
}
func (this *Query) Result(field ...string) *Query {
this.resultFields = append(this.resultFields, field...)
return this
}
func (this *Query) Attr(field string, value interface{}) *Query {
if types.IsSlice(value) {
return this.Op(field, OperandIn, value)
} else {
return this.Op(field, OperandEq, value)
}
}
func (this *Query) Op(field string, operandCode OperandCode, value interface{}) *Query {
operands, ok := this.operandMap[field]
if ok {
this.operandMap[field] = append(operands, NewOperand(operandCode, value))
} else {
this.operandMap[field] = []*Operand{NewOperand(operandCode, value)}
}
return this
}
func (this *Query) Or(fieldValues []OperandMap) *Query {
return this.Op("", OperandOr, fieldValues)
}
func (this *Query) Not(field string, value interface{}) *Query {
if types.IsSlice(value) {
return this.Op(field, OperandNotIn, value)
} else {
return this.Op(field, OperandEq, value)
}
}
func (this *Query) Lt(field string, value interface{}) *Query {
return this.Op(field, OperandLt, value)
}
func (this *Query) Lte(field string, value interface{}) *Query {
return this.Op(field, OperandLte, value)
}
func (this *Query) Gt(field string, value interface{}) *Query {
return this.Op(field, OperandGt, value)
}
func (this *Query) Gte(field string, value interface{}) *Query {
return this.Op(field, OperandGte, value)
}
func (this *Query) Asc(field string) *Query {
if this.hasSortField(field) {
this.removeSortField(field)
}
this.sortFields = append(this.sortFields, &SortField{
Name: field,
Type: SortAsc,
})
return this
}
func (this *Query) Desc(field string) *Query {
if this.hasSortField(field) {
this.removeSortField(field)
}
this.sortFields = append(this.sortFields, &SortField{
Name: field,
Type: SortDesc,
})
return this
}
func (this *Query) Offset(offset int) *Query {
this.offset = offset
return this
}
func (this *Query) Limit(size int) *Query {
this.size = size
return this
}
func (this *Query) Node() *Query {
node := teaconfigs.SharedNodeConfig()
if node != nil {
this.Attr("nodeId", node.Id)
} else {
this.Attr("nodeId", "")
}
return this
}
func (this *Query) FindOne(modelPtr interface{}) (interface{}, error) {
return sharedDriver.FindOne(this, modelPtr)
}
func (this *Query) FindOnes(modelPtr interface{}) ([]interface{}, error) {
return sharedDriver.FindOnes(this, modelPtr)
}
func (this *Query) InsertOne(modelPtr interface{}) error {
return sharedDriver.InsertOne(this.table, modelPtr)
}
func (this *Query) InsertOnes(modelPtrSlice interface{}) error {
return sharedDriver.InsertOnes(this.table, modelPtrSlice)
}
func (this *Query) Delete() error {
return sharedDriver.DeleteOnes(this)
}
func (this *Query) Count() (int64, error) {
return sharedDriver.Count(this)
}
func (this *Query) Sum(field string) (float64, error) {
return sharedDriver.Sum(this, field)
}
func (this *Query) Min(field string) (float64, error) {
return sharedDriver.Min(this, field)
}
func (this *Query) Max(field string) (float64, error) {
return sharedDriver.Max(this, field)
}
func (this *Query) Avg(field string) (float64, error) {
return sharedDriver.Avg(this, field)
}
func (this *Query) Group(field string, result map[string]Expr) ([]maps.Map, error) {
return sharedDriver.Group(this, field, result)
}
func (this *Query) hasSortField(field string) bool {
for _, f := range this.sortFields {
if f.Name == field {
return true
}
}
return false
}
func (this *Query) removeSortField(field string) {
result := []*SortField{}
for _, f := range this.sortFields {
if f.Name != field {
result = append(result, f)
}
}
this.sortFields = result
}