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/importers/base.py CHANGED
@@ -25,22 +25,16 @@ def refuse_existing_table(db: Database, schema_name: str | None, table_name: str
25
25
  """Stop a ``TO NEW`` before any DDL when the target table already exists.
26
26
 
27
27
  Only the schema the CREATE TABLE will use counts: for an unqualified
28
- name, the database's current schema (MySQL: database). Best effort:
29
- when the database cannot say, the command goes ahead and the CREATE
30
- TABLE reports any conflict itself.
28
+ name, the database's current schema (MySQL: database). If that schema
29
+ is unknown, CREATE TABLE reports any conflict itself. Lookup failures
30
+ propagate so the executor can fail an open batch.
31
31
  """
32
32
  lookup_schema = schema_name
33
33
  if schema_name is None and not db.unqualified_lookup_is_scoped:
34
- try:
35
- lookup_schema = db.current_schema()
36
- except Exception:
37
- lookup_schema = None
34
+ lookup_schema = db.current_schema()
38
35
  if lookup_schema is None:
39
36
  return # An unqualified lookup would search every schema.
40
- try:
41
- exists = db.table_exists_where_created(table_name, lookup_schema)
42
- except Exception:
43
- return # The database cannot say; CREATE TABLE reports any conflict.
37
+ exists = db.table_exists_where_created(table_name, lookup_schema)
44
38
  if exists:
45
39
  name = db.schema_qualified_table_name(schema_name, table_name)
46
40
  raise ErrInfo(
@@ -124,7 +118,7 @@ def import_data_table(
124
118
  # Most adapters delay commit until after populating the table; adapters
125
119
  # that need DDL committed first (Firebird) must do it here.
126
120
  if db.needs_explicit_commit_after_ddl():
127
- db.conn.commit()
121
+ db.commit_now()
128
122
  except Exception as e:
129
123
  raise ErrInfo(
130
124
  type="db",
execsql/importers/csv.py CHANGED
@@ -16,7 +16,6 @@ from execsql.exceptions import ErrInfo
16
16
  from execsql.db.base import Database
17
17
  from execsql.importers.base import refuse_existing_table
18
18
  import execsql.state as _state
19
- from execsql.types import dbt_firebird
20
19
 
21
20
  __all__ = ["importfile", "importtable"]
22
21
 
@@ -66,10 +65,9 @@ def importtable(
66
65
  ) from e
67
66
  try:
68
67
  db.execute(sql)
69
- # Don't commit table creation here; the commit will be done after data import
70
- # ...except for Firebird. Execute the commit directly via the connection so it is always done.
71
- if db.type == dbt_firebird:
72
- db.conn.commit()
68
+ # Firebird requires the DDL committed before the new table is used.
69
+ if db.needs_explicit_commit_after_ddl():
70
+ db.commit_now()
73
71
  except Exception as e:
74
72
  raise ErrInfo(
75
73
  type="db",
@@ -35,9 +35,20 @@ from typing import Any
35
35
  import execsql.state as _state
36
36
  from execsql.gui.base import format_table, row_count_text
37
37
  from execsql.script import current_script_line
38
- from execsql.utils.errors import exit_now
38
+ from execsql.utils.errors import exception_desc, exit_now
39
39
  from execsql.utils.fileio import EncodedFile, check_dir
40
- from execsql.utils.gui import GUI_HALT, GuiSpec, enable_gui, gui_console_isrunning
40
+ from execsql.utils.gui import ConsoleUIError, GUI_HALT, GuiSpec, enable_gui, get_gui_response, gui_console_isrunning
41
+
42
+
43
+ def _condition_value(condition: str) -> bool:
44
+ """Evaluate a required condition; an evaluation error cannot be ignored."""
45
+ try:
46
+ return _state.xcmd_test(condition)
47
+ except ErrInfo as error:
48
+ error.always_halt = True
49
+ raise
50
+ except Exception as error:
51
+ raise ErrInfo("cmd", exception_msg=exception_desc(), always_halt=True) from error
41
52
 
42
53
 
43
54
  def x_assert(**kwargs: Any) -> None:
@@ -66,7 +77,7 @@ def x_assert(**kwargs: Any) -> None:
66
77
  else:
67
78
  message = f"Assertion failed: {condition}"
68
79
 
69
- result = _state.xcmd_test(condition)
80
+ result = _condition_value(condition)
70
81
  if result:
71
82
  if _state.exec_log is not None:
72
83
  _state.exec_log.log_user_msg(f"ASSERT passed: {condition}")
@@ -74,54 +85,52 @@ def x_assert(**kwargs: Any) -> None:
74
85
  raise ErrInfo(type="assert", other_msg=message)
75
86
 
76
87
 
77
- def _ast_only_stub(name: str):
78
- """Return an ErrInfo for a metacommand the AST executor handles structurally.
88
+ def unsupported_block_error(name: str) -> ErrInfo:
89
+ """Explain a block boundary supplied dynamically instead of in source.
79
90
 
80
- These handlers stay registered in the dispatch table so ``execsql list
81
- keywords`` and the VS Code grammar generator still see the keyword. The AST parser converts the source form (``IF`` / ``ENDIF`` /
82
- ``LOOP`` / ``BEGIN BATCH`` / ``BREAK`` / ``ELSE`` / ``ELSEIF`` / ``ANDIF``
83
- / ``ORIF``) into structural AST nodes that the executor walks directly —
84
- none of these dispatch handlers fire for parsed scripts. Reaching one
85
- means the AST parser failed to recognise the keyword, which is a bug.
91
+ The dispatch entries also keep structural keywords discoverable. They
92
+ can be reached by variable text or an inline IF containing a boundary;
93
+ this is an unsupported script form, not an internal parser failure.
86
94
  """
87
- from execsql.exceptions import ErrInfo
88
-
89
95
  return ErrInfo(
90
96
  type="cmd",
91
- other_msg=f"{name} should be handled by the AST executor, not the dispatch table.",
97
+ other_msg=(
98
+ f"{name} cannot run here; write the complete block directly in the script. "
99
+ "To append a block, define it with BEGIN SCRIPT and use EXTEND SCRIPT ... WITH SCRIPT."
100
+ ),
92
101
  )
93
102
 
94
103
 
95
104
  def x_if(**kwargs: Any) -> None:
96
- raise _ast_only_stub("IF")
105
+ raise unsupported_block_error("IF")
97
106
 
98
107
 
99
108
  def x_if_orif(**kwargs: Any) -> None:
100
- raise _ast_only_stub("ORIF")
109
+ raise unsupported_block_error("ORIF")
101
110
 
102
111
 
103
112
  def x_if_andif(**kwargs: Any) -> None:
104
- raise _ast_only_stub("ANDIF")
113
+ raise unsupported_block_error("ANDIF")
105
114
 
106
115
 
107
116
  def x_if_elseif(**kwargs: Any) -> None:
108
- raise _ast_only_stub("ELSEIF")
117
+ raise unsupported_block_error("ELSEIF")
109
118
 
110
119
 
111
120
  def x_if_else(**kwargs: Any) -> None:
112
- raise _ast_only_stub("ELSE")
121
+ raise unsupported_block_error("ELSE")
113
122
 
114
123
 
115
124
  def x_if_block(**kwargs: Any) -> None:
116
- raise _ast_only_stub("IF")
125
+ raise unsupported_block_error("IF")
117
126
 
118
127
 
119
128
  def x_if_end(**kwargs: Any) -> None:
120
- raise _ast_only_stub("ENDIF")
129
+ raise unsupported_block_error("ENDIF")
121
130
 
122
131
 
123
132
  def x_loop(**kwargs: Any) -> None:
124
- raise _ast_only_stub("LOOP")
133
+ raise unsupported_block_error("LOOP")
125
134
 
126
135
 
127
136
  def x_halt(**kwargs: Any) -> None:
@@ -180,11 +189,11 @@ def x_metacommand_error_halt(**kwargs: Any) -> None:
180
189
 
181
190
 
182
191
  def x_begin_batch(**kwargs: Any) -> None:
183
- raise _ast_only_stub("BEGIN BATCH")
192
+ raise unsupported_block_error("BEGIN BATCH")
184
193
 
185
194
 
186
195
  def x_end_batch(**kwargs: Any) -> None:
187
- raise _ast_only_stub("END BATCH")
196
+ raise unsupported_block_error("END BATCH")
188
197
 
189
198
 
190
199
  def x_rollback(**kwargs: Any) -> None:
@@ -193,13 +202,13 @@ def x_rollback(**kwargs: Any) -> None:
193
202
 
194
203
 
195
204
  def x_break(**kwargs: Any) -> None:
196
- raise _ast_only_stub("BREAK")
205
+ raise unsupported_block_error("BREAK")
197
206
 
198
207
 
199
208
  def x_wait_until(**kwargs: Any) -> None:
200
209
  countdown = int(kwargs["seconds"])
201
210
  while countdown > 0:
202
- if _state.xcmd_test(kwargs["condition"]):
211
+ if _condition_value(kwargs["condition"]):
203
212
  return
204
213
  time.sleep(1)
205
214
  countdown -= 1
@@ -279,6 +288,10 @@ def x_halt_msg(**kwargs: Any) -> None:
279
288
  "help_url": None,
280
289
  }
281
290
  _state.gui_manager_queue.put(GuiSpec(GUI_HALT, gui_args, return_queue))
282
- return_queue.get(block=True)
291
+ try:
292
+ get_gui_response(return_queue, GUI_HALT)
293
+ except ConsoleUIError as error:
294
+ # Failure to display a halt message cannot allow the script to resume.
295
+ raise ErrInfo(type="cmd", other_msg=str(error), always_halt=True) from error
283
296
  _state.exec_log.log_exit_halt(*current_script_line(), msg=errmsg)
284
297
  exit_now(errlevel, None)
@@ -37,7 +37,7 @@ from execsql.script import current_script_line
37
37
  from execsql.utils.crypto import Encrypt
38
38
  from execsql.utils.errors import exception_desc, exit_now
39
39
  from execsql.utils.fileio import register_secret
40
- from execsql.utils.gui import GUI_SELECTSUB, GuiSpec, enable_gui
40
+ from execsql.utils.gui import GUI_SELECTSUB, GuiSpec, enable_gui, get_gui_response
41
41
  from execsql.utils.strings import get_subvarset, unquoted
42
42
 
43
43
 
@@ -235,19 +235,21 @@ def x_prompt_selectsub(**kwargs: Any) -> None:
235
235
  "title": "Select data",
236
236
  "message": msg,
237
237
  "button_list": btns,
238
- "column_headers": hdrs,
239
- "rowset": rows,
238
+ "headers": hdrs,
239
+ "rows": rows,
240
240
  "help_url": help_url,
241
241
  }
242
242
  _state.gui_manager_queue.put(GuiSpec(GUI_SELECTSUB, gui_args, return_queue))
243
- user_response = return_queue.get(block=True)
243
+ user_response = get_gui_response(return_queue, GUI_SELECTSUB)
244
244
  btn_val = user_response["button"]
245
- return_val = user_response["return_value"]
246
- selected_row = None
247
- if btn_val and btn_val == 1:
248
- selected_row = rows[int(return_val[0])]
245
+ selected_row = user_response.get("row")
246
+ if btn_val == 1:
247
+ if not isinstance(selected_row, (list, tuple)) or len(selected_row) != len(hdrs):
248
+ raise ErrInfo(type="cmd", other_msg="The row selection dialog returned an invalid source row.")
249
+ elif btn_val is not None and not (btn_val == 2 and cont):
250
+ raise ErrInfo(type="cmd", other_msg="The row selection dialog returned an invalid button.")
249
251
  script, line_no = current_script_line()
250
- if btn_val is None or (btn_val == 1 and selected_row is None):
252
+ if btn_val is None:
251
253
  if _state.status.cancel_halt:
252
254
  _state.exec_log.log_exit_halt(
253
255
  script,
@@ -174,7 +174,7 @@ def _copy_rows(
174
174
  ) from e
175
175
  db2.execute(create_tbl)
176
176
  if db2.needs_explicit_commit_after_ddl():
177
- db2.execute("COMMIT;")
177
+ db2.commit_now()
178
178
 
179
179
  def get_ts() -> DataTable:
180
180
  nonlocal get_ts_tablespec
@@ -182,15 +182,6 @@ def x_write_create_table_alias(**kwargs: Any) -> None:
182
182
  )
183
183
  db = _state.dbs.aliased_as(alias)
184
184
  tbl = db.schema_qualified_table_name(schema, table)
185
- try:
186
- if not db.table_exists(table, schema):
187
- raise ErrInfo(
188
- type="cmd",
189
- command_text=kwargs["metacommandline"],
190
- other_msg=f"Table {tbl} does not exist",
191
- )
192
- except Exception:
193
- pass # Best-effort check; some adapters lack information_schema.
194
185
  select_stmt = f"select * from {tbl};"
195
186
  try:
196
187
  hdrs, rows = db.select_rowsource(select_stmt)
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
  from execsql.exceptions import ErrInfo
3
+ from execsql.gui.base import normalize_text_entry
3
4
 
4
5
  """
5
6
  Interactive user prompt metacommand handlers for execsql.
@@ -60,6 +61,7 @@ from execsql.utils.gui import (
60
61
  GuiSpec,
61
62
  QUERY_CONSOLE,
62
63
  enable_gui,
64
+ get_gui_response,
63
65
  get_yn,
64
66
  get_yn_win,
65
67
  gui_connect,
@@ -92,7 +94,7 @@ def x_prompt(**kwargs: Any) -> None:
92
94
  "help_url": help_url,
93
95
  }
94
96
  _state.gui_manager_queue.put(GuiSpec(GUI_DISPLAY, gui_args, return_queue))
95
- user_response = return_queue.get(block=True)
97
+ user_response = get_gui_response(return_queue, GUI_DISPLAY)
96
98
  btn = user_response["button"]
97
99
  if not btn and _state.status.cancel_halt:
98
100
  msg = f"Halted from display of {sq_name}"
@@ -133,7 +135,7 @@ def x_prompt_enter(**kwargs: Any) -> None:
133
135
  "help_url": help_url,
134
136
  }
135
137
  _state.gui_manager_queue.put(GuiSpec(GUI_DISPLAY, gui_args, return_queue))
136
- user_response = return_queue.get(block=True)
138
+ user_response = get_gui_response(return_queue, GUI_DISPLAY)
137
139
  btnval = user_response["button"]
138
140
  txtval = user_response["return_value"]
139
141
  if btnval is None:
@@ -141,6 +143,7 @@ def x_prompt_enter(**kwargs: Any) -> None:
141
143
  _state.exec_log.log_exit_halt(*current_script_line(), msg="Quit from prompt to enter a SUB value.")
142
144
  exit_now(2, None)
143
145
  else:
146
+ txtval = normalize_text_entry(txtval, texttype, textcase)
144
147
  subvarset = _state.subvars if sub_var[0] != "~" else _state.current_localvars()
145
148
  subvarset.add_substitution(sub_var, txtval)
146
149
  script_name, lno = current_script_line()
@@ -162,15 +165,6 @@ def x_prompt_entryform(**kwargs: Any) -> None:
162
165
  message = kwargs["message"]
163
166
  help_url = unquoted(kwargs["help"])
164
167
  tbl1 = _state.dbs.current().schema_qualified_table_name(spec_schema, spec_table)
165
- try:
166
- if not _state.dbs.current().table_exists(spec_table, spec_schema):
167
- raise ErrInfo(
168
- type="cmd",
169
- command_text=kwargs["metacommandline"],
170
- other_msg=f"Table {spec_table} does not exist",
171
- )
172
- except Exception:
173
- pass # Best-effort check; some adapters lack information_schema.
174
168
  curs = _state.dbs.current().cursor()
175
169
  cmd = f"select * from {tbl1};"
176
170
  curs.execute(cmd)
@@ -301,23 +295,23 @@ def x_prompt_entryform(**kwargs: Any) -> None:
301
295
  "help_url": help_url,
302
296
  }
303
297
  _state.gui_manager_queue.put(GuiSpec(GUI_ENTRY, gui_args, return_queue))
304
- user_response = return_queue.get(block=True)
298
+ user_response = get_gui_response(return_queue, GUI_ENTRY)
305
299
  btn = user_response["button"]
306
300
  entries = user_response["return_value"]
307
301
  script, line_no = current_script_line()
308
302
  if btn:
309
303
  assert entries is not None
310
- assert subvar is not None
311
304
  for entry in entries:
305
+ subvarset = _state.subvars if entry.name[0] != "~" else _state.current_localvars()
312
306
  if entry.value:
313
307
  value = str(entry.value)
314
- subvarset = _state.subvars if subvar[0] != "~" else _state.current_localvars()
315
308
  subvarset.add_substitution(entry.name, value)
316
309
  _state.exec_log.log_status_info(
317
310
  f"Substitution variable {entry.name} set to {{{value}}} on line {line_no} of {script}",
318
311
  )
319
312
  else:
320
- if _state.subvars.sub_exists(entry.name):
313
+ if subvarset.sub_exists(entry.name):
314
+ subvarset.remove_substitution(entry.name)
321
315
  _state.exec_log.log_status_info(
322
316
  f"Substitution variable {entry.name} removed on line {line_no} of {script}",
323
317
  )
@@ -351,7 +345,7 @@ def x_prompt_pause(**kwargs: Any) -> None:
351
345
  "countdown": maxtime_secs,
352
346
  }
353
347
  _state.gui_manager_queue.put(GuiSpec(GUI_PAUSE, gui_args, return_queue))
354
- user_response = return_queue.get(block=True)
348
+ user_response = get_gui_response(return_queue, GUI_PAUSE)
355
349
  quit = user_response["quit"]
356
350
  return_queue.task_done()
357
351
  if quit and _state.status.cancel_halt:
@@ -419,7 +413,7 @@ def prompt_compare(button_list: list, **kwargs: Any) -> Any:
419
413
  "help_url": help_url,
420
414
  }
421
415
  _state.gui_manager_queue.put(GuiSpec(GUI_COMPARE, gui_args, return_queue))
422
- user_response = return_queue.get(block=True)
416
+ user_response = get_gui_response(return_queue, GUI_COMPARE)
423
417
  btn = user_response["button"]
424
418
  if btn is None and _state.status.cancel_halt:
425
419
  script, line_no = current_script_line()
@@ -472,7 +466,7 @@ def x_prompt_ask(**kwargs: Any) -> None:
472
466
  "help_url": help_url,
473
467
  }
