execsql2 2.37.0__py3-none-any.whl → 2.38.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- execsql/api.py +95 -45
- execsql/cli/commands/run.py +2 -2
- execsql/cli/lint.py +6 -1
- execsql/cli/run.py +5 -0
- execsql/config.py +7 -0
- execsql/data/execsql.conf.template +21 -0
- execsql/db/base.py +52 -24
- execsql/db/duckdb.py +43 -4
- execsql/db/firebird.py +8 -0
- execsql/db/mysql.py +8 -3
- execsql/db/oracle.py +5 -0
- execsql/db/postgres.py +2 -2
- execsql/db/sqlite.py +117 -10
- execsql/db/sqlserver.py +11 -7
- execsql/db/target_types.py +151 -4
- execsql/debug/repl.py +2 -1
- execsql/exceptions.py +14 -0
- execsql/exporters/html.py +26 -42
- execsql/exporters/ods.py +93 -21
- execsql/exporters/sqlite.py +82 -47
- execsql/format.py +12 -4
- execsql/gui/desktop.py +2 -1
- execsql/gui/tui.py +5 -2
- execsql/importers/base.py +9 -7
- execsql/importers/json.py +37 -13
- execsql/importers/ods.py +22 -2
- execsql/lsp/document.py +6 -1
- execsql/lsp/hover.py +2 -1
- execsql/metacommands/conditions.py +3 -1
- execsql/metacommands/connect.py +18 -3
- execsql/metacommands/dispatch.py +10 -0
- execsql/metacommands/io.py +1 -0
- execsql/metacommands/io_export.py +1 -2
- execsql/metacommands/io_fileops.py +26 -90
- execsql/metacommands/io_import.py +26 -8
- execsql/metacommands/reference.py +7 -0
- execsql/parser.py +14 -37
- execsql/script/control.py +17 -7
- execsql/script/executor.py +160 -34
- execsql/script/preparation.py +42 -0
- execsql/types.py +6 -5
- execsql/utils/errors.py +2 -2
- execsql/utils/fileio.py +342 -134
- execsql/utils/spreadsheet.py +74 -0
- execsql/utils/strings.py +13 -7
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/README.md +5 -5
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/example_config_prompt.sql +2 -2
- {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/METADATA +32 -119
- {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/RECORD +64 -62
- {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/WHEEL +1 -1
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/script_template.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/entry_points.txt +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/licenses/LICENSE.txt +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/licenses/NOTICE +0 -0
execsql/exporters/html.py
CHANGED
|
@@ -28,6 +28,26 @@ from execsql.utils.fileio import filewriter_close
|
|
|
28
28
|
__all__ = ["export_html", "export_cgi_html", "write_query_to_html", "write_query_to_cgi_html"]
|
|
29
29
|
|
|
30
30
|
|
|
31
|
+
def _write_table(f: Any, hdrs: list[str], rows: Any, desc: str | None) -> None:
|
|
32
|
+
"""Write the table fragment shared by standalone and CGI HTML exports."""
|
|
33
|
+
f.write("<table>\n")
|
|
34
|
+
if desc is not None:
|
|
35
|
+
f.write(f"<caption>{desc}</caption>\n")
|
|
36
|
+
f.write("<thead><tr>")
|
|
37
|
+
for h in hdrs:
|
|
38
|
+
f.write(f"<th>{html_mod.escape(str(h))}</th>")
|
|
39
|
+
f.write("</tr></thead>\n<tbody>\n")
|
|
40
|
+
for r in rows:
|
|
41
|
+
f.write("<tr>")
|
|
42
|
+
for v in r:
|
|
43
|
+
# `if v is not None`, not `if v`: 0, False, and an empty
|
|
44
|
+
# string are all falsy, and a zero measurement silently became
|
|
45
|
+
# an empty cell.
|
|
46
|
+
f.write(f"<td>{html_mod.escape(str(v)) if v is not None else ''}</td>")
|
|
47
|
+
f.write("</tr>\n")
|
|
48
|
+
f.write("</tbody>\n</table>\n")
|
|
49
|
+
|
|
50
|
+
|
|
31
51
|
def export_html(
|
|
32
52
|
outfile: str,
|
|
33
53
|
hdrs: list[str],
|
|
@@ -40,24 +60,6 @@ def export_html(
|
|
|
40
60
|
"""Write a complete HTML document containing a data table to a file or ZIP archive."""
|
|
41
61
|
conf = _state.conf
|
|
42
62
|
|
|
43
|
-
def write_table(f):
|
|
44
|
-
f.write("<table>\n")
|
|
45
|
-
if desc is not None:
|
|
46
|
-
f.write(f"<caption>{desc}</caption>\n")
|
|
47
|
-
f.write("<thead><tr>")
|
|
48
|
-
for h in hdrs:
|
|
49
|
-
f.write(f"<th>{html_mod.escape(str(h))}</th>")
|
|
50
|
-
f.write("</tr></thead>\n<tbody>\n")
|
|
51
|
-
for r in rows:
|
|
52
|
-
f.write("<tr>")
|
|
53
|
-
for v in r:
|
|
54
|
-
# `if v is not None`, not `if v`: 0, False, and an empty
|
|
55
|
-
# string are all falsy, and a zero measurement silently became
|
|
56
|
-
# an empty cell.
|
|
57
|
-
f.write(f"<td>{html_mod.escape(str(v)) if v is not None else ''}</td>")
|
|
58
|
-
f.write("</tr>\n")
|
|
59
|
-
f.write("</tbody>\n</table>\n")
|
|
60
|
-
|
|
61
63
|
script, lno = current_script_line()
|
|
62
64
|
# If not append, write a complete HTML document with header and table.
|
|
63
65
|
# If append and the file does not exist, write just the table.
|
|
@@ -112,7 +114,7 @@ def export_html(
|
|
|
112
114
|
f.write("tbody tr { border-bottom: 1px dotted #814324; }\n")
|
|
113
115
|
f.write("</style>")
|
|
114
116
|
f.write("\n</head>\n<body>\n")
|
|
115
|
-
|
|
117
|
+
_write_table(f, hdrs, rows, desc)
|
|
116
118
|
f.write("</body>\n</html>\n")
|
|
117
119
|
finally:
|
|
118
120
|
if outfile.lower() != "stdout":
|
|
@@ -120,14 +122,14 @@ def export_html(
|
|
|
120
122
|
elif not zipfile and append:
|
|
121
123
|
if outfile.lower() == "stdout":
|
|
122
124
|
f = sys.stdout
|
|
123
|
-
|
|
125
|
+
_write_table(f, hdrs, rows, desc)
|
|
124
126
|
elif not Path(outfile).is_file():
|
|
125
127
|
from execsql.utils.fileio import EncodedFile
|
|
126
128
|
|
|
127
129
|
ef = EncodedFile(outfile, conf.output_encoding)
|
|
128
130
|
f = ef.open("wt")
|
|
129
131
|
try:
|
|
130
|
-
|
|
132
|
+
_write_table(f, hdrs, rows, desc)
|
|
131
133
|
finally:
|
|
132
134
|
f.close()
|
|
133
135
|
else:
|
|
@@ -152,7 +154,7 @@ def export_html(
|
|
|
152
154
|
else:
|
|
153
155
|
t.write(line)
|
|
154
156
|
t.write("\n")
|
|
155
|
-
|
|
157
|
+
_write_table(t, hdrs, rows, desc)
|
|
156
158
|
t.write(remainder)
|
|
157
159
|
for line in f:
|
|
158
160
|
t.write(line)
|
|
@@ -183,24 +185,6 @@ def export_cgi_html(
|
|
|
183
185
|
"""Write a CGI-style HTML fragment (Content-Type header + table) to a file or ZIP archive."""
|
|
184
186
|
conf = _state.conf
|
|
185
187
|
|
|
186
|
-
def write_table(f):
|
|
187
|
-
f.write("<table>\n")
|
|
188
|
-
if desc is not None:
|
|
189
|
-
f.write(f"<caption>{desc}</caption>\n")
|
|
190
|
-
f.write("<thead><tr>")
|
|
191
|
-
for h in hdrs:
|
|
192
|
-
f.write(f"<th>{html_mod.escape(str(h))}</th>")
|
|
193
|
-
f.write("</tr></thead>\n<tbody>\n")
|
|
194
|
-
for r in rows:
|
|
195
|
-
f.write("<tr>")
|
|
196
|
-
for v in r:
|
|
197
|
-
# `if v is not None`, not `if v`: 0, False, and an empty
|
|
198
|
-
# string are all falsy, and a zero measurement silently became
|
|
199
|
-
# an empty cell.
|
|
200
|
-
f.write(f"<td>{html_mod.escape(str(v)) if v is not None else ''}</td>")
|
|
201
|
-
f.write("</tr>\n")
|
|
202
|
-
f.write("</tbody>\n</table>\n")
|
|
203
|
-
|
|
204
188
|
script, lno = current_script_line()
|
|
205
189
|
if zipfile or not append or (append and not Path(outfile).is_file()):
|
|
206
190
|
if zipfile is None:
|
|
@@ -216,7 +200,7 @@ def export_cgi_html(
|
|
|
216
200
|
f = ZipWriter(zipfile, outfile, append)
|
|
217
201
|
try:
|
|
218
202
|
f.write("Content-Type: text/html\n\n")
|
|
219
|
-
|
|
203
|
+
_write_table(f, hdrs, rows, desc)
|
|
220
204
|
finally:
|
|
221
205
|
if outfile.lower() != "stdout":
|
|
222
206
|
f.close()
|
|
@@ -229,7 +213,7 @@ def export_cgi_html(
|
|
|
229
213
|
ef = EncodedFile(outfile, conf.output_encoding)
|
|
230
214
|
f = ef.open("a")
|
|
231
215
|
try:
|
|
232
|
-
|
|
216
|
+
_write_table(f, hdrs, rows, desc)
|
|
233
217
|
finally:
|
|
234
218
|
if outfile.lower() != "stdout":
|
|
235
219
|
f.close()
|
execsql/exporters/ods.py
CHANGED
|
@@ -24,6 +24,7 @@ from execsql.script import current_script_line
|
|
|
24
24
|
from execsql.utils.errors import exception_desc, fatal_error, install_hint
|
|
25
25
|
from execsql.utils.fileio import filewriter_close
|
|
26
26
|
from execsql.utils.strings import unquoted
|
|
27
|
+
from execsql.utils.spreadsheet import CellTextBudget, SheetBudget
|
|
27
28
|
|
|
28
29
|
__all__ = ["OdsFile", "export_ods", "write_query_to_ods", "write_queries_to_ods"]
|
|
29
30
|
|
|
@@ -179,26 +180,36 @@ class OdsFile:
|
|
|
179
180
|
return sheet
|
|
180
181
|
return None
|
|
181
182
|
|
|
182
|
-
def sheet_data(self, sheetname: Any, junk_header_rows: int = 0) -> list:
|
|
183
|
-
"""
|
|
183
|
+
def sheet_data(self, sheetname: Any, junk_header_rows: int = 0, *, budget: SheetBudget | None = None) -> list:
|
|
184
|
+
"""Read a sheet after SKIP, checking optional budgets before expanding rows or padding."""
|
|
184
185
|
sheet = self.sheet_named(sheetname)
|
|
185
186
|
if not sheet:
|
|
186
187
|
raise OdsFileError(f"There is no sheet named {sheetname}")
|
|
187
188
|
|
|
188
189
|
rows: list[list | None] = []
|
|
189
190
|
pending_empty = 0
|
|
191
|
+
skip_rows = max(0, junk_header_rows)
|
|
190
192
|
for sheetrow in sheet.getElementsByType(of.table.TableRow):
|
|
191
|
-
|
|
192
|
-
|
|
193
|
+
repeat = _expansion_count(sheetrow, "numberrowsrepeated")
|
|
194
|
+
skipped = min(skip_rows, repeat)
|
|
195
|
+
skip_rows -= skipped
|
|
196
|
+
repeat -= skipped
|
|
197
|
+
if repeat == 0:
|
|
198
|
+
_validate_expansion_counts(sheetrow)
|
|
199
|
+
continue
|
|
200
|
+
data = _row_values(sheetrow, budget, row_repeat=repeat, pending_rows=pending_empty)
|
|
193
201
|
if not data:
|
|
194
202
|
# Empty rows are kept only when data follows them, so the
|
|
195
203
|
# filler rows a spreadsheet writes below the data are dropped.
|
|
196
204
|
pending_empty += repeat
|
|
197
205
|
continue
|
|
206
|
+
if budget is not None:
|
|
207
|
+
budget.add_row(data, repeat, pending_empty)
|
|
198
208
|
rows.extend(None for _ in range(pending_empty))
|
|
199
209
|
pending_empty = 0
|
|
200
210
|
rows.extend(list(data) for _ in range(repeat))
|
|
201
|
-
|
|
211
|
+
if junk_header_rows < 0:
|
|
212
|
+
rows = rows[junk_header_rows:]
|
|
202
213
|
# Every row is as wide as the widest, so a blank trailing header is
|
|
203
214
|
# still seen, and an empty row inside the data is a row of NULLs that
|
|
204
215
|
# EMPTY_ROWS keeps or drops.
|
|
@@ -309,20 +320,55 @@ class OdsFile:
|
|
|
309
320
|
self.wbk = None
|
|
310
321
|
|
|
311
322
|
|
|
312
|
-
def
|
|
323
|
+
def _expansion_count(node: Any, attribute: str) -> int:
|
|
324
|
+
"""Read a non-negative ODS repeat/span/space count without expanding it."""
|
|
325
|
+
value = node.getAttribute(attribute) or "1"
|
|
326
|
+
try:
|
|
327
|
+
count = int(value)
|
|
328
|
+
if count < 0:
|
|
329
|
+
raise ValueError("negative count")
|
|
330
|
+
except (ValueError, TypeError) as exc:
|
|
331
|
+
shown = value[:120] + "..." if isinstance(value, str) and len(value) > 120 else value
|
|
332
|
+
raise ErrInfo(type="error", other_msg=f"Invalid ODS expansion count {shown!r} for {attribute}.") from exc
|
|
333
|
+
return count
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _validate_expansion_counts(node: Any) -> None:
|
|
337
|
+
"""Validate skipped content without allocating its repeated rows or text."""
|
|
338
|
+
qname = getattr(node, "qname", None)
|
|
339
|
+
if qname == (of.table.TABLENS, "table-row"):
|
|
340
|
+
_expansion_count(node, "numberrowsrepeated")
|
|
341
|
+
elif qname in ((of.table.TABLENS, "table-cell"), (of.table.TABLENS, "covered-table-cell")):
|
|
342
|
+
_expansion_count(node, "numbercolumnsrepeated")
|
|
343
|
+
if qname == (of.table.TABLENS, "table-cell"):
|
|
344
|
+
_expansion_count(node, "numbercolumnsspanned")
|
|
345
|
+
_expansion_count(node, "numberrowsspanned")
|
|
346
|
+
elif qname == (of.text.TEXTNS, "s"):
|
|
347
|
+
_expansion_count(node, "c")
|
|
348
|
+
for child in getattr(node, "childNodes", ()):
|
|
349
|
+
_validate_expansion_counts(child)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _paragraph_text(node: Any, budget: CellTextBudget | None = None) -> str:
|
|
313
353
|
"""Return the text of a ``<text:p>``, expanding ODF's space, tab and line-break elements."""
|
|
314
354
|
parts: list[str] = []
|
|
315
355
|
for child in node.childNodes:
|
|
316
356
|
if child.nodeType == child.TEXT_NODE:
|
|
317
|
-
|
|
357
|
+
value = str(child)
|
|
358
|
+
if budget is not None:
|
|
359
|
+
budget.add(len(value))
|
|
318
360
|
elif child.qname == (of.text.TEXTNS, "s"):
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
361
|
+
count = _expansion_count(child, "c")
|
|
362
|
+
if budget is not None:
|
|
363
|
+
budget.add(count)
|
|
364
|
+
value = " " * count
|
|
365
|
+
elif child.qname in ((of.text.TEXTNS, "tab"), (of.text.TEXTNS, "line-break")):
|
|
366
|
+
if budget is not None:
|
|
367
|
+
budget.add(1)
|
|
368
|
+
value = "\t" if child.qname == (of.text.TEXTNS, "tab") else "\n"
|
|
324
369
|
else:
|
|
325
|
-
|
|
370
|
+
value = _paragraph_text(child, budget)
|
|
371
|
+
parts.append(value)
|
|
326
372
|
return "".join(parts)
|
|
327
373
|
|
|
328
374
|
|
|
@@ -336,7 +382,7 @@ def _time_text(duration: str) -> str:
|
|
|
336
382
|
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}{fraction}"
|
|
337
383
|
|
|
338
384
|
|
|
339
|
-
def _cell_value(cell: Any) -> str | None:
|
|
385
|
+
def _cell_value(cell: Any, budget: CellTextBudget | None = None) -> str | None:
|
|
340
386
|
"""Return a cell's stored value as text, or ``None`` for an empty cell.
|
|
341
387
|
|
|
342
388
|
The value comes from the cell's ``office:value-type`` and value
|
|
@@ -361,25 +407,39 @@ def _cell_value(cell: Any) -> str | None:
|
|
|
361
407
|
else:
|
|
362
408
|
value = None
|
|
363
409
|
if value:
|
|
410
|
+
if budget is not None:
|
|
411
|
+
budget.add(len(value))
|
|
364
412
|
return value
|
|
365
413
|
if vtype == "string":
|
|
366
414
|
value = cell.getAttribute("stringvalue")
|
|
367
415
|
if value:
|
|
416
|
+
if budget is not None:
|
|
417
|
+
budget.add(len(value))
|
|
368
418
|
return value
|
|
369
419
|
# A string cell, a cell with no type, or a typed cell missing its value
|
|
370
420
|
# attribute: the paragraphs are the value, one line each.
|
|
371
|
-
paragraphs = [
|
|
372
|
-
|
|
373
|
-
|
|
421
|
+
paragraphs: list[str] = []
|
|
422
|
+
for child in cell.childNodes:
|
|
423
|
+
if getattr(child, "qname", None) == (of.text.TEXTNS, "p"):
|
|
424
|
+
if paragraphs and budget is not None:
|
|
425
|
+
budget.add(1)
|
|
426
|
+
paragraphs.append(_paragraph_text(child, budget))
|
|
374
427
|
text = "\n".join(paragraphs)
|
|
375
428
|
return text or None
|
|
376
429
|
|
|
377
430
|
|
|
378
|
-
def _row_values(
|
|
431
|
+
def _row_values(
|
|
432
|
+
sheetrow: Any,
|
|
433
|
+
budget: SheetBudget | None = None,
|
|
434
|
+
*,
|
|
435
|
+
row_repeat: int = 1,
|
|
436
|
+
pending_rows: int = 0,
|
|
437
|
+
) -> list:
|
|
379
438
|
"""Return one value per column of a ``<table:table-row>``, without trailing empty cells."""
|
|
380
439
|
values: list = []
|
|
381
440
|
pending_empty = 0
|
|
382
441
|
covered_by_span = 0
|
|
442
|
+
row_text = 0
|
|
383
443
|
for cell in sheetrow.childNodes:
|
|
384
444
|
qname = getattr(cell, "qname", None)
|
|
385
445
|
if qname == (of.table.TABLENS, "table-cell"):
|
|
@@ -388,7 +448,7 @@ def _row_values(sheetrow: Any) -> list:
|
|
|
388
448
|
covered = True
|
|
389
449
|
else:
|
|
390
450
|
continue
|
|
391
|
-
repeat =
|
|
451
|
+
repeat = _expansion_count(cell, "numbercolumnsrepeated")
|
|
392
452
|
if covered:
|
|
393
453
|
# A horizontal merge repeats its value across the columns it
|
|
394
454
|
# covers (below); covered cells left over belong to a merge from
|
|
@@ -398,11 +458,20 @@ def _row_values(sheetrow: Any) -> list:
|
|
|
398
458
|
repeat -= skip
|
|
399
459
|
value = None
|
|
400
460
|
else:
|
|
401
|
-
|
|
402
|
-
|
|
461
|
+
spanned = _expansion_count(cell, "numbercolumnsspanned")
|
|
462
|
+
_expansion_count(cell, "numberrowsspanned")
|
|
403
463
|
if spanned > 1:
|
|
404
464
|
covered_by_span = spanned - 1
|
|
405
465
|
repeat *= spanned
|
|
466
|
+
if repeat == 0:
|
|
467
|
+
_validate_expansion_counts(cell)
|
|
468
|
+
continue
|
|
469
|
+
text_budget = (
|
|
470
|
+
CellTextBudget(budget, budget.text_chars + row_text * row_repeat, repeat * row_repeat)
|
|
471
|
+
if budget is not None
|
|
472
|
+
else None
|
|
473
|
+
)
|
|
474
|
+
value = _cell_value(cell, text_budget)
|
|
406
475
|
if repeat <= 0:
|
|
407
476
|
continue
|
|
408
477
|
if value is None:
|
|
@@ -410,6 +479,9 @@ def _row_values(sheetrow: Any) -> list:
|
|
|
410
479
|
# repeated across the whole sheet width costs nothing.
|
|
411
480
|
pending_empty += repeat
|
|
412
481
|
continue
|
|
482
|
+
if budget is not None:
|
|
483
|
+
budget.check_row(len(values) + pending_empty + repeat, row_repeat, pending_rows)
|
|
484
|
+
row_text += len(value) * repeat
|
|
413
485
|
values.extend([None] * pending_empty)
|
|
414
486
|
pending_empty = 0
|
|
415
487
|
values.extend([value] * repeat)
|
execsql/exporters/sqlite.py
CHANGED
|
@@ -7,11 +7,10 @@ Provides :func:`write_query_to_sqlite`, which writes a query result set
|
|
|
7
7
|
to a table in an SQLite database file. Used by ``EXPORT … FORMAT sqlite``.
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
|
-
import math
|
|
11
10
|
from decimal import Decimal
|
|
12
|
-
from pathlib import Path
|
|
13
11
|
from typing import Any
|
|
14
12
|
|
|
13
|
+
from execsql.db.sqlite import sqlite_affinity as _affinity
|
|
15
14
|
from execsql.exceptions import ErrInfo
|
|
16
15
|
from execsql.types import dbt_sqlite
|
|
17
16
|
|
|
@@ -20,6 +19,23 @@ _SQLITE_SAFE = (int, float, str, bytes)
|
|
|
20
19
|
__all__ = ["export_sqlite", "write_query_to_sqlite"]
|
|
21
20
|
|
|
22
21
|
|
|
22
|
+
def _exact_number(value: Decimal, affinity: str) -> int | float:
|
|
23
|
+
"""Bind a Decimal only if its numeric readback retains the source value."""
|
|
24
|
+
if value.is_nan():
|
|
25
|
+
raise ValueError("NaN has no SQLite numeric representation")
|
|
26
|
+
if value.is_infinite():
|
|
27
|
+
return float(value)
|
|
28
|
+
if -(2**63) <= value <= 2**63 - 1 and value == value.to_integral_value():
|
|
29
|
+
number = int(value)
|
|
30
|
+
if affinity == "REAL" and Decimal(repr(float(number))) != value:
|
|
31
|
+
raise ValueError("the integer is not exactly representable in a SQLite REAL")
|
|
32
|
+
return number
|
|
33
|
+
result = float(value)
|
|
34
|
+
if Decimal(repr(result)) != value:
|
|
35
|
+
raise ValueError("the decimal cannot be read back without losing digits")
|
|
36
|
+
return result
|
|
37
|
+
|
|
38
|
+
|
|
23
39
|
def export_sqlite(
|
|
24
40
|
outfile: str,
|
|
25
41
|
hdrs: list[str],
|
|
@@ -27,60 +43,79 @@ def export_sqlite(
|
|
|
27
43
|
append: bool,
|
|
28
44
|
tablename: str,
|
|
29
45
|
) -> None:
|
|
30
|
-
"""
|
|
46
|
+
"""Export rows, rejecting lossy decimals before opening the destination.
|
|
47
|
+
|
|
48
|
+
Numeric Decimal values must read back equal to their source; spelling and
|
|
49
|
+
scale are not retained. Text columns retain their string representation.
|
|
50
|
+
Replacement DDL and inserts share a transaction, restored on any failure.
|
|
51
|
+
APPEND still refuses an existing table.
|
|
52
|
+
"""
|
|
31
53
|
import sqlite3
|
|
32
54
|
|
|
33
55
|
from execsql.models import DataTable
|
|
34
56
|
|
|
35
|
-
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
|
|
57
|
+
rowdata = list(rows)
|
|
58
|
+
# Non-finite Decimals cannot pass the general-purpose integer scanner.
|
|
59
|
+
# A fractional placeholder preserves numeric inference without changing data.
|
|
60
|
+
scanrows = (
|
|
61
|
+
tuple(Decimal("0.5") if isinstance(v, Decimal) and not v.is_finite() else v for v in row) for row in rowdata
|
|
62
|
+
)
|
|
63
|
+
tablespec = DataTable(hdrs, scanrows, infer_strings=False)
|
|
64
|
+
create_sql = tablespec.create_table(dbt_sqlite, schemaname=None, tablename=tablename)
|
|
39
65
|
qtable = dbt_sqlite.quoted(tablename)
|
|
40
|
-
|
|
41
|
-
|
|
66
|
+
# Let SQLite expose the generated declarations, including quoted identifiers.
|
|
67
|
+
probe = sqlite3.connect(":memory:")
|
|
42
68
|
try:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
probe.execute(create_sql)
|
|
70
|
+
affinities = [_affinity(col[2]) for col in probe.execute(f"PRAGMA table_info({qtable})")]
|
|
71
|
+
converted = []
|
|
72
|
+
for row in rowdata:
|
|
73
|
+
bound: list[Any] = []
|
|
74
|
+
for i, value in enumerate(row):
|
|
75
|
+
if isinstance(value, Decimal) and i < len(affinities) and affinities[i] not in ("TEXT", "BLOB"):
|
|
76
|
+
try:
|
|
77
|
+
number = _exact_number(value, affinities[i])
|
|
78
|
+
if value.is_finite():
|
|
79
|
+
# Preserve the previous stored value too: SQLite's text
|
|
80
|
+
# parser can round differently from Python's conversion.
|
|
81
|
+
cast_type = "REAL" if affinities[i] == "REAL" else "NUMERIC"
|
|
82
|
+
previous = probe.execute(f"SELECT CAST(? AS {cast_type})", (str(value),)).fetchone()[0]
|
|
83
|
+
if Decimal(repr(previous)) != value:
|
|
84
|
+
raise ValueError("SQLite's numeric conversion loses source digits")
|
|
85
|
+
bound.append(number)
|
|
86
|
+
except ValueError as exc:
|
|
87
|
+
raise ErrInfo(
|
|
88
|
+
type="error",
|
|
89
|
+
other_msg=(
|
|
90
|
+
f"Can't export column {hdrs[i]} to SQLite table {tablename}: {value} "
|
|
91
|
+
f"cannot be stored without losing digits. Nothing was written. "
|
|
92
|
+
"To preserve the exact text, export CAST(column AS VARCHAR)."
|
|
93
|
+
),
|
|
94
|
+
) from exc
|
|
55
95
|
else:
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
n_chunks = math.ceil(len(rowdata) / chunksize)
|
|
77
|
-
for i in range(n_chunks):
|
|
78
|
-
start = i * chunksize
|
|
79
|
-
end = start + chunksize
|
|
80
|
-
curs.executemany(sql, rowdata[start:end])
|
|
96
|
+
bound.append(value if value is None or isinstance(value, _SQLITE_SAFE) else str(value))
|
|
97
|
+
converted.append(tuple(bound))
|
|
98
|
+
finally:
|
|
99
|
+
probe.close()
|
|
100
|
+
|
|
101
|
+
sdb = sqlite3.connect(outfile, isolation_level=None)
|
|
102
|
+
try:
|
|
103
|
+
sdb.execute("BEGIN")
|
|
104
|
+
exists = sdb.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (tablename,)).fetchone()
|
|
105
|
+
if exists is not None:
|
|
106
|
+
if append:
|
|
107
|
+
raise ErrInfo(type="error", other_msg=f"The table {tablename} already exists in {outfile}.")
|
|
108
|
+
sdb.execute(f"DROP TABLE {qtable}")
|
|
109
|
+
sdb.execute(create_sql)
|
|
110
|
+
columns = ",".join(dbt_sqlite.quoted(col) for col in hdrs)
|
|
111
|
+
parameters = ",".join("?" for _ in hdrs)
|
|
112
|
+
insert_sql = f"INSERT INTO {qtable} ({columns}) VALUES ({parameters})"
|
|
113
|
+
chunksize = 10000
|
|
114
|
+
for start in range(0, len(converted), chunksize):
|
|
115
|
+
sdb.executemany(insert_sql, converted[start : start + chunksize])
|
|
81
116
|
sdb.commit()
|
|
82
|
-
curs.close()
|
|
83
117
|
finally:
|
|
118
|
+
# close() rolls back an open transaction, including on BaseException.
|
|
84
119
|
sdb.close()
|
|
85
120
|
|
|
86
121
|
|
execsql/format.py
CHANGED
|
@@ -1032,12 +1032,20 @@ def format_sql_block(
|
|
|
1032
1032
|
layout_lines = [line for i, line in enumerate(lines) if line.strip() and i not in interior]
|
|
1033
1033
|
base = min((len(line) - len(line.lstrip()) for line in layout_lines), default=0)
|
|
1034
1034
|
target_prefix = " " * (depth * indent)
|
|
1035
|
-
rebased = [
|
|
1035
|
+
rebased = []
|
|
1036
|
+
for i, line in enumerate(lines):
|
|
1037
|
+
if i in interior:
|
|
1038
|
+
rebased.append(line)
|
|
1039
|
+
elif line.strip():
|
|
1040
|
+
rebased.append(line[base:])
|
|
1041
|
+
else:
|
|
1042
|
+
rebased.append("")
|
|
1036
1043
|
|
|
1037
1044
|
if not use_sql:
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1045
|
+
for i, line in enumerate(rebased):
|
|
1046
|
+
if i not in interior and line.strip():
|
|
1047
|
+
rebased[i] = target_prefix + line
|
|
1048
|
+
return rebased
|
|
1041
1049
|
|
|
1042
1050
|
# When comments appear mid-statement, use the marker-based approach
|
|
1043
1051
|
# which preserves both comments AND sqlglot formatting. When all
|
execsql/gui/desktop.py
CHANGED
|
@@ -15,6 +15,7 @@ import time
|
|
|
15
15
|
from typing import Any, cast
|
|
16
16
|
from collections.abc import Callable
|
|
17
17
|
|
|
18
|
+
from execsql.exceptions import ScriptExit
|
|
18
19
|
from execsql.gui.base import GuiBackend
|
|
19
20
|
|
|
20
21
|
# ---------------------------------------------------------------------------
|
|
@@ -1513,7 +1514,7 @@ class _TkinterSyncQueue:
|
|
|
1513
1514
|
except Exception as exc:
|
|
1514
1515
|
result = {"error": str(exc), "button": None}
|
|
1515
1516
|
if result is not None and spec.gui_type in self._EXIT_ON_CANCEL and result.get("button") is None:
|
|
1516
|
-
raise
|
|
1517
|
+
raise ScriptExit(2)
|
|
1517
1518
|
spec.return_queue.put(result if result is not None else {})
|
|
1518
1519
|
|
|
1519
1520
|
def get_nowait(self) -> Any:
|
execsql/gui/tui.py
CHANGED
|
@@ -23,6 +23,7 @@ import queue as _stdlib_queue
|
|
|
23
23
|
import threading
|
|
24
24
|
from typing import Any, Literal, cast
|
|
25
25
|
|
|
26
|
+
from execsql.exceptions import ScriptExit
|
|
26
27
|
from execsql.gui.base import GuiBackend
|
|
27
28
|
|
|
28
29
|
# ---------------------------------------------------------------------------
|
|
@@ -1449,7 +1450,7 @@ class _TextualSyncQueue:
|
|
|
1449
1450
|
spec.return_queue.put({"error": str(exc), "button": None})
|
|
1450
1451
|
return
|
|
1451
1452
|
if result is not None and result.get("cancelled"):
|
|
1452
|
-
raise
|
|
1453
|
+
raise ScriptExit(2)
|
|
1453
1454
|
spec.return_queue.put(result if result is not None else {})
|
|
1454
1455
|
|
|
1455
1456
|
def get_nowait(self) -> Any:
|
|
@@ -1604,7 +1605,7 @@ class ConsoleApp(App):
|
|
|
1604
1605
|
if tid is not None:
|
|
1605
1606
|
ctypes.pythonapi.PyThreadState_SetAsyncExc(
|
|
1606
1607
|
ctypes.c_ulong(tid),
|
|
1607
|
-
ctypes.py_object(
|
|
1608
|
+
ctypes.py_object(ScriptExit),
|
|
1608
1609
|
)
|
|
1609
1610
|
spec.return_queue.put(result if result is not None else {})
|
|
1610
1611
|
else:
|
|
@@ -1651,6 +1652,8 @@ class ConsoleApp(App):
|
|
|
1651
1652
|
pass # Widget may not be mounted yet.
|
|
1652
1653
|
|
|
1653
1654
|
def action_request_quit(self) -> None:
|
|
1655
|
+
# A plain SystemExit, not ScriptExit: quitting while a statement runs
|
|
1656
|
+
# interrupts it, and it is rolled back like a failed one.
|
|
1654
1657
|
if self._script_thread and self._script_thread.is_alive():
|
|
1655
1658
|
tid = self._script_thread.ident
|
|
1656
1659
|
if tid is not None:
|
execsql/importers/base.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
1
|
"""
|
|
4
2
|
Shared data-import utilities for execsql.
|
|
5
3
|
|
|
6
|
-
Provides :func:`import_data_table`,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
Provides :func:`import_data_table`, a common back-end for row-based
|
|
5
|
+
importers. CSV also has a separate file-oriented path. Input
|
|
6
|
+
headers are processed before target changes. For REPLACEMENT, the existing
|
|
7
|
+
target is dropped before source-row scanning/type inference; a later validation
|
|
8
|
+
failure does not guarantee that the previous table survives.
|
|
9
|
+
NEW/REPLACEMENT infer column declarations from the source; existing targets
|
|
10
|
+
convert values according to their destination column types.
|
|
11
11
|
"""
|
|
12
12
|
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
13
15
|
from typing import Any
|
|
14
16
|
|
|
15
17
|
from execsql.exceptions import ErrInfo
|