From 4ff09b1a0d6605763fa6af125d261d78aeadf3a5 Mon Sep 17 00:00:00 2001 From: surister Date: Sat, 6 Jul 2024 17:44:23 +0200 Subject: [PATCH] Javascript: Fix the error assignment ordering --- .../cratedb_sqlparse/parser.js | 34 ++++++++++++++++++- cratedb_sqlparse_js/tests/exceptions.test.js | 12 +++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cratedb_sqlparse_js/cratedb_sqlparse/parser.js b/cratedb_sqlparse_js/cratedb_sqlparse/parser.js index ad67f90..09a40ad 100644 --- a/cratedb_sqlparse_js/cratedb_sqlparse/parser.js +++ b/cratedb_sqlparse_js/cratedb_sqlparse/parser.js @@ -29,6 +29,15 @@ SqlBaseLexer.prototype.END_DOLLAR_QUOTED_STRING_sempred = END_DOLLAR_QUOTED_STRI export class ParseError extends Error { name = 'ParseError' + /** + * + * @param {String} query + * @param {String} msg + * @param offending_token + * @param e + * @member {String} errorMessage + * @member {String} errorMessageVerbose + */ constructor(query, msg, offending_token, e) { super(msg); this.query = query; @@ -147,6 +156,11 @@ export class Statement { } } +/** + * + * @param {String} string + * @returns {String} + */ function trim(string) { return string.replace(/^\s+|\s+$/gm, ''); } @@ -163,7 +177,7 @@ function findSuitableError(statement, errors) { errorQuery = trim(errorQuery); // If a good match error_query contains statement.query - if (statement.query.includes(errorQuery)) { + if (errorQuery.includes(statement.query)) { statement.exception = error; errors.splice(errors.indexOf(error), 1); } @@ -200,6 +214,24 @@ export function sqlparse(query, raise_exception = false) { statements.push(stmt) } + if (errorListener.errors.length === 1) { + // Fixme, what if there are two unassigned errors ? + // can that even be possible? + let error = errorListener.errors[0] + + for (const stmt of statements) { + if (stmt.exception === null && stmt.query.includes(error.query)) { + stmt.exception = error + break; + } + } + } + + if (errorListener.errors.length > 1) { + console.error("Could not match errors to queries, too much ambiguity, please report it opening an issue with the query.") + } + + const stmtEnricher = new AstBuilder() for (const stmt of statements) { diff --git a/cratedb_sqlparse_js/tests/exceptions.test.js b/cratedb_sqlparse_js/tests/exceptions.test.js index 76bd7be..d2ba4d5 100644 --- a/cratedb_sqlparse_js/tests/exceptions.test.js +++ b/cratedb_sqlparse_js/tests/exceptions.test.js @@ -27,7 +27,8 @@ test('Several Errors should be collected and not thrown by default', () => { expect(stmts[2].exception).not.toBeNull(); }) -test('Several Errors should be collected and not thrown by default 2', () => { +test('Several Errors should be collected and not thrown by default 2, special case*', () => { + // This query is an special case, see parser const stmts = sqlparse(` SELEC 1; SELECT A, B, C, D FROM tbl1; @@ -41,12 +42,11 @@ test('Several Errors should be collected and not thrown by default 2', () => { }) test('Several Errors should be collected and not thrown by default 3', () => { - const stmts = sqlparse(` + // language=SQL format=false +const stmts = sqlparse(` SELECT 1; - SELECT A, B, C, D - FROM tbl1; - INSERT INTO doc.tbl - VALUES (1, 2, 'three', ['four']); + SELECT A, B, C, D FROM tbl1; + INSERT INTO doc.tbl VALUES (1, 2, 'three', ['four']); `) expect(stmts).length(3)