474
468
  _state.gui_manager_queue.put(GuiSpec(GUI_DISPLAY, gui_args, return_queue))
475
- user_response = return_queue.get(block=True)
469
+ user_response = get_gui_response(return_queue, GUI_DISPLAY)
476
470
  btn = user_response["button"]
477
471
  if btn is None:
478
472
  if _state.status.cancel_halt:
@@ -517,7 +511,7 @@ def x_prompt_map(**kwargs: Any) -> None:
517
511
  "color_col": color_col,
518
512
  }
519
513
  _state.gui_manager_queue.put(GuiSpec(GUI_MAP, gui_args, return_queue))
520
- user_response = return_queue.get(block=True)
514
+ user_response = get_gui_response(return_queue, GUI_MAP)
521
515
  btn = user_response["button"]
522
516
  if not btn and _state.status.cancel_halt:
523
517
  msg = f"Halted from map of {sq_name}"
@@ -540,15 +534,6 @@ def x_prompt_action(**kwargs: Any) -> None:
540
534
  if do_continue is not None:
541
535
  do_continue = bool(do_continue)
542
536
  tbl1 = _state.dbs.current().schema_qualified_table_name(spec_schema, spec_table)
543
- try:
544
- if not _state.dbs.current().table_exists(spec_table, spec_schema):
545
- raise ErrInfo(
546
- type="cmd",
547
- command_text=kwargs["metacommandline"],
548
- other_msg=f"Table {spec_table} does not exist",
549
- )
550
- except Exception:
551
- pass # Best-effort check; some adapters lack information_schema.
552
537
  curs = _state.dbs.current().cursor()
