-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
191 lines (157 loc) · 3.49 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
package neo4j
import (
"github.com/aliworkshop/dbcore"
)
type Query interface {
GetSort() (sort []dbcore.SortItem)
SetBody(body any)
GetBody() (body any)
GetModel() (instance dbcore.Modeler)
SetModelFunc(func() dbcore.Modeler)
SetTransaction(transaction any)
GetTransaction() (transaction any)
SetPageSize(pageSize int)
GetPageSize() int
SetPage(page int)
GetPage() (page int)
GetFilter() map[string]any
SetQuery(query string)
GetQuery() string
GetCountQuery() string
GetParams() map[string]any
GetPrefix() string
WithModelFunc(func() dbcore.Modeler) Query
WithBody(body any) Query
WithPage(page int) Query
WithPageSize(pageSize int) Query
WithSorts(sort ...dbcore.SortItem) Query
WithTransaction(transaction any) Query
WithQuery(query string) Query
WithCountQuery(query string) Query
WithFilter(key string, value any) Query
WithParams(key string, value any) Query
WithPrefix(prefix string) Query
}
type ModelFunc func() dbcore.Modeler
type query struct {
query string
countQuery string
params map[string]any
modelFunc ModelFunc
transaction any
pageSize int
page int
sortItem []dbcore.SortItem
body any
filters map[string]any
prefix string
}
func NewQuery() Query {
return &query{
params: make(map[string]any),
filters: make(map[string]any),
}
}
func (q *query) GetSort() (sort []dbcore.SortItem) {
return q.sortItem
}
func (q *query) SetBody(body any) {
q.body = body
}
func (q *query) GetBody() (body any) {
return q.body
}
func (q *query) GetModel() (instance dbcore.Modeler) {
return q.modelFunc()
}
func (q *query) SetModelFunc(f func() dbcore.Modeler) {
q.modelFunc = f
}
func (q *query) SetTransaction(transaction any) {
q.transaction = transaction
}
func (q *query) GetTransaction() (transaction any) {
return q.transaction
}
func (q *query) SetPageSize(pageSize int) {
q.pageSize = pageSize
}
func (q *query) GetPageSize() int {
if q.pageSize == 0 {
q.pageSize = 10
}
return q.pageSize
}
func (q *query) SetPage(page int) {
q.page = page
}
func (q *query) GetPage() (page int) {
if q.page == 0 {
q.page = 1
}
return q.page
}
func (q *query) GetFilter() map[string]any {
return q.filters
}
func (q *query) SetQuery(query string) {
q.query = query
}
func (q *query) GetQuery() string {
return q.query
}
func (q *query) GetCountQuery() string {
return q.countQuery
}
func (q *query) GetParams() map[string]any {
return q.params
}
func (q *query) GetPrefix() string {
return q.prefix
}
func (q *query) WithModelFunc(f func() dbcore.Modeler) Query {
q.modelFunc = f
return q
}
func (q *query) WithBody(body any) Query {
q.body = body
return q
}
func (q *query) WithPage(page int) Query {
q.page = page
return q
}
func (q *query) WithPageSize(pageSize int) Query {
q.pageSize = pageSize
return q
}
func (q *query) WithSorts(sort ...dbcore.SortItem) Query {
for _, s := range sort {
q.sortItem = append(q.sortItem, dbcore.SortItem{Field: s.Field, Order: s.Order})
}
return q
}
func (q *query) WithTransaction(transaction any) Query {
q.transaction = transaction
return q
}
func (q *query) WithQuery(query string) Query {
q.query = query
return q
}
func (q *query) WithCountQuery(query string) Query {
q.countQuery = query
return q
}
func (q *query) WithFilter(key string, value any) Query {
q.filters[key] = value
return q
}
func (q *query) WithParams(key string, value any) Query {
q.params[key] = value
return q
}
func (q *query) WithPrefix(prefix string) Query {
q.prefix = prefix
return q
}