execsql2 2.35.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 (71) hide show
  1. execsql/api.py +111 -53
  2. execsql/cli/commands/run.py +2 -2
  3. execsql/cli/lint.py +60 -7
  4. execsql/cli/run.py +10 -10
  5. execsql/config.py +7 -0
  6. execsql/data/execsql.conf.template +31 -7
  7. execsql/db/base.py +64 -26
  8. execsql/db/duckdb.py +51 -5
  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 +4 -3
  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 +28 -12
  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 +32 -93
  35. execsql/metacommands/io_import.py +26 -8
  36. execsql/metacommands/io_write.py +3 -1
  37. execsql/metacommands/reference.py +7 -0
  38. execsql/metacommands/script_ext.py +13 -10
  39. execsql/metacommands/system.py +13 -6
  40. execsql/parser.py +14 -37
  41. execsql/script/ast.py +21 -0
  42. execsql/script/control.py +73 -3
  43. execsql/script/executor.py +503 -227
  44. execsql/script/parser.py +102 -52
  45. execsql/script/preparation.py +42 -0
  46. execsql/state.py +23 -0
  47. execsql/types.py +6 -5
  48. execsql/utils/errors.py +24 -6
  49. execsql/utils/fileio.py +342 -134
  50. execsql/utils/sigterm.py +154 -0
  51. execsql/utils/spreadsheet.py +74 -0
  52. execsql/utils/strings.py +13 -7
  53. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/README.md +5 -5
  54. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/example_config_prompt.sql +2 -2
  55. {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/METADATA +32 -119
  56. {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/RECORD +71 -68
  57. {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/WHEEL +1 -1
  58. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
  59. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
  60. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_compare.sql +0 -0
  61. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
  62. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
  63. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
  64. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
  65. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/script_template.sql +0 -0
  66. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
  67. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
  68. {execsql2-2.35.0.data → execsql2-2.38.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
  69. {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/entry_points.txt +0 -0
  70. {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/licenses/LICENSE.txt +0 -0
  71. {execsql2-2.35.0.dist-info → execsql2-2.38.0.dist-info}/licenses/NOTICE +0 -0
execsql/script/parser.py CHANGED
@@ -25,7 +25,8 @@ Usage::
25
25
  from __future__ import annotations
26
26
 
27
27
  import re
28
- from collections.abc import Iterable
28
+ from collections.abc import Callable, Iterable
29
+ from dataclasses import replace
29
30
  from pathlib import Path
30
31
  from typing import cast
31
32
 
@@ -96,7 +97,7 @@ _INCLUDE_RX = re.compile(
96
97
  )
97
98
 
98
99
 
99
- def _strip_quotes(s: str) -> str:
100
+ def strip_quotes(s: str) -> str:
100
101
  """Strip a matching pair of surrounding quotes from *s*."""
101
102
  if len(s) >= 2 and s[0] in ('"', "'") and s[-1] == s[0]:
102
103
  return s[1:-1]
@@ -126,6 +127,94 @@ _EXEC_SCRIPT_RX = re.compile(
126
127
  re.I,
127
128
  )
128
129
 
130
+
131
+ def directive_node(command: str, span: SourceSpan) -> IncludeDirective | None:
132
+ """The :class:`IncludeDirective` for an INCLUDE or EXECUTE SCRIPT *command*, or ``None`` for any other.
133
+
134
+ *command* is the metacommand without ``-- !x!``.
135
+ """
136
+ m = _INCLUDE_RX.match(command)
137
+ if m:
138
+ return IncludeDirective(
139
+ span=span,
140
+ target=strip_quotes(m.group("target").strip()),
141
+ if_exists=m.group("exists") is not None,
142
+ command=command.strip(),
143
+ )
144
+ m = _EXEC_SCRIPT_RX.match(command)
145
+ if m:
146
+ return IncludeDirective(
147
+ span=span,
148
+ target=m.group("script_id"),
149
+ is_execute_script=True,
150
+ if_exists=m.group("exists") is not None,
151
+ arguments=m.group("argexp"),
152
+ loop_type=m.group("looptype").upper() if m.group("looptype") else None,
153
+ loop_condition=m.group("loopcond"),
154
+ command=command.strip(),
155
+ )
156
+ return None
157
+
158
+
159
+ def substitute_directive(node: IncludeDirective, substitute: Callable[[str], str]) -> IncludeDirective:
160
+ """*node* with *substitute* applied once to each part that is substituted when it runs.
161
+
162
+ Those parts are INCLUDE's file and EXECUTE SCRIPT's script name and
163
+ arguments. For a node parsed from a metacommand, ``command`` becomes
164
+ the metacommand with them substituted. The metacommand is not
165
+ parsed again, so a value with a line break in it stays one value (an
166
+ unquoted argument still ends at a comma or a closing parenthesis, as it
167
+ always has). A ``WHILE`` / ``UNTIL`` condition is left as written: it
168
+ is substituted on each pass of the loop.
169
+ """
170
+ m = (_EXEC_SCRIPT_RX if node.is_execute_script else _INCLUDE_RX).match(node.command)
171
+ if m is None:
172
+ # Built without the text of a metacommand.
173
+ target = substitute(node.target)
174
+ return replace(
175
+ node,
176
+ target=target if node.is_execute_script else strip_quotes(target.strip()),
177
+ arguments=substitute(node.arguments) if node.arguments is not None else None,
178
+ )
179
+ pieces: list[str] = []
180
+ values: dict[str, str] = {}
181
+ last = 0
182
+ for group in ("script_id", "argexp") if node.is_execute_script else ("target",):
183
+ if m.group(group) is None:
184
+ continue
185
+ start, end = m.span(group)
186
+ values[group] = substitute(node.command[start:end])
187
+ pieces += [node.command[last:start], values[group]]
188
+ last = end
189
+ pieces.append(node.command[last:])
190
+ command = "".join(pieces)
191
+ if node.is_execute_script:
192
+ return replace(node, target=values["script_id"], arguments=values.get("argexp"), command=command)
193
+ return replace(node, target=strip_quotes(values["target"].strip()), command=command)
194
+
195
+
196
+ def metacommand_node(command: str, span: SourceSpan) -> IfBlock | IncludeDirective | MetaCommandStatement:
197
+ """The node for a metacommand that opens or closes no block, however it is reached.
198
+
199
+ The same on a line of its own, inside an inline ``IF (…) { … }`` and
200
+ when added with ``EXTEND SCRIPT … WITH METACOMMAND``: an inline ``IF``
201
+ is an :class:`IfBlock`, INCLUDE and EXECUTE SCRIPT an
202
+ :class:`IncludeDirective`, and any other a :class:`MetaCommandStatement`
203
+ for the dispatch table. A metacommand whose text a substitution
204
+ variable supplies (``-- !x! !!cmd!!``) is classified here as written,
205
+ before substitution, so it always goes to the dispatch table (#113).
206
+ """
207
+ m = _IF_INLINE_RX.match(command)
208
+ if m:
209
+ return IfBlock(
210
+ span=span,
211
+ condition=m.group("cond").strip(),
212
+ body=[metacommand_node(m.group("cmd").strip(), span)],
213
+ command=command.strip(),
214
+ )
215
+ return directive_node(command, span) or MetaCommandStatement(span=span, command=command)
216
+
217
+
129
218
  # A parameter default value is either a double-quoted string, a single-quoted
130
219
  # string (both may contain spaces, commas, and other special characters), or
131
220
  # a bare run of non-whitespace characters. Unquoted values must not begin
@@ -197,7 +286,7 @@ def _parse_param_defs(
197
286
  )
198
287
  name, default = m.group(1), m.group(2)
199
288
  if default is not None:
200
- default = _strip_quotes(default)
289
+ default = strip_quotes(default)
201
290
  if seen_optional is None:
202
291
  seen_optional = name
203
292
  elif seen_optional is not None:
@@ -512,7 +601,7 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
512
601
  in_sql_block = True
513
602
  block_stack.append(
514
603
  _BlockFrame(
515
- SqlBlock(span=SourceSpan(source_name, file_lineno)),
604
+ SqlBlock(span=SourceSpan(source_name, file_lineno), command=cmd_text),
516
605
  kind="sqlblock",
517
606
  start_line=file_lineno,
518
607
  source=source_name,
@@ -576,6 +665,7 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
576
665
  span=SourceSpan(source_name, file_lineno),
577
666
  name=name,
578
667
  param_defs=param_defs,
668
+ command=cmd_text,
579
669
  ),
580
670
  kind="script",
581
671
  start_line=file_lineno,
@@ -630,6 +720,7 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
630
720
  IfBlock(
631
721
  span=SourceSpan(source_name, file_lineno),
632
722
  condition=m.group("cond").strip(),
723
+ command=cmd_text,
633
724
  ),
634
725
  kind="if",
635
726
  start_line=file_lineno,
@@ -639,18 +730,8 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
639
730
  continue
640
731
 
641
732
  # -- IF (inline form): IF (cond) { cmd } --
642
- m = _IF_INLINE_RX.match(cmd_text)
643
- if m:
644
- inner = MetaCommandStatement(
645
- span=SourceSpan(source_name, file_lineno),
646
- command=m.group("cmd").strip(),
647
- )
648
- if_node = IfBlock(
649
- span=SourceSpan(source_name, file_lineno),
650
- condition=m.group("cond").strip(),
651
- body=[inner],
652
- )
653
- _current_body().append(if_node)
733
+ if _IF_INLINE_RX.match(cmd_text):
734
+ _current_body().append(metacommand_node(cmd_text, SourceSpan(source_name, file_lineno)))
654
735
  continue
655
736
 
656
737
  # -- ELSEIF --
@@ -673,6 +754,7 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
673
754
  ElseIfClause(
674
755
  condition=m.group("cond").strip(),
675
756
  span=SourceSpan(source_name, file_lineno),
757
+ command=cmd_text,
676
758
  ),
677
759
  )
678
760
  continue
@@ -767,6 +849,7 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
767
849
  span=SourceSpan(source_name, file_lineno),
768
850
  loop_type=m.group("looptype").upper(),
769
851
  condition=m.group("loopcond").strip(),
852
+ command=cmd_text,
770
853
  ),
771
854
  kind="loop",
772
855
  start_line=file_lineno,
@@ -793,7 +876,7 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
793
876
  if m:
794
877
  block_stack.append(
795
878
  _BlockFrame(
796
- BatchBlock(span=SourceSpan(source_name, file_lineno)),
879
+ BatchBlock(span=SourceSpan(source_name, file_lineno), command=cmd_text),
797
880
  kind="batch",
798
881
  start_line=file_lineno,
799
882
  source=source_name,
@@ -814,41 +897,8 @@ def _parse_lines(lines: Iterable[str], source_name: str, errors: list[ErrInfo] |
814
897
  _close_block(closed, file_lineno)
815
898
  continue
816
899
 
817
- # -- INCLUDE --
818
- m = _INCLUDE_RX.match(cmd_text)
819
- if m:
820
- _current_body().append(
821
- IncludeDirective(
822
- span=SourceSpan(source_name, file_lineno),
823
- target=_strip_quotes(m.group("target").strip()),
824
- if_exists=m.group("exists") is not None,
825
- ),
826
- )
827
- continue
828
-
829
- # -- EXECUTE SCRIPT / RUN SCRIPT --
830
- m = _EXEC_SCRIPT_RX.match(cmd_text)
831
- if m:
832
- _current_body().append(
833
- IncludeDirective(
834
- span=SourceSpan(source_name, file_lineno),
835
- target=m.group("script_id"),
836
- is_execute_script=True,
837
- if_exists=m.group("exists") is not None,
838
- arguments=m.group("argexp"),
839
- loop_type=m.group("looptype").upper() if m.group("looptype") else None,
840
- loop_condition=m.group("loopcond"),
841
- ),
842
- )
843
- continue
844
-
845
- # -- All other metacommands (flat) --
846
- _current_body().append(
847
- MetaCommandStatement(
848
- span=SourceSpan(source_name, file_lineno),
849
- command=cmd_text,
850
- ),
851
- )
900
+ # -- INCLUDE, EXECUTE SCRIPT and all other metacommands (flat) --
901
+ _current_body().append(metacommand_node(cmd_text, SourceSpan(source_name, file_lineno)))
852
902
  continue
853
903
 
854
904
  # --- SQL line ---
@@ -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/state.py CHANGED
@@ -467,6 +467,29 @@ class _StateModule(types.ModuleType):
467
467
  return sorted(set(super().__dir__()) | _CONTEXT_ATTRS)
468
468
 
469
469
 
470
+ def _context_property(name: str) -> property:
471
+ """A property on :class:`_StateModule` that reads *name* from the active context.
472
+
473
+ Without it every ``_state.conf`` read failed the module's own lookup and
474
+ reached :meth:`_StateModule.__getattr__` through an ``AttributeError``.
475
+ Building that error is cheap in a script, but on Python 3.13 it checks
476
+ whether the module shadows a standard-library one, which takes ~12 µs
477
+ when ``sys.path[0]`` is ``''`` (``python -c``, stdin, the REPL,
478
+ notebooks). Writes and deletes still go through ``__setattr__`` and
479
+ ``__delattr__``.
480
+ """
481
+
482
+ def get(self: _StateModule) -> Any:
483
+ return getattr(_get_ctx(), name)
484
+
485
+ return property(get)
486
+
487
+
488
+ for _name in _CONTEXT_ATTRS:
489
+ setattr(_StateModule, _name, _context_property(_name))
490
+ del _name
491
+
492
+
470
493
  # ---------------------------------------------------------------------------
471
494
  # Utility functions
472
495
  # ---------------------------------------------------------------------------
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",
@@ -121,7 +121,8 @@ def stamp_errinfo(errinfo: ErrInfo) -> ErrInfo:
121
121
  errinfo.script_file = lc.source
122
122
  errinfo.script_line_no = lc.line_no
123
123
  if errinfo.cmd is None:
124
- errinfo.cmd = lc.command.commandline() if hasattr(lc.command, "commandline") else None
124
+ # The metacommand as written, without "-- !x!", as upstream reported it.
125
+ errinfo.cmd = getattr(lc.command, "statement", None)
125
126
  errinfo.cmdtype = lc.command_type
126
127
  return errinfo
127
128
 
@@ -129,10 +130,14 @@ def stamp_errinfo(errinfo: ErrInfo) -> ErrInfo:
129
130
  def _run_deferred_script(spec: Any) -> None:
130
131
  """Execute a deferred ScriptExecSpec (ON ERROR_HALT / ON CANCEL_HALT EXECUTE SCRIPT).
131
132
 
132
- Executes the target script natively through the AST executor.
133
+ Executes the target script natively through the AST executor. Its
134
+ arguments are substituted now, when the halt fires, so a deferred
135
+ variable (``!{$ERROR_MESSAGE}!``) holds the error that fired it.
133
136
  """
134
137
  from execsql.script.ast import IncludeDirective, SourceSpan
135
- from execsql.script.executor import _execute_script_native
138
+ from execsql.script.engine import substitute_vars
139
+ from execsql.script.executor import _execute_script_native, _script_args, _stack_localvars
140
+ from execsql.script.parser import substitute_directive
136
141
  from execsql.state import get_context
137
142
 
138
143
  script_id = spec.script_id.lower() if hasattr(spec, "script_id") else None
@@ -145,8 +150,16 @@ def _run_deferred_script(spec: Any) -> None:
145
150
  arguments=spec.arg_exp,
146
151
  loop_type=spec.looptype,
147
152
  loop_condition=spec.loopcond,
153
+ command=(
154
+ f"EXECUTE SCRIPT {spec.script_id}"
155
+ + (f" WITH ARGUMENTS ({spec.arg_exp})" if spec.arg_exp else "")
156
+ + (f" {spec.looptype} ({spec.loopcond})" if spec.looptype else "")
157
+ ),
148
158
  )
149
- _execute_script_native(ctx, node, ctx.ast_scripts[script_id])
159
+ localvars = _stack_localvars(ctx)
160
+ node = substitute_directive(node, lambda text: substitute_vars(text, localvars, ctx=ctx))
161
+ script_block = ctx.ast_scripts[script_id]
162
+ _execute_script_native(ctx, node, script_block, _script_args(node, script_block))
150
163
  else:
151
164
  raise ErrInfo(
152
165
  type="error",
@@ -199,6 +212,11 @@ def exit_now(exit_status: int, errinfo: ErrInfo | None, logmsg: str | None = Non
199
212
  except Exception:
200
213
  if _state.exec_log is not None:
201
214
  _state.exec_log.log_status_error("Failed to write the ON CANCEL_HALT WRITE message.")
215
+ # A halt inside BEGIN BATCH rolls the batch back: after the error is
216
+ # shown, in case a lost connection makes the rollback wait, and before
217
+ # the ON ERROR_HALT / ON CANCEL_HALT script, whose own SQL then commits.
218
+ if _state.status is not None:
219
+ _state.status.batch.abandon_all()
202
220
  # Defer import to avoid circular dependencies
203
221
  from execsql.utils.gui import gui_console_isrunning, gui_console_wait_user, gui_console_off
204
222
 
@@ -260,7 +278,7 @@ def exit_now(exit_status: int, errinfo: ErrInfo | None, logmsg: str | None = Non
260
278
  if lost_output is not None:
261
279
  manifest.add_error(lost_output)
262
280
  manifest.finish(exit_status, errinfo)
263
- sys.exit(exit_status)
281
+ raise ScriptExit(exit_status)
264
282
 
265
283
 
266
284
  def fatal_error(error_msg: str | None = None) -> None: