-
Notifications
You must be signed in to change notification settings - Fork 1
/
xcondition.go
180 lines (160 loc) · 4.15 KB
/
xcondition.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
package xdominion
import (
"fmt"
"strings"
)
const (
OP_Equal = "="
OP_NotEqual = "!="
OP_Inferior = "<="
OP_StrictInferior = "<"
OP_Superior = ">="
OP_StrictSuperior = ">"
OP_Between = "between"
OP_In = "in"
OP_NotIn = "not in"
OP_Like = "like"
OP_NotLike = "not like"
OP_iLike = "ilike"
OP_NotiLike = "not ilike"
)
/*
The XConditions is a colection of XCondition
*/
type XConditions []XCondition
// =====================
// XConditions
// =====================
func (c *XConditions) CreateConditions(table *XTable, DB string, baseindex int) (string, []interface{}) {
cond := ""
data := []interface{}{}
for _, xc := range *c {
scond, sdata, indexused := xc.GetCondition(table, DB, baseindex)
cond += scond
if indexused {
data = append(data, sdata)
baseindex++
}
}
return cond, data
}
func (c *XConditions) Clone() XConditions {
nc := XConditions{}
for _, xc := range *c {
nxc := xc.Clone()
nc = append(nc, nxc)
}
return nc
}
/*
The XCondition structure
*/
type XCondition struct {
Field string
Operator string
Limit interface{}
LimitExtra interface{}
OperatorGlobal string
AtomOpen int
AtomClose int
}
// =====================
// XCondition
// =====================
func NewXCondition(field string, operator string, limit interface{}, args ...interface{}) XCondition {
c := XCondition{Field: field, Operator: operator, Limit: limit}
for i, p := range args {
if i == 0 {
c.OperatorGlobal = p.(string)
}
if i == 1 {
c.AtomOpen = p.(int)
}
if i == 2 {
c.AtomClose = p.(int)
}
if i == 3 {
c.LimitExtra = p
}
}
return c
}
func (c *XCondition) GetCondition(table *XTable, DB string, baseindex int) (string, interface{}, bool) {
field := table.GetField(c.Field)
if field == nil {
return "", "", false
}
cond := ""
if len(c.OperatorGlobal) > 0 {
cond += " " + c.OperatorGlobal + " "
}
if c.AtomOpen > 0 {
cond += strings.Repeat("(", c.AtomOpen)
}
indexused := true
var value interface{} = nil
switch c.Operator {
case OP_Equal:
if c.Limit == nil {
cond += table.Prepend + field.GetName() + " is null"
indexused = false
} else {
value = c.Limit
cond += table.Prepend + field.GetName() + OP_Equal + getQueryString(DB, baseindex)
}
case OP_NotEqual:
if c.Limit == nil {
cond += table.Prepend + field.GetName() + " is not null"
indexused = false
} else {
value = c.Limit
cond += table.Prepend + field.GetName() + OP_NotEqual + getQueryString(DB, baseindex)
}
case OP_Superior, OP_StrictSuperior, OP_Inferior, OP_StrictInferior:
value = c.Limit
cond += table.Prepend + field.GetName() + c.Operator + getQueryString(DB, baseindex)
case OP_In, OP_NotIn:
cond += table.Prepend + field.GetName() + " " + c.Operator + " " + c.Limit.(string)
indexused = false
case OP_Like:
value = fmt.Sprint(c.Limit)
cond += table.Prepend + field.GetName() + " like " + getQueryString(DB, baseindex)
case OP_NotLike:
value = fmt.Sprint(c.Limit)
cond += table.Prepend + field.GetName() + " not like " + getQueryString(DB, baseindex)
case OP_iLike:
value = fmt.Sprint(c.Limit)
switch DB {
case DB_Postgres:
cond += table.Prepend + field.GetName() + " ilike " + getQueryString(DB, baseindex)
case DB_MySQL:
cond += "lower(" + table.Prepend + field.GetName() + ") like lower(" + getQueryString(DB, baseindex) + ")"
}
case OP_NotiLike:
value = fmt.Sprint(c.Limit)
switch DB {
case DB_Postgres:
cond += table.Prepend + field.GetName() + " not ilike " + getQueryString(DB, baseindex)
case DB_MySQL:
cond += "lower(" + table.Prepend + field.GetName() + ") not like lower(" + getQueryString(DB, baseindex) + ")"
}
default:
// warning: operator not supported
}
if c.AtomClose > 0 {
cond += strings.Repeat(")", c.AtomClose)
}
return cond, value, indexused
}
func (c *XCondition) Clone() XCondition {
nc := XCondition{
Field: c.Field,
Operator: c.Operator,
Limit: c.Limit,
LimitExtra: c.LimitExtra,
OperatorGlobal: c.OperatorGlobal,
AtomOpen: c.AtomOpen,
AtomClose: c.AtomClose,
}
return nc
}