sql-ddl-to-json-schema
Version:
Parse and convert SQL DDL statements to a JSON Schema.
51 lines (50 loc) • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenameTable = void 0;
/**
* Formatter for P_RENAME_TABLE rule's parsed JSON.
*/
class RenameTable {
database;
/**
* Get table with given name.
*
* @param name Table name.
*/
getTable(name) {
return this.database.getTable(name) ?? undefined;
}
/**
* Setter for database.
*
* @param database Database instance.
*/
setDatabase(database) {
this.database = database;
}
/**
* Get tables from database.
*/
getTables() {
return this.database.getTables();
}
/**
* Renames one of the tables.
*
* @param json JSON format parsed from SQL.
*/
handleDef(json) {
if (json.id === 'P_RENAME_TABLE') {
json.def.forEach((def) => {
const table = this.getTables().find((t) => t.name === def.table);
if (!table) {
return;
}
table.renameTo(def.newName);
});
return;
}
throw new TypeError(`Expected P_RENAME_TABLE rule to be handled but received ${json.id}`);
}
}
exports.RenameTable = RenameTable;