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
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
#access_username=
|
|
43
43
|
|
|
44
44
|
# Whether to prompt for a password.
|
|
45
|
+
# Server/DSN usernames are retained when prompting is disabled.
|
|
45
46
|
# Values: Yes or No. Default: Yes.
|
|
46
47
|
#password_prompt=Yes
|
|
47
48
|
|
|
@@ -77,6 +78,21 @@
|
|
|
77
78
|
[input]
|
|
78
79
|
# Settings for handling imported data.
|
|
79
80
|
|
|
81
|
+
# ODS archive limits, checked before parsing. XML total is in MiB; the
|
|
82
|
+
# compression ratio applies per archive member. 0 disables a limit.
|
|
83
|
+
#ods_max_xml_mb=100
|
|
84
|
+
#ods_max_compression_ratio=100
|
|
85
|
+
|
|
86
|
+
# Materialized spreadsheet limits (currently enforced for ODS imports).
|
|
87
|
+
# Rows include the header after SKIP; cells include rectangular padding.
|
|
88
|
+
# Interior blanks count; trailing empty filler does not. Text counts include
|
|
89
|
+
# repeated/spanned copies. 0 disables an individual limit; values are never truncated.
|
|
90
|
+
#import_max_rows=1048576
|
|
91
|
+
#import_max_columns=16384
|
|
92
|
+
#import_max_cells=10000000
|
|
93
|
+
#import_max_cell_chars=1048576
|
|
94
|
+
#import_max_text_chars=268435456
|
|
95
|
+
|
|
80
96
|
# MS Access: create columns for decimal data as NUMERIC(precision,scale)
|
|
81
97
|
# (Yes) or as DOUBLE (No) when IMPORT or COPY creates a table.
|
|
82
98
|
# Values: Yes or No. Default: No.
|
|
@@ -199,6 +215,9 @@
|
|
|
199
215
|
#css_styles=
|
|
200
216
|
|
|
201
217
|
# Seconds to keep retrying when an output file has an access conflict.
|
|
218
|
+
# Background-writer waits, including blocked writes/closes, end after this
|
|
219
|
+
# interval plus 5 seconds (at most 10 seconds on a halt). On timeout, queued
|
|
220
|
+
# output is discarded; the one I/O call already in progress may finish later.
|
|
202
221
|
# Value: A positive non-zero number. Default: 600.
|
|
203
222
|
#outfile_open_timeout=600
|
|
204
223
|
|
|
@@ -225,11 +244,13 @@
|
|
|
225
244
|
# 3: open a console window immediately
|
|
226
245
|
#gui_level=0
|
|
227
246
|
|
|
228
|
-
# The height (in lines) of the console
|
|
247
|
+
# The height (in text lines) of the native Tkinter console.
|
|
248
|
+
# Textual/console backends use the host terminal dimensions.
|
|
229
249
|
# Value: An integer >= 5. Default: 25.
|
|
230
250
|
#console_height=25
|
|
231
251
|
|
|
232
|
-
# The width (in characters) of the console
|
|
252
|
+
# The width (in characters) of the native Tkinter console.
|
|
253
|
+
# Textual/console backends use the host terminal dimensions.
|
|
233
254
|
# Value: An integer >= 20. Default: 100.
|
|
234
255
|
#console_width=100
|
|
235
256
|
|
|
@@ -305,6 +326,7 @@
|
|
|
305
326
|
# Whether to allow the SYSTEM_CMD (SHELL) metacommand.
|
|
306
327
|
# Set to No to prevent scripts from executing OS commands.
|
|
307
328
|
# Also controllable via --no-system-cmd CLI flag.
|
|
329
|
+
# Library API: omitted/None inherits this value; an explicit boolean overrides it.
|
|
308
330
|
# Values: Yes or No. Default: Yes.
|
|
309
331
|
#allow_system_cmd=Yes
|
|
310
332
|
|
|
@@ -317,12 +339,14 @@
|
|
|
317
339
|
# Whether to allow the RM_FILE metacommand (which deletes a file).
|
|
318
340
|
# Set to No to prevent scripts from deleting files.
|
|
319
341
|
# Also controllable via --no-rm-file CLI flag.
|
|
342
|
+
# Library API: omitted/None inherits this value; an explicit boolean overrides it.
|
|
320
343
|
# Values: Yes or No. Default: Yes.
|
|
321
344
|
#allow_rm_file=Yes
|
|
322
345
|
|
|
323
346
|
# Whether to allow the SERVE metacommand (which copies a file to stdout
|
|
324
347
|
# with HTTP headers, for CGI use). Set to No to disable.
|
|
325
348
|
# Also controllable via --no-serve CLI flag.
|
|
349
|
+
# Library API: omitted/None inherits this value; an explicit boolean overrides it.
|
|
326
350
|
# Values: Yes or No. Default: Yes.
|
|
327
351
|
#allow_serve=Yes
|
|
328
352
|
|
execsql/db/base.py
CHANGED
|
@@ -24,7 +24,8 @@ from collections.abc import Callable, Generator, Iterator
|
|
|
24
24
|
|
|
25
25
|
from execsql.exceptions import ErrInfo
|
|
26
26
|
from execsql.utils.errors import exception_desc
|
|
27
|
-
from execsql.db.target_types import ColumnKinds, existing_table_converter, kind_for_python_type
|
|
27
|
+
from execsql.db.target_types import ColumnKinds, ColumnMetadata, existing_table_converter, kind_for_python_type
|
|
28
|
+
from execsql.types import DT_Decimal
|
|
28
29
|
from execsql.db.tiers import SupportTier, announce_tier
|
|
29
30
|
from execsql.types import DataType
|
|
30
31
|
import execsql.state as _state
|
|
@@ -285,6 +286,9 @@ class Database(ABC):
|
|
|
285
286
|
#: (an ODBC DSN without transactions): nothing can be held back.
|
|
286
287
|
driver_autocommits: bool = False
|
|
287
288
|
|
|
289
|
+
#: Native Decimal parameter capacity for exact binding; None leaves it unchanged.
|
|
290
|
+
decimal_binding_precision: int | None = None
|
|
291
|
+
|
|
288
292
|
def holding(self) -> bool:
|
|
289
293
|
"""Whether statements on this connection wait for a commit.
|
|
290
294
|
|
|
@@ -360,6 +364,15 @@ class Database(ABC):
|
|
|
360
364
|
what was just reported as failed.
|
|
361
365
|
"""
|
|
362
366
|
if self.conn and self.autocommit:
|
|
367
|
+
self.commit_now()
|
|
368
|
+
|
|
369
|
+
def commit_now(self) -> None:
|
|
370
|
+
"""Commit even under AUTOCOMMIT OFF, rolling back a failed commit.
|
|
371
|
+
|
|
372
|
+
Used only where a DBMS requires an immediate DDL commit before the
|
|
373
|
+
next statement can use the change. This commits the whole transaction.
|
|
374
|
+
"""
|
|
375
|
+
if self.conn:
|
|
363
376
|
try:
|
|
364
377
|
self.conn.commit()
|
|
365
378
|
except BaseException:
|
|
@@ -372,13 +385,15 @@ class Database(ABC):
|
|
|
372
385
|
try:
|
|
373
386
|
self.conn.rollback()
|
|
374
387
|
except Exception as e:
|
|
375
|
-
# Best-effort; connection may already be closed.
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
388
|
+
# Best-effort; connection may already be closed.
|
|
389
|
+
self._log_failed_rollback(e)
|
|
390
|
+
|
|
391
|
+
def _log_failed_rollback(self, e: Exception) -> None:
|
|
392
|
+
"""Log a rollback that failed, so a cascading failure isn't invisible in a CI / cron log."""
|
|
393
|
+
if _state.exec_log is not None:
|
|
394
|
+
_state.exec_log.log_status_info(
|
|
395
|
+
f"Rollback failed on {self.__class__.__name__}: {e!r}",
|
|
396
|
+
)
|
|
382
397
|
|
|
383
398
|
def needs_explicit_commit_after_ddl(self) -> bool:
|
|
384
399
|
"""Return True if this adapter's driver does NOT auto-commit DDL.
|
|
@@ -622,16 +637,24 @@ class Database(ABC):
|
|
|
622
637
|
*sq_name* is schema-qualified and quoted, as for an ``INSERT``, and
|
|
623
638
|
is looked up the way the ``INSERT`` finds it. Keys are lower-cased
|
|
624
639
|
column names; a column with no fitting execsql type maps to ``None``.
|
|
625
|
-
|
|
626
|
-
|
|
640
|
+
The dict-compatible result also carries known decimal scales in
|
|
641
|
+
``ColumnMetadata.decimal_scales``. Legacy plain-dict providers remain
|
|
642
|
+
supported with unknown scales. This default reads the driver's empty
|
|
643
|
+
query description; adapters with exact catalog types override it.
|
|
627
644
|
"""
|
|
628
645
|
description, _rows = self._column_type_query(sq_name, f"select * from {sq_name} where 1=0;")
|
|
629
|
-
return {d[0].lower(): self.
|
|
646
|
+
return ColumnMetadata({d[0].lower(): self._described_column(d) for d in description})
|
|
630
647
|
|
|
631
648
|
def _described_kind(self, description: Any) -> type[DataType] | None:
|
|
632
649
|
"""The execsql type for one column of a cursor description (a DB-API 7-item sequence)."""
|
|
633
650
|
return kind_for_python_type(description[1], description[5])
|
|
634
651
|
|
|
652
|
+
def _described_column(self, description: Any) -> tuple[type[DataType] | None, int | None]:
|
|
653
|
+
"""Read a kind and decimal scale from a DB-API description."""
|
|
654
|
+
kind = self._described_kind(description)
|
|
655
|
+
scale = description[5]
|
|
656
|
+
return kind, scale if kind is DT_Decimal and type(scale) is int else None
|
|
657
|
+
|
|
635
658
|
def _column_type_query(self, sq_name: str, sql: str, params: list | None = None) -> tuple[Any, list]:
|
|
636
659
|
"""Run *sql* for :meth:`column_kinds` of *sq_name*; return the cursor description and the rows."""
|
|
637
660
|
with self._cursor() as curs:
|
|
@@ -744,7 +767,7 @@ class Database(ABC):
|
|
|
744
767
|
|
|
745
768
|
*rowsource* must be a generator yielding lists of values in column order.
|
|
746
769
|
*tablespec_src* is a zero-argument callable that returns the table's type specification.
|
|
747
|
-
|
|
770
|
+
Only selected columns are converted to the type scanned for each column; with
|
|
748
771
|
*existing_table* (the table was there before the IMPORT or COPY), the
|
|
749
772
|
type of the column it goes into decides instead (see
|
|
750
773
|
``db.target_types.existing_table_converter``).
|
|
@@ -770,9 +793,18 @@ class Database(ABC):
|
|
|
770
793
|
converters = [self._scanned_type_converter(col.column_type()[1]) for col in tablespec.cols]
|
|
771
794
|
if existing_table:
|
|
772
795
|
kinds = self.column_kinds(sq_name)
|
|
796
|
+
scales = kinds.decimal_scales if isinstance(kinds, ColumnMetadata) else {}
|
|
773
797
|
converters = [
|
|
774
|
-
existing_table_converter(
|
|
775
|
-
|
|
798
|
+
existing_table_converter(
|
|
799
|
+
kinds.get(col.name.lower()),
|
|
800
|
+
col.column_type()[1],
|
|
801
|
+
convert,
|
|
802
|
+
col.name,
|
|
803
|
+
sq_name,
|
|
804
|
+
decimal_binding_precision=self.decimal_binding_precision,
|
|
805
|
+
decimal_scale=scales.get(col.name.lower()) if included else None,
|
|
806
|
+
)
|
|
807
|
+
for col, convert, included in zip(tablespec.cols, converters, incl_col)
|
|
776
808
|
]
|
|
777
809
|
# Construct INSERT statement.
|
|
778
810
|
columns = [self.type.quoted(col) for col in sel_cols]
|
|
@@ -871,11 +903,7 @@ class Database(ABC):
|
|
|
871
903
|
line[i] = None
|
|
872
904
|
# Trailing values left off the line are NULL.
|
|
873
905
|
line = list(line) + [None] * (len(ts_colnames) - len(line))
|
|
874
|
-
|
|
875
|
-
row = []
|
|
876
|
-
for i, v in enumerate(lt):
|
|
877
|
-
if incl_col[i]:
|
|
878
|
-
row.append(v)
|
|
906
|
+
row = [converters[i](val) for i, val in enumerate(line) if incl_col[i]]
|
|
879
907
|
add_line = True
|
|
880
908
|
if not _state.conf.empty_rows:
|
|
881
909
|
add_line = not all(c is None for c in row)
|
|
@@ -1076,8 +1104,8 @@ class DatabasePool:
|
|
|
1076
1104
|
return self.pool[self.initial_db]
|
|
1077
1105
|
|
|
1078
1106
|
def aliased_as(self, db_alias: str) -> Database:
|
|
1079
|
-
"""Return the ``Database`` registered under *db_alias
|
|
1080
|
-
return self.pool[db_alias]
|
|
1107
|
+
"""Return the ``Database`` registered under *db_alias* (any letter case)."""
|
|
1108
|
+
return self.pool[db_alias.lower()]
|
|
1081
1109
|
|
|
1082
1110
|
def make_current(self, db_alias: str) -> None:
|
|
1083
1111
|
"""Set the active database to *db_alias*; raises ``ErrInfo`` if the alias is unknown."""
|
|
@@ -1090,7 +1118,8 @@ class DatabasePool:
|
|
|
1090
1118
|
self.current_db = db_alias
|
|
1091
1119
|
|
|
1092
1120
|
def disconnect(self, alias: str) -> None:
|
|
1093
|
-
"""Close and remove the connection registered under *alias* from the pool."""
|
|
1121
|
+
"""Close and remove the connection registered under *alias* (any letter case) from the pool."""
|
|
1122
|
+
alias = alias.lower()
|
|
1094
1123
|
if alias == self.current_db or (alias == "initial" and "initial" in self.pool):
|
|
1095
1124
|
raise ErrInfo(
|
|
1096
1125
|
type="error",
|
|
@@ -1100,9 +1129,17 @@ class DatabasePool:
|
|
|
1100
1129
|
self.pool[alias].close()
|
|
1101
1130
|
del self.pool[alias]
|
|
1102
1131
|
|
|
1103
|
-
def closeall(self) -> None:
|
|
1104
|
-
"""Roll back and close
|
|
1132
|
+
def closeall(self, *, except_db: Database | None = None) -> None:
|
|
1133
|
+
"""Roll back and close the pool's connections except *except_db*.
|
|
1134
|
+
|
|
1135
|
+
The exception is matched by object identity under every alias and is
|
|
1136
|
+
neither rolled back nor closed. All aliases are removed, including
|
|
1137
|
+
aliases of the excluded caller-owned connection.
|
|
1138
|
+
Rollback follows the pool's ``do_rollback`` setting.
|
|
1139
|
+
"""
|
|
1105
1140
|
for alias, db in self.pool.items():
|
|
1141
|
+
if db is except_db:
|
|
1142
|
+
continue
|
|
1106
1143
|
nm = db.name()
|
|
1107
1144
|
try:
|
|
1108
1145
|
if self.do_rollback:
|
execsql/db/dsn.py
CHANGED
|
@@ -100,10 +100,9 @@ class DsnDatabase(Database):
|
|
|
100
100
|
# so that ``;``, ``=`` and other ODBC delimiters in user-
|
|
101
101
|
# supplied credentials cannot inject additional attributes.
|
|
102
102
|
parts = [f"DSN={_odbc_quote(self.db_name)}"]
|
|
103
|
-
if self.
|
|
104
|
-
assert self.user is not None
|
|
105
|
-
assert self.password is not None
|
|
103
|
+
if self.user:
|
|
106
104
|
parts.append(f"UID={_odbc_quote(self.user)}")
|
|
105
|
+
if self.password is not None:
|
|
107
106
|
parts.append(f"PWD={_odbc_quote(self.password)}")
|
|
108
107
|
connstr = ";".join(parts) + ";"
|
|
109
108
|
kwargs = {"autocommit": autocommit} if autocommit else {}
|
execsql/db/duckdb.py
CHANGED
|
@@ -9,6 +9,7 @@ on the CLI.
|
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
11
|
import importlib.util
|
|
12
|
+
from decimal import Decimal
|
|
12
13
|
import re
|
|
13
14
|
import sys
|
|
14
15
|
from collections import deque
|
|
@@ -18,8 +19,8 @@ from pathlib import Path
|
|
|
18
19
|
from typing import Any
|
|
19
20
|
|
|
20
21
|
from execsql.db.base import Database, statement_start
|
|
21
|
-
from execsql.db.target_types import kind_for_type_name
|
|
22
|
-
from execsql.types import DataType
|
|
22
|
+
from execsql.db.target_types import kind_for_type_name, kind_and_scale_for_type_name, decimal_parameter
|
|
23
|
+
from execsql.types import DataType, DT_Decimal
|
|
23
24
|
from execsql.exceptions import ErrInfo
|
|
24
25
|
from execsql.utils.errors import exception_desc, fatal_error, install_hint
|
|
25
26
|
import execsql.state as _state
|
|
@@ -172,6 +173,9 @@ class DuckDBDatabase(Database):
|
|
|
172
173
|
and sees the run's temporary tables.
|
|
173
174
|
"""
|
|
174
175
|
|
|
176
|
+
#: Keep Decimal parameters exact across exponent/width conversion in drivers.
|
|
177
|
+
decimal_binding_precision = 38
|
|
178
|
+
|
|
175
179
|
#: A transaction this adapter or a script's own BEGIN opened, not yet ended.
|
|
176
180
|
in_transaction: bool = False
|
|
177
181
|
#: The cursor whose result the connection holds, if any.
|
|
@@ -266,13 +270,29 @@ class DuckDBDatabase(Database):
|
|
|
266
270
|
self.in_transaction = False
|
|
267
271
|
|
|
268
272
|
def rollback(self) -> None:
|
|
269
|
-
"""Roll back the open transaction.
|
|
273
|
+
"""Roll back the open transaction, if one is open.
|
|
274
|
+
|
|
275
|
+
The driver decides whether one is: ``in_transaction`` misses a
|
|
276
|
+
transaction begun partway through a statement. Outside a transaction
|
|
277
|
+
DuckDB has committed each statement as it ran, and its "no transaction
|
|
278
|
+
is active" error is not logged as a failed rollback.
|
|
279
|
+
"""
|
|
280
|
+
import duckdb
|
|
281
|
+
|
|
270
282
|
try:
|
|
271
283
|
self._take_connection()
|
|
272
284
|
except Exception: # best effort, as the rollback is: a failed transaction may not give up its rows
|
|
273
285
|
self._holder = None
|
|
274
|
-
super().rollback()
|
|
275
286
|
self.in_transaction = False
|
|
287
|
+
if self.conn is None:
|
|
288
|
+
return
|
|
289
|
+
try:
|
|
290
|
+
self.conn.rollback()
|
|
291
|
+
except duckdb.TransactionException as e:
|
|
292
|
+
if "no transaction is active" not in str(e):
|
|
293
|
+
self._log_failed_rollback(e)
|
|
294
|
+
except Exception as e:
|
|
295
|
+
self._log_failed_rollback(e)
|
|
276
296
|
|
|
277
297
|
def close(self) -> None:
|
|
278
298
|
"""Close the connection; an open transaction is rolled back."""
|
|
@@ -333,6 +353,25 @@ class DuckDBDatabase(Database):
|
|
|
333
353
|
"""DuckDB describes a column by its type's name (``DECIMAL(18,3)``, ``TIMESTAMP WITH TIME ZONE``)."""
|
|
334
354
|
return kind_for_type_name(str(description[1]))
|
|
335
355
|
|
|
356
|
+
def _scanned_type_converter(self, data_type: type[DataType]) -> Callable[[Any], Any]:
|
|
357
|
+
convert = super()._scanned_type_converter(data_type)
|
|
358
|
+
if data_type is not DT_Decimal:
|
|
359
|
+
return convert
|
|
360
|
+
|
|
361
|
+
def bind(value: Any) -> Any:
|
|
362
|
+
result = convert(value)
|
|
363
|
+
if isinstance(result, Decimal) and result.is_finite():
|
|
364
|
+
exponent = result.as_tuple().exponent
|
|
365
|
+
if isinstance(exponent, int) and exponent > 0:
|
|
366
|
+
return decimal_parameter(result, self.decimal_binding_precision, "value", "target table")
|
|
367
|
+
return result
|
|
368
|
+
|
|
369
|
+
return bind
|
|
370
|
+
|
|
371
|
+
def _described_column(self, description: Any) -> tuple[type[DataType] | None, int | None]:
|
|
372
|
+
_kind, scale = kind_and_scale_for_type_name(str(description[1]))
|
|
373
|
+
return self._described_kind(description), scale
|
|
374
|
+
|
|
336
375
|
def view_exists(self, view_name: str, schema_name: str | None = None) -> bool:
|
|
337
376
|
"""Return True if the named view exists in the DuckDB database."""
|
|
338
377
|
# DuckDB information_schema has no 'views' table; views are listed in 'tables'
|
execsql/db/firebird.py
CHANGED
|
@@ -8,7 +8,13 @@ via the ``firebird-driver`` package. Corresponds to ``-t f`` on the CLI.
|
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
from contextlib import ExitStack
|
|
12
|
+
from threading import RLock
|
|
13
|
+
from typing import Any
|
|
14
|
+
from uuid import uuid4
|
|
15
|
+
|
|
11
16
|
from execsql.db.base import Database
|
|
17
|
+
from execsql.types import DataType, DT_Decimal
|
|
12
18
|
from execsql.db.tiers import SupportTier
|
|
13
19
|
from execsql.exceptions import ErrInfo
|
|
14
20
|
from execsql.utils.errors import exception_desc, fatal_error, install_hint
|
|
@@ -24,14 +30,21 @@ import execsql.state as _state
|
|
|
24
30
|
|
|
25
31
|
__all__ = ["FirebirdDatabase"]
|
|
26
32
|
|
|
33
|
+
_CONFIG_LOCK = RLock()
|
|
34
|
+
|
|
27
35
|
|
|
28
36
|
class FirebirdDatabase(Database):
|
|
29
37
|
"""Firebird adapter using the firebird-driver package."""
|
|
30
38
|
|
|
31
|
-
#:
|
|
39
|
+
#: Live Firebird tests are opt-in; no Firebird server runs in CI.
|
|
32
40
|
support_tier = SupportTier.BEST_EFFORT
|
|
33
41
|
support_tier_name = "Firebird"
|
|
34
42
|
|
|
43
|
+
def _described_column(self, description: Any) -> tuple[type[DataType] | None, int | None]:
|
|
44
|
+
kind, scale = super()._described_column(description)
|
|
45
|
+
# firebird-driver exposes the engine's exponent, the negative SQL scale.
|
|
46
|
+
return kind, -scale if kind is DT_Decimal and scale is not None else None
|
|
47
|
+
|
|
35
48
|
def __init__(
|
|
36
49
|
self,
|
|
37
50
|
server_name: str,
|
|
@@ -53,7 +66,7 @@ class FirebirdDatabase(Database):
|
|
|
53
66
|
super().__init__(
|
|
54
67
|
server_name=str(server_name),
|
|
55
68
|
db_name=str(db_name),
|
|
56
|
-
user_name=str(user_name),
|
|
69
|
+
user_name=str(user_name) if user_name is not None else None,
|
|
57
70
|
need_passwd=need_passwd,
|
|
58
71
|
port=port if port else 3050,
|
|
59
72
|
)
|
|
@@ -84,24 +97,27 @@ class FirebirdDatabase(Database):
|
|
|
84
97
|
connect_timeout = 30
|
|
85
98
|
|
|
86
99
|
def db_conn():
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
100
|
+
# firebird-driver takes connection options through named configs,
|
|
101
|
+
# not connect(host=, port=, timeout=). Private configs leave the
|
|
102
|
+
# process-wide defaults and other concurrent connections alone.
|
|
103
|
+
config_name = f"execsql_{uuid4().hex}"
|
|
104
|
+
config = firebird_lib.driver_config
|
|
105
|
+
with _CONFIG_LOCK, ExitStack() as cleanup:
|
|
106
|
+
server = config.register_server(config_name)
|
|
107
|
+
cleanup.callback(config.servers.value.remove, server)
|
|
108
|
+
server.host.value = self.server_name
|
|
109
|
+
server.port.value = str(self.port)
|
|
110
|
+
database = config.register_database(config_name)
|
|
111
|
+
cleanup.callback(config.databases.value.remove, database)
|
|
112
|
+
database.server.value = config_name
|
|
113
|
+
database.database.value = self.db_name
|
|
114
|
+
database.timeout.value = connect_timeout
|
|
115
|
+
credentials = {}
|
|
116
|
+
if self.user:
|
|
117
|
+
credentials["user"] = self.user
|
|
118
|
+
if self.password is not None:
|
|
119
|
+
credentials["password"] = self.password
|
|
120
|
+
return firebird_lib.connect(config_name, charset=self.encoding, **credentials)
|
|
105
121
|
|
|
106
122
|
if self.conn is None:
|
|
107
123
|
try:
|
|
@@ -181,7 +197,8 @@ class FirebirdDatabase(Database):
|
|
|
181
197
|
other_msg=f"Failed test for existence of Firebird table {table_name}",
|
|
182
198
|
) from e
|
|
183
199
|
rows = curs.fetchall()
|
|
184
|
-
self.
|
|
200
|
+
if not self.holding():
|
|
201
|
+
self.commit()
|
|
185
202
|
return len(rows) > 0
|
|
186
203
|
|
|
187
204
|
def column_exists(
|
|
@@ -260,4 +277,4 @@ class FirebirdDatabase(Database):
|
|
|
260
277
|
"""Drop *tablename*, schema-qualified and quoted by the caller."""
|
|
261
278
|
# Firebird will thrown an error if there are foreign keys into the table.
|
|
262
279
|
self.execute(f"DROP TABLE {tablename};")
|
|
263
|
-
self.
|
|
280
|
+
self.commit_now()
|
execsql/db/mysql.py
CHANGED
|
@@ -13,7 +13,7 @@ import tempfile
|
|
|
13
13
|
from typing import Any, cast
|
|
14
14
|
|
|
15
15
|
from execsql.db.base import Database
|
|
16
|
-
from execsql.db.target_types import ColumnKinds,
|
|
16
|
+
from execsql.db.target_types import ColumnKinds, ColumnMetadata, kind_and_scale_for_type_name
|
|
17
17
|
from execsql.exceptions import ErrInfo
|
|
18
18
|
from execsql.utils.errors import exception_desc, fatal_error, install_hint
|
|
19
19
|
from execsql.utils.auth import (
|
|
@@ -87,7 +87,12 @@ def _data_length(filename: str) -> int | None:
|
|
|
87
87
|
except OSError:
|
|
88
88
|
return None
|
|
89
89
|
# Keep the line ending of the last line of data.
|
|
90
|
-
|
|
90
|
+
if ending == b"\r\n":
|
|
91
|
+
keep = content_end + 2
|
|
92
|
+
elif ending[:1] in (b"\r", b"\n"):
|
|
93
|
+
keep = content_end + 1
|
|
94
|
+
else:
|
|
95
|
+
keep = content_end
|
|
91
96
|
return keep if keep < size else None
|
|
92
97
|
|
|
93
98
|
|
|
@@ -248,7 +253,7 @@ class MySQLDatabase(Database):
|
|
|
248
253
|
def column_kinds(self, sq_name: str) -> ColumnKinds:
|
|
249
254
|
"""The execsql type of each column of *sq_name*, from ``SHOW COLUMNS`` (which tells BOOLEAN from TINYINT)."""
|
|
250
255
|
_description, rows = self._column_type_query(sq_name, f"show columns from {sq_name};")
|
|
251
|
-
return {str(row[0]).lower():
|
|
256
|
+
return ColumnMetadata({str(row[0]).lower(): kind_and_scale_for_type_name(_text(row[1])) for row in rows})
|
|
252
257
|
|
|
253
258
|
def view_exists(self, view_name: str, schema_name: str | None = None) -> bool:
|
|
254
259
|
return super().view_exists(
|
|
@@ -265,26 +270,20 @@ class MySQLDatabase(Database):
|
|
|
265
270
|
connect_timeout = 30
|
|
266
271
|
|
|
267
272
|
def db_conn():
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
database=self.db_name,
|
|
283
|
-
port=self.port,
|
|
284
|
-
charset=self.encoding,
|
|
285
|
-
local_infile=True,
|
|
286
|
-
connect_timeout=connect_timeout,
|
|
287
|
-
)
|
|
273
|
+
credentials = {}
|
|
274
|
+
if self.user:
|
|
275
|
+
credentials["user"] = self.user
|
|
276
|
+
if self.password is not None:
|
|
277
|
+
credentials["password"] = self.password
|
|
278
|
+
return mysql_lib.connect(
|
|
279
|
+
host=self.server_name,
|
|
280
|
+
database=self.db_name,
|
|
281
|
+
port=self.port,
|
|
282
|
+
charset=self.encoding,
|
|
283
|
+
local_infile=True,
|
|
284
|
+
connect_timeout=connect_timeout,
|
|
285
|
+
**credentials,
|
|
286
|
+
)
|
|
288
287
|
|
|
289
288
|
if self.conn is None:
|
|
290
289
|
try:
|
execsql/db/oracle.py
CHANGED
|
@@ -131,10 +131,12 @@ class OracleDatabase(Database):
|
|
|
131
131
|
|
|
132
132
|
def db_conn(db: OracleDatabase, db_name: str):
|
|
133
133
|
dsn = driver.makedsn(db.server_name, db.port, service_name=db_name)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
credentials = {}
|
|
135
|
+
if db.user:
|
|
136
|
+
credentials["user"] = db.user
|
|
137
|
+
if db.password is not None:
|
|
138
|
+
credentials["password"] = db.password
|
|
139
|
+
return driver.connect(dsn=dsn, **credentials)
|
|
138
140
|
|
|
139
141
|
if self.conn is None:
|
|
140
142
|
try:
|
|
@@ -304,6 +306,11 @@ class OracleDatabase(Database):
|
|
|
304
306
|
type_name = _ORACLE_TYPE_NAMES.get(name, "")
|
|
305
307
|
return kind_for_type_name(type_name, oracle=True)
|
|
306
308
|
|
|
309
|
+
def _described_column(self, description: Any) -> tuple[type[DataType] | None, int | None]:
|
|
310
|
+
kind, scale = super()._described_column(description)
|
|
311
|
+
# oracledb uses -127 for unconstrained NUMBER and FLOAT, not rounding.
|
|
312
|
+
return kind, None if description[5] == -127 else scale
|
|
313
|
+
|
|
307
314
|
def view_exists(self, view_name: str, schema_name: str | None = None) -> bool:
|
|
308
315
|
"""Return True if the named view exists in the Oracle database."""
|
|
309
316
|
# Oracle folds unquoted identifiers to uppercase.
|
execsql/db/postgres.py
CHANGED
|
@@ -14,7 +14,7 @@ import re
|
|
|
14
14
|
from typing import Any
|
|
15
15
|
|
|
16
16
|
from execsql.db.base import Database
|
|
17
|
-
from execsql.db.target_types import ColumnKinds,
|
|
17
|
+
from execsql.db.target_types import ColumnKinds, ColumnMetadata, kind_and_scale_for_type_name
|
|
18
18
|
from execsql.exceptions import ErrInfo
|
|
19
19
|
from execsql.utils.errors import exception_desc, fatal_error, install_hint
|
|
20
20
|
from execsql.utils.auth import (
|
|
@@ -90,24 +90,19 @@ class PostgresDatabase(Database):
|
|
|
90
90
|
# between runs; a cached plan whose result type then changes triggers
|
|
91
91
|
# PostgreSQL's "cached plan must not change result type" error. psycopg2
|
|
92
92
|
# never auto-prepared, so this preserves backwards-compatible behavior.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
dbname=db_name,
|
|
107
|
-
port=db.port,
|
|
108
|
-
connect_timeout=db.connect_timeout,
|
|
109
|
-
prepare_threshold=None,
|
|
110
|
-
)
|
|
93
|
+
credentials = {}
|
|
94
|
+
if db.user:
|
|
95
|
+
credentials["user"] = db.user
|
|
96
|
+
if db.password is not None:
|
|
97
|
+
credentials["password"] = db.password
|
|
98
|
+
return psycopg.connect(
|
|
99
|
+
host=str(db.server_name),
|
|
100
|
+
dbname=db_name,
|
|
101
|
+
port=db.port,
|
|
102
|
+
connect_timeout=db.connect_timeout,
|
|
103
|
+
prepare_threshold=None,
|
|
104
|
+
**credentials,
|
|
105
|
+
)
|
|
111
106
|
except Exception as e:
|
|
112
107
|
msg = (
|
|
113
108
|
f"Failed to open PostgreSQL database {self.db_name} on {self.server_name}; "
|
|
@@ -233,7 +228,7 @@ class PostgresDatabase(Database):
|
|
|
233
228
|
"where attrelid = %s::regclass and attnum > 0 and not attisdropped order by attnum;",
|
|
234
229
|
[sq_name],
|
|
235
230
|
)
|
|
236
|
-
return {name.lower():
|
|
231
|
+
return ColumnMetadata({name.lower(): kind_and_scale_for_type_name(type_name) for name, type_name in rows})
|
|
237
232
|
|
|
238
233
|
def view_exists(self, view_name: str, schema_name: str | None = None) -> bool:
|
|
239
234
|
"""Return True if the named view exists in the PostgreSQL database."""
|