execsql2 2.6.0__py3-none-any.whl → 2.8.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/cli/__init__.py +6 -0
- execsql/cli/run.py +95 -7
- execsql/exporters/__init__.py +3 -3
- execsql/exporters/delimited.py +2 -2
- execsql/exporters/markdown.py +126 -0
- execsql/exporters/xlsx.py +317 -0
- execsql/exporters/yaml.py +87 -0
- execsql/metacommands/__init__.py +25 -2
- execsql/metacommands/control.py +33 -0
- execsql/metacommands/dispatch.py +42 -0
- execsql/metacommands/io.py +2 -0
- execsql/metacommands/io_export.py +75 -0
- execsql/script/engine.py +16 -0
- execsql/state.py +10 -0
- {execsql2-2.6.0.dist-info → execsql2-2.8.0.dist-info}/METADATA +6 -2
- {execsql2-2.6.0.dist-info → execsql2-2.8.0.dist-info}/RECORD +35 -32
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/README.md +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/example_config_prompt.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/execsql.conf +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/md_compare.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/pg_upsert.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/script_template.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
- {execsql2-2.6.0.data → execsql2-2.8.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
- {execsql2-2.6.0.dist-info → execsql2-2.8.0.dist-info}/WHEEL +0 -0
- {execsql2-2.6.0.dist-info → execsql2-2.8.0.dist-info}/entry_points.txt +0 -0
- {execsql2-2.6.0.dist-info → execsql2-2.8.0.dist-info}/licenses/LICENSE.txt +0 -0
- {execsql2-2.6.0.dist-info → execsql2-2.8.0.dist-info}/licenses/NOTICE +0 -0
execsql/cli/__init__.py
CHANGED
|
@@ -254,6 +254,11 @@ def main(
|
|
|
254
254
|
"--dump-keywords",
|
|
255
255
|
help="Dump all metacommand keywords as JSON and exit.",
|
|
256
256
|
),
|
|
257
|
+
profile: bool = typer.Option(
|
|
258
|
+
False,
|
|
259
|
+
"--profile",
|
|
260
|
+
help="Record per-statement execution times and print a timing summary after the script completes.",
|
|
261
|
+
),
|
|
257
262
|
version: bool | None = typer.Option(
|
|
258
263
|
None,
|
|
259
264
|
"--version",
|
|
@@ -415,6 +420,7 @@ def main(
|
|
|
415
420
|
dsn=dsn,
|
|
416
421
|
output_dir=output_dir,
|
|
417
422
|
progress=progress,
|
|
423
|
+
profile=profile,
|
|
418
424
|
)
|
|
419
425
|
|
|
420
426
|
|
execsql/cli/run.py
CHANGED
|
@@ -20,11 +20,11 @@ from execsql.cli.dsn import _parse_connection_string
|
|
|
20
20
|
from execsql.cli.help import _console, _err_console
|
|
21
21
|
from execsql.config import ConfigData, StatObj
|
|
22
22
|
from execsql.exceptions import ConfigError, ErrInfo
|
|
23
|
-
from execsql.script import SubVarSet, current_script_line, read_sqlfile, read_sqlstring, runscripts
|
|
23
|
+
from execsql.script import SubVarSet, current_script_line, read_sqlfile, read_sqlstring, runscripts, substitute_vars
|
|
24
24
|
from execsql.utils.fileio import FileWriter, Logger, filewriter_end
|
|
25
25
|
from execsql.utils.gui import gui_connect, gui_console_isrunning, gui_console_off, gui_console_on, gui_console_wait_user
|
|
26
26
|
|
|
27
|
-
__all__ = ["_connect_initial_db", "_print_dry_run", "_run"]
|
|
27
|
+
__all__ = ["_connect_initial_db", "_print_dry_run", "_print_profile", "_run"]
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
# ---------------------------------------------------------------------------
|
|
@@ -33,7 +33,16 @@ __all__ = ["_connect_initial_db", "_print_dry_run", "_run"]
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
def _print_dry_run(cmdlist: object) -> None:
|
|
36
|
-
"""Print the parsed command list for --dry-run mode.
|
|
36
|
+
"""Print the parsed command list for --dry-run mode.
|
|
37
|
+
|
|
38
|
+
Substitution variables (``$VAR``, ``&ENV``, ``@COUNTER``) that are already
|
|
39
|
+
populated — from environment variables, ``--assign-arg`` values, or config —
|
|
40
|
+
are expanded in the displayed text. System variables that are set at
|
|
41
|
+
execution time (e.g. ``$CURRENT_TIME``, ``$DB_NAME``, ``$TIMER``) will
|
|
42
|
+
appear unexpanded because ``set_system_vars()`` has not yet been called.
|
|
43
|
+
Local ``~``-prefixed script-scope variables are also not expanded (no script
|
|
44
|
+
execution context exists in dry-run mode).
|
|
45
|
+
"""
|
|
37
46
|
if cmdlist is None or not cmdlist.cmdlist:
|
|
38
47
|
_console.print("[yellow]No commands found in script.[/yellow]")
|
|
39
48
|
return
|
|
@@ -43,7 +52,71 @@ def _print_dry_run(cmdlist: object) -> None:
|
|
|
43
52
|
for i, cmd in enumerate(cmdlist.cmdlist, 1):
|
|
44
53
|
ctype = "SQL " if cmd.command_type == "sql" else "METACMD"
|
|
45
54
|
source_info = f"[dim]{cmd.source}:{cmd.line_no}[/dim]"
|
|
46
|
-
|
|
55
|
+
raw = cmd.commandline()
|
|
56
|
+
try:
|
|
57
|
+
expanded = substitute_vars(raw)
|
|
58
|
+
except Exception:
|
|
59
|
+
# Cycle detection or other expansion errors — fall back to raw text.
|
|
60
|
+
expanded = raw
|
|
61
|
+
_console.print(f" [dim]{i:>4}[/dim] [bold green]{ctype}[/bold green] {source_info} {expanded}")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# ---------------------------------------------------------------------------
|
|
65
|
+
# Profile report helper
|
|
66
|
+
# ---------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _print_profile(profile_data: list[tuple]) -> None:
|
|
70
|
+
"""Print a per-statement timing summary to stdout.
|
|
71
|
+
|
|
72
|
+
Args:
|
|
73
|
+
profile_data: List of ``(source, line_no, command_type, elapsed_secs,
|
|
74
|
+
command_text_preview)`` tuples collected during execution.
|
|
75
|
+
"""
|
|
76
|
+
if not profile_data:
|
|
77
|
+
_console.print("[dim]Profile: no statements recorded.[/dim]")
|
|
78
|
+
return
|
|
79
|
+
|
|
80
|
+
total_secs = sum(row[3] for row in profile_data)
|
|
81
|
+
n = len(profile_data)
|
|
82
|
+
|
|
83
|
+
# Sort descending by elapsed time; show top 20 (or all if <= 20).
|
|
84
|
+
sorted_data = sorted(profile_data, key=lambda r: r[3], reverse=True)
|
|
85
|
+
display = sorted_data[:20]
|
|
86
|
+
|
|
87
|
+
_console.print()
|
|
88
|
+
_console.print(f"[bold cyan]Profile:[/bold cyan] {n} statement{'s' if n != 1 else ''} in {total_secs:.3f}s")
|
|
89
|
+
_console.print()
|
|
90
|
+
|
|
91
|
+
header = f" {'Time (s)':<10} {'Pct':<7} {'Source:Line':<20} {'Type':<7} Command"
|
|
92
|
+
sep = f" {'-' * 10} {'-' * 7} {'-' * 20} {'-' * 7} {'-' * 40}"
|
|
93
|
+
_console.print(f"[dim]{header}[/dim]")
|
|
94
|
+
_console.print(f"[dim]{sep}[/dim]")
|
|
95
|
+
|
|
96
|
+
for source, line_no, command_type, elapsed, preview in display:
|
|
97
|
+
pct = (elapsed / total_secs * 100) if total_secs > 0 else 0.0
|
|
98
|
+
source_col = f"{source}:{line_no}"
|
|
99
|
+
if len(source_col) > 20:
|
|
100
|
+
source_col = "..." + source_col[-17:]
|
|
101
|
+
ctype_label = "SQL " if command_type == "sql" else "METACMD"
|
|
102
|
+
preview_short = preview[:50].replace("\n", " ").strip()
|
|
103
|
+
if len(preview) > 50:
|
|
104
|
+
preview_short += "..."
|
|
105
|
+
_console.print(
|
|
106
|
+
f" [yellow]{elapsed:<10.3f}[/yellow] "
|
|
107
|
+
f"[dim]{pct:<6.1f}%[/dim] "
|
|
108
|
+
f"[cyan]{source_col:<20}[/cyan] "
|
|
109
|
+
f"[green]{ctype_label:<7}[/green] "
|
|
110
|
+
f"{preview_short}",
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
if len(sorted_data) > 20:
|
|
114
|
+
omitted = len(sorted_data) - 20
|
|
115
|
+
_console.print(
|
|
116
|
+
f"[dim] ... {omitted} more statement{'s' if omitted != 1 else ''} not shown (top 20 by time)[/dim]",
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
_console.print()
|
|
47
120
|
|
|
48
121
|
|
|
49
122
|
# ---------------------------------------------------------------------------
|
|
@@ -76,6 +149,7 @@ def _run(
|
|
|
76
149
|
dsn: str | None = None,
|
|
77
150
|
output_dir: str | None = None,
|
|
78
151
|
progress: bool = False,
|
|
152
|
+
profile: bool = False,
|
|
79
153
|
) -> None:
|
|
80
154
|
"""Initialise state, connect to the database, load the script, and run it.
|
|
81
155
|
|
|
@@ -378,7 +452,10 @@ def _run(
|
|
|
378
452
|
atexit.register(_state.dbs.closeall)
|
|
379
453
|
_state.dbs.do_rollback = True
|
|
380
454
|
|
|
381
|
-
|
|
455
|
+
if profile:
|
|
456
|
+
_state.profile_data = []
|
|
457
|
+
|
|
458
|
+
_execute_script_direct(conf, profile=profile)
|
|
382
459
|
|
|
383
460
|
|
|
384
461
|
# ---------------------------------------------------------------------------
|
|
@@ -437,8 +514,15 @@ def _execute_script_textual_console(conf: ConfigData) -> None:
|
|
|
437
514
|
_state.exec_log.log_exit_end()
|
|
438
515
|
|
|
439
516
|
|
|
440
|
-
def _execute_script_direct(conf: ConfigData) -> None:
|
|
441
|
-
"""Run runscripts() in the current (main) thread — used when Textual is not active.
|
|
517
|
+
def _execute_script_direct(conf: ConfigData, *, profile: bool = False) -> None:
|
|
518
|
+
"""Run runscripts() in the current (main) thread — used when Textual is not active.
|
|
519
|
+
|
|
520
|
+
Args:
|
|
521
|
+
conf: The active configuration object.
|
|
522
|
+
profile: When ``True``, print a per-statement timing summary after the
|
|
523
|
+
script completes. Timing data must already have been activated on
|
|
524
|
+
``_state.profile_data`` before this function is called.
|
|
525
|
+
"""
|
|
442
526
|
import execsql.state as _state
|
|
443
527
|
import execsql.utils.gui as _gui
|
|
444
528
|
|
|
@@ -463,6 +547,8 @@ def _execute_script_direct(conf: ConfigData) -> None:
|
|
|
463
547
|
if gui_console_isrunning():
|
|
464
548
|
gui_console_off()
|
|
465
549
|
_state.exec_log.log_status_info(f"{_state.cmds_run} commands run")
|
|
550
|
+
if profile and _state.profile_data is not None:
|
|
551
|
+
_print_profile(_state.profile_data)
|
|
466
552
|
sys.exit(exc.code)
|
|
467
553
|
except ConfigError:
|
|
468
554
|
raise
|
|
@@ -489,6 +575,8 @@ def _execute_script_direct(conf: ConfigData) -> None:
|
|
|
489
575
|
if gui_console_isrunning():
|
|
490
576
|
gui_console_off()
|
|
491
577
|
_state.exec_log.log_status_info(f"{_state.cmds_run} commands run")
|
|
578
|
+
if profile and _state.profile_data is not None:
|
|
579
|
+
_print_profile(_state.profile_data)
|
|
492
580
|
_state.exec_log.log_exit_end()
|
|
493
581
|
|
|
494
582
|
|
execsql/exporters/__init__.py
CHANGED
|
@@ -9,9 +9,9 @@ handlers can access them via ``_state.write_query_to_csv`` etc. without
|
|
|
9
9
|
importing directly from here.
|
|
10
10
|
|
|
11
11
|
Sub-modules: ``base``, ``delimited``, ``json``, ``xml``, ``html``,
|
|
12
|
-
``latex``, ``ods``, ``xls``, ``zip``, ``raw``, ``pretty``,
|
|
13
|
-
``templates``, ``feather``, ``parquet``, ``duckdb``,
|
|
14
|
-
``protocol``.
|
|
12
|
+
``latex``, ``markdown``, ``ods``, ``xls``, ``zip``, ``raw``, ``pretty``,
|
|
13
|
+
``values``, ``templates``, ``feather``, ``parquet``, ``duckdb``,
|
|
14
|
+
``sqlite``, ``protocol``.
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
17
|
from execsql.exporters.protocol import QueryExporter, RowsetExporter
|
execsql/exporters/delimited.py
CHANGED
|
@@ -27,7 +27,7 @@ from execsql.exceptions import ErrInfo
|
|
|
27
27
|
from execsql.models import DataTable
|
|
28
28
|
from execsql.utils.errors import exception_desc
|
|
29
29
|
from execsql.utils.fileio import filewriter_close
|
|
30
|
-
from execsql.utils.strings import clean_words, fold_words
|
|
30
|
+
from execsql.utils.strings import clean_words, dedup_words, fold_words
|
|
31
31
|
|
|
32
32
|
__all__ = ["LineDelimiter", "CsvFile", "CsvWriter", "DelimitedWriter", "write_delimited_file"]
|
|
33
33
|
|
|
@@ -677,7 +677,7 @@ class CsvFile(EncodedFile):
|
|
|
677
677
|
if conf.fold_col_hdrs != "no":
|
|
678
678
|
colnames = fold_words(colnames, conf.fold_col_hdrs)
|
|
679
679
|
if conf.dedup_col_hdrs:
|
|
680
|
-
colnames =
|
|
680
|
+
colnames = dedup_words(colnames)
|
|
681
681
|
return colnames
|
|
682
682
|
|
|
683
683
|
def column_headers(self) -> list[str]:
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
GitHub-Flavored Markdown (GFM) pipe table export for execsql.
|
|
5
|
+
|
|
6
|
+
Provides :func:`write_query_to_markdown`, which serializes a query result
|
|
7
|
+
set as a GFM pipe table suitable for inclusion in GitHub README files,
|
|
8
|
+
wikis, and any Markdown renderer that supports the pipe-table extension.
|
|
9
|
+
|
|
10
|
+
Example output::
|
|
11
|
+
|
|
12
|
+
| id | name | score |
|
|
13
|
+
|----|--------|-------|
|
|
14
|
+
| 1 | Alice | 95.2 |
|
|
15
|
+
| 2 | Bob | 87.0 |
|
|
16
|
+
|
|
17
|
+
No optional dependencies — pure Python.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from typing import Any
|
|
21
|
+
|
|
22
|
+
import execsql.state as _state
|
|
23
|
+
from execsql.exceptions import ErrInfo
|
|
24
|
+
from execsql.exporters.zip import ZipWriter
|
|
25
|
+
from execsql.utils.errors import exception_desc
|
|
26
|
+
from execsql.utils.fileio import filewriter_close
|
|
27
|
+
|
|
28
|
+
__all__ = ["write_query_to_markdown"]
|
|
29
|
+
|
|
30
|
+
_PIPE_ESCAPE = str.maketrans({"|": r"\|", "\\": "\\\\"})
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _cell(value: Any) -> str:
|
|
34
|
+
"""Render a single cell value as a Markdown-safe string.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
value: The cell value from the result set. ``None`` is rendered as
|
|
38
|
+
an empty string. Pipe characters are escaped so they do not
|
|
39
|
+
break the table structure.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
A string safe to embed between pipe characters in a GFM table row.
|
|
43
|
+
"""
|
|
44
|
+
if value is None:
|
|
45
|
+
return ""
|
|
46
|
+
return str(value).translate(_PIPE_ESCAPE)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def write_query_to_markdown(
|
|
50
|
+
select_stmt: str,
|
|
51
|
+
db: Any,
|
|
52
|
+
outfile: str,
|
|
53
|
+
append: bool = False,
|
|
54
|
+
desc: str | None = None,
|
|
55
|
+
zipfile: str | None = None,
|
|
56
|
+
) -> None:
|
|
57
|
+
"""Execute *select_stmt* and write the result set as a GFM pipe table.
|
|
58
|
+
|
|
59
|
+
Writes a GitHub-Flavored Markdown pipe table to *outfile* (or into
|
|
60
|
+
*zipfile* when provided). Column widths are derived from the widest
|
|
61
|
+
value in each column (including the header), so the table renders
|
|
62
|
+
legibly in plain-text editors as well as in Markdown renderers.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
select_stmt: SQL SELECT statement to execute.
|
|
66
|
+
db: Database connection object exposing ``select_rowsource()``.
|
|
67
|
+
outfile: Destination file path, or ``"stdout"`` for console output.
|
|
68
|
+
append: When ``True`` open the file in append mode. A blank line
|
|
69
|
+
is written before the table so consecutive appended tables are
|
|
70
|
+
visually separated.
|
|
71
|
+
desc: Optional human-readable description. When provided it is
|
|
72
|
+
written as an HTML comment (``<!-- desc -->``), which is valid
|
|
73
|
+
Markdown and invisible in rendered output.
|
|
74
|
+
zipfile: When set, write into this ZIP archive instead of a plain
|
|
75
|
+
file. *outfile* becomes the entry name inside the archive.
|
|
76
|
+
"""
|
|
77
|
+
conf = _state.conf
|
|
78
|
+
try:
|
|
79
|
+
hdrs, rows = db.select_rowsource(select_stmt)
|
|
80
|
+
except ErrInfo:
|
|
81
|
+
raise
|
|
82
|
+
except Exception as e:
|
|
83
|
+
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
84
|
+
|
|
85
|
+
# Materialise the full result set so we can compute column widths in one
|
|
86
|
+
# pass before writing. GFM tables require consistent column widths for
|
|
87
|
+
# readability; width computation requires seeing all rows first.
|
|
88
|
+
str_hdrs: list[str] = [_cell(h) for h in hdrs]
|
|
89
|
+
str_rows: list[list[str]] = [[_cell(v) for v in row] for row in rows]
|
|
90
|
+
|
|
91
|
+
# Minimum separator width is 3 dashes (GFM spec minimum for alignment row).
|
|
92
|
+
col_widths: list[int] = [max(3, len(h)) for h in str_hdrs]
|
|
93
|
+
for row in str_rows:
|
|
94
|
+
for i, cell in enumerate(row):
|
|
95
|
+
if len(cell) > col_widths[i]:
|
|
96
|
+
col_widths[i] = len(cell)
|
|
97
|
+
|
|
98
|
+
def _format_row(cells: list[str]) -> str:
|
|
99
|
+
padded = (f" {c:<{col_widths[i]}} " for i, c in enumerate(cells))
|
|
100
|
+
return "|" + "|".join(padded) + "|\n"
|
|
101
|
+
|
|
102
|
+
def _format_separator() -> str:
|
|
103
|
+
dashes = (f" {'-' * col_widths[i]} " for i in range(len(str_hdrs)))
|
|
104
|
+
return "|" + "|".join(dashes) + "|\n"
|
|
105
|
+
|
|
106
|
+
if zipfile is None:
|
|
107
|
+
filewriter_close(outfile)
|
|
108
|
+
from execsql.utils.fileio import EncodedFile
|
|
109
|
+
|
|
110
|
+
ef = EncodedFile(outfile, conf.output_encoding)
|
|
111
|
+
f = ef.open("at" if append else "wt")
|
|
112
|
+
else:
|
|
113
|
+
f = ZipWriter(zipfile, outfile, append)
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
if append:
|
|
117
|
+
# Blank line separates consecutive tables when appending.
|
|
118
|
+
f.write("\n")
|
|
119
|
+
if desc is not None:
|
|
120
|
+
f.write(f"<!-- {desc} -->\n\n")
|
|
121
|
+
f.write(_format_row(str_hdrs))
|
|
122
|
+
f.write(_format_separator())
|
|
123
|
+
for row in str_rows:
|
|
124
|
+
f.write(_format_row(row))
|
|
125
|
+
finally:
|
|
126
|
+
f.close()
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
XLSX (Excel Open XML) export for execsql.
|
|
5
|
+
|
|
6
|
+
Provides :func:`write_query_to_xlsx` (single-sheet export) and
|
|
7
|
+
:func:`write_queries_to_xlsx` (multi-sheet export). Requires the
|
|
8
|
+
``openpyxl`` package (``execsql2[excel]``).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import datetime
|
|
12
|
+
import getpass
|
|
13
|
+
import os
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
import execsql.state as _state
|
|
18
|
+
from execsql.exceptions import ErrInfo
|
|
19
|
+
from execsql.exporters.base import ExportRecord
|
|
20
|
+
from execsql.exporters.pretty import prettyprint_query
|
|
21
|
+
from execsql.script import current_script_line
|
|
22
|
+
from execsql.utils.errors import exception_desc, fatal_error
|
|
23
|
+
from execsql.utils.fileio import filewriter_close
|
|
24
|
+
from execsql.utils.strings import unquoted
|
|
25
|
+
|
|
26
|
+
__all__ = ["write_query_to_xlsx", "write_queries_to_xlsx"]
|
|
27
|
+
|
|
28
|
+
# ---------------------------------------------------------------------------
|
|
29
|
+
# Internal helpers
|
|
30
|
+
# ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _require_openpyxl() -> Any:
|
|
34
|
+
"""Import and return the openpyxl module, raising a fatal error if absent."""
|
|
35
|
+
try:
|
|
36
|
+
import openpyxl
|
|
37
|
+
|
|
38
|
+
return openpyxl
|
|
39
|
+
except ImportError:
|
|
40
|
+
fatal_error("The openpyxl library is needed to write Excel (.xlsx) spreadsheets (install execsql2[excel]).")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _cell_value(item: Any) -> Any:
|
|
44
|
+
"""Return a value suitable for writing directly to an openpyxl cell.
|
|
45
|
+
|
|
46
|
+
openpyxl natively handles int, float, bool, str, datetime.datetime,
|
|
47
|
+
datetime.date, and None. datetime.time is converted to a string because
|
|
48
|
+
openpyxl does not have a native time-only cell type.
|
|
49
|
+
"""
|
|
50
|
+
if item is None:
|
|
51
|
+
return None
|
|
52
|
+
if isinstance(item, bool):
|
|
53
|
+
# bool must be checked before int — bool is a subclass of int.
|
|
54
|
+
return item
|
|
55
|
+
if isinstance(item, int | float):
|
|
56
|
+
return item
|
|
57
|
+
if isinstance(item, datetime.datetime):
|
|
58
|
+
return item
|
|
59
|
+
if isinstance(item, datetime.date):
|
|
60
|
+
return item
|
|
61
|
+
if isinstance(item, datetime.time):
|
|
62
|
+
# openpyxl has no native time-only type; store as HH:MM:SS string.
|
|
63
|
+
return item.strftime("%H:%M:%S")
|
|
64
|
+
return str(item)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# ---------------------------------------------------------------------------
|
|
68
|
+
# Public API
|
|
69
|
+
# ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def write_query_to_xlsx(
|
|
73
|
+
select_stmt: str,
|
|
74
|
+
db: Any,
|
|
75
|
+
outfile: str,
|
|
76
|
+
append: bool = False,
|
|
77
|
+
desc: str | None = None,
|
|
78
|
+
sheetname: str | None = None,
|
|
79
|
+
) -> None:
|
|
80
|
+
"""Execute *select_stmt* and write the result to a single worksheet in an XLSX file.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
select_stmt: SQL SELECT statement to execute.
|
|
84
|
+
db: An execsql database adapter with a ``select_rowsource()`` method.
|
|
85
|
+
outfile: Destination ``.xlsx`` file path.
|
|
86
|
+
append: If ``True`` and *outfile* exists, add a new sheet to the
|
|
87
|
+
existing workbook. If ``False``, overwrite any existing file.
|
|
88
|
+
desc: Optional human-readable description stored in the inventory sheet.
|
|
89
|
+
sheetname: Name for the new worksheet. Defaults to ``"Sheet1"``
|
|
90
|
+
(or ``"Sheet2"``, ``"Sheet3"``, etc. when appending to avoid
|
|
91
|
+
name collisions).
|
|
92
|
+
"""
|
|
93
|
+
openpyxl = _require_openpyxl()
|
|
94
|
+
|
|
95
|
+
try:
|
|
96
|
+
hdrs, rows = db.select_rowsource(select_stmt)
|
|
97
|
+
except ErrInfo:
|
|
98
|
+
raise
|
|
99
|
+
except Exception as e:
|
|
100
|
+
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
101
|
+
|
|
102
|
+
# ------------------------------------------------------------------
|
|
103
|
+
# Determine sheet name and open/create workbook
|
|
104
|
+
# ------------------------------------------------------------------
|
|
105
|
+
if append and Path(outfile).is_file():
|
|
106
|
+
wb = openpyxl.load_workbook(outfile)
|
|
107
|
+
existing_names = wb.sheetnames
|
|
108
|
+
base = sheetname or "Sheet"
|
|
109
|
+
sheet_name = base
|
|
110
|
+
sheet_no = 1
|
|
111
|
+
while sheet_name in existing_names:
|
|
112
|
+
sheet_no += 1
|
|
113
|
+
sheet_name = f"{base}{sheet_no}"
|
|
114
|
+
else:
|
|
115
|
+
sheet_name = sheetname or "Sheet1"
|
|
116
|
+
if Path(outfile).is_file():
|
|
117
|
+
filewriter_close(outfile)
|
|
118
|
+
os.unlink(outfile)
|
|
119
|
+
wb = openpyxl.Workbook()
|
|
120
|
+
# openpyxl creates a default sheet named "Sheet"; remove it so we
|
|
121
|
+
# start with a clean workbook.
|
|
122
|
+
if wb.sheetnames:
|
|
123
|
+
del wb[wb.sheetnames[0]]
|
|
124
|
+
|
|
125
|
+
# ------------------------------------------------------------------
|
|
126
|
+
# Ensure the inventory sheet exists
|
|
127
|
+
# ------------------------------------------------------------------
|
|
128
|
+
inventory_name = "Datasheets"
|
|
129
|
+
if inventory_name not in wb.sheetnames:
|
|
130
|
+
inv_ws = wb.create_sheet(inventory_name)
|
|
131
|
+
bold_font = openpyxl.styles.Font(bold=True)
|
|
132
|
+
for col_idx, hdr in enumerate(
|
|
133
|
+
("datasheet_name", "created_on", "created_by", "description", "source"),
|
|
134
|
+
start=1,
|
|
135
|
+
):
|
|
136
|
+
cell = inv_ws.cell(row=1, column=col_idx, value=hdr)
|
|
137
|
+
cell.font = bold_font
|
|
138
|
+
else:
|
|
139
|
+
inv_ws = wb[inventory_name]
|
|
140
|
+
|
|
141
|
+
# ------------------------------------------------------------------
|
|
142
|
+
# Write data to a new sheet
|
|
143
|
+
# ------------------------------------------------------------------
|
|
144
|
+
ws = wb.create_sheet(sheet_name)
|
|
145
|
+
bold_font = openpyxl.styles.Font(bold=True)
|
|
146
|
+
|
|
147
|
+
# Header row
|
|
148
|
+
for col_idx, hdr in enumerate(hdrs, start=1):
|
|
149
|
+
cell = ws.cell(row=1, column=col_idx, value=str(hdr))
|
|
150
|
+
cell.font = bold_font
|
|
151
|
+
|
|
152
|
+
# Data rows
|
|
153
|
+
for row_idx, row in enumerate(rows, start=2):
|
|
154
|
+
for col_idx, item in enumerate(row, start=1):
|
|
155
|
+
ws.cell(row=row_idx, column=col_idx, value=_cell_value(item))
|
|
156
|
+
|
|
157
|
+
# ------------------------------------------------------------------
|
|
158
|
+
# Update inventory sheet
|
|
159
|
+
# ------------------------------------------------------------------
|
|
160
|
+
script, lno = current_script_line()
|
|
161
|
+
src = (
|
|
162
|
+
f"{select_stmt} with database {_state.dbs.current().name()}, "
|
|
163
|
+
f"with script {str(Path(script).resolve())}, line {lno}"
|
|
164
|
+
)
|
|
165
|
+
next_row = inv_ws.max_row + 1
|
|
166
|
+
for col_idx, value in enumerate(
|
|
167
|
+
(
|
|
168
|
+
sheet_name,
|
|
169
|
+
datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
|
|
170
|
+
getpass.getuser(),
|
|
171
|
+
desc,
|
|
172
|
+
src,
|
|
173
|
+
),
|
|
174
|
+
start=1,
|
|
175
|
+
):
|
|
176
|
+
inv_ws.cell(row=next_row, column=col_idx, value=value)
|
|
177
|
+
|
|
178
|
+
wb.save(outfile)
|
|
179
|
+
wb.close()
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def write_queries_to_xlsx(
|
|
183
|
+
table_list: str,
|
|
184
|
+
db: Any,
|
|
185
|
+
outfile: str,
|
|
186
|
+
append: bool = False,
|
|
187
|
+
tee: bool = False,
|
|
188
|
+
desc: str | None = None,
|
|
189
|
+
) -> None:
|
|
190
|
+
"""Write multiple tables/queries to separate worksheets in a single XLSX workbook.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
table_list: Comma-separated list of table names (optionally schema-qualified).
|
|
194
|
+
db: An execsql database adapter with a ``select_rowsource()`` method.
|
|
195
|
+
outfile: Destination ``.xlsx`` file path.
|
|
196
|
+
append: If ``True`` and *outfile* exists, add new sheets to the existing
|
|
197
|
+
workbook rather than replacing it.
|
|
198
|
+
tee: If ``True``, also pretty-print each query result to stdout.
|
|
199
|
+
desc: Optional description(s). A single string is applied to every
|
|
200
|
+
sheet; a comma-separated string with the same count as *table_list*
|
|
201
|
+
assigns individual descriptions per sheet.
|
|
202
|
+
"""
|
|
203
|
+
openpyxl = _require_openpyxl()
|
|
204
|
+
|
|
205
|
+
tables = [t.strip() for t in table_list.split(",")]
|
|
206
|
+
if desc is not None:
|
|
207
|
+
descriptions = [d.strip() for d in desc.split(",")]
|
|
208
|
+
one_desc = len(descriptions) != len(tables)
|
|
209
|
+
else:
|
|
210
|
+
descriptions = []
|
|
211
|
+
one_desc = False
|
|
212
|
+
|
|
213
|
+
# ------------------------------------------------------------------
|
|
214
|
+
# Open or create workbook
|
|
215
|
+
# ------------------------------------------------------------------
|
|
216
|
+
if Path(outfile).is_file() and not append:
|
|
217
|
+
filewriter_close(outfile)
|
|
218
|
+
os.unlink(outfile)
|
|
219
|
+
|
|
220
|
+
if Path(outfile).is_file():
|
|
221
|
+
wb = openpyxl.load_workbook(outfile)
|
|
222
|
+
else:
|
|
223
|
+
wb = openpyxl.Workbook()
|
|
224
|
+
# Remove the default empty sheet created by openpyxl.
|
|
225
|
+
if wb.sheetnames:
|
|
226
|
+
del wb[wb.sheetnames[0]]
|
|
227
|
+
|
|
228
|
+
# ------------------------------------------------------------------
|
|
229
|
+
# Ensure the inventory sheet exists
|
|
230
|
+
# ------------------------------------------------------------------
|
|
231
|
+
inventory_name = "Datasheets"
|
|
232
|
+
if inventory_name not in wb.sheetnames:
|
|
233
|
+
inv_ws = wb.create_sheet(inventory_name)
|
|
234
|
+
bold_font = openpyxl.styles.Font(bold=True)
|
|
235
|
+
for col_idx, hdr in enumerate(
|
|
236
|
+
("datasheet_name", "created_on", "created_by", "description", "source"),
|
|
237
|
+
start=1,
|
|
238
|
+
):
|
|
239
|
+
cell = inv_ws.cell(row=1, column=col_idx, value=hdr)
|
|
240
|
+
cell.font = bold_font
|
|
241
|
+
else:
|
|
242
|
+
inv_ws = wb[inventory_name]
|
|
243
|
+
|
|
244
|
+
# ------------------------------------------------------------------
|
|
245
|
+
# Write each table to its own sheet
|
|
246
|
+
# ------------------------------------------------------------------
|
|
247
|
+
bold_font = openpyxl.styles.Font(bold=True)
|
|
248
|
+
|
|
249
|
+
for i, t in enumerate(tables):
|
|
250
|
+
# Determine the table name used for the sheet label.
|
|
251
|
+
if "." in t:
|
|
252
|
+
st = t.split(".")
|
|
253
|
+
if len(st) != 2:
|
|
254
|
+
raise ErrInfo("cmd", other_msg=f"Unrecognized table specification in <{t}>")
|
|
255
|
+
tblname = unquoted(st[1])
|
|
256
|
+
else:
|
|
257
|
+
tblname = unquoted(t)
|
|
258
|
+
|
|
259
|
+
# Avoid duplicate sheet names.
|
|
260
|
+
existing_names = wb.sheetnames
|
|
261
|
+
sheet_name = tblname
|
|
262
|
+
sheet_no = 1
|
|
263
|
+
while sheet_name in existing_names:
|
|
264
|
+
sheet_name = f"{tblname}_{sheet_no}"
|
|
265
|
+
sheet_no += 1
|
|
266
|
+
|
|
267
|
+
# Fetch data.
|
|
268
|
+
select_stmt = f"select * from {t};"
|
|
269
|
+
try:
|
|
270
|
+
hdrs, rows = db.select_rowsource(select_stmt)
|
|
271
|
+
except ErrInfo:
|
|
272
|
+
raise
|
|
273
|
+
except Exception as e:
|
|
274
|
+
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
275
|
+
|
|
276
|
+
# Write data sheet.
|
|
277
|
+
ws = wb.create_sheet(sheet_name)
|
|
278
|
+
|
|
279
|
+
for col_idx, hdr in enumerate(hdrs, start=1):
|
|
280
|
+
cell = ws.cell(row=1, column=col_idx, value=str(hdr))
|
|
281
|
+
cell.font = bold_font
|
|
282
|
+
|
|
283
|
+
for row_idx, row in enumerate(rows, start=2):
|
|
284
|
+
for col_idx, item in enumerate(row, start=1):
|
|
285
|
+
ws.cell(row=row_idx, column=col_idx, value=_cell_value(item))
|
|
286
|
+
|
|
287
|
+
# Determine per-sheet description.
|
|
288
|
+
if desc is None:
|
|
289
|
+
d = None
|
|
290
|
+
elif one_desc:
|
|
291
|
+
d = desc
|
|
292
|
+
else:
|
|
293
|
+
d = descriptions[i]
|
|
294
|
+
|
|
295
|
+
# Update inventory sheet.
|
|
296
|
+
script, lno = current_script_line()
|
|
297
|
+
src = f"From database {_state.dbs.current().name()}, with script {str(Path(script).resolve())}, line {lno}"
|
|
298
|
+
next_row = inv_ws.max_row + 1
|
|
299
|
+
for col_idx, value in enumerate(
|
|
300
|
+
(
|
|
301
|
+
sheet_name,
|
|
302
|
+
datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
|
|
303
|
+
getpass.getuser(),
|
|
304
|
+
d,
|
|
305
|
+
src,
|
|
306
|
+
),
|
|
307
|
+
start=1,
|
|
308
|
+
):
|
|
309
|
+
inv_ws.cell(row=next_row, column=col_idx, value=value)
|
|
310
|
+
|
|
311
|
+
if tee and outfile.lower() != "stdout":
|
|
312
|
+
prettyprint_query(select_stmt, db, "stdout", False, desc=d)
|
|
313
|
+
|
|
314
|
+
_state.export_metadata.add(ExportRecord(queryname=select_stmt, outfile=outfile, zipfile=None, description=d))
|
|
315
|
+
|
|
316
|
+
wb.save(outfile)
|
|
317
|
+
wb.close()
|