553
538
  cmd = f"select * from {tbl1};"
554
539
  curs.execute(cmd)
@@ -606,13 +591,56 @@ def x_prompt_action(**kwargs: Any) -> None:
606
591
  "rowset": rows,
607
592
  }
608
593
  _state.gui_manager_queue.put(GuiSpec(GUI_ACTION, gui_args, return_queue))
609
- user_response = return_queue.get(block=True)
594
+ user_response = get_gui_response(return_queue, GUI_ACTION)
610
595
  btn = user_response["button"]
611
596
  script, line_no = current_script_line()
612
- if not btn and _state.status.cancel_halt:
613
- msg = f"Halted from entry form {tbl1}"
614
- _state.exec_log.log_exit_halt(script, line_no, msg)
615
- exit_now(2, None)
597
+ if btn is None:
598
+ if _state.status.cancel_halt:
599
+ msg = f"Halted from action form {tbl1}"
600
+ _state.exec_log.log_exit_halt(script, line_no, msg)
601
+ exit_now(2, None)
602
+ return
603
+ if btn == 0 and do_continue:
604
+ return
605
+ if not isinstance(btn, int) or not 1 <= btn <= len(action_list):
606
+ raise ErrInfo(type="cmd", other_msg="The action dialog returned an invalid action.")
607
+ action = action_list[btn - 1]
608
+ selected_row = user_response.get("row")
609
+ if action.data_required and (
610
+ not colnames or not isinstance(selected_row, (list, tuple)) or len(selected_row) != len(colnames)
611
+ ):
612
+ raise ErrInfo(type="cmd", other_msg="The selected action requires a selected data row.")
613
+
614
+ from execsql.metacommands.script_ext import _get_ast_script
615
+ from execsql.script.ast import IncludeDirective, SourceSpan
616
+ from execsql.script.executor import _ScriptExecutionError, _execute_script_native, _script_args
617
+ from execsql.state import get_context
618
+
619
+ if not isinstance(action.script, str) or not action.script.strip():
620
+ raise ErrInfo(type="cmd", other_msg="A script name is required for the selected action.")
621
+ script_name = action.script.strip()
622
+ block = _get_ast_script(script_name)
623
+ node = IncludeDirective(
624
+ span=SourceSpan(script, line_no or 0),
625
+ target=script_name,
626
+ is_execute_script=True,
627
+ command=f"EXECUTE SCRIPT {script_name}",
628
+ )
629
+ arguments = _script_args(node, block)
630
+ if action.data_required:
631
+ assert colnames is not None and selected_row is not None
632
+ for name, item in zip(colnames, selected_row):
633
+ name = "@" + name
634
+ value = "" if item is None else str(item)
635
+ _state.subvars.add_substitution(name, value)
636
+ if _state.conf.log_datavars:
637
+ _state.exec_log.log_status_info(
638
+ f"Substitution string {name} set to {{{value}}} on line {line_no} of {script}",
639
+ )
640
+ try:
641
+ _execute_script_native(get_context(), node, block, arguments)
642
+ except Exception as error:
643
+ raise _ScriptExecutionError(error) from error
616
644
 
