UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

72 lines (71 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ColumnReference = void 0; const utils_1 = require("../../../../shared/utils"); const column_reference_on_1 = require("./column-reference-on"); const index_column_1 = require("./index-column"); /** * Column reference to another column in foreign keys. */ class ColumnReference { table; match; columns = []; on = []; /** * Creates a column reference from a JSON def. * * @param json JSON format parsed from SQL. */ static fromDef(json) { if (json.id === 'P_COLUMN_REFERENCE') { const def = json.def; const columnReference = new ColumnReference(); columnReference.table = def.table; if (def.match) { columnReference.match = def.match.toLowerCase(); } if (def.columns.length) { columnReference.columns = def.columns.map(index_column_1.IndexColumn.fromDef); } if (def.on.length) { columnReference.on = def.on.map(column_reference_on_1.ColumnReferenceOn.fromObject); } return columnReference; } throw new TypeError(`Unknown json id to build column reference from: ${json.id}`); } /** * JSON casting of this object calls this method. */ toJSON() { const json = { table: this.table, }; if ((0, utils_1.isDefined)(this.match)) { json.match = this.match; } if (this.on && this.on.length) { json.on = this.on.map((o) => o.toJSON()); } if (this.columns && this.columns.length) { json.columns = this.columns.map((c) => c.toJSON()); } return json; } clone() { const reference = new ColumnReference(); reference.table = this.table; if ((0, utils_1.isDefined)(this.match)) { reference.match = this.match; } if (this.on && this.on.length) { reference.on = this.on.map((o) => o.clone()); } if (this.columns && this.columns.length) { reference.columns = this.columns.map((c) => c.clone()); } return reference; } } exports.ColumnReference = ColumnReference;