From 9647c08e9fd69b500fc63854222a8ca5f0a0f11d Mon Sep 17 00:00:00 2001 From: azmy60 Date: Wed, 3 Jun 2026 11:21:50 +0700 Subject: [PATCH 1/2] fix error parsing definer --- src/parser.ts | 4 +++- test/parser/mysql.spec.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/parser/mysql.spec.ts diff --git a/src/parser.ts b/src/parser.ts index a1797be..aec8a28 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1047,7 +1047,9 @@ function stateMachineStatementParser( } if (statement.definer !== undefined && statement.definer > 0) { - if (statement.definer === 1 && prevToken?.type === 'whitespace') { + // Enter consume mode once the user@host begins, with or without a + // whitespace separating it from "=". + if (statement.definer === 1) { statement.definer++; setPrevToken(token); return; diff --git a/test/parser/mysql.spec.ts b/test/parser/mysql.spec.ts new file mode 100644 index 0000000..ec99318 --- /dev/null +++ b/test/parser/mysql.spec.ts @@ -0,0 +1,27 @@ +import { parse } from '../../src/parser'; +import { expect } from 'chai'; + +describe('Parser for MySQL', () => { + it('parses a CREATE DEFINER query with wildcard', () => { + const sql = + 'CREATE DEFINER=`example_user`@`%` PROCEDURE test_proc()\n' + + 'BEGIN\n' + + ' SELECT 1;\n' + + 'END;'; + + const result = parse(sql, true, 'mysql'); + expect(result.body.length).to.eql(1); + expect(result.body[0].type).to.eql('CREATE_PROCEDURE'); + expect(result.body[0].executionType).to.eql('MODIFICATION'); + + const sql2 = + 'CREATE DEFINER=`example_user`@`%` PROCEDURE test_proc()\n' + + 'BEGIN\n' + + ' SELECT 1;\n' + + 'END;\n' + + `SELECT 1 as one;`; + + const result2 = parse(sql2, true, 'mysql'); + expect(result2.body.length).to.eql(2); + }); +}); From e09f4920221a49ef05ce377ce091e76315adb9d6 Mon Sep 17 00:00:00 2001 From: azmy60 Date: Wed, 3 Jun 2026 12:42:33 +0700 Subject: [PATCH 2/2] update tests --- test/parser/mysql.spec.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/parser/mysql.spec.ts b/test/parser/mysql.spec.ts index ec99318..90e9095 100644 --- a/test/parser/mysql.spec.ts +++ b/test/parser/mysql.spec.ts @@ -2,7 +2,7 @@ import { parse } from '../../src/parser'; import { expect } from 'chai'; describe('Parser for MySQL', () => { - it('parses a CREATE DEFINER query with wildcard', () => { + it('parses a CREATE PROCEDURE query with DEFINER user_name@host_name', () => { const sql = 'CREATE DEFINER=`example_user`@`%` PROCEDURE test_proc()\n' + 'BEGIN\n' + @@ -24,4 +24,30 @@ describe('Parser for MySQL', () => { const result2 = parse(sql2, true, 'mysql'); expect(result2.body.length).to.eql(2); }); + + it('parses a CREATE PROCEDURE query with DEFINER=CURRENT_USER', () => { + const sql = + 'CREATE DEFINER=CURRENT_USER PROCEDURE test_proc()\n' + + 'BEGIN\n' + + ' SELECT 1;\n' + + 'END;'; + + const result = parse(sql, true, 'mysql'); + expect(result.body.length).to.eql(1); + expect(result.body[0].type).to.eql('CREATE_PROCEDURE'); + expect(result.body[0].executionType).to.eql('MODIFICATION'); + }); + + it('parses a CREATE PROCEDURE query with DEFINER=CURRENT_USER()', () => { + const sql = + 'CREATE DEFINER=CURRENT_USER() PROCEDURE test_proc()\n' + + 'BEGIN\n' + + ' SELECT 1;\n' + + 'END;'; + + const result = parse(sql, true, 'mysql'); + expect(result.body.length).to.eql(1); + expect(result.body[0].type).to.eql('CREATE_PROCEDURE'); + expect(result.body[0].executionType).to.eql('MODIFICATION'); + }); });