This repository was archived by the owner on Apr 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdml.py
More file actions
437 lines (357 loc) · 11.4 KB
/
dml.py
File metadata and controls
437 lines (357 loc) · 11.4 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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from dataclasses import dataclass
from typing import Optional
from typing import Tuple
from typing import List
from rfmt.blocks import LineBlock as LB
from rfmt.blocks import TextBlock as TB
from rfmt.blocks import StackBlock as SB
from rfmt.blocks import IndentBlock as IB
from rfmt.blocks import ChoiceBlock as CB
from rfmt.blocks import WrapBlock as WB
from .utils import with_commas
from .types import SQLType
from .query import SQLQuery
from .query import SQLNamedTable
from .query_impl import SQLFrom
from .expr import SQLExpr
from .ident import SQLIdentifierPath
from .node import SQLNode
from .node import SQLNodeList
from .query import SQLTableSource
@dataclass(frozen=True)
class SQLDML(SQLNode):
@staticmethod
def consume(lex):
return (SQLInsert.consume(lex) or
SQLUpdate.consume(lex) or
SQLDelete.consume(lex) or
SQLCreate.consume(lex))
@dataclass(frozen=True)
class SQLInsert(SQLDML):
table: SQLTableSource
fields: Optional[SQLNodeList[SQLNode]]
sql: SQLNode
def sqlf(self, compact):
# TODO(scannell) Add compact version
if not self.fields:
return SB([
LB([
TB('INSERT INTO '),
self.table.sqlf(compact=True),
TB(' ('),
]),
IB(self.sql.sqlf(compact)),
TB(')'),
])
return SB([
LB([
TB('INSERT INTO '),
self.table.sqlf(compact=True),
TB(' ('),
]),
IB(
SB(with_commas(compact, self.fields, tail=')'))
),
self.sql.sqlf(compact)
])
@staticmethod
def consume(lex) -> 'Optional[SQLInsert]':
if not lex.consume('INSERT'):
return None
lex.consume('INTO')
table = SQLNamedTable.parse(lex, is_write=True)
lex.expect('(')
# It's possible to have a SQL statement in here.
# .. this is with an implicit column list.
query = SQLQuery.consume(lex)
if query:
lex.expect(')')
return SQLInsert(table, None, query)
# Pull out the values
cols = []
while True:
cols.append(SQLIdentifierPath.parse(lex))
if not lex.consume(','):
break
lex.expect(')')
# Now the SQL
query = SQLQuery.consume(lex)
if query:
return SQLInsert(table, SQLNodeList(cols), query)
lex.error('Insert with explicit values unsupported')
return None
@dataclass(frozen=True)
class SQLDelete(SQLDML):
table: SQLNode
where_expr: SQLNode
def sqlf(self, compact):
delete = [TB('DELETE FROM '), self.table.sqlf(compact)]
return SB([
LB(delete),
LB([
TB('WHERE '),
self.where_expr.sqlf(compact),
])
])
@staticmethod
def consume(lex) -> 'Optional[SQLDelete]':
if not lex.consume('DELETE'):
return None
lex.consume('FROM')
table = SQLNamedTable.parse(lex, is_write=True)
lex.expect('WHERE')
where_expr = SQLExpr.parse(lex)
return SQLDelete(table, where_expr)
@dataclass(frozen=True)
class SQLUpdate(SQLDML):
table_name: SQLNode
update_fields: SQLNodeList[SQLIdentifierPath]
update_exprs: SQLNodeList[SQLNode]
from_tables: SQLNode
where_expr: SQLNode
# TODO(scannell) honour compact mode
def sqlf(self, compact):
tname = [TB('UPDATE '), self.table_name.sqlf(compact)]
updatestmt = [
LB(tname),
TB('SET'),
]
for i in range(len(self.update_fields)):
field = self.update_fields[i]
expr = self.update_exprs[i]
if i == len(self.update_fields)-1:
updatestmt.append(LB([
field.sqlf(False), TB(' = '), expr.sqlf(False)
]))
else:
updatestmt.append(LB([
field.sqlf(False), TB(' = '), expr.sqlf(False),
TB(',')
]))
if self.from_tables:
updatestmt.append(LB([
TB(' FROM '),
self.from_tables.sqlf(False)
]))
if self.where_expr:
updatestmt.append(LB([
TB(' WHERE '),
self.where_expr.sqlf(False)
]))
return SB(updatestmt)
@staticmethod
def consume(lex) -> 'Optional[SQLUpdate]':
if not lex.consume('UPDATE'):
return None
table_name = SQLNamedTable.parse(lex, is_write=True)
lex.expect('SET')
update_fields = []
update_exprs = []
while True:
update_fields.append(SQLIdentifierPath.parse(lex))
lex.expect('=')
update_exprs.append(SQLExpr.parse(lex))
if not lex.consume(','):
break
from_tables = lex.consume('FROM') and SQLFrom.parse(lex)
where_expr = lex.consume('WHERE') and SQLExpr.parse(lex)
return SQLUpdate(table_name,
SQLNodeList(update_fields),
SQLNodeList(update_exprs),
from_tables, where_expr)
@dataclass(frozen=True)
class SQLOption(SQLDML):
key: str
val: SQLExpr
def sqlf(self, compact):
compact_sql = LB([TB(key), TB('='), TB(self.val.sqlf(True))])
if compact:
return compact_sql
return CB([
compact_sql,
SB([
LB([TB(key), TB(' =')]),
IB(self.val.sqlf(False)),
])
])
@staticmethod
def parse(lex) -> 'SQLOption':
key = lex.consumer_identifier()
lex.expect('=')
val = SQLExpr.parse(lex)
return SQLOption(key, val)
@dataclass(frozen=True)
class SQLOptions(SQLDML):
options: SQLNodeList[SQLOption]
def sqlf(self, compact):
lines = [TB('OPTIONS(')]
lines.extend(with_commas(True, self.options))
lines.append(TB(')'))
if compact:
return LB(lines)
stack = [TB('OPTIONS(')]
stack.extend(IB(WB(with_commas(False, self.options))))
stack.append(TB(')'))
return CB([
LB(lines),
SB(stack),
])
@staticmethod
def consume(lex) -> 'Optional[SQLOptions]':
if not lex.consume('OPTIONS'):
return None
lex.expect('(')
options: List[SQLOption] = []
while True:
options.append(SQLOption.parse(lex))
if not lex.consume(','):
break
lex.expect(')')
return SQLOptions(SQLNodeList(options))
@dataclass(frozen=True)
class SQLColumn(SQLDML):
name: str
col_type: SQLType
nullable: bool
opts: Optional[SQLOptions]
def sqlf(self, compact):
lines = [TB(self.name), TB(' '),
self.col_type.sqlf(True)]
if not self.nullable:
lines.append(TB(' NOT NULL'))
if self.opts:
lines.append(TB(' '))
lines.append(self.opts.sqlf(True))
if compact:
return LB(lines)
# Indented block for non-compact
iblock = [self.col_type.sqlf(False)]
if not self.nullable:
iblock.append(TB('NOT NULL'))
if self.opts:
iblock.append(self.opts.sqlf(False))
return CB([
LB(lines),
SB([
TB(self.name),
IB(SB(iblock)),
])
])
@staticmethod
def parse(lex) -> 'SQLColumn':
name = (lex.consume_identifier() or
lex.error('expected column name'))
col_type = SQLType.parse(lex)
# Get whether nullable
nullable = True
if lex.consume('NOT'):
lex.expect('NULL')
nullable = False
# Any options
opts = SQLOptions.consume(lex)
return SQLColumn(
name, col_type,
nullable, opts
)
@dataclass(frozen=True)
class SQLCreate(SQLDML):
clause: str
table: SQLTableSource
columns: SQLNodeList[SQLColumn]
options: Optional[SQLOptions]
query: Optional[SQLNode]
def sqlf(self, compact):
lines = [TB(self.clause), TB(' ')]
lines.append(self.table.sqlf(True))
if self.columns:
lines.append(TB(' ('))
lines.extend(with_commas(True, self.columns))
lines.append(TB(')'))
if self.options:
lines.append(TB(' '))
lines.append(self.options.sqlf(True))
if self.query:
lines.append(TB(' AS '))
lines.append(self.query.sqlf(True))
if compact:
return LB(lines)
stack = [LB([TB(self.clause),TB(' '),self.table.sqlf(True)])]
if self.columns:
stack.extend([
TB('('),
IB(WB(
with_commas(False, self.columns),
)),
TB(')'),
])
if self.options:
stack.append(self.options.sqlf(False))
if self.query:
stack.extend([
TB('AS'),
self.query.sqlf(False),
])
return CB([
LB(lines),
SB(stack),
])
@staticmethod
def consume(lex):
if not lex.consume('CREATE'):
return None
# Find CFREATE OR REPLACE TABLE clause
clause = None
if lex.consume('OR'):
lex.expect('REPLACE')
lex.expect('TABLE')
clause = 'CREATE OR REPLACE TABLE'
else:
lex.expect('TABLE')
# Check for IF NOT EXISTS
if lex.consume('IF'):
lex.expect('NOT')
lex.expect('EXISTS')
clause = 'CREATE TABLE IF NOT EXISTS'
else:
clause = 'CREATE TABLE'
# Parse table
table = SQLNamedTable.parse_no_alias(lex, is_write=True)
# Parse columns
columns: List[SQLColumn] = []
if lex.consume('('):
while True:
columns.append(SQLColumn.parse(lex))
if not lex.consume(','):
break
lex.expect(')')
partition_by = None
if lex.consume('PARTITION'):
lex.expect('BY')
# TODO: Finish
lex.error('PARTITION BY unimplemented')
cluster_by = None
if lex.consume('CLUSTER'):
lex.expect('BY')
# TODO: Finish
lex.error('CLUSTER BY unimplemented')
# Find options
options = SQLOptions.consume(lex)
# Find query-for-creation (if any)
query = None
if lex.consume('AS'):
query = SQLQuery.parse(lex)
return SQLCreate(clause, table, columns, options, query)