rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
51 lines • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropConstraintParser = void 0;
const SqlTokenizer_1 = require("./SqlTokenizer");
const DDLStatements_1 = require("../models/DDLStatements");
const FullNameParser_1 = require("./FullNameParser");
/**
* Parses standalone DROP CONSTRAINT statements.
*/
class DropConstraintParser {
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(`[DropConstraintParser] Unexpected token "${lexemes[result.newIndex].value}" after DROP CONSTRAINT statement.`);
}
return result.value;
}
static parseFromLexeme(lexemes, index) {
var _a, _b, _c;
let idx = index;
if (((_a = lexemes[idx]) === null || _a === void 0 ? void 0 : _a.value.toLowerCase()) !== "drop constraint") {
throw new Error(`[DropConstraintParser] Expected DROP CONSTRAINT at index ${idx}.`);
}
idx++;
let ifExists = false;
if (((_b = lexemes[idx]) === null || _b === void 0 ? void 0 : _b.value.toLowerCase()) === "if exists") {
ifExists = true;
idx++;
}
const { name, newIndex } = FullNameParser_1.FullNameParser.parseFromLexeme(lexemes, idx);
idx = newIndex;
let behavior = null;
const nextValue = (_c = lexemes[idx]) === null || _c === void 0 ? void 0 : _c.value.toLowerCase();
if (nextValue === "cascade" || nextValue === "restrict") {
behavior = nextValue;
idx++;
}
return {
value: new DDLStatements_1.DropConstraintStatement({
constraintName: name,
ifExists,
behavior
}),
newIndex: idx
};
}
}
exports.DropConstraintParser = DropConstraintParser;
//# sourceMappingURL=DropConstraintParser.js.map