sql-ddl-to-json-schema
Version:
Parse and convert SQL DDL statements to a JSON Schema.
74 lines (73 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateTable = void 0;
const table_1 = require("../table");
/**
* Formatter for P_CREATE_TABLE rule's parsed JSON.
*/
class CreateTable {
database;
/**
* Get table with given name.
*
* @param name Table name.
*/
getTable(name) {
return this.database.getTable(name);
}
/**
* Get tables from database.
*/
getTables() {
return this.database.getTables();
}
/**
* Setter for database.
*
* @param database Database instance.
*/
setDatabase(database) {
this.database = database;
}
/**
* Pushes a table to database.
*
* @param table Table to be added.
*/
pushTable(table) {
this.database.pushTable(table);
}
/**
* Creates a table and add it to the array.
*
* @param json JSON format parsed from SQL.
*/
handleDef(json) {
if (json.id === 'P_CREATE_TABLE') {
const def = json.def;
if (def.id === 'P_CREATE_TABLE_COMMON') {
const table = table_1.Table.fromCommonDef(def, this.database);
if (table) {
this.pushTable(table);
}
}
else if (def.id === 'P_CREATE_TABLE_LIKE') {
const table = table_1.Table.fromAlikeDef(def, this.getTables());
if (!table) {
return;
}
/**
* Through tests it is noticed that foreign keys are
* not kept on duplicated table ~ duartealexf.
*/
table.foreignKeys = [];
if (table) {
this.pushTable(table);
}
}
return;
}
throw new TypeError(`Expected P_CREATE_TABLE rule to be handled but received ${json.id}`);
}
}
exports.CreateTable = CreateTable;