617
645
 
618
646
  def x_prompt_savefile(**kwargs: Any) -> None:
@@ -631,7 +659,7 @@ def x_prompt_savefile(**kwargs: Any) -> None:
631
659
  return_queue: _queue.Queue[Any] = _queue.Queue()
632
660
  gui_args = {"working_dir": working_dir, "script": script}
633
661
  _state.gui_manager_queue.put(GuiSpec(GUI_SAVEFILE, gui_args, return_queue))
634
- user_response = return_queue.get(block=True)
662
+ user_response = get_gui_response(return_queue, GUI_SAVEFILE)
635
663
  fn = user_response["filename"]
636
664
  if not fn:
637
665
  if _state.status.cancel_halt:
@@ -704,7 +732,7 @@ def x_prompt_openfile(**kwargs: Any) -> None:
704
732
  return_queue: _queue.Queue[Any] = _queue.Queue()
705
733
  gui_args = {"working_dir": working_dir, "script": script}
706
734
  _state.gui_manager_queue.put(GuiSpec(GUI_OPENFILE, gui_args, return_queue))
707
- user_response = return_queue.get(block=True)
735
+ user_response = get_gui_response(return_queue, GUI_OPENFILE)
708
736
  fn = user_response["filename"]
709
737
  if not fn:
710
738
  if _state.status.cancel_halt:
@@ -763,7 +791,7 @@ def x_prompt_directory(**kwargs: Any) -> None:
763
791
  return_queue: _queue.Queue[Any] = _queue.Queue()
764
792
  gui_args = {"working_dir": working_dir, "script": script}
765
793
  _state.gui_manager_queue.put(GuiSpec(GUI_DIRECTORY, gui_args, return_queue))
766
- user_response = return_queue.get(block=True)
794
+ user_response = get_gui_response(return_queue, GUI_DIRECTORY)
767
795
  dirname = user_response["directory"]
768
796
  if not dirname:
769
797
  if _state.status.cancel_halt:
@@ -836,13 +864,33 @@ def prompt_select_rows(button_list: list, **kwargs: Any) -> Any:
836
864
  "help_url": help_url,
837
865
  }
838
866
  _state.gui_manager_queue.put(GuiSpec(GUI_SELECTROWS, gui_args, return_queue))
839
- user_response = return_queue.get(block=True)
867
+ user_response = get_gui_response(return_queue, GUI_SELECTROWS)
840
868
  btn = user_response["button"]
841
869
  if btn is None and _state.status.cancel_halt:
842
870
  script, line_no = current_script_line()
843
871
  msg = f"Halted from selection of rows from {sq_name1} into {sq_name2}"
