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.
Files changed (64) hide show
  1. execsql/api.py +95 -45
  2. execsql/cli/commands/run.py +2 -2
  3. execsql/cli/lint.py +6 -1
  4. execsql/cli/run.py +5 -0
  5. execsql/config.py +7 -0
  6. execsql/data/execsql.conf.template +21 -0
  7. execsql/db/base.py +52 -24
  8. execsql/db/duckdb.py +43 -4
  9. execsql/db/firebird.py +8 -0
  10. execsql/db/mysql.py +8 -3
  11. execsql/db/oracle.py +5 -0
  12. execsql/db/postgres.py +2 -2
  13. execsql/db/sqlite.py +117 -10
  14. execsql/db/sqlserver.py +11 -7
  15. execsql/db/target_types.py +151 -4
  16. execsql/debug/repl.py +2 -1
  17. execsql/exceptions.py +14 -0
  18. execsql/exporters/html.py +26 -42
  19. execsql/exporters/ods.py +93 -21
  20. execsql/exporters/sqlite.py +82 -47
  21. execsql/format.py +12 -4
  22. execsql/gui/desktop.py +2 -1
  23. execsql/gui/tui.py +5 -2
  24. execsql/importers/base.py +9 -7
  25. execsql/importers/json.py +37 -13
  26. execsql/importers/ods.py +22 -2
  27. execsql/lsp/document.py +6 -1
  28. execsql/lsp/hover.py +2 -1
  29. execsql/metacommands/conditions.py +3 -1
  30. execsql/metacommands/connect.py +18 -3
  31. execsql/metacommands/dispatch.py +10 -0
  32. execsql/metacommands/io.py +1 -0
  33. execsql/metacommands/io_export.py +1 -2
  34. execsql/metacommands/io_fileops.py +26 -90
  35. execsql/metacommands/io_import.py +26 -8
  36. execsql/metacommands/reference.py +7 -0
  37. execsql/parser.py +14 -37
  38. execsql/script/control.py +17 -7
  39. execsql/script/executor.py +160 -34
  40. execsql/script/preparation.py +42 -0
  41. execsql/types.py +6 -5
  42. execsql/utils/errors.py +2 -2
  43. execsql/utils/fileio.py +342 -134
  44. execsql/utils/spreadsheet.py +74 -0
  45. execsql/utils/strings.py +13 -7
  46. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/README.md +5 -5
  47. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/example_config_prompt.sql +2 -2
  48. {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/METADATA +32 -119
  49. {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/RECORD +64 -62
  50. {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/WHEEL +1 -1
  51. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
  52. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
  53. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_compare.sql +0 -0
  54. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
  55. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
  56. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
  57. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
  58. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/script_template.sql +0 -0
  59. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
  60. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
  61. {execsql2-2.37.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
  62. {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/entry_points.txt +0 -0
  63. {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/licenses/LICENSE.txt +0 -0
  64. {execsql2-2.37.0.dist-info → execsql2-2.38.0.dist-info}/licenses/NOTICE +0 -0
@@ -50,7 +50,7 @@ from pathlib import Path
50
50
  from typing import Any, cast
51
51
  from collections.abc import Callable
52
52
 
53
- from execsql.exceptions import ErrInfo
53
+ from execsql.exceptions import ErrInfo, ScriptExit
54
54
  from execsql.script.ast import (
55
55
  BatchBlock,
56
56
  Comment,
@@ -67,6 +67,7 @@ from execsql.script.ast import (
67
67
  SqlStatement,
68
68
  )
69
69
  from execsql import manifest as _manifest
70
+ from execsql.script.control import _roll_back
70
71
  from execsql.script.parser import substitute_directive
71
72
  from execsql.script.engine import set_dynamic_system_vars, set_static_system_vars, substitute_vars
72
73
  from execsql.script.variables import ScriptArgSubVarSet, SubVarSet
@@ -304,6 +305,17 @@ def _exec_sql(
304
305
  db.commit()
305
306
  except Exception as exc:
306
307
  e = _as_errinfo(exc)
308
+ except _CONTROL_FLOW:
309
+ raise
310
+ except BaseException:
311
+ # Interrupted (Ctrl-C) after the statement ran: treat it as a failed
312
+ # statement, or the next commit stores it -- a BREAKPOINT prompt
313
+ # carries on, and execsql.run()'s caller keeps the connection.
314
+ try:
315
+ _record_interrupt(ctx, cmd, source, line_no, sql=True)
316
+ finally:
317
+ dbs.current().rollback()
318
+ raise
307
319
  if e:
308
320
  _sql_failed(ctx, e, cmd, source, line_no)
309
321
  return
@@ -312,6 +324,55 @@ def _exec_sql(
312
324
  ctx.prompt_input(result)
313
325
 
314
326
 
327
+ def _record_interrupt(ctx: RuntimeContext, cmd: str, source: str, line_no: int, *, sql: bool) -> None:
328
+ """Record an interrupted statement or metacommand as a failed one, without halting.
329
+
330
+ The interrupt itself ends the run, or the BREAKPOINT prompt that caught
331
+ it reads the next input. The open batches fail first, so a second
332
+ interrupt during the rollback that follows cannot let one commit.
333
+ """
334
+ assert ctx.status is not None
335
+ assert ctx.subvars is not None
336
+ _fail_open_batches(ctx, source, line_no, what="an interrupt")
337
+ if sql:
338
+ ctx.status.sql_error = True
339
+ else:
340
+ ctx.status.metacommand_error = True
341
+ ctx.subvars.add_substitution("$LAST_ERROR", cmd)
342
+ ctx.subvars.add_substitution("$ERROR_MESSAGE", "Interrupted.")
343
+ if ctx.exec_log is not None:
344
+ kind = "SQL statement" if sql else "Metacommand"
345
+ ctx.exec_log.log_status_info(f"{kind} interrupted on line {line_no} of {source}.")
346
+
347
+
348
+ def _roll_back_after_interrupt(ctx: RuntimeContext) -> None:
349
+ """Roll back every connection with autocommit on, after an interrupt.
350
+
351
+ The executor rolls back what an interrupted statement, metacommand or
352
+ ``END BATCH`` did, but an interrupt can land between any two lines of
353
+ Python, such as between the rollbacks of a batch's databases. Whatever
354
+ is still pending on a connection with autocommit on is unfinished work
355
+ the script never committed, and the next commit on that connection
356
+ would store it: the script's own, after a BREAKPOINT prompt carries on
357
+ (:func:`execute_input`), or the caller's, on a connection passed to
358
+ :func:`execsql.run` that outlives the run. A connection under
359
+ AUTOCOMMIT OFF is left alone; its transaction is the script's. A
360
+ second interrupt does not stop the other connections' rollbacks.
361
+ """
362
+ if ctx.dbs is None:
363
+ return
364
+ interrupt: BaseException | None = None
365
+ for alias in ctx.dbs.aliases():
366
+ db = ctx.dbs.aliased_as(alias)
367
+ try:
368
+ if db.autocommit:
369
+ db.rollback()
370
+ except BaseException as exc:
371
+ interrupt = interrupt or exc
372
+ if interrupt is not None:
373
+ raise interrupt
374
+
375
+
315
376
  def _sql_failed(ctx: RuntimeContext, e: ErrInfo, cmd: str, source: str, line_no: int) -> None:
316
377
  """Report an SQL error: halt under ERROR_HALT ON, else record it and carry on.
317
378
 
@@ -350,17 +411,19 @@ def _as_errinfo(exc: Exception) -> ErrInfo:
350
411
  return e
351
412
 
352
413
 
353
- def _fail_open_batches(ctx: RuntimeContext, source: str, line_no: int) -> None:
414
+ def _fail_open_batches(ctx: RuntimeContext, source: str, line_no: int | None = None, *, what: str = "an error") -> None:
354
415
  """Keep every open batch from committing after an error inside it.
355
416
 
356
417
  Whatever failed (a statement, a metacommand, or SQL a metacommand ran),
357
418
  the batch is not committed: a database error may already have rolled
358
419
  back the batch's earlier work, and execsql cannot tell from here whether
359
- it did.
420
+ it did. *what* names the failure in END BATCH's error, at *line_no* of
421
+ *source*, or in *source* when there is no line.
360
422
  """
361
423
  assert ctx.status is not None
362
424
  if ctx.status.batch.in_batch():
363
- ctx.status.batch.fail_enclosing(f"an error on line {line_no} of {source}")
425
+ where = f"on line {line_no} of {source}" if line_no is not None else f"in {source}"
426
+ ctx.status.batch.fail_enclosing(f"{what} {where}")
364
427
 
365
428
 
366
429
  def _metacommand_failed(
@@ -427,6 +490,20 @@ def _exec_metacommand(
427
490
  return result
428
491
  except Exception as exc:
429
492
  e = _as_errinfo(exc)
493
+ except _CONTROL_FLOW:
494
+ raise
495
+ except BaseException:
496
+ # Interrupted (Ctrl-C): what the metacommand wrote (EXECUTE, IMPORT,
497
+ # COPY, PG_UPSERT) may wait for a commit that never came. Each
498
+ # statement is committed as it runs, so on a connection with
499
+ # autocommit on that is all that is pending; under AUTOCOMMIT OFF it
500
+ # joins what the script holds, which the script decides.
501
+ try:
502
+ _record_interrupt(ctx, cmd, source, line_no, sql=False)
503
+ finally:
504
+ assert ctx.dbs is not None
505
+ _roll_back([ctx.dbs.aliased_as(alias) for alias in ctx.dbs.aliases()])
506
+ raise
430
507
  if e:
431
508
  _metacommand_failed(ctx, cmd, e, source, line_no)
432
509
  if (manifest := _manifest.current()) is not None:
@@ -677,7 +754,8 @@ def _execute_batch(
677
754
  on the END BATCH line, after the batch's uncommitted databases are rolled
678
755
  back, and every enclosing batch then rolls back at its own END BATCH
679
756
  instead of committing. Leaving it any other way (BREAK,
680
- an error, HALT) abandons it: see
757
+ an error, HALT, an interrupt, END BATCH's included before its commit)
758
+ abandons it: see
681
759
  :meth:`~execsql.script.control.BatchLevels.abandon_batch` for when that
682
760
  rolls back and when an enclosing batch takes the statements over. A
683
761
  halt usually gets there first: :func:`~execsql.utils.errors.exit_now`
@@ -688,40 +766,69 @@ def _execute_batch(
688
766
 
689
767
  status = ctx.status
690
768
  assert status is not None
691
- status.batch.new_batch()
692
- _push_block_frame(
693
- ctx,
694
- ExecFrame(kind="batch", label="", source=node.span.file, line=node.span.start_line),
695
- )
769
+ # This batch's level, once pushed.
770
+ depth = len(status.batch.batchlevels) + 1
696
771
  completed = False
697
772
  try:
698
- _execute_nodes(ctx, node.body, node.span.file, localvars, in_loop=in_loop)
773
+ status.batch.new_batch()
774
+ _push_block_frame(
775
+ ctx,
776
+ ExecFrame(kind="batch", label="", source=node.span.file, line=node.span.start_line),
777
+ )
778
+ try:
779
+ _execute_nodes(ctx, node.body, node.span.file, localvars, in_loop=in_loop)
780
+ finally:
781
+ ctx.ast_exec_stack.pop()
782
+ if status.batch.in_batch():
783
+ end_line = node.span.effective_end_line
784
+ try:
785
+ _end_batch(ctx, node.span.file, end_line)
786
+ except (*_CONTROL_FLOW, Exception):
787
+ # An error (normally an ErrInfo that exit_now() raises at a
788
+ # BREAKPOINT prompt, already reported) is not an interrupt.
789
+ raise
790
+ except BaseException:
791
+ # Interrupted at END BATCH: the enclosing batches must not
792
+ # commit the rest (a BREAKPOINT prompt carries on after Ctrl-C).
793
+ _record_interrupt(ctx, "END BATCH", node.span.file, end_line, sql=True)
794
+ raise
699
795
  completed = True
700
796
  finally:
701
- ctx.ast_exec_stack.pop()
702
- if status.batch.in_batch() and not completed:
703
- status.batch.abandon_batch()
704
- if status.batch.in_batch():
705
- end_line = node.span.effective_end_line
706
- ctx.last_command = ExecutingStatement(
707
- MetaCommandStatement(span=SourceSpan(node.span.file, end_line), command="END BATCH"),
708
- )
709
- failure = status.batch.failure()
710
- if failure is not None:
797
+ # Left any other way than through END BATCH, or interrupted at END
798
+ # BATCH before its commit: this level, if still open, is abandoned,
799
+ # with any deeper level an interrupt left behind.
800
+ # Only END BATCH's commit (end_batch) or abandoning takes it off the
801
+ # stack. An interrupt inside the rollbacks themselves can still
802
+ # leave work pending: _roll_back_after_interrupt() rolls back what is
803
+ # left, where the interrupt ends the run or the input typed at a
804
+ # BREAKPOINT prompt.
805
+ while not completed and len(status.batch.batchlevels) >= depth:
711
806
  status.batch.abandon_batch()
712
- e = ErrInfo(type="error", other_msg=f"The batch was not committed: {failure}.")
713
- _sql_failed(ctx, e, "END BATCH", node.span.file, end_line)
714
- return
715
- # The commit is the batch's last SQL: SQL_ERROR() right after END
716
- # BATCH says whether it failed, as after any statement.
717
- status.sql_error = False
718
- try:
719
- status.batch.end_batch()
720
- except Exception as exc:
721
- # end_batch has rolled back what it had not committed, which can
722
- # include an enclosing batch's statements on a shared connection;
723
- # _sql_failed keeps that batch from committing the rest.
724
- _sql_failed(ctx, _as_errinfo(exc), "END BATCH", node.span.file, end_line)
807
+
808
+
809
+ def _end_batch(ctx: RuntimeContext, source: str, end_line: int) -> None:
810
+ """END BATCH: commit the innermost batch, or roll it back if anything in it failed."""
811
+ status = ctx.status
812
+ assert status is not None
813
+ ctx.last_command = ExecutingStatement(
814
+ MetaCommandStatement(span=SourceSpan(source, end_line), command="END BATCH"),
815
+ )
816
+ failure = status.batch.failure()
817
+ if failure is not None:
818
+ status.batch.abandon_batch()
819
+ e = ErrInfo(type="error", other_msg=f"The batch was not committed: {failure}.")
820
+ _sql_failed(ctx, e, "END BATCH", source, end_line)
821
+ return
822
+ # The commit is the batch's last SQL: SQL_ERROR() right after END
823
+ # BATCH says whether it failed, as after any statement.
824
+ status.sql_error = False
825
+ try:
826
+ status.batch.end_batch()
827
+ except Exception as exc:
828
+ # end_batch has rolled back what it had not committed, which can
829
+ # include an enclosing batch's statements on a shared connection;
830
+ # _sql_failed keeps that batch from committing the rest.
831
+ _sql_failed(ctx, _as_errinfo(exc), "END BATCH", source, end_line)
725
832
 
726
833
 
727
834
  def _pre_register_scripts(ctx: RuntimeContext, nodes: list[Node]) -> None:
@@ -1082,6 +1189,13 @@ class _BreakLoop(BaseException):
1082
1189
  """
1083
1190
 
1084
1191
 
1192
+ # BaseExceptions that are execsql's own control flow, not an interrupt:
1193
+ # ScriptExit (HALT, a halting error, a cancelled PROMPT or dialog, ``.quit``)
1194
+ # and _BreakLoop (BREAK). Any other BaseException -- KeyboardInterrupt, or a
1195
+ # plain SystemExit from a signal handler -- is an interrupt.
1196
+ _CONTROL_FLOW = (ScriptExit, _BreakLoop)
1197
+
1198
+
1085
1199
  def _break_has_target(ctx: RuntimeContext) -> bool:
1086
1200
  """Whether a BREAK at the current statement has something to leave.
1087
1201
 
@@ -1261,6 +1375,18 @@ def execute_input(
1261
1375
  ctx.prompt_input = show
1262
1376
  try:
1263
1377
  _execute_nodes(ctx, script.body, script.source)
1378
+ except _CONTROL_FLOW:
1379
+ raise
1380
+ except BaseException as exc:
1381
+ # The prompt reports the error or catches Ctrl-C, and the script
1382
+ # carries on, so whatever was stopped (a statement, a condition's
1383
+ # query, END BATCH) may have left a batch's work half done or
1384
+ # rolled back: no open batch may commit.
1385
+ what = "an error" if isinstance(exc, Exception) else "an interrupt"
1386
+ _fail_open_batches(ctx, f"input typed at {script.source}", what=what)
1387
+ if not isinstance(exc, Exception):
1388
+ _roll_back_after_interrupt(ctx)
1389
+ raise
1264
1390
  finally:
1265
1391
  ctx.prompt_input = prompt_input
1266
1392
  ctx.last_command = last_command
@@ -0,0 +1,42 @@
1
+ """Build the shared CLI/library execution plan for configured startup files."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Sequence
6
+ from pathlib import Path
7
+ from typing import TYPE_CHECKING
8
+
9
+ if TYPE_CHECKING:
10
+ from execsql.script.ast import Script
11
+
12
+
13
+ def prepare_script(script: Script, required: Sequence[str], optional: Sequence[str]) -> Script:
14
+ """Prepend configured includes without changing the parsed main script.
15
+
16
+ ConfigData has resolved and ordered the filenames. Required files precede
17
+ optional ones; each configured file runs once, and the main file is not
18
+ included into itself. Normal INCLUDE execution applies containment,
19
+ recursion checks, source reporting and runtime error handling.
20
+ """
21
+ if not required and not optional:
22
+ return script
23
+
24
+ from execsql.script.ast import IncludeDirective, Script, SourceSpan
25
+
26
+ seen = {str(Path(script.source).resolve())} if script.source != "<inline>" else set()
27
+ includes = []
28
+ for if_exists, filenames in ((False, required), (True, optional)):
29
+ for filename in filenames:
30
+ if filename in seen:
31
+ continue
32
+ seen.add(filename)
33
+ modifier = " IF EXISTS" if if_exists else ""
34
+ includes.append(
35
+ IncludeDirective(
36
+ span=SourceSpan("<config>", 1),
37
+ target=filename,
38
+ if_exists=if_exists,
39
+ command=f"INCLUDE{modifier} {filename}",
40
+ ),
41
+ )
42
+ return Script(source=script.source, body=[*includes, *script.body]) if includes else script
execsql/types.py CHANGED
@@ -514,11 +514,12 @@ class DT_Decimal(DataType):
514
514
  self.precision = digits
515
515
  self.scale = 0
516
516
  return
517
- if exponent < 0 and abs(exponent) > digits:
518
- self.precision = abs(exponent) + 1
517
+ if exponent > 0:
518
+ self.precision = digits + exponent
519
+ self.scale = 0
519
520
  else:
520
- self.precision = digits
521
- self.scale = abs(exponent)
521
+ self.precision = -exponent + 1 if exponent < 0 and -exponent > digits else digits
522
+ self.scale = -exponent
522
523
 
523
524
  def _from_data(self, data: object) -> object:
524
525
  if data is None:
@@ -730,7 +731,7 @@ class DbType:
730
731
  ) from e
731
732
  if max_len and max_len > 0 and dts[1]:
732
733
  spec = f"{self.quoted(column_name)} {dts[0]}({max_len})"
733
- elif data_type.precspec and precision and scale:
734
+ elif data_type.precspec and precision is not None and scale is not None:
734
735
  # numeric
735
736
  spec = f"{self.quoted(column_name)} {dts[0]}({precision},{scale})"
736
737
  else:
execsql/utils/errors.py CHANGED
@@ -23,7 +23,7 @@ from pathlib import Path
23
23
  from typing import Any
24
24
 
25
25
  import execsql.state as _state
26
- from execsql.exceptions import ErrInfo
26
+ from execsql.exceptions import ErrInfo, ScriptExit
27
27
 
28
28
  __all__ = [
29
29
  "error_origin",
@@ -278,7 +278,7 @@ def exit_now(exit_status: int, errinfo: ErrInfo | None, logmsg: str | None = Non
278
278
  if lost_output is not None:
279
279
  manifest.add_error(lost_output)
280
280
  manifest.finish(exit_status, errinfo)
281
- sys.exit(exit_status)
281
+ raise ScriptExit(exit_status)
282
282
 
283
283
 
284
284
  def fatal_error(error_msg: str | None = None) -> None: