UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

122 lines (121 loc) 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpatialIndex = void 0; const utils_1 = require("../../../../shared/utils"); const index_column_1 = require("./index-column"); const index_options_1 = require("./index-options"); /** * Spatial index of a table. */ class SpatialIndex { name; columns = []; options; /** * Creates a spatial index from a JSON def. * * @param json JSON format parsed from SQL. */ static fromDef(json) { if (json.id === 'O_CREATE_TABLE_CREATE_DEFINITION') { return SpatialIndex.fromObject(json.def.spatialIndex); } if (json.id === 'P_CREATE_INDEX') { return SpatialIndex.fromObject(json.def); } throw new TypeError(`Unknown json id to build spatial index from: ${json.id}`); } /** * Creates a spatial index from an object containing needed properties. */ static fromObject(json) { const spatialIndex = new SpatialIndex(); spatialIndex.columns = json.columns.map(index_column_1.IndexColumn.fromDef); if (json.name) { spatialIndex.name = json.name; } if ((0, utils_1.isDefined)(json.options) && json.options.length) { spatialIndex.options = index_options_1.IndexOptions.fromArray(json.options); } return spatialIndex; } /** * JSON casting of this object calls this method. */ toJSON() { const json = { columns: this.columns.map((c) => c.toJSON()), }; if ((0, utils_1.isDefined)(this.name)) { json.name = this.name; } if ((0, utils_1.isDefined)(this.options)) { json.options = this.options.toJSON(); } return json; } /** * Create a deep clone of this model. */ clone() { const index = new SpatialIndex(); index.columns = this.columns.map((c) => c.clone()); if ((0, utils_1.isDefined)(this.name)) { index.name = this.name; } if ((0, utils_1.isDefined)(this.options)) { index.options = this.options.clone(); } return index; } /** * Drops a column from index. Returns whether column was removed * * @param name Column name to be dropped. */ dropColumn(name) { let pos = -1; const found = this.columns.some((c, i) => { pos = i; return c.column === name; }); if (!found || pos < 0) { return false; } const end = this.columns.splice(pos); end.shift(); this.columns = this.columns.concat(end); return true; } /** * Get the columns in given table which this * spatial index's index columns refer to. * * @param table Table in question. */ getColumnsFromTable(table) { return (table.columns ?? []).filter((tableColumn) => this.columns.some((indexColumn) => indexColumn.column === tableColumn.name)); } /** * Whether the given table has all of this spatial index's columns. * * @param table Table in question. */ hasAllColumnsFromTable(table) { return ((table.columns ?? []).filter((tableColumn) => this.columns.some((indexColumn) => indexColumn.column === tableColumn.name)).length === this.columns.length); } /** * Rename index column name. * * @param column Column being renamed. * @param newName New column name. */ renameColumn(column, newName) { this.columns .filter((c) => c.column === column.name) .forEach((c) => { c.column = newName; }); } } exports.SpatialIndex = SpatialIndex;