rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
55 lines • 2.4 kB
JavaScript
import { SqlTokenizer } from "./SqlTokenizer";
import { CreateSchemaStatement } from "../models/DDLStatements";
import { FullNameParser } from "./FullNameParser";
import { QualifiedName } from "../models/ValueComponent";
/**
* Parses CREATE SCHEMA statements.
*/
export class CreateSchemaParser {
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(`[CreateSchemaParser] Unexpected token "${lexemes[result.newIndex].value}" after CREATE SCHEMA 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()) !== "create schema") {
throw new Error(`[CreateSchemaParser] Expected CREATE SCHEMA at index ${idx}.`);
}
idx++;
// Handle optional IF NOT EXISTS modifier.
let ifNotExists = false;
if (((_b = lexemes[idx]) === null || _b === void 0 ? void 0 : _b.value.toLowerCase()) === "if not exists") {
ifNotExists = true;
idx++;
}
if (!lexemes[idx]) {
throw new Error("[CreateSchemaParser] Missing schema name.");
}
// Parse the target schema identifier.
const { namespaces, name, newIndex } = FullNameParser.parseFromLexeme(lexemes, idx);
const schemaName = new QualifiedName(namespaces, name);
idx = newIndex;
// Handle optional AUTHORIZATION clause so ownership can be preserved.
let authorization = null;
if (((_c = lexemes[idx]) === null || _c === void 0 ? void 0 : _c.value.toLowerCase()) === "authorization") {
idx++;
if (!lexemes[idx]) {
throw new Error("[CreateSchemaParser] Expected identifier after AUTHORIZATION.");
}
const authResult = FullNameParser.parseFromLexeme(lexemes, idx);
authorization = authResult.name;
idx = authResult.newIndex;
}
return {
value: new CreateSchemaStatement({ schemaName, ifNotExists, authorization }),
newIndex: idx
};
}
}
//# sourceMappingURL=CreateSchemaParser.js.map