-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path_sql_utils.py
More file actions
339 lines (252 loc) · 10 KB
/
Copy path_sql_utils.py
File metadata and controls
339 lines (252 loc) · 10 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
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
"""
Homegrown SQL query builder.
See tests/test_sql_utils.py for some usage examples.
I've written at length about it elsewhere:
https://death.andgravity.com/query-builder
Introduction to the series, has more examples.
https://death.andgravity.com/query-builder-why
"Why use an SQL query builder in the first place?"
The problem we're trying to solve.
https://death.andgravity.com/own-query-builder
"Why I wrote my own SQL query builder"
High-level design decisions, in the context of reader.
https://death.andgravity.com/query-builder-how
Code walk-through / tutorial.
Low-level design decisions.
Better thought-out versions of the unimplemented features below.
---
For a version of this that supports UNIONs see the second prototype in
https://github.com/lemon24/reader/issues/123#issuecomment-624045621
Here's a version based on the same idea that works with the 2021 refactor:
https://gist.github.com/lemon24/3777dd39c96bf30014a331943ed789fa
---
For a version of this that supports INSERT/UPDATE/DELETE, see
https://github.com/lemon24/reader/commit/7c97fccf61d16946176c2455be3634c5a8343e1b
The way things work now has changed slightly;
we'd probably make them flag keywords now.
To make VALUES bake in the parentheses, we just need to set:
query.formats[0]['VALUES'] = '({value})'
That's to add one values tuple at a time. To add one column, we could do this:
* output keyword even if called with no args
* add(..., flag=...), and allow arbitrary flags;
INSERT_INTO('x', 'y', flag='table') -> flag is "INTO table"
* parens_keywords = {'INSERT', 'VALUES'};
different than subquery_keywords because it applies once to the whole set
---
To support marking arbitrary things as subqueries,
add a signalling tuple and a helper function:
class _Subquery(tuple): pass
def Subquery(*args) -> _Subquery:
return _Subquery(args) # from_arg() has to support 1-tuples
Then, in from_arg(), if arg is a _Subquery, set is_subquery.
Usage looks like this:
Query().FROM(
Subquery('alias', 'subquery'),
('alias', 'not subquery'),
)
Alternatively, add an is_subquery kwarg to add():
query = Query().FROM(('alias', 'subquery'), is_subquery=True)
query.FROM(('alias', 'not subquery'))
---
To support using Queries as arguments directly,
without having to convert them to strings first,
allow _Thing.value to be a Query (and support it in);
then, in _lines_keyword(), convert queries to str and override is_subquery.
"""
import functools
import textwrap
from collections import defaultdict
from typing import Any
from typing import Callable
from typing import cast
from typing import Dict
from typing import Iterable
from typing import Iterator
from typing import List
from typing import Mapping
from typing import NamedTuple
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
_T = TypeVar('_T')
_U = TypeVar('_U')
_Q = TypeVar('_Q', bound='BaseQuery')
_QArg = Union[str, Tuple[str, ...]]
class _Thing(NamedTuple):
value: str
alias: str = ''
keyword: str = ''
is_subquery: bool = False
@classmethod
def from_arg(cls, arg: _QArg, **kwargs: Any) -> '_Thing':
if isinstance(arg, str):
alias, value = '', arg
elif len(arg) == 2:
alias, value = arg
else: # pragma: no cover
raise ValueError(f"invalid arg: {arg!r}")
return cls(_clean_up(value), _clean_up(alias), **kwargs)
class _FlagList(List[_T]):
flag: str = ''
def _clean_up(thing: str) -> str:
return textwrap.dedent(thing.rstrip()).strip()
class BaseQuery:
keywords = [
'WITH',
'SELECT',
'FROM',
'WHERE',
'GROUP BY',
'HAVING',
'ORDER BY',
'LIMIT',
]
separators: Mapping[str, str] = dict(WHERE='AND', HAVING='AND')
default_separator = ','
formats: Tuple[Mapping[str, str], ...] = (
defaultdict(lambda: '{value}'),
defaultdict(lambda: '{value} AS {alias}', WITH='{alias} AS {value}'),
)
subquery_keywords = {'WITH'}
fake_keywords = dict(JOIN='FROM')
flag_keywords = dict(SELECT={'DISTINCT', 'ALL'})
def __init__(
self,
data: Optional[Mapping[str, Iterable[_QArg]]] = None,
separators: Optional[Mapping[str, str]] = None,
) -> None:
self.data: Mapping[str, _FlagList[_Thing]] = {}
if data is None:
data = dict.fromkeys(self.keywords, ())
for keyword, args in data.items():
self.data[keyword] = _FlagList()
self.add(keyword, *args)
if separators is not None:
self.separators = separators
def add(self: _Q, keyword: str, *args: _QArg) -> _Q:
keyword, fake_keyword = self._resolve_fakes(keyword)
keyword, flag = self._resolve_flags(keyword)
target = self.data[keyword]
if flag:
if target.flag: # pragma: no cover
raise ValueError(f"{keyword} already has flag: {flag!r}")
target.flag = flag
kwargs: Dict[str, Any] = {}
if fake_keyword:
kwargs.update(keyword=fake_keyword)
if keyword in self.subquery_keywords:
kwargs.update(is_subquery=True)
for arg in args:
target.append(_Thing.from_arg(arg, **kwargs))
return self
def _resolve_fakes(self, keyword: str) -> Tuple[str, str]:
for part, real in self.fake_keywords.items():
if part in keyword:
return real, keyword
return keyword, ''
def _resolve_flags(self, keyword: str) -> Tuple[str, str]:
prefix, _, flag = keyword.partition(' ')
if prefix in self.flag_keywords:
if flag and flag not in self.flag_keywords[prefix]:
raise ValueError(f"invalid flag for {prefix}: {flag!r}")
return prefix, flag
return keyword, ''
def __getattr__(self: _Q, name: str) -> Callable[..., _Q]:
# conveniently, avoids shadowing dunder methods (e.g. __deepcopy__)
if not name.isupper():
return getattr(super(), name) # type: ignore
return functools.partial(self.add, name.replace('_', ' '))
def __str__(self) -> str:
return ''.join(self._lines())
def _lines(self) -> Iterable[str]:
for keyword, things in self.data.items():
if not things:
continue
if things.flag:
yield f'{keyword} {things.flag}\n'
else:
yield f'{keyword}\n'
grouped: Tuple[List[_Thing], ...] = ([], [])
for thing in things:
grouped[bool(thing.keyword)].append(thing)
for group in grouped:
yield from self._lines_keyword(keyword, group)
def _lines_keyword(self, keyword: str, things: Sequence[_Thing]) -> Iterable[str]:
for i, thing in enumerate(things):
last = i + 1 == len(things)
if thing.keyword:
yield thing.keyword + '\n'
format = self.formats[bool(thing.alias)][keyword]
value = thing.value
if thing.is_subquery:
value = f'(\n{self._indent(value)}\n)'
yield self._indent(format.format(value=value, alias=thing.alias))
if not last and not thing.keyword:
try:
yield ' ' + self.separators[keyword]
except KeyError:
yield self.default_separator
yield '\n'
_indent = functools.partial(textwrap.indent, prefix=' ')
if TYPE_CHECKING: # pragma: no cover
_SWMBase = BaseQuery
else:
_SWMBase = object
_SWM = TypeVar('_SWM', bound='ScrollingWindowMixin')
class ScrollingWindowMixin(_SWMBase):
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
self.scrolling_window_order_by()
def scrolling_window_order_by(
self: _SWM, *things: str, desc: bool = False, keyword: str = 'WHERE'
) -> _SWM:
self.__things = [_clean_up(t) for t in things]
self.__desc = desc
self.__keyword = keyword
order = 'DESC' if desc else 'ASC'
return self.ORDER_BY(*(f'{thing} {order}' for thing in things))
def extract_last(self, result: Tuple[_T, ...]) -> Optional[Tuple[_T, ...]]:
names = [t.alias or t.value for t in self.data['SELECT']]
return tuple(result[names.index(t)] for t in self.__things) or None
def add_last(self, last: Optional[Tuple[_T, ...]]) -> Sequence[Tuple[str, _T]]:
self.__add_last()
return self.__last_params(last)
__make_label = 'last_{}'.format
def __add_last(self: _SWM) -> None:
op = '<' if self.__desc else '>'
labels = (':' + self.__make_label(i) for i in range(len(self.__things)))
comparison = BaseQuery({'(': self.__things, f') {op} (': labels, ')': ['']})
self.add(self.__keyword, str(comparison).rstrip())
def __last_params(self, last: Optional[Tuple[_T, ...]]) -> Sequence[Tuple[str, _T]]:
return [(self.__make_label(i), t) for i, t in enumerate(last or ())]
class Query(ScrollingWindowMixin, BaseQuery):
pass
if TYPE_CHECKING: # pragma: no cover
import sqlite3
def paginated_query(
db: 'sqlite3.Connection',
query: Query,
params: Dict[str, Any] = {}, # noqa: B006
chunk_size: Optional[int] = 0,
last: Optional[_U] = None,
row_factory: Optional[Callable[[Tuple[Any, ...]], _T]] = None,
) -> Iterator[Tuple[_T, _U]]:
params = dict(params)
if chunk_size:
query.LIMIT(":chunk_size")
params['chunk_size'] = chunk_size
if last:
params.update(query.add_last(last)) # type: ignore
rv = (
(row_factory(t) if row_factory else t, cast(_U, query.extract_last(t)))
for t in db.execute(str(query), params)
)
# Consume the result to avoid blocking the database,
# but only if the query is actually paginated
# (we may need the pass-through for performance).
if chunk_size:
return iter(list(rv))
return rv