-
Notifications
You must be signed in to change notification settings - Fork 2
/
op.go
166 lines (162 loc) · 3.15 KB
/
op.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
package sq
import (
xerr "github.com/goclub/error"
"reflect"
)
type OP struct {
Query string
Values []interface{}
Symbol string
Placeholder string
Multiple []OP
OrGroup []Condition
Ignore bool
}
func (op OP) sql(column Column, values *[]interface{}) string {
var and stringQueue
if len(op.OrGroup) != 0 {
raw := conditions(op.OrGroup).coreSQL("OR")
if raw.Query == "" {
return ""
}
raw.Query = "(" + raw.Query + ")"
*values = append(*values, raw.Values...)
return raw.Query
} else if len(op.Multiple) != 0 {
for _, subOP := range op.Multiple {
and.Push(subOP.sql(column, values))
}
} else {
if len(op.Query) != 0 {
and.Push(op.Query)
*values = append(*values, op.Values...)
} else {
and.Push(column.wrapField())
and.Push(op.Symbol)
if len(op.Placeholder) != 0 {
and.Push(op.Placeholder)
} else {
and.Push(sqlPlaceholder)
}
*values = append(*values, op.Values...)
}
}
return and.Join(" ")
}
func Equal(v interface{}) OP {
return OP{
Symbol: "=",
Values: []interface{}{v},
}
}
func NotEqual(v interface{}) OP {
return OP{
Symbol: "<>",
Values: []interface{}{v},
}
}
func SubQuery(symbol string, qb QB) OP {
raw := qb.SQLSelect()
query, values := raw.Query, raw.Values
return OP{
Placeholder: "(" + query + ")",
Symbol: symbol,
Values: values,
}
}
func Like(s string) OP {
return OP{
Symbol: "LIKE",
Values: []interface{}{"%" + s + "%"},
}
}
func In(slice interface{}) OP {
var placeholder string
var values []interface{}
rValue := reflect.ValueOf(slice)
if rValue.Type().Kind() != reflect.Slice {
panic(xerr.New("sq.In(" + rValue.Type().Name() + ") slice must be slice"))
}
if rValue.Len() == 0 {
placeholder = "(NULL)"
} else {
var placeholderList stringQueue
for i := 0; i < rValue.Len(); i++ {
values = append(values, rValue.Index(i).Interface())
placeholderList.Push(sqlPlaceholder)
}
placeholder = "(" + placeholderList.Join(",") + ")"
}
return OP{
Symbol: "IN",
Values: values,
Placeholder: placeholder,
}
}
func LikeLeft(s string) OP {
return OP{
Symbol: "LIKE",
Values: []interface{}{s + "%"},
}
}
func LikeRight(s string) OP {
return OP{
Symbol: "LIKE",
Values: []interface{}{"%" + s},
}
}
func Between(begin interface{}, end interface{}) OP {
return OP{
Symbol: "BETWEEN",
Values: []interface{}{begin, end},
Placeholder: `? AND ?`,
}
}
func NotBetween(begin interface{}, end interface{}) OP {
return OP{
Symbol: "NOT BETWEEN",
Values: []interface{}{begin, end},
Placeholder: `? AND ?`,
}
}
func GT(v interface{}) OP {
return OP{
Symbol: ">",
Values: []interface{}{v},
}
}
func GTE(v interface{}) OP {
return OP{
Symbol: ">=",
Values: []interface{}{v},
}
}
func LT(v interface{}) OP {
return OP{
Symbol: "<",
Values: []interface{}{v},
}
}
func LTE(v interface{}) OP {
return OP{
Symbol: "<=",
Values: []interface{}{v},
}
}
func IsNull() OP {
return OP{
Symbol: "IS NULL",
Values: nil,
}
}
func Multiple(ops []OP) OP {
return OP{
Multiple: ops,
}
}
func IF(condition bool, op OP) OP {
if condition == false {
op.Ignore = true
}
return op
}