-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
184 lines (158 loc) · 3.02 KB
/
helper.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
package mysql
import (
"strconv"
"strings"
)
type WhereInterface interface {
Add(field string, value interface{})
Sql() string
Bind() []interface{}
}
type InInterface interface {
Add(field string, value interface{})
Sql() string
Bind() []interface{}
}
type SorterInterface interface {
Add(SorterElement)
Sql() string
}
type PagerInterface interface {
Add(PagerElement)
Sql() string
}
type SorterElement interface {
GetField() string
GetReverse() bool
}
type PagerElement interface {
GetSize() int64
GetOffset() int64
}
type Where struct {
condition []string
bind []interface{}
}
func NewWhere() *Where {
return &Where{
condition: []string{},
bind: []interface{}{},
}
}
func (this *Where) Add(field string, value interface{}) {
switch value.(type) {
case int,
int8,
int16,
int32,
int64,
uint,
uint8,
uint16,
uint32,
uint64,
float32,
float64,
bool:
// continue
case string:
if value == "" {
return
}
default:
if value == nil {
return
}
}
this.condition = append(this.condition, field+" = ?")
this.bind = append(this.bind, value)
}
func (this *Where) Sql() string {
if len(this.condition) == 0 {
return ""
}
return " WHERE " + strings.Join(this.condition, " AND ")
}
func (this *Where) Bind() []interface{} {
return this.bind
}
type In struct {
fields []string
bind map[string][]interface{}
}
func NewIn() *In {
return &In{
fields: []string{},
bind: map[string][]interface{}{},
}
}
func (this *In) Add(field string, value interface{}) {
if _, ok := this.bind[field]; !ok {
this.bind[field] = []interface{}{}
this.fields = append(this.fields, field)
}
this.bind[field] = append(this.bind[field], value)
}
func (this *In) Sql() string {
sqls := []string{}
if len(this.fields) == 0 {
return ""
}
for _, field := range this.fields {
values := this.bind[field]
sqls = append(sqls,
field+" IN ("+strings.TrimSuffix(strings.Repeat("?,", len(values)), ",")+")")
}
return " WHERE " + strings.Join(sqls, " AND ")
}
func (this *In) Bind() []interface{} {
bind := []interface{}{}
for _, field := range this.fields {
bind = append(bind, this.bind[field]...)
}
return bind
}
type Sorter struct {
sqls []string
}
func NewSorter() *Sorter {
return &Sorter{
sqls: []string{},
}
}
func (this *Sorter) Add(sorter SorterElement) {
sql := sorter.GetField()
if sorter.GetReverse() {
sql += " DESC"
} else {
sql += " ASC"
}
this.sqls = append(this.sqls, sql)
}
func (this *Sorter) Sql() string {
if len(this.sqls) == 0 {
return ""
}
return " ORDER BY " + strings.Join(this.sqls, ", ")
}
type Pager struct {
Size int64 `json:"size"`
Offset int64 `json:"offset"`
}
func NewPager() *Pager {
return &Pager{}
}
func (this *Pager) Add(pager PagerElement) {
this.Size = pager.GetSize()
this.Offset = pager.GetOffset()
}
func (this *Pager) Sql() string {
if this.Size <= 0 {
return ""
}
return " LIMIT " + strconv.FormatInt(this.Size, 10) +
" OFFSET " + strconv.FormatInt(this.Offset, 10)
}
type Count struct {
Total int `json:"total"`
}