|
|
@@ -15,87 +15,13 @@
|
|
|
// limitations under the License.
|
|
|
|
|
|
import sqlStatementsParser from './sqlStatementsParser';
|
|
|
+import { testParser, runSharedStatementsParserTests } from './sharedStatementsParserTests.ts';
|
|
|
|
|
|
describe('sqlStatementsParser.js', () => {
|
|
|
- const stringifySplitResult = function (result) {
|
|
|
- let s = '[';
|
|
|
- let first = true;
|
|
|
- result.forEach(entry => {
|
|
|
- s += first ? '{\n' : ', {\n';
|
|
|
- s += " statement: '" + entry.statement.replace(/\n/g, '\\n') + "',\n";
|
|
|
- s +=
|
|
|
- ' location: { first_line: ' +
|
|
|
- entry.location.first_line +
|
|
|
- ', first_column: ' +
|
|
|
- entry.location.first_column +
|
|
|
- ', last_line: ' +
|
|
|
- entry.location.last_line +
|
|
|
- ', last_column: ' +
|
|
|
- entry.location.last_column +
|
|
|
- ' }';
|
|
|
- if (entry.firstToken) {
|
|
|
- s += ",\n firstToken: '" + entry.firstToken + "'";
|
|
|
- }
|
|
|
- if (entry.database) {
|
|
|
- s += ",\n database: '" + entry.database + "'";
|
|
|
- }
|
|
|
- s += '\n}';
|
|
|
- first = false;
|
|
|
- });
|
|
|
- s += ']';
|
|
|
- return s;
|
|
|
- };
|
|
|
-
|
|
|
- const testParser = function (input, expectedOutput) {
|
|
|
- try {
|
|
|
- expectedOutput.forEach(entry => {
|
|
|
- entry.type = 'statement';
|
|
|
- });
|
|
|
- const result = sqlStatementsParser.parse(input);
|
|
|
- const because =
|
|
|
- '\n\nParser output: ' +
|
|
|
- stringifySplitResult(result) +
|
|
|
- '\nExpected output: ' +
|
|
|
- stringifySplitResult(expectedOutput);
|
|
|
-
|
|
|
- expect(result).toEqual(expectedOutput, because);
|
|
|
- } catch (error) {
|
|
|
- console.error(error);
|
|
|
- console.warn(error.message);
|
|
|
-
|
|
|
- fail('Got error');
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- it('should split "" correctly', () => {
|
|
|
- testParser('', [
|
|
|
- {
|
|
|
- statement: '',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 0 }
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split ";" correctly', () => {
|
|
|
- testParser(';', [
|
|
|
- {
|
|
|
- statement: ';',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should handle "-" correctly', () => {
|
|
|
- testParser('-', [
|
|
|
- {
|
|
|
- statement: '-',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
+ runSharedStatementsParserTests(sqlStatementsParser);
|
|
|
|
|
|
it('should handle "/" correctly', () => {
|
|
|
- testParser('/', [
|
|
|
+ testParser(sqlStatementsParser, '/', [
|
|
|
{
|
|
|
statement: '/',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
|
|
|
@@ -104,7 +30,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
});
|
|
|
|
|
|
it('should split escaped \\ correctly in single quotes', () => {
|
|
|
- testParser("SELECT '\\\\';\n SELECT 1;", [
|
|
|
+ testParser(sqlStatementsParser, "SELECT '\\\\';\n SELECT 1;", [
|
|
|
{
|
|
|
type: 'statement',
|
|
|
statement: "SELECT '\\\\';",
|
|
|
@@ -121,7 +47,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
});
|
|
|
|
|
|
it('should split escaped \\ correctly in double quotes', () => {
|
|
|
- testParser('SELECT "\\\\";\n SELECT 1;', [
|
|
|
+ testParser(sqlStatementsParser, 'SELECT "\\\\";\n SELECT 1;', [
|
|
|
{
|
|
|
type: 'statement',
|
|
|
statement: 'SELECT "\\\\";',
|
|
|
@@ -138,7 +64,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
});
|
|
|
|
|
|
it('should split "; \\n; \\r\\n;" correctly', () => {
|
|
|
- testParser('; \n; \r\n;', [
|
|
|
+ testParser(sqlStatementsParser, '; \n; \r\n;', [
|
|
|
{
|
|
|
statement: ';',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
|
|
|
@@ -154,18 +80,8 @@ describe('sqlStatementsParser.js', () => {
|
|
|
]);
|
|
|
});
|
|
|
|
|
|
- it('should split "select * from bla" correctly', () => {
|
|
|
- testParser('select * from bla', [
|
|
|
- {
|
|
|
- statement: 'select * from bla',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 17 },
|
|
|
- firstToken: 'select'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
it('should split ";;select * from bla" correctly', () => {
|
|
|
- testParser(';;select * from bla', [
|
|
|
+ testParser(sqlStatementsParser, ';;select * from bla', [
|
|
|
{
|
|
|
statement: ';',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 1, last_column: 1 }
|
|
|
@@ -182,18 +98,8 @@ describe('sqlStatementsParser.js', () => {
|
|
|
]);
|
|
|
});
|
|
|
|
|
|
- it('should split "select * from bla;" correctly', () => {
|
|
|
- testParser('select * from bla;', [
|
|
|
- {
|
|
|
- statement: 'select * from bla;',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
|
|
|
- firstToken: 'select'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
it('should split "select * from bla;select * from ble" correctly', () => {
|
|
|
- testParser('select * from bla;select * from ble', [
|
|
|
+ testParser(sqlStatementsParser, 'select * from bla;select * from ble', [
|
|
|
{
|
|
|
statement: 'select * from bla;',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
|
|
|
@@ -208,7 +114,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
});
|
|
|
|
|
|
it('should split "select * from bla;;select * from ble" correctly', () => {
|
|
|
- testParser('select * from bla;;select * from ble', [
|
|
|
+ testParser(sqlStatementsParser, 'select * from bla;;select * from ble', [
|
|
|
{
|
|
|
statement: 'select * from bla;',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
|
|
|
@@ -227,7 +133,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
});
|
|
|
|
|
|
it('should split "select * from bla;\\n;select * from ble" correctly', () => {
|
|
|
- testParser('select * from bla;\n;select * from ble', [
|
|
|
+ testParser(sqlStatementsParser, 'select * from bla;\n;select * from ble', [
|
|
|
{
|
|
|
statement: 'select * from bla;',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 1, last_column: 18 },
|
|
|
@@ -246,7 +152,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
});
|
|
|
|
|
|
it('should split "select * \\nfrom bla;\\r\\nselect * from ble;\\n" correctly', () => {
|
|
|
- testParser('select * \nfrom bla;\r\nselect * from ble;\n', [
|
|
|
+ testParser(sqlStatementsParser, 'select * \nfrom bla;\r\nselect * from ble;\n', [
|
|
|
{
|
|
|
statement: 'select * \nfrom bla;',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 2, last_column: 9 },
|
|
|
@@ -260,18 +166,9 @@ describe('sqlStatementsParser.js', () => {
|
|
|
]);
|
|
|
});
|
|
|
|
|
|
- it('should split "select * from bla where x = ";";" correctly', () => {
|
|
|
- testParser('select * from bla where x = ";";', [
|
|
|
- {
|
|
|
- statement: 'select * from bla where x = ";";',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 32 },
|
|
|
- firstToken: 'select'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
it('should split "select * from bla where x = \';\';\\n\\nSELECT bla FROM foo WHERE y = `;` AND true = false;" correctly', () => {
|
|
|
testParser(
|
|
|
+ sqlStatementsParser,
|
|
|
"select * from bla where x = ';';\n\nSELECT bla FROM foo WHERE y = `;` AND true = false;",
|
|
|
[
|
|
|
{
|
|
|
@@ -288,108 +185,9 @@ describe('sqlStatementsParser.js', () => {
|
|
|
);
|
|
|
});
|
|
|
|
|
|
- it('should split "select * from bla where x = "; AND boo = 1;\\n\\nUSE db" correctly', () => {
|
|
|
- testParser('select * from bla where x = "; AND boo = 1;\n\nUSE db', [
|
|
|
- {
|
|
|
- statement: 'select * from bla where x = "; AND boo = 1;\n\nUSE db',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 3, last_column: 6 },
|
|
|
- firstToken: 'select'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "--- Some comment with ; ; \\nselect * from bla where x = ";";" correctly', () => {
|
|
|
- testParser('--- Some comment with ; ; \nselect * from bla where x = ";";', [
|
|
|
- {
|
|
|
- statement: '--- Some comment with ; ; \nselect * from bla where x = ";";',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 2, last_column: 32 },
|
|
|
- firstToken: 'select'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "select *\n-- bla\n from bla;" correctly', () => {
|
|
|
- testParser('select *\n-- bla\n from bla;', [
|
|
|
- {
|
|
|
- statement: 'select *\n-- bla\n from bla;',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 3, last_column: 10 },
|
|
|
- firstToken: 'select'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "select *\\n/* bla \\n\\n*/\\n from bla;" correctly', () => {
|
|
|
- testParser('select *\n/* bla \n\n*/\n from bla;', [
|
|
|
- {
|
|
|
- statement: 'select *\n/* bla \n\n*/\n from bla;',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 5, last_column: 10 },
|
|
|
- firstToken: 'select'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "SELECT\\n id -- some ID\\n FROM customers;" correctly', () => {
|
|
|
- testParser('SELECT\n id -- some ID\n FROM customers;', [
|
|
|
- {
|
|
|
- statement: 'SELECT\n id -- some ID\n FROM customers;',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 3, last_column: 16 },
|
|
|
- firstToken: 'SELECT'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "SELECT\n id -- some ID;\n FROM customers;" correctly', () => {
|
|
|
- testParser('SELECT\n id -- some ID;\n FROM customers;', [
|
|
|
- {
|
|
|
- statement: 'SELECT\n id -- some ID;\n FROM customers;',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 3, last_column: 16 },
|
|
|
- firstToken: 'SELECT'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "SELECT id\\n\\n /* from customers; */ FROM other;" correctly', () => {
|
|
|
- testParser('SELECT id\n\n /* from customers; */ FROM other;', [
|
|
|
- {
|
|
|
- statement: 'SELECT id\n\n /* from customers; */ FROM other;',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 3, last_column: 34 },
|
|
|
- firstToken: 'SELECT'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "SELECT ` " correctly', () => {
|
|
|
- testParser('SELECT ` ', [
|
|
|
- {
|
|
|
- statement: 'SELECT ` ',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
|
|
|
- firstToken: 'SELECT'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "SELECT " " correctly', () => {
|
|
|
- testParser('SELECT " ', [
|
|
|
- {
|
|
|
- statement: 'SELECT " ',
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
|
|
|
- firstToken: 'SELECT'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
- it('should split "SELECT \' " correctly', () => {
|
|
|
- testParser("SELECT ' ", [
|
|
|
- {
|
|
|
- statement: "SELECT ' ",
|
|
|
- location: { first_line: 1, first_column: 0, last_line: 1, last_column: 11 },
|
|
|
- firstToken: 'SELECT'
|
|
|
- }
|
|
|
- ]);
|
|
|
- });
|
|
|
-
|
|
|
it('should split "-- Some comment\\n\\n CREATE TABLE bla (id int); /* some \\nother comment*/ ALTER TABLE boo" correctly', () => {
|
|
|
testParser(
|
|
|
+ sqlStatementsParser,
|
|
|
'-- Some comment\n\n CREATE TABLE bla (id int); /* some \nother comment*/ ALTER TABLE boo',
|
|
|
[
|
|
|
{
|
|
|
@@ -408,6 +206,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
|
|
|
it('should split "SELECT " \\" ;; ", \'"\', \' ;\' from bla; /* \\n\\n"" ; \\n; */ FROM other;" correctly', () => {
|
|
|
testParser(
|
|
|
+ sqlStatementsParser,
|
|
|
'USE `db;`;\r\nSELECT " \\" ;; ", \'"\', \' ;\' from bla; /* \n\n"" ; \n; FROM other;*/',
|
|
|
[
|
|
|
{
|
|
|
@@ -430,7 +229,7 @@ describe('sqlStatementsParser.js', () => {
|
|
|
});
|
|
|
|
|
|
it('should find databases in use statements "-- commented USE statement\\nUSE boo;" correctly', () => {
|
|
|
- testParser('-- commented USE statement\nUSE boo;/* USE baa; */use `foo`', [
|
|
|
+ testParser(sqlStatementsParser, '-- commented USE statement\nUSE boo;/* USE baa; */use `foo`', [
|
|
|
{
|
|
|
statement: '-- commented USE statement\nUSE boo;',
|
|
|
location: { first_line: 1, first_column: 0, last_line: 2, last_column: 8 },
|