/
Condition.io
110 lines (91 loc) · 2.53 KB
/
Condition.io
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
ConditionError := Exception clone
parseSimpleCondition := method(msg, context,
if(context isNil,
context = thisContext
)
one := msg clone setNext(nil) asString
field := Iorm Condition Field with(one)
op := msg next name
two := msg next argAt(0) doInContext(context)
value := Iorm Condition Value with(two)
node := op switch(
"==",
Iorm Condition Equals with(field, value),
"!=",
Iorm Condition Differs with(field, value),
">",
Iorm Condition GreaterThan with(field, value),
"<",
Iorm Condition LessThan with(field, value)
)
if(node isNil,
ConditionError raise("No appropriate SQL operator found for '#{ op }'" interpolate)
)
node
)
parseSimple := method(
msg := call message argAt(0)
context := call message argAt(1) ifNilEval(thisContext)
parseSimpleCondition(msg, context)
)
Condition := Object clone do(
children ::= nil
init := method(
children = list()
resend
)
getAsSQL := method(session,
children map(getAsSQL(session)) join(" AND ") # right?
)
addChild := method(child,
children append(child)
self
)
addFilterCondition := method(condition,
addChild(condition) # if we use AND to join the conditions, that's ok
self
)
filter := method(
# for the lazy ones
addFilterCondition(Iorm parseSimpleCondition(call message argAt(0)))
self
)
with := method(
c := self clone
call evalArgs foreach(child, c addChild(child))
c
)
Value := clone do(
value ::= nil
getAsSQL := method(session,
session quote(value asString asSymbol)
)
with := method(value,
c := self clone
c setValue(value)
c
)
)
Field := clone do(
name ::= nil
getAsSQL := method(session,
name
)
with := method(name,
c := self clone
c setName(name)
c
)
)
BinaryOperator := clone do(
operator ::= nil
getAsSQL := method(session,
children map(getAsSQL(session)) join(" #{ operator } " interpolate)
)
)
Equals := BinaryOperator clone setOperator("=")
Differs := BinaryOperator clone setOperator("!=")
GreaterThan := BinaryOperator clone setOperator(">")
LessThan := BinaryOperator clone setOperator("<")
And := BinaryOperator clone setOperator("AND")
)