UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

57 lines (56 loc) 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DropTable = void 0; /** * Formatter for P_DROP_TABLE rule's parsed JSON. */ class DropTable { database; /** * Get table with given name. * * @param name Table name. */ getTable(name) { return this.database.getTable(name); } /** * Setter for database. * * @param database Database instance. */ setDatabase(database) { this.database = database; } /** * Drops one of the tables. * * @param json JSON format parsed from SQL. */ handleDef(json) { if (json.id === 'P_DROP_TABLE') { json.def.forEach((tableName) => { const table = this.getTable(tableName); if (!table) { return; } let tables = this.database.getTables(); const hasReference = tables.some((t) => t.foreignKeys?.some((k) => k.referencesTable(table))); if (hasReference) { return; } if (!table) { // throw new Error(`Found "DROP TABLE" statement for an unexisting table ${table}`); return; } const end = tables.splice(tables.indexOf(table)); end.shift(); tables = tables.concat(end); this.database.setTables(tables); }); return; } throw new TypeError(`Expected P_DROP_TABLE rule to be handled but received ${json.id}`); } } exports.DropTable = DropTable;