rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
78 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReturningClauseParser = void 0;
// Provides parsing for RETURNING clauses in SQL (used in UPDATE, INSERT, DELETE, etc.)
const Lexeme_1 = require("../models/Lexeme");
const ValueComponent_1 = require("../models/ValueComponent");
const Clause_1 = require("../models/Clause");
const LexemeCommentUtils_1 = require("./utils/LexemeCommentUtils");
class ReturningClauseParser {
/**
* Parse RETURNING clause from lexemes, starting at the given index.
* Returns a ReturningClause instance and the new index after parsing.
*/
static parseFromLexeme(lexemes, index) {
var _a, _b, _c, _d, _e, _f;
let idx = index;
if (((_a = lexemes[idx]) === null || _a === void 0 ? void 0 : _a.value) !== "returning") {
throw new Error(`Syntax error at position ${idx}: Expected 'RETURNING' but found '${(_b = lexemes[idx]) === null || _b === void 0 ? void 0 : _b.value}'.`);
}
const returningLexeme = lexemes[idx];
const returningComments = (0, LexemeCommentUtils_1.extractLexemeComments)(returningLexeme);
idx++;
const columns = [];
// Track inline comments that should precede the next returning column.
let pendingBeforeForNext = [...returningComments.after];
while (idx < lexemes.length) {
const lexeme = lexemes[idx];
let column = null;
if (lexeme.type & Lexeme_1.TokenType.Identifier) {
column = new ValueComponent_1.IdentifierString(lexeme.value);
}
else if (lexeme.value === "*") {
column = new ValueComponent_1.IdentifierString("*");
}
if (!column) {
break;
}
const columnComments = (0, LexemeCommentUtils_1.extractLexemeComments)(lexeme);
const beforeComments = [];
if (pendingBeforeForNext.length > 0) {
beforeComments.push(...pendingBeforeForNext);
}
if (columnComments.before.length > 0) {
beforeComments.push(...columnComments.before);
}
if (beforeComments.length > 0) {
column.addPositionedComments("before", beforeComments);
}
if (columnComments.after.length > 0) {
column.addPositionedComments("after", columnComments.after);
}
columns.push(column);
pendingBeforeForNext = [];
idx++;
if (((_c = lexemes[idx]) === null || _c === void 0 ? void 0 : _c.type) === Lexeme_1.TokenType.Comma) {
const commaComments = (0, LexemeCommentUtils_1.extractLexemeComments)(lexemes[idx]);
pendingBeforeForNext = [...commaComments.after];
idx++;
continue;
}
break;
}
if (pendingBeforeForNext.length > 0 && columns.length > 0) {
columns[columns.length - 1].addPositionedComments("after", pendingBeforeForNext);
}
if (columns.length === 0) {
const position = (_f = (_e = (_d = lexemes[idx]) === null || _d === void 0 ? void 0 : _d.position) === null || _e === void 0 ? void 0 : _e.startPosition) !== null && _f !== void 0 ? _f : idx;
throw new Error(`[ReturningClauseParser] Expected a column or '*' after RETURNING at position ${position}.`);
}
const clause = new Clause_1.ReturningClause(columns);
if (returningComments.before.length > 0) {
clause.addPositionedComments("before", returningComments.before);
}
return { value: clause, newIndex: idx };
}
}
exports.ReturningClauseParser = ReturningClauseParser;
//# sourceMappingURL=ReturningClauseParser.js.map