execsql2 2.38.0__py3-none-any.whl → 2.40.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 (49) hide show
  1. execsql/cli/lint.py +40 -8
  2. execsql/cli/run.py +123 -56
  3. execsql/data/execsql.conf.template +5 -2
  4. execsql/db/base.py +9 -0
  5. execsql/db/dsn.py +2 -3
  6. execsql/db/firebird.py +31 -22
  7. execsql/db/mysql.py +14 -20
  8. execsql/db/oracle.py +6 -4
  9. execsql/db/postgres.py +13 -18
  10. execsql/exceptions.py +5 -0
  11. execsql/gui/__init__.py +13 -10
  12. execsql/gui/base.py +131 -8
  13. execsql/gui/console.py +77 -30
  14. execsql/gui/desktop.py +290 -120
  15. execsql/gui/tui.py +384 -132
  16. execsql/importers/base.py +6 -12
  17. execsql/importers/csv.py +3 -5
  18. execsql/metacommands/control.py +40 -27
  19. execsql/metacommands/data.py +11 -9
  20. execsql/metacommands/io_fileops.py +1 -1
  21. execsql/metacommands/io_write.py +0 -9
  22. execsql/metacommands/prompt.py +94 -42
  23. execsql/metacommands/script_ext.py +6 -2
  24. execsql/metacommands/system.py +0 -2
  25. execsql/parser.py +118 -8
  26. execsql/script/engine.py +1 -1
  27. execsql/script/executor.py +31 -3
  28. execsql/script/parser.py +31 -0
  29. execsql/utils/auth.py +11 -6
  30. execsql/utils/gui.py +162 -53
  31. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/METADATA +2 -2
  32. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/RECORD +49 -49
  33. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/README.md +0 -0
  34. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
  35. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/example_config_prompt.sql +0 -0
  36. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
  37. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/md_compare.sql +0 -0
  38. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
  39. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
  40. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
  41. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
  42. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/script_template.sql +0 -0
  43. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
  44. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
  45. {execsql2-2.38.0.data → execsql2-2.40.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
  46. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/WHEEL +0 -0
  47. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/entry_points.txt +0 -0
  48. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/licenses/LICENSE.txt +0 -0
  49. {execsql2-2.38.0.dist-info → execsql2-2.40.0.dist-info}/licenses/NOTICE +0 -0
execsql/parser.py CHANGED
@@ -23,6 +23,8 @@ Classes:
23
23
  """
24
24
 
25
25
  import re
26
+ from collections.abc import Callable
27
+ from difflib import get_close_matches
26
28
  from typing import Any, cast
27
29
 
28
30
  from execsql.exceptions import CondParserError, NumericParserError
@@ -192,23 +194,131 @@ class CondParser(CondTokens):
192
194
  """Recursive-descent parser for boolean conditional expressions."""
193
195
 
194
196
  # Takes a conditional expression string.
195
- def __init__(self, condexpr: str, conditionallist: Any = None) -> None:
197
+ def __init__(
198
+ self,
199
+ condexpr: str,
200
+ conditionallist: Any = None,
201
+ *,
202
+ diagnostic_source: Callable[[int], tuple[str, int]] | None = None,
203
+ ) -> None:
196
204
  """Initialise the parser with the conditional expression string.
197
205
 
198
206
  *conditionallist* is the table of conditional tests to match against;
199
207
  ``None`` means the current run's ``conditionallist``. Passing one lets
200
208
  a caller with no run in progress, such as ``execsql lint``, parse
201
209
  (without evaluating) a condition.
210
+
211
+ *diagnostic_source* maps a parsed position to the original text and
212
+ position, so lint can parse placeholder values but display variables.
202
213
  """
203
214
  self.condexpr = condexpr
204
215
  self.cond_expr = SourceString(condexpr)
205
216
  self.conditionallist = conditionallist
217
+ self._diagnostic_source = diagnostic_source or (lambda position: (condexpr, position))
218
+ self._operand_start = 0
219
+ self._not_end = -1
220
+
221
+ def _test_syntax(self, name: str) -> str | None:
222
+ """Find a rejected test's documented syntax, including registered aliases."""
223
+ from execsql.metacommands import reference
224
+
225
+ entry = reference.condition(name)
226
+ if entry is not None:
227
+ return " or ".join(entry.forms)
228
+ table = self.conditionallist
229
+ if table is None:
230
+ import execsql.state as _state
231
+
232
+ table = _state.conditionallist
233
+ # Match only the name part of registered call patterns. This recognizes
234
+ # aliases such as EQUAL(S)? and HAS_ROWS without another keyword list.
235
+ for command in table or ():
236
+ prefix, opening, _ = command.pattern.partition(r"\(")
237
+ try:
238
+ named = bool(opening and re.fullmatch(prefix, name, re.I))
239
+ except re.error:
240
+ # A registration can wrap its whole pattern in a group; its
241
+ # prefix alone is then not a complete regular expression.
242
+ named = False
243
+ if named or name == (command.description or "").upper():
244
+ entry = reference.condition(command.description or "")
245
+ if entry is None:
246
+ # Aliases and alternate quoting forms share a handler;
247
+ # only the canonical registration carries its description.
248
+ for other in table:
249
+ if other.exec_fn is command.exec_fn:
250
+ entry = reference.condition(other.description or "")
251
+ if entry is not None:
252
+ break
253
+ return " or ".join(entry.forms) if entry else f"{name}(...)"
254
+ return None
255
+
256
+ def _syntax_error(self, *, trailing: bool = False) -> str:
257
+ """Explain rejected syntax without changing what the parser accepts."""
258
+ source = self.cond_expr.str
259
+ position = self.cond_expr.currpos
260
+ # A literal such as NO or a NOT operator can consume the beginning of
261
+ # an unknown name (NO_SUCH_FN). Report the complete identifier.
262
+ suffix = re.match(r"([A-Za-z_]\w*)", source[position:])
263
+ known_suffix = (
264
+ position == self._not_end and suffix is not None and self._test_syntax(suffix[1].upper()) is not None
265
+ )
266
+ if not known_suffix and position < len(source) and (source[position].isalnum() or source[position] == "_"):
267
+ while position > self._operand_start and (source[position - 1].isalnum() or source[position - 1] == "_"):
268
+ position -= 1
269
+ _, cursor = self._diagnostic_source(self.cond_expr.currpos)
270
+ source, position = self._diagnostic_source(position)
271
+ fragment = source[position:].strip()
272
+ if not fragment:
273
+ return f"Expected a condition test at the end of condition: {source.strip()}."
274
+ if fragment.startswith(")"):
275
+ return f"Unexpected ')' in condition: {source.strip()}."
276
+ if trailing and position == cursor and not fragment.startswith(("=", "<", ">", "!")):
277
+ return f"Expected AND or OR before {fragment!r} in condition: {source.strip()}."
278
+ match = re.match(r"(?P<name>[A-Za-z_]\w*)\s*(?P<opening>\()?", fragment)
279
+ if match is None or (not match["opening"] and re.match(r"[=<>!]+", fragment[match.end() :].lstrip())):
280
+ return f"Not a condition test: {source.strip()}; compare values with EQUALS(a, b) or IS_TRUE(x)."
281
+ name = match["name"].upper()
282
+ syntax = self._test_syntax(name)
283
+ if syntax is None:
284
+ from execsql.metacommands import reference
285
+
286
+ choices = sorted(c.name for c in reference.conditions() if "(" in c.forms[0])
287
+ near = get_close_matches(name, choices, n=1, cutoff=0.75)
288
+ hint = f" (did you mean {near[0]}?)" if near else ""
289
+ return f"Unknown condition test: {name}{hint}: {fragment}."
290
+ if match["opening"]:
291
+ depth = 0
292
+ commas = 0
293
+ quote = ""
294
+ for char in fragment[match.start("opening") :]:
295
+ if quote:
296
+ if char == quote:
297
+ quote = ""
298
+ elif char in "\"'`":
299
+ quote = char
300
+ elif char == "(":
301
+ depth += 1
302
+ elif char == ")":
303
+ depth -= 1
304
+ if depth == 0:
305
+ break
306
+ elif char == "," and depth == 1:
307
+ commas += 1
308
+ if quote:
309
+ return f"Missing closing quote {quote!r} in condition: {fragment}."
310
+ if depth > 0:
311
+ return f"Missing ')' in condition: {fragment}."
312
+ if "," in syntax and not commas:
313
+ return f"Separate the arguments of {name} with a comma: {fragment}. Expected: {syntax}."
314
+ return f"Invalid syntax for {name}: {fragment}. Expected: {syntax}."
206
315
 
207
316
  def match_not(self) -> int | None:
208
317
  """Match a NOT operator and return its token type, or None."""
209
318
  # Try to match 'NOT' operator. If not found, return None
210
319
  m1 = self.cond_expr.match_str("NOT")
211
320
  if m1 is not None:
321
+ self._not_end = self.cond_expr.currpos
212
322
  return self.NOT
213
323
  return None
214
324
 
@@ -217,6 +327,7 @@ class CondParser(CondTokens):
217
327
  # Try to match 'AND' operator. If not found, return None
218
328
  m1 = self.cond_expr.match_str("AND")
219
329
  if m1 is not None:
330
+ self._operand_start = self.cond_expr.currpos
220
331
  return self.AND
221
332
  return None
222
333
 
@@ -225,6 +336,7 @@ class CondParser(CondTokens):
225
336
  # Try to match 'OR' operator. If not found, return None
226
337
  m1 = self.cond_expr.match_str("OR")
227
338
  if m1 is not None:
339
+ self._operand_start = self.cond_expr.currpos
228
340
  return self.OR
229
341
  return None
230
342
 
@@ -249,14 +361,14 @@ class CondParser(CondTokens):
249
361
  m1 = self.expression()
250
362
  rp = self.cond_expr.match_str(")")
251
363
  if rp is None:
364
+ if not self.cond_expr.eoi():
365
+ raise CondParserError(self._syntax_error(trailing=True))
252
366
  raise CondParserError(
253
- f"Expected closing parenthesis at position {self.cond_expr.currpos} of {self.cond_expr.str}.",
367
+ f"Missing ')' in condition: {self._diagnostic_source(0)[0].strip()}.",
254
368
  )
255
369
  return m1
256
370
  else:
257
- raise CondParserError(
258
- f"Can't parse a factor at position {self.cond_expr.currpos} of {self.cond_expr.str}.",
259
- )
371
+ raise CondParserError(self._syntax_error())
260
372
 
261
373
  def term(self) -> Any:
262
374
  """Parse a term: a factor optionally followed by AND and another term."""
@@ -282,9 +394,7 @@ class CondParser(CondTokens):
282
394
  """Parse the entire conditional expression and return the AST root."""
283
395
  exp = self.expression()
284
396
  if not self.cond_expr.eoi():
285
- raise CondParserError(
286
- f"Conditional expression parser did not consume entire string; remainder = {self.cond_expr.remainder()}.",
287
- )
397
+ raise CondParserError(self._syntax_error(trailing=True))
288
398
  return exp
289
399
 
290
400
 
execsql/script/engine.py CHANGED
@@ -128,7 +128,7 @@ class MetaCommand:
128
128
  if er:
129
129
  # The caller records the error ($LAST_ERROR, $ERROR_MESSAGE,
130
130
  # the log, the error history) and halts or carries on.
131
- if self.set_error_flag or _state.status.halt_on_metacommand_err:
131
+ if self.set_error_flag or _state.status.halt_on_metacommand_err or er.always_halt:
132
132
  raise er
133
133
  else:
134
134
  if self.set_error_flag:
@@ -451,11 +451,19 @@ def _metacommand_failed(
451
451
  if ctx.exec_log is not None:
452
452
  ctx.exec_log.log_status_info(f"Metacommand error: {logged_error(e)}")
453
453
  _fail_open_batches(ctx, source, line_no)
454
- if status.halt_on_metacommand_err or always_halt:
454
+ if status.halt_on_metacommand_err or always_halt or e.always_halt:
455
455
  exit_now(1, e)
456
456
  status.error_history.append((source, line_no, cmd, e.errmsg()))
457
457
 
458
458
 
459
+ class _ScriptExecutionError(BaseException):
460
+ """Carry a called script's error past metacommand handlers to its AST caller."""
461
+
462
+ def __init__(self, error: Exception) -> None:
463
+ super().__init__(str(error))
464
+ self.error = error
465
+
466
+
459
467
  def _exec_metacommand(
460
468
  ctx: RuntimeContext,
461
469
  cmd: str,
@@ -488,6 +496,16 @@ def _exec_metacommand(
488
496
  if (manifest := _manifest.current()) is not None:
489
497
  manifest.record_metacommand(cmd, source, line_no, metacommandlist)
490
498
  return result
499
+ # Variable-supplied boundaries without a dispatch entry (END LOOP,
500
+ # SCRIPT and SQL) follow the same error policy as IF and BEGIN BATCH.
501
+ from execsql.metacommands.control import unsupported_block_error
502
+ from execsql.script.parser import block_metacommand_name
503
+
504
+ if name := block_metacommand_name(cmd):
505
+ raise unsupported_block_error(name)
506
+ except _ScriptExecutionError as failure:
507
+ # The called script already owns its error policy; do not apply the prompt's again.
508
+ raise failure.error from failure.error.__cause__
491
509
  except Exception as exc:
492
510
  e = _as_errinfo(exc)
493
511
  except _CONTROL_FLOW:
@@ -637,7 +655,7 @@ def _execute_node(
637
655
 
638
656
  elif isinstance(node, IncludeDirective):
639
657
  ctx.last_command = ExecutingStatement(node)
640
- _execute_include(ctx, node, localvars)
658
+ _execute_include(ctx, node, localvars, in_loop=in_loop)
641
659
 
642
660
  else:
643
661
  raise ErrInfo(
@@ -878,6 +896,8 @@ def _execute_include(
878
896
  ctx: RuntimeContext,
879
897
  node: IncludeDirective,
880
898
  localvars: SubVarSet | None = None,
899
+ *,
900
+ in_loop: bool = False,
881
901
  ) -> None:
882
902
  """Run an INCLUDE or EXECUTE SCRIPT written in the script.
883
903
 
@@ -885,9 +905,17 @@ def _execute_include(
885
905
  as in any other metacommand, so ``!!$COUNTER_1!!`` counts once and
886
906
  ``$LAST_ERROR`` holds the text that ran. An EXECUTE SCRIPT ``WHILE`` /
887
907
  ``UNTIL`` condition is substituted on each pass instead.
908
+ In a LOOP body, INCLUDE filenames resolve deferred variables before
909
+ that substitution; EXECUTE SCRIPT arguments keep their own deferred semantics.
888
910
  """
889
911
  effective_locals = _stack_localvars(ctx) or localvars
890
- directive = substitute_directive(node, lambda text: substitute_vars(text, effective_locals, ctx=ctx))
912
+
913
+ def substitute(text: str) -> str:
914
+ if in_loop and not node.is_execute_script:
915
+ text = _convert_deferred_vars(text)
916
+ return substitute_vars(text, effective_locals, ctx=ctx)
917
+
918
+ directive = substitute_directive(node, substitute)
891
919
  _run_directive(ctx, directive, localvars)
892
920
 
893
921
 
execsql/script/parser.py CHANGED
@@ -193,6 +193,37 @@ def substitute_directive(node: IncludeDirective, substitute: Callable[[str], str
193
193
  return replace(node, target=strip_quotes(values["target"].strip()), command=command)
194
194
 
195
195
 
196
+ def block_metacommand_name(command: str) -> str | None:
197
+ """Name a block boundary that needs parsing with the surrounding source.
198
+
199
+ Reuse the source parser's patterns, including boundaries nested in an
200
+ inline IF. This only detects unsupported dynamic boundaries; it never
201
+ reclassifies variable text into executable control flow.
202
+ """
203
+ inline = _IF_INLINE_RX.match(command)
204
+ if inline:
205
+ return block_metacommand_name(inline.group("cmd").strip())
206
+ for pattern, name in (
207
+ (_IF_BLOCK_RX, "IF"),
208
+ (_ELSEIF_RX, "ELSEIF"),
209
+ (_ANDIF_RX, "ANDIF"),
210
+ (_ORIF_RX, "ORIF"),
211
+ (_ELSE_RX, "ELSE"),
212
+ (_ENDIF_RX, "ENDIF"),
213
+ (_LOOP_RX, "LOOP"),
214
+ (_ENDLOOP_RX, "END LOOP"),
215
+ (_BEGIN_BATCH_RX, "BEGIN BATCH"),
216
+ (_END_BATCH_RX, "END BATCH"),
217
+ (_BEGIN_SCRIPT_RX, "BEGIN SCRIPT"),
218
+ (_END_SCRIPT_RX, "END SCRIPT"),
219
+ (_BEGIN_SQL_RX, "BEGIN SQL"),
220
+ (_END_SQL_RX, "END SQL"),
221
+ ):
222
+ if pattern.match(command):
223
+ return name
224
+ return None
225
+
226
+
196
227
  def metacommand_node(command: str, span: SourceSpan) -> IfBlock | IncludeDirective | MetaCommandStatement:
197
228
  """The node for a metacommand that opens or closes no block, however it is reached.
198
229
 
execsql/utils/auth.py CHANGED
@@ -36,6 +36,7 @@ from typing import Any, cast
36
36
 
37
37
  import execsql.state as _state
38
38
  from execsql.utils.fileio import register_secret
39
+ from execsql.utils.gui import ConsoleUIError
39
40
 
40
41
  __all__ = [
41
42
  "begin_login",
@@ -343,21 +344,23 @@ def get_password(
343
344
  gui_manager_queue = getattr(_state, "gui_manager_queue", None)
344
345
  if gui_manager_thread:
345
346
  return_queue: queue.Queue[Any] = queue.Queue()
346
- from execsql.utils.gui import GuiSpec, QUERY_CONSOLE
347
+ from execsql.utils.gui import GuiSpec, QUERY_CONSOLE, get_gui_response
347
348
 
348
349
  assert gui_manager_queue is not None
349
350
  gui_manager_queue.put(GuiSpec(QUERY_CONSOLE, {}, return_queue))
350
- user_response = return_queue.get(block=True)
351
+ user_response = get_gui_response(return_queue, QUERY_CONSOLE)
351
352
  use_gui = user_response["console_running"]
353
+ except ConsoleUIError:
354
+ raise
352
355
  except Exception:
353
- pass # GUI query failed; fall back to non-GUI password prompt.
356
+ pass # GUI query unavailable; fall back to non-GUI password prompt.
354
357
 
355
358
  conf = _state.conf
356
359
  if use_gui or (conf is not None and conf.gui_level > 0):
357
360
  import queue as _queue
358
361
 
359
362
  try:
360
- from execsql.utils.gui import enable_gui, GuiSpec, GUI_DISPLAY
363
+ from execsql.utils.gui import enable_gui, GuiSpec, GUI_DISPLAY, get_gui_response
361
364
 
362
365
  enable_gui()
363
366
  return_queue = _queue.Queue()
@@ -371,9 +374,9 @@ def get_password(
371
374
  gui_manager_queue = getattr(_state, "gui_manager_queue", None)
372
375
  assert gui_manager_queue is not None
373
376
  gui_manager_queue.put(GuiSpec(GUI_DISPLAY, gui_args, return_queue))
374
- user_response = return_queue.get(block=True)
377
+ user_response = get_gui_response(return_queue, GUI_DISPLAY)
375
378
  btn = user_response["button"]
376
- passwd = cast(str, user_response["return_value"])
379
+ passwd = cast(str, user_response["return_value"] or "")
377
380
  if not btn and _state.status and _state.status.cancel_halt:
378
381
  if _state.exec_log:
379
382
  _state.exec_log.log_exit_halt(
@@ -382,6 +385,8 @@ def get_password(
382
385
  f"Canceled on password prompt for {dbms_name} database {database_name}, user {user_name}",
383
386
  )
384
387
  exit_now(2, None)
388
+ except ConsoleUIError:
389
+ raise
385
390
  except Exception:
386
391
  prompt_text = prompt.replace("\n", " ", 1).replace("\n", ", ") + " >"
387
392
  passwd = getpass.getpass(str(prompt_text))