UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

153 lines (152 loc) 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Index = void 0; const utils_1 = require("../../../../shared/utils"); const index_column_1 = require("./index-column"); const index_options_1 = require("./index-options"); /** * Table index. */ class Index { name; indexType; columns = []; options; /** * Creates an index from a JSON def. * * @param json JSON format parsed from SQL. */ static fromDef(json) { if (json.id === 'O_CREATE_TABLE_CREATE_DEFINITION') { return Index.fromObject(json.def.index); } if (json.id === 'P_CREATE_INDEX') { return Index.fromObject(json.def); } throw new TypeError(`Unknown json id to build index from: ${json.id}`); } /** * Creates an index from an object containing needed properties. * * @param json Object containing properties. */ static fromObject(json) { const index = new Index(); index.columns = json.columns.map(index_column_1.IndexColumn.fromDef); if (json.name) { index.name = json.name; } if (json.index) { index.indexType = json.index.def.toLowerCase(); } if ((0, utils_1.isDefined)(json.options) && json.options.length) { index.options = index_options_1.IndexOptions.fromArray(json.options); } return index; } /** * JSON casting of this object calls this method. */ toJSON() { const json = { columns: this.columns.map((c) => c.toJSON()), }; if ((0, utils_1.isDefined)(this.options)) { json.options = this.options.toJSON(); } if ((0, utils_1.isDefined)(this.indexType)) { json.indexType = this.indexType; } if ((0, utils_1.isDefined)(this.name)) { json.name = this.name; } return json; } /** * Create a deep clone of this model. */ clone() { const index = new Index(); index.columns = this.columns.map((c) => c.clone()); if ((0, utils_1.isDefined)(this.options)) { index.options = this.options.clone(); } if ((0, utils_1.isDefined)(this.indexType)) { index.indexType = this.indexType; } if ((0, utils_1.isDefined)(this.name)) { index.name = this.name; } 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 * 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 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); } /** * Set size of this index to the size of index's column in given * table, if the size of this index is not already set. * @param table Table to search size for. */ setIndexSizeFromTable(table) { this.columns .filter((i) => !(0, utils_1.isDefined)(i.length)) .forEach((indexColumn) => { const column = (table.columns ?? []).find((c) => c.name === indexColumn.column); if (!column) { return; } const indexableSize = column.type.getMaxIndexableSize(); if (indexableSize > 0) { indexColumn.length = indexableSize; } }); } /** * 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.Index = Index;