sql-query-identifier icon indicating copy to clipboard operation
sql-query-identifier copied to clipboard

Add detection for transaction statements

Open not-night-but opened this issue 3 months ago • 7 comments

Add detection for transaction statements like BEGIN TRANSACTION, START TRANSACTION, COMMIT, ROLLBACK, etc. I am probably missing some corner cases here and there may be some syntax I'm not aware of but this seems to work

not-night-but avatar Nov 13 '25 06:11 not-night-but

@not-night-but Is this ready for merging? Is it possible to fix the lint build?

azmy60 avatar Nov 13 '25 07:11 azmy60

@azmy60 I'm not sure why the build is failing, it runs fine on my machine, the errors in the pipeline are from node modules, and I can't rerun the workflow to check if gh is just being stupid

not-night-but avatar Nov 13 '25 07:11 not-night-but

I've pinned the version of @types/node in #84 which seems to have resolved the lint issue on CI, which I'm guessing was from it pulling in a very new version of the dependency that somehow clashed with the version of node running it or something. 🤷

MasterOdin avatar Nov 20 '25 19:11 MasterOdin

Alrighty I'll look into it and address your feedback. Thanks for the review!

not-night-but avatar Nov 20 '25 19:11 not-night-but

@not-night-but Does this support all START/BEGIN syntaxes?

Disclaimer: I had AI help me with these, need double checking

-- ANSI-ish / generic
START TRANSACTION;              -- Most portable explicit form
BEGIN;                          -- Very common, but overloaded in some dialects
BEGIN TRANSACTION;              -- Accepted by several engines

-- MySQL / MariaDB
-- https://dev.mysql.com/doc/refman/8.4/en/commit.html
START TRANSACTION;
BEGIN;                          -- Alias
BEGIN WORK;                     -- Alias
START TRANSACTION READ ONLY;    -- With characteristics
START TRANSACTION ISOLATION LEVEL SERIALIZABLE;

-- PostgreSQL (and Redshift, CockroachDB, YugabyteDB)
-- https://www.postgresql.org/docs/current/sql-start-transaction.html
BEGIN;
START TRANSACTION;
BEGIN TRANSACTION;
START TRANSACTION ISOLATION LEVEL REPEATABLE READ;

-- SQL Server (T-SQL)
-- https://learn.microsoft.com/en-us/sql/t-sql/language-elements/begin-transaction-transact-sql?view=sql-server-ver17
BEGIN TRANSACTION;
BEGIN TRAN;                     -- Shorthand
BEGIN TRANSACTION txn_name;     -- Named transaction

-- SQLite
-- https://sqlite.org/lang_transaction.html
BEGIN;                          -- Deferred by default
BEGIN TRANSACTION;
BEGIN DEFERRED TRANSACTION;
BEGIN IMMEDIATE TRANSACTION;
BEGIN EXCLUSIVE TRANSACTION;

-- Oracle
-- No explicit BEGIN TRANSACTION.
-- Transaction starts implicitly on first DML.
SET TRANSACTION READ ONLY;      -- You may set characteristics
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

-- Snowflake
-- https://docs.snowflake.com/en/sql-reference/transactions#explicit-transactions
BEGIN;
BEGIN TRANSACTION;
START TRANSACTION;

-- BigQuery (scripting)
BEGIN;
BEGIN TRANSACTION;

-- Firebird
-- Started via client API normally; SQL uses SET TRANSACTION.
SET TRANSACTION READ WRITE;

-- MS Access / Jet SQL
BEGIN TRANSACTION;

rathboma avatar Nov 21 '25 18:11 rathboma

Most of these might already be captured, but we probably need tests either way

rathboma avatar Nov 21 '25 18:11 rathboma

@rathboma Added those test cases, looks like it all works. One thing to note though is that specifically for mysql dbs, Those aliases (BEGIN and BEGIN WORK) do not work as transactions within procedures, functions, triggers, and events, but work as the beginning of compound statements. I'm not sure if this will be an issue for us as that whole statement is just identified as a CREATE_PROCEDURE statement, etc but I wanted to at least bring it up

not-night-but avatar Nov 25 '25 05:11 not-night-but