rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
64 lines • 3.86 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 Clause_1 = require("../models/Clause");
const LexemeCommentUtils_1 = require("./utils/LexemeCommentUtils");
const SelectClauseParser_1 = require("./SelectClauseParser");
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;
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 items = [];
// Parse first item
const firstItemResult = SelectClauseParser_1.SelectItemParser.parseItem(lexemes, idx);
items.push(firstItemResult.value);
idx = firstItemResult.newIndex;
// Parse subsequent items separated by commas
while (idx < lexemes.length && (lexemes[idx].type & Lexeme_1.TokenType.Comma)) {
const commaLexeme = lexemes[idx];
// We might want to attach comma comments to the previous item or next item
// SelectItemParser handles its own leading comments, but comma trailing comments might need handling.
// For simplicity, we let SelectItemParser handle comments attached to the item start.
// If we want to preserve comma comments precisely, we might need more logic,
// but SelectClauseParser doesn't seem to do anything special for comma comments other than skipping them.
// However, ReturningClauseParser had complex comment handling.
// Let's rely on SelectItemParser for now as it is robust for SELECT lists.
idx++; // skip comma
const itemResult = SelectClauseParser_1.SelectItemParser.parseItem(lexemes, idx);
items.push(itemResult.value);
idx = itemResult.newIndex;
}
if (items.length === 0) {
const position = (_e = (_d = (_c = lexemes[idx]) === null || _c === void 0 ? void 0 : _c.position) === null || _d === void 0 ? void 0 : _d.startPosition) !== null && _e !== void 0 ? _e : idx;
throw new Error(`[ReturningClauseParser] Expected a column or '*' after RETURNING at position ${position}.`);
}
const clause = new Clause_1.ReturningClause(items);
if (returningComments.before.length > 0) {
clause.addPositionedComments("before", returningComments.before);
}
// returningComments.after are usually attached to the first item by SelectItemParser if they are immediately before it?
// Actually extractLexemeComments(returningLexeme) gets comments attached to RETURNING token.
// If there are comments after RETURNING, they should be attached to the clause or the first item.
// In the original parser:
// let pendingBeforeForNext: string[] = [...returningComments.after];
// And then added to the first column.
if (returningComments.after.length > 0 && items.length > 0) {
items[0].addPositionedComments("before", returningComments.after);
}
return { value: clause, newIndex: idx };
}
}
exports.ReturningClauseParser = ReturningClauseParser;
//# sourceMappingURL=ReturningClauseParser.js.map