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
@@ -7,11 +7,10 @@ Provides :func:`write_query_to_sqlite`, which writes a query result set
7
7
  to a table in an SQLite database file. Used by ``EXPORT … FORMAT sqlite``.
8
8
  """
9
9
 
10
- import math
11
10
  from decimal import Decimal
12
- from pathlib import Path
13
11
  from typing import Any
14
12
 
13
+ from execsql.db.sqlite import sqlite_affinity as _affinity
15
14
  from execsql.exceptions import ErrInfo
16
15
  from execsql.types import dbt_sqlite
17
16
 
@@ -20,6 +19,23 @@ _SQLITE_SAFE = (int, float, str, bytes)
20
19
  __all__ = ["export_sqlite", "write_query_to_sqlite"]
21
20
 
22
21
 
22
+ def _exact_number(value: Decimal, affinity: str) -> int | float:
23
+ """Bind a Decimal only if its numeric readback retains the source value."""
24
+ if value.is_nan():
25
+ raise ValueError("NaN has no SQLite numeric representation")
26
+ if value.is_infinite():
27
+ return float(value)
28
+ if -(2**63) <= value <= 2**63 - 1 and value == value.to_integral_value():
29
+ number = int(value)
30
+ if affinity == "REAL" and Decimal(repr(float(number))) != value:
31
+ raise ValueError("the integer is not exactly representable in a SQLite REAL")
32
+ return number
33
+ result = float(value)
34
+ if Decimal(repr(result)) != value:
35
+ raise ValueError("the decimal cannot be read back without losing digits")
36
+ return result
37
+
38
+
23
39
  def export_sqlite(
24
40
  outfile: str,
25
41
  hdrs: list[str],
@@ -27,60 +43,79 @@ def export_sqlite(
27
43
  append: bool,
28
44
  tablename: str,
29
45
  ) -> None:
30
- """Write pre-fetched rows to a table in an SQLite database file, creating it if necessary."""
46
+ """Export rows, rejecting lossy decimals before opening the destination.
47
+
48
+ Numeric Decimal values must read back equal to their source; spelling and
49
+ scale are not retained. Text columns retain their string representation.
50
+ Replacement DDL and inserts share a transaction, restored on any failure.
51
+ APPEND still refuses an existing table.
52
+ """
31
53
  import sqlite3
32
54
 
33
55
  from execsql.models import DataTable
34
56
 
35
- chunksize = 10000
36
- # B07a/F025: tablename is substituted from user input — use the
37
- # existing dbt_sqlite.quoted helper to identifier-quote every site
38
- # rather than f-string-interpolating into SQL.
57
+ rowdata = list(rows)
58
+ # Non-finite Decimals cannot pass the general-purpose integer scanner.
59
+ # A fractional placeholder preserves numeric inference without changing data.
60
+ scanrows = (
61
+ tuple(Decimal("0.5") if isinstance(v, Decimal) and not v.is_finite() else v for v in row) for row in rowdata
62
+ )
63
+ tablespec = DataTable(hdrs, scanrows, infer_strings=False)
64
+ create_sql = tablespec.create_table(dbt_sqlite, schemaname=None, tablename=tablename)
39
65
  qtable = dbt_sqlite.quoted(tablename)
40
- pre_exist = Path(outfile).is_file()
41
- sdb = sqlite3.connect(outfile)
66
+ # Let SQLite expose the generated declarations, including quoted identifiers.
67
+ probe = sqlite3.connect(":memory:")
42
68
  try:
43
- if pre_exist:
44
- curs = sdb.cursor()
45
- # Existence check: the table name is a value here (not an
46
- # identifier), so use a parameter rather than string concat.
47
- res = curs.execute(
48
- "select name from sqlite_master where type='table' and name=?;",
49
- (tablename,),
50
- )
51
- rv = res.fetchone()
52
- if not (rv is None or rv[0] == 0):
53
- if append:
54
- raise ErrInfo(type="error", other_msg=f"The table {tablename} already exists in {outfile}.")
69
+ probe.execute(create_sql)
70
+ affinities = [_affinity(col[2]) for col in probe.execute(f"PRAGMA table_info({qtable})")]
71
+ converted = []
72
+ for row in rowdata:
73
+ bound: list[Any] = []
74
+ for i, value in enumerate(row):
75
+ if isinstance(value, Decimal) and i < len(affinities) and affinities[i] not in ("TEXT", "BLOB"):
76
+ try:
77
+ number = _exact_number(value, affinities[i])
78
+ if value.is_finite():
79
+ # Preserve the previous stored value too: SQLite's text
80
+ # parser can round differently from Python's conversion.
81
+ cast_type = "REAL" if affinities[i] == "REAL" else "NUMERIC"
82
+ previous = probe.execute(f"SELECT CAST(? AS {cast_type})", (str(value),)).fetchone()[0]
83
+ if Decimal(repr(previous)) != value:
84
+ raise ValueError("SQLite's numeric conversion loses source digits")
85
+ bound.append(number)
86
+ except ValueError as exc:
87
+ raise ErrInfo(
88
+ type="error",
89
+ other_msg=(
90
+ f"Can't export column {hdrs[i]} to SQLite table {tablename}: {value} "
91
+ f"cannot be stored without losing digits. Nothing was written. "
92
+ "To preserve the exact text, export CAST(column AS VARCHAR)."
93
+ ),
94
+ ) from exc
55
95
  else:
56
- curs.execute(f"drop table {qtable};")
57
- curs.close()
58
- # Construct and run the CREATE TABLE statement
59
- rowdata = list(rows)
60
- tablespec = DataTable(hdrs, rowdata, infer_strings=False)
61
- rowdata = [
62
- tuple(
63
- str(v) if isinstance(v, Decimal) else v if (v is None or isinstance(v, _SQLITE_SAFE)) else str(v)
64
- for v in row
65
- )
66
- for row in rowdata
67
- ]
68
- sql = tablespec.create_table(dbt_sqlite, schemaname=None, tablename=tablename)
69
- curs = sdb.cursor()
70
- curs.execute(sql)
71
- # Export all rows of data
72
- columns = [dbt_sqlite.quoted(col) for col in hdrs]
73
- colspec = ",".join(columns)
74
- paramspec = ",".join(("?",) * len(columns))
75
- sql = f"insert into {qtable} ({colspec}) values ({paramspec});"
76
- n_chunks = math.ceil(len(rowdata) / chunksize)
77
- for i in range(n_chunks):
78
- start = i * chunksize
79
- end = start + chunksize
80
- curs.executemany(sql, rowdata[start:end])
96
+ bound.append(value if value is None or isinstance(value, _SQLITE_SAFE) else str(value))
97
+ converted.append(tuple(bound))
98
+ finally:
99
+ probe.close()
100
+
101
+ sdb = sqlite3.connect(outfile, isolation_level=None)
102
+ try:
103
+ sdb.execute("BEGIN")
104
+ exists = sdb.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (tablename,)).fetchone()
105
+ if exists is not None:
106
+ if append:
107
+ raise ErrInfo(type="error", other_msg=f"The table {tablename} already exists in {outfile}.")
108
+ sdb.execute(f"DROP TABLE {qtable}")
109
+ sdb.execute(create_sql)
110
+ columns = ",".join(dbt_sqlite.quoted(col) for col in hdrs)
111
+ parameters = ",".join("?" for _ in hdrs)
112
+ insert_sql = f"INSERT INTO {qtable} ({columns}) VALUES ({parameters})"
113
+ chunksize = 10000
114
+ for start in range(0, len(converted), chunksize):
115
+ sdb.executemany(insert_sql, converted[start : start + chunksize])
81
116
  sdb.commit()
82
- curs.close()
83
117
  finally:
118
+ # close() rolls back an open transaction, including on BaseException.
84
119
  sdb.close()
85
120
 
86
121
 
execsql/format.py CHANGED
@@ -1032,12 +1032,20 @@ def format_sql_block(
1032
1032
  layout_lines = [line for i, line in enumerate(lines) if line.strip() and i not in interior]
1033
1033
  base = min((len(line) - len(line.lstrip()) for line in layout_lines), default=0)
1034
1034
  target_prefix = " " * (depth * indent)
1035
- rebased = [line if i in interior else (line[base:] if line.strip() else "") for i, line in enumerate(lines)]
1035
+ rebased = []
1036
+ for i, line in enumerate(lines):
1037
+ if i in interior:
1038
+ rebased.append(line)
1039
+ elif line.strip():
1040
+ rebased.append(line[base:])
1041
+ else:
1042
+ rebased.append("")
1036
1043
 
1037
1044
  if not use_sql:
1038
- return [
1039
- line if i in interior else (target_prefix + line if line.strip() else "") for i, line in enumerate(rebased)
1040
- ]
1045
+ for i, line in enumerate(rebased):
1046
+ if i not in interior and line.strip():
1047
+ rebased[i] = target_prefix + line
1048
+ return rebased
1041
1049
 
1042
1050
  # When comments appear mid-statement, use the marker-based approach
1043
1051
  # which preserves both comments AND sqlglot formatting. When all
execsql/gui/desktop.py CHANGED
@@ -15,6 +15,7 @@ import time
15
15
  from typing import Any, cast
16
16
  from collections.abc import Callable
17
17
 
18
+ from execsql.exceptions import ScriptExit
18
19
  from execsql.gui.base import GuiBackend
19
20
 
20
21
  # ---------------------------------------------------------------------------
@@ -1513,7 +1514,7 @@ class _TkinterSyncQueue:
1513
1514
  except Exception as exc:
1514
1515
  result = {"error": str(exc), "button": None}
1515
1516
  if result is not None and spec.gui_type in self._EXIT_ON_CANCEL and result.get("button") is None:
1516
- raise SystemExit(2)
1517
+ raise ScriptExit(2)
1517
1518
  spec.return_queue.put(result if result is not None else {})
1518
1519
 
1519
1520
  def get_nowait(self) -> Any:
execsql/gui/tui.py CHANGED
@@ -23,6 +23,7 @@ import queue as _stdlib_queue
23
23
  import threading
24
24
  from typing import Any, Literal, cast
25
25
 
26
+ from execsql.exceptions import ScriptExit
26
27
  from execsql.gui.base import GuiBackend
27
28
 
28
29
  # ---------------------------------------------------------------------------
@@ -1449,7 +1450,7 @@ class _TextualSyncQueue:
1449
1450
  spec.return_queue.put({"error": str(exc), "button": None})
1450
1451
  return
1451
1452
  if result is not None and result.get("cancelled"):
1452
- raise SystemExit(2)
1453
+ raise ScriptExit(2)
1453
1454
  spec.return_queue.put(result if result is not None else {})
1454
1455
 
1455
1456
  def get_nowait(self) -> Any:
@@ -1604,7 +1605,7 @@ class ConsoleApp(App):
1604
1605
  if tid is not None:
1605
1606
  ctypes.pythonapi.PyThreadState_SetAsyncExc(
1606
1607
  ctypes.c_ulong(tid),
1607
- ctypes.py_object(SystemExit),
1608
+ ctypes.py_object(ScriptExit),
1608
1609
  )
1609
1610
  spec.return_queue.put(result if result is not None else {})
1610
1611
  else:
@@ -1651,6 +1652,8 @@ class ConsoleApp(App):
1651
1652
  pass # Widget may not be mounted yet.
1652
1653
 
1653
1654
  def action_request_quit(self) -> None:
1655
+ # A plain SystemExit, not ScriptExit: quitting while a statement runs
1656
+ # interrupts it, and it is rolled back like a failed one.
1654
1657
  if self._script_thread and self._script_thread.is_alive():
1655
1658
  tid = self._script_thread.ident
1656
1659
  if tid is not None:
execsql/importers/base.py CHANGED
@@ -1,15 +1,17 @@
1
- from __future__ import annotations
2
-
3
1
  """
