rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
65 lines • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentOnParser = void 0;
const DDLStatements_1 = require("../models/DDLStatements");
const ValueComponent_1 = require("../models/ValueComponent");
const FullNameParser_1 = require("./FullNameParser");
const SqlTokenizer_1 = require("./SqlTokenizer");
const ValueParser_1 = require("./ValueParser");
/**
* Parses COMMENT ON TABLE/COLUMN statements.
*/
class CommentOnParser {
/**
* Parses a full SQL string containing a single COMMENT ON statement.
* @param sql SQL text containing one COMMENT ON TABLE/COLUMN statement.
* @returns Parsed COMMENT ON statement model.
*/
static parse(sql) {
const tokenizer = new SqlTokenizer_1.SqlTokenizer(sql);
const lexemes = tokenizer.readLexemes();
const result = this.parseFromLexeme(lexemes, 0);
if (result.newIndex < lexemes.length) {
throw new Error(`[CommentOnParser] Unexpected token "${lexemes[result.newIndex].value}" after COMMENT ON statement.`);
}
return result.value;
}
/**
* Parses COMMENT ON tokens from a lexeme array starting at the specified index.
* @param lexemes Tokenized SQL lexemes.
* @param index Lexeme index where COMMENT ON parsing starts.
* @returns Parsed statement and the next unread lexeme index.
*/
static parseFromLexeme(lexemes, index) {
var _a, _b, _c;
let idx = index;
const command = (_a = lexemes[idx]) === null || _a === void 0 ? void 0 : _a.value.toLowerCase();
if (command !== "comment on table" && command !== "comment on column") {
throw new Error(`[CommentOnParser] Expected COMMENT ON TABLE or COMMENT ON COLUMN at index ${idx}.`);
}
const targetKind = command === "comment on table" ? "table" : "column";
idx++;
const targetResult = FullNameParser_1.FullNameParser.parseFromLexeme(lexemes, idx);
const target = new ValueComponent_1.QualifiedName(targetResult.namespaces, targetResult.name);
idx = targetResult.newIndex;
if (((_b = lexemes[idx]) === null || _b === void 0 ? void 0 : _b.value.toLowerCase()) !== "is") {
throw new Error(`[CommentOnParser] Expected IS keyword at index ${idx}.`);
}
idx++;
if (((_c = lexemes[idx]) === null || _c === void 0 ? void 0 : _c.value.toLowerCase()) === "null") {
idx++;
return {
value: new DDLStatements_1.CommentOnStatement({ targetKind, target, comment: null }),
newIndex: idx,
};
}
const valueResult = ValueParser_1.ValueParser.parseFromLexeme(lexemes, idx);
idx = valueResult.newIndex;
return {
value: new DDLStatements_1.CommentOnStatement({ targetKind, target, comment: valueResult.value }),
newIndex: idx,
};
}
}
exports.CommentOnParser = CommentOnParser;
//# sourceMappingURL=CommentOnParser.js.map