UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

477 lines (476 loc) 18.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TableOptions = void 0; const utils_1 = require("../../../../shared/utils"); /** * Class to represent table options as parsed from SQL. */ class TableOptions { autoincrement; avgRowLength; charset; checksum; collation; comment; compression; connection; dataDirectory; indexDirectory; delayKeyWrite; encryption; encryptionKeyId; ietfQuotes; engine; insertMethod; keyBlockSize; maxRows; minRows; packKeys; pageChecksum; password; rowFormat; statsAutoRecalc; statsPersistent; statsSamplePages; transactional; withSystemVersioning; tablespaceName; tablespaceStorage; union; /** * Creates table options from a JSON def. * * @param json JSON format parsed from SQL. */ static fromDef(json) { if (json.id === 'P_CREATE_TABLE_OPTIONS') { return TableOptions.fromArray(json.def); } throw new TypeError(`Unknown json id to build table options from: ${json.id}`); } /** * Creates table options instance from an array of options. * * @param options JSON format parsed from SQL. */ static fromArray(options) { const tableOptions = new TableOptions(); options.forEach((option) => { if ((0, utils_1.isDefined)(option.def.autoincrement)) { tableOptions.autoincrement = option.def.autoincrement; } if ((0, utils_1.isDefined)(option.def.avgRowLength)) { tableOptions.avgRowLength = option.def.avgRowLength; } if ((0, utils_1.isDefined)(option.def.charset)) { tableOptions.charset = option.def.charset.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.checksum)) { tableOptions.checksum = option.def.checksum; } if ((0, utils_1.isDefined)(option.def.collation)) { tableOptions.collation = option.def.collation.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.comment)) { tableOptions.comment = option.def.comment; } if ((0, utils_1.isDefined)(option.def.compression)) { tableOptions.compression = option.def.compression.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.connection)) { tableOptions.connection = option.def.connection; } if ((0, utils_1.isDefined)(option.def.dataDirectory)) { tableOptions.dataDirectory = option.def.dataDirectory; } if ((0, utils_1.isDefined)(option.def.indexDirectory)) { tableOptions.indexDirectory = option.def.indexDirectory; } if ((0, utils_1.isDefined)(option.def.delayKeyWrite)) { tableOptions.delayKeyWrite = option.def.delayKeyWrite; } if ((0, utils_1.isDefined)(option.def.encryption)) { tableOptions.encryption = option.def.encryption.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.encryptionKeyId)) { tableOptions.encryptionKeyId = option.def.encryptionKeyId; } if ((0, utils_1.isDefined)(option.def.ietfQuotes)) { tableOptions.ietfQuotes = option.def.ietfQuotes.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.engine)) { tableOptions.engine = option.def.engine; } if ((0, utils_1.isDefined)(option.def.insertMethod)) { tableOptions.insertMethod = option.def.insertMethod.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.keyBlockSize)) { tableOptions.keyBlockSize = option.def.keyBlockSize; } if ((0, utils_1.isDefined)(option.def.maxRows)) { tableOptions.maxRows = option.def.maxRows; } if ((0, utils_1.isDefined)(option.def.minRows)) { tableOptions.minRows = option.def.minRows; } if ((0, utils_1.isDefined)(option.def.packKeys)) { if ((0, utils_1.isString)(option.def.packKeys)) { tableOptions.packKeys = option.def.packKeys.toLowerCase(); } else { tableOptions.packKeys = option.def.packKeys; } } if ((0, utils_1.isDefined)(option.def.pageChecksum)) { tableOptions.pageChecksum = option.def.pageChecksum; } if ((0, utils_1.isDefined)(option.def.password)) { tableOptions.password = option.def.password; } if ((0, utils_1.isDefined)(option.def.rowFormat)) { tableOptions.rowFormat = option.def.rowFormat.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.statsAutoRecalc)) { if ((0, utils_1.isString)(option.def.statsAutoRecalc)) { tableOptions.statsAutoRecalc = option.def.statsAutoRecalc.toLowerCase(); } else { tableOptions.statsAutoRecalc = option.def.statsAutoRecalc; } } if ((0, utils_1.isDefined)(option.def.statsPersistent)) { if ((0, utils_1.isString)(option.def.statsPersistent)) { tableOptions.statsPersistent = option.def.statsPersistent.toLowerCase(); } else { tableOptions.statsPersistent = option.def.statsPersistent; } } if ((0, utils_1.isDefined)(option.def.statsSamplePages)) { if ((0, utils_1.isString)(option.def.statsSamplePages)) { tableOptions.statsSamplePages = option.def.statsSamplePages.toLowerCase(); } else { tableOptions.statsSamplePages = option.def.statsSamplePages; } } if ((0, utils_1.isDefined)(option.def.transactional)) { tableOptions.transactional = option.def.transactional; } if ((0, utils_1.isDefined)(option.def.withSystemVersioning)) { tableOptions.withSystemVersioning = option.def.withSystemVersioning; } if ((0, utils_1.isDefined)(option.def.tablespaceName)) { tableOptions.tablespaceName = option.def.tablespaceName; } if ((0, utils_1.isDefined)(option.def.tablespaceStorage)) { tableOptions.tablespaceStorage = option.def.tablespaceStorage.toLowerCase(); } if ((0, utils_1.isDefined)(option.def.union)) { tableOptions.union = option.def.union; } }); return tableOptions; } /** * JSON casting of this object calls this method. * */ toJSON() { const json = {}; if ((0, utils_1.isDefined)(this.autoincrement)) { json.autoincrement = this.autoincrement; } if ((0, utils_1.isDefined)(this.avgRowLength)) { json.avgRowLength = this.avgRowLength; } if ((0, utils_1.isDefined)(this.charset)) { json.charset = this.charset; } if ((0, utils_1.isDefined)(this.checksum)) { json.checksum = this.checksum; } if ((0, utils_1.isDefined)(this.collation)) { json.collation = this.collation; } if ((0, utils_1.isDefined)(this.comment)) { json.comment = this.comment; } if ((0, utils_1.isDefined)(this.compression)) { json.compression = this.compression; } if ((0, utils_1.isDefined)(this.connection)) { json.connection = this.connection; } if ((0, utils_1.isDefined)(this.dataDirectory)) { json.dataDirectory = this.dataDirectory; } if ((0, utils_1.isDefined)(this.indexDirectory)) { json.indexDirectory = this.indexDirectory; } if ((0, utils_1.isDefined)(this.delayKeyWrite)) { json.delayKeyWrite = this.delayKeyWrite; } if ((0, utils_1.isDefined)(this.encryption)) { json.encryption = this.encryption; } if ((0, utils_1.isDefined)(this.encryptionKeyId)) { json.encryptionKeyId = this.encryptionKeyId; } if ((0, utils_1.isDefined)(this.ietfQuotes)) { json.ietfQuotes = this.ietfQuotes; } if ((0, utils_1.isDefined)(this.engine)) { json.engine = this.engine; } if ((0, utils_1.isDefined)(this.insertMethod)) { json.insertMethod = this.insertMethod; } if ((0, utils_1.isDefined)(this.keyBlockSize)) { json.keyBlockSize = this.keyBlockSize; } if ((0, utils_1.isDefined)(this.maxRows)) { json.maxRows = this.maxRows; } if ((0, utils_1.isDefined)(this.minRows)) { json.minRows = this.minRows; } if ((0, utils_1.isDefined)(this.packKeys)) { json.packKeys = this.packKeys; } if ((0, utils_1.isDefined)(this.pageChecksum)) { json.pageChecksum = this.pageChecksum; } if ((0, utils_1.isDefined)(this.password)) { json.password = this.password; } if ((0, utils_1.isDefined)(this.rowFormat)) { json.rowFormat = this.rowFormat; } if ((0, utils_1.isDefined)(this.statsAutoRecalc)) { json.statsAutoRecalc = this.statsAutoRecalc; } if ((0, utils_1.isDefined)(this.statsPersistent)) { json.statsPersistent = this.statsPersistent; } if ((0, utils_1.isDefined)(this.statsSamplePages)) { json.statsSamplePages = this.statsSamplePages; } if ((0, utils_1.isDefined)(this.transactional)) { json.transactional = this.transactional; } if ((0, utils_1.isDefined)(this.withSystemVersioning)) { json.withSystemVersioning = this.withSystemVersioning; } if ((0, utils_1.isDefined)(this.tablespaceName)) { json.tablespaceName = this.tablespaceName; } if ((0, utils_1.isDefined)(this.tablespaceStorage)) { json.tablespaceStorage = this.tablespaceStorage; } if ((0, utils_1.isDefined)(this.union)) { json.union = this.union; } return json; } /** * Create a deep clone of this model. */ clone() { const options = new TableOptions(); if ((0, utils_1.isDefined)(this.autoincrement)) { options.autoincrement = this.autoincrement; } if ((0, utils_1.isDefined)(this.avgRowLength)) { options.avgRowLength = this.avgRowLength; } if ((0, utils_1.isDefined)(this.charset)) { options.charset = this.charset; } if ((0, utils_1.isDefined)(this.checksum)) { options.checksum = this.checksum; } if ((0, utils_1.isDefined)(this.collation)) { options.collation = this.collation; } if ((0, utils_1.isDefined)(this.comment)) { options.comment = this.comment; } if ((0, utils_1.isDefined)(this.compression)) { options.compression = this.compression; } if ((0, utils_1.isDefined)(this.connection)) { options.connection = this.connection; } if ((0, utils_1.isDefined)(this.dataDirectory)) { options.dataDirectory = this.dataDirectory; } if ((0, utils_1.isDefined)(this.indexDirectory)) { options.indexDirectory = this.indexDirectory; } if ((0, utils_1.isDefined)(this.delayKeyWrite)) { options.delayKeyWrite = this.delayKeyWrite; } if ((0, utils_1.isDefined)(this.encryption)) { options.encryption = this.encryption; } if ((0, utils_1.isDefined)(this.encryptionKeyId)) { options.encryptionKeyId = this.encryptionKeyId; } if ((0, utils_1.isDefined)(this.ietfQuotes)) { options.ietfQuotes = this.ietfQuotes; } if ((0, utils_1.isDefined)(this.engine)) { options.engine = this.engine; } if ((0, utils_1.isDefined)(this.insertMethod)) { options.insertMethod = this.insertMethod; } if ((0, utils_1.isDefined)(this.keyBlockSize)) { options.keyBlockSize = this.keyBlockSize; } if ((0, utils_1.isDefined)(this.maxRows)) { options.maxRows = this.maxRows; } if ((0, utils_1.isDefined)(this.minRows)) { options.minRows = this.minRows; } if ((0, utils_1.isDefined)(this.packKeys)) { options.packKeys = this.packKeys; } if ((0, utils_1.isDefined)(this.pageChecksum)) { options.pageChecksum = this.pageChecksum; } if ((0, utils_1.isDefined)(this.password)) { options.password = this.password; } if ((0, utils_1.isDefined)(this.rowFormat)) { options.rowFormat = this.rowFormat; } if ((0, utils_1.isDefined)(this.statsAutoRecalc)) { options.statsAutoRecalc = this.statsAutoRecalc; } if ((0, utils_1.isDefined)(this.statsPersistent)) { options.statsPersistent = this.statsPersistent; } if ((0, utils_1.isDefined)(this.statsSamplePages)) { options.statsSamplePages = this.statsSamplePages; } if ((0, utils_1.isDefined)(this.transactional)) { options.transactional = this.transactional; } if ((0, utils_1.isDefined)(this.withSystemVersioning)) { options.withSystemVersioning = this.withSystemVersioning; } if ((0, utils_1.isDefined)(this.tablespaceName)) { options.tablespaceName = this.tablespaceName; } if ((0, utils_1.isDefined)(this.tablespaceStorage)) { options.tablespaceStorage = this.tablespaceStorage; } if ((0, utils_1.isDefined)(this.union)) { options.union = this.union.slice(); } return options; } /** * Merge this option instance with another one. * Common properties of this instance are overwritten. */ mergeWith(options) { if ((0, utils_1.isDefined)(options.autoincrement)) { this.autoincrement = options.autoincrement; } if ((0, utils_1.isDefined)(options.avgRowLength)) { this.avgRowLength = options.avgRowLength; } if ((0, utils_1.isDefined)(options.charset)) { this.charset = options.charset; } if ((0, utils_1.isDefined)(options.checksum)) { this.checksum = options.checksum; } if ((0, utils_1.isDefined)(options.collation)) { this.collation = options.collation; } if ((0, utils_1.isDefined)(options.comment)) { this.comment = options.comment; } if ((0, utils_1.isDefined)(options.compression)) { this.compression = options.compression; } if ((0, utils_1.isDefined)(options.connection)) { this.connection = options.connection; } if ((0, utils_1.isDefined)(options.dataDirectory)) { this.dataDirectory = options.dataDirectory; } if ((0, utils_1.isDefined)(options.indexDirectory)) { this.indexDirectory = options.indexDirectory; } if ((0, utils_1.isDefined)(options.delayKeyWrite)) { this.delayKeyWrite = options.delayKeyWrite; } if ((0, utils_1.isDefined)(options.encryption)) { this.encryption = options.encryption; } if ((0, utils_1.isDefined)(options.encryptionKeyId)) { this.encryptionKeyId = options.encryptionKeyId; } if ((0, utils_1.isDefined)(options.ietfQuotes)) { this.ietfQuotes = options.ietfQuotes; } if ((0, utils_1.isDefined)(options.engine)) { this.engine = options.engine; } if ((0, utils_1.isDefined)(options.insertMethod)) { this.insertMethod = options.insertMethod; } if ((0, utils_1.isDefined)(options.keyBlockSize)) { this.keyBlockSize = options.keyBlockSize; } if ((0, utils_1.isDefined)(options.maxRows)) { this.maxRows = options.maxRows; } if ((0, utils_1.isDefined)(options.minRows)) { this.minRows = options.minRows; } if ((0, utils_1.isDefined)(options.packKeys)) { this.packKeys = options.packKeys; } if ((0, utils_1.isDefined)(options.pageChecksum)) { this.pageChecksum = options.pageChecksum; } if ((0, utils_1.isDefined)(options.password)) { this.password = options.password; } if ((0, utils_1.isDefined)(options.rowFormat)) { this.rowFormat = options.rowFormat; } if ((0, utils_1.isDefined)(options.statsAutoRecalc)) { this.statsAutoRecalc = options.statsAutoRecalc; } if ((0, utils_1.isDefined)(options.statsPersistent)) { this.statsPersistent = options.statsPersistent; } if ((0, utils_1.isDefined)(options.statsSamplePages)) { this.statsSamplePages = options.statsSamplePages; } if ((0, utils_1.isDefined)(options.transactional)) { this.transactional = options.transactional; } if ((0, utils_1.isDefined)(options.withSystemVersioning)) { this.withSystemVersioning = options.withSystemVersioning; } if ((0, utils_1.isDefined)(options.tablespaceName)) { this.tablespaceName = options.tablespaceName; } if ((0, utils_1.isDefined)(options.tablespaceStorage)) { this.tablespaceStorage = options.tablespaceStorage; } if ((0, utils_1.isDefined)(options.union)) { this.union = options.union.slice(); } } } exports.TableOptions = TableOptions;