4
2
  Shared data-import utilities for execsql.
5
3
 
6
- Provides :func:`import_data_table`, the common back-end used by all
7
- importer sub-modules. Given a :class:`~execsql.models.DataTable` and a
8
- :class:`~execsql.db.base.Database`, it generates and executes a
9
- ``CREATE TABLE`` statement and inserts all rows using the column type
10
- information inferred during scanning.
4
+ Provides :func:`import_data_table`, a common back-end for row-based
5
+ importers. CSV also has a separate file-oriented path. Input
6
+ headers are processed before target changes. For REPLACEMENT, the existing
7
+ target is dropped before source-row scanning/type inference; a later validation
8
+ failure does not guarantee that the previous table survives.
9
+ NEW/REPLACEMENT infer column declarations from the source; existing targets
10
+ convert values according to their destination column types.
11
11
  """
12
12
 
13
+ from __future__ import annotations
14
+
13
15
  from typing import Any
14
16
 
15
17
  from execsql.exceptions import ErrInfo
execsql/importers/json.py CHANGED
@@ -3,7 +3,7 @@ from __future__ import annotations
3
3
  """
4
4
  JSON import for execsql.
5
5
 
6
- Provides :func:`import_json`, used by ``IMPORT … FORMAT json``.
6
+ Provides :func:`import_json`, used by ``IMPORT … FROM JSON``.
7
7
  Supports JSON arrays of objects (``[{…}, …]``) and `JSON Lines
8
8
  <https://jsonlines.org/>`_ (JSONL, one object per line). Nested
