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
@@ -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,
@@ -63,11 +63,14 @@ from execsql.script.ast import (
63
63
  Script,
64
64
  ScriptBlock,
65
65
  SqlBlock,
66
+ SourceSpan,
66
67
  SqlStatement,
67
68
  )
68
69
  from execsql import manifest as _manifest
70
+ from execsql.script.control import _roll_back
71
+ from execsql.script.parser import substitute_directive
69
72
  from execsql.script.engine import set_dynamic_system_vars, set_static_system_vars, substitute_vars
70
- from execsql.script.variables import SubVarSet
73
+ from execsql.script.variables import ScriptArgSubVarSet, SubVarSet
71
74
  from execsql.state import ExecFrame, RuntimeContext, active_context, get_context, xcmd_test
72
75
  from execsql.utils.errors import exception_desc, exit_now, logged_error, stamp_errinfo
73
76
 
@@ -177,39 +180,64 @@ def _convert_deferred_vars(text: str) -> str:
177
180
  return _DEFER_RX.sub(r"!!\1!!", text)
178
181
 
179
182
 
180
- def _eval_condition(
183
+ def _condition_holds(
181
184
  ctx: RuntimeContext,
185
+ command: str,
186
+ span: SourceSpan,
182
187
  condition: str,
183
188
  modifiers: list[ConditionModifier] | None = None,
184
189
  ) -> bool:
185
- """Evaluate a condition string with optional ANDIF/ORIF modifiers.
190
+ """Evaluate the condition of IF, ELSEIF, LOOP or EXECUTE SCRIPT … WHILE / UNTIL, with its ANDIF / ORIF modifiers.
186
191
 
187
192
  Short-circuits ANDIF (stops on first False) and ORIF (stops on first True)
188
193
  so that patterns like ``IF (sub_defined(x)) ANDIF (not sub_empty(x))``
189
194
  don't evaluate ``sub_empty`` when ``x`` is undefined.
195
+
196
+ *command* is the metacommand as written, on the line *span* starts; an
197
+ error is reported there, or on the ANDIF / ORIF that raised it.
190
198
  """
191
199
  effective_locals = _stack_localvars(ctx)
192
- expanded = substitute_vars(condition, effective_locals, ctx=ctx)
193
- result = xcmd_test(expanded)
194
-
195
- if modifiers:
196
- for mod in modifiers:
197
- # Short-circuit: AND with False can't become True,
198
- # OR with True can't become False.
199
- if mod.kind == "AND" and not result:
200
- continue
201
- if mod.kind == "OR" and result:
202
- continue
203
- mod_expanded = substitute_vars(mod.condition, effective_locals, ctx=ctx)
204
- mod_result = xcmd_test(mod_expanded)
205
- if mod.kind == "AND":
206
- result = result and mod_result
207
- else: # OR
208
- result = result or mod_result
209
-
200
+ result = _test(ctx, command, span, condition, effective_locals)
201
+ for mod in modifiers or []:
202
+ # Short-circuit: AND with False can't become True,
203
+ # OR with True can't become False.
204
+ if (mod.kind == "AND" and not result) or (mod.kind == "OR" and result):
205
+ continue
206
+ keyword = "ANDIF" if mod.kind == "AND" else "ORIF"
207
+ result = _test(ctx, f"{keyword} ({mod.condition})", mod.span, mod.condition, effective_locals)
210
208
  return result
211
209
 
212
210
 
211
+ def _test(
212
+ ctx: RuntimeContext,
213
+ command: str,
214
+ span: SourceSpan,
215
+ condition: str,
216
+ localvars: SubVarSet | None,
217
+ ) -> bool:
218
+ """One condition of *command*: its value.
219
+
220
+ A condition that cannot be evaluated stops the run. Its error is raised
221
+ with the line and text of *command*, which may be an ELSEIF, ANDIF or
222
+ ORIF rather than the statement on record, or a loop condition evaluated
223
+ after its body.
224
+ """
225
+ try:
226
+ return xcmd_test(substitute_vars(condition, localvars, ctx=ctx))
227
+ except Exception as exc:
228
+ e = _as_errinfo(exc)
229
+ if e.script_file is None:
230
+ e.script_file, e.script_line_no = span.file, span.start_line
231
+ e.cmd, e.cmdtype = command, "cmd"
232
+ # The run stops, except at a BREAKPOINT prompt, which carries on:
233
+ # a test that failed in the database (HASROWS) has rolled back the
234
+ # connection, so the batch must not commit.
235
+ _fail_open_batches(ctx, span.file, span.start_line)
236
+ if e is exc:
237
+ raise
238
+ raise e from exc
239
+
240
+
213
241
  def _set_command_vars(ctx: RuntimeContext, source: str, line_no: int) -> None:
214
242
  """Set per-command system variables (current script, line, time)."""
215
243
  assert ctx.subvars is not None
@@ -275,34 +303,159 @@ def _exec_sql(
275
303
  db.execute(cmd)
276
304
  if commit:
277
305
  db.commit()
278
- except ErrInfo as errinfo:
279
- e = errinfo
280
- except SystemExit:
281
- raise
282
306
  except Exception as exc:
283
- e = ErrInfo(type="exception", exception_msg=exception_desc())
284
- e.__cause__ = exc # the driver's own message, which a prompt shows
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
285
319
  if e:
286
- stamp_errinfo(e)
287
- subvars.add_substitution("$LAST_ERROR", cmd)
288
- subvars.add_substitution("$ERROR_MESSAGE", e.errmsg())
289
- status.sql_error = True
290
- if ctx.exec_log is not None:
291
- ctx.exec_log.log_status_info(f"SQL error: {logged_error(e)}")
292
- if status.halt_on_err:
293
- exit_now(1, e)
294
- status.error_history.append((source, line_no, cmd, e.errmsg()))
320
+ _sql_failed(ctx, e, cmd, source, line_no)
295
321
  return
296
322
  subvars.add_substitution("$LAST_SQL", cmd)
297
323
  if ctx.prompt_input is not None:
298
324
  ctx.prompt_input(result)
299
325
 
300
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
+
376
+ def _sql_failed(ctx: RuntimeContext, e: ErrInfo, cmd: str, source: str, line_no: int) -> None:
377
+ """Report an SQL error: halt under ERROR_HALT ON, else record it and carry on.
378
+
379
+ Sets ``$LAST_ERROR`` to *cmd*, ``$ERROR_MESSAGE`` and ``SQL_ERROR()``.
380
+ """
381
+ assert ctx.status is not None
382
+ assert ctx.subvars is not None
383
+ stamp_errinfo(e)
384
+ ctx.subvars.add_substitution("$LAST_ERROR", cmd)
385
+ ctx.subvars.add_substitution("$ERROR_MESSAGE", e.errmsg())
386
+ ctx.status.sql_error = True
387
+ if ctx.exec_log is not None:
388
+ ctx.exec_log.log_status_info(f"SQL error: {logged_error(e)}")
389
+ _fail_open_batches(ctx, source, line_no)
390
+ if ctx.status.halt_on_err:
391
+ exit_now(1, e)
392
+ ctx.status.error_history.append((source, line_no, cmd, e.errmsg()))
393
+
394
+
301
395
  # ---------------------------------------------------------------------------
302
396
  # Metacommand execution
303
397
  # ---------------------------------------------------------------------------
304
398
 
305
399
 
400
+ def _as_errinfo(exc: Exception) -> ErrInfo:
401
+ """*exc* as an :class:`ErrInfo`: itself if it is one, else an exception report.
402
+
403
+ The report keeps *exc* as its ``__cause__``: the driver's or parser's own
404
+ message, which a prompt shows. Call it in the ``except`` clause, where
405
+ :func:`exception_desc` can still see the exception.
406
+ """
407
+ if isinstance(exc, ErrInfo):
408
+ return exc
409
+ e = ErrInfo(type="exception", exception_msg=exception_desc())
410
+ e.__cause__ = exc
411
+ return e
412
+
413
+
414
+ def _fail_open_batches(ctx: RuntimeContext, source: str, line_no: int | None = None, *, what: str = "an error") -> None:
415
+ """Keep every open batch from committing after an error inside it.
416
+
417
+ Whatever failed (a statement, a metacommand, or SQL a metacommand ran),
418
+ the batch is not committed: a database error may already have rolled
419
+ back the batch's earlier work, and execsql cannot tell from here whether
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.
422
+ """
423
+ assert ctx.status is not None
424
+ if ctx.status.batch.in_batch():
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}")
427
+
428
+
429
+ def _metacommand_failed(
430
+ ctx: RuntimeContext,
431
+ cmd: str,
432
+ e: ErrInfo,
433
+ source: str,
434
+ line_no: int,
435
+ *,
436
+ always_halt: bool = False,
437
+ ) -> None:
438
+ """Record a failed metacommand; stop the run if METACOMMAND_ERROR_HALT is on, or *always_halt*.
439
+
440
+ With it off the script carries on, and ``METACOMMAND_ERROR()``,
441
+ ``$LAST_ERROR`` and ``$ERROR_MESSAGE`` say what failed.
442
+ """
443
+ status = ctx.status
444
+ subvars = ctx.subvars
445
+ assert status is not None
446
+ assert subvars is not None
447
+ stamp_errinfo(e)
448
+ status.metacommand_error = True
449
+ subvars.add_substitution("$LAST_ERROR", cmd)
450
+ subvars.add_substitution("$ERROR_MESSAGE", e.errmsg())
451
+ if ctx.exec_log is not None:
452
+ ctx.exec_log.log_status_info(f"Metacommand error: {logged_error(e)}")
453
+ _fail_open_batches(ctx, source, line_no)
454
+ if status.halt_on_metacommand_err or always_halt:
455
+ exit_now(1, e)
456
+ status.error_history.append((source, line_no, cmd, e.errmsg()))
457
+
458
+
306
459
  def _exec_metacommand(
307
460
  ctx: RuntimeContext,
308
461
  cmd: str,
@@ -335,28 +488,30 @@ def _exec_metacommand(
335
488
  if (manifest := _manifest.current()) is not None:
336
489
  manifest.record_metacommand(cmd, source, line_no, metacommandlist)
337
490
  return result
338
- except ErrInfo as errinfo:
339
- e = errinfo
340
- except SystemExit:
341
- raise
342
491
  except Exception as exc:
343
- e = ErrInfo(type="exception", exception_msg=exception_desc())
344
- e.__cause__ = exc
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
345
507
  if e:
346
- stamp_errinfo(e)
347
- status.metacommand_error = True
348
- subvars.add_substitution("$LAST_ERROR", cmd)
349
- subvars.add_substitution("$ERROR_MESSAGE", e.errmsg())
350
- if ctx.exec_log is not None:
351
- ctx.exec_log.log_status_info(f"Metacommand error: {logged_error(e)}")
352
- if status.halt_on_metacommand_err:
353
- exit_now(1, e)
508
+ _metacommand_failed(ctx, cmd, e, source, line_no)
354
509
  if (manifest := _manifest.current()) is not None:
355
510
  manifest.record_metacommand(cmd, source, line_no, metacommandlist)
356
- status.error_history.append((source, line_no, cmd, e.errmsg()))
357
511
  return None
358
512
  # No handler matched — truly unknown metacommand
359
513
  status.metacommand_error = True
514
+ _fail_open_batches(ctx, source, line_no)
360
515
  raise ErrInfo(type="cmd", command_text=cmd, other_msg="Unknown metacommand")
361
516
 
362
517
 
@@ -443,10 +598,22 @@ def _execute_node(
443
598
  # !!$COUNTER_N!! and re-roll !!$RANDOM!!/!!$UUID!! references.
444
599
  effective_locals = _stack_localvars(ctx) or localvars
445
600
  expanded = substitute_vars(command, effective_locals, ctx=ctx)
446
- if _BREAK_RX.match(expanded):
447
- raise _BreakLoop
448
601
  ctx.last_command = ExecutingStatement(node)
449
- _exec_metacommand(ctx, expanded, node.span.file, node.span.start_line)
602
+ if not _BREAK_RX.match(expanded):
603
+ _exec_metacommand(ctx, expanded, node.span.file, node.span.start_line)
604
+ elif _break_has_target(ctx):
605
+ raise _BreakLoop
606
+ else:
607
+ _metacommand_failed(
608
+ ctx,
609
+ expanded,
610
+ ErrInfo(
611
+ type="cmd",
612
+ other_msg="BREAK has nothing to leave: it is not inside a LOOP, SCRIPT or included file.",
613
+ ),
614
+ node.span.file,
615
+ node.span.start_line,
616
+ )
450
617
 
451
618
  elif isinstance(node, IfBlock):
452
619
  ctx.last_command = ExecutingStatement(node)
@@ -499,7 +666,7 @@ def _execute_if(
499
666
  """
500
667
  from execsql.state import ExecFrame
501
668
 
502
- if _eval_condition(ctx, node.condition, node.condition_modifiers):
669
+ if _condition_holds(ctx, node.command, node.span, node.condition, node.condition_modifiers):
503
670
  _push_block_frame(
504
671
  ctx,
505
672
  ExecFrame(kind="if", label=node.condition, source=node.span.file, line=node.span.start_line),
@@ -512,7 +679,7 @@ def _execute_if(
512
679
 
513
680
  # Try ELSEIF clauses
514
681
  for clause in node.elseif_clauses:
515
- if _eval_condition(ctx, clause.condition, clause.condition_modifiers):
682
+ if _condition_holds(ctx, clause.command, clause.span, clause.condition, clause.condition_modifiers):
516
683
  _push_block_frame(
517
684
  ctx,
518
685
  ExecFrame(kind="elseif", label=clause.condition, source=node.span.file, line=node.span.start_line),
@@ -552,9 +719,8 @@ def _execute_loop(
552
719
  try:
553
720
  if node.loop_type == "WHILE":
554
721
  while True:
555
- effective_locals = _stack_localvars(ctx)
556
- expanded = substitute_vars(condition, effective_locals, ctx=ctx)
557
- if not xcmd_test(expanded):
722
+ ctx.last_command = ExecutingStatement(node)
723
+ if not _condition_holds(ctx, node.command, node.span, condition):
558
724
  break
559
725
  frame.iteration += 1
560
726
  try:
@@ -568,9 +734,8 @@ def _execute_loop(
568
734
  _execute_nodes(ctx, node.body, node.span.file, localvars, in_loop=True)
569
735
  except _BreakLoop:
570
736
  break
571
- effective_locals = _stack_localvars(ctx)
572
- expanded = substitute_vars(condition, effective_locals, ctx=ctx)
573
- if xcmd_test(expanded):
737
+ ctx.last_command = ExecutingStatement(node)
738
+ if _condition_holds(ctx, node.command, node.span, condition):
574
739
  break
575
740
  finally:
576
741
  ctx.ast_exec_stack.pop()
@@ -583,22 +748,87 @@ def _execute_batch(
583
748
  *,
584
749
  in_loop: bool = False,
585
750
  ) -> None:
586
- """Execute a BEGIN BATCH / END BATCH block."""
751
+ """Execute a BEGIN BATCH / END BATCH block.
752
+
753
+ Reaching END BATCH commits the batch; a commit that fails is an SQL error
754
+ on the END BATCH line, after the batch's uncommitted databases are rolled
755
+ back, and every enclosing batch then rolls back at its own END BATCH
756
+ instead of committing. Leaving it any other way (BREAK,
757
+ an error, HALT, an interrupt, END BATCH's included before its commit)
758
+ abandons it: see
759
+ :meth:`~execsql.script.control.BatchLevels.abandon_batch` for when that
760
+ rolls back and when an enclosing batch takes the statements over. A
761
+ halt usually gets there first: :func:`~execsql.utils.errors.exit_now`
762
+ abandons every open batch before an ON ERROR_HALT script runs, so
763
+ nothing is left to do here.
764
+ """
587
765
  from execsql.state import ExecFrame
588
766
 
589
767
  status = ctx.status
590
768
  assert status is not None
591
- status.batch.new_batch()
592
- _push_block_frame(
593
- ctx,
594
- ExecFrame(kind="batch", label="", source=node.span.file, line=node.span.start_line),
595
- )
769
+ # This batch's level, once pushed.
770
+ depth = len(status.batch.batchlevels) + 1
771
+ completed = False
596
772
  try:
597
- _execute_nodes(ctx, node.body, node.span.file, localvars, in_loop=in_loop)
598
- finally:
599
- ctx.ast_exec_stack.pop()
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()
600
782
  if status.batch.in_batch():
601
- status.batch.end_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
795
+ completed = True
796
+ finally:
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:
806
+ status.batch.abandon_batch()
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)
602
832
 
603
833
 
604
834
  def _pre_register_scripts(ctx: RuntimeContext, nodes: list[Node]) -> None:
@@ -649,107 +879,123 @@ def _execute_include(
649
879
  node: IncludeDirective,
650
880
  localvars: SubVarSet | None = None,
651
881
  ) -> None:
652
- """Execute an INCLUDE or EXECUTE SCRIPT directive.
653
-
654
- **INCLUDE** is handled natively: the target file is parsed by the AST
655
- parser and executed through the AST executor with circular-include
656
- detection.
882
+ """Run an INCLUDE or EXECUTE SCRIPT written in the script.
657
883
 
658
- **EXECUTE SCRIPT** is handled natively when the target is in
659
- ``ctx.ast_scripts``: arguments are parsed, a local variable overlay
660
- is created, and the body is executed through the AST executor.
661
- WHILE/UNTIL loops are handled natively too.
884
+ Variables in its file, script name and arguments are substituted once,
885
+ as in any other metacommand, so ``!!$COUNTER_1!!`` counts once and
886
+ ``$LAST_ERROR`` holds the text that ran. An EXECUTE SCRIPT ``WHILE`` /
887
+ ``UNTIL`` condition is substituted on each pass instead.
662
888
  """
663
- if node.is_execute_script:
664
- # Substitute variables in the target — the script name may be passed
665
- # as a parameter (e.g., EXECUTE SCRIPT !!#script_name!!).
666
- effective_locals = _stack_localvars(ctx) or localvars
667
- target = substitute_vars(node.target, effective_locals, ctx=ctx).strip().lower()
668
-
669
- # Native path: target is in our AST registry
670
- if target in ctx.ast_scripts:
671
- _execute_script_native(ctx, node, ctx.ast_scripts[target], localvars)
672
- return
673
-
674
- # Target not registered — IF EXISTS silently skips; otherwise error.
675
- if node.if_exists:
676
- return
677
- raise ErrInfo(
678
- "cmd",
679
- other_msg=f"There is no SCRIPT named {node.target}.",
680
- )
681
-
682
- # --- INCLUDE (file inclusion) — parse and execute natively ---
683
- _execute_include_native(ctx, node, localvars)
889
+ effective_locals = _stack_localvars(ctx) or localvars
890
+ directive = substitute_directive(node, lambda text: substitute_vars(text, effective_locals, ctx=ctx))
891
+ _run_directive(ctx, directive, localvars)
684
892
 
685
893
 
686
- def _execute_script_native(
894
+ def _run_directive(
687
895
  ctx: RuntimeContext,
688
896
  node: IncludeDirective,
689
- script_block: ScriptBlock,
690
897
  localvars: SubVarSet | None = None,
691
898
  ) -> None:
692
- """Execute a SCRIPT block natively through the AST executor.
899
+ """Run an INCLUDE or EXECUTE SCRIPT whose variables have been substituted.
693
900
 
694
- Pushes a SCRIPT-kind :class:`~execsql.state.ExecFrame` onto
695
- ``ctx.ast_exec_stack`` so that metacommand handlers (``x_sub``,
696
- ``x_rm_sub``, ``xf_sub_defined``, prompt handlers, etc.) can access
697
- ``~`` local variables and ``#`` script arguments via
698
- ``ctx.current_localvars()`` / ``ctx.current_paramvals()``. The frame
699
- is popped on exit (including on error) via ``try/finally``.
901
+ **INCLUDE** parses the target file with the AST parser and executes it
902
+ with circular-include detection. **EXECUTE SCRIPT** runs a SCRIPT
903
+ registered in ``ctx.ast_scripts`` with its arguments in a local variable
904
+ overlay, once or in a WHILE / UNTIL loop.
905
+
906
+ The INCLUDE or EXECUTE SCRIPT failing itself (no such file or script, a
907
+ missing argument) is a metacommand error; see :func:`_directive_failed`.
908
+ """
909
+ if (manifest := _manifest.current()) is not None:
910
+ assert ctx.metacommandlist is not None
911
+ manifest.record_metacommand(node.command, node.span.file, node.span.start_line, ctx.metacommandlist)
912
+
913
+ if not node.is_execute_script:
914
+ _execute_include_native(ctx, node, localvars)
915
+ return
916
+
917
+ target = node.target.strip().lower()
918
+ script_block = ctx.ast_scripts.get(target)
919
+ if script_block is None:
920
+ # IF EXISTS silently skips a SCRIPT that is not defined.
921
+ if not node.if_exists:
922
+ _directive_failed(ctx, node, ErrInfo("cmd", other_msg=f"There is no SCRIPT named {node.target.strip()}."))
923
+ return
924
+ try:
925
+ paramvals = _script_args(node, script_block)
926
+ except ErrInfo as e:
927
+ _directive_failed(ctx, node, e)
928
+ return
929
+ _execute_script_native(ctx, node, script_block, paramvals)
930
+
931
+
932
+ def _directive_failed(ctx: RuntimeContext, node: IncludeDirective, e: ErrInfo) -> None:
933
+ """INCLUDE or EXECUTE SCRIPT itself failed (no such file or script, a missing argument): a metacommand error.
934
+
935
+ Errors raised by the statements an included file or script runs are
936
+ theirs, not the directive's, and are handled where they happen.
937
+ """
938
+ _metacommand_failed(ctx, node.command, e, node.span.file, node.span.start_line)
939
+
940
+
941
+ def _script_args(node: IncludeDirective, script_block: ScriptBlock) -> ScriptArgSubVarSet | None:
942
+ """The arguments an EXECUTE SCRIPT passes to *script_block*, with defaults for those it leaves out.
943
+
944
+ ``node.arguments`` must already be substituted. Raises :class:`ErrInfo`
945
+ when a required parameter has no argument.
700
946
  """
701
- from execsql.script.variables import ScriptArgSubVarSet
702
947
  from execsql.utils.strings import wo_quotes
703
948
 
704
- # Parse arguments (replicates ScriptExecSpec logic)
705
- # Expand the argument string in the *caller's* scope so that references
706
- # like val=!!#parent_param!! or val=!!~parent_local!! resolve correctly
707
- # before we push a new scope for this script.
708
- paramvals: ScriptArgSubVarSet | None = None
709
949
  if node.arguments is not None:
710
- caller_locals = _stack_localvars(ctx)
711
- expanded_args = substitute_vars(node.arguments, caller_locals, ctx=ctx)
712
950
  args_rx = re.compile(
713
951
  r'(?P<param>#?\w+)\s*=\s*(?P<arg>(?:(?:[^"\'\[][^,\)]*)|(?:"[^"]*")|(?:\'[^\']*\')|(?:\[[^\]]*\])))',
714
952
  re.I,
715
953
  )
716
- all_args = re.findall(args_rx, expanded_args)
954
+ all_args = re.findall(args_rx, node.arguments)
717
955
  all_cleaned_args = [(ae[0], wo_quotes(ae[1])) for ae in all_args]
718
956
  all_prepared_args = [(ae[0] if ae[0][0] == "#" else "#" + ae[0], ae[1]) for ae in all_cleaned_args]
719
957
  paramvals = ScriptArgSubVarSet()
720
958
  for param, arg in all_prepared_args:
721
959
  paramvals.add_substitution(param, arg)
722
-
723
- # Validate parameter names match — with default parameter support
724
- if script_block.param_defs is not None:
725
- passed_names = [p[0].lstrip("#") for p in all_prepared_args]
726
- required = [p.name for p in script_block.param_defs if p.required]
727
- missing = [p for p in required if p not in passed_names]
728
- if missing:
729
- raise ErrInfo(
730
- "error",
731
- other_msg=(f"Missing required parameter(s) ({', '.join(missing)}) in call to {script_block.name}."),
732
- )
733
- # Inject defaults for optional params not provided
734
- for pdef in script_block.param_defs:
735
- if pdef.default is not None and pdef.name not in passed_names:
736
- paramvals.add_substitution(f"#{pdef.name}", pdef.default)
960
+ passed_names = [p[0].lstrip("#") for p in all_prepared_args]
737
961
  else:
738
- if script_block.param_defs is not None:
739
- required = [p.name for p in script_block.param_defs if p.required]
740
- if required:
741
- raise ErrInfo(
742
- "error",
743
- other_msg=(
744
- f"Missing required parameter(s) ({', '.join(required)}) in call to {script_block.name}."
745
- ),
746
- )
747
- # No args provided but all params have defaults — inject them all
748
- paramvals = ScriptArgSubVarSet()
749
- for pdef in script_block.param_defs:
750
- if pdef.default is not None:
751
- paramvals.add_substitution(f"#{pdef.name}", pdef.default)
962
+ if script_block.param_defs is None:
963
+ return None
964
+ paramvals = ScriptArgSubVarSet()
965
+ passed_names = []
966
+
967
+ # Validate parameter names match — with default parameter support
968
+ if script_block.param_defs is not None:
969
+ missing = [p.name for p in script_block.param_defs if p.required and p.name not in passed_names]
970
+ if missing:
971
+ raise ErrInfo(
972
+ "error",
973
+ other_msg=f"Missing required parameter(s) ({', '.join(missing)}) in call to {script_block.name}.",
974
+ )
975
+ # Inject defaults for optional params not provided
976
+ for pdef in script_block.param_defs:
977
+ if pdef.default is not None and pdef.name not in passed_names:
978
+ paramvals.add_substitution(f"#{pdef.name}", pdef.default)
979
+ return paramvals
752
980
 
981
+
982
+ def _execute_script_native(
983
+ ctx: RuntimeContext,
984
+ node: IncludeDirective,
985
+ script_block: ScriptBlock,
986
+ paramvals: ScriptArgSubVarSet | None,
987
+ ) -> None:
988
+ """Execute a SCRIPT block natively through the AST executor.
989
+
990
+ *paramvals* are its arguments, from :func:`_script_args`.
991
+
992
+ Pushes a SCRIPT-kind :class:`~execsql.state.ExecFrame` onto
993
+ ``ctx.ast_exec_stack`` so that metacommand handlers (``x_sub``,
994
+ ``x_rm_sub``, ``xf_sub_defined``, prompt handlers, etc.) can access
995
+ ``~`` local variables and ``#`` script arguments via
996
+ ``ctx.current_localvars()`` / ``ctx.current_paramvals()``. The frame
997
+ is popped on exit (including on error) via ``try/finally``.
998
+ """
753
999
  # Push a SCRIPT-kind ExecFrame onto ast_exec_stack so that:
754
1000
  # - get_subvarset() can find ~local and +outer-scope variables
755
1001
  # - xf_sub_defined/xf_sub_empty can check ~local and #param variables
@@ -782,9 +1028,8 @@ def _execute_script_native(
782
1028
 
783
1029
  if node.loop_type == "WHILE":
784
1030
  while True:
785
- effective_locals = _stack_localvars(ctx)
786
- expanded = substitute_vars(condition, effective_locals, ctx=ctx)
787
- if not xcmd_test(expanded):
1031
+ ctx.last_command = ExecutingStatement(node)
1032
+ if not _condition_holds(ctx, node.command, node.span, condition):
788
1033
  break
789
1034
  frame.iteration += 1
790
1035
  try:
@@ -798,18 +1043,14 @@ def _execute_script_native(
798
1043
  _run_body()
799
1044
  except _BreakLoop:
800
1045
  break
801
- effective_locals = _stack_localvars(ctx)
802
- expanded = substitute_vars(condition, effective_locals, ctx=ctx)
803
- if xcmd_test(expanded):
1046
+ ctx.last_command = ExecutingStatement(node)
1047
+ if _condition_holds(ctx, node.command, node.span, condition):
804
1048
  break
805
1049
  else:
806
1050
  try:
807
1051
  _run_body()
808
- except _BreakLoop as exc:
809
- raise ErrInfo(
810
- type="cmd",
811
- other_msg=f"BREAK metacommand inside SCRIPT '{script_block.name}' but not inside a LOOP.",
812
- ) from exc
1052
+ except _BreakLoop:
1053
+ pass # BREAK outside a LOOP in the script returns from it
813
1054
  finally:
814
1055
  _pop_frame(ctx)
815
1056
 
@@ -824,24 +1065,68 @@ def _execute_include_native(
824
1065
  Handles tilde expansion, IF EXISTS, logging, and circular-include
825
1066
  detection via ``ctx.include_chain``.
826
1067
  """
827
- from execsql.script.parser import parse_script
828
- from execsql.utils.errors import file_size_date
1068
+ try:
1069
+ loaded = _load_include(ctx, node)
1070
+ except _OutsideIncludeRoot as refused:
1071
+ # A security boundary, not a script error to carry on from: the run
1072
+ # stops whatever METACOMMAND_ERROR_HALT says.
1073
+ _metacommand_failed(ctx, node.command, refused.errinfo, node.span.file, node.span.start_line, always_halt=True)
1074
+ return
1075
+ except Exception as exc:
1076
+ _directive_failed(ctx, node, _as_errinfo(exc))
1077
+ return
1078
+ if loaded is None:
1079
+ return
1080
+ resolved, included_tree = loaded
829
1081
 
830
- # Substitute variables in the target path
831
- effective_locals = _stack_localvars(ctx) or localvars
832
- target = substitute_vars(node.target, effective_locals, ctx=ctx).strip()
1082
+ # Pre-register SCRIPT blocks in the included file so forward references work.
1083
+ _pre_register_scripts(ctx, included_tree.body)
1084
+
1085
+ # Execute with include-chain tracking
1086
+ from execsql.state import ExecFrame
1087
+
1088
+ ctx.include_chain.append(resolved)
1089
+ _push_block_frame(
1090
+ ctx,
1091
+ ExecFrame(kind="include", label=Path(resolved).name, source=resolved, line=1),
1092
+ )
1093
+ try:
1094
+ _execute_nodes(ctx, included_tree.body, included_tree.source, localvars)
1095
+ except _BreakLoop:
1096
+ pass # BREAK outside a LOOP in the file leaves it
1097
+ finally:
1098
+ ctx.ast_exec_stack.pop()
1099
+ ctx.include_chain.pop()
1100
+
1101
+
1102
+ class _OutsideIncludeRoot(Exception):
1103
+ """An INCLUDE's file is outside ``include_root``; *errinfo* says where it resolved."""
833
1104
 
834
- # Strip surrounding quotes — the AST parser captures the full target
835
- # including any quotes, but the legacy dispatch regex stripped them.
836
- if len(target) >= 2 and target[0] in ('"', "'") and target[-1] == target[0]:
837
- target = target[1:-1]
1105
+ def __init__(self, errinfo: ErrInfo) -> None:
1106
+ super().__init__(str(errinfo))
1107
+ self.errinfo = errinfo
838
1108
 
839
- # Tilde expansion (matches x_include legacy handler)
1109
+
1110
+ def _load_include(
1111
+ ctx: RuntimeContext,
1112
+ node: IncludeDirective,
1113
+ ) -> tuple[str, Any] | None:
1114
+ """Find and parse the file an INCLUDE names: ``(resolved path, tree)``, or ``None`` to skip it (IF EXISTS).
1115
+
1116
+ Raises :class:`_OutsideIncludeRoot` for a file outside ``include_root``,
1117
+ and :class:`ErrInfo` when the INCLUDE itself fails: no such file, a
1118
+ circular include, or a file that does not parse.
1119
+ """
1120
+ from execsql.script.parser import parse_script
1121
+ from execsql.utils.errors import file_size_date
1122
+
1123
+ target = node.target
1124
+ # Tilde expansion (matches the monolith's x_include)
840
1125
  if len(target) > 1 and target[0] == "~" and target[1] == os.sep:
841
1126
  target = str(Path.home() / target[2:])
842
1127
 
843
- # Optional containment: when conf.include_root is set, the resolved
844
- # INCLUDE / EXECUTE SCRIPT target must live under that root.
1128
+ # Optional containment: when conf.include_root is set, the file must
1129
+ # live under that root.
845
1130
  include_root = getattr(ctx.conf, "include_root", None) if hasattr(ctx, "conf") else None
846
1131
  if include_root is None:
847
1132
  import execsql.state as _state
@@ -850,14 +1135,17 @@ def _execute_include_native(
850
1135
  if include_root:
851
1136
  from execsql.utils.fileio import safe_output_path
852
1137
 
853
- target = safe_output_path(target, include_root)
1138
+ try:
1139
+ target = safe_output_path(target, include_root)
1140
+ except ErrInfo as e:
1141
+ raise _OutsideIncludeRoot(e) from e
854
1142
 
855
1143
  target_path = Path(target)
856
1144
 
857
1145
  # IF EXISTS handling
858
1146
  if node.if_exists:
859
1147
  if not target_path.is_file():
860
- return
1148
+ return None
861
1149
  else:
862
1150
  if not target_path.is_file():
863
1151
  raise ErrInfo(type="error", other_msg=f"File {target} does not exist.")
@@ -883,23 +1171,7 @@ def _execute_include_native(
883
1171
  included_tree = parse_script(target, encoding=encoding)
884
1172
  if (manifest := _manifest.current()) is not None:
885
1173
  manifest.record_include(target, node.span.file, node.span.start_line)
886
-
887
- # Pre-register SCRIPT blocks in the included file so forward references work.
888
- _pre_register_scripts(ctx, included_tree.body)
889
-
890
- # Execute with include-chain tracking
891
- from execsql.state import ExecFrame
892
-
893
- ctx.include_chain.append(resolved)
894
- _push_block_frame(
895
- ctx,
896
- ExecFrame(kind="include", label=Path(resolved).name, source=resolved, line=1),
897
- )
898
- try:
899
- _execute_nodes(ctx, included_tree.body, included_tree.source, localvars)
900
- finally:
901
- ctx.ast_exec_stack.pop()
902
- ctx.include_chain.pop()
1174
+ return resolved, included_tree
903
1175
 
904
1176
 
905
1177
  # ---------------------------------------------------------------------------
@@ -908,7 +1180,7 @@ def _execute_include_native(
908
1180
 
909
1181
 
910
1182
  class _BreakLoop(BaseException):
911
- """Raised by BREAK metacommand to exit the innermost loop.
1183
+ """Raised by BREAK to leave the innermost LOOP, SCRIPT or included file.
912
1184
 
913
1185
  A ``BaseException``, like ``KeyboardInterrupt``, so the ``except
914
1186
  Exception`` handlers it may pass on its way to the loop leave it alone:
@@ -917,13 +1189,30 @@ class _BreakLoop(BaseException):
917
1189
  """
918
1190
 
919
1191
 
920
- def _inside_loop(ctx: RuntimeContext) -> bool:
921
- """Whether a BREAK at the current statement would leave a loop within its scope."""
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
+
1199
+ def _break_has_target(ctx: RuntimeContext) -> bool:
1200
+ """Whether a BREAK at the current statement has something to leave.
1201
+
1202
+ BREAK leaves the innermost LOOP, SCRIPT or included file (a SCRIPT that
1203
+ ``EXECUTE SCRIPT … WHILE`` / ``UNTIL`` repeats stops repeating); in the main
1204
+ script outside all of them there is nothing to leave.
1205
+
1206
+ Each kind listed here must have a runner that catches ``_BreakLoop``:
1207
+ ``_execute_loop``, ``_execute_script_native`` and
1208
+ ``_execute_include_native``. A ``_BreakLoop`` nobody catches would end
1209
+ the run as an uncaught BaseException.
1210
+ """
922
1211
  for frame in reversed(ctx.ast_exec_stack):
923
- if frame.kind in ("loop_while", "loop_until"):
1212
+ if frame.kind in ("loop_while", "loop_until", "script", "include"):
924
1213
  return True
925
- if frame.kind in ("script", "main"):
926
- return frame.kind == "script" and frame.iteration > 0
1214
+ if frame.kind == "main":
1215
+ return False
927
1216
  return False
928
1217
 
929
1218
 
@@ -986,9 +1275,8 @@ class ExecutingStatement:
986
1275
  node = self.node
987
1276
  if isinstance(node, SqlStatement):
988
1277
  return _StatementText(node.text)
989
- if isinstance(node, MetaCommandStatement):
990
- return _StatementText(node.command, "-- !x! ")
991
- return _StatementText("")
1278
+ command = getattr(node, "command", "")
1279
+ return _StatementText(command, "-- !x! " if command else "")
992
1280
 
993
1281
  def __repr__(self) -> str:
994
1282
  return f"ExecutingStatement({self.source}:{self.line_no}, {self.command_type})"
@@ -1006,23 +1294,7 @@ def _node_cmd_type(node: Node) -> str:
1006
1294
 
1007
1295
 
1008
1296
  def _node_cmd_text(node: Node) -> str:
1009
- if isinstance(node, SqlStatement):
1010
- return node.text
1011
- if isinstance(node, MetaCommandStatement):
1012
- return "-- !x! " + node.command
1013
- if isinstance(node, IfBlock):
1014
- return f"-- !x! IF ({node.condition})"
1015
- if isinstance(node, LoopBlock):
1016
- return f"-- !x! LOOP {node.loop_type} ({node.condition})"
1017
- if isinstance(node, BatchBlock):
1018
- return "-- !x! BEGIN BATCH"
1019
- if isinstance(node, ScriptBlock):
1020
- return f"-- !x! BEGIN SCRIPT {node.name}"
1021
- if isinstance(node, IncludeDirective):
1022
- if node.is_execute_script:
1023
- return f"-- !x! EXECUTE SCRIPT {node.target}"
1024
- return f"-- !x! INCLUDE {node.target}"
1025
- return repr(node)
1297
+ return ExecutingStatement(node).command.commandline() or repr(node)
1026
1298
 
1027
1299
 
1028
1300
  # ---------------------------------------------------------------------------
@@ -1068,11 +1340,6 @@ def execute(script: Script, *, ctx: RuntimeContext | None = None) -> None:
1068
1340
  _push_frame(ctx, "<main>", script.source, line_no=1, kind="main")
1069
1341
  try:
1070
1342
  _execute_nodes(ctx, script.body, script.source)
1071
- except _BreakLoop as exc:
1072
- raise ErrInfo(
1073
- type="cmd",
1074
- other_msg="BREAK metacommand outside of a LOOP block.",
1075
- ) from exc
1076
1343
  finally:
1077
1344
  _pop_frame(ctx)
1078
1345
 
@@ -1091,7 +1358,8 @@ def execute_input(
1091
1358
  """Run input typed at a BREAKPOINT prompt as the next lines of the paused script.
1092
1359
 
1093
1360
  The input runs as if it were written where the script paused: in its
1094
- variable scope, its batch and its loop, so a BREAK leaves that loop.
1361
+ variable scope, its batch and its loop, so a BREAK leaves that loop
1362
+ (or the SCRIPT or included file it paused in).
1095
1363
  Unlike :func:`execute`, nothing is reset.
1096
1364
 
1097
1365
  *show* receives the result of each SQL statement run. An error that
@@ -1107,9 +1375,17 @@ def execute_input(
1107
1375
  ctx.prompt_input = show
1108
1376
  try:
1109
1377
  _execute_nodes(ctx, script.body, script.source)
1110
- except _BreakLoop as exc:
1111
- if not _inside_loop(ctx):
1112
- raise ErrInfo(type="cmd", other_msg="BREAK metacommand outside of a LOOP block.") from exc
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)
1113
1389
  raise
1114
1390
  finally:
1115
1391
  ctx.prompt_input = prompt_input