-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonquery.py
402 lines (342 loc) · 12.3 KB
/
jsonquery.py
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# Select records by JSON via web2py's DAL.
# Common utility functions, methods and classes for web2py DAL statements.
# license: MIT
# author: lmk
# datetime: 2016-02-26 22:55:00
import json
import traceback
def generate_condition(table, field, value, operator="eq"):
"""
Generate Web2py `WHERE` statement.
table: <web2py TABLE object>
e.g. "table1"
field: <str>
e.g. "field1"
value: object
e.g. 100
operator: <str>
default: "eq"
e.g. "gte"
"""
if operator == "eq":
return (table[field] == value)
elif operator == "ne":
return (table[field] != value)
elif operator == "gte":
return (table[field] >= value)
elif operator == "lte":
return (table[field] <= value)
elif operator == "gt":
return (table[field] > value)
elif operator == "lt":
return (table[field] < value)
elif operator == "start":
return (table[field].startswith(value))
elif operator == "end":
return (table[field].endswith(value))
elif operator == "contain":
return (table[field].contains(value))
elif operator in ["year", "month", "day", "hour", "minutes", "seconds"]:
return (getattr(table[field], operator)() == value)
def generate_order_field(field, config):
"""
Generate order field.
Parameters:
field: web2py FIELD object
config: dict
e.g. {"field": 'col1', "sort": "desc"}
"""
if config.get("sort") == "desc":
return ~field
return field
def merge_tables(record, _extra=None):
"""
Merge columns from joined tables into single table.
Parameters:
record: web2py table-joined record
_extra: aliased field(s)
format: [
{"db.table.field AS alias1": "alias1"},
{"db.table.field AS alias2": "alias2"}
]
"""
merged = dict()
for table_name in record.keys():
if (table_name == "_extra") & (_extra is not None):
for each in _extra:
k, v = each.items()[-1]
value = record[table_name][k]
if value:
merged[v] = value
else:
try:
value = record[table_name]
if (value is not None) & (isinstance(value, dict) is True):
merged.update(value)
except:
print(traceback.format_exc())
return merged
class JsonQuery(object):
def __init__(self, db, logger=None, verbose=False):
self._db = db
self._logger = logger
self._verbose = verbose
# _extra: pair w2p generated alias name with user-defined alias name
# and store them in a list for later use.
self._extra = []
def load_json(self, jsonstr, funcname=None):
parsed = None
try:
parsed = json.loads(jsonstr)
self.log(funcname, "parsed", parsed)
except:
parsed = None
self.warn(funcname,
traceback.format_exc())
return parsed
def log(self, funcname, varname, data):
if self._verbose and self._logger:
self._logger.debug("[DD] %s :: %s" % (
funcname,
varname
))
self._logger.debug(data)
def warn(self, funcname, message):
if self._verbose and self._logger:
self._logger.warn("[WW] %s :: %s" % (
funcname,
message
))
def construct_DISTINCT(self, distinct_field):
"""
Construct DISTINCT field.
Return: web2py DAL<field> object
"""
if distinct_field:
table = distinct_field.get('table')
field = distinct_field.get('field')
return self._db[table][field]
else:
return None
def construct_FIELDS(self, fields):
"""
Construct FIELDS to select from specific table.
Return: list of web2py fields
"""
_fields = []
for each in fields:
generated = None
table = each["table"]
tmp_fields = each.get("fields")
if tmp_fields:
for each_field in tmp_fields:
field = each_field["field"]
alias = each_field.get("alias")
# hasCount: true or false
# NOTE: that field is `count field`
# (used for `GROUP BY`)
hasCount = each_field.get("count") or False
generated = self._db[table][field]
if hasCount:
generated = generated.count()
tmp_dict = {str(generated): field}
if not alias:
self._extra.append(tmp_dict)
if alias:
generated = generated.with_alias(alias)
tmp_dict = {str(generated): alias}
self._extra.append(tmp_dict)
_fields.append(generated)
else:
generated = self._db[table].ALL
_fields.append(generated)
self.log("construct_FIELDS", "_fields", _fields)
return _fields
def construct_GROUP(self, group_fields):
"""
Construct GROUP statement.
Return: web2py GROUP statement
"""
_group = None
for each in group_fields:
table = each["table"]
fields = each.get("fields")
if not fields:
self.warn("construct_GROUP",
"There is no `groupby` field for `%s`. At least, "
"one field must be provided." % table)
continue
for each_field in fields:
field = each_field["field"]
if _group is None:
_group = self._db[table][field]
else:
_group |= self._db[table][field]
self.log("construct_GROUP", "_group", _group)
return _group
def construct_JOIN(self, join):
"""
Construct JOIN statement.
Return: web2py JOIN statement.
"""
joined = []
for each in join:
ON_table = each.get("on").get("table")
ON_field = each.get("on").get("field")
JOINER_table = each.get("joiner").get("table")
JOINER_field = each.get("joiner").get("field")
JOINER_value = self._db[JOINER_table][JOINER_field]
where = generate_condition(
self._db[ON_table],
ON_field,
JOINER_value
)
generated = self._db[ON_table].on(where)
joined.append(generated)
self.log("construct_JOIN", "joined", joined)
return joined
def construct_LIMIT(self, limit):
"""
Construct LIMIT statement.
Return: <int> tuple
e.g. (start, end)
"""
if limit:
return (limit.get('start'), limit.get('end'))
else:
return None
def construct_ORDER(self, order_fields):
"""
Construct ORDER statement.
Return: web2py ORDER statement
"""
_order = None
for each in order_fields:
table = each["table"]
fields = each.get("fields")
if not fields:
self.warn("construct_ORDER",
"There is no `orderby` field for `%s`. At least, "
"one field must be provided." % table)
continue
for each_field in fields:
field = each_field["field"]
generated = generate_order_field(
self._db[table][field],
each_field
)
if _order is None:
_order = generated
else:
_order |= generated
self.log("construct_ORDER", "_order", _order)
return _order
def construct_WHERE(self, where):
"""
Construct WHERE statement.
Return: web2py WHERE statement
"""
_where = None
for each in where:
table = each["table"]
conditions = each["conditions"]
for condition in conditions:
field = condition["field"]
value = condition["value"]
operator = condition["operator"]
generated = generate_condition(
self._db[table],
field,
value,
operator
)
if _where is None:
_where = generated
else:
_where &= generated
self.log("construct_WHERE", "_where", _where)
return _where
def run(self, stored):
"""
Select record(s) according to given criteria written in JSON.
stored: dict (simply assume python-dict)
Attributes of stored
==========================
fields: JSON Object Array
NOTE: FIELDS MUST BE PROVIDED.
If `count` is used, `group_fields` must be provided.
e.g-1. [{"table": "table1", "fields": [
{"field": "f1", "alias": "field1"},
{"field": "f2", "alias": "col2"},
{"field": "col3"},
{"field": "col4", "count": true},
{"field": "col5", "count": true,
"alias": "total"}
]
}, ...]
e.g-2. [{"table": "table1"}, {"table": "table2"}]
order_fields: JSON Object Array
default: None
e.g. [{"table": "table1", "fields": [
{"field": "f1", "sort": "desc"},
{"field": "f2", "sort": "asc"},
{"field": "col3"}] // default:"asc"
}, ...]
group_fields: JSON Object Array
default: None
e.g. Similar as `order_fields` value. But `sort` is not required.
distinct_field: JSON Object
default: None
e.g. {"table": "table1", "field": "col1"}
where: JSON Object Array
default: None
e.g. [{table: "table1",
conditions: [{"field": "field1",
"value": 1,
"operator": "eq"},
...
]}, ...]
join: JSON Object Array
default: None
format:
[{
"on":
{"table": "table1", "field": "col1"},
"joiner":
{"table": "table2", "field": "col1"}
}, ...]
limit: JSON Object
default: None
e.g. {"start": 0, "end": 10}
merge: true or false
default: false
NOTE: if `join` is provided, `merge` must be set true.
Return: web2py's DAL record(s)
"""
fields = self.construct_FIELDS(stored.get('fields'))
order_fields = self.construct_ORDER(stored.get('order_fields', []))
group_fields = self.construct_GROUP(stored.get('group_fields', []))
distinct_field = self.construct_DISTINCT(
stored.get('distinct_field'))
where = self.construct_WHERE(stored.get('where', []))
join = self.construct_JOIN(stored.get('join', []))
limit = self.construct_LIMIT(stored.get('limit', []))
merge = stored.get('merge', False)
records = self._db(where).select(
*fields,
left=join,
distinct=distinct_field,
orderby=order_fields,
groupby=group_fields,
limitby=limit)
if merge:
records = [merge_tables(record.as_dict(),
self._extra) for record in records]
return records
def run_from_file(self, path, mode='rb'):
try:
json_file = open(path, mode)
parsed = json.load(json_file)
return self.run(parsed)
except:
raise