execsql2 2.39.1__py3-none-any.whl → 2.41.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.
- execsql/__init__.py +4 -2
- execsql/api.py +40 -27
- execsql/cli/commands/__init__.py +1 -0
- execsql/cli/commands/inspect.py +292 -0
- execsql/cli/dispatch.py +3 -3
- execsql/cli/inspection.py +276 -0
- execsql/cli/lint.py +3 -4
- execsql/cli/options.py +15 -7
- execsql/config.py +15 -2
- execsql/db/access.py +6 -5
- execsql/db/base.py +14 -0
- execsql/db/dsn.py +3 -2
- execsql/db/duckdb.py +2 -1
- execsql/db/firebird.py +3 -2
- execsql/db/mysql.py +3 -2
- execsql/db/oracle.py +3 -2
- execsql/db/postgres.py +3 -2
- execsql/db/sqlite.py +2 -1
- execsql/db/sqlserver.py +3 -2
- execsql/ddl.py +310 -0
- execsql/exporters/xls.py +18 -4
- execsql/file_inspection.py +393 -0
- execsql/importers/base.py +19 -12
- execsql/importers/feather.py +16 -21
- execsql/importers/json.py +26 -32
- execsql/importers/xls.py +9 -4
- execsql/metacommands/control.py +21 -23
- execsql/metacommands/script_ext.py +6 -2
- execsql/schema.py +816 -0
- execsql/script/executor.py +11 -0
- execsql/script/parser.py +31 -0
- execsql/script/variables.py +14 -2
- execsql/utils/errors.py +21 -3
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/README.md +7 -0
- execsql2-2.41.0.data/data/execsql2_extras/inspect/README.md +116 -0
- execsql2-2.41.0.data/data/execsql2_extras/inspect/make_demo.py +106 -0
- {execsql2-2.39.1.dist-info → execsql2-2.41.0.dist-info}/METADATA +84 -19
- {execsql2-2.39.1.dist-info → execsql2-2.41.0.dist-info}/RECORD +54 -47
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/example_config_prompt.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/md_compare.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/md_glossary.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/md_upsert.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/pg_compare.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/pg_glossary.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/script_template.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/ss_compare.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/ss_glossary.sql +0 -0
- {execsql2-2.39.1.data → execsql2-2.41.0.data}/data/execsql2_extras/ss_upsert.sql +0 -0
- {execsql2-2.39.1.dist-info → execsql2-2.41.0.dist-info}/WHEEL +0 -0
- {execsql2-2.39.1.dist-info → execsql2-2.41.0.dist-info}/entry_points.txt +0 -0
- {execsql2-2.39.1.dist-info → execsql2-2.41.0.dist-info}/licenses/LICENSE.txt +0 -0
- {execsql2-2.39.1.dist-info → execsql2-2.41.0.dist-info}/licenses/NOTICE +0 -0
execsql/__init__.py
CHANGED
|
@@ -5,7 +5,7 @@ This package provides the ``execsql`` CLI command (distributed as the
|
|
|
5
5
|
``execsql2`` package on PyPI) and the ``execsql`` importable module.
|
|
6
6
|
|
|
7
7
|
The top-level package re-exports the public Python API for programmatic
|
|
8
|
-
use: :func:`run`, :class:`ScriptResult`, :class:`ScriptError`, and the
|
|
8
|
+
use: :func:`run`, :func:`inspect_schema`, :func:`inspect_file`, :class:`ScriptResult`, :class:`ScriptError`, and the
|
|
9
9
|
:class:`ExecSqlError` exception that :meth:`ScriptResult.raise_on_error`
|
|
10
10
|
raises. Internal sub-modules carry the rest of the implementation
|
|
11
11
|
(``cli``, ``db``, ``script``, ``metacommands``, etc.).
|
|
@@ -21,5 +21,7 @@ except PackageNotFoundError:
|
|
|
21
21
|
__version__ = "unknown"
|
|
22
22
|
|
|
23
23
|
from execsql.api import ExecSqlError, ScriptError, ScriptResult, run
|
|
24
|
+
from execsql.schema import inspect_schema
|
|
25
|
+
from execsql.file_inspection import inspect_file
|
|
24
26
|
|
|
25
|
-
__all__ = ["__version__", "run", "ScriptResult", "ScriptError", "ExecSqlError"]
|
|
27
|
+
__all__ = ["__version__", "run", "ScriptResult", "ScriptError", "ExecSqlError", "inspect_schema", "inspect_file"]
|
execsql/api.py
CHANGED
|
@@ -246,10 +246,18 @@ class _LibraryConfig:
|
|
|
246
246
|
# ---------------------------------------------------------------------------
|
|
247
247
|
|
|
248
248
|
|
|
249
|
-
def _connect_from_dsn(
|
|
249
|
+
def _connect_from_dsn(
|
|
250
|
+
dsn: str,
|
|
251
|
+
new_db: bool = False,
|
|
252
|
+
*,
|
|
253
|
+
prompt_password: bool = True,
|
|
254
|
+
encoding: str | None = None,
|
|
255
|
+
) -> Any:
|
|
250
256
|
"""Create a database connection from a DSN URL string.
|
|
251
257
|
|
|
252
258
|
Returns a :class:`~execsql.db.base.Database` subclass instance.
|
|
259
|
+
With *prompt_password* false, use supplied or driver credentials without
|
|
260
|
+
interactive password/keyring lookup.
|
|
253
261
|
"""
|
|
254
262
|
from execsql.db.factory import (
|
|
255
263
|
db_Access,
|
|
@@ -263,6 +271,7 @@ def _connect_from_dsn(dsn: str, new_db: bool = False) -> Any:
|
|
|
263
271
|
db_SqlServer,
|
|
264
272
|
)
|
|
265
273
|
|
|
274
|
+
encoding_options: dict[str, Any] = {"encoding": encoding} if encoding is not None else {}
|
|
266
275
|
params = _parse_connection_string(dsn)
|
|
267
276
|
db_type = params["db_type"]
|
|
268
277
|
server = params["server"]
|
|
@@ -275,16 +284,18 @@ def _connect_from_dsn(dsn: str, new_db: bool = False) -> Any:
|
|
|
275
284
|
if db_type == "l":
|
|
276
285
|
file = db_file or ":memory:"
|
|
277
286
|
# In-memory databases are always "new"
|
|
278
|
-
return db_SQLite(file, new_db=new_db or file == ":memory:")
|
|
287
|
+
return db_SQLite(file, new_db=new_db or file == ":memory:", **encoding_options)
|
|
279
288
|
elif db_type == "k":
|
|
280
289
|
file = db_file or ":memory:"
|
|
281
|
-
return db_DuckDB(file, new_db=new_db or file == ":memory:")
|
|
290
|
+
return db_DuckDB(file, new_db=new_db or file == ":memory:", **encoding_options)
|
|
282
291
|
elif db_type == "p":
|
|
283
292
|
return db_Postgres(
|
|
284
293
|
server or "localhost",
|
|
285
294
|
db_name,
|
|
286
295
|
user=user,
|
|
296
|
+
pw_needed=prompt_password,
|
|
287
297
|
password=password,
|
|
298
|
+
**encoding_options,
|
|
288
299
|
port=port or 5432,
|
|
289
300
|
new_db=new_db,
|
|
290
301
|
)
|
|
@@ -293,7 +304,9 @@ def _connect_from_dsn(dsn: str, new_db: bool = False) -> Any:
|
|
|
293
304
|
server or "localhost",
|
|
294
305
|
db_name,
|
|
295
306
|
user=user,
|
|
307
|
+
pw_needed=prompt_password,
|
|
296
308
|
password=password,
|
|
309
|
+
**encoding_options,
|
|
297
310
|
port=port or 3306,
|
|
298
311
|
)
|
|
299
312
|
elif db_type == "s":
|
|
@@ -301,7 +314,9 @@ def _connect_from_dsn(dsn: str, new_db: bool = False) -> Any:
|
|
|
301
314
|
server or "localhost",
|
|
302
315
|
db_name,
|
|
303
316
|
user=user,
|
|
317
|
+
pw_needed=prompt_password,
|
|
304
318
|
password=password,
|
|
319
|
+
**encoding_options,
|
|
305
320
|
port=port,
|
|
306
321
|
trust_server_certificate=params["trust_server_certificate"],
|
|
307
322
|
)
|
|
@@ -310,7 +325,9 @@ def _connect_from_dsn(dsn: str, new_db: bool = False) -> Any:
|
|
|
310
325
|
server or "localhost",
|
|
311
326
|
db_name,
|
|
312
327
|
user=user,
|
|
328
|
+
pw_needed=prompt_password,
|
|
313
329
|
password=password,
|
|
330
|
+
**encoding_options,
|
|
314
331
|
port=port or 1521,
|
|
315
332
|
)
|
|
316
333
|
elif db_type == "f":
|
|
@@ -318,13 +335,15 @@ def _connect_from_dsn(dsn: str, new_db: bool = False) -> Any:
|
|
|
318
335
|
server or "localhost",
|
|
319
336
|
db_name or db_file,
|
|
320
337
|
user=user,
|
|
338
|
+
pw_needed=prompt_password,
|
|
321
339
|
password=password,
|
|
340
|
+
**encoding_options,
|
|
322
341
|
port=port or 3050,
|
|
323
342
|
)
|
|
324
343
|
elif db_type == "a":
|
|
325
|
-
return db_Access(db_file)
|
|
344
|
+
return db_Access(db_file, **encoding_options)
|
|
326
345
|
elif db_type == "d":
|
|
327
|
-
return db_Dsn(db_name, user=user, password=password)
|
|
346
|
+
return db_Dsn(db_name, user=user, password=password, **encoding_options)
|
|
328
347
|
else:
|
|
329
348
|
raise ValueError(f"Unsupported database type: {db_type!r}")
|
|
330
349
|
|
|
@@ -582,10 +601,8 @@ def run(
|
|
|
582
601
|
_roll_back_after_interrupt(ctx)
|
|
583
602
|
raise
|
|
584
603
|
|
|
585
|
-
#
|
|
586
|
-
#
|
|
587
|
-
# reaches the history, so there is no overlap with the exception
|
|
588
|
-
# paths above. They happened first, so they are listed first.
|
|
604
|
+
# Execution errors are captured before error-halt handlers run.
|
|
605
|
+
# Unreported termination and output errors above happened last.
|
|
589
606
|
if ctx.status is not None:
|
|
590
607
|
errors[:0] = [
|
|
591
608
|
ScriptError(message=err_msg, source=err_source, line=err_line, sql=err_cmd)
|
|
@@ -679,17 +696,12 @@ def _capture_errors(
|
|
|
679
696
|
*,
|
|
680
697
|
interrupted: bool = False,
|
|
681
698
|
) -> None:
|
|
682
|
-
"""Record
|
|
683
|
-
|
|
684
|
-
``exit_now``
|
|
685
|
-
|
|
686
|
-
SQL statement's text is in ``$LAST_ERROR``, which the executor sets just
|
|
687
|
-
before halting; it is reported only when the halt came from SQL, so a
|
|
688
|
-
metacommand error is never paired with an earlier statement. A ``HALT``
|
|
689
|
-
carries no error, only its exit status; nor does a run *interrupted* by a
|
|
690
|
-
``SystemExit`` that execsql did not raise (a signal handler's).
|
|
699
|
+
"""Record termination and lost output not already captured in error history.
|
|
700
|
+
|
|
701
|
+
``exit_now()`` snapshots execution errors before handlers run. An explicit
|
|
702
|
+
HALT or an external SystemExit still needs its own termination diagnostic.
|
|
691
703
|
"""
|
|
692
|
-
err = ctx.halt_error
|
|
704
|
+
err = None if interrupted else ctx.halt_error
|
|
693
705
|
if err is None and interrupted:
|
|
694
706
|
msg = "Script interrupted by SystemExit" + (
|
|
695
707
|
f" with exit status {exit_code}." if type(exit_code) is int else "."
|
|
@@ -702,14 +714,15 @@ def _capture_errors(
|
|
|
702
714
|
lc = ctx.last_command
|
|
703
715
|
if err is not None and lc is not None and lc.command_type == "sql" and ctx.subvars is not None:
|
|
704
716
|
sql = ctx.subvars.varvalue("$LAST_ERROR") or None
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
717
|
+
if err is None or ctx.status is None or ctx.status.last_halting_error_id != id(err):
|
|
718
|
+
errors.append(
|
|
719
|
+
ScriptError(
|
|
720
|
+
message=msg,
|
|
721
|
+
source=_last_source(ctx, err),
|
|
722
|
+
line=_last_line(ctx, err),
|
|
723
|
+
sql=sql,
|
|
724
|
+
),
|
|
725
|
+
)
|
|
713
726
|
if ctx.halt_output_error is not None:
|
|
714
727
|
errors.append(ScriptError(message=ctx.halt_output_error.errmsg(), source="<output>"))
|
|
715
728
|
|
execsql/cli/commands/__init__.py
CHANGED
|
@@ -12,3 +12,4 @@ from execsql.cli.commands import config # noqa: F401
|
|
|
12
12
|
from execsql.cli.commands import listing # noqa: F401
|
|
13
13
|
from execsql.cli.commands import init # noqa: F401
|
|
14
14
|
from execsql.cli.commands import lsp # noqa: F401
|
|
15
|
+
from execsql.cli.commands import inspect # noqa: F401
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"""Read-only inspection of database structure and importable files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from typing import Annotated
|
|
9
|
+
|
|
10
|
+
import typer
|
|
11
|
+
|
|
12
|
+
from execsql.cli.application import ExecsqlCommand, ExecsqlSubGroup, app
|
|
13
|
+
from execsql.cli.options import NoPasswdOpt, OutputFormat, OutputFormatOpt
|
|
14
|
+
from execsql.cli.options import ConfigFileOpt
|
|
15
|
+
from execsql.file_inspection import FileFormat
|
|
16
|
+
from execsql.ddl import SqlDialect
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SqlOutputFormat(str, Enum):
|
|
20
|
+
"""Only table and file inspection support executable SQL output."""
|
|
21
|
+
|
|
22
|
+
text = "text"
|
|
23
|
+
json = "json"
|
|
24
|
+
sql = "sql"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
_SqlOutputFormatOpt = Annotated[
|
|
28
|
+
SqlOutputFormat,
|
|
29
|
+
typer.Option("--output-format", help="Readable text, versioned JSON or copyable SQL."),
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
inspect_app = typer.Typer(
|
|
33
|
+
cls=ExecsqlSubGroup,
|
|
34
|
+
name="inspect",
|
|
35
|
+
help="Inspect database structure or importable files without executing scripts.",
|
|
36
|
+
rich_markup_mode=None,
|
|
37
|
+
add_completion=False,
|
|
38
|
+
context_settings={"help_option_names": ["-h", "--help"]},
|
|
39
|
+
)
|
|
40
|
+
app.add_typer(inspect_app, name="inspect")
|
|
41
|
+
|
|
42
|
+
_SchemaOpt = Annotated[str | None, typer.Option("--schema", help="Exact schema name (case-sensitive).")]
|
|
43
|
+
_PromptPasswordOpt = Annotated[
|
|
44
|
+
bool,
|
|
45
|
+
typer.Option("--prompt-password", help="Allow a password prompt on a terminal, including with JSON or SQL output."),
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
_InspectionDsnOpt = Annotated[
|
|
49
|
+
str | None,
|
|
50
|
+
typer.Option(
|
|
51
|
+
"--dsn",
|
|
52
|
+
"--connection-string",
|
|
53
|
+
metavar="URL",
|
|
54
|
+
help="Database connection URL; omit to use [connect] configuration defaults.",
|
|
55
|
+
),
|
|
56
|
+
]
|
|
57
|
+
_ShowSettingsOpt = Annotated[bool, typer.Option("--show-settings", help="Include applicable settings in text output.")]
|
|
58
|
+
_ShowConfigOpt = Annotated[
|
|
59
|
+
bool,
|
|
60
|
+
typer.Option("--show-config", help="Include loaded configuration files in text output."),
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@inspect_app.command(cls=ExecsqlCommand, name="database")
|
|
65
|
+
def database_cmd(
|
|
66
|
+
dsn: _InspectionDsnOpt = None,
|
|
67
|
+
config_file: ConfigFileOpt = None,
|
|
68
|
+
show_settings: _ShowSettingsOpt = False,
|
|
69
|
+
show_config: _ShowConfigOpt = False,
|
|
70
|
+
output_format: OutputFormatOpt = OutputFormat.text,
|
|
71
|
+
no_passwd: NoPasswdOpt = False,
|
|
72
|
+
prompt_password: _PromptPasswordOpt = False,
|
|
73
|
+
) -> None:
|
|
74
|
+
"""Show database identity and namespaces with visible table/view counts."""
|
|
75
|
+
_inspect(
|
|
76
|
+
dsn=dsn,
|
|
77
|
+
config_file=config_file,
|
|
78
|
+
show_settings=show_settings,
|
|
79
|
+
show_config=show_config,
|
|
80
|
+
schema=None,
|
|
81
|
+
table=None,
|
|
82
|
+
output_format=output_format,
|
|
83
|
+
no_passwd=no_passwd,
|
|
84
|
+
prompt_password=prompt_password,
|
|
85
|
+
database_overview=True,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@inspect_app.command(cls=ExecsqlCommand, name="schema")
|
|
90
|
+
def schema_cmd(
|
|
91
|
+
name: Annotated[str, typer.Argument(metavar="NAME", help="Exact schema name (case-sensitive).")],
|
|
92
|
+
dsn: _InspectionDsnOpt = None,
|
|
93
|
+
config_file: ConfigFileOpt = None,
|
|
94
|
+
show_settings: _ShowSettingsOpt = False,
|
|
95
|
+
show_config: _ShowConfigOpt = False,
|
|
96
|
+
output_format: OutputFormatOpt = OutputFormat.text,
|
|
97
|
+
no_passwd: NoPasswdOpt = False,
|
|
98
|
+
prompt_password: _PromptPasswordOpt = False,
|
|
99
|
+
) -> None:
|
|
100
|
+
"""List tables/views and key summaries within one schema. Text by default."""
|
|
101
|
+
_inspect(
|
|
102
|
+
dsn=dsn,
|
|
103
|
+
config_file=config_file,
|
|
104
|
+
show_settings=show_settings,
|
|
105
|
+
show_config=show_config,
|
|
106
|
+
schema=name,
|
|
107
|
+
table=None,
|
|
108
|
+
output_format=output_format,
|
|
109
|
+
no_passwd=no_passwd,
|
|
110
|
+
prompt_password=prompt_password,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@inspect_app.command(cls=ExecsqlCommand, name="table")
|
|
115
|
+
def table_cmd(
|
|
116
|
+
name: Annotated[str, typer.Argument(metavar="NAME", help="Exact table or view name (case-sensitive).")],
|
|
117
|
+
dsn: _InspectionDsnOpt = None,
|
|
118
|
+
config_file: ConfigFileOpt = None,
|
|
119
|
+
show_settings: _ShowSettingsOpt = False,
|
|
120
|
+
show_config: _ShowConfigOpt = False,
|
|
121
|
+
schema: _SchemaOpt = None,
|
|
122
|
+
output_format: _SqlOutputFormatOpt = SqlOutputFormat.text,
|
|
123
|
+
no_passwd: NoPasswdOpt = False,
|
|
124
|
+
prompt_password: _PromptPasswordOpt = False,
|
|
125
|
+
) -> None:
|
|
126
|
+
"""Inspect columns, types, keys and relationships for a table or view."""
|
|
127
|
+
_inspect(
|
|
128
|
+
dsn=dsn,
|
|
129
|
+
config_file=config_file,
|
|
130
|
+
show_settings=show_settings,
|
|
131
|
+
show_config=show_config,
|
|
132
|
+
schema=schema,
|
|
133
|
+
table=name,
|
|
134
|
+
output_format=SqlOutputFormat.sql
|
|
135
|
+
if output_format is SqlOutputFormat.sql
|
|
136
|
+
else OutputFormat(output_format.value),
|
|
137
|
+
no_passwd=no_passwd,
|
|
138
|
+
prompt_password=prompt_password,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _inspect(
|
|
143
|
+
*,
|
|
144
|
+
dsn: str | None,
|
|
145
|
+
config_file: str | None,
|
|
146
|
+
show_settings: bool,
|
|
147
|
+
show_config: bool,
|
|
148
|
+
schema: str | None,
|
|
149
|
+
table: str | None,
|
|
150
|
+
output_format: OutputFormat | SqlOutputFormat,
|
|
151
|
+
no_passwd: bool,
|
|
152
|
+
prompt_password: bool,
|
|
153
|
+
database_overview: bool = False,
|
|
154
|
+
) -> None:
|
|
155
|
+
"""Apply one connection, prompting and output policy to every catalog scope."""
|
|
156
|
+
from execsql.schema import inspect_schema
|
|
157
|
+
|
|
158
|
+
from execsql.cli.inspection import print_inspection, print_inspection_error, print_inspection_context
|
|
159
|
+
|
|
160
|
+
sql_requested = output_format is SqlOutputFormat.sql
|
|
161
|
+
if sql_requested and (show_settings or show_config):
|
|
162
|
+
raise typer.BadParameter("SQL output cannot be combined with --show-settings or --show-config.")
|
|
163
|
+
if prompt_password and no_passwd:
|
|
164
|
+
raise typer.BadParameter("--prompt-password and --no-passwd cannot be combined.")
|
|
165
|
+
terminal = _terminal_available()
|
|
166
|
+
if prompt_password and not terminal:
|
|
167
|
+
raise typer.BadParameter("Password prompting requires an interactive terminal.")
|
|
168
|
+
allow_prompt: bool | None = False
|
|
169
|
+
if terminal and not no_passwd:
|
|
170
|
+
if prompt_password:
|
|
171
|
+
allow_prompt = True
|
|
172
|
+
elif output_format is OutputFormat.text:
|
|
173
|
+
allow_prompt = None # Follow the configured password-prompt preference.
|
|
174
|
+
try:
|
|
175
|
+
result = inspect_schema(
|
|
176
|
+
dsn=dsn,
|
|
177
|
+
config_file=config_file,
|
|
178
|
+
schema=schema,
|
|
179
|
+
table=table,
|
|
180
|
+
prompt_password=allow_prompt,
|
|
181
|
+
include_database=database_overview,
|
|
182
|
+
include_details=not (sql_requested or (database_overview and output_format is OutputFormat.text)),
|
|
183
|
+
include_sql=sql_requested,
|
|
184
|
+
)
|
|
185
|
+
except ValueError as exc:
|
|
186
|
+
raise typer.BadParameter(str(exc)) from exc
|
|
187
|
+
if output_format is OutputFormat.json:
|
|
188
|
+
sys.stdout.write(json.dumps(result, indent=2, ensure_ascii=True) + "\n")
|
|
189
|
+
elif result["success"] and sql_requested:
|
|
190
|
+
sys.stdout.write(result["sql"])
|
|
191
|
+
elif result["success"]:
|
|
192
|
+
print_inspection(result, detailed=table is not None, database_overview=database_overview)
|
|
193
|
+
print_inspection_context(result, show_settings=show_settings, show_config=show_config)
|
|
194
|
+
else:
|
|
195
|
+
print_inspection_error(result, dsn)
|
|
196
|
+
if not result["success"]:
|
|
197
|
+
raise typer.Exit(code=1)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _terminal_available() -> bool:
|
|
201
|
+
"""Never let an automatic password prompt consume redirected input."""
|
|
202
|
+
return sys.stdin is not None and sys.stdin.isatty()
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
@inspect_app.command(cls=ExecsqlCommand, name="file")
|
|
206
|
+
def file_cmd(
|
|
207
|
+
path: Annotated[
|
|
208
|
+
str,
|
|
209
|
+
typer.Argument(metavar="PATH", help="Local file to inspect without loading it into a database."),
|
|
210
|
+
],
|
|
211
|
+
file_format: Annotated[
|
|
212
|
+
FileFormat | None,
|
|
213
|
+
typer.Option("--format", help="Override the filename's format hint."),
|
|
214
|
+
] = None,
|
|
215
|
+
sheet: Annotated[
|
|
216
|
+
str | None,
|
|
217
|
+
typer.Option("--sheet", help="Worksheet name or 1-based number; omit to list sheets."),
|
|
218
|
+
] = None,
|
|
219
|
+
sample_rows: Annotated[
|
|
220
|
+
int | None,
|
|
221
|
+
typer.Option("--sample-rows", min=1, help="Infer from at most N data rows; default scans all rows."),
|
|
222
|
+
] = None,
|
|
223
|
+
encoding: Annotated[
|
|
224
|
+
str | None,
|
|
225
|
+
typer.Option("--encoding", help="Text or legacy XLS encoding; overrides import configuration."),
|
|
226
|
+
] = None,
|
|
227
|
+
delimiter: Annotated[
|
|
228
|
+
str | None,
|
|
229
|
+
typer.Option("--delimiter", help="One delimiter character, TAB, US or SPACE; defaults quote to double quote."),
|
|
230
|
+
] = None,
|
|
231
|
+
quote: Annotated[
|
|
232
|
+
str | None,
|
|
233
|
+
typer.Option("--quote", help="Quote character or 'none'; requires --delimiter."),
|
|
234
|
+
] = None,
|
|
235
|
+
skip_rows: Annotated[
|
|
236
|
+
int,
|
|
237
|
+
typer.Option("--skip-rows", min=0, help="Skip leading lines/worksheet rows before the header."),
|
|
238
|
+
] = 0,
|
|
239
|
+
config_file: ConfigFileOpt = None,
|
|
240
|
+
show_settings: _ShowSettingsOpt = False,
|
|
241
|
+
show_config: _ShowConfigOpt = False,
|
|
242
|
+
output_format: _SqlOutputFormatOpt = SqlOutputFormat.text,
|
|
243
|
+
dialect: Annotated[
|
|
244
|
+
SqlDialect | None,
|
|
245
|
+
typer.Option("--dialect", help="Required destination dialect for SQL output."),
|
|
246
|
+
] = None,
|
|
247
|
+
table_name: Annotated[
|
|
248
|
+
str | None,
|
|
249
|
+
typer.Option("--table-name", help="Required destination table name for SQL output."),
|
|
250
|
+
] = None,
|
|
251
|
+
schema: Annotated[str | None, typer.Option("--schema", help="Optional destination schema for SQL output.")] = None,
|
|
252
|
+
) -> None:
|
|
253
|
+
"""Inspect import formats, columns and inferred types; full scan by default."""
|
|
254
|
+
from execsql.file_inspection import inspect_file
|
|
255
|
+
from execsql.cli.inspection import print_file_inspection
|
|
256
|
+
from execsql.cli.help import _err_console
|
|
257
|
+
from execsql.cli.inspection import _literal
|
|
258
|
+
|
|
259
|
+
sql_requested = output_format is SqlOutputFormat.sql
|
|
260
|
+
if sql_requested and (dialect is None or not table_name):
|
|
261
|
+
raise typer.BadParameter("SQL output requires both --dialect and --table-name.")
|
|
262
|
+
if sql_requested and (show_settings or show_config or sample_rows is not None):
|
|
263
|
+
raise typer.BadParameter("SQL output requires a full scan and cannot use --show-settings or --show-config.")
|
|
264
|
+
if not sql_requested and any(value is not None for value in (dialect, table_name, schema)):
|
|
265
|
+
raise typer.BadParameter("--dialect, --table-name and --schema require --output-format sql.")
|
|
266
|
+
try:
|
|
267
|
+
result = inspect_file(
|
|
268
|
+
path,
|
|
269
|
+
file_format=file_format.value if file_format else None,
|
|
270
|
+
sheet=sheet,
|
|
271
|
+
sample_rows=sample_rows,
|
|
272
|
+
encoding=encoding,
|
|
273
|
+
delimiter=delimiter,
|
|
274
|
+
quote=quote,
|
|
275
|
+
skip_rows=skip_rows,
|
|
276
|
+
config_file=config_file,
|
|
277
|
+
dialect=dialect.value if dialect else None,
|
|
278
|
+
table_name=table_name,
|
|
279
|
+
schema=schema,
|
|
280
|
+
)
|
|
281
|
+
except ValueError as exc:
|
|
282
|
+
raise typer.BadParameter(str(exc)) from exc
|
|
283
|
+
if output_format is SqlOutputFormat.json:
|
|
284
|
+
sys.stdout.write(json.dumps(result, indent=2, ensure_ascii=True) + "\n")
|
|
285
|
+
elif result["success"] and sql_requested:
|
|
286
|
+
sys.stdout.write(result["sql"])
|
|
287
|
+
elif result["success"]:
|
|
288
|
+
print_file_inspection(result, show_settings=show_settings, show_config=show_config)
|
|
289
|
+
else:
|
|
290
|
+
_err_console.print(_literal(f"Error: {result['error']['message']}"), style="bold red")
|
|
291
|
+
if not result["success"]:
|
|
292
|
+
raise typer.Exit(code=1)
|
execsql/cli/dispatch.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Command dispatch for the ``execsql`` console script.
|
|
2
2
|
|
|
3
|
-
execsql
|
|
4
|
-
``
|
|
3
|
+
execsql provides commands — ``run``, ``format``, ``lint``, ``config``,
|
|
4
|
+
``list``, ``init``, ``lsp`` and ``inspect`` — each a real :class:`typer.Typer` command,
|
|
5
5
|
so ``execsql --help`` lists them the way any multi-command CLI does.
|
|
6
6
|
|
|
7
7
|
What this module adds on top is one rule, applied before the parser sees
|
|
@@ -29,7 +29,7 @@ from __future__ import annotations
|
|
|
29
29
|
__all__ = ["COMMANDS", "GLOBAL_FLAGS", "dispatch", "normalize"]
|
|
30
30
|
|
|
31
31
|
#: Tokens that name a command.
|
|
32
|
-
COMMANDS = ("run", "format", "lint", "config", "list", "init", "lsp")
|
|
32
|
+
COMMANDS = ("run", "format", "lint", "config", "list", "init", "lsp", "inspect")
|
|
33
33
|
|
|
34
34
|
#: Options declared on the app rather than on a command, which must reach
|
|
35
35
|
#: the parser without ``run`` in front of them. ``-m``, ``--encodings`` and
|