Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #552
splitSQLStatementsended a statement at every;outside a quote or comment. A SQLite trigger body and a PostgreSQL 14+BEGIN ATOMICfunction or procedure body hold;of their own, so the SQLite and PostgreSQL connectors, which run the pieces one by one, got incomplete statements. On SQLite everyCREATE TRIGGERfailed withincomplete input.The change is in
src/utils/sql-parser.ts. While scanning,splitWithnow reads the words of a statement (only outside comments, strings and quoted names, because it reads only the scanner's plain tokens) and keeps the body whole:CREATE [TEMP|TEMPORARY] TRIGGERopens a body at itsBEGIN.CREATEopens a body atBEGIN ATOMIC.ENDthat starts a statement. For SQLite this is the rulesqlite3_complete()uses: anENDthat comes right after a;. ACASE ... END, and a column that is namedend(SQLite acceptsUPDATE t SET end = 1), do not start a statement, so they do not close the body. The next;then ends the statement as before. PostgreSQL also allows an empty body,BEGIN ATOMIC END, so there anENDright afterATOMICcloses the body too. The same check runs for SQLite, so a trigger written asBEGIN ENDis closed there as well.sqlite3_complete()does not close on that one, but SQLite rejects a trigger with no statement in it either way.BEGIN, aBEGINin any other statement,BEGIN ATOMICinside a$$body, and a column or alias namedbegindo not open a body.ENDruns to the end of the input.The read-only check is not weakened. A body only opens in a statement that starts with
CREATE, which read-only mode refuses anyway. Classes change only fromunknowntoddlfor these statements, and no verdict changes.Behavior change: a body that is still open at the end of the input (a trigger with no
END) is now one piece instead of several. SQLite and PostgreSQL reject it either way.Tests:
sql-parser.test.ts: 14 new cases (9 fail on main).sqlite.integration.test.ts(no Docker): a trigger with two statements and aCASE, then an insert that fires it. Fails on main withincomplete input.manager.test.ts"should resolve an alias from ~/.ssh/config", which also fails on main on Windows (the test expects.ssh/configand gets a backslash path).tsc --noEmitis clean.I also checked it against real parsers and real files:
WHEN,ENDin strings, comments and quoted names, a column namedend, TEMP, IF NOT EXISTS, BEGIN/COMMIT/END statements, CRLF, non-ASCII names), split and compared with thesqlite3_complete()rule (the SQLite engine accepts 27541 of them): 0 differ with the change, 48302 differ on main. Every trigger in these scripts has at least one statement in its body.sqlite-utils4.2.1 database (FTS5 and counts triggers) runs throughSQLiteConnector.executeSQLand theexecute_sqltool, and the triggers fire. On main it fails withincomplete input.BEGIN ATOMICblocks of PostgreSQL'screate_function_sql.sql: 14 statements, all split correctly. On main all 10 blocks were split wrongly.CREATE ... TRIGGER ... BEGINorCREATE ... BEGIN ATOMIChead. No difference for MySQL, MariaDB, SQL Server, Oracle, ClickHouse or no dialect.Known limits: I had no PostgreSQL server, so the PostgreSQL half is checked against libpg-query and not against a running server.
CREATE RULE ... DO ALSO (stmt; stmt)is still split at its inner;, as on main.Two more limits.
EXPLAIN CREATE TRIGGER ...on SQLite is still cut at the inner semicolons, as on main. One case is new with this change: in PostgreSQL, two adjacent column namesbeginandatomic(SELECT 1 AS begin, atomic FROM t; SELECT 2) are read as a body start, so the next statement is joined to that piece. Read-only mode still judges the joined piece by its first statement.Validated with Claude Opus 5.5.