844
872
  _state.exec_log.log_exit_halt(script, line_no, msg)
845
873
  exit_now(2, None)
874
+ if btn is None:
875
+ return btn
876
+ if "rows" not in user_response:
877
+ raise ErrInfo("error", other_msg="The row-selection dialog did not return its selections.")
878
+ selections = user_response["rows"]
879
+ if selections is None: # The console can display the source but cannot select rows.
880
+ return btn
881
+ if not isinstance(selections, list) or any(
882
+ not isinstance(row, (list, tuple)) or len(row) != len(hdrs1) for row in selections
883
+ ):
884
+ raise ErrInfo("error", other_msg="The row-selection dialog returned invalid rows.")
885
+ if selections:
886
+ columns = ",".join(db2.quote_identifier(header) for header in hdrs1)
887
+ sql = f"insert into {sq_name2} ({columns}) values ({db2.paramsubs(len(hdrs1))});"
888
+ _state.status.batch.using_db(db2)
889
+ db2.begin_transaction()
890
+ for row in selections:
891
+ db2.execute(sql, list(row))
892
+ if not db2.holding():
893
+ db2.commit()
846
894
  return btn
847
895
 
848
896
 
@@ -878,12 +926,13 @@ def x_ask(**kwargs: Any) -> None:
878
926
  "button_list": [("Yes", 1, "y"), ("No", 0, "n")],
