-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRelation.go
271 lines (234 loc) · 4.6 KB
/
Relation.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package gouda
import (
"container/vector"
"fmt"
"reflect"
"strings"
)
type RequestKind int
type OperandKind int
type Condition struct {
field string
operand OperandKind
value Value
or vector.Vector
}
const (
NULL OperandKind = iota
EQUAL
NOTEQUAL
ISNOTNULL
ISNULL
GREATER
LOWER
GREATEROREQUAL
LOWEROREQUAL
OR
)
const (
SELECT RequestKind = iota
UPDATE
INSERT
COUNT
DELETE
)
type Relation struct {
conditions vector.Vector
table string
limit_offset int
limit_count int
order_field vector.StringVector
order_direction vector.StringVector
kind RequestKind
attributes vector.StringVector
values map[string]Value
id int
identifier_field string
}
func (r *Relation) Count(fields []string) *Relation {
for _, s := range fields {
r.attributes.Push(s)
}
r.kind = COUNT
return r
}
func (r *Relation) Delete() *Relation {
r.kind = DELETE
return r
}
func (r *Relation) Insert(mp map[string]Value) *Relation {
r.kind = INSERT
r.values = mp
return r
}
func (r *Relation) Update(mp map[string]Value, identifier string, id int) *Relation {
r.kind = UPDATE
r.values = mp
r.id = id
r.identifier_field = identifier
return r
}
func (r *Relation) Where(s *Condition) *Relation {
r.conditions.Push(s)
return r
}
func (r *Relation) Table(t string) *Relation {
r.table = t
return r
}
func (r *Relation) Limit(offset, count int) *Relation {
r.limit_offset = offset
r.limit_count = count
return r
}
func (r *Relation) First() *Relation {
r.Limit(0, 1)
return r
}
func (r *Relation) Order(field, direction string) *Relation {
r.order_field.Push(field)
r.order_direction.Push(direction)
return r
}
func (r *Relation) String() string {
s := " Conditions : \n\t ("
for _, ss := range r.conditions {
s += ss.(*Condition).String()
if ss != r.conditions.Last() {
s += ", "
}
}
s += ") \n"
if r.order_field.Len() > 0 {
s += "Order :\n\t ("
for i, ss := range r.order_field {
s += ss
s += " "
s += r.order_direction[i]
if ss != r.order_field.Last() {
s += ", "
}
}
s += ") \n"
}
if r.limit_count > 0 {
s += "Offset : " + fmt.Sprint(r.limit_offset) + "\n"
s += "Count : " + fmt.Sprint(r.limit_count) + "\n"
}
return "Une relation" + s
}
func From(t interface{}) (r *Relation) { return NewRelation(t) }
func NewRelation(t interface{}) (r *Relation) {
r = new(Relation)
r.kind = SELECT
switch typ := t.(type) {
case string:
r.Table(t.(string))
default:
tab := strings.Split(reflect.Typeof(t).String(), ".", 0)
tablename := strings.ToLower(tab[len(tab)-1])
r.Table(tablename)
}
return
}
/** Condition **/
func F(field string) (c *Condition) {
c = new(Condition)
c.operand = NULL
c.field = strings.ToLower(field)
return
}
func (c *Condition) Value(v interface{}) *Condition {
switch v.(type) {
case int:
c.value = SysInt(v.(int)).Value()
case string:
c.value = SysString(v.(string)).Value()
}
return c
}
func (c *Condition) Eq(v interface{}) *Condition {
c.operand = EQUAL
return c.Value(v)
}
func (c *Condition) NEq(v interface{}) *Condition {
c.operand = NOTEQUAL
return c.Value(v)
}
func (c *Condition) Gt(v interface{}) *Condition {
c.operand = GREATER
return c.Value(v)
}
func (c *Condition) Lt(v interface{}) *Condition {
c.operand = LOWER
return c.Value(v)
}
func (c *Condition) GtEq(v interface{}) *Condition {
c.operand = GREATEROREQUAL
return c.Value(v)
}
func (c *Condition) LtEq(v interface{}) *Condition {
c.operand = LOWEROREQUAL
return c.Value(v)
}
func (c *Condition) IsNotNull() *Condition {
c.operand = ISNOTNULL
return c
}
func (c *Condition) IsNull() *Condition {
c.operand = ISNULL
return c
}
func (c *Condition) Or(c2 *Condition) *Condition {
if c.operand != OR {
orc := new(Condition)
orc.operand = OR
orc.or.Push(c)
c = orc
}
c.or.Push(c2)
return c
}
func (c *Condition) String() string {
if c.operand == OR {
ret := " ( "
for i := range c.or {
ret += c.or.At(i).(*Condition).String()
if i != c.or.Len()-1 {
ret += " ) OR ("
}
}
ret += ")"
return ret
}
ret := c.field
switch c.operand {
case EQUAL:
ret += " = "
case NOTEQUAL:
ret += " != "
case ISNOTNULL:
ret += " IS NOT NULL "
case ISNULL:
ret += " IS NULL "
case LOWER:
ret += " < "
case GREATER:
ret += " > "
case LOWEROREQUAL:
ret += " <= "
case GREATEROREQUAL:
ret += " >= "
default:
ret += " NO SE OP"
}
if c.operand != ISNOTNULL && c.operand != ISNULL {
switch c.value.Kind() {
case IntKind:
ret += fmt.Sprint(int(c.value.Int()))
case StringKind:
ret += fmt.Sprint(string(c.value.String()))
}
}
return ret
}