Skip to content

Commit

Permalink
Javascript: Fix the error assignment ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
surister committed Jul 6, 2024
1 parent d09333f commit 4ff09b1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
34 changes: 33 additions & 1 deletion cratedb_sqlparse_js/cratedb_sqlparse/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,6 +156,11 @@ export class Statement {
}
}

/**
*
* @param {String} string
* @returns {String}
*/
function trim(string) {
return string.replace(/^\s+|\s+$/gm, '');
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions cratedb_sqlparse_js/tests/exceptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 4ff09b1

Please sign in to comment.