UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

124 lines (123 loc) 3.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FulltextIndex = void 0; const utils_1 = require("../../../../shared/utils"); const index_column_1 = require("./index-column"); const index_options_1 = require("./index-options"); /** * Fulltext index of a table. */ class FulltextIndex { name; columns = []; options; /** * Creates a fulltext index from a JSON def. * * @param json JSON format parsed from SQL. */ static fromDef(json) { if (json.id === 'O_CREATE_TABLE_CREATE_DEFINITION') { return FulltextIndex.fromObject(json.def.fulltextIndex); } if (json.id === 'P_CREATE_INDEX') { return FulltextIndex.fromObject(json.def); } throw new TypeError(`Unknown json id to build fulltext index from: ${json.id}`); } /** * Creates a fulltext index from an object containing needed properties. * * @param json Object containing properties. */ static fromObject(json) { const fulltextIndex = new FulltextIndex(); fulltextIndex.columns = json.columns.map(index_column_1.IndexColumn.fromDef); if (json.name) { fulltextIndex.name = json.name; } if ((0, utils_1.isDefined)(json.options) && json.options.length) { fulltextIndex.options = index_options_1.IndexOptions.fromArray(json.options); } return fulltextIndex; } /** * 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 FulltextIndex(); 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, returning 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 * fulltext 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)); } /** * Get whether the given table has all of this fultext 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.FulltextIndex = FulltextIndex;