879
927
  }
880
928
  _state.gui_manager_queue.put(GuiSpec(GUI_DISPLAY, gui_args, return_queue))
881
- user_response = return_queue.get(block=True)
929
+ user_response = get_gui_response(return_queue, GUI_DISPLAY)
882
930
  btn = user_response["button"]
883
931
  if btn is None:
884
932
  if _state.status.cancel_halt:
885
933
  _state.exec_log.log_exit_halt(script, lno, msg="Quit from ASK metacommand")
886
934
  exit_now(2, None)
935
+ return None
887
936
  else:
888
937
  respstr = "Yes" if btn == 1 else "No"
889
938
  else:
@@ -925,7 +974,7 @@ def x_pause(**kwargs: Any) -> None:
925
974
  if _state.gui_manager_thread:
926
975
  return_queue: _queue.Queue[Any] = _queue.Queue()
927
976
  _state.gui_manager_queue.put(GuiSpec(QUERY_CONSOLE, {}, return_queue))
928
- user_response = return_queue.get(block=True)
977
+ user_response = get_gui_response(return_queue, QUERY_CONSOLE)
929
978
  use_gui = user_response["console_running"]
930
979
  if use_gui or _state.conf.gui_level > 0:
931
980
  enable_gui()
@@ -936,7 +985,7 @@ def x_pause(**kwargs: Any) -> None:
936
985
  "countdown": maxtime_secs,
