Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sqlit/domains/query/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from sqlit.domains.connections.app.session import ConnectionSession
from sqlit.domains.connections.cli.prompts import prompt_for_password
from sqlit.domains.connections.domain.config import ConnectionConfig
from sqlit.domains.query.app.multi_statement import split_statements
from sqlit.domains.query.app.query_service import (
DialectQueryAnalyzer,
QueryKind,
QueryResult,
QueryService,
)
from sqlit.domains.query.editing.comments import is_comment_only_statement
from sqlit.shared.app.runtime import RuntimeConfig
from sqlit.shared.app.services import AppServices, build_app_services

Expand Down Expand Up @@ -172,6 +174,20 @@ def cmd_query(

try:
with create_session(config) as session:
statements = [s for s in split_statements(query) if not is_comment_only_statement(s)]
if len(statements) > 1:
# Same as the TUI: run them in order, stop on the first error, show the last result.
for statement in statements[:-1]:
service.execute(
connection=session.connection,
executor=session.provider.query_executor,
query=statement,
config=config,
max_rows=max_rows,
save_to_history=True,
)
query = statements[-1]

has_cursor = hasattr(session.connection, "cursor") and callable(getattr(session.connection, "cursor", None))

if _should_stream_results(
Expand Down
13 changes: 13 additions & 0 deletions tests/cli/test_cli_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,16 @@ def test_cli_version_short_option(tmp_path: Path):
)
assert result.returncode == 0
assert "sqlit" in result.stdout


def test_query_file_runs_every_statement(tmp_path: Path, monkeypatch):
monkeypatch.setenv("SQLIT_CONFIG_DIR", str(tmp_path))
db = tmp_path / "demo.db"
script = tmp_path / "script.sql"
script.write_text("create table z(a int);\n-- seed\ninsert into z values (1);\nselect * from z;\n")

run_cli("connections", "add", "sqlite", "--name", "d", "--file-path", str(db))
result = run_cli("query", "-c", "d", "-f", str(script), "--format", "json", check=False)

assert result.returncode == 0, result.stdout + result.stderr
assert json.loads(result.stdout) == [{"a": 1}]