-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_builder.py
329 lines (253 loc) · 10.6 KB
/
query_builder.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
import collections
class QueryBuilder:
###
# Build up a SELECT programmatically.
###
def __init__(self, toplevel_clause=None, kind='SELECT'):
self._kind = kind
if not toplevel_clause:
toplevel_clause = AND()
self._where = toplevel_clause
self._projections = []
self._main_relation = None
self._joins = []
self._group_by = []
self._having = []
self._having_params = []
self._limit = None
self._offset = None
self._relation_aliases = set()
def relation(self, main_relation_to_query):
self._main_relation = main_relation_to_query
self._scan_alias(main_relation_to_query)
return self
def join(self, relation, on=None, using=None, params=None, kind='INNER'):
assert len(list(v for v in (on, using) if v)) == 1, \
'At most one of on or using'
join_tuple = (relation, on, using, kind, params)
# Multiple calls to join() with same params become
# no-ops. Lets distinct blocks of code needing to
# add the same join in order to ultimately add in additional
# where clauses happen w/o additional communication.
if join_tuple not in self._joins:
self._joins.append(join_tuple)
self._scan_alias(relation)
return self
def left_join(self, relation, on=None, using=None, params=None):
return self.join(relation, on=on, using=using,
kind='LEFT', params=params)
def right_join(self, relation, on=None, using=None, params=None):
return self.join(relation, on=on, using=using,
kind='RIGHT', params=params)
def outer_join(self, relation, on=None, using=None, params=None):
return self.join(relation, on=on, using=using,
kind='FULL OUTER', params=params)
def project(self, *args):
self._projections.extend(args)
return self
def having(self, expression, *params):
self._having.append(expression)
self._having_params.extend(params)
return self
def group_by(self, *args):
self._group_by.extend(args)
return self
def where(self, *args):
self._where.append(args)
return self
def limit(self, value: int, offset=None):
assert isinstance(value, int)
assert isinstance(offset, int) or offset is None
assert value >= 0
self._limit = value
self._offset = offset
return self
def offset(self, offset):
assert isinstance(offset, int) and offset > 0
assert self._limit is not None
self._offset = offset
return self
@property
def statement(self):
assert self._projections
buf = [self._kind]
buf.append(', '.join(self._projections))
if self._main_relation:
buf.append('FROM')
buf.append(self._main_relation)
if self._joins:
assert self._main_relation, 'Can only join given a main relation'
for relation, on, using, kind, params in self._joins:
how_expression = on or using
assert how_expression
if on:
assert not using
how = 'ON'
else:
assert using
how = 'USING'
buf.append('%s JOIN %s %s (%s)' % (
kind, relation, how, how_expression))
if self._where.expression:
buf.append('WHERE')
buf.append(self._where.expression)
if self._group_by:
buf.append('GROUP BY')
buf.append(', '.join(str(gb) for gb in self._group_by))
if self._having_params:
buf.append('HAVING')
buf.append(', '.join(str(h) for h in self._having))
if self._limit:
buf.append('LIMIT %s')
if self._offset:
buf.append('OFFSET %s')
return ' '.join(buf)
@property
def parameters(self):
params = []
# join params are buried as the last member of
# the _joins tuples. See .join().
for j in self._joins:
join_params = j[-1]
if join_params:
if isinstance(join_params, tuple):
params.extend(join_params)
else:
params.append(join_params)
params.extend(self._where.parameters)
params.extend(self._having_params)
if self._limit:
params.append(self._limit)
if self._offset:
params.append(self._offset)
return tuple(params)
def _scan_alias(self, relation_expr):
assert '"' not in relation_expr, \
'Not smart enough for quoted relations, masochist!'
if ' ' in relation_expr:
# Not smart enough for 'as' kword yet, but that
# goes in here when/if time comes. We'll vomit
# if there's more than one space joined word
# at least.
relation, alias = relation_expr.split(' ')
if alias in self._relation_aliases:
raise AliasException('Already using relation alias %s' % alias)
self._relation_aliases.add(alias)
class AliasException(Exception):
pass
class ExpressionAndParams:
def __init__(self, operator: str, operands):
self.operator = operator
self._operands = []
if operands:
assert isinstance(operands, collections.Sequence) \
and not isinstance(operands, str)
for op in operands:
# make use of member-wise check in this method.
self.append(op)
# String, as in '(foo=%s) AND (bar like %s)', determined very late
# in _expand() when diven by 1st dereference to either
# .expression or .parameters properties.
self._expression = None
# Tuple, as in (42, 'Joe %'), , determined very late
# in _expand() when diven by 1st dereference to either
# .expression or .parameters properties.
self._parameters = None
def append(self, *args):
# args should be one of:
#
# 0) a single tuple, from .where's *args. Process its single member
# 1) expression string, tuple of multiple params
# 2) expression string, param single non-tuple
# 3) expression string only, no additional params.
# 4) expression string, more than one arg not wrapped in tuple.
# 5) a single subordinate ExpressionAndParams clause.
# In the case of 2), we go ahead and wrap as a singleton tuple
# just to make _expand() a little simpler in only needing to then
# deal with two cases.
# In case of 3) we also make things a little easier for _expand,
# by tacking on an empty tuple as the params for that clause.
# Likewise for 4) : wrap the trailing parameters in a single tuple.
# Case 0:
if len(args) == 1 and isinstance(args, tuple):
args = args[0]
# Case 1: expression string, tuple of multiple params
if isinstance(args, tuple) and len(args) == 2 and isinstance(args[0], str) \
and isinstance(args[1], tuple):
self._operands.append(args)
# Case 2: expression string, param single non-tuple
elif isinstance(args, tuple) and len(args) == 2 and isinstance(args[0], str) \
and not isinstance(args[1], tuple):
self._operands.append(tuple((args[0], (args[1],))))
# Case 3: expression string only in a tuple, no additional params.
elif isinstance(args, tuple) and len(args) == 1 and isinstance(args[0], str):
self._operands.append((args[0], ()))
# Case 3a: bare expression string, no tuple wrapping
elif isinstance(args, str):
self._operands.append((args, ()))
# Case 4: expression string, more than one parameter not wrapped
# in a tuple. Bundle 'em, all up into a tuple.
elif isinstance(args, tuple) and len(args) > 2 and isinstance(args[0], str):
self._operands.append((args[0], tuple(args[1:])))
# Case 5: a single subordinate ExpressionAndParams clause.
elif isinstance(args, ExpressionAndParams):
self._operands.append(args)
# Case 6: a tuple containing an ExpressionAndParams
elif isinstance(args, tuple) and len(args) == 1 \
and isinstance(args[0], ExpressionAndParams):
self._operands.append(args[0])
else:
raise TypeError("Don't know how to handle %r: %s %s" %
(args, len(args), isinstance(args[0], str),))
@property
def expression(self):
if self._expression is None:
self._expand()
return self._expression
@property
def parameters(self):
if self._parameters is None:
self._expand()
return self._parameters
def _expand(self):
##
# Assign to ._expression and ._params
# by visiting all in ._operands
##
params = []
exp_buf = []
for o in self._operands:
# Sanity check operand. Should be either
# 1) tuple of (str expression, tuple of params)
# 2) subordinate ExpressionAndParams instances.
assert (isinstance(o, tuple)
and len(o) == 2
and isinstance(o[0], str)
and isinstance(o[1], tuple)) or (
isinstance(o, ExpressionAndParams)
), 'Unexpected member of ._operands: %r' % (o,)
if isinstance(o, tuple):
exp_buf.append(o[0])
params.extend(o[1])
elif isinstance(o, ExpressionAndParams):
# Recurse into it ...
exp_buf.append(o.expression)
params.extend(o.parameters)
else:
raise Exception('Unknown operand: %r' % (arg,))
if len(exp_buf) > 1:
# operator (AND or OR) join these buggers
spaced_op = ' %s ' % self.operator
expression = spaced_op.join('(' + e + ')' for e in exp_buf)
elif len(exp_buf) == 1:
# Just one single clause, no need for joining together.
expression = exp_buf[0]
else:
# No clause at all!
expression = ''
self._expression = expression
self._parameters = params
def OR(*args):
return ExpressionAndParams('OR', args)
def AND(*args):
return ExpressionAndParams('AND', args)