execsql2 2.37.0__py3-none-any.whl → 2.39.1__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.
- execsql/api.py +95 -45
- execsql/cli/commands/run.py +2 -2
- execsql/cli/lint.py +46 -9
- execsql/cli/run.py +128 -56
- execsql/config.py +7 -0
- execsql/data/execsql.conf.template +26 -2
- execsql/db/base.py +61 -24
- execsql/db/dsn.py +2 -3
- execsql/db/duckdb.py +43 -4
- execsql/db/firebird.py +39 -22
- execsql/db/mysql.py +22 -23
- execsql/db/oracle.py +11 -4
- execsql/db/postgres.py +15 -20
- execsql/db/sqlite.py +117 -10
- execsql/db/sqlserver.py +11 -7
- execsql/db/target_types.py +151 -4
- execsql/debug/repl.py +2 -1
- execsql/exceptions.py +19 -0
- execsql/exporters/html.py +26 -42
- execsql/exporters/ods.py +93 -21
- execsql/exporters/sqlite.py +82 -47
- execsql/format.py +12 -4
- execsql/gui/__init__.py +13 -10
- execsql/gui/base.py +131 -8
- execsql/gui/console.py +77 -30
- execsql/gui/desktop.py +290 -119
- execsql/gui/tui.py +384 -129
- execsql/importers/base.py +15 -19
- execsql/importers/csv.py +3 -5
- execsql/importers/json.py +37 -13
- execsql/importers/ods.py +22 -2
- execsql/lsp/document.py +6 -1
- execsql/lsp/hover.py +2 -1
- execsql/metacommands/conditions.py +3 -1
- execsql/metacommands/connect.py +18 -3
- execsql/metacommands/control.py +20 -5
- execsql/metacommands/data.py +11 -9
- execsql/metacommands/dispatch.py +10 -0
- execsql/metacommands/io.py +1 -0
- execsql/metacommands/io_export.py +1 -2
- execsql/metacommands/io_fileops.py +27 -91
- execsql/metacommands/io_import.py +26 -8
- execsql/metacommands/io_write.py +0 -9
- execsql/metacommands/prompt.py +94 -42
- execsql/metacommands/reference.py +7 -0
- execsql/metacommands/system.py +0 -2
- execsql/parser.py +132 -45
- execsql/script/control.py +17 -7
- execsql/script/engine.py +1 -1
- execsql/script/executor.py +184 -37
- execsql/script/preparation.py +42 -0
- execsql/types.py +6 -5
- execsql/utils/auth.py +11 -6
- execsql/utils/errors.py +2 -2
- execsql/utils/fileio.py +342 -134
- execsql/utils/gui.py +162 -53
- execsql/utils/spreadsheet.py +74 -0
- execsql/utils/strings.py +13 -7
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/README.md +5 -5
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/example_config_prompt.sql +2 -2
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/METADATA +32 -119
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/RECORD +77 -75
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/WHEEL +1 -1
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/config_settings.sqlite +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/make_config_db.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/md_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/md_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/md_upsert.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/pg_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/pg_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/script_template.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/ss_compare.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/ss_glossary.sql +0 -0
- {execsql2-2.37.0.data → execsql2-2.39.1.data}/data/execsql2_extras/ss_upsert.sql +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/entry_points.txt +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/licenses/LICENSE.txt +0 -0
- {execsql2-2.37.0.dist-info → execsql2-2.39.1.dist-info}/licenses/NOTICE +0 -0
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`,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
@@ -23,22 +25,16 @@ def refuse_existing_table(db: Database, schema_name: str | None, table_name: str
|
|
|
23
25
|
"""Stop a ``TO NEW`` before any DDL when the target table already exists.
|
|
24
26
|
|
|
25
27
|
Only the schema the CREATE TABLE will use counts: for an unqualified
|
|
26
|
-
name, the database's current schema (MySQL: database).
|
|
27
|
-
|
|
28
|
-
|
|
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.
|
|
29
31
|
"""
|
|
30
32
|
lookup_schema = schema_name
|
|
31
33
|
if schema_name is None and not db.unqualified_lookup_is_scoped:
|
|
32
|
-
|
|
33
|
-
lookup_schema = db.current_schema()
|
|
34
|
-
except Exception:
|
|
35
|
-
lookup_schema = None
|
|
34
|
+
lookup_schema = db.current_schema()
|
|
36
35
|
if lookup_schema is None:
|
|
37
36
|
return # An unqualified lookup would search every schema.
|
|
38
|
-
|
|
39
|
-
exists = db.table_exists_where_created(table_name, lookup_schema)
|
|
40
|
-
except Exception:
|
|
41
|
-
return # The database cannot say; CREATE TABLE reports any conflict.
|
|
37
|
+
exists = db.table_exists_where_created(table_name, lookup_schema)
|
|
42
38
|
if exists:
|
|
43
39
|
name = db.schema_qualified_table_name(schema_name, table_name)
|
|
44
40
|
raise ErrInfo(
|
|
@@ -122,7 +118,7 @@ def import_data_table(
|
|
|
122
118
|
# Most adapters delay commit until after populating the table; adapters
|
|
123
119
|
# that need DDL committed first (Firebird) must do it here.
|
|
124
120
|
if db.needs_explicit_commit_after_ddl():
|
|
125
|
-
db.
|
|
121
|
+
db.commit_now()
|
|
126
122
|
except Exception as e:
|
|
127
123
|
raise ErrInfo(
|
|
128
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
|
-
#
|
|
70
|
-
|
|
71
|
-
|
|
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",
|
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 …
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
execsql/metacommands/connect.py
CHANGED
|
@@ -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,9 +463,7 @@ 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
|
-
|
|
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
|
|
|
@@ -473,7 +482,13 @@ def x_disconnect(**kwargs: Any) -> None:
|
|
|
473
482
|
raise ErrInfo(type="cmd", other_msg="You may not disconnect from a database that is currently used in a batch.")
|
|
474
483
|
_state.exec_log.log_status_info(f"Disconnecting from database with alias '{alias}'")
|
|
475
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.
|
|
476
489
|
_state.dbs.make_current("initial")
|
|
490
|
+
_set_current_db_vars("initial")
|
|
491
|
+
_state.exec_log.log_db_connect(_state.dbs.current())
|
|
477
492
|
_state.dbs.disconnect(alias)
|
|
478
493
|
|
|
479
494
|
|
execsql/metacommands/control.py
CHANGED
|
@@ -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 =
|
|
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}")
|
|
@@ -199,7 +210,7 @@ def x_break(**kwargs: Any) -> None:
|
|
|
199
210
|
def x_wait_until(**kwargs: Any) -> None:
|
|
200
211
|
countdown = int(kwargs["seconds"])
|
|
201
212
|
while countdown > 0:
|
|
202
|
-
if
|
|
213
|
+
if _condition_value(kwargs["condition"]):
|
|
203
214
|
return
|
|
204
215
|
time.sleep(1)
|
|
205
216
|
countdown -= 1
|
|
@@ -279,6 +290,10 @@ def x_halt_msg(**kwargs: Any) -> None:
|
|
|
279
290
|
"help_url": None,
|
|
280
291
|
}
|
|
281
292
|
_state.gui_manager_queue.put(GuiSpec(GUI_HALT, gui_args, return_queue))
|
|
282
|
-
|
|
293
|
+
try:
|
|
294
|
+
get_gui_response(return_queue, GUI_HALT)
|
|
295
|
+
except ConsoleUIError as error:
|
|
296
|
+
# Failure to display a halt message cannot allow the script to resume.
|
|
297
|
+
raise ErrInfo(type="cmd", other_msg=str(error), always_halt=True) from error
|
|
283
298
|
_state.exec_log.log_exit_halt(*current_script_line(), msg=errmsg)
|
|
284
299
|
exit_now(errlevel, None)
|
execsql/metacommands/data.py
CHANGED
|
@@ -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
|
-
"
|
|
239
|
-
"
|
|
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
|
|
243
|
+
user_response = get_gui_response(return_queue, GUI_SELECTSUB)
|
|
244
244
|
btn_val = user_response["button"]
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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
|
|
252
|
+
if btn_val is None:
|
|
251
253
|
if _state.status.cancel_halt:
|
|
252
254
|
_state.exec_log.log_exit_halt(
|
|
253
255
|
script,
|
execsql/metacommands/dispatch.py
CHANGED
|
@@ -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,
|
execsql/metacommands/io.py
CHANGED
|
@@ -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 ``
|
|
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
|
|
|
@@ -18,6 +18,7 @@ from shutil import copyfileobj
|
|
|
18
18
|
from typing import Any, Literal
|
|
19
19
|
|
|
20
20
|
import execsql.state as _state
|
|
21
|
+
from execsql.db.base import Database
|
|
21
22
|
from execsql.exceptions import ErrInfo
|
|
22
23
|
from execsql.importers.base import refuse_existing_table
|
|
23
24
|
from execsql.models import DataTable
|
|
@@ -102,73 +103,8 @@ def x_copy(**kwargs: Any) -> None:
|
|
|
102
103
|
db1 = _state.dbs.aliased_as(alias1)
|
|
103
104
|
db2 = _state.dbs.aliased_as(alias2)
|
|
104
105
|
tbl1 = db1.schema_qualified_table_name(schema1, table1)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
# (PostgreSQL reads Orders as orders), and the SELECT below names a missing
|
|
108
|
-
# source in the database's own words.
|
|
109
|
-
if new_tbl2 and new_tbl2 == "new":
|
|
110
|
-
refuse_existing_table(db2, schema2, table2)
|
|
111
|
-
select_stmt = f"select * from {tbl1};"
|
|
112
|
-
|
|
113
|
-
get_ts_tablespec = None
|
|
114
|
-
rows_to_close = None
|
|
115
|
-
spool: _RowSpool | None = None
|
|
116
|
-
rows_for_insert: Any
|
|
117
|
-
|
|
118
|
-
if new_tbl2:
|
|
119
|
-
try:
|
|
120
|
-
hdrs, rows = db1.select_rowsource(select_stmt, decode_bytes=False)
|
|
121
|
-
except ErrInfo:
|
|
122
|
-
raise
|
|
123
|
-
except Exception as e:
|
|
124
|
-
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
125
|
-
rows_to_close = rows
|
|
126
|
-
# Values come from a database cursor, so a text value stays text.
|
|
127
|
-
spool = _RowSpool(rows)
|
|
128
|
-
get_ts_tablespec = DataTable(hdrs, spool, infer_strings=False)
|
|
129
|
-
rows_for_insert = spool.replay()
|
|
130
|
-
tbl_desc = get_ts_tablespec
|
|
131
|
-
create_tbl = tbl_desc.create_table(db2.type, schema2, table2)
|
|
132
|
-
if new_tbl2 == "replacement":
|
|
133
|
-
try:
|
|
134
|
-
db2.drop_table_for_replacement(schema2, table2)
|
|
135
|
-
except ErrInfo:
|
|
136
|
-
raise
|
|
137
|
-
except Exception as e:
|
|
138
|
-
raise ErrInfo(
|
|
139
|
-
type="db",
|
|
140
|
-
other_msg=f"Could not drop existing table ({tbl2}) for COPY metacommand",
|
|
141
|
-
exception_msg=exception_desc(),
|
|
142
|
-
) from e
|
|
143
|
-
db2.execute(create_tbl)
|
|
144
|
-
if db2.needs_explicit_commit_after_ddl():
|
|
145
|
-
db2.execute("COMMIT;")
|
|
146
|
-
else:
|
|
147
|
-
try:
|
|
148
|
-
hdrs, rows = db1.select_rowsource(select_stmt, decode_bytes=False)
|
|
149
|
-
except ErrInfo:
|
|
150
|
-
raise
|
|
151
|
-
except Exception as e:
|
|
152
|
-
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
153
|
-
rows_to_close = rows
|
|
154
|
-
rows_for_insert = rows
|
|
155
|
-
|
|
156
|
-
def get_ts() -> DataTable:
|
|
157
|
-
nonlocal get_ts_tablespec
|
|
158
|
-
if get_ts_tablespec is None:
|
|
159
|
-
ts_hdrs, ts_rows = db1.select_rowsource(select_stmt, decode_bytes=False)
|
|
160
|
-
get_ts_tablespec = DataTable(ts_hdrs, ts_rows, infer_strings=False)
|
|
161
|
-
return get_ts_tablespec
|
|
162
|
-
|
|
163
|
-
try:
|
|
164
|
-
db2.populate_table(schema2, table2, rows_for_insert, hdrs, get_ts, existing_table=not new_tbl2)
|
|
165
|
-
db2.commit()
|
|
166
|
-
except BaseException:
|
|
167
|
-
_close_rowsource(rows_to_close)
|
|
168
|
-
raise
|
|
169
|
-
finally:
|
|
170
|
-
if spool is not None:
|
|
171
|
-
spool.close()
|
|
106
|
+
# Let the source database resolve unquoted names and report missing tables.
|
|
107
|
+
_copy_rows(db1, db2, f"select * from {tbl1};", schema2, table2, new_tbl2)
|
|
172
108
|
|
|
173
109
|
|
|
174
110
|
def x_copy_query(**kwargs: Any) -> None:
|
|
@@ -193,29 +129,38 @@ def x_copy_query(**kwargs: Any) -> None:
|
|
|
193
129
|
)
|
|
194
130
|
db1 = _state.dbs.aliased_as(alias1)
|
|
195
131
|
db2 = _state.dbs.aliased_as(alias2)
|
|
132
|
+
_copy_rows(db1, db2, select_stmt, schema2, table2, new_tbl2)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _copy_rows(
|
|
136
|
+
db1: Database,
|
|
137
|
+
db2: Database,
|
|
138
|
+
select_stmt: str,
|
|
139
|
+
schema2: str | None,
|
|
140
|
+
table2: str,
|
|
141
|
+
new_tbl2: str | None,
|
|
142
|
+
) -> None:
|
|
143
|
+
"""Copy a query's rows, sharing table creation, type inference and spool cleanup."""
|
|
196
144
|
tbl2 = db2.schema_qualified_table_name(schema2, table2)
|
|
197
|
-
if new_tbl2
|
|
145
|
+
if new_tbl2 == "new":
|
|
198
146
|
refuse_existing_table(db2, schema2, table2)
|
|
199
147
|
|
|
200
|
-
get_ts_tablespec = None
|
|
201
|
-
rows_to_close = None
|
|
148
|
+
get_ts_tablespec: DataTable | None = None
|
|
202
149
|
spool: _RowSpool | None = None
|
|
203
|
-
|
|
150
|
+
try:
|
|
151
|
+
hdrs, rows = db1.select_rowsource(select_stmt, decode_bytes=False)
|
|
152
|
+
except ErrInfo:
|
|
153
|
+
raise
|
|
154
|
+
except Exception as e:
|
|
155
|
+
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
156
|
+
rows_for_insert: Iterator[Any] = rows
|
|
204
157
|
|
|
205
158
|
if new_tbl2:
|
|
206
|
-
try:
|
|
207
|
-
hdrs, rows = db1.select_rowsource(select_stmt, decode_bytes=False)
|
|
208
|
-
except ErrInfo:
|
|
209
|
-
raise
|
|
210
|
-
except Exception as e:
|
|
211
|
-
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
212
|
-
rows_to_close = rows
|
|
213
159
|
# Values come from a database cursor, so a text value stays text.
|
|
214
160
|
spool = _RowSpool(rows)
|
|
215
161
|
get_ts_tablespec = DataTable(hdrs, spool, infer_strings=False)
|
|
216
162
|
rows_for_insert = spool.replay()
|
|
217
|
-
|
|
218
|
-
create_tbl = tbl_desc.create_table(db2.type, schema2, table2)
|
|
163
|
+
create_tbl = get_ts_tablespec.create_table(db2.type, schema2, table2)
|
|
219
164
|
if new_tbl2 == "replacement":
|
|
220
165
|
try:
|
|
221
166
|
db2.drop_table_for_replacement(schema2, table2)
|
|
@@ -229,16 +174,7 @@ def x_copy_query(**kwargs: Any) -> None:
|
|
|
229
174
|
) from e
|
|
230
175
|
db2.execute(create_tbl)
|
|
231
176
|
if db2.needs_explicit_commit_after_ddl():
|
|
232
|
-
db2.
|
|
233
|
-
else:
|
|
234
|
-
try:
|
|
235
|
-
hdrs, rows = db1.select_rowsource(select_stmt, decode_bytes=False)
|
|
236
|
-
except ErrInfo:
|
|
237
|
-
raise
|
|
238
|
-
except Exception as e:
|
|
239
|
-
raise ErrInfo("db", select_stmt, exception_msg=exception_desc()) from e
|
|
240
|
-
rows_to_close = rows
|
|
241
|
-
rows_for_insert = rows
|
|
177
|
+
db2.commit_now()
|
|
242
178
|
|
|
243
179
|
def get_ts() -> DataTable:
|
|
244
180
|
nonlocal get_ts_tablespec
|
|
@@ -251,7 +187,7 @@ def x_copy_query(**kwargs: Any) -> None:
|
|
|
251
187
|
db2.populate_table(schema2, table2, rows_for_insert, hdrs, get_ts, existing_table=not new_tbl2)
|
|
252
188
|
db2.commit()
|
|
253
189
|
except BaseException:
|
|
254
|
-
_close_rowsource(
|
|
190
|
+
_close_rowsource(rows)
|
|
255
191
|
raise
|
|
256
192
|
finally:
|
|
257
193
|
if spool is not None:
|