-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathquery.py
More file actions
313 lines (259 loc) · 9.18 KB
/
query.py
File metadata and controls
313 lines (259 loc) · 9.18 KB
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
import copy
# https://ply.readthedocs.io
from ply import lex, yacc
from .column import get_column_by_type
class Lexer(object):
def __init__(self):
self.lexer = lex.lex(module=self, debug=False)
# List of token names. This is always required
tokens = (
'LBORDER', 'RBORDER',
'AND', 'OR',
'EQUAL', 'NOT_EQUAL', 'GTE', 'GT', 'LTE', 'LT', 'LIKE',
'QUOTE_STRING', 'STRING'
)
reserved = {
'and': 'AND',
'or': 'OR',
'like': 'LIKE',
}
# Regular expression rules for simple tokens
t_LBORDER = r'\('
t_RBORDER = r'\)'
t_EQUAL = r'='
t_NOT_EQUAL = r'(!=)|(<>)'
t_GTE = r'>='
t_GT = r'>'
t_LTE = r'<='
t_LT = r'<'
def t_QUOTE_STRING(self, t):
r"'([^']*)'"
t.value = t.value[1:-1]
return t
def t_STRING(self, t):
r'[^=|!|>|<|/(|/)|\s]+'
t.type = self.reserved.get(t.value, 'STRING')
return t
# Define a rule so we can track line numbers
def t_newline(self, t):
r'\n+'
t.lexer.lineno += len(t.value)
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'
# t_ignore_COMMENT = r'\#'
# Error handling rule
def t_error(self, t):
raise ValueError('Illegal character!', t.value[0])
class ConditionsParser(object):
def __init__(self):
self.yaccer = yacc.yacc(module=self, debug=False)
self.lexer = Lexer().lexer
# Parse
def parse(self, raw_rows, raw_columns, conditions):
self.raw_rows = raw_rows
self.raw_columns = raw_columns
self.raw_columns_map = {column['name']: column for column in raw_columns}
# main
rows = self.yaccer.parse(conditions, lexer=self.lexer)
return rows
def _check_column_exists(self, column):
if column not in self.raw_columns_map:
raise ValueError('Column not found!', column)
def _merge(self, left_rows, condition, right_rows):
merged_rows = []
left_rows_id_list = [row['_id'] for row in left_rows]
if condition.lower() == 'and':
for row in right_rows:
if row['_id'] in left_rows_id_list:
merged_rows.append(row)
elif condition.lower() == 'or':
merged_rows = left_rows
for row in right_rows:
if row['_id'] not in left_rows_id_list:
merged_rows.append(row)
return merged_rows
def _filter(self, column, condition, value):
self._check_column_exists(column)
filtered_rows = []
column_type = self.raw_columns_map[column].get('type')
column_obj = get_column_by_type(column_type)
value = column_obj.parse_input_value(value)
if condition == '=':
for row in self.raw_rows:
cell_value = row.get(column)
if column_obj.parse_table_value(cell_value).equal(value):
filtered_rows.append(row)
elif condition in ('!=', '<>'):
for row in self.raw_rows:
cell_value = row.get(column)
if column_obj.parse_table_value(cell_value).unequal(value):
filtered_rows.append(row)
elif condition == '>=':
for row in self.raw_rows:
cell_value = row.get(column)
if column_obj.parse_table_value(cell_value).greater_equal_than(value):
filtered_rows.append(row)
elif condition == '>':
for row in self.raw_rows:
cell_value = row.get(column)
if column_obj.parse_table_value(cell_value).greater_than(value):
filtered_rows.append(row)
elif condition == '<=':
for row in self.raw_rows:
cell_value = row.get(column)
if column_obj.parse_table_value(cell_value).less_equal_than(value):
filtered_rows.append(row)
elif condition == '<':
for row in self.raw_rows:
cell_value = row.get(column)
if column_obj.parse_table_value(cell_value).less_than(value):
filtered_rows.append(row)
elif condition == 'like':
for row in self.raw_rows:
cell_value = row.get(column)
if column_obj.parse_table_value(cell_value).like(value):
filtered_rows.append(row)
return filtered_rows
# List of token names. This is always required
tokens = (
'AND', 'OR',
'EQUAL', 'NOT_EQUAL', 'GTE', 'GT', 'LTE', 'LT', 'LIKE',
'QUOTE_STRING', 'STRING',
)
def p_merge(self, p):
"""merge : filter AND filter
| filter OR filter
| merge AND filter
| merge OR filter
| filter
"""
if len(p.slice) > 2:
p[0] = self._merge(p[1], p[2], p[3])
else:
p[0] = p[1]
def p_filter(self, p):
"""filter : factor EQUAL factor
| factor NOT_EQUAL factor
| factor GTE factor
| factor GT factor
| factor LTE factor
| factor LT factor
| factor LIKE factor
"""
p[0] = self._filter(p[1], p[2], p[3])
def p_factor(self, p):
"""factor : QUOTE_STRING
| STRING
"""
p[0] = p[1]
# Error rule for syntax errors
def p_error(self, p):
raise ValueError('Syntax error in input!', p.value)
class QuerySet(object):
def __init__(self, base, table_name):
self.base = base
self.table_name = table_name
self.raw_rows = []
self.raw_columns = []
self.conditions = ''
self.rows = []
def __str__(self):
return '<SeaTable Queryset [ %s ]>' % self.table_name
def __iter__(self):
return iter(self.rows)
def __len__(self):
return len(self.rows)
def __getitem__(self, index):
return self.rows[index]
def __bool__(self):
return len(self.rows) > 0
def _clone(self):
clone = self.__class__(self.base, self.table_name)
return clone
def _execute_conditions(self):
if self.conditions and self.raw_rows and self.raw_columns:
# main
conditions_parser = ConditionsParser()
rows = conditions_parser.parse(
copy.deepcopy(self.raw_rows),
copy.deepcopy(self.raw_columns),
copy.copy(self.conditions),
)
self.rows = rows
else:
self.rows = self.raw_rows
def filter(self, conditions=''):
"""Performs the query and returns a new QuerySet instance.
:param conditions: str
:return: queryset
"""
clone = self._clone()
clone.raw_rows = copy.deepcopy(self.rows)
clone.raw_columns = copy.deepcopy(self.raw_columns)
clone.conditions = conditions
clone._execute_conditions()
return clone
def get(self, conditions=''):
"""Performs the query and returns a single row matching the given keyword arguments.
:param conditions: str
:return row: dict
"""
clone = self._clone()
clone.raw_rows = copy.deepcopy(self.rows)
clone.raw_columns = copy.deepcopy(self.raw_columns)
clone.conditions = conditions
clone._execute_conditions()
if len(clone.rows) == 0:
return None
else:
return clone.rows[0]
def all(self):
"""Returns a new QuerySet that is a copy of the current one.
:return: queryset
"""
clone = self._clone()
clone.raw_rows = copy.deepcopy(self.raw_rows)
clone.raw_columns = copy.deepcopy(self.raw_columns)
clone.conditions = copy.copy(self.conditions)
clone.rows = copy.deepcopy(self.rows)
return clone
def update(self, row_data):
"""Updates all elements in the current QuerySet, setting all the given fields to the appropriate values.
:param row_data: dict
:return rows: list
"""
for row in self.rows:
response = self.base.update_row(self.table_name, row['_id'], row_data)
row.update(row_data)
return self.rows
def delete(self):
"""Deletes the rows in the current QuerySet.
:return: int
"""
row_ids = [row['_id'] for row in self.rows]
response = self.base.batch_delete_rows(self.table_name, row_ids)
self.rows = []
return len(row_ids)
def first(self):
"""Returns the first object of a query, returns None if no match is found.
:return row: dict
"""
if self.rows:
return self.rows[0]
else:
return None
def last(self):
"""Returns the last object of a query, returns None if no match is found.
:return row: dict
"""
if self.rows:
return self.rows[-1]
else:
return None
def count(self):
"""Returns the number of rows as an integer.
:return: int
"""
return len(self.rows)
def exists(self):
return len(self.rows) > 0