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..90e9095 --- /dev/null +++ b/test/parser/mysql.spec.ts @@ -0,0 +1,53 @@ +import { parse } from '../../src/parser'; +import { expect } from 'chai'; + +describe('Parser for MySQL', () => { + it('parses a CREATE PROCEDURE query with DEFINER user_name@host_name', () => { + 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); + }); + + 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'); + }); +});