rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
79 lines • 3.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DropSchemaParser = void 0;
const SqlTokenizer_1 = require("./SqlTokenizer");
const DDLStatements_1 = require("../models/DDLStatements");
const Lexeme_1 = require("../models/Lexeme");
const FullNameParser_1 = require("./FullNameParser");
const ValueComponent_1 = require("../models/ValueComponent");
/**
* Parses DROP SCHEMA statements.
*/
class DropSchemaParser {
/**
* Parses a DROP SCHEMA statement from raw SQL and validates that the entire string is consumed.
* @param sql - The SQL string to parse
* @returns The parsed DropSchemaStatement
* @throws Error if the SQL string contains unexpected trailing tokens or is malformed
*/
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(`[DropSchemaParser] Unexpected token "${lexemes[result.newIndex].value}" after DROP SCHEMA statement.`);
}
return result.value;
}
/**
* Parses a DROP SCHEMA statement from lexemes starting at the provided index.
* @param lexemes - The lexeme stream that contains the statement
* @param index - The position within the lexeme stream where DROP SCHEMA should begin
* @returns An object containing the parsed DropSchemaStatement and the new lexeme index
*/
static parseFromLexeme(lexemes, index) {
var _a, _b, _c, _d;
let idx = index;
if (((_a = lexemes[idx]) === null || _a === void 0 ? void 0 : _a.value.toLowerCase()) !== "drop schema") {
throw new Error(`[DropSchemaParser] Expected DROP SCHEMA at index ${idx}.`);
}
idx++;
// Handle optional IF EXISTS modifier.
let ifExists = false;
if (((_b = lexemes[idx]) === null || _b === void 0 ? void 0 : _b.value.toLowerCase()) === "if exists") {
ifExists = true;
idx++;
}
const schemaNames = [];
// Parse comma-separated schema identifiers.
while (idx < lexemes.length) {
if (!lexemes[idx]) {
break;
}
const { namespaces, name, newIndex } = FullNameParser_1.FullNameParser.parseFromLexeme(lexemes, idx);
schemaNames.push(new ValueComponent_1.QualifiedName(namespaces, name));
idx = newIndex;
if (((_c = lexemes[idx]) === null || _c === void 0 ? void 0 : _c.type) === Lexeme_1.TokenType.Comma) {
idx++;
continue;
}
break;
}
if (schemaNames.length === 0) {
throw new Error("[DropSchemaParser] DROP SCHEMA must specify at least one schema name.");
}
// Handle optional CASCADE/RESTRICT behavior.
let behavior = null;
const nextValue = (_d = lexemes[idx]) === null || _d === void 0 ? void 0 : _d.value.toLowerCase();
if (nextValue === "cascade" || nextValue === "restrict") {
behavior = nextValue;
idx++;
}
return {
value: new DDLStatements_1.DropSchemaStatement({ schemaNames, ifExists, behavior }),
newIndex: idx
};
}
}
exports.DropSchemaParser = DropSchemaParser;
//# sourceMappingURL=DropSchemaParser.js.map