execsql2 2.37.0__py3-none-any.whl → 2.39.1__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 +46 -9
- execsql/cli/run.py +128 -56
- execsql/config.py +7 -0
- execsql/data/execsql.conf.template +26 -2
- execsql/db/base.py +61 -24
- execsql/db/dsn.py +2 -3
- execsql/db/duckdb.py +43 -4
- execsql/db/firebird.py +39 -22
- execsql/db/mysql.py +22 -23
- execsql/db/oracle.py +11 -4
- execsql/db/postgres.py +15 -20
- 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 +19 -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/__init__.py +13 -10
- execsql/gui/base.py +131 -8
- execsql/gui/console.py +77 -30
- execsql/gui/desktop.py +290 -119
- execsql/gui/tui.py +384 -129
- execsql/importers/base.py +15 -19
- execsql/importers/csv.py +3 -5
- 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/control.py +20 -5
- execsql/metacommands/data.py +11 -9
- execsql/metacommands/dispatch.py +10 -0
- execsql/metacommands/io.py +1 -0
- execsql/metacommands/io_export.py +1 -2
- execsql/metacommands/io_fileops.py +27 -91
- execsql/metacommands/io_import.py +26 -8
- execsql/metacommands/io_write.py +0 -9
- execsql/metacommands/prompt.py +94 -42
- execsql/metacommands/reference.py +7 -0
- execsql/metacommands/system.py +0 -2
- execsql/parser.py +132 -45
- execsql/script/control.py +17 -7
- execsql/script/engine.py +1 -1
- execsql/script/executor.py +184 -37
- execsql/script/preparation.py +42 -0
- execsql/types.py +6 -5
- execsql/utils/auth.py +11 -6
- execsql/utils/errors.py +2 -2
- execsql/utils/fileio.py +342 -134
- execsql/utils/gui.py +162 -53
- execsql/utils/spreadsheet.py +74 -0
- execsql/utils/strings.py +13 -7
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/README.md +5 -5
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/example_config_prompt.sql +2 -2
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/METADATA +32 -119
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/RECORD +77 -75
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/WHEEL +1 -1
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/config_settings.sqlite +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/make_config_db.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/md_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/md_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/md_upsert.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/pg_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/pg_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/script_template.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/ss_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/ss_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/ss_upsert.sql +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/entry_points.txt +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/licenses/LICENSE.txt +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/licenses/NOTICE +0 -0
execsql/api.py
CHANGED
|
@@ -39,7 +39,7 @@ from typing import Any, cast
|
|
|
39
39
|
|
|
40
40
|
from execsql.cli.dsn import _parse_connection_string
|
|
41
41
|
from execsql.config import ConfigData, StatObj, WriteHooks
|
|
42
|
-
from execsql.exceptions import ErrInfo
|
|
42
|
+
from execsql.exceptions import ErrInfo, ScriptExit
|
|
43
43
|
from execsql.state import RuntimeContext, active_context
|
|
44
44
|
|
|
45
45
|
__all__ = ["run", "ScriptResult", "ScriptError"]
|
|
@@ -224,6 +224,11 @@ class _LibraryConfig:
|
|
|
224
224
|
self.email_format = "plain"
|
|
225
225
|
self.email_css: str | None = None
|
|
226
226
|
|
|
227
|
+
# Default capabilities when no configuration file is supplied.
|
|
228
|
+
self.allow_system_cmd = True
|
|
229
|
+
self.allow_rm_file = True
|
|
230
|
+
self.allow_serve = True
|
|
231
|
+
|
|
227
232
|
# Includes
|
|
228
233
|
self.include_req: list = []
|
|
229
234
|
self.include_opt: list = []
|
|
@@ -340,9 +345,9 @@ def run(
|
|
|
340
345
|
encoding: str = "utf-8",
|
|
341
346
|
halt_on_error: bool = True,
|
|
342
347
|
new_db: bool = False,
|
|
343
|
-
allow_system_cmd: bool =
|
|
344
|
-
allow_rm_file: bool =
|
|
345
|
-
allow_serve: bool =
|
|
348
|
+
allow_system_cmd: bool | None = None,
|
|
349
|
+
allow_rm_file: bool | None = None,
|
|
350
|
+
allow_serve: bool | None = None,
|
|
346
351
|
) -> ScriptResult:
|
|
347
352
|
"""Execute a SQL script and return the result.
|
|
348
353
|
|
|
@@ -356,10 +361,26 @@ def run(
|
|
|
356
361
|
``"postgresql://user:pass@host/db"``).
|
|
357
362
|
connection: A pre-existing :class:`~execsql.db.base.Database`
|
|
358
363
|
instance. ``run()`` will NOT close this connection on exit.
|
|
364
|
+
Connections opened by the script with CONNECT are run-owned:
|
|
365
|
+
their pending work is rolled back and they are closed on exit,
|
|
366
|
+
including under AUTOCOMMIT OFF. Commit their work in the script
|
|
367
|
+
before it ends if it must be kept.
|
|
368
|
+
What an interrupted SQL statement, metacommand or ``END BATCH``
|
|
369
|
+
did is rolled back before the interrupt is re-raised: an SQL
|
|
370
|
+
statement rolls back its connection's open transaction, and a
|
|
371
|
+
metacommand everything not yet committed on each connection
|
|
372
|
+
with autocommit on (under AUTOCOMMIT OFF, a metacommand's writes
|
|
373
|
+
stay with what the script holds). An interrupt that ends the run
|
|
374
|
+
also rolls back everything else not yet committed on each
|
|
375
|
+
connection with autocommit on. Work the database had already
|
|
376
|
+
committed when the interrupt arrived stays committed. A
|
|
377
|
+
``SystemExit`` from a signal handler is rolled back the same way,
|
|
378
|
+
and the run returns a failed result.
|
|
359
379
|
variables: Substitution variables as ``{"NAME": "value"}``.
|
|
360
380
|
Keys without a ``$`` prefix get one added automatically, so
|
|
361
381
|
``{"SCHEMA": "public"}`` is referenced as ``!!$SCHEMA!!``.
|
|
362
|
-
config_file: Optional execsql configuration file to load.
|
|
382
|
+
config_file: Optional execsql configuration file to load. Configured
|
|
383
|
+
variables and startup includes apply before the main script runs.
|
|
363
384
|
encoding: Script file encoding (default ``"utf-8"``).
|
|
364
385
|
halt_on_error: If ``True`` (default), stop on the first SQL
|
|
365
386
|
error. If ``False``, capture errors and continue.
|
|
@@ -368,20 +389,26 @@ def run(
|
|
|
368
389
|
allow_system_cmd: If ``False``, the SYSTEM_CMD metacommand is
|
|
369
390
|
disabled and will raise an error if encountered. Matches the
|
|
370
391
|
``--no-system-cmd`` CLI flag and the ``allow_system_cmd``
|
|
371
|
-
config option.
|
|
392
|
+
config option. None (default) inherits the configured value,
|
|
393
|
+
or True without a config file; explicit True overrides a restriction.
|
|
372
394
|
allow_rm_file: If ``False``, the RM_FILE metacommand is disabled
|
|
373
395
|
and will raise an error if encountered. Matches the
|
|
374
396
|
``--no-rm-file`` CLI flag and the ``allow_rm_file`` config
|
|
375
|
-
option.
|
|
397
|
+
option. None (default) inherits the configured value;
|
|
398
|
+
an explicit boolean overrides it.
|
|
376
399
|
allow_serve: If ``False``, the SERVE metacommand is disabled and
|
|
377
400
|
will raise an error if encountered. Matches the ``--no-serve``
|
|
378
|
-
CLI flag and the ``allow_serve`` config option.
|
|
401
|
+
CLI flag and the ``allow_serve`` config option. None (default)
|
|
402
|
+
inherits the configured value; an explicit boolean overrides it.
|
|
379
403
|
|
|
380
404
|
Returns:
|
|
381
405
|
A :class:`ScriptResult` with execution outcome, timing, errors,
|
|
382
406
|
and final variable state. A script that cannot be read, a bad
|
|
383
407
|
*config_file*, and a *dsn* that cannot be parsed or opened all
|
|
384
|
-
return ``success=False`` rather than raising.
|
|
408
|
+
return ``success=False`` rather than raising. A blocked background
|
|
409
|
+
output writer adds an error with source ``<output>`` and is abandoned
|
|
410
|
+
at its flush deadline. Its current I/O may finish later; queued
|
|
411
|
+
output is discarded and no further opens or writes are started.
|
|
385
412
|
|
|
386
413
|
Raises:
|
|
387
414
|
ValueError: If the argument combination is invalid (e.g. both
|
|
@@ -420,27 +447,6 @@ def run(
|
|
|
420
447
|
# ------------------------------------------------------------------
|
|
421
448
|
ctx = RuntimeContext()
|
|
422
449
|
|
|
423
|
-
# Configuration
|
|
424
|
-
conf_overrides: dict[str, Any] = {"script_encoding": encoding}
|
|
425
|
-
conf: Any
|
|
426
|
-
if config_file is not None:
|
|
427
|
-
# Load a real ConfigData with the explicit config file
|
|
428
|
-
from execsql.script.variables import SubVarSet
|
|
429
|
-
|
|
430
|
-
if not Path(config_file).is_file():
|
|
431
|
-
return _failed(f"Config file {str(config_file)!r} does not exist.", "<config>")
|
|
432
|
-
temp_subvars = SubVarSet()
|
|
433
|
-
script_dir = str(Path(script).resolve().parent) if script else os.getcwd()
|
|
434
|
-
try:
|
|
435
|
-
conf = ConfigData(script_dir, temp_subvars, config_file=str(config_file))
|
|
436
|
-
except Exception as exc:
|
|
437
|
-
return _failed(_error_text(exc), "<config>")
|
|
438
|
-
# Apply any overrides
|
|
439
|
-
for k, v in conf_overrides.items():
|
|
440
|
-
setattr(conf, k, v)
|
|
441
|
-
else:
|
|
442
|
-
conf = _LibraryConfig(**conf_overrides)
|
|
443
|
-
|
|
444
450
|
# Substitution variables
|
|
445
451
|
from execsql.script.variables import SubVarSet
|
|
446
452
|
|
|
@@ -461,6 +467,21 @@ def run(
|
|
|
461
467
|
|
|
462
468
|
subvars.add_substitution("$HOSTNAME", platform.node())
|
|
463
469
|
|
|
470
|
+
# Configuration
|
|
471
|
+
conf: Any
|
|
472
|
+
if config_file is not None:
|
|
473
|
+
# Load a real ConfigData with the explicit config file
|
|
474
|
+
if not Path(config_file).is_file():
|
|
475
|
+
return _failed(f"Config file {str(config_file)!r} does not exist.", "<config>")
|
|
476
|
+
script_dir = str(Path(script).resolve().parent) if script else os.getcwd()
|
|
477
|
+
try:
|
|
478
|
+
conf = ConfigData(script_dir, subvars, config_file=str(config_file))
|
|
479
|
+
except Exception as exc:
|
|
480
|
+
return _failed(_error_text(exc), "<config>")
|
|
481
|
+
conf.script_encoding = encoding
|
|
482
|
+
else:
|
|
483
|
+
conf = _LibraryConfig(script_encoding=encoding)
|
|
484
|
+
|
|
464
485
|
# User-supplied variables
|
|
465
486
|
if variables:
|
|
466
487
|
for name, value in variables.items():
|
|
@@ -476,11 +497,18 @@ def run(
|
|
|
476
497
|
ctx.subvars = subvars
|
|
477
498
|
ctx.status = StatObj()
|
|
478
499
|
ctx.status.halt_on_err = halt_on_error
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
500
|
+
if allow_system_cmd is not None:
|
|
501
|
+
conf.allow_system_cmd = allow_system_cmd
|
|
502
|
+
if allow_rm_file is not None:
|
|
503
|
+
conf.allow_rm_file = allow_rm_file
|
|
504
|
+
if allow_serve is not None:
|
|
505
|
+
conf.allow_serve = allow_serve
|
|
482
506
|
ctx.conf = conf
|
|
483
507
|
|
|
508
|
+
from execsql.script.preparation import prepare_script
|
|
509
|
+
|
|
510
|
+
tree = prepare_script(tree, conf.include_req, conf.include_opt)
|
|
511
|
+
|
|
484
512
|
# Capture output to a buffer (suppress stdout/stderr)
|
|
485
513
|
stdout_buf = io.StringIO()
|
|
486
514
|
stderr_buf = io.StringIO()
|
|
@@ -521,7 +549,7 @@ def run(
|
|
|
521
549
|
# ------------------------------------------------------------------
|
|
522
550
|
# Execute
|
|
523
551
|
# ------------------------------------------------------------------
|
|
524
|
-
from execsql.script.executor import execute
|
|
552
|
+
from execsql.script.executor import _roll_back_after_interrupt, execute
|
|
525
553
|
|
|
526
554
|
errors: list[ScriptError] = []
|
|
527
555
|
t0 = time.perf_counter()
|
|
@@ -534,8 +562,11 @@ def run(
|
|
|
534
562
|
try:
|
|
535
563
|
execute(tree, ctx=ctx)
|
|
536
564
|
except SystemExit as exc:
|
|
537
|
-
# exit_now()
|
|
538
|
-
|
|
565
|
+
# exit_now() raises ScriptExit; any other SystemExit (a signal
|
|
566
|
+
# handler's) interrupted the run. Either is reported as an error.
|
|
567
|
+
if not isinstance(exc, ScriptExit):
|
|
568
|
+
_roll_back_after_interrupt(ctx)
|
|
569
|
+
_capture_errors(ctx, errors, exc.code, interrupted=not isinstance(exc, ScriptExit))
|
|
539
570
|
except ErrInfo as exc:
|
|
540
571
|
errors.append(
|
|
541
572
|
ScriptError(
|
|
@@ -547,6 +578,9 @@ def run(
|
|
|
547
578
|
)
|
|
548
579
|
except Exception as exc:
|
|
549
580
|
errors.append(ScriptError(message=str(exc), source="<runtime>"))
|
|
581
|
+
except BaseException:
|
|
582
|
+
_roll_back_after_interrupt(ctx)
|
|
583
|
+
raise
|
|
550
584
|
|
|
551
585
|
# Non-halting SQL errors (halt_on_error=False) are recorded in
|
|
552
586
|
# status.error_history as execution continues; a halting error never
|
|
@@ -562,13 +596,15 @@ def run(
|
|
|
562
596
|
|
|
563
597
|
# Flush and close every file the script wrote, so the caller can read
|
|
564
598
|
# them the moment run() returns. Output to a file that could not be
|
|
565
|
-
# opened is reported as an error.
|
|
599
|
+
# opened is reported as an error. A blocked writer is abandoned;
|
|
600
|
+
# healthy writers are stopped below.
|
|
566
601
|
from execsql.utils.fileio import filewriter_close_all_after_write
|
|
567
602
|
|
|
568
603
|
try:
|
|
569
604
|
filewriter_close_all_after_write()
|
|
570
605
|
except ErrInfo as exc:
|
|
571
|
-
|
|
606
|
+
if ctx.halt_output_error is not exc:
|
|
607
|
+
errors.append(ScriptError(message=exc.errmsg(), source="<output>"))
|
|
572
608
|
except Exception:
|
|
573
609
|
pass # Best-effort: a failed flush must not mask a script error.
|
|
574
610
|
|
|
@@ -589,9 +625,12 @@ def run(
|
|
|
589
625
|
from execsql.utils.fileio import filewriter_end
|
|
590
626
|
|
|
591
627
|
filewriter_end(ctx.filewriter)
|
|
592
|
-
if
|
|
628
|
+
if ctx.dbs is not None:
|
|
593
629
|
try:
|
|
594
|
-
|
|
630
|
+
if owns_connection:
|
|
631
|
+
ctx.dbs.closeall()
|
|
632
|
+
else:
|
|
633
|
+
ctx.dbs.closeall(except_db=connection)
|
|
595
634
|
except Exception:
|
|
596
635
|
pass
|
|
597
636
|
if ctx.tempfiles is not None:
|
|
@@ -621,8 +660,8 @@ def _start_filewriter(ctx: RuntimeContext, conf: Any) -> None:
|
|
|
621
660
|
writer, and every entry point in :mod:`execsql.utils.fileio` drops the
|
|
622
661
|
write when none is running. A writer per run keeps concurrent runs from
|
|
623
662
|
sharing open files or reply queues, and a halt in one run (which stops
|
|
624
|
-
its writer) from discarding another run's output.
|
|
625
|
-
|
|
663
|
+
its writer) from discarding another run's output. Healthy writers stop
|
|
664
|
+
before run() returns; a blocked daemon is abandoned with an output error.
|
|
626
665
|
"""
|
|
627
666
|
from execsql.utils.fileio import FileWriter
|
|
628
667
|
|
|
@@ -633,7 +672,13 @@ def _start_filewriter(ctx: RuntimeContext, conf: Any) -> None:
|
|
|
633
672
|
ctx.filewriter.start()
|
|
634
673
|
|
|
635
674
|
|
|
636
|
-
def _capture_errors(
|
|
675
|
+
def _capture_errors(
|
|
676
|
+
ctx: RuntimeContext,
|
|
677
|
+
errors: list[ScriptError],
|
|
678
|
+
exit_code: Any,
|
|
679
|
+
*,
|
|
680
|
+
interrupted: bool = False,
|
|
681
|
+
) -> None:
|
|
637
682
|
"""Record the error that halted the run, after ``exit_now()`` raised ``SystemExit``.
|
|
638
683
|
|
|
639
684
|
``exit_now`` leaves the halting :class:`ErrInfo` on the context, and any
|
|
@@ -641,10 +686,15 @@ def _capture_errors(ctx: RuntimeContext, errors: list[ScriptError], exit_code: A
|
|
|
641
686
|
SQL statement's text is in ``$LAST_ERROR``, which the executor sets just
|
|
642
687
|
before halting; it is reported only when the halt came from SQL, so a
|
|
643
688
|
metacommand error is never paired with an earlier statement. A ``HALT``
|
|
644
|
-
carries no error, only its exit status
|
|
689
|
+
carries no error, only its exit status; nor does a run *interrupted* by a
|
|
690
|
+
``SystemExit`` that execsql did not raise (a signal handler's).
|
|
645
691
|
"""
|
|
646
692
|
err = ctx.halt_error
|
|
647
|
-
if err is None:
|
|
693
|
+
if err is None and interrupted:
|
|
694
|
+
msg = "Script interrupted by SystemExit" + (
|
|
695
|
+
f" with exit status {exit_code}." if type(exit_code) is int else "."
|
|
696
|
+
)
|
|
697
|
+
elif err is None:
|
|
648
698
|
msg = f"Script halted by HALT with exit status {exit_code}."
|
|
649
699
|
else:
|
|
650
700
|
msg = err.errmsg()
|
execsql/cli/commands/run.py
CHANGED
|
@@ -114,9 +114,9 @@ def main(
|
|
|
114
114
|
"--output-dir",
|
|
115
115
|
metavar="DIR",
|
|
116
116
|
help=(
|
|
117
|
-
"
|
|
117
|
+
"Containment root for EXPORT output files. "
|
|
118
118
|
"Relative paths in EXPORT metacommands are joined to this directory. "
|
|
119
|
-
"Absolute paths
|
|
119
|
+
"Absolute paths must also stay inside it; stdout is exempt. WRITE is not constrained."
|
|
120
120
|
),
|
|
121
121
|
),
|
|
122
122
|
progress: bool = typer.Option(
|
execsql/cli/lint.py
CHANGED
|
@@ -52,7 +52,7 @@ import functools
|
|
|
52
52
|
import json
|
|
53
53
|
import re
|
|
54
54
|
from collections import Counter
|
|
55
|
-
from collections.abc import Iterable
|
|
55
|
+
from collections.abc import Callable, Iterable
|
|
56
56
|
from pathlib import Path
|
|
57
57
|
from typing import TYPE_CHECKING, NamedTuple
|
|
58
58
|
|
|
@@ -730,6 +730,21 @@ def _placeholder_forms(text: str) -> list[str]:
|
|
|
730
730
|
return [_with_placeholders(text, value) for value in _PLACEHOLDERS]
|
|
731
731
|
|
|
732
732
|
|
|
733
|
+
def _condition_placeholder(text: str, value: str) -> tuple[str, Callable[[int], tuple[str, int]]]:
|
|
734
|
+
"""Replace variables and retain a map from parsed offsets to original text."""
|
|
735
|
+
pieces: list[str] = []
|
|
736
|
+
offsets: list[int] = []
|
|
737
|
+
start = 0
|
|
738
|
+
for match in _RX_ANY_VAR.finditer(text):
|
|
739
|
+
pieces.extend((text[start : match.start()], value))
|
|
740
|
+
offsets.extend(range(start, match.start()))
|
|
741
|
+
offsets.extend([match.start()] * len(value))
|
|
742
|
+
start = match.end()
|
|
743
|
+
pieces.append(text[start:])
|
|
744
|
+
offsets.extend(range(start, len(text) + 1)) # includes the end-of-input position
|
|
745
|
+
return "".join(pieces), lambda position: (text, offsets[position])
|
|
746
|
+
|
|
747
|
+
|
|
733
748
|
# Named groups that capture a condition in a metacommand's pattern: ASSERT,
|
|
734
749
|
# WAIT_UNTIL, and the WHILE / UNTIL of ON ... EXECUTE SCRIPT.
|
|
735
750
|
_CONDITION_GROUPS = ("condtest", "condition", "loopcond")
|
|
@@ -777,15 +792,32 @@ def _check_condition(condition: str, source: str, line_no: int, issues: list[_Is
|
|
|
777
792
|
from execsql.exceptions import CondParserError
|
|
778
793
|
from execsql.parser import CondParser
|
|
779
794
|
|
|
780
|
-
forms =
|
|
781
|
-
|
|
795
|
+
forms = (
|
|
796
|
+
[_condition_placeholder(condition, value) for value in _PLACEHOLDERS]
|
|
797
|
+
if _RX_ANY_VAR.search(condition)
|
|
798
|
+
else [(condition, None)]
|
|
799
|
+
)
|
|
800
|
+
message = ""
|
|
801
|
+
furthest = -1
|
|
802
|
+
for form, original in forms:
|
|
803
|
+
parser = CondParser(form, _conditional_table(), diagnostic_source=original)
|
|
782
804
|
try:
|
|
783
|
-
|
|
784
|
-
except
|
|
785
|
-
|
|
786
|
-
|
|
805
|
+
parser.parse()
|
|
806
|
+
except CondParserError as exc:
|
|
807
|
+
problem = str(exc)
|
|
808
|
+
except RecursionError:
|
|
809
|
+
problem = "Condition nests too deeply to parse."
|
|
810
|
+
else:
|
|
811
|
+
return
|
|
812
|
+
position = parser.cond_expr.currpos
|
|
813
|
+
if original is not None:
|
|
814
|
+
_, position = original(position)
|
|
815
|
+
# A boolean placeholder can reach a real error after the variable;
|
|
816
|
+
# an identifier placeholder might fail at the variable itself.
|
|
817
|
+
if position > furthest:
|
|
818
|
+
furthest, message = position, problem
|
|
787
819
|
issues.append(
|
|
788
|
-
_issue("P004", source, line_no,
|
|
820
|
+
_issue("P004", source, line_no, message),
|
|
789
821
|
)
|
|
790
822
|
|
|
791
823
|
|
|
@@ -1079,7 +1111,12 @@ def lint(
|
|
|
1079
1111
|
issues.append(_issue("S001", script_path or script.source, 0, "script is empty"))
|
|
1080
1112
|
return issues
|
|
1081
1113
|
|
|
1082
|
-
|
|
1114
|
+
if script_path:
|
|
1115
|
+
script_dir = Path(script_path).resolve().parent
|
|
1116
|
+
elif base_dir:
|
|
1117
|
+
script_dir = Path(base_dir)
|
|
1118
|
+
else:
|
|
1119
|
+
script_dir = None
|
|
1083
1120
|
script_blocks = _collect_script_blocks(script)
|
|
1084
1121
|
|
|
1085
1122
|
# Pass 1: collect all variable definitions
|
execsql/cli/run.py
CHANGED
|
@@ -14,6 +14,7 @@ import os
|
|
|
14
14
|
import platform
|
|
15
15
|
import sys
|
|
16
16
|
import traceback
|
|
17
|
+
from collections.abc import Callable
|
|
17
18
|
from pathlib import Path
|
|
18
19
|
from typing import TYPE_CHECKING, Any
|
|
19
20
|
|
|
@@ -23,6 +24,7 @@ from execsql.cli.help import _console, _err_console
|
|
|
23
24
|
from execsql.config import ConfigData, StatObj
|
|
24
25
|
from execsql.exceptions import ConfigError, ErrInfo
|
|
25
26
|
from execsql.script import SubVarSet, current_script_line, substitute_vars
|
|
27
|
+
from execsql.script.preparation import prepare_script
|
|
26
28
|
from execsql.utils.fileio import FileWriter, Logger, filewriter_end
|
|
27
29
|
from execsql.utils.gui import gui_connect, gui_console_isrunning, gui_console_off, gui_console_on, gui_console_wait_user
|
|
28
30
|
|
|
@@ -735,6 +737,8 @@ def _run(
|
|
|
735
737
|
for name, value in named_vars or ():
|
|
736
738
|
_state.subvars.add_substitution(name, value)
|
|
737
739
|
_ast_tree = _load_script(command, script_name, conf.script_encoding)
|
|
740
|
+
if _ast_tree is not None:
|
|
741
|
+
_ast_tree = prepare_script(_ast_tree, conf.include_req, conf.include_opt)
|
|
738
742
|
_print_dry_run(_ast_tree)
|
|
739
743
|
raise SystemExit(0)
|
|
740
744
|
|
|
@@ -795,6 +799,8 @@ def _run(
|
|
|
795
799
|
# Load the SQL script (--dry-run / --ping already exited above)
|
|
796
800
|
# ------------------------------------------------------------------
|
|
797
801
|
_ast_tree = _load_script(command, script_name, conf.script_encoding)
|
|
802
|
+
if _ast_tree is not None:
|
|
803
|
+
_ast_tree = prepare_script(_ast_tree, conf.include_req, conf.include_opt)
|
|
798
804
|
|
|
799
805
|
# ------------------------------------------------------------------
|
|
800
806
|
# Start GUI console if requested
|
|
@@ -802,54 +808,54 @@ def _run(
|
|
|
802
808
|
if conf.gui_level > 2:
|
|
803
809
|
gui_console_on()
|
|
804
810
|
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
from execsql.utils.errors import fatal_error
|
|
811
|
+
def initialize() -> None:
|
|
812
|
+
"""Connect and initialize script state on the execution thread."""
|
|
813
|
+
if conf.server is None and conf.db is None and conf.db_file is None:
|
|
814
|
+
if conf.gui_level > 1:
|
|
815
|
+
gui_connect("initial", f"Select the database to use with {script_name or '<inline>'}.")
|
|
816
|
+
db = _state.dbs.current()
|
|
817
|
+
else:
|
|
818
|
+
from execsql.utils.errors import fatal_error
|
|
814
819
|
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
820
|
+
fatal_error(
|
|
821
|
+
"Database not specified in configuration files or command-line arguments, and prompt not requested.",
|
|
822
|
+
)
|
|
823
|
+
else:
|
|
824
|
+
db = _connect_initial_db(conf)
|
|
825
|
+
_state.dbs.add("initial", db)
|
|
821
826
|
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
827
|
+
_state.exec_log.log_db_connect(db)
|
|
828
|
+
if manifest is not None:
|
|
829
|
+
manifest.set_database(db)
|
|
830
|
+
_state.subvars.add_substitution("$CURRENT_DBMS", db.type.dbms_id)
|
|
831
|
+
_state.subvars.add_substitution("$CURRENT_DATABASE", db.name())
|
|
832
|
+
_state.subvars.add_substitution("$DB_SERVER", db.server_name)
|
|
833
|
+
_state.subvars.add_substitution("$SYSTEM_CMD_EXIT_STATUS", "0")
|
|
829
834
|
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
# ------------------------------------------------------------------
|
|
833
|
-
atexit.register(_state.dbs.closeall)
|
|
834
|
-
_state.dbs.do_rollback = True
|
|
835
|
+
atexit.register(_state.dbs.closeall)
|
|
836
|
+
_state.dbs.do_rollback = True
|
|
835
837
|
|
|
836
|
-
|
|
837
|
-
|
|
838
|
+
if profile:
|
|
839
|
+
_state.profile_data = []
|
|
838
840
|
|
|
839
|
-
|
|
840
|
-
|
|
841
|
+
if debug:
|
|
842
|
+
_state.step_mode = True
|
|
841
843
|
|
|
842
|
-
|
|
843
|
-
|
|
844
|
+
if no_system_cmd:
|
|
845
|
+
conf.allow_system_cmd = False
|
|
844
846
|
|
|
845
|
-
|
|
846
|
-
|
|
847
|
+
if no_rm_file:
|
|
848
|
+
conf.allow_rm_file = False
|
|
847
849
|
|
|
848
|
-
|
|
849
|
-
|
|
850
|
+
if no_serve:
|
|
851
|
+
conf.allow_serve = False
|
|
850
852
|
|
|
851
|
-
if _ast_tree is not None:
|
|
852
|
-
_execute_script_ast(_ast_tree, conf, profile=profile, profile_limit=profile_limit)
|
|
853
|
+
if _ast_tree is not None and _uses_textual_console(conf):
|
|
854
|
+
_execute_script_ast(_ast_tree, conf, profile=profile, profile_limit=profile_limit, initialize=initialize)
|
|
855
|
+
else:
|
|
856
|
+
initialize()
|
|
857
|
+
if _ast_tree is not None:
|
|
858
|
+
_execute_script_ast(_ast_tree, conf, profile=profile, profile_limit=profile_limit)
|
|
853
859
|
if manifest is not None:
|
|
854
860
|
manifest.finish(0)
|
|
855
861
|
|
|
@@ -876,29 +882,37 @@ def _end_run_on_sigterm() -> None:
|
|
|
876
882
|
signal.signal(signal.SIGTERM, terminate)
|
|
877
883
|
|
|
878
884
|
|
|
885
|
+
def _uses_textual_console(conf: ConfigData) -> bool:
|
|
886
|
+
import execsql.utils.gui as _gui
|
|
887
|
+
|
|
888
|
+
if conf.gui_level > 2:
|
|
889
|
+
try:
|
|
890
|
+
from execsql.gui.tui import TextualBackend
|
|
891
|
+
|
|
892
|
+
return isinstance(_gui._active_backend, TextualBackend)
|
|
893
|
+
except ImportError:
|
|
894
|
+
pass
|
|
895
|
+
return False
|
|
896
|
+
|
|
897
|
+
|
|
879
898
|
def _execute_script_ast(
|
|
880
899
|
tree: Any,
|
|
881
900
|
conf: ConfigData,
|
|
882
901
|
*,
|
|
883
902
|
profile: bool = False,
|
|
884
903
|
profile_limit: int = 20,
|
|
904
|
+
initialize: Callable[[], None] | None = None,
|
|
885
905
|
) -> None:
|
|
886
|
-
"""Execute a script
|
|
906
|
+
"""Execute a script, optionally initializing its connections on its owning thread."""
|
|
887
907
|
import execsql.state as _state
|
|
888
|
-
import execsql.utils.gui as _gui
|
|
889
908
|
|
|
890
909
|
from execsql.script.executor import execute
|
|
891
910
|
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
if isinstance(_gui._active_backend, TextualBackend):
|
|
898
|
-
_execute_script_textual_console(tree, conf)
|
|
899
|
-
return
|
|
900
|
-
except ImportError:
|
|
901
|
-
pass
|
|
911
|
+
if _uses_textual_console(conf):
|
|
912
|
+
_execute_script_textual_console(tree, conf, initialize=initialize)
|
|
913
|
+
return
|
|
914
|
+
if initialize is not None:
|
|
915
|
+
initialize()
|
|
902
916
|
|
|
903
917
|
try:
|
|
904
918
|
execute(tree)
|
|
@@ -945,20 +959,73 @@ def _execute_script_ast(
|
|
|
945
959
|
_state.exec_log.log_exit_end()
|
|
946
960
|
|
|
947
961
|
|
|
948
|
-
def _execute_script_textual_console(
|
|
949
|
-
|
|
962
|
+
def _execute_script_textual_console(
|
|
963
|
+
tree: Any,
|
|
964
|
+
conf: ConfigData,
|
|
965
|
+
*,
|
|
966
|
+
initialize: Callable[[], None] | None = None,
|
|
967
|
+
) -> None:
|
|
968
|
+
"""Run the script while ConsoleApp runs on main; own connections when initializing."""
|
|
950
969
|
import execsql.state as _state
|
|
951
970
|
import execsql.utils.gui as _gui
|
|
952
971
|
from execsql.gui.tui import ConsoleApp, _ConsoleDialogQueue
|
|
953
972
|
|
|
954
|
-
from execsql.script.executor import execute
|
|
973
|
+
from execsql.script.executor import _CONTROL_FLOW, _roll_back_after_interrupt, execute
|
|
955
974
|
|
|
956
975
|
dialog_queue = _ConsoleDialogQueue()
|
|
957
976
|
_state.gui_manager_queue = dialog_queue
|
|
958
977
|
|
|
959
978
|
ctx = _state.get_context()
|
|
979
|
+
|
|
980
|
+
def run_script() -> None:
|
|
981
|
+
with _state.active_context(ctx):
|
|
982
|
+
try:
|
|
983
|
+
try:
|
|
984
|
+
if initialize is not None:
|
|
985
|
+
try:
|
|
986
|
+
initialize()
|
|
987
|
+
except ConfigError:
|
|
988
|
+
raise
|
|
989
|
+
except Exception as exc:
|
|
990
|
+
from execsql.utils.errors import exit_now
|
|
991
|
+
|
|
992
|
+
error = exc if isinstance(exc, ErrInfo) else ErrInfo("exception", exception_msg=str(exc))
|
|
993
|
+
exit_now(1, error)
|
|
994
|
+
try:
|
|
995
|
+
execute(tree, ctx=ctx)
|
|
996
|
+
except ConfigError:
|
|
997
|
+
raise
|
|
998
|
+
except Exception as exc:
|
|
999
|
+
from execsql.utils.errors import exit_now
|
|
1000
|
+
|
|
1001
|
+
if isinstance(exc, ErrInfo):
|
|
1002
|
+
error = exc
|
|
1003
|
+
else:
|
|
1004
|
+
strace = traceback.extract_tb(exc.__traceback__)[-1:]
|
|
1005
|
+
lno = strace[0][1] if strace else "?"
|
|
1006
|
+
msg = f"{Path(sys.argv[0]).name}: Uncaught exception {type(exc)} ({exc}) on line {lno}"
|
|
1007
|
+
script, slno = current_script_line()
|
|
1008
|
+
if script is not None:
|
|
1009
|
+
msg += f" in script {script}, line {slno}"
|
|
1010
|
+
error = ErrInfo("exception", exception_msg=msg)
|
|
1011
|
+
# Deferred error scripts need the worker's live connections.
|
|
1012
|
+
exit_now(1, error)
|
|
1013
|
+
_flush_output_files()
|
|
1014
|
+
_state.dbs.do_rollback = False
|
|
1015
|
+
finally:
|
|
1016
|
+
if initialize is not None:
|
|
1017
|
+
_state.dbs.closeall()
|
|
1018
|
+
except (*_CONTROL_FLOW, Exception):
|
|
1019
|
+
raise
|
|
1020
|
+
except BaseException:
|
|
1021
|
+
_roll_back_after_interrupt(ctx)
|
|
1022
|
+
if initialize is not None:
|
|
1023
|
+
# Quit can arrive during closeall itself; finish cleanup on its owner.
|
|
1024
|
+
_state.dbs.closeall()
|
|
1025
|
+
raise
|
|
1026
|
+
|
|
960
1027
|
app = ConsoleApp(
|
|
961
|
-
script_runner=
|
|
1028
|
+
script_runner=run_script,
|
|
962
1029
|
dialog_queue=dialog_queue,
|
|
963
1030
|
wait_on_exit=conf.gui_wait_on_exit,
|
|
964
1031
|
)
|
|
@@ -969,9 +1036,15 @@ def _execute_script_textual_console(tree: Any, conf: ConfigData) -> None:
|
|
|
969
1036
|
try:
|
|
970
1037
|
app.run()
|
|
971
1038
|
finally:
|
|
1039
|
+
app.wait_for_script()
|
|
972
1040
|
_state.output.reset()
|
|
973
1041
|
_gui._active_backend._console_app = None
|
|
974
1042
|
|
|
1043
|
+
if app._quit_requested or isinstance(app._script_exception, KeyboardInterrupt):
|
|
1044
|
+
_state.exec_log.log_exit_interrupted()
|
|
1045
|
+
_state.exec_log.log_status_info(f"{_state.cmds_run} commands run")
|
|
1046
|
+
raise SystemExit(130)
|
|
1047
|
+
|
|
975
1048
|
if app._script_exception is not None:
|
|
976
1049
|
exc = app._script_exception
|
|
977
1050
|
if isinstance(exc, SystemExit):
|
|
@@ -995,7 +1068,6 @@ def _execute_script_textual_console(tree: Any, conf: ConfigData) -> None:
|
|
|
995
1068
|
exit_now(1, ErrInfo("exception", exception_msg=msg))
|
|
996
1069
|
|
|
997
1070
|
_state.dbs.do_rollback = False
|
|
998
|
-
_flush_output_files()
|
|
999
1071
|
_state.exec_log.log_status_info(f"{_state.cmds_run} commands run")
|
|
1000
1072
|
_state.exec_log.log_exit_end()
|
|
1001
1073
|
|
execsql/config.py
CHANGED
|
@@ -23,6 +23,7 @@ from configparser import ConfigParser
|
|
|
23
23
|
from pathlib import Path
|
|
24
24
|
from typing import Protocol
|
|
25
25
|
|
|
26
|
+
from execsql.utils.spreadsheet import INPUT_LIMIT_DEFAULTS
|
|
26
27
|
from execsql.utils.paths import is_network_path
|
|
27
28
|
from execsql.exceptions import ConfigError, ConfigFileError
|
|
28
29
|
from execsql.utils.crypto import Encrypt
|
|
@@ -296,6 +297,10 @@ class ConfigData:
|
|
|
296
297
|
("ignore", "replace", "xmlcharrefreplace", "backslashreplace"),
|
|
297
298
|
)
|
|
298
299
|
# --- [input] ---
|
|
300
|
+
for name in INPUT_LIMIT_DEFAULTS:
|
|
301
|
+
self._get_int(cp, self._INPUT_SECTION, name, name)
|
|
302
|
+
if cp.has_option(self._INPUT_SECTION, name) and getattr(self, name) < 0:
|
|
303
|
+
raise ConfigError(f"Invalid {name}: {getattr(self, name)}; must be >= 0.")
|
|
299
304
|
self._get_int(cp, self._INPUT_SECTION, "max_int", "max_int")
|
|
300
305
|
self._get_bool(cp, self._INPUT_SECTION, "boolean_int", "boolean_int")
|
|
301
306
|
self._get_bool(cp, self._INPUT_SECTION, "boolean_words", "boolean_words")
|
|
@@ -500,6 +505,8 @@ class ConfigData:
|
|
|
500
505
|
self.import_encoding = "utf8"
|
|
501
506
|
self.enc_err_disposition: str | None = None
|
|
502
507
|
self.import_common_cols_only = False
|
|
508
|
+
for name, default in INPUT_LIMIT_DEFAULTS.items():
|
|
509
|
+
setattr(self, name, default)
|
|
503
510
|
self.max_int = 2147483647
|
|
504
511
|
self.boolean_int = True
|
|
505
512
|
self.boolean_words = False
|