9
9
  objects are flattened with dot-separated keys; nested arrays and
@@ -12,6 +12,7 @@ maps to a scalar database value.
12
12
  """
13
13
 
14
14
  import json
15
+ from decimal import Decimal
15
16
  from pathlib import Path
16
17
  from typing import Any
17
18
 
@@ -22,30 +23,51 @@ from execsql.importers.base import import_data_table
22
23
  __all__ = ["import_json"]
23
24
 
24
25
 
26
+ def _array_number(value: Any) -> Any:
27
+ """Keep the established JSON text representation of numbers inside arrays."""
28
+ return float(value) if isinstance(value, Decimal) else str(value)
29
+
30
+
25
31
  def _flatten(obj: Any, prefix: str = "", sep: str = ".") -> dict[str, Any]:
26
32
  """Recursively flatten a nested dict.
27
33
 
28
34
  Nested dicts produce dot-separated keys. All other compound values
29
35
  (lists, nested lists-of-dicts) are serialized as JSON strings so the
30
- result is always ``{str: scalar}``.
36
+ result is always ``{str: scalar}``. Distinct paths that produce the
37
+ same column name raise ErrInfo instead of discarding a value.
31
38
  """
32
39
  items: dict[str, Any] = {}
33
- if isinstance(obj, dict):
34
- for key, value in obj.items():
35
- new_key = f"{prefix}{sep}{key}" if prefix else key
40
+ source_paths: dict[str, tuple[str, ...]] = {}
41
+
42
+ def visit(current: Any, parent: str, path: tuple[str, ...]) -> None:
43
+ if not isinstance(current, dict):
44
+ return
45
+ for key, value in current.items():
46
+ column = f"{parent}{sep}{key}" if parent else key
47
+ source_path = (*path, key)
36
48
  if isinstance(value, dict):
37
- items.update(_flatten(value, new_key, sep))
38
- elif isinstance(value, list):
39
- # Serialize arrays as JSON strings — tables are flat.
40
- items[new_key] = json.dumps(value, default=str)
49
+ visit(value, column, source_path)
41
50
  else:
42
- items[new_key] = value
51
+ if column in items:
52
+ raise ErrInfo(
53
+ type="error",
54
+ other_msg=(
55
+ f"JSON field paths {json.dumps(source_paths[column])} and {json.dumps(source_path)} "
56
+ f"flatten to the same column {column!r}. Rename the conflicting keys."
57
+ ),
58
+ )
59
+ source_paths[column] = source_path
60
+ items[column] = json.dumps(value, default=_array_number) if isinstance(value, list) else value
61
+
62
+ visit(obj, prefix, ())
43
63
  return items
44
64
 
45
65
 
46
66
  def _parse_json_file(filename: str, encoding: str) -> list[dict[str, Any]]:
47
67
  """Read a JSON file and return a list of flat dicts.