937
986
  }
938
987
  _state.gui_manager_queue.put(GuiSpec(GUI_PAUSE, gui_args, return_queue))
939
- user_response = return_queue.get(block=True)
988
+ user_response = get_gui_response(return_queue, GUI_PAUSE)
940
989
  quit = user_response["quit"]
941
990
  return_queue.task_done()
942
991
  else:
@@ -955,12 +1004,15 @@ def x_pause(**kwargs: Any) -> None:
955
1004
 
956
1005
  def x_msg(**kwargs: Any) -> None:
957
1006
  message = kwargs["message"]
958
- current_script_line()
1007
+ script, line_no = current_script_line()
959
1008
  enable_gui()
960
1009
  return_queue: _queue.Queue[Any] = _queue.Queue()
961
1010
  gui_args = {"title": "Message", "message": message}
962
1011
  _state.gui_manager_queue.put(GuiSpec(GUI_MSG, gui_args, return_queue))
963
- return_queue.get(block=True)
1012
+ response = get_gui_response(return_queue, GUI_MSG)
1013
+ if response["button"] is None and _state.status.cancel_halt:
1014
+ _state.exec_log.log_exit_halt(script, line_no, msg="Quit from PROMPT MESSAGE metacommand")
1015
+ exit_now(2, None)
964
1016
  return None
965
1017
 
966
1018
 
@@ -69,12 +69,16 @@ def x_extendscript(**kwargs: Any) -> None:
69
69
 
70
70
  def x_extendscript_metacommand(**kwargs: Any) -> None:
71
71
  """Append a single metacommand line to an existing SCRIPT body."""
72
- from execsql.script.parser import metacommand_node
72
+ from execsql.metacommands.control import unsupported_block_error
73
+ from execsql.script.parser import block_metacommand_name, metacommand_node
73
74
 
74
75
  block = _get_ast_script(kwargs["script"])
76
+ command = kwargs["cmd"].strip()
77
+ if name := block_metacommand_name(command):
78
+ raise unsupported_block_error(name)
75
79
  script_file, script_line_no = current_script_line()
76
80
  span = _new_span(script_file, script_line_no or 0)
77
- block.body.append(metacommand_node(kwargs["cmd"].strip(), span))
81
+ block.body.append(metacommand_node(command, span))
78
82
 
79
83
 
80
84
  def x_extendscript_sql(**kwargs: Any) -> None:
@@ -157,13 +157,11 @@ def x_console_hideshow(**kwargs: Any) -> None:
157
157
 
158
158
  def x_consolewidth(**kwargs: Any) -> None:
159
159
  width = kwargs["width"]
160
- _state.conf.gui_console_width = int(width)
161
160
  gui_console_width(width)
162
161
 
163
162
 
164
163
  def x_consoleheight(**kwargs: Any) -> None:
165
164
  height = kwargs["height"]
166
- _state.conf.gui_console_height = int(height)
167
165
  gui_console_height(height)
168
166
 
169
167