-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__init__.py
More file actions
370 lines (320 loc) · 12.1 KB
/
Copy path__init__.py
File metadata and controls
370 lines (320 loc) · 12.1 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
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
from __future__ import annotations
import typing
from contextvars import ContextVar
from copy import deepcopy
from dataclasses import dataclass, field, replace
from enum import auto, Enum, unique
from numbers import Number
from types import TracebackType
from sql_tstring.parser import (
Clause,
Element,
Expression,
ExpressionGroup,
Function,
Group,
Literal,
Operator,
parse,
Part,
Placeholder,
PlaceholderType,
Statement,
)
from sql_tstring.t import t, Template as TTemplate
try:
from string.templatelib import Template
except ImportError:
class Template: # type: ignore[no-redef]
pass
@unique
class RewritingValue(Enum):
ABSENT = auto()
IS_NULL = auto()
IS_NOT_NULL = auto()
class LiteralValue:
def __init__(self, value: typing.Any) -> None:
self.value = value
type AbsentType = typing.Literal[RewritingValue.ABSENT]
Absent: AbsentType = RewritingValue.ABSENT
IsNull = RewritingValue.IS_NULL
IsNotNull = RewritingValue.IS_NOT_NULL
@dataclass
class Context:
allow_numeric: bool = False
columns: set[str] = field(default_factory=set)
dialect: typing.Literal["asyncpg", "sql"] = "sql"
tables: set[str] = field(default_factory=set)
_context_var: ContextVar[Context] = ContextVar("sql_tstring_context")
def get_context() -> Context:
try:
return _context_var.get()
except LookupError:
context = Context()
_context_var.set(context)
return context
def set_context(context: Context) -> None:
_context_var.set(context)
def sql_context(
columns: set[typing.LiteralString] | None = None,
dialect: typing.Literal["asyncpg", "sql"] | None = None,
tables: set[typing.LiteralString] | None = None,
*,
allow_numeric: bool | None = None,
) -> _ContextManager:
ctx = get_context()
ctx_manager = _ContextManager(ctx)
if allow_numeric is not None:
ctx_manager._context.allow_numeric = allow_numeric
if columns is not None:
ctx_manager._context.columns = columns
if dialect is not None:
ctx_manager._context.dialect = dialect
if tables is not None:
ctx_manager._context.tables = tables
return ctx_manager
def sql(
query_or_template: str | Template | TTemplate, values: dict[str, typing.Any] | None = None
) -> tuple[str, list]:
template: Template | TTemplate
if isinstance(query_or_template, (Template, TTemplate)) and values is None:
template = query_or_template
elif isinstance(query_or_template, str) and values is not None:
template = t(query_or_template, values)
else:
raise ValueError("Must call with a template, or a query string and values")
parsed_queries = parse(template)
result_str = ""
result_values: list[typing.Any] = []
ctx = get_context()
for raw_parsed_query in parsed_queries:
parsed_query = deepcopy(raw_parsed_query)
new_values = _replace_placeholders(parsed_query, 0)
result_str += _print_node(parsed_query, [None] * len(result_values), ctx.dialect)
result_values.extend(new_values)
return result_str, result_values
class _ContextManager:
def __init__(self, context: Context) -> None:
self._context = replace(context)
def __enter__(self) -> Context:
self._original_context = get_context()
set_context(self._context)
return self._context
def __exit__(
self,
_type: type[BaseException] | None,
_value: BaseException | None,
_traceback: TracebackType | None,
) -> None:
set_context(self._original_context)
def _safely_convert_placeholder_value(
value: object,
*,
parent_node: Expression | Function | Group | Literal,
allow_numeric: bool = False,
case_sensitive: set[str] | None = None,
case_insensitive: set[str] | None = None,
value_type: type = str,
) -> Part | Placeholder:
if isinstance(value, LiteralValue):
if value.value is None:
return Part(text="NULL", parent=parent_node)
elif isinstance(value.value, bool) or (allow_numeric and isinstance(value.value, Number)):
return Part(text=str(value.value), parent=parent_node)
else:
return Placeholder(parent=parent_node, value=value.value)
else:
if case_sensitive is None:
case_sensitive = set()
if case_insensitive is None:
case_insensitive = set()
if value is None:
text = "NULL"
elif isinstance(value, bool):
text = str(value)
elif allow_numeric and isinstance(value, Number):
text = str(value)
elif not isinstance(value, value_type):
raise ValueError(f"{value} is not valid, must be {value_type}")
elif isinstance(value, str) and (
value not in case_sensitive and value.lower() not in case_insensitive
):
raise ValueError(
f"{value} is not valid, must be one of {case_sensitive} or {case_insensitive}"
)
else:
text = str(value)
return Part(text=text, parent=parent_node)
def _print_node(
node: Element,
placeholders: list | None = None,
dialect: str = "sql",
strip: bool = True,
) -> str:
if placeholders is None:
placeholders = []
match node:
case Statement():
result = " ".join(_print_node(clause, placeholders, dialect) for clause in node.clauses)
case Clause() | ExpressionGroup():
result = ""
for expression in node.expressions:
addition = _print_node(expression, placeholders, dialect)
separator = ""
if result != "":
separator = expression.separator
if addition != "":
result += f" {separator} {addition}"
result = result.strip()
if isinstance(node, ExpressionGroup):
if result != "":
result = f"({result})"
else:
if node.removed:
result = ""
elif result == "" and not node.properties.allow_empty:
result = ""
else:
result = f"{node.text} {result}"
case Expression():
if not node.removed:
result = " ".join(_print_node(part, placeholders, dialect) for part in node.parts)
else:
result = ""
case Function():
arguments = " ".join(_print_node(part, placeholders, dialect) for part in node.parts)
result = f"{node.name}({arguments})"
case Group():
result = (
f"({" ".join(_print_node(part, placeholders, dialect) for part in node.parts)})"
)
case Operator():
result = node.text
case Part():
result = node.text
case Placeholder():
placeholders.append(None)
result = f"${len(placeholders)}" if dialect == "asyncpg" else "?"
case Literal():
value = "".join(_print_node(part, placeholders, dialect, False) for part in node.parts)
result = f"'{value}'"
if strip:
return result.strip()
else:
return result
def _replace_placeholders(
node: Element,
index: int,
) -> list[typing.Any]:
result = []
match node:
case Statement():
for clause_ in node.clauses:
result.extend(_replace_placeholders(clause_, 0))
case Clause() | ExpressionGroup():
for index, expression_ in enumerate(node.expressions):
result.extend(_replace_placeholders(expression_, index))
case Expression() | Function() | Group() | Literal():
for index, part in enumerate(node.parts):
result.extend(_replace_placeholders(part, index))
case Placeholder():
result.extend(_replace_placeholder(node, index))
return result
def _replace_placeholder(
node: Placeholder,
index: int,
) -> list[typing.Any]:
result = []
ctx = get_context()
clause_or_function = node.parent
while not isinstance(clause_or_function, (Clause, Function)):
clause_or_function = clause_or_function.parent # type: ignore
clause: Clause | None = None
placeholder_type = PlaceholderType.VARIABLE
if isinstance(clause_or_function, Clause):
clause = clause_or_function
placeholder_type = clause_or_function.properties.placeholder_type
value = node.value
new_node: Part | Placeholder
if value is RewritingValue.ABSENT:
if placeholder_type == PlaceholderType.VARIABLE_DEFAULT:
new_node = Part(text="DEFAULT", parent=node.parent)
node.parent.parts[index] = new_node
elif placeholder_type == PlaceholderType.LOCK:
if clause is not None:
clause.removed = True
else:
expression: Expression | ExpressionGroup | Function | Group | Literal | Statement = (
node.parent
)
while not isinstance(expression, Expression):
expression = expression.parent
expression.removed = True
elif isinstance(node.parent, Literal):
if not isinstance(value, str):
raise RuntimeError("Invalid placeholder usage")
else:
for position, part in enumerate(node.parent.parent.parts):
if part is node.parent:
value = ""
for bit in part.parts:
if isinstance(bit, Placeholder):
value += str(bit.value)
else:
value += bit.text
node.parent.parent.parts[position] = Placeholder(
parent=part.parent, value=value
)
result.append(value)
else:
match placeholder_type:
case PlaceholderType.COLUMN:
new_node = _safely_convert_placeholder_value(
value,
allow_numeric=ctx.allow_numeric,
case_sensitive=ctx.columns,
parent_node=node.parent,
)
case PlaceholderType.FRAME:
new_node = _safely_convert_placeholder_value(
value, value_type=int, parent_node=node.parent
)
case PlaceholderType.LOCK:
new_node = _safely_convert_placeholder_value(
value, case_insensitive={"", "nowait", "skip locked"}, parent_node=node.parent
)
case PlaceholderType.SORT:
new_node = _safely_convert_placeholder_value(
value,
allow_numeric=ctx.allow_numeric,
case_sensitive=ctx.columns,
case_insensitive={"asc", "ascending", "desc", "descending"},
parent_node=node.parent,
)
case PlaceholderType.TABLE:
new_node = _safely_convert_placeholder_value(
value,
allow_numeric=ctx.allow_numeric,
case_sensitive=ctx.tables,
parent_node=node.parent,
)
case _:
if (
value is RewritingValue.IS_NULL or value is RewritingValue.IS_NOT_NULL
) and placeholder_type == PlaceholderType.VARIABLE_CONDITION:
for part in node.parent.parts:
if isinstance(part, Operator):
if value is RewritingValue.IS_NULL:
part.text = "IS"
else:
part.text = "IS NOT"
new_node = Part(text="NULL", parent=node.parent)
else:
new_node = node
if isinstance(new_node, Placeholder):
result.append(new_node.value) # type: ignore[arg-type]
if isinstance(node.parent, (Expression, ExpressionGroup, Function, Group)):
node.parent.parts[index] = new_node
else:
raise RuntimeError("Invalid placeholder")
return result