48
68
 
69
+ Fractional and exponent scalar numbers retain their exact Decimal digits;
70
+ integers remain int. Arrays retain their established JSON text encoding.
49
71
  Accepts either a JSON array of objects or `JSON Lines
50
72
  <https://jsonlines.org/>`_ (JSONL). The JSONL path streams the
51
73
  file line-by-line so the raw text isn't buffered alongside the
@@ -69,7 +91,7 @@ def _parse_json_file(filename: str, encoding: str) -> list[dict[str, Any]]:
69
91
  # Standard JSON array — must buffer the whole file (json.loads
70
92
  # has no streaming mode; ijson would be needed for that).
71
93
  text = Path(filename).read_text(encoding=encoding)
72
- raw = json.loads(text)
94
+ raw = json.loads(text, parse_float=Decimal)
73
95
  if not isinstance(raw, list):
74
96
  raise ErrInfo(type="error", other_msg="JSON file root is not an array of objects.")
75
97
  records = raw
@@ -82,7 +104,7 @@ def _parse_json_file(filename: str, encoding: str) -> list[dict[str, Any]]:
82
104
  if not line:
83
105
  continue
84
106
  try:
85
- obj = json.loads(line)
107
+ obj = json.loads(line, parse_float=Decimal)
86
108
  except json.JSONDecodeError as exc:
87
109
  raise ErrInfo(
88
110
  type="error",
@@ -126,7 +148,9 @@ def import_json(
126
148
 
127
149
  Objects are flattened so that nested keys become dot-separated column
128
150
  names (e.g. ``address.city``). Arrays within objects are stored as
129
- JSON strings.
151
+ JSON strings. Colliding paths within a record are rejected before
152
+ any table is created, replaced or populated. Fractional and exponent scalar
153
+ numbers are parsed as Decimal before table inference and target conversion.
130
154
  """
131
155
  from execsql.utils.errors import exception_desc
132
156
 
execsql/importers/ods.py CHANGED
@@ -15,9 +15,23 @@ from execsql.exceptions import ErrInfo
15
15
  from execsql.db.base import Database
16
16
  from execsql.exporters.ods import OdsFile
17
17
  from execsql.importers.base import import_data_table
18
+ from execsql.utils.fileio import check_zip_decompression_ratio
19
+ from execsql.utils.spreadsheet import SheetBudget
18
20
  import execsql.state as _state
19
21
 
20
- __all__ = ["importods", "ods_data"]
22
+ __all__ = ["check_ods_archive", "importods", "ods_data"]
23
+
24
+
25
+ def check_ods_archive(filename: str) -> None:
26
+ """Apply ODS archive limits before any reader, including sheet-name discovery."""
27
+ budget = SheetBudget(_state.conf, filename, "<workbook>")
28
+ check_zip_decompression_ratio(
29
+ filename,
30
+ max_uncompressed_mb=budget.limits["ods_max_xml_mb"],
31
+ max_ratio=budget.limits["ods_max_compression_ratio"],
32
+ xml_only=True,
33
+ limit_names=("ods_max_xml_mb", "ods_max_compression_ratio"),
34
+ )
21
35
 
22
36
 
23
37
  def ods_data(
@@ -30,15 +44,21 @@ def ods_data(
30
44
 
31
45
  conf = _state.conf
32
46
 
47
+ budget = SheetBudget(conf, filename, sheetname)
48
+ check_ods_archive(filename)
33
49
  wbk = OdsFile()
34
50
  try:
35
51
  wbk.open(filename)
36
52
  except Exception as e:
37
53
  raise ErrInfo(type="cmd", other_msg=f"{filename} is not a valid OpenDocument spreadsheet.") from e
38
54
  try:
39
- alldata = wbk.sheet_data(sheetname, junk_header_rows)
55
+ alldata = wbk.sheet_data(sheetname, junk_header_rows, budget=budget)
56
+ except ErrInfo:
57
+ raise
40
58
  except Exception as e:
41
59
  raise ErrInfo(type="cmd", other_msg=f"{sheetname} is not a worksheet in {filename}.") from e
60
+ finally:
61
+ wbk.close()
42
62
  if not alldata:
43
63
  raise ErrInfo(type="error", other_msg=f"The input file {filename}, sheet {sheetname} contains no data.")
44
64
  colhdrs = alldata[0]
execsql/lsp/document.py CHANGED
@@ -173,7 +173,12 @@ def file_facts(source: str, path: str | None, base_dir: str | None = None) -> Fi
173
173
  steps: list[_Define | _ScriptDef | _ScriptCall | _Include] = []
174
174
  reads_ini = False
175
175
  lines = source.splitlines()
176
- script_dir = Path(path).parent if path else (Path(base_dir) if base_dir else None)
176
+ if path:
177
+ script_dir = Path(path).parent
178
+ elif base_dir:
179
+ script_dir = Path(base_dir)
180
+ else:
181
+ script_dir = None
177
182
  tree = parse_string(source, path or "<untitled>", errors=[])
178
183
 
179
184
  for node in tree.walk():
execsql/lsp/hover.py CHANGED
@@ -106,6 +106,7 @@ def hover(index: ScriptIndex, source: str, line: int, character: int) -> types.H
106
106
  start = m.end("lead")
107
107
  written = re.match(r"\s*".join(map(re.escape, entry.keyword.split())), text[start:], re.I)
108
108
  first_word = re.match(r"[A-Za-z_]+", text[start:])
109
- keyword_end = start + (written.end() if written else first_word.end() if first_word else 0)
109
+ keyword = written or first_word
110
+ keyword_end = start + (keyword.end() if keyword else 0)
110
111
  body = f"**{entry.keyword}** — {entry.category} metacommand\n\n{entry.summary}\n\n{_syntax_block(entry.forms)}\n\n[Reference]({entry.url})"
111
112
  return types.Hover(contents=markdown(body), range=span(line, start, keyword_end))
@@ -401,7 +401,9 @@ def xf_columnexists(**kwargs: Any) -> bool:
401
401
 
402
402
  def xf_aliasdefined(**kwargs: Any) -> bool:
403
403
  alias = kwargs["alias"]
404
- return alias in _state.dbs.aliases()
404
+ # CONNECT stores the alias lowercased; match it case-insensitively,
405
+ # as USE and make_current already do.
406
+ return alias.lower() in _state.dbs.aliases()
405
407
 
406
408
 
407
409
  def xf_metacommanderror(**kwargs: Any) -> bool:
@@ -442,6 +442,17 @@ def x_connect_dsn(**kwargs: Any) -> None:
442
442
  return None
443
443
 
444
444
 
445
+ def _set_current_db_vars(db_alias: str) -> None:
446
+ """Point $CURRENT_DBMS, $CURRENT_DATABASE and $DB_SERVER at *db_alias*.
447
+
448
+ Shared by ``USE`` and the ``DISCONNECT`` that switches the run back to
449
+ the initial database, so the variables always name the database in use.
450
+ """
451
+ _state.subvars.add_substitution("$CURRENT_DBMS", _state.dbs.aliased_as(db_alias).type.dbms_id)
452
+ _state.subvars.add_substitution("$CURRENT_DATABASE", _state.dbs.aliased_as(db_alias).name())
453
+ _state.subvars.add_substitution("$DB_SERVER", _state.dbs.aliased_as(db_alias).server_name)
454
+
455
+
445
456
  def x_use(**kwargs: Any) -> None:
446
457
  db_alias = kwargs["db_alias"].lower()
447
458
  if db_alias not in _state.dbs.aliases():
@@ -452,27 +463,32 @@ def x_use(**kwargs: Any) -> None:
452
463
  )
453
464
  _state.dbs.make_current(db_alias)
454
465
  _state.exec_log.log_db_connect(_state.dbs.current())
455
- _state.subvars.add_substitution("$CURRENT_DBMS", _state.dbs.aliased_as(db_alias).type.dbms_id)
456
- _state.subvars.add_substitution("$CURRENT_DATABASE", _state.dbs.aliased_as(db_alias).name())
457
- _state.subvars.add_substitution("$DB_SERVER", _state.dbs.aliased_as(db_alias).server_name)
466
+ _set_current_db_vars(db_alias)
458
467
  return None
459
468
 
460
469
 
461
470
  def x_disconnect(**kwargs: Any) -> None:
462
471
  alias = kwargs["alias"]
463
472
  current_alias = _state.dbs.current_alias()
464
- if alias is None:
465
- alias = _state.dbs.current_alias()
466
- if alias.lower() == "initial":
467
- raise ErrInfo(type="error", other_msg="You may not disconnect from the initial database used.")
468
- if _state.status.batch.uses_db(alias):
469
- raise ErrInfo(
470
- type="error",
471
- other_msg="You may not disconnect from a database that is currently used in a batch.",
472
- )
473
+ # Aliases are stored lowercase (DatabasePool.add), as USE expects.
474
+ alias = current_alias if alias is None else alias.lower()
475
+ # type="cmd": reported as a metacommand error; the engine adds the
476
+ # "Metacommand: DISCONNECT ..." line itself.
477
+ if alias == "initial":
478
+ raise ErrInfo(type="cmd", other_msg="You may not disconnect from the initial database used.")
479
+ if alias not in _state.dbs.aliases():
480
+ raise ErrInfo(type="cmd", other_msg=f"Unrecognized database alias: {alias}.")
481
+ if _state.status.batch.uses_db(_state.dbs.aliased_as(alias)):
482
+ raise ErrInfo(type="cmd", other_msg="You may not disconnect from a database that is currently used in a batch.")
473
483
  _state.exec_log.log_status_info(f"Disconnecting from database with alias '{alias}'")
474
484
  if alias == current_alias:
485
+ # Switch the run back to the initial database before the entry is
486
+ # removed, and share USE's variable updates, so $CURRENT_DATABASE,
487
+ # $CURRENT_DBMS and $DB_SERVER name the database in use, not the
488
+ # one just closed, and the log's connection record moves too.
475
489
  _state.dbs.make_current("initial")
490
+ _set_current_db_vars("initial")
491
+ _state.exec_log.log_db_connect(_state.dbs.current())
476
492
  _state.dbs.disconnect(alias)
477
493
 
478
494
 
@@ -12,6 +12,8 @@ from __future__ import annotations
12
12
 
13
13
  import re
14
14
 
15
+ from execsql.utils.spreadsheet import INPUT_LIMIT_DEFAULTS
16
+
15
17
  # Handler imports — grouped by module for readability.
16
18
  from execsql.metacommands.connect import (
17
19
  x_autocommit_off,
@@ -125,6 +127,7 @@ from execsql.metacommands.io import (
125
127
  x_import_ods_pattern,
126
128
  x_import_parquet,
127
129
  x_import_row_buffer,
130
+ x_input_limit,
128
131
  x_import_xls,
129
132
  x_import_xls_pattern,
130
133
  x_include,
@@ -713,6 +716,13 @@ def build_dispatch_table() -> MetaCommandList:
713
716
  description="MAX_INT",
714
717
  category="config_option",
715
718
  )
719
+ for name in INPUT_LIMIT_DEFAULTS:
720
+ mcl.add(
721
+ rf"^\s*CONFIG\s+(?P<name>{name.upper()})\s+(?P<value>[0-9]+)\s*$",
722
+ x_input_limit,
723
+ description=name.upper(),
724
+ category="config_option",
725
+ )
716
726
  mcl.add(
717
727
  r"^\s*CONFIG\s+IMPORT_ROW_BUFFER\s+(?P<rows>[1-9][0-9]*)\s*$",
718
728
  x_import_row_buffer,
@@ -39,6 +39,7 @@ from execsql.metacommands.io_import import ( # noqa: F401
39
39
  x_import_ods_pattern,
40
40
  x_import_parquet,
41
41
  x_import_row_buffer,
42
+ x_input_limit,
42
43
  x_import_xls,
43
44
  x_show_progress,
44
45
  x_import_xls_pattern,
@@ -11,8 +11,7 @@
11
11
  - ``x_export_row_buffer`` — CONFIG EXPORT_ROW_BUFFER (set the per-export
12
12
  row-buffer size).
13
13
 
14
- Per-format dispatch happens inside ``x_export``'s
15
- ``if filefmt in (...): ... elif ...`` chain, which calls the
14
+ Per-format dispatch happens inside ``_dispatch_format()``, which calls the
16
15
  appropriate writer in :mod:`execsql.exporters`.
17
16
  """
18
17