Skip to content

Commit

Permalink
#211 Fix trigger query parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCovizzi committed Jun 4, 2022
1 parent 87421fa commit 5e402a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/sqlite/queryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function extractStatements(query: string): Statement[] {

let statement: Statement|undefined;
let isStmt = false;
let isInternalBlock = false;
let isString = false;
let isComment = false;
let isCommand = false;
Expand All @@ -18,6 +19,7 @@ export function extractStatements(query: string): Statement[] {
let char = line[charIndex];
let prevChar = charIndex>0? line[charIndex-1] : undefined;
let nextChar = charIndex<line.length-1? line[charIndex+1] : undefined;
let lastWord = (n: number) => line.substring(charIndex-n+1, charIndex+1)

if (isStmt) {
if (statement) statement.sql += char;
Expand All @@ -28,7 +30,13 @@ export function extractStatements(query: string): Statement[] {
} else if (!isString && char === '/' && nextChar === '*') {
isComment = true;
commentChar = '/';
} else if (!isString && !isComment && char === ';') {
} else if (!isString && !isComment && lastWord(5).toLowerCase() === "begin") {
isInternalBlock = true;
} else if (!isString && !isComment && lastWord(3).toLowerCase() === "end") {
isInternalBlock = false;
} else if (!isString && !isComment && isInternalBlock && lastWord(11).toLowerCase() === "transaction") {
isInternalBlock = false;
} else if (!isString && !isComment && !isInternalBlock && char === ';') {
isStmt = false;
if (statement) {
statement.position.end = [lineIndex, charIndex];
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/sqlite/queryParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,33 @@ describe("QueryParser Tests", function () {
expect(actual.map(s => s.sql)).toEqual(expected);
});

test("should parse query with TRIGGER and SELECT (issue #210)", function() {
let query = `CREATE TRIGGER newWidgetSale BEFORE UPDATE ON widgetSale
BEGIN
SELECT RAISE(ROLLBACK, 'cannot update table "widget sale"') FROM widgetSale WHERE id = NEW.id and reconciled = 1;
END
;`

let actual = queryParser.extractStatements(query);
let expected = [query];

expect(actual.map(s => s.sql)).toEqual(expected);
});

test("should parse query with transaction", function() {
let query = `BEGIN TRANSACTION; -- start
SELECT RAISE(ROLLBACK, 'cannot update table "widget sale"') FROM widgetSale WHERE id = NEW.id and reconciled = 1;
END TRANSACTION;`

let actual = queryParser.extractStatements(query);
let expected = [
"BEGIN TRANSACTION;",
"SELECT RAISE(ROLLBACK, 'cannot update table \"widget sale\"') FROM widgetSale WHERE id = NEW.id and reconciled = 1;",
"END TRANSACTION;"
];

expect(actual.map(s => s.sql)).toEqual(expected);
});


});

0 comments on commit 5e402a9

Please sign in to comment.