UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

60 lines (59 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateIndex = void 0; const unique_key_1 = require("../unique-key"); const fulltext_index_1 = require("../fulltext-index"); const spatial_index_1 = require("../spatial-index"); const index_1 = require("../index"); /** * Formatter for P_CREATE_INDEX rule's parsed JSON. */ class CreateIndex { database; /** * Get table with given name. * * @param name Table name. */ getTable(name) { return this.database.getTable(name); } /** * Setter for database. * * @param database Database instance. */ setDatabase(database) { this.database = database; } /** * Creates an index and adds it to one of the tables. * * @param json JSON format parsed from SQL. */ handleDef(json) { if (json.id === 'P_CREATE_INDEX') { const table = this.getTable(json.def.table); if (!table) { // throw new Error(`Found "CREATE INDEX" statement to an unexisting table ${json.def.table}`); return; } const type = json.def.type.toLowerCase(); if (type.includes('unique')) { table.pushUniqueKey(unique_key_1.UniqueKey.fromDef(json)); } else if (type.includes('fulltext')) { table.pushFulltextIndex(fulltext_index_1.FulltextIndex.fromDef(json)); } else if (type.includes('spatial')) { table.pushSpatialIndex(spatial_index_1.SpatialIndex.fromDef(json)); } else { table.pushIndex(index_1.Index.fromDef(json)); } return; } throw new TypeError(`Expected P_CREATE_INDEX rule to be handled but received ${json.id}`); } } exports.CreateIndex = CreateIndex;