|
| 1 | +"""``Cursor.execute`` and ``AsyncCursor.execute`` reject |
| 2 | +multi-statement SQL with ``ProgrammingError``, matching stdlib |
| 3 | +``sqlite3.Cursor.execute``. |
| 4 | +
|
| 5 | +dqlite's server prepare path returns only the first statement; |
| 6 | +without this guard, ``"INSERT ...; INSERT ..."`` silently drops |
| 7 | +everything past the first ``;`` with no diagnostic — silent data |
| 8 | +loss. |
| 9 | +
|
| 10 | +Multi-statement intent is via ``executescript`` (we stub it as |
| 11 | +``NotSupportedError``). |
| 12 | +""" |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from dqlitedbapi.cursor import _is_multi_statement |
| 17 | + |
| 18 | + |
| 19 | +class TestIsMultiStatement: |
| 20 | + @pytest.mark.parametrize( |
| 21 | + "sql", |
| 22 | + [ |
| 23 | + # Single statement, no trailing semicolon. |
| 24 | + "SELECT 1", |
| 25 | + # Single statement, trailing whitespace only. |
| 26 | + "SELECT 1 ", |
| 27 | + # Single statement plus trailing semicolon. |
| 28 | + "SELECT 1;", |
| 29 | + # Single statement + trailing whitespace after semicolon. |
| 30 | + "SELECT 1;\n \n", |
| 31 | + # Single statement + trailing line comment. |
| 32 | + "SELECT 1; -- trailing", |
| 33 | + # Single statement + trailing block comment. |
| 34 | + "SELECT 1; /* trailing */", |
| 35 | + # Semicolon inside string literal — not a boundary. |
| 36 | + "INSERT INTO t VALUES (';')", |
| 37 | + # Semicolon inside line comment — not a boundary. |
| 38 | + "-- ; not a real semicolon\nSELECT 1", |
| 39 | + # Semicolon inside block comment — not a boundary. |
| 40 | + "/* ; not real */ SELECT 1", |
| 41 | + # Semicolon inside double-quoted identifier — not a |
| 42 | + # boundary (SQLite identifier-quoting rule). |
| 43 | + 'SELECT "col;name" FROM t', |
| 44 | + # Empty SQL (covered by a sibling issue's empty-SQL |
| 45 | + # classification fix; here it must NOT be flagged as |
| 46 | + # multi-statement). |
| 47 | + "", |
| 48 | + " ", |
| 49 | + "-- comment\n", |
| 50 | + ], |
| 51 | + ) |
| 52 | + def test_single_statement_not_flagged(self, sql: str) -> None: |
| 53 | + assert _is_multi_statement(sql) is False |
| 54 | + |
| 55 | + @pytest.mark.parametrize( |
| 56 | + "sql", |
| 57 | + [ |
| 58 | + # Two DML statements. |
| 59 | + "INSERT INTO t VALUES (1); INSERT INTO t VALUES (2)", |
| 60 | + # DDL + DDL. |
| 61 | + "CREATE TABLE a (x); CREATE TABLE b (y)", |
| 62 | + # Mixed DDL + DML. |
| 63 | + "CREATE TABLE t (x); INSERT INTO t VALUES (1)", |
| 64 | + # Whitespace-then-statement after the first ``;``. |
| 65 | + "SELECT 1; SELECT 2", |
| 66 | + # Comment + second statement past the first ``;``. |
| 67 | + "SELECT 1; /* sep */ SELECT 2", |
| 68 | + # Double semicolon followed by another statement — |
| 69 | + # stdlib treats consecutive ``;`` as separate statements. |
| 70 | + "SELECT 1;; SELECT 2", |
| 71 | + ], |
| 72 | + ) |
| 73 | + def test_multi_statement_flagged(self, sql: str) -> None: |
| 74 | + assert _is_multi_statement(sql) is True |
| 75 | + |
| 76 | + |
| 77 | +class TestExecuteRejectsMultiStatementSync: |
| 78 | + def test_rejects_two_dml(self) -> None: |
| 79 | + from dqlitedbapi.connection import Connection |
| 80 | + from dqlitedbapi.cursor import Cursor |
| 81 | + from dqlitedbapi.exceptions import ProgrammingError |
| 82 | + |
| 83 | + conn = Connection("localhost:19001", timeout=2.0) |
| 84 | + cur = Cursor(conn) |
| 85 | + with pytest.raises(ProgrammingError, match="one statement at a time"): |
| 86 | + cur.execute("INSERT INTO t VALUES (1); INSERT INTO t VALUES (2)") |
| 87 | + |
| 88 | + def test_accepts_single_statement_with_trailing_comment(self) -> None: |
| 89 | + from dqlitedbapi.connection import Connection |
| 90 | + from dqlitedbapi.cursor import Cursor |
| 91 | + from dqlitedbapi.exceptions import ProgrammingError |
| 92 | + |
| 93 | + conn = Connection("localhost:19001", timeout=2.0) |
| 94 | + cur = Cursor(conn) |
| 95 | + # Should not raise the multi-statement error. (It will fail |
| 96 | + # at the wire round-trip because the server isn't running, |
| 97 | + # but that's a separate error class.) |
| 98 | + with pytest.raises(Exception) as excinfo: |
| 99 | + cur.execute("SELECT 1; -- comment") |
| 100 | + assert not isinstance(excinfo.value, ProgrammingError) or ( |
| 101 | + "one statement at a time" not in str(excinfo.value) |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +class TestExecuteRejectsMultiStatementAsync: |
| 107 | + async def test_rejects_two_dml(self) -> None: |
| 108 | + from dqlitedbapi.aio.connection import AsyncConnection |
| 109 | + from dqlitedbapi.aio.cursor import AsyncCursor |
| 110 | + from dqlitedbapi.exceptions import ProgrammingError |
| 111 | + |
| 112 | + conn = AsyncConnection("localhost:19001") |
| 113 | + cur = AsyncCursor(conn) |
| 114 | + with pytest.raises(ProgrammingError, match="one statement at a time"): |
| 115 | + await cur.execute("INSERT INTO t VALUES (1); INSERT INTO t VALUES (2)") |
0 commit comments