forked from dasfaha/sky
/
query_condition.go
316 lines (272 loc) · 8.79 KB
/
query_condition.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package skyd
import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
)
//------------------------------------------------------------------------------
//
// Constants
//
//------------------------------------------------------------------------------
const (
QueryConditionUnitSteps = "steps"
QueryConditionUnitSessions = "sessions"
QueryConditionUnitSeconds = "seconds"
)
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A condition step made within a query.
type QueryCondition struct {
query *Query
functionName string
Expression string
WithinRangeStart int
WithinRangeEnd int
WithinUnits string
Steps QueryStepList
}
//------------------------------------------------------------------------------
//
// Constructors
//
//------------------------------------------------------------------------------
// Creates a new condition.
func NewQueryCondition(query *Query) *QueryCondition {
id := query.NextIdentifier()
return &QueryCondition{
query: query,
functionName: fmt.Sprintf("a%d", id),
WithinRangeStart: 0,
WithinRangeEnd: 0,
WithinUnits: QueryConditionUnitSteps,
}
}
//------------------------------------------------------------------------------
//
// Accessors
//
//------------------------------------------------------------------------------
// Retrieves the query this condition is associated with.
func (c *QueryCondition) Query() *Query {
return c.query
}
// Retrieves the function name used during codegen.
func (c *QueryCondition) FunctionName() string {
return c.functionName
}
// Retrieves the merge function name used during codegen.
func (c *QueryCondition) MergeFunctionName() string {
return ""
}
// Retrieves the child steps.
func (c *QueryCondition) GetSteps() QueryStepList {
return c.Steps
}
//------------------------------------------------------------------------------
//
// Methods
//
//------------------------------------------------------------------------------
//--------------------------------------
// Serialization
//--------------------------------------
// Encodes a query condition into an untyped map.
func (c *QueryCondition) Serialize() map[string]interface{} {
return map[string]interface{}{
"type": QueryStepTypeCondition,
"expression": c.Expression,
"within": []int{c.WithinRangeStart, c.WithinRangeEnd},
"withinUnits": c.WithinUnits,
"steps": c.Steps.Serialize(),
}
}
// Decodes a query condition from an untyped map.
func (c *QueryCondition) Deserialize(obj map[string]interface{}) error {
if obj == nil {
return errors.New("skyd.QueryCondition: Unable to deserialize nil.")
}
if obj["type"] != QueryStepTypeCondition {
return fmt.Errorf("skyd.QueryCondition: Invalid step type: %v", obj["type"])
}
// Deserialize "expression".
if expression, ok := obj["expression"].(string); ok {
c.Expression = expression
} else {
if obj["expression"] == nil {
c.Expression = "true"
} else {
return fmt.Errorf("Invalid 'expression': %v", obj["expression"])
}
}
// Deserialize "within" range.
if withinRange, ok := obj["within"].([]interface{}); ok && len(withinRange) == 2 {
if withinRangeStart, ok := withinRange[0].(float64); ok {
c.WithinRangeStart = int(withinRangeStart)
} else {
return fmt.Errorf("skyd.QueryCondition: Invalid 'within' range start: %v", withinRange[0])
}
if withinRangeEnd, ok := withinRange[1].(float64); ok {
c.WithinRangeEnd = int(withinRangeEnd)
} else {
return fmt.Errorf("skyd.QueryCondition: Invalid 'within' range end: %v", withinRange[1])
}
} else {
if obj["within"] == nil {
c.WithinRangeStart = 0
c.WithinRangeEnd = 0
} else {
return fmt.Errorf("Invalid 'within' range: %v", obj["within"])
}
}
// Deserialize "within units".
if withinUnits, ok := obj["withinUnits"].(string); ok {
switch withinUnits {
case QueryConditionUnitSteps, QueryConditionUnitSessions, QueryConditionUnitSeconds:
c.WithinUnits = withinUnits
default:
return fmt.Errorf("Invalid 'within units': %v", withinUnits)
}
} else {
if obj["withinUnits"] == nil {
c.WithinUnits = QueryConditionUnitSteps
} else {
return fmt.Errorf("Invalid 'within units': %v", obj["withinUnits"])
}
}
// Deserialize steps.
var err error
c.Steps, err = DeserializeQueryStepList(obj["steps"], c.query)
if err != nil {
return err
}
return nil
}
//--------------------------------------
// Code Generation
//--------------------------------------
// Generates Lua code for the query.
func (c *QueryCondition) CodegenAggregateFunction() (string, error) {
buffer := new(bytes.Buffer)
// Validate.
if c.WithinRangeStart > c.WithinRangeEnd {
return "", fmt.Errorf("skyd.QueryCondition: Invalid 'within' range: %d..%d", c.WithinRangeStart, c.WithinRangeEnd)
}
// Generate child step functions.
str, err := c.Steps.CodegenAggregateFunctions()
if err != nil {
return "", err
}
buffer.WriteString(str)
// Generate main function.
fmt.Fprintf(buffer, "function %s(cursor, data)\n", c.FunctionName())
if c.WithinRangeStart > 0 {
fmt.Fprintf(buffer, " if cursor:eos() or cursor:eof() then return false end\n")
}
if c.WithinUnits == QueryConditionUnitSteps {
fmt.Fprintf(buffer, " index = 0\n")
}
fmt.Fprintf(buffer, " repeat\n")
if c.WithinUnits == QueryConditionUnitSteps {
fmt.Fprintf(buffer, " if index >= %d and index <= %d then\n", c.WithinRangeStart, c.WithinRangeEnd)
}
// Generate conditional expression.
expressionCode, err := c.CodegenExpression()
if err != nil {
return "", err
}
fmt.Fprintf(buffer, " if %s then\n", expressionCode)
// Call each step function.
for _, step := range c.Steps {
fmt.Fprintf(buffer, " %s(cursor, data)\n", step.FunctionName())
}
fmt.Fprintf(buffer, " return true\n")
fmt.Fprintf(buffer, " end\n")
fmt.Fprintf(buffer, " end\n")
if c.WithinUnits == QueryConditionUnitSteps {
fmt.Fprintf(buffer, " if index >= %d then break end\n", c.WithinRangeEnd)
fmt.Fprintf(buffer, " index = index + 1\n")
}
fmt.Fprintf(buffer, " until not cursor:next()\n")
fmt.Fprintf(buffer, " return false\n")
// End function definition.
fmt.Fprintln(buffer, "end")
return buffer.String(), nil
}
// Generates Lua code for the query.
func (c *QueryCondition) CodegenMergeFunction() (string, error) {
buffer := new(bytes.Buffer)
// Generate child step functions.
str, err := c.Steps.CodegenMergeFunctions()
if err != nil {
return "", err
}
buffer.WriteString(str)
return buffer.String(), nil
}
// Generates Lua code for the expression.
func (c *QueryCondition) CodegenExpression() (string, error) {
// Do not transform simple booleans.
if c.Expression == "true" || c.Expression == "false" {
return c.Expression, nil
}
// Full expressions should be prepended with cursor's event reference.
r, _ := regexp.Compile(`^ *(\w+) *(==) *(?:"([^"]*)"|'([^']*)'|(\d+(?:\.\d+)?)|(true|false)) *$`)
m := r.FindSubmatch([]byte(c.Expression))
if m == nil {
return "", fmt.Errorf("skyd.QueryCondition: Invalid expression: %v", c.Expression)
}
// Find the property.
property := c.query.table.propertyFile.GetPropertyByName(string(m[1]))
if property == nil {
return "", fmt.Errorf("skyd.QueryCondition: Property not found: %v", string(m[1]))
}
// Validate the expression value.
var value string
switch property.DataType {
case FactorDataType, StringDataType:
// Validate string value.
var stringValue string
if m[3] != nil {
stringValue = string(m[3])
} else if m[4] != nil {
stringValue = string(m[4])
} else {
return "", fmt.Errorf("skyd.QueryCondition: Expression value must be a string literal for string and factor properties: %v", c.Expression)
}
// Convert factors.
if property.DataType == FactorDataType {
sequence, err := c.query.factors.Factorize(c.query.table.Name, property.Name, stringValue, false)
if err != nil {
return "", err
} else {
value = strconv.FormatUint(sequence, 10)
}
} else {
value = fmt.Sprintf(`"%s"`, stringValue)
}
case IntegerDataType, FloatDataType:
if m[5] == nil {
return "", fmt.Errorf("skyd.QueryCondition: Expression value must be a numeric literal for integer and float properties: %v", c.Expression)
}
value = string(m[5])
case BooleanDataType:
if m[6] == nil {
return "", fmt.Errorf("skyd.QueryCondition: Expression value must be a boolean literal for boolean properties: %v", c.Expression)
}
value = string(m[6])
}
return fmt.Sprintf("cursor.event:%s() %s %s", m[1], m[2], value), nil
}
//--------------------------------------
// Factorization
//--------------------------------------
// Converts factorized fields back to their original strings.
func (c *QueryCondition) Defactorize(data interface{}) error {
return c.Steps.Defactorize(data)
}