UNPKG

rawsql-ts

Version:

High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

47 lines 2.48 kB
import { WhereClause } from "../models/Clause"; import { SqlTokenizer } from "./SqlTokenizer"; import { ValueParser } from "./ValueParser"; import { CommentUtils } from "../utils/CommentUtils"; export class WhereClauseParser { // Parse SQL string to AST (was: parse) static parse(query) { const tokenizer = new SqlTokenizer(query); // Initialize tokenizer const lexemes = tokenizer.readLexmes(); // Get tokens // Parse const result = this.parseFromLexeme(lexemes, 0); // Error if there are remaining tokens if (result.newIndex < lexemes.length) { throw new Error(`Syntax error: Unexpected token "${lexemes[result.newIndex].value}" at position ${result.newIndex}. The WHERE clause is complete but there are additional tokens.`); } return result.value; } // Parse from lexeme array (was: parse) static parseFromLexeme(lexemes, index) { let idx = index; // Capture comments associated with the WHERE clause const whereLexeme = lexemes[idx]; const whereTokenComments = CommentUtils.collectClauseComments(lexemes, idx, 'where'); // Preserve positioned comments from the WHERE keyword so we can reapply them during printing const wherePositionedComments = whereLexeme.positionedComments; if (whereLexeme.value !== 'where') { throw new Error(`Syntax error at position ${idx}: Expected 'WHERE' keyword but found "${lexemes[idx].value}". WHERE clauses must start with the WHERE keyword.`); } idx++; if (idx >= lexemes.length) { throw new Error(`Syntax error: Unexpected end of input after 'WHERE' keyword. The WHERE clause requires a condition expression.`); } const item = ValueParser.parseFromLexeme(lexemes, idx); const clause = new WhereClause(item.value); // Set comments from the WHERE token to the clause clause.comments = whereTokenComments; if (wherePositionedComments && wherePositionedComments.length > 0) { // Clone positioned comments so mutations on the clause do not leak back to the tokenizer state clause.positionedComments = wherePositionedComments.map((comment) => ({ position: comment.position, comments: [...comment.comments], })); } return { value: clause, newIndex: item.newIndex }; } } //# sourceMappingURL=WhereClauseParser.js.map