From 915b80b3a77507b517a88ef9ccc96ae120935e26 Mon Sep 17 00:00:00 2001 From: worksofliam Date: Fri, 20 Dec 2024 12:53:53 -0500 Subject: [PATCH] Refactor SQL validation logic to ensure statement ranges are valid before processing and improve error handling Signed-off-by: worksofliam --- src/language/providers/problemProvider.ts | 64 ++++++++++++----------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/language/providers/problemProvider.ts b/src/language/providers/problemProvider.ts index 536193fc..8992bfce 100644 --- a/src/language/providers/problemProvider.ts +++ b/src/language/providers/problemProvider.ts @@ -99,40 +99,44 @@ async function validateSqlDocument(document: TextDocument, specificStatement?: n } } - statementRanges.push(range); + if (range[0] !== range[1]) { + statementRanges.push(range); + } } } - const sqlStatementContents = statementRanges.map(range => content.substring(range[0], range[1])); - const se = performance.now(); - const syntaxChecked = await window.withProgress({location: ProgressLocation.Window, title: `$(sync-spin) Checking SQL Syntax`}, () => {return checker.checkMultipleStatements(sqlStatementContents)}); - const ee = performance.now(); - - if (syntaxChecked) { - if (syntaxChecked.length > 0) { - let errors: Diagnostic[] = []; - for (let i = 0; i < statementRanges.length; i++) { - const currentRange = statementRanges[i]; - const groupError = syntaxChecked[i]; - - if (shouldShowError(groupError)) { - - const selectedWord = document.getWordRangeAtPosition(document.positionAt(currentRange[0] + groupError.offset)) - || new Range( - document.positionAt(currentRange[0] + groupError.offset - 1), - document.positionAt(currentRange[0] + groupError.offset) - ); - - errors.push({ - message: `${groupError.text} - ${groupError.sqlstate}`, - code: groupError.sqlid, - range: selectedWord, - severity: diagnosticTypeMap[groupError.type], - }); + if (statementRanges.length > 0) { + const sqlStatementContents = statementRanges.map(range => content.substring(range[0], range[1])); + const se = performance.now(); + const syntaxChecked = await window.withProgress({ location: ProgressLocation.Window, title: `$(sync-spin) Checking SQL Syntax` }, () => { return checker.checkMultipleStatements(sqlStatementContents) }); + const ee = performance.now(); + + if (syntaxChecked) { + if (syntaxChecked.length > 0) { + let errors: Diagnostic[] = []; + for (let i = 0; i < statementRanges.length; i++) { + const currentRange = statementRanges[i]; + const groupError = syntaxChecked[i]; + + if (shouldShowError(groupError)) { + + const selectedWord = document.getWordRangeAtPosition(document.positionAt(currentRange[0] + groupError.offset)) + || new Range( + document.positionAt(currentRange[0] + groupError.offset - 1), + document.positionAt(currentRange[0] + groupError.offset) + ); + + errors.push({ + message: `${groupError.text} - ${groupError.sqlstate}`, + code: groupError.sqlid, + range: selectedWord, + severity: diagnosticTypeMap[groupError.type], + }); + } } - } - sqlDiagnosticCollection.set(document.uri, errors); + sqlDiagnosticCollection.set(document.uri, errors); + } } } } @@ -149,7 +153,7 @@ function getStatementRangeFromGroup(currentGroup: StatementGroup): StatementRang statementRange = [currentGroup.range.start, currentGroup.range.end]; const label = firstStatement.getLabel(); - if (label) { + if (label) { if (label.toUpperCase() === `CL`) { statementRange = undefined; } else {