From d7fd0c334c0e8d5030de8dd5735a3563ecfb232f Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Tue, 10 Nov 2026 01:08:25 +0530 Subject: [PATCH] Run every statement in a query passed to the CLI sqlit query -f script.sql handed the whole file to the driver, and SQLite refuses more than one statement per execute. --- sqlit/domains/query/cli/commands.py | 16 ++++++++++++++++ tests/cli/test_cli_main.py | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/sqlit/domains/query/cli/commands.py b/sqlit/domains/query/cli/commands.py index c348e94c..d5e9ff9a 100644 --- a/sqlit/domains/query/cli/commands.py +++ b/sqlit/domains/query/cli/commands.py @@ -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 @@ -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( diff --git a/tests/cli/test_cli_main.py b/tests/cli/test_cli_main.py index 0e13e27b..1d93ea49 100644 --- a/tests/cli/test_cli_main.py +++ b/tests/cli/test_cli_main.py @@ -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}]