UNPKG

rawsql-ts

Version:

High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

59 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateSchemaParser = void 0; const SqlTokenizer_1 = require("./SqlTokenizer"); const DDLStatements_1 = require("../models/DDLStatements"); const FullNameParser_1 = require("./FullNameParser"); const ValueComponent_1 = require("../models/ValueComponent"); /** * Parses CREATE SCHEMA statements. */ class CreateSchemaParser { 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(`[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_1.FullNameParser.parseFromLexeme(lexemes, idx); const schemaName = new ValueComponent_1.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_1.FullNameParser.parseFromLexeme(lexemes, idx); authorization = authResult.name; idx = authResult.newIndex; } return { value: new DDLStatements_1.CreateSchemaStatement({ schemaName, ifNotExists, authorization }), newIndex: idx }; } } exports.CreateSchemaParser = CreateSchemaParser; //# sourceMappingURL=CreateSchemaParser.js.map