rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
47 lines • 1.84 kB
JavaScript
import { SqlTokenizer } from "./SqlTokenizer";
import { DropConstraintStatement } from "../models/DDLStatements";
import { FullNameParser } from "./FullNameParser";
/**
* Parses standalone DROP CONSTRAINT statements.
*/
export class DropConstraintParser {
static parse(sql) {
const tokenizer = new 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.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 DropConstraintStatement({
constraintName: name,
ifExists,
behavior
}),
newIndex: idx
};
}
}
//# sourceMappingURL=DropConstraintParser.js.map