@dbml/core 6.4.0-alpha.0 → 6.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +97 -97
- package/lib/index.mjs +486 -468
- package/package.json +3 -3
- package/types/export/DbmlExporter.d.ts +30 -0
- package/types/export/JsonExporter.d.ts +13 -0
- package/types/export/ModelExporter.d.ts +7 -9
- package/types/export/index.d.ts +11 -3
- package/types/import/index.d.ts +10 -3
- package/types/index.d.ts +5 -2
package/lib/index.mjs
CHANGED
|
@@ -14740,7 +14740,7 @@ function hasWhiteSpace(_) {
|
|
|
14740
14740
|
function hasWhiteSpaceOrUpperCase(_) {
|
|
14741
14741
|
return /[\sA-Z]/g.test(_);
|
|
14742
14742
|
}
|
|
14743
|
-
function shouldPrintSchema
|
|
14743
|
+
function shouldPrintSchema(_, HY) {
|
|
14744
14744
|
return _.name !== "public" || _.name === "public" && HY.database[1].hasDefaultSchema;
|
|
14745
14745
|
}
|
|
14746
14746
|
function buildJunctionFields1(_, HY) {
|
|
@@ -14816,7 +14816,7 @@ var DbmlExporter_default = class _ {
|
|
|
14816
14816
|
static exportEnums(HY, P) {
|
|
14817
14817
|
let UY = HY.map((HY) => {
|
|
14818
14818
|
let UY = P.enums[HY], WY = P.schemas[UY.schemaId];
|
|
14819
|
-
return `Enum ${shouldPrintSchema
|
|
14819
|
+
return `Enum ${shouldPrintSchema(WY, P) ? `"${WY.name}".` : ""}"${UY.name}" {\n${UY.valueIds.map((HY) => ` "${P.enumValues[HY].name}"${P.enumValues[HY].note ? ` [note: ${_.escapeNote(P.enumValues[HY].note)}]` : ""}`).join("\n")}\n}\n`;
|
|
14820
14820
|
});
|
|
14821
14821
|
return UY.length ? UY.join("\n") : "";
|
|
14822
14822
|
}
|
|
@@ -14833,8 +14833,8 @@ var DbmlExporter_default = class _ {
|
|
|
14833
14833
|
_ += `${UY.dbdefault.value}`;
|
|
14834
14834
|
break;
|
|
14835
14835
|
case "string": {
|
|
14836
|
-
let HY = UY.dbdefault.value.includes("\n") ? "'''" : "'";
|
|
14837
|
-
_ += `${
|
|
14836
|
+
let HY = String(UY.dbdefault.value), P = HY.includes("\n") ? "'''" : "'";
|
|
14837
|
+
_ += `${P}${HY}${P}`;
|
|
14838
14838
|
break;
|
|
14839
14839
|
}
|
|
14840
14840
|
case "expression":
|
|
@@ -14885,7 +14885,7 @@ var DbmlExporter_default = class _ {
|
|
|
14885
14885
|
static exportTables(HY, P) {
|
|
14886
14886
|
let UY = _.getTableContentArr(HY, P).map((HY) => {
|
|
14887
14887
|
let UY = P.tables[HY.tableId], WY = P.schemas[UY.schemaId], GY = _.getTableSettings(UY), KY = `"${UY.name}"`;
|
|
14888
|
-
shouldPrintSchema
|
|
14888
|
+
shouldPrintSchema(WY, P) && (KY = `"${WY.name}"."${UY.name}"`);
|
|
14889
14889
|
let qY = UY.alias ? ` as ${addDoubleQuoteIfNeeded(UY.alias)}` : "", JY = HY.fieldContents.map((_) => ` ${_}\n`).join(""), YY = "";
|
|
14890
14890
|
isEmpty_default(HY.checkContents) || (YY = `\n Checks {\n${HY.checkContents.map((_) => ` ${_}\n`).join("")} }\n`);
|
|
14891
14891
|
let XY = "";
|
|
@@ -14899,16 +14899,16 @@ var DbmlExporter_default = class _ {
|
|
|
14899
14899
|
let P = _.map((_) => `"${HY.fields[_].name}"`).join(", ");
|
|
14900
14900
|
return _.length === 1 ? P : `(${P})`;
|
|
14901
14901
|
}
|
|
14902
|
-
static exportRefs(
|
|
14903
|
-
let
|
|
14904
|
-
let
|
|
14905
|
-
|
|
14906
|
-
let
|
|
14907
|
-
|
|
14908
|
-
let
|
|
14909
|
-
return
|
|
14902
|
+
static exportRefs(HY, P) {
|
|
14903
|
+
let UY = HY.map((HY) => {
|
|
14904
|
+
let UY = P.refs[HY], WY = UY.endpointIds.findIndex((_) => P.endpoints[_].relation === "1"), GY = WY === -1, KY = GY ? 0 : WY, qY = UY.endpointIds[1 - KY], JY = UY.endpointIds[KY], YY = P.endpoints[qY], XY = P.endpoints[JY], ZY = "Ref", QY = P.fields[XY.fieldIds[0]], $Y = P.tables[QY.tableId], eX = P.schemas[$Y.schemaId], tX = _.buildFieldName(XY.fieldIds, P);
|
|
14905
|
+
UY.name && (ZY += ` ${shouldPrintSchema(P.schemas[UY.schemaId], P) ? `"${P.schemas[UY.schemaId].name}".` : ""}"${UY.name}"`), ZY += ":", ZY += `${shouldPrintSchema(eX, P) ? `"${eX.name}".` : ""}"${$Y.name}".${tX} `;
|
|
14906
|
+
let nX = P.fields[YY.fieldIds[0]], rX = P.tables[nX.tableId], iX = P.schemas[rX.schemaId], aX = _.buildFieldName(YY.fieldIds, P);
|
|
14907
|
+
GY ? ZY += "<> " : YY.relation === "1" ? ZY += "- " : ZY += "< ", ZY += `${shouldPrintSchema(iX, P) ? `"${iX.name}".` : ""}"${rX.name}".${aX}`;
|
|
14908
|
+
let oX = [];
|
|
14909
|
+
return UY.onUpdate && oX.push(`update: ${UY.onUpdate.toLowerCase()}`), UY.onDelete && oX.push(`delete: ${UY.onDelete.toLowerCase()}`), oX.length > 0 && (ZY += ` [${oX.join(", ")}]`), ZY += "\n", ZY;
|
|
14910
14910
|
});
|
|
14911
|
-
return
|
|
14911
|
+
return UY.length ? UY.join("\n") : "";
|
|
14912
14912
|
}
|
|
14913
14913
|
static getTableGroupSettings(_) {
|
|
14914
14914
|
let HY = [];
|
|
@@ -14917,9 +14917,9 @@ var DbmlExporter_default = class _ {
|
|
|
14917
14917
|
static exportTableGroups(HY, P) {
|
|
14918
14918
|
let UY = HY.map((HY) => {
|
|
14919
14919
|
let UY = P.tableGroups[HY], WY = P.schemas[UY.schemaId], GY = _.getTableGroupSettings(UY), KY = UY.note ? ` Note: ${_.escapeNote(UY.note)}\n` : "";
|
|
14920
|
-
return `TableGroup ${`${shouldPrintSchema
|
|
14920
|
+
return `TableGroup ${`${shouldPrintSchema(WY, P) ? `"${WY.name}".` : ""}"${UY.name}"`}${GY} {\n${UY.tableIds.reduce((_, HY) => {
|
|
14921
14921
|
let UY = P.tables[HY], WY = P.schemas[UY.schemaId];
|
|
14922
|
-
return `${_}${` ${shouldPrintSchema
|
|
14922
|
+
return `${_}${` ${shouldPrintSchema(WY, P) ? `"${WY.name}".` : ""}"${UY.name}"`}\n`;
|
|
14923
14923
|
}, "")}${KY}}\n`;
|
|
14924
14924
|
});
|
|
14925
14925
|
return UY.length ? UY.join("\n") : "";
|
|
@@ -14931,7 +14931,7 @@ var DbmlExporter_default = class _ {
|
|
|
14931
14931
|
}, "");
|
|
14932
14932
|
}
|
|
14933
14933
|
static exportRecords(_) {
|
|
14934
|
-
let HY = _
|
|
14934
|
+
let { records: HY } = _;
|
|
14935
14935
|
if (!HY || isEmpty_default(HY)) return "";
|
|
14936
14936
|
let P = groupBy_default(Object.values(HY), (_) => `${_.schemaName || ""}.${_.tableName}`);
|
|
14937
14937
|
return Object.values(P).map((_) => {
|
|
@@ -14945,8 +14945,8 @@ var DbmlExporter_default = class _ {
|
|
|
14945
14945
|
}).map((_) => ` ${_.map(formatRecordValue).join(", ")}`).join("\n")}\n}\n`;
|
|
14946
14946
|
}).join("\n");
|
|
14947
14947
|
}
|
|
14948
|
-
static export(HY, P
|
|
14949
|
-
let UY = [], WY = HY.database[1], GY = P
|
|
14948
|
+
static export(HY, P) {
|
|
14949
|
+
let UY = [], WY = HY.database[1], { includeRecords: GY } = P;
|
|
14950
14950
|
return WY.schemaIds.forEach((P) => {
|
|
14951
14951
|
let { enumIds: WY, tableIds: GY, tableGroupIds: KY, refIds: qY } = HY.schemas[P];
|
|
14952
14952
|
isEmpty_default(WY) || UY.push(_.exportEnums(WY, HY)), isEmpty_default(GY) || UY.push(_.exportTables(GY, HY)), isEmpty_default(KY) || UY.push(_.exportTableGroups(KY, HY)), isEmpty_default(qY) || UY.push(_.exportRefs(qY, HY));
|
|
@@ -15013,14 +15013,14 @@ var MysqlExporter_default = class _ {
|
|
|
15013
15013
|
..._.checkContents,
|
|
15014
15014
|
..._.compositePKs
|
|
15015
15015
|
], UY = P.tables[_.tableId], WY = P.schemas[UY.schemaId];
|
|
15016
|
-
return `CREATE TABLE ${shouldPrintSchema
|
|
15016
|
+
return `CREATE TABLE ${shouldPrintSchema(WY, P) ? `\`${WY.name}\`.` : ""}\`${UY.name}\` (\n${HY.map((_) => ` ${_}`).join(",\n")}\n);\n`;
|
|
15017
15017
|
});
|
|
15018
15018
|
}
|
|
15019
15019
|
static buildFieldName(_, HY) {
|
|
15020
15020
|
return `(${_.map((_) => `\`${HY.fields[_].name}\``).join(", ")})`;
|
|
15021
15021
|
}
|
|
15022
15022
|
static buildTableManyToMany(_, HY, P, UY, WY) {
|
|
15023
|
-
let GY = `CREATE TABLE ${shouldPrintSchema
|
|
15023
|
+
let GY = `CREATE TABLE ${shouldPrintSchema(UY, WY) ? `\`${UY.name}\`.` : ""}\`${P}\` (\n`, KY = [..._.keys()].join("`, `"), qY = [...HY.keys()].join("`, `");
|
|
15024
15024
|
return _.forEach((_, HY) => {
|
|
15025
15025
|
GY += ` \`${HY}\` ${_},\n`;
|
|
15026
15026
|
}), HY.forEach((_, HY) => {
|
|
@@ -15029,7 +15029,7 @@ var MysqlExporter_default = class _ {
|
|
|
15029
15029
|
}
|
|
15030
15030
|
static buildForeignKeyManyToMany(_, HY, P, UY, WY, GY, KY) {
|
|
15031
15031
|
let qY = [..._.keys()].join("`, `");
|
|
15032
|
-
return `ALTER TABLE ${shouldPrintSchema
|
|
15032
|
+
return `ALTER TABLE ${shouldPrintSchema(WY, KY) ? `\`${WY.name}\`.` : ""}\`${P}\` ADD FOREIGN KEY (\`${qY}\`) REFERENCES ${shouldPrintSchema(GY, KY) ? `\`${GY.name}\`.` : ""}\`${UY}\` ${HY};\n\n`;
|
|
15033
15033
|
}
|
|
15034
15034
|
static exportRefs(_, HY, P) {
|
|
15035
15035
|
return _.map((_) => {
|
|
@@ -15037,7 +15037,7 @@ var MysqlExporter_default = class _ {
|
|
|
15037
15037
|
if (GY === -1) {
|
|
15038
15038
|
let _ = buildJunctionFields1(XY.fieldIds, HY), WY = buildJunctionFields2(YY.fieldIds, HY, _), GY = buildNewTableName(QY.name, nX.name, P);
|
|
15039
15039
|
UY += this.buildTableManyToMany(_, WY, GY, $Y, HY), UY += this.buildForeignKeyManyToMany(_, eX, GY, QY.name, $Y, $Y, HY), UY += this.buildForeignKeyManyToMany(WY, iX, GY, nX.name, $Y, rX, HY);
|
|
15040
|
-
} else UY = `ALTER TABLE ${shouldPrintSchema
|
|
15040
|
+
} else UY = `ALTER TABLE ${shouldPrintSchema(rX, HY) ? `\`${rX.name}\`.` : ""}\`${nX.name}\` ADD `, WY.name && (UY += `CONSTRAINT \`${WY.name}\` `), UY += `FOREIGN KEY ${iX} REFERENCES ${shouldPrintSchema($Y, HY) ? `\`${$Y.name}\`.` : ""}\`${QY.name}\` ${eX}`, WY.onDelete && (UY += ` ON DELETE ${WY.onDelete.toUpperCase()}`), WY.onUpdate && (UY += ` ON UPDATE ${WY.onUpdate.toUpperCase()}`), UY += ";\n";
|
|
15041
15041
|
return UY;
|
|
15042
15042
|
});
|
|
15043
15043
|
}
|
|
@@ -15045,8 +15045,8 @@ var MysqlExporter_default = class _ {
|
|
|
15045
15045
|
return _.filter((_) => !HY.indexes[_].pk).map((_, P) => {
|
|
15046
15046
|
let UY = HY.indexes[_], WY = HY.tables[UY.tableId], GY = HY.schemas[WY.schemaId], KY = "CREATE";
|
|
15047
15047
|
UY.unique && (KY += " UNIQUE");
|
|
15048
|
-
let qY = UY.name ? `\`${UY.name}\`` : `\`${shouldPrintSchema
|
|
15049
|
-
KY += ` INDEX ${qY} ON ${shouldPrintSchema
|
|
15048
|
+
let qY = UY.name ? `\`${UY.name}\`` : `\`${shouldPrintSchema(GY, HY) ? `\`${GY.name}\`.` : ""}${WY.name}_index_${P}\``;
|
|
15049
|
+
KY += ` INDEX ${qY} ON ${shouldPrintSchema(GY, HY) ? `\`${GY.name}\`.` : ""}\`${WY.name}\``;
|
|
15050
15050
|
let JY = [];
|
|
15051
15051
|
return UY.columnIds.forEach((_) => {
|
|
15052
15052
|
let P = HY.indexColumns[_], UY = "";
|
|
@@ -15059,7 +15059,7 @@ var MysqlExporter_default = class _ {
|
|
|
15059
15059
|
let P = "";
|
|
15060
15060
|
if (_.type === "table") {
|
|
15061
15061
|
let UY = HY.tables[_.tableId], WY = HY.schemas[UY.schemaId];
|
|
15062
|
-
P += `ALTER TABLE ${shouldPrintSchema
|
|
15062
|
+
P += `ALTER TABLE ${shouldPrintSchema(WY, HY) ? `\`${WY.name}\`.` : ""}\`${UY.name}\` COMMENT = '${UY.note.replace(/'/g, "''")}'`;
|
|
15063
15063
|
}
|
|
15064
15064
|
return P += ";\n", P;
|
|
15065
15065
|
});
|
|
@@ -15067,7 +15067,7 @@ var MysqlExporter_default = class _ {
|
|
|
15067
15067
|
static export(HY) {
|
|
15068
15068
|
let P = HY.database[1], UY = new Set(Object.values(HY.tables).map((_) => _.name)), WY = P.schemaIds.reduce((P, WY) => {
|
|
15069
15069
|
let GY = HY.schemas[WY], { tableIds: KY, refIds: qY } = GY;
|
|
15070
|
-
shouldPrintSchema
|
|
15070
|
+
shouldPrintSchema(GY, HY) && P.schemas.push(`CREATE SCHEMA \`${GY.name}\`;\n`), isEmpty_default(KY) || P.tables.push(..._.exportTables(KY, HY));
|
|
15071
15071
|
let JY = flatten_default(KY.map((_) => HY.tables[_].indexIds));
|
|
15072
15072
|
isEmpty_default(JY) || P.indexes.push(..._.exportIndexes(JY, HY));
|
|
15073
15073
|
let YY = flatten_default(KY.map((_) => {
|
|
@@ -15097,7 +15097,7 @@ var MysqlExporter_default = class _ {
|
|
|
15097
15097
|
return concat_default(WY.schemas, WY.enums, WY.tables, WY.indexes, WY.comments, WY.refs, KY).join("\n");
|
|
15098
15098
|
}
|
|
15099
15099
|
};
|
|
15100
|
-
function shouldPrintSchema(_) {
|
|
15100
|
+
function shouldPrintSchema$1(_) {
|
|
15101
15101
|
return _.name !== "public" || _.name === "public" && _.database.hasDefaultSchema;
|
|
15102
15102
|
}
|
|
15103
15103
|
function shouldPrintSchemaName(_) {
|
|
@@ -15116,7 +15116,7 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15116
15116
|
}
|
|
15117
15117
|
static exportEnums(_, HY) {
|
|
15118
15118
|
return _.map((_) => {
|
|
15119
|
-
let P = HY.enums[_], UY = HY.schemas[P.schemaId], WY = `${shouldPrintSchema
|
|
15119
|
+
let P = HY.enums[_], UY = HY.schemas[P.schemaId], WY = `${shouldPrintSchema(UY, HY) ? `"${UY.name}".` : ""}"${P.name}"`;
|
|
15120
15120
|
return [WY, `CREATE TYPE ${WY} AS ENUM (\n${P.valueIds.map((_) => ` '${HY.enumValues[_].name}'`).join(",\n")}\n);\n`];
|
|
15121
15121
|
});
|
|
15122
15122
|
}
|
|
@@ -15185,14 +15185,14 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15185
15185
|
..._.checkContents,
|
|
15186
15186
|
..._.compositePKs
|
|
15187
15187
|
], UY = P.tables[_.tableId], WY = P.schemas[UY.schemaId];
|
|
15188
|
-
return `CREATE TABLE ${shouldPrintSchema
|
|
15188
|
+
return `CREATE TABLE ${shouldPrintSchema(WY, P) ? `"${WY.name}".` : ""}"${UY.name}" (\n${HY.map((_) => ` ${_}`).join(",\n")}\n);\n`;
|
|
15189
15189
|
});
|
|
15190
15190
|
}
|
|
15191
15191
|
static buildFieldName(_, HY) {
|
|
15192
15192
|
return `(${_.map((_) => `"${HY.fields[_].name}"`).join(", ")})`;
|
|
15193
15193
|
}
|
|
15194
15194
|
static buildTableManyToMany(_, HY, P, UY, WY) {
|
|
15195
|
-
let GY = `CREATE TABLE ${shouldPrintSchema
|
|
15195
|
+
let GY = `CREATE TABLE ${shouldPrintSchema(UY, WY) ? `"${UY.name}".` : ""}"${P}" (\n`, KY = [..._.keys()].join("\", \""), qY = [...HY.keys()].join("\", \"");
|
|
15196
15196
|
return _.forEach((_, HY) => {
|
|
15197
15197
|
GY += ` "${HY}" ${_},\n`;
|
|
15198
15198
|
}), HY.forEach((_, HY) => {
|
|
@@ -15201,7 +15201,7 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15201
15201
|
}
|
|
15202
15202
|
static buildForeignKeyManyToMany(_, HY, P, UY, WY, GY, KY) {
|
|
15203
15203
|
let qY = [..._.keys()].join("\", \"");
|
|
15204
|
-
return `ALTER TABLE ${shouldPrintSchema
|
|
15204
|
+
return `ALTER TABLE ${shouldPrintSchema(WY, KY) ? `"${WY.name}".` : ""}"${P}" ADD FOREIGN KEY ("${qY}") REFERENCES ${shouldPrintSchema(GY, KY) ? `"${GY.name}".` : ""}"${UY}" ${HY} DEFERRABLE INITIALLY IMMEDIATE;\n\n`;
|
|
15205
15205
|
}
|
|
15206
15206
|
static exportRefs(_, HY, P) {
|
|
15207
15207
|
return _.map((_) => {
|
|
@@ -15209,7 +15209,7 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15209
15209
|
if (GY === -1) {
|
|
15210
15210
|
let _ = buildJunctionFields1(XY.fieldIds, HY), WY = buildJunctionFields2(YY.fieldIds, HY, _), GY = buildNewTableName(QY.name, nX.name, P);
|
|
15211
15211
|
UY += this.buildTableManyToMany(_, WY, GY, $Y, HY), UY += this.buildForeignKeyManyToMany(_, eX, GY, QY.name, $Y, $Y, HY), UY += this.buildForeignKeyManyToMany(WY, iX, GY, nX.name, $Y, rX, HY);
|
|
15212
|
-
} else UY = `ALTER TABLE ${shouldPrintSchema
|
|
15212
|
+
} else UY = `ALTER TABLE ${shouldPrintSchema(rX, HY) ? `"${rX.name}".` : ""}"${nX.name}" ADD `, WY.name && (UY += `CONSTRAINT "${WY.name}" `), UY += `FOREIGN KEY ${iX} REFERENCES ${shouldPrintSchema($Y, HY) ? `"${$Y.name}".` : ""}"${QY.name}" ${eX}`, WY.onDelete && (UY += ` ON DELETE ${WY.onDelete.toUpperCase()}`), WY.onUpdate && (UY += ` ON UPDATE ${WY.onUpdate.toUpperCase()}`), UY += " DEFERRABLE INITIALLY IMMEDIATE;\n";
|
|
15213
15213
|
return UY;
|
|
15214
15214
|
});
|
|
15215
15215
|
}
|
|
@@ -15218,7 +15218,7 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15218
15218
|
let P = HY.indexes[_], UY = HY.tables[P.tableId], WY = HY.schemas[UY.schemaId], GY = "CREATE";
|
|
15219
15219
|
P.unique && (GY += " UNIQUE");
|
|
15220
15220
|
let KY = P.name ? `"${P.name}"` : "";
|
|
15221
|
-
GY += " INDEX", KY && (GY += ` ${KY}`), GY += ` ON ${shouldPrintSchema
|
|
15221
|
+
GY += " INDEX", KY && (GY += ` ${KY}`), GY += ` ON ${shouldPrintSchema(WY, HY) ? `"${WY.name}".` : ""}"${UY.name}"`, P.type && (GY += ` USING ${P.type.toUpperCase()}`);
|
|
15222
15222
|
let qY = [];
|
|
15223
15223
|
return P.columnIds.forEach((_) => {
|
|
15224
15224
|
let P = HY.indexColumns[_], UY = "";
|
|
@@ -15231,11 +15231,11 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15231
15231
|
let P = "COMMENT ON", UY = HY.tables[_.tableId], WY = HY.schemas[UY.schemaId];
|
|
15232
15232
|
switch (_.type) {
|
|
15233
15233
|
case "table":
|
|
15234
|
-
P += ` TABLE ${shouldPrintSchema
|
|
15234
|
+
P += ` TABLE ${shouldPrintSchema(WY, HY) ? `"${WY.name}".` : ""}"${UY.name}" IS '${UY.note.replace(/'/g, "''")}'`;
|
|
15235
15235
|
break;
|
|
15236
15236
|
case "column": {
|
|
15237
15237
|
let GY = HY.fields[_.fieldId];
|
|
15238
|
-
P += ` COLUMN ${shouldPrintSchema
|
|
15238
|
+
P += ` COLUMN ${shouldPrintSchema(WY, HY) ? `"${WY.name}".` : ""}"${UY.name}"."${GY.name}" IS '${GY.note.replace(/'/g, "''")}'`;
|
|
15239
15239
|
break;
|
|
15240
15240
|
}
|
|
15241
15241
|
default: break;
|
|
@@ -15246,7 +15246,7 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15246
15246
|
static export(HY) {
|
|
15247
15247
|
let P = HY.database[1], UY = new Set(Object.values(HY.tables).map((_) => _.name)), WY = /* @__PURE__ */ new Set(), GY = P.schemaIds.reduce((P, UY) => {
|
|
15248
15248
|
let GY = HY.schemas[UY], { enumIds: KY } = GY;
|
|
15249
|
-
return shouldPrintSchema
|
|
15249
|
+
return shouldPrintSchema(GY, HY) && P.schemas.push(`CREATE SCHEMA "${GY.name}";\n`), isEmpty_default(KY) || _.exportEnums(KY, HY).forEach((_) => {
|
|
15250
15250
|
let [HY, UY] = _;
|
|
15251
15251
|
P.enums.push(UY), WY.add(HY);
|
|
15252
15252
|
}), P;
|
|
@@ -15286,418 +15286,6 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
|
|
|
15286
15286
|
];
|
|
15287
15287
|
return concat_default(KY.schemas, KY.enums, KY.tables, KY.indexes, KY.comments, KY.refs, JY).join("\n");
|
|
15288
15288
|
}
|
|
15289
|
-
}, JsonExporter_default = class {
|
|
15290
|
-
static export(_, HY = {}) {
|
|
15291
|
-
return (typeof HY == "boolean" ? { isNormalized: HY } : HY).isNormalized === !1 ? JSON.stringify(_.export(), null, 2) : JSON.stringify(_, null, 2);
|
|
15292
|
-
}
|
|
15293
|
-
};
|
|
15294
|
-
init_lodash();
|
|
15295
|
-
var SqlServerExporter_default = class _ {
|
|
15296
|
-
static exportRecords(_) {
|
|
15297
|
-
let HY = Object.values(_.records || {});
|
|
15298
|
-
return isEmpty_default(HY) ? [] : HY.map((_) => {
|
|
15299
|
-
let { schemaName: HY, tableName: P, columns: UY, values: WY } = _, GY = HY ? `[${HY}].[${P}]` : `[${P}]`, KY = UY.length > 0 ? `([${UY.join("], [")}])` : "", qY = (_) => _.value === null ? "NULL" : _.type === "expression" || isNumericType(_.type) ? _.value : isBooleanType(_.type) ? String(_.value).toUpperCase() === "TRUE" ? "1" : "0" : isStringType(_.type) || isDateTimeType(_.type) ? `'${_.value.replace(/'/g, "''")}'` : isBinaryType(_.type) ? `0x${_.value}` : `CAST('${_.value.replace(/'/g, "''")}' AS ${_.type})`;
|
|
15300
|
-
return `INSERT INTO ${GY} ${KY}\nVALUES\n ${WY.map((_) => `(${_.map(qY).join(", ")})`).join(",\n ")};\nGO`;
|
|
15301
|
-
});
|
|
15302
|
-
}
|
|
15303
|
-
static getFieldLines(_, HY) {
|
|
15304
|
-
return HY.tables[_].fieldIds.map((_) => {
|
|
15305
|
-
let P = HY.fields[_], UY = "";
|
|
15306
|
-
if (P.enumId) {
|
|
15307
|
-
let _ = HY.enums[P.enumId];
|
|
15308
|
-
UY = `[${P.name}] nvarchar(255) NOT NULL CHECK ([${P.name}] IN (`;
|
|
15309
|
-
let WY = _.valueIds.map((_) => `'${HY.enumValues[_].name}'`);
|
|
15310
|
-
UY += `${WY.join(", ")}))`;
|
|
15311
|
-
} else UY = `[${P.name}] ${P.type.type_name === "varchar" ? "nvarchar(255)" : P.type.type_name}`;
|
|
15312
|
-
if (P.unique && (UY += " UNIQUE"), P.pk && (UY += " PRIMARY KEY"), P.not_null && (UY += " NOT NULL"), P.increment && (UY += " IDENTITY(1, 1)"), P.checkIds && P.checkIds.length > 0) if (P.checkIds.length === 1) {
|
|
15313
|
-
let _ = HY.checks[P.checkIds[0]];
|
|
15314
|
-
_.name && (UY += ` CONSTRAINT [${_.name}]`), UY += ` CHECK (${_.expression})`;
|
|
15315
|
-
} else {
|
|
15316
|
-
let _ = P.checkIds.map((_) => `(${HY.checks[_].expression})`);
|
|
15317
|
-
UY += ` CHECK (${_.join(" AND ")})`;
|
|
15318
|
-
}
|
|
15319
|
-
return P.dbdefault && (P.dbdefault.type === "boolean" && (P.dbdefault.value === null || typeof P.dbdefault.value == "string" && P.dbdefault.value.toLowerCase() === "null") || (P.dbdefault.type === "expression" ? UY += ` DEFAULT (${P.dbdefault.value})` : P.dbdefault.type === "string" ? UY += ` DEFAULT '${P.dbdefault.value}'` : UY += ` DEFAULT (${P.dbdefault.value})`)), UY;
|
|
15320
|
-
});
|
|
15321
|
-
}
|
|
15322
|
-
static getCompositePKs(_, HY) {
|
|
15323
|
-
let P = HY.tables[_];
|
|
15324
|
-
return (P.indexIds ? P.indexIds.filter((_) => HY.indexes[_].pk) : []).map((_) => {
|
|
15325
|
-
let P = HY.indexes[_], UY = "PRIMARY KEY", WY = [];
|
|
15326
|
-
return P.columnIds.forEach((_) => {
|
|
15327
|
-
let P = HY.indexColumns[_], UY = "";
|
|
15328
|
-
UY = P.type === "expression" ? `(${P.value})` : `[${P.value}]`, WY.push(UY);
|
|
15329
|
-
}), UY += ` (${WY.join(", ")})`, UY;
|
|
15330
|
-
});
|
|
15331
|
-
}
|
|
15332
|
-
static getCheckLines(_, HY) {
|
|
15333
|
-
let P = HY.tables[_];
|
|
15334
|
-
return !P.checkIds || P.checkIds.length === 0 ? [] : P.checkIds.map((_) => {
|
|
15335
|
-
let P = HY.checks[_], UY = "";
|
|
15336
|
-
return P.name && (UY = `CONSTRAINT [${P.name}] `), UY += `CHECK (${P.expression})`, UY;
|
|
15337
|
-
});
|
|
15338
|
-
}
|
|
15339
|
-
static getTableContentArr(HY, P) {
|
|
15340
|
-
return HY.map((HY) => ({
|
|
15341
|
-
tableId: HY,
|
|
15342
|
-
fieldContents: _.getFieldLines(HY, P),
|
|
15343
|
-
checkContents: _.getCheckLines(HY, P),
|
|
15344
|
-
compositePKs: _.getCompositePKs(HY, P)
|
|
15345
|
-
}));
|
|
15346
|
-
}
|
|
15347
|
-
static exportTables(HY, P) {
|
|
15348
|
-
return _.getTableContentArr(HY, P).map((_) => {
|
|
15349
|
-
let HY = [
|
|
15350
|
-
..._.fieldContents,
|
|
15351
|
-
..._.checkContents,
|
|
15352
|
-
..._.compositePKs
|
|
15353
|
-
], UY = P.tables[_.tableId], WY = P.schemas[UY.schemaId];
|
|
15354
|
-
return `CREATE TABLE ${shouldPrintSchema$1(WY, P) ? `[${WY.name}].` : ""}[${UY.name}] (\n${HY.map((_) => ` ${_}`).join(",\n")}\n)\nGO\n`;
|
|
15355
|
-
});
|
|
15356
|
-
}
|
|
15357
|
-
static buildTableManyToMany(_, HY, P, UY, WY) {
|
|
15358
|
-
let GY = `CREATE TABLE ${shouldPrintSchema$1(UY, WY) ? `[${UY.name}].` : ""}[${P}] (\n`, KY = [..._.keys()].join("], ["), qY = [...HY.keys()].join("], [");
|
|
15359
|
-
return _.forEach((_, HY) => {
|
|
15360
|
-
GY += ` [${HY}] ${_},\n`;
|
|
15361
|
-
}), HY.forEach((_, HY) => {
|
|
15362
|
-
GY += ` [${HY}] ${_},\n`;
|
|
15363
|
-
}), GY += ` PRIMARY KEY ([${KY}], [${qY}])\n`, GY += ");\nGO\n\n", GY;
|
|
15364
|
-
}
|
|
15365
|
-
static buildForeignKeyManyToMany(_, HY, P, UY, WY, GY, KY) {
|
|
15366
|
-
let qY = [..._.keys()].join("], [");
|
|
15367
|
-
return `ALTER TABLE ${shouldPrintSchema$1(WY, KY) ? `[${WY.name}].` : ""}[${P}] ADD FOREIGN KEY ([${qY}]) REFERENCES ${shouldPrintSchema$1(GY, KY) ? `[${GY.name}].` : ""}[${UY}] ${HY};\nGO\n\n`;
|
|
15368
|
-
}
|
|
15369
|
-
static buildFieldName(_, HY) {
|
|
15370
|
-
return `(${_.map((_) => `[${HY.fields[_].name}]`).join(", ")})`;
|
|
15371
|
-
}
|
|
15372
|
-
static exportRefs(_, HY, P) {
|
|
15373
|
-
return _.map((_) => {
|
|
15374
|
-
let UY = "", WY = HY.refs[_], GY = WY.endpointIds.findIndex((_) => HY.endpoints[_].relation === "1"), KY = GY === -1 ? 0 : GY, qY = WY.endpointIds[1 - KY], JY = WY.endpointIds[KY], YY = HY.endpoints[qY], XY = HY.endpoints[JY], ZY = HY.fields[XY.fieldIds[0]], QY = HY.tables[ZY.tableId], $Y = HY.schemas[QY.schemaId], eX = this.buildFieldName(XY.fieldIds, HY, "mssql"), tX = HY.fields[YY.fieldIds[0]], nX = HY.tables[tX.tableId], rX = HY.schemas[nX.schemaId], iX = this.buildFieldName(YY.fieldIds, HY, "mssql");
|
|
15375
|
-
if (GY === -1) {
|
|
15376
|
-
let _ = buildJunctionFields1(XY.fieldIds, HY), WY = buildJunctionFields2(YY.fieldIds, HY, _), GY = buildNewTableName(QY.name, nX.name, P);
|
|
15377
|
-
UY += this.buildTableManyToMany(_, WY, GY, $Y, HY), UY += this.buildForeignKeyManyToMany(_, eX, GY, QY.name, $Y, $Y, HY), UY += this.buildForeignKeyManyToMany(WY, iX, GY, nX.name, $Y, rX, HY);
|
|
15378
|
-
} else UY = `ALTER TABLE ${shouldPrintSchema$1(rX, HY) ? `[${rX.name}].` : ""}[${nX.name}] ADD `, WY.name && (UY += `CONSTRAINT [${WY.name}] `), UY += `FOREIGN KEY ${iX} REFERENCES ${shouldPrintSchema$1($Y, HY) ? `[${$Y.name}].` : ""}[${QY.name}] ${eX}`, WY.onDelete && (UY += ` ON DELETE ${WY.onDelete.toUpperCase()}`), WY.onUpdate && (UY += ` ON UPDATE ${WY.onUpdate.toUpperCase()}`), UY += "\nGO\n";
|
|
15379
|
-
return UY;
|
|
15380
|
-
});
|
|
15381
|
-
}
|
|
15382
|
-
static exportIndexes(_, HY) {
|
|
15383
|
-
return _.filter((_) => !HY.indexes[_].pk).map((_, P) => {
|
|
15384
|
-
let UY = HY.indexes[_], WY = HY.tables[UY.tableId], GY = HY.schemas[WY.schemaId], KY = "CREATE";
|
|
15385
|
-
UY.unique && (KY += " UNIQUE");
|
|
15386
|
-
let qY = UY.name ? `[${UY.name}]` : `${shouldPrintSchema$1(GY, HY) ? `[${GY.name}].` : ""}[${WY.name}_index_${P}]`;
|
|
15387
|
-
KY += ` INDEX ${qY} ON ${shouldPrintSchema$1(GY, HY) ? `[${GY.name}].` : ""}[${WY.name}]`;
|
|
15388
|
-
let JY = [];
|
|
15389
|
-
return UY.columnIds.forEach((_) => {
|
|
15390
|
-
let P = HY.indexColumns[_], UY = "";
|
|
15391
|
-
UY = P.type === "expression" ? `(${P.value})` : `"${P.value}"`, JY.push(UY);
|
|
15392
|
-
}), KY += ` (${JY.join(", ")})`, KY += "\nGO\n", KY;
|
|
15393
|
-
});
|
|
15394
|
-
}
|
|
15395
|
-
static exportComments(_, HY) {
|
|
15396
|
-
return _.map((_) => {
|
|
15397
|
-
let P = HY.tables[_.tableId], UY = HY.schemas[P.schemaId], WY = "";
|
|
15398
|
-
switch (WY = "EXEC sp_addextendedproperty\n", _.type) {
|
|
15399
|
-
case "table":
|
|
15400
|
-
WY += "@name = N'Table_Description',\n", WY += `@value = '${P.note.replace(/'/g, "''")}',\n`, WY += `@level0type = N'Schema', @level0name = '${shouldPrintSchema$1(UY, HY) ? `${UY.name}` : "dbo"}',\n`, WY += `@level1type = N'Table', @level1name = '${P.name}';\n`;
|
|
15401
|
-
break;
|
|
15402
|
-
case "column": {
|
|
15403
|
-
let GY = HY.fields[_.fieldId];
|
|
15404
|
-
WY += "@name = N'Column_Description',\n", WY += `@value = '${GY.note.replace(/'/g, "''")}',\n`, WY += `@level0type = N'Schema', @level0name = '${shouldPrintSchema$1(UY, HY) ? `${UY.name}` : "dbo"}',\n`, WY += `@level1type = N'Table', @level1name = '${P.name}',\n`, WY += `@level2type = N'Column', @level2name = '${GY.name}';\n`;
|
|
15405
|
-
break;
|
|
15406
|
-
}
|
|
15407
|
-
default: break;
|
|
15408
|
-
}
|
|
15409
|
-
return WY += "GO\n", WY;
|
|
15410
|
-
});
|
|
15411
|
-
}
|
|
15412
|
-
static export(HY) {
|
|
15413
|
-
let P = HY.database[1], UY = new Set(Object.values(HY.tables).map((_) => _.name)), WY = P.schemaIds.reduce((P, WY) => {
|
|
15414
|
-
let GY = HY.schemas[WY], { tableIds: KY, refIds: qY } = GY;
|
|
15415
|
-
shouldPrintSchema$1(GY, HY) && P.schemas.push(`CREATE SCHEMA [${GY.name}]\nGO\n`), isEmpty_default(KY) || P.tables.push(..._.exportTables(KY, HY));
|
|
15416
|
-
let JY = flatten_default(KY.map((_) => HY.tables[_].indexIds));
|
|
15417
|
-
isEmpty_default(JY) || P.indexes.push(..._.exportIndexes(JY, HY));
|
|
15418
|
-
let YY = flatten_default(KY.map((_) => {
|
|
15419
|
-
let { fieldIds: P, note: UY } = HY.tables[_], WY = P.filter((_) => HY.fields[_].note).map((HY) => ({
|
|
15420
|
-
type: "column",
|
|
15421
|
-
fieldId: HY,
|
|
15422
|
-
tableId: _
|
|
15423
|
-
}));
|
|
15424
|
-
return UY ? [{
|
|
15425
|
-
type: "table",
|
|
15426
|
-
tableId: _
|
|
15427
|
-
}].concat(WY) : WY;
|
|
15428
|
-
}));
|
|
15429
|
-
return isEmpty_default(YY) || P.comments.push(..._.exportComments(YY, HY)), isEmpty_default(qY) || P.refs.push(..._.exportRefs(qY, HY, UY)), P;
|
|
15430
|
-
}, {
|
|
15431
|
-
schemas: [],
|
|
15432
|
-
enums: [],
|
|
15433
|
-
tables: [],
|
|
15434
|
-
indexes: [],
|
|
15435
|
-
comments: [],
|
|
15436
|
-
refs: []
|
|
15437
|
-
}), GY = _.exportRecords(HY), KY = isEmpty_default(GY) ? [] : (() => {
|
|
15438
|
-
let _ = [...new Set(Object.values(HY.records || {}).map((_) => _.schemaName ? `[${_.schemaName}].[${_.tableName}]` : `[${_.tableName}]`))];
|
|
15439
|
-
return [
|
|
15440
|
-
"-- Disable constraint checks for tables with data",
|
|
15441
|
-
..._.map((_) => `ALTER TABLE ${_} NOCHECK CONSTRAINT ALL;\nGO`),
|
|
15442
|
-
"",
|
|
15443
|
-
...GY,
|
|
15444
|
-
"",
|
|
15445
|
-
"-- Re-enable constraint checks",
|
|
15446
|
-
..._.map((_) => `ALTER TABLE ${_} WITH CHECK CHECK CONSTRAINT ALL;\nGO`)
|
|
15447
|
-
];
|
|
15448
|
-
})();
|
|
15449
|
-
return concat_default(WY.schemas, WY.enums, WY.tables, WY.indexes, WY.comments, WY.refs, KY).join("\n");
|
|
15450
|
-
}
|
|
15451
|
-
};
|
|
15452
|
-
init_lodash();
|
|
15453
|
-
var OracleExporter_default = class {
|
|
15454
|
-
static exportRecords(_) {
|
|
15455
|
-
let HY = Object.values(_.records || {});
|
|
15456
|
-
return isEmpty_default(HY) ? [] : HY.map((_) => {
|
|
15457
|
-
let { schemaName: HY, tableName: P, columns: UY, values: WY } = _, GY = HY ? `"${HY}"."${P}"` : `"${P}"`, KY = UY.length > 0 ? `("${UY.join("\", \"")}")` : "", qY = (_) => {
|
|
15458
|
-
if (_.value === null) return "NULL";
|
|
15459
|
-
if (_.type === "expression" || isNumericType(_.type)) return _.value;
|
|
15460
|
-
if (isBooleanType(_.type)) return String(_.value).toUpperCase() === "TRUE" ? "1" : "0";
|
|
15461
|
-
if (isDateTimeType(_.type)) {
|
|
15462
|
-
let HY = parseIsoDatetime(_.value);
|
|
15463
|
-
if (HY) {
|
|
15464
|
-
let { datetime: _, hasTimezone: P } = HY, UY = formatDatetimeForOracle(_, P);
|
|
15465
|
-
return P ? `TO_TIMESTAMP_TZ('${UY}', 'YYYY-MM-DD HH24:MI:SS.FF3 TZH:TZM')` : `TO_TIMESTAMP('${UY}', 'YYYY-MM-DD HH24:MI:SS.FF3')`;
|
|
15466
|
-
}
|
|
15467
|
-
return `'${_.value.replace(/'/g, "''")}'`;
|
|
15468
|
-
}
|
|
15469
|
-
return isStringType(_.type) ? `'${_.value.replace(/'/g, "''")}'` : isBinaryType(_.type) ? `HEXTORAW('${_.value}')` : `CAST('${_.value.replace(/'/g, "''")}' AS ${_.type})`;
|
|
15470
|
-
};
|
|
15471
|
-
return WY.length > 1 ? `INSERT ALL\n${WY.map((_) => ` INTO ${GY} ${KY} VALUES (${_.map(qY).join(", ")})`).join("\n")}\nSELECT * FROM dual;` : `INSERT INTO ${GY} ${KY}\nVALUES (${WY[0].map(qY).join(", ")});`;
|
|
15472
|
-
});
|
|
15473
|
-
}
|
|
15474
|
-
static buildSchemaToTableNameSetMap(_) {
|
|
15475
|
-
let HY = /* @__PURE__ */ new Map();
|
|
15476
|
-
return forEach_default(_.tables, (P) => {
|
|
15477
|
-
let UY = _.schemas[P.schemaId], WY = HY.get(UY.name);
|
|
15478
|
-
if (!WY) {
|
|
15479
|
-
HY.set(UY.name, new Set([P.name]));
|
|
15480
|
-
return;
|
|
15481
|
-
}
|
|
15482
|
-
WY.add(P.name);
|
|
15483
|
-
}), HY;
|
|
15484
|
-
}
|
|
15485
|
-
static buildTableNameWithSchema(_, HY, P) {
|
|
15486
|
-
return `${shouldPrintSchema$1(HY, _) ? `${escapeObjectName(HY.name, "oracle")}.` : ""}${escapeObjectName(P.name, "oracle")}`;
|
|
15487
|
-
}
|
|
15488
|
-
static exportSchema(_) {
|
|
15489
|
-
return `CREATE USER ${escapeObjectName(_, "oracle")}\nNO AUTHENTICATION
|
|
15490
|
-
DEFAULT TABLESPACE system
|
|
15491
|
-
TEMPORARY TABLESPACE temp
|
|
15492
|
-
QUOTA UNLIMITED ON system;
|
|
15493
|
-
`;
|
|
15494
|
-
}
|
|
15495
|
-
static getFieldLines(_, HY) {
|
|
15496
|
-
return HY.tables[_].fieldIds.map((_) => {
|
|
15497
|
-
let P = HY.fields[_], UY = escapeObjectName(P.name, "oracle"), WY = UY;
|
|
15498
|
-
if (P.enumId) {
|
|
15499
|
-
let _ = HY.enums[P.enumId].valueIds.map((_) => `'${HY.enumValues[_].name}'`).join(", ");
|
|
15500
|
-
WY += ` nvarchar2(255) NOT NULL CHECK (${UY} IN (${_}))`;
|
|
15501
|
-
} else WY += ` ${P.type.type_name}`;
|
|
15502
|
-
let GY = { ...P };
|
|
15503
|
-
if (GY.increment && (WY += " GENERATED AS IDENTITY", GY.dbdefault = null, GY.not_null = !1), GY.dbdefault && (GY.dbdefault.type === "boolean" && (GY.dbdefault.value === null || typeof GY.dbdefault.value == "string" && GY.dbdefault.value.toLowerCase() === "null") || (GY.dbdefault.type === "string" ? WY += ` DEFAULT '${GY.dbdefault.value}'` : WY += ` DEFAULT ${GY.dbdefault.value}`)), GY.unique && (WY += " UNIQUE"), GY.pk && (WY += " PRIMARY KEY"), GY.not_null && (WY += " NOT NULL"), GY.checkIds && GY.checkIds.length > 0) if (GY.checkIds.length === 1) {
|
|
15504
|
-
let _ = HY.checks[GY.checkIds[0]];
|
|
15505
|
-
_.name && (WY += ` CONSTRAINT "${_.name}"`), WY += ` CHECK (${_.expression})`;
|
|
15506
|
-
} else {
|
|
15507
|
-
let _ = GY.checkIds.map((_) => `(${HY.checks[_].expression})`);
|
|
15508
|
-
WY += ` CHECK (${_.join(" AND ")})`;
|
|
15509
|
-
}
|
|
15510
|
-
return WY;
|
|
15511
|
-
});
|
|
15512
|
-
}
|
|
15513
|
-
static getCompositePKs(_, HY) {
|
|
15514
|
-
let P = HY.tables[_];
|
|
15515
|
-
return (P.indexIds ? P.indexIds.filter((_) => HY.indexes[_].pk) : []).map((_) => {
|
|
15516
|
-
let P = HY.indexes[_], UY = "PRIMARY KEY", WY = [];
|
|
15517
|
-
return P.columnIds.forEach((_) => {
|
|
15518
|
-
let P = HY.indexColumns[_], UY = "";
|
|
15519
|
-
UY = P.type === "expression" ? `(${P.value})` : `"${P.value}"`, WY.push(UY);
|
|
15520
|
-
}), UY += ` (${WY.join(", ")})`, UY;
|
|
15521
|
-
});
|
|
15522
|
-
}
|
|
15523
|
-
static getCheckLines(_, HY) {
|
|
15524
|
-
let P = HY.tables[_];
|
|
15525
|
-
return !P.checkIds || P.checkIds.length === 0 ? [] : P.checkIds.map((_) => {
|
|
15526
|
-
let P = HY.checks[_], UY = "";
|
|
15527
|
-
return P.name && (UY = `CONSTRAINT "${P.name}" `), UY += `CHECK (${P.expression})`, UY;
|
|
15528
|
-
});
|
|
15529
|
-
}
|
|
15530
|
-
static getTableContents(_, HY) {
|
|
15531
|
-
return _.map((_) => ({
|
|
15532
|
-
tableId: _,
|
|
15533
|
-
fieldContents: this.getFieldLines(_, HY),
|
|
15534
|
-
checkContents: this.getCheckLines(_, HY),
|
|
15535
|
-
compositePKs: this.getCompositePKs(_, HY)
|
|
15536
|
-
}));
|
|
15537
|
-
}
|
|
15538
|
-
static exportTables(_, HY) {
|
|
15539
|
-
return this.getTableContents(_, HY).map((_) => {
|
|
15540
|
-
let P = [
|
|
15541
|
-
..._.fieldContents,
|
|
15542
|
-
..._.checkContents,
|
|
15543
|
-
..._.compositePKs
|
|
15544
|
-
], UY = HY.tables[_.tableId], WY = HY.schemas[UY.schemaId];
|
|
15545
|
-
return `CREATE TABLE ${this.buildTableNameWithSchema(HY, WY, UY)} (\n${P.map((_) => ` ${_}`).join(",\n")}\n);\n`;
|
|
15546
|
-
});
|
|
15547
|
-
}
|
|
15548
|
-
static buildReferenceFieldNamesString(_, HY) {
|
|
15549
|
-
return `(${_.map((_) => `"${HY.fields[_].name}"`).join(", ")})`;
|
|
15550
|
-
}
|
|
15551
|
-
static buildTableManyToMany(_, HY, P) {
|
|
15552
|
-
let UY = `CREATE TABLE ${P} (\n`;
|
|
15553
|
-
_.forEach((_, HY) => {
|
|
15554
|
-
UY += ` "${HY}" ${_},\n`;
|
|
15555
|
-
}), HY.forEach((_, HY) => {
|
|
15556
|
-
UY += ` "${HY}" ${_},\n`;
|
|
15557
|
-
});
|
|
15558
|
-
let WY = [..._.keys()].join("`, `"), GY = [...HY.keys()].join("`, `");
|
|
15559
|
-
return UY += ` PRIMARY KEY ("${WY}", "${GY}")\n);\n`, UY;
|
|
15560
|
-
}
|
|
15561
|
-
static buildForeignKeyManyToMany(_, HY, P, UY) {
|
|
15562
|
-
return `ALTER TABLE ${_} ADD FOREIGN KEY ("${[...HY.keys()].join("`, `")}") REFERENCES ${P} ${UY} DEFERRABLE INITIALLY IMMEDIATE;\n`;
|
|
15563
|
-
}
|
|
15564
|
-
static exportReferencesAndNewTablesIfExists(_, HY, P) {
|
|
15565
|
-
let UY = {
|
|
15566
|
-
refs: [],
|
|
15567
|
-
tables: []
|
|
15568
|
-
};
|
|
15569
|
-
return _.forEach((_) => {
|
|
15570
|
-
let WY = HY.refs[_], GY = WY.endpointIds.findIndex((_) => HY.endpoints[_].relation === "1"), KY = GY === -1 ? 0 : GY, qY = WY.endpointIds[1 - KY], JY = HY.endpoints[qY], YY = HY.fields[JY.fieldIds[0]], XY = HY.tables[YY.tableId], ZY = HY.schemas[XY.schemaId], QY = this.buildReferenceFieldNamesString(JY.fieldIds, HY), $Y = WY.endpointIds[KY], eX = HY.endpoints[$Y], tX = HY.fields[eX.fieldIds[0]], nX = HY.tables[tX.tableId], rX = HY.schemas[nX.schemaId], iX = this.buildReferenceFieldNamesString(eX.fieldIds, HY);
|
|
15571
|
-
if (GY !== -1) {
|
|
15572
|
-
let _ = `ALTER TABLE ${this.buildTableNameWithSchema(HY, ZY, XY)} ADD`;
|
|
15573
|
-
WY.name && (_ += ` CONSTRAINT ${escapeObjectName(WY.name, "oracle")}`);
|
|
15574
|
-
let P = this.buildTableNameWithSchema(HY, rX, nX);
|
|
15575
|
-
_ += ` FOREIGN KEY ${QY} REFERENCES ${P} ${iX}`, WY.onDelete && (_ += ` ON DELETE ${WY.onDelete.toUpperCase()}`), _ += " DEFERRABLE INITIALLY IMMEDIATE;\n", UY.refs.push(_);
|
|
15576
|
-
return;
|
|
15577
|
-
}
|
|
15578
|
-
let aX = buildJunctionFields1(eX.fieldIds, HY), oX = buildJunctionFields2(JY.fieldIds, HY, aX), sX = buildUniqueTableName(rX, nX.name, XY.name, P), cX = P.get(rX);
|
|
15579
|
-
cX ? cX.add(sX) : P.set(rX, new Set([sX]));
|
|
15580
|
-
let lX = `${shouldPrintSchema$1(rX, HY) ? `"${rX.name}".` : ""}"${sX}"`;
|
|
15581
|
-
UY.tables.push(this.buildTableManyToMany(aX, oX, lX));
|
|
15582
|
-
let uX = this.buildTableNameWithSchema(HY, rX, nX);
|
|
15583
|
-
UY.refs.push(this.buildForeignKeyManyToMany(lX, aX, uX, iX));
|
|
15584
|
-
let dX = this.buildTableNameWithSchema(HY, ZY, XY);
|
|
15585
|
-
UY.refs.push(this.buildForeignKeyManyToMany(lX, oX, dX, QY));
|
|
15586
|
-
}), UY;
|
|
15587
|
-
}
|
|
15588
|
-
static exportReferenceGrants(_, HY) {
|
|
15589
|
-
if (Object.keys(_.schemas).length <= 1) return [];
|
|
15590
|
-
let P = [];
|
|
15591
|
-
return HY.forEach((HY) => {
|
|
15592
|
-
let UY = _.refs[HY], WY = UY.endpointIds.findIndex((HY) => _.endpoints[HY].relation === "1"), GY = WY === -1 ? 0 : WY, KY = UY.endpointIds[GY], qY = _.endpoints[KY], JY = _.fields[qY.fieldIds[0]], YY = _.tables[JY.tableId], XY = _.schemas[YY.schemaId], ZY = UY.endpointIds[1 - GY], QY = _.endpoints[ZY], $Y = _.fields[QY.fieldIds[0]], eX = _.tables[$Y.tableId], tX = _.schemas[eX.schemaId];
|
|
15593
|
-
if (XY.name === tX.name) {
|
|
15594
|
-
P.push("");
|
|
15595
|
-
return;
|
|
15596
|
-
}
|
|
15597
|
-
let nX = this.buildTableNameWithSchema(_, XY, YY);
|
|
15598
|
-
if (P.push(nX), WY !== -1) return;
|
|
15599
|
-
let rX = this.buildTableNameWithSchema(_, tX, eX);
|
|
15600
|
-
P.push(rX);
|
|
15601
|
-
}), P.filter((_, HY) => _ && P.indexOf(_) === HY).map((_) => `GRANT REFERENCES ON ${_} TO PUBLIC;\n`);
|
|
15602
|
-
}
|
|
15603
|
-
static exportIndexes(_, HY) {
|
|
15604
|
-
return _.filter((_) => !HY.indexes[_].pk).map((_) => {
|
|
15605
|
-
let P = HY.indexes[_], UY = HY.tables[P.tableId], WY = HY.schemas[UY.schemaId], GY = "CREATE";
|
|
15606
|
-
P.unique && (GY += " UNIQUE"), GY += " INDEX", P.name && (GY += ` ${escapeObjectName(P.name, "oracle")}`);
|
|
15607
|
-
let KY = this.buildTableNameWithSchema(HY, WY, UY);
|
|
15608
|
-
GY += ` ON ${KY}`;
|
|
15609
|
-
let qY = [];
|
|
15610
|
-
P.columnIds.forEach((_) => {
|
|
15611
|
-
let P = HY.indexColumns[_], UY = "";
|
|
15612
|
-
UY = P.type === "expression" ? `${P.value}` : `"${P.value}"`, qY.push(UY);
|
|
15613
|
-
});
|
|
15614
|
-
let JY = qY.join(", ");
|
|
15615
|
-
return GY += ` (${JY});\n`, GY;
|
|
15616
|
-
});
|
|
15617
|
-
}
|
|
15618
|
-
static exportComments(_, HY) {
|
|
15619
|
-
return _.map((_) => {
|
|
15620
|
-
let P = "COMMENT ON", UY = HY.tables[_.tableId], WY = HY.schemas[UY.schemaId], GY = this.buildTableNameWithSchema(HY, WY, UY);
|
|
15621
|
-
switch (_.type) {
|
|
15622
|
-
case "table":
|
|
15623
|
-
P += ` TABLE ${GY} IS '${UY.note.replace(/'/g, "''")}'`;
|
|
15624
|
-
break;
|
|
15625
|
-
case "column": {
|
|
15626
|
-
let UY = HY.fields[_.fieldId];
|
|
15627
|
-
P += ` COLUMN ${GY}.${escapeObjectName(UY.name, "oracle")} IS '${UY.note.replace(/'/g, "''")}'`;
|
|
15628
|
-
break;
|
|
15629
|
-
}
|
|
15630
|
-
default: break;
|
|
15631
|
-
}
|
|
15632
|
-
return P += ";\n", P;
|
|
15633
|
-
});
|
|
15634
|
-
}
|
|
15635
|
-
static export(_) {
|
|
15636
|
-
let HY = _.database[1], P = this.buildSchemaToTableNameSetMap(_), UY = HY.schemaIds.reduce((HY, UY) => {
|
|
15637
|
-
let WY = _.schemas[UY], { tableIds: GY, refIds: KY } = WY;
|
|
15638
|
-
shouldPrintSchema$1(WY, _) && HY.schemas.push(this.exportSchema(WY.name)), isEmpty_default(GY) || HY.tables.push(...this.exportTables(GY, _));
|
|
15639
|
-
let qY = flatten_default(GY.map((HY) => _.tables[HY].indexIds));
|
|
15640
|
-
isEmpty_default(qY) || HY.indexes.push(...this.exportIndexes(qY, _));
|
|
15641
|
-
let JY = flatten_default(GY.map((HY) => {
|
|
15642
|
-
let { fieldIds: P, note: UY } = _.tables[HY], WY = P.filter((HY) => _.fields[HY].note).map((_) => ({
|
|
15643
|
-
type: "column",
|
|
15644
|
-
fieldId: _,
|
|
15645
|
-
tableId: HY
|
|
15646
|
-
}));
|
|
15647
|
-
return UY ? [{
|
|
15648
|
-
type: "table",
|
|
15649
|
-
tableId: HY
|
|
15650
|
-
}].concat(WY) : WY;
|
|
15651
|
-
}));
|
|
15652
|
-
if (isEmpty_default(JY) || HY.comments.push(...this.exportComments(JY, _)), !isEmpty_default(KY)) {
|
|
15653
|
-
let { refs: UY, tables: WY } = this.exportReferencesAndNewTablesIfExists(KY, _, P);
|
|
15654
|
-
HY.tables.push(...WY), HY.refs.push(...UY), HY.referenceGrants.push(...this.exportReferenceGrants(_, KY));
|
|
15655
|
-
}
|
|
15656
|
-
return HY;
|
|
15657
|
-
}, {
|
|
15658
|
-
schemas: [],
|
|
15659
|
-
tables: [],
|
|
15660
|
-
indexes: [],
|
|
15661
|
-
comments: [],
|
|
15662
|
-
referenceGrants: [],
|
|
15663
|
-
refs: []
|
|
15664
|
-
}), WY = this.exportRecords(_), GY = isEmpty_default(WY) ? [] : [
|
|
15665
|
-
"-- Disable constraint checking for INSERT",
|
|
15666
|
-
"SET CONSTRAINTS ALL DEFERRED;",
|
|
15667
|
-
"",
|
|
15668
|
-
...WY,
|
|
15669
|
-
"",
|
|
15670
|
-
"SET CONSTRAINTS ALL IMMEDIATE;",
|
|
15671
|
-
"COMMIT;"
|
|
15672
|
-
];
|
|
15673
|
-
return concat_default(UY.schemas, UY.tables, UY.indexes, UY.comments, UY.referenceGrants, UY.refs, GY).join("\n");
|
|
15674
|
-
}
|
|
15675
|
-
}, ModelExporter_default = class {
|
|
15676
|
-
static export(_ = {}, HY = "", P = {}) {
|
|
15677
|
-
let UY = "", WY = typeof P == "boolean" ? { isNormalized: P } : P, GY = WY.isNormalized === !1 ? _.normalize() : _;
|
|
15678
|
-
switch (HY) {
|
|
15679
|
-
case "dbml":
|
|
15680
|
-
UY = DbmlExporter_default.export(GY, WY);
|
|
15681
|
-
break;
|
|
15682
|
-
case "mysql":
|
|
15683
|
-
UY = MysqlExporter_default.export(GY);
|
|
15684
|
-
break;
|
|
15685
|
-
case "postgres":
|
|
15686
|
-
UY = PostgresExporter_default.export(GY);
|
|
15687
|
-
break;
|
|
15688
|
-
case "json":
|
|
15689
|
-
UY = JsonExporter_default.export(_, WY);
|
|
15690
|
-
break;
|
|
15691
|
-
case "mssql":
|
|
15692
|
-
UY = SqlServerExporter_default.export(GY);
|
|
15693
|
-
break;
|
|
15694
|
-
case "oracle":
|
|
15695
|
-
UY = OracleExporter_default.export(GY);
|
|
15696
|
-
break;
|
|
15697
|
-
default: break;
|
|
15698
|
-
}
|
|
15699
|
-
return UY;
|
|
15700
|
-
}
|
|
15701
15289
|
}, ElementError = class extends Error {
|
|
15702
15290
|
constructor(_, HY = { start: {
|
|
15703
15291
|
line: 1,
|
|
@@ -15919,7 +15507,7 @@ var table_default = class extends element_default {
|
|
|
15919
15507
|
this.checkField(_), this.fields.push(_);
|
|
15920
15508
|
}
|
|
15921
15509
|
checkField(_) {
|
|
15922
|
-
this.fields.some((HY) => HY.name === _.name) && _.error(`Field "${_.name}" existed in table ${shouldPrintSchema(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
|
|
15510
|
+
this.fields.some((HY) => HY.name === _.name) && _.error(`Field "${_.name}" existed in table ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
|
|
15923
15511
|
}
|
|
15924
15512
|
processIndexes(_) {
|
|
15925
15513
|
_.forEach((_) => {
|
|
@@ -15934,7 +15522,7 @@ var table_default = class extends element_default {
|
|
|
15934
15522
|
}
|
|
15935
15523
|
checkIndex(_) {
|
|
15936
15524
|
_.columns.forEach((HY) => {
|
|
15937
|
-
HY.type === "column" && !this.findField(HY.value) && _.error(`Column "${HY.value}" do not exist in table ${shouldPrintSchema(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
|
|
15525
|
+
HY.type === "column" && !this.findField(HY.value) && _.error(`Column "${HY.value}" do not exist in table ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
|
|
15938
15526
|
});
|
|
15939
15527
|
}
|
|
15940
15528
|
processChecks(_) {
|
|
@@ -16099,13 +15687,13 @@ var enum_default = class extends element_default {
|
|
|
16099
15687
|
this.checkValue(_), this.values.push(_);
|
|
16100
15688
|
}
|
|
16101
15689
|
checkValue(_) {
|
|
16102
|
-
this.values.some((HY) => HY.name === _.name) && _.error(`Enum value "${_.name}" existed in enum ${shouldPrintSchema(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
|
|
15690
|
+
this.values.some((HY) => HY.name === _.name) && _.error(`Enum value "${_.name}" existed in enum ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
|
|
16103
15691
|
}
|
|
16104
15692
|
pushField(_) {
|
|
16105
15693
|
this.checkField(_), this.fields.push(_);
|
|
16106
15694
|
}
|
|
16107
15695
|
checkField(_) {
|
|
16108
|
-
this.fields.some((HY) => HY.id === _.id) && this.error(`Field ${shouldPrintSchema(_.table.schema) ? `"${_.table.schema.name}".` : ""}"${_.table.name}"."${_.name}" already associated with enum ${shouldPrintSchema(this.schema) ? `"${this.schema.name}".` : ""}${this.name}"`);
|
|
15696
|
+
this.fields.some((HY) => HY.id === _.id) && this.error(`Field ${shouldPrintSchema$1(_.table.schema) ? `"${_.table.schema.name}".` : ""}"${_.table.name}"."${_.name}" already associated with enum ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}${this.name}"`);
|
|
16109
15697
|
}
|
|
16110
15698
|
export() {
|
|
16111
15699
|
return {
|
|
@@ -16158,7 +15746,7 @@ var tableGroup_default = class extends element_default {
|
|
|
16158
15746
|
this.checkTable(_), this.tables.push(_), _.group = this;
|
|
16159
15747
|
}
|
|
16160
15748
|
checkTable(_) {
|
|
16161
|
-
this.tables.some((HY) => HY.id === _.id) && this.error(`Table ${shouldPrintSchema(_.schema) ? `"${_.schema.name}".` : ""}.${_.name} is already in the group`), _.group && this.error(`Table ${shouldPrintSchema(_.schema) ? `"${_.schema.name}".` : ""}.${_.name} is already in group ${shouldPrintSchema(_.group.schema) ? `"${_.group.schema.name}".` : ""}${_.group.name}`);
|
|
15749
|
+
this.tables.some((HY) => HY.id === _.id) && this.error(`Table ${shouldPrintSchema$1(_.schema) ? `"${_.schema.name}".` : ""}.${_.name} is already in the group`), _.group && this.error(`Table ${shouldPrintSchema$1(_.schema) ? `"${_.schema.name}".` : ""}.${_.name} is already in group ${shouldPrintSchema$1(_.group.schema) ? `"${_.group.schema.name}".` : ""}${_.group.name}`);
|
|
16162
15750
|
}
|
|
16163
15751
|
export() {
|
|
16164
15752
|
return {
|
|
@@ -16234,12 +15822,12 @@ var tableGroup_default = class extends element_default {
|
|
|
16234
15822
|
if (_) P.push(_.name);
|
|
16235
15823
|
else {
|
|
16236
15824
|
let _ = HY.indexes.find((_) => _.pk);
|
|
16237
|
-
_ ? P = _.columns.map((_) => _.value) : this.error(`Can't find primary or composite key in table ${shouldPrintSchema(HY.schema) ? `"${HY.schema.name}".` : ""}"${HY.name}"`);
|
|
15825
|
+
_ ? P = _.columns.map((_) => _.value) : this.error(`Can't find primary or composite key in table ${shouldPrintSchema$1(HY.schema) ? `"${HY.schema.name}".` : ""}"${HY.name}"`);
|
|
16238
15826
|
}
|
|
16239
15827
|
}
|
|
16240
15828
|
P.forEach((_) => {
|
|
16241
15829
|
let P = HY.findField(_);
|
|
16242
|
-
P || this.error(`Can't find field ${shouldPrintSchema(HY.schema) ? `"${HY.schema.name}".` : ""}"${_}" in table "${HY.name}"`), this.fields.push(P), P.pushEndpoint(this);
|
|
15830
|
+
P || this.error(`Can't find field ${shouldPrintSchema$1(HY.schema) ? `"${HY.schema.name}".` : ""}"${_}" in table "${HY.name}"`), this.fields.push(P), P.pushEndpoint(this);
|
|
16243
15831
|
});
|
|
16244
15832
|
}
|
|
16245
15833
|
normalize(_) {
|
|
@@ -16324,7 +15912,7 @@ var schema_default = class extends element_default {
|
|
|
16324
15912
|
this.checkTable(_), this.tables.push(_);
|
|
16325
15913
|
}
|
|
16326
15914
|
checkTable(_) {
|
|
16327
|
-
this.tables.some((HY) => HY.name === _.name) && _.error(`Table ${shouldPrintSchema(this) ? `"${this.name}".` : ""}"${_.name}" existed`);
|
|
15915
|
+
this.tables.some((HY) => HY.name === _.name) && _.error(`Table ${shouldPrintSchema$1(this) ? `"${this.name}".` : ""}"${_.name}" existed`);
|
|
16328
15916
|
}
|
|
16329
15917
|
findTable(_) {
|
|
16330
15918
|
return this.tables.find((HY) => HY.name === _);
|
|
@@ -16341,7 +15929,7 @@ var schema_default = class extends element_default {
|
|
|
16341
15929
|
this.checkEnum(_), this.enums.push(_);
|
|
16342
15930
|
}
|
|
16343
15931
|
checkEnum(_) {
|
|
16344
|
-
this.enums.some((HY) => HY.name === _.name) && _.error(`Enum ${shouldPrintSchema(this) ? `"${this.name}".` : ""}"${_.name}" existed`);
|
|
15932
|
+
this.enums.some((HY) => HY.name === _.name) && _.error(`Enum ${shouldPrintSchema$1(this) ? `"${this.name}".` : ""}"${_.name}" existed`);
|
|
16345
15933
|
}
|
|
16346
15934
|
processRefs(_) {
|
|
16347
15935
|
_.forEach((_) => {
|
|
@@ -16372,7 +15960,7 @@ var schema_default = class extends element_default {
|
|
|
16372
15960
|
this.checkTableGroup(_), this.tableGroups.push(_);
|
|
16373
15961
|
}
|
|
16374
15962
|
checkTableGroup(_) {
|
|
16375
|
-
this.tableGroups.some((HY) => HY.name === _.name) && _.error(`Table Group ${shouldPrintSchema(this) ? `"${this.name}".` : ""}"${_.name}" existed`);
|
|
15963
|
+
this.tableGroups.some((HY) => HY.name === _.name) && _.error(`Table Group ${shouldPrintSchema$1(this) ? `"${this.name}".` : ""}"${_.name}" existed`);
|
|
16376
15964
|
}
|
|
16377
15965
|
checkSameId(_) {
|
|
16378
15966
|
return this.name === _.name || this.alias === _.name || this.name === _.alias || this.alias && this.alias === _.alias;
|
|
@@ -16645,6 +16233,393 @@ var database_default = class extends element_default {
|
|
|
16645
16233
|
_.records[HY.id] = { ...HY };
|
|
16646
16234
|
}), this.tablePartials.forEach((HY) => HY.normalize(_)), _;
|
|
16647
16235
|
}
|
|
16236
|
+
}, JsonExporter_default = class {
|
|
16237
|
+
static export(_, HY) {
|
|
16238
|
+
let { isNormalized: P } = HY;
|
|
16239
|
+
return !P && _ instanceof database_default ? JSON.stringify(_.export(), null, 2) : JSON.stringify(_, null, 2);
|
|
16240
|
+
}
|
|
16241
|
+
};
|
|
16242
|
+
init_lodash();
|
|
16243
|
+
var SqlServerExporter_default = class _ {
|
|
16244
|
+
static exportRecords(_) {
|
|
16245
|
+
let HY = Object.values(_.records || {});
|
|
16246
|
+
return isEmpty_default(HY) ? [] : HY.map((_) => {
|
|
16247
|
+
let { schemaName: HY, tableName: P, columns: UY, values: WY } = _, GY = HY ? `[${HY}].[${P}]` : `[${P}]`, KY = UY.length > 0 ? `([${UY.join("], [")}])` : "", qY = (_) => _.value === null ? "NULL" : _.type === "expression" || isNumericType(_.type) ? _.value : isBooleanType(_.type) ? String(_.value).toUpperCase() === "TRUE" ? "1" : "0" : isStringType(_.type) || isDateTimeType(_.type) ? `'${_.value.replace(/'/g, "''")}'` : isBinaryType(_.type) ? `0x${_.value}` : `CAST('${_.value.replace(/'/g, "''")}' AS ${_.type})`;
|
|
16248
|
+
return `INSERT INTO ${GY} ${KY}\nVALUES\n ${WY.map((_) => `(${_.map(qY).join(", ")})`).join(",\n ")};\nGO`;
|
|
16249
|
+
});
|
|
16250
|
+
}
|
|
16251
|
+
static getFieldLines(_, HY) {
|
|
16252
|
+
return HY.tables[_].fieldIds.map((_) => {
|
|
16253
|
+
let P = HY.fields[_], UY = "";
|
|
16254
|
+
if (P.enumId) {
|
|
16255
|
+
let _ = HY.enums[P.enumId];
|
|
16256
|
+
UY = `[${P.name}] nvarchar(255) NOT NULL CHECK ([${P.name}] IN (`;
|
|
16257
|
+
let WY = _.valueIds.map((_) => `'${HY.enumValues[_].name}'`);
|
|
16258
|
+
UY += `${WY.join(", ")}))`;
|
|
16259
|
+
} else UY = `[${P.name}] ${P.type.type_name === "varchar" ? "nvarchar(255)" : P.type.type_name}`;
|
|
16260
|
+
if (P.unique && (UY += " UNIQUE"), P.pk && (UY += " PRIMARY KEY"), P.not_null && (UY += " NOT NULL"), P.increment && (UY += " IDENTITY(1, 1)"), P.checkIds && P.checkIds.length > 0) if (P.checkIds.length === 1) {
|
|
16261
|
+
let _ = HY.checks[P.checkIds[0]];
|
|
16262
|
+
_.name && (UY += ` CONSTRAINT [${_.name}]`), UY += ` CHECK (${_.expression})`;
|
|
16263
|
+
} else {
|
|
16264
|
+
let _ = P.checkIds.map((_) => `(${HY.checks[_].expression})`);
|
|
16265
|
+
UY += ` CHECK (${_.join(" AND ")})`;
|
|
16266
|
+
}
|
|
16267
|
+
return P.dbdefault && (P.dbdefault.type === "boolean" && (P.dbdefault.value === null || typeof P.dbdefault.value == "string" && P.dbdefault.value.toLowerCase() === "null") || (P.dbdefault.type === "expression" ? UY += ` DEFAULT (${P.dbdefault.value})` : P.dbdefault.type === "string" ? UY += ` DEFAULT '${P.dbdefault.value}'` : UY += ` DEFAULT (${P.dbdefault.value})`)), UY;
|
|
16268
|
+
});
|
|
16269
|
+
}
|
|
16270
|
+
static getCompositePKs(_, HY) {
|
|
16271
|
+
let P = HY.tables[_];
|
|
16272
|
+
return (P.indexIds ? P.indexIds.filter((_) => HY.indexes[_].pk) : []).map((_) => {
|
|
16273
|
+
let P = HY.indexes[_], UY = "PRIMARY KEY", WY = [];
|
|
16274
|
+
return P.columnIds.forEach((_) => {
|
|
16275
|
+
let P = HY.indexColumns[_], UY = "";
|
|
16276
|
+
UY = P.type === "expression" ? `(${P.value})` : `[${P.value}]`, WY.push(UY);
|
|
16277
|
+
}), UY += ` (${WY.join(", ")})`, UY;
|
|
16278
|
+
});
|
|
16279
|
+
}
|
|
16280
|
+
static getCheckLines(_, HY) {
|
|
16281
|
+
let P = HY.tables[_];
|
|
16282
|
+
return !P.checkIds || P.checkIds.length === 0 ? [] : P.checkIds.map((_) => {
|
|
16283
|
+
let P = HY.checks[_], UY = "";
|
|
16284
|
+
return P.name && (UY = `CONSTRAINT [${P.name}] `), UY += `CHECK (${P.expression})`, UY;
|
|
16285
|
+
});
|
|
16286
|
+
}
|
|
16287
|
+
static getTableContentArr(HY, P) {
|
|
16288
|
+
return HY.map((HY) => ({
|
|
16289
|
+
tableId: HY,
|
|
16290
|
+
fieldContents: _.getFieldLines(HY, P),
|
|
16291
|
+
checkContents: _.getCheckLines(HY, P),
|
|
16292
|
+
compositePKs: _.getCompositePKs(HY, P)
|
|
16293
|
+
}));
|
|
16294
|
+
}
|
|
16295
|
+
static exportTables(HY, P) {
|
|
16296
|
+
return _.getTableContentArr(HY, P).map((_) => {
|
|
16297
|
+
let HY = [
|
|
16298
|
+
..._.fieldContents,
|
|
16299
|
+
..._.checkContents,
|
|
16300
|
+
..._.compositePKs
|
|
16301
|
+
], UY = P.tables[_.tableId], WY = P.schemas[UY.schemaId];
|
|
16302
|
+
return `CREATE TABLE ${shouldPrintSchema(WY, P) ? `[${WY.name}].` : ""}[${UY.name}] (\n${HY.map((_) => ` ${_}`).join(",\n")}\n)\nGO\n`;
|
|
16303
|
+
});
|
|
16304
|
+
}
|
|
16305
|
+
static buildTableManyToMany(_, HY, P, UY, WY) {
|
|
16306
|
+
let GY = `CREATE TABLE ${shouldPrintSchema(UY, WY) ? `[${UY.name}].` : ""}[${P}] (\n`, KY = [..._.keys()].join("], ["), qY = [...HY.keys()].join("], [");
|
|
16307
|
+
return _.forEach((_, HY) => {
|
|
16308
|
+
GY += ` [${HY}] ${_},\n`;
|
|
16309
|
+
}), HY.forEach((_, HY) => {
|
|
16310
|
+
GY += ` [${HY}] ${_},\n`;
|
|
16311
|
+
}), GY += ` PRIMARY KEY ([${KY}], [${qY}])\n`, GY += ");\nGO\n\n", GY;
|
|
16312
|
+
}
|
|
16313
|
+
static buildForeignKeyManyToMany(_, HY, P, UY, WY, GY, KY) {
|
|
16314
|
+
let qY = [..._.keys()].join("], [");
|
|
16315
|
+
return `ALTER TABLE ${shouldPrintSchema(WY, KY) ? `[${WY.name}].` : ""}[${P}] ADD FOREIGN KEY ([${qY}]) REFERENCES ${shouldPrintSchema(GY, KY) ? `[${GY.name}].` : ""}[${UY}] ${HY};\nGO\n\n`;
|
|
16316
|
+
}
|
|
16317
|
+
static buildFieldName(_, HY) {
|
|
16318
|
+
return `(${_.map((_) => `[${HY.fields[_].name}]`).join(", ")})`;
|
|
16319
|
+
}
|
|
16320
|
+
static exportRefs(_, HY, P) {
|
|
16321
|
+
return _.map((_) => {
|
|
16322
|
+
let UY = "", WY = HY.refs[_], GY = WY.endpointIds.findIndex((_) => HY.endpoints[_].relation === "1"), KY = GY === -1 ? 0 : GY, qY = WY.endpointIds[1 - KY], JY = WY.endpointIds[KY], YY = HY.endpoints[qY], XY = HY.endpoints[JY], ZY = HY.fields[XY.fieldIds[0]], QY = HY.tables[ZY.tableId], $Y = HY.schemas[QY.schemaId], eX = this.buildFieldName(XY.fieldIds, HY, "mssql"), tX = HY.fields[YY.fieldIds[0]], nX = HY.tables[tX.tableId], rX = HY.schemas[nX.schemaId], iX = this.buildFieldName(YY.fieldIds, HY, "mssql");
|
|
16323
|
+
if (GY === -1) {
|
|
16324
|
+
let _ = buildJunctionFields1(XY.fieldIds, HY), WY = buildJunctionFields2(YY.fieldIds, HY, _), GY = buildNewTableName(QY.name, nX.name, P);
|
|
16325
|
+
UY += this.buildTableManyToMany(_, WY, GY, $Y, HY), UY += this.buildForeignKeyManyToMany(_, eX, GY, QY.name, $Y, $Y, HY), UY += this.buildForeignKeyManyToMany(WY, iX, GY, nX.name, $Y, rX, HY);
|
|
16326
|
+
} else UY = `ALTER TABLE ${shouldPrintSchema(rX, HY) ? `[${rX.name}].` : ""}[${nX.name}] ADD `, WY.name && (UY += `CONSTRAINT [${WY.name}] `), UY += `FOREIGN KEY ${iX} REFERENCES ${shouldPrintSchema($Y, HY) ? `[${$Y.name}].` : ""}[${QY.name}] ${eX}`, WY.onDelete && (UY += ` ON DELETE ${WY.onDelete.toUpperCase()}`), WY.onUpdate && (UY += ` ON UPDATE ${WY.onUpdate.toUpperCase()}`), UY += "\nGO\n";
|
|
16327
|
+
return UY;
|
|
16328
|
+
});
|
|
16329
|
+
}
|
|
16330
|
+
static exportIndexes(_, HY) {
|
|
16331
|
+
return _.filter((_) => !HY.indexes[_].pk).map((_, P) => {
|
|
16332
|
+
let UY = HY.indexes[_], WY = HY.tables[UY.tableId], GY = HY.schemas[WY.schemaId], KY = "CREATE";
|
|
16333
|
+
UY.unique && (KY += " UNIQUE");
|
|
16334
|
+
let qY = UY.name ? `[${UY.name}]` : `${shouldPrintSchema(GY, HY) ? `[${GY.name}].` : ""}[${WY.name}_index_${P}]`;
|
|
16335
|
+
KY += ` INDEX ${qY} ON ${shouldPrintSchema(GY, HY) ? `[${GY.name}].` : ""}[${WY.name}]`;
|
|
16336
|
+
let JY = [];
|
|
16337
|
+
return UY.columnIds.forEach((_) => {
|
|
16338
|
+
let P = HY.indexColumns[_], UY = "";
|
|
16339
|
+
UY = P.type === "expression" ? `(${P.value})` : `"${P.value}"`, JY.push(UY);
|
|
16340
|
+
}), KY += ` (${JY.join(", ")})`, KY += "\nGO\n", KY;
|
|
16341
|
+
});
|
|
16342
|
+
}
|
|
16343
|
+
static exportComments(_, HY) {
|
|
16344
|
+
return _.map((_) => {
|
|
16345
|
+
let P = HY.tables[_.tableId], UY = HY.schemas[P.schemaId], WY = "";
|
|
16346
|
+
switch (WY = "EXEC sp_addextendedproperty\n", _.type) {
|
|
16347
|
+
case "table":
|
|
16348
|
+
WY += "@name = N'Table_Description',\n", WY += `@value = '${P.note.replace(/'/g, "''")}',\n`, WY += `@level0type = N'Schema', @level0name = '${shouldPrintSchema(UY, HY) ? `${UY.name}` : "dbo"}',\n`, WY += `@level1type = N'Table', @level1name = '${P.name}';\n`;
|
|
16349
|
+
break;
|
|
16350
|
+
case "column": {
|
|
16351
|
+
let GY = HY.fields[_.fieldId];
|
|
16352
|
+
WY += "@name = N'Column_Description',\n", WY += `@value = '${GY.note.replace(/'/g, "''")}',\n`, WY += `@level0type = N'Schema', @level0name = '${shouldPrintSchema(UY, HY) ? `${UY.name}` : "dbo"}',\n`, WY += `@level1type = N'Table', @level1name = '${P.name}',\n`, WY += `@level2type = N'Column', @level2name = '${GY.name}';\n`;
|
|
16353
|
+
break;
|
|
16354
|
+
}
|
|
16355
|
+
default: break;
|
|
16356
|
+
}
|
|
16357
|
+
return WY += "GO\n", WY;
|
|
16358
|
+
});
|
|
16359
|
+
}
|
|
16360
|
+
static export(HY) {
|
|
16361
|
+
let P = HY.database[1], UY = new Set(Object.values(HY.tables).map((_) => _.name)), WY = P.schemaIds.reduce((P, WY) => {
|
|
16362
|
+
let GY = HY.schemas[WY], { tableIds: KY, refIds: qY } = GY;
|
|
16363
|
+
shouldPrintSchema(GY, HY) && P.schemas.push(`CREATE SCHEMA [${GY.name}]\nGO\n`), isEmpty_default(KY) || P.tables.push(..._.exportTables(KY, HY));
|
|
16364
|
+
let JY = flatten_default(KY.map((_) => HY.tables[_].indexIds));
|
|
16365
|
+
isEmpty_default(JY) || P.indexes.push(..._.exportIndexes(JY, HY));
|
|
16366
|
+
let YY = flatten_default(KY.map((_) => {
|
|
16367
|
+
let { fieldIds: P, note: UY } = HY.tables[_], WY = P.filter((_) => HY.fields[_].note).map((HY) => ({
|
|
16368
|
+
type: "column",
|
|
16369
|
+
fieldId: HY,
|
|
16370
|
+
tableId: _
|
|
16371
|
+
}));
|
|
16372
|
+
return UY ? [{
|
|
16373
|
+
type: "table",
|
|
16374
|
+
tableId: _
|
|
16375
|
+
}].concat(WY) : WY;
|
|
16376
|
+
}));
|
|
16377
|
+
return isEmpty_default(YY) || P.comments.push(..._.exportComments(YY, HY)), isEmpty_default(qY) || P.refs.push(..._.exportRefs(qY, HY, UY)), P;
|
|
16378
|
+
}, {
|
|
16379
|
+
schemas: [],
|
|
16380
|
+
enums: [],
|
|
16381
|
+
tables: [],
|
|
16382
|
+
indexes: [],
|
|
16383
|
+
comments: [],
|
|
16384
|
+
refs: []
|
|
16385
|
+
}), GY = _.exportRecords(HY), KY = isEmpty_default(GY) ? [] : (() => {
|
|
16386
|
+
let _ = [...new Set(Object.values(HY.records || {}).map((_) => _.schemaName ? `[${_.schemaName}].[${_.tableName}]` : `[${_.tableName}]`))];
|
|
16387
|
+
return [
|
|
16388
|
+
"-- Disable constraint checks for tables with data",
|
|
16389
|
+
..._.map((_) => `ALTER TABLE ${_} NOCHECK CONSTRAINT ALL;\nGO`),
|
|
16390
|
+
"",
|
|
16391
|
+
...GY,
|
|
16392
|
+
"",
|
|
16393
|
+
"-- Re-enable constraint checks",
|
|
16394
|
+
..._.map((_) => `ALTER TABLE ${_} WITH CHECK CHECK CONSTRAINT ALL;\nGO`)
|
|
16395
|
+
];
|
|
16396
|
+
})();
|
|
16397
|
+
return concat_default(WY.schemas, WY.enums, WY.tables, WY.indexes, WY.comments, WY.refs, KY).join("\n");
|
|
16398
|
+
}
|
|
16399
|
+
};
|
|
16400
|
+
init_lodash();
|
|
16401
|
+
var OracleExporter_default = class {
|
|
16402
|
+
static exportRecords(_) {
|
|
16403
|
+
let HY = Object.values(_.records || {});
|
|
16404
|
+
return isEmpty_default(HY) ? [] : HY.map((_) => {
|
|
16405
|
+
let { schemaName: HY, tableName: P, columns: UY, values: WY } = _, GY = HY ? `"${HY}"."${P}"` : `"${P}"`, KY = UY.length > 0 ? `("${UY.join("\", \"")}")` : "", qY = (_) => {
|
|
16406
|
+
if (_.value === null) return "NULL";
|
|
16407
|
+
if (_.type === "expression" || isNumericType(_.type)) return _.value;
|
|
16408
|
+
if (isBooleanType(_.type)) return String(_.value).toUpperCase() === "TRUE" ? "1" : "0";
|
|
16409
|
+
if (isDateTimeType(_.type)) {
|
|
16410
|
+
let HY = parseIsoDatetime(_.value);
|
|
16411
|
+
if (HY) {
|
|
16412
|
+
let { datetime: _, hasTimezone: P } = HY, UY = formatDatetimeForOracle(_, P);
|
|
16413
|
+
return P ? `TO_TIMESTAMP_TZ('${UY}', 'YYYY-MM-DD HH24:MI:SS.FF3 TZH:TZM')` : `TO_TIMESTAMP('${UY}', 'YYYY-MM-DD HH24:MI:SS.FF3')`;
|
|
16414
|
+
}
|
|
16415
|
+
return `'${_.value.replace(/'/g, "''")}'`;
|
|
16416
|
+
}
|
|
16417
|
+
return isStringType(_.type) ? `'${_.value.replace(/'/g, "''")}'` : isBinaryType(_.type) ? `HEXTORAW('${_.value}')` : `CAST('${_.value.replace(/'/g, "''")}' AS ${_.type})`;
|
|
16418
|
+
};
|
|
16419
|
+
return WY.length > 1 ? `INSERT ALL\n${WY.map((_) => ` INTO ${GY} ${KY} VALUES (${_.map(qY).join(", ")})`).join("\n")}\nSELECT * FROM dual;` : `INSERT INTO ${GY} ${KY}\nVALUES (${WY[0].map(qY).join(", ")});`;
|
|
16420
|
+
});
|
|
16421
|
+
}
|
|
16422
|
+
static buildSchemaToTableNameSetMap(_) {
|
|
16423
|
+
let HY = /* @__PURE__ */ new Map();
|
|
16424
|
+
return forEach_default(_.tables, (P) => {
|
|
16425
|
+
let UY = _.schemas[P.schemaId], WY = HY.get(UY.name);
|
|
16426
|
+
if (!WY) {
|
|
16427
|
+
HY.set(UY.name, new Set([P.name]));
|
|
16428
|
+
return;
|
|
16429
|
+
}
|
|
16430
|
+
WY.add(P.name);
|
|
16431
|
+
}), HY;
|
|
16432
|
+
}
|
|
16433
|
+
static buildTableNameWithSchema(_, HY, P) {
|
|
16434
|
+
return `${shouldPrintSchema(HY, _) ? `${escapeObjectName(HY.name, "oracle")}.` : ""}${escapeObjectName(P.name, "oracle")}`;
|
|
16435
|
+
}
|
|
16436
|
+
static exportSchema(_) {
|
|
16437
|
+
return `CREATE USER ${escapeObjectName(_, "oracle")}\nNO AUTHENTICATION
|
|
16438
|
+
DEFAULT TABLESPACE system
|
|
16439
|
+
TEMPORARY TABLESPACE temp
|
|
16440
|
+
QUOTA UNLIMITED ON system;
|
|
16441
|
+
`;
|
|
16442
|
+
}
|
|
16443
|
+
static getFieldLines(_, HY) {
|
|
16444
|
+
return HY.tables[_].fieldIds.map((_) => {
|
|
16445
|
+
let P = HY.fields[_], UY = escapeObjectName(P.name, "oracle"), WY = UY;
|
|
16446
|
+
if (P.enumId) {
|
|
16447
|
+
let _ = HY.enums[P.enumId].valueIds.map((_) => `'${HY.enumValues[_].name}'`).join(", ");
|
|
16448
|
+
WY += ` nvarchar2(255) NOT NULL CHECK (${UY} IN (${_}))`;
|
|
16449
|
+
} else WY += ` ${P.type.type_name}`;
|
|
16450
|
+
let GY = { ...P };
|
|
16451
|
+
if (GY.increment && (WY += " GENERATED AS IDENTITY", GY.dbdefault = null, GY.not_null = !1), GY.dbdefault && (GY.dbdefault.type === "boolean" && (GY.dbdefault.value === null || typeof GY.dbdefault.value == "string" && GY.dbdefault.value.toLowerCase() === "null") || (GY.dbdefault.type === "string" ? WY += ` DEFAULT '${GY.dbdefault.value}'` : WY += ` DEFAULT ${GY.dbdefault.value}`)), GY.unique && (WY += " UNIQUE"), GY.pk && (WY += " PRIMARY KEY"), GY.not_null && (WY += " NOT NULL"), GY.checkIds && GY.checkIds.length > 0) if (GY.checkIds.length === 1) {
|
|
16452
|
+
let _ = HY.checks[GY.checkIds[0]];
|
|
16453
|
+
_.name && (WY += ` CONSTRAINT "${_.name}"`), WY += ` CHECK (${_.expression})`;
|
|
16454
|
+
} else {
|
|
16455
|
+
let _ = GY.checkIds.map((_) => `(${HY.checks[_].expression})`);
|
|
16456
|
+
WY += ` CHECK (${_.join(" AND ")})`;
|
|
16457
|
+
}
|
|
16458
|
+
return WY;
|
|
16459
|
+
});
|
|
16460
|
+
}
|
|
16461
|
+
static getCompositePKs(_, HY) {
|
|
16462
|
+
let P = HY.tables[_];
|
|
16463
|
+
return (P.indexIds ? P.indexIds.filter((_) => HY.indexes[_].pk) : []).map((_) => {
|
|
16464
|
+
let P = HY.indexes[_], UY = "PRIMARY KEY", WY = [];
|
|
16465
|
+
return P.columnIds.forEach((_) => {
|
|
16466
|
+
let P = HY.indexColumns[_], UY = "";
|
|
16467
|
+
UY = P.type === "expression" ? `(${P.value})` : `"${P.value}"`, WY.push(UY);
|
|
16468
|
+
}), UY += ` (${WY.join(", ")})`, UY;
|
|
16469
|
+
});
|
|
16470
|
+
}
|
|
16471
|
+
static getCheckLines(_, HY) {
|
|
16472
|
+
let P = HY.tables[_];
|
|
16473
|
+
return !P.checkIds || P.checkIds.length === 0 ? [] : P.checkIds.map((_) => {
|
|
16474
|
+
let P = HY.checks[_], UY = "";
|
|
16475
|
+
return P.name && (UY = `CONSTRAINT "${P.name}" `), UY += `CHECK (${P.expression})`, UY;
|
|
16476
|
+
});
|
|
16477
|
+
}
|
|
16478
|
+
static getTableContents(_, HY) {
|
|
16479
|
+
return _.map((_) => ({
|
|
16480
|
+
tableId: _,
|
|
16481
|
+
fieldContents: this.getFieldLines(_, HY),
|
|
16482
|
+
checkContents: this.getCheckLines(_, HY),
|
|
16483
|
+
compositePKs: this.getCompositePKs(_, HY)
|
|
16484
|
+
}));
|
|
16485
|
+
}
|
|
16486
|
+
static exportTables(_, HY) {
|
|
16487
|
+
return this.getTableContents(_, HY).map((_) => {
|
|
16488
|
+
let P = [
|
|
16489
|
+
..._.fieldContents,
|
|
16490
|
+
..._.checkContents,
|
|
16491
|
+
..._.compositePKs
|
|
16492
|
+
], UY = HY.tables[_.tableId], WY = HY.schemas[UY.schemaId];
|
|
16493
|
+
return `CREATE TABLE ${this.buildTableNameWithSchema(HY, WY, UY)} (\n${P.map((_) => ` ${_}`).join(",\n")}\n);\n`;
|
|
16494
|
+
});
|
|
16495
|
+
}
|
|
16496
|
+
static buildReferenceFieldNamesString(_, HY) {
|
|
16497
|
+
return `(${_.map((_) => `"${HY.fields[_].name}"`).join(", ")})`;
|
|
16498
|
+
}
|
|
16499
|
+
static buildTableManyToMany(_, HY, P) {
|
|
16500
|
+
let UY = `CREATE TABLE ${P} (\n`;
|
|
16501
|
+
_.forEach((_, HY) => {
|
|
16502
|
+
UY += ` "${HY}" ${_},\n`;
|
|
16503
|
+
}), HY.forEach((_, HY) => {
|
|
16504
|
+
UY += ` "${HY}" ${_},\n`;
|
|
16505
|
+
});
|
|
16506
|
+
let WY = [..._.keys()].join("`, `"), GY = [...HY.keys()].join("`, `");
|
|
16507
|
+
return UY += ` PRIMARY KEY ("${WY}", "${GY}")\n);\n`, UY;
|
|
16508
|
+
}
|
|
16509
|
+
static buildForeignKeyManyToMany(_, HY, P, UY) {
|
|
16510
|
+
return `ALTER TABLE ${_} ADD FOREIGN KEY ("${[...HY.keys()].join("`, `")}") REFERENCES ${P} ${UY} DEFERRABLE INITIALLY IMMEDIATE;\n`;
|
|
16511
|
+
}
|
|
16512
|
+
static exportReferencesAndNewTablesIfExists(_, HY, P) {
|
|
16513
|
+
let UY = {
|
|
16514
|
+
refs: [],
|
|
16515
|
+
tables: []
|
|
16516
|
+
};
|
|
16517
|
+
return _.forEach((_) => {
|
|
16518
|
+
let WY = HY.refs[_], GY = WY.endpointIds.findIndex((_) => HY.endpoints[_].relation === "1"), KY = GY === -1 ? 0 : GY, qY = WY.endpointIds[1 - KY], JY = HY.endpoints[qY], YY = HY.fields[JY.fieldIds[0]], XY = HY.tables[YY.tableId], ZY = HY.schemas[XY.schemaId], QY = this.buildReferenceFieldNamesString(JY.fieldIds, HY), $Y = WY.endpointIds[KY], eX = HY.endpoints[$Y], tX = HY.fields[eX.fieldIds[0]], nX = HY.tables[tX.tableId], rX = HY.schemas[nX.schemaId], iX = this.buildReferenceFieldNamesString(eX.fieldIds, HY);
|
|
16519
|
+
if (GY !== -1) {
|
|
16520
|
+
let _ = `ALTER TABLE ${this.buildTableNameWithSchema(HY, ZY, XY)} ADD`;
|
|
16521
|
+
WY.name && (_ += ` CONSTRAINT ${escapeObjectName(WY.name, "oracle")}`);
|
|
16522
|
+
let P = this.buildTableNameWithSchema(HY, rX, nX);
|
|
16523
|
+
_ += ` FOREIGN KEY ${QY} REFERENCES ${P} ${iX}`, WY.onDelete && (_ += ` ON DELETE ${WY.onDelete.toUpperCase()}`), _ += " DEFERRABLE INITIALLY IMMEDIATE;\n", UY.refs.push(_);
|
|
16524
|
+
return;
|
|
16525
|
+
}
|
|
16526
|
+
let aX = buildJunctionFields1(eX.fieldIds, HY), oX = buildJunctionFields2(JY.fieldIds, HY, aX), sX = buildUniqueTableName(rX, nX.name, XY.name, P), cX = P.get(rX);
|
|
16527
|
+
cX ? cX.add(sX) : P.set(rX, new Set([sX]));
|
|
16528
|
+
let lX = `${shouldPrintSchema(rX, HY) ? `"${rX.name}".` : ""}"${sX}"`;
|
|
16529
|
+
UY.tables.push(this.buildTableManyToMany(aX, oX, lX));
|
|
16530
|
+
let uX = this.buildTableNameWithSchema(HY, rX, nX);
|
|
16531
|
+
UY.refs.push(this.buildForeignKeyManyToMany(lX, aX, uX, iX));
|
|
16532
|
+
let dX = this.buildTableNameWithSchema(HY, ZY, XY);
|
|
16533
|
+
UY.refs.push(this.buildForeignKeyManyToMany(lX, oX, dX, QY));
|
|
16534
|
+
}), UY;
|
|
16535
|
+
}
|
|
16536
|
+
static exportReferenceGrants(_, HY) {
|
|
16537
|
+
if (Object.keys(_.schemas).length <= 1) return [];
|
|
16538
|
+
let P = [];
|
|
16539
|
+
return HY.forEach((HY) => {
|
|
16540
|
+
let UY = _.refs[HY], WY = UY.endpointIds.findIndex((HY) => _.endpoints[HY].relation === "1"), GY = WY === -1 ? 0 : WY, KY = UY.endpointIds[GY], qY = _.endpoints[KY], JY = _.fields[qY.fieldIds[0]], YY = _.tables[JY.tableId], XY = _.schemas[YY.schemaId], ZY = UY.endpointIds[1 - GY], QY = _.endpoints[ZY], $Y = _.fields[QY.fieldIds[0]], eX = _.tables[$Y.tableId], tX = _.schemas[eX.schemaId];
|
|
16541
|
+
if (XY.name === tX.name) {
|
|
16542
|
+
P.push("");
|
|
16543
|
+
return;
|
|
16544
|
+
}
|
|
16545
|
+
let nX = this.buildTableNameWithSchema(_, XY, YY);
|
|
16546
|
+
if (P.push(nX), WY !== -1) return;
|
|
16547
|
+
let rX = this.buildTableNameWithSchema(_, tX, eX);
|
|
16548
|
+
P.push(rX);
|
|
16549
|
+
}), P.filter((_, HY) => _ && P.indexOf(_) === HY).map((_) => `GRANT REFERENCES ON ${_} TO PUBLIC;\n`);
|
|
16550
|
+
}
|
|
16551
|
+
static exportIndexes(_, HY) {
|
|
16552
|
+
return _.filter((_) => !HY.indexes[_].pk).map((_) => {
|
|
16553
|
+
let P = HY.indexes[_], UY = HY.tables[P.tableId], WY = HY.schemas[UY.schemaId], GY = "CREATE";
|
|
16554
|
+
P.unique && (GY += " UNIQUE"), GY += " INDEX", P.name && (GY += ` ${escapeObjectName(P.name, "oracle")}`);
|
|
16555
|
+
let KY = this.buildTableNameWithSchema(HY, WY, UY);
|
|
16556
|
+
GY += ` ON ${KY}`;
|
|
16557
|
+
let qY = [];
|
|
16558
|
+
P.columnIds.forEach((_) => {
|
|
16559
|
+
let P = HY.indexColumns[_], UY = "";
|
|
16560
|
+
UY = P.type === "expression" ? `${P.value}` : `"${P.value}"`, qY.push(UY);
|
|
16561
|
+
});
|
|
16562
|
+
let JY = qY.join(", ");
|
|
16563
|
+
return GY += ` (${JY});\n`, GY;
|
|
16564
|
+
});
|
|
16565
|
+
}
|
|
16566
|
+
static exportComments(_, HY) {
|
|
16567
|
+
return _.map((_) => {
|
|
16568
|
+
let P = "COMMENT ON", UY = HY.tables[_.tableId], WY = HY.schemas[UY.schemaId], GY = this.buildTableNameWithSchema(HY, WY, UY);
|
|
16569
|
+
switch (_.type) {
|
|
16570
|
+
case "table":
|
|
16571
|
+
P += ` TABLE ${GY} IS '${UY.note.replace(/'/g, "''")}'`;
|
|
16572
|
+
break;
|
|
16573
|
+
case "column": {
|
|
16574
|
+
let UY = HY.fields[_.fieldId];
|
|
16575
|
+
P += ` COLUMN ${GY}.${escapeObjectName(UY.name, "oracle")} IS '${UY.note.replace(/'/g, "''")}'`;
|
|
16576
|
+
break;
|
|
16577
|
+
}
|
|
16578
|
+
default: break;
|
|
16579
|
+
}
|
|
16580
|
+
return P += ";\n", P;
|
|
16581
|
+
});
|
|
16582
|
+
}
|
|
16583
|
+
static export(_) {
|
|
16584
|
+
let HY = _.database[1], P = this.buildSchemaToTableNameSetMap(_), UY = HY.schemaIds.reduce((HY, UY) => {
|
|
16585
|
+
let WY = _.schemas[UY], { tableIds: GY, refIds: KY } = WY;
|
|
16586
|
+
shouldPrintSchema(WY, _) && HY.schemas.push(this.exportSchema(WY.name)), isEmpty_default(GY) || HY.tables.push(...this.exportTables(GY, _));
|
|
16587
|
+
let qY = flatten_default(GY.map((HY) => _.tables[HY].indexIds));
|
|
16588
|
+
isEmpty_default(qY) || HY.indexes.push(...this.exportIndexes(qY, _));
|
|
16589
|
+
let JY = flatten_default(GY.map((HY) => {
|
|
16590
|
+
let { fieldIds: P, note: UY } = _.tables[HY], WY = P.filter((HY) => _.fields[HY].note).map((_) => ({
|
|
16591
|
+
type: "column",
|
|
16592
|
+
fieldId: _,
|
|
16593
|
+
tableId: HY
|
|
16594
|
+
}));
|
|
16595
|
+
return UY ? [{
|
|
16596
|
+
type: "table",
|
|
16597
|
+
tableId: HY
|
|
16598
|
+
}].concat(WY) : WY;
|
|
16599
|
+
}));
|
|
16600
|
+
if (isEmpty_default(JY) || HY.comments.push(...this.exportComments(JY, _)), !isEmpty_default(KY)) {
|
|
16601
|
+
let { refs: UY, tables: WY } = this.exportReferencesAndNewTablesIfExists(KY, _, P);
|
|
16602
|
+
HY.tables.push(...WY), HY.refs.push(...UY), HY.referenceGrants.push(...this.exportReferenceGrants(_, KY));
|
|
16603
|
+
}
|
|
16604
|
+
return HY;
|
|
16605
|
+
}, {
|
|
16606
|
+
schemas: [],
|
|
16607
|
+
tables: [],
|
|
16608
|
+
indexes: [],
|
|
16609
|
+
comments: [],
|
|
16610
|
+
referenceGrants: [],
|
|
16611
|
+
refs: []
|
|
16612
|
+
}), WY = this.exportRecords(_), GY = isEmpty_default(WY) ? [] : [
|
|
16613
|
+
"-- Disable constraint checking for INSERT",
|
|
16614
|
+
"SET CONSTRAINTS ALL DEFERRED;",
|
|
16615
|
+
"",
|
|
16616
|
+
...WY,
|
|
16617
|
+
"",
|
|
16618
|
+
"SET CONSTRAINTS ALL IMMEDIATE;",
|
|
16619
|
+
"COMMIT;"
|
|
16620
|
+
];
|
|
16621
|
+
return concat_default(UY.schemas, UY.tables, UY.indexes, UY.comments, UY.referenceGrants, UY.refs, GY).join("\n");
|
|
16622
|
+
}
|
|
16648
16623
|
}, t = { 763: () => {} }, e = {};
|
|
16649
16624
|
function n(_) {
|
|
16650
16625
|
var HY = e[_];
|
|
@@ -1820917,6 +1820892,54 @@ var init_actions = __esmMin((() => {
|
|
|
1820917
1820892
|
throw CompilerError.create(_);
|
|
1820918
1820893
|
}
|
|
1820919
1820894
|
}
|
|
1820895
|
+
};
|
|
1820896
|
+
function _export(_, HY, P = {
|
|
1820897
|
+
isNormalized: !0,
|
|
1820898
|
+
includeRecords: !0
|
|
1820899
|
+
}) {
|
|
1820900
|
+
let UY = normalizeExportOptions(P), WY = new Parser_default().parse(_, "dbmlv2");
|
|
1820901
|
+
return ModelExporter_default.export(WY.normalize(), HY, UY);
|
|
1820902
|
+
}
|
|
1820903
|
+
function normalizeExportOptions(_ = {}) {
|
|
1820904
|
+
if (typeof _ == "boolean") return {
|
|
1820905
|
+
isNormalized: _,
|
|
1820906
|
+
includeRecords: !0
|
|
1820907
|
+
};
|
|
1820908
|
+
let { isNormalized: HY = !0, includeRecords: P = !0 } = _;
|
|
1820909
|
+
return {
|
|
1820910
|
+
isNormalized: HY,
|
|
1820911
|
+
includeRecords: P
|
|
1820912
|
+
};
|
|
1820913
|
+
}
|
|
1820914
|
+
var export_default = { export: _export }, ModelExporter_default = class {
|
|
1820915
|
+
static export(_, HY, P = {
|
|
1820916
|
+
isNormalized: !0,
|
|
1820917
|
+
includeRecords: !0
|
|
1820918
|
+
}) {
|
|
1820919
|
+
let { isNormalized: UY, includeRecords: WY } = normalizeExportOptions(P), GY = _ instanceof database_default ? _.normalize() : _, KY = "";
|
|
1820920
|
+
switch (HY) {
|
|
1820921
|
+
case "dbml":
|
|
1820922
|
+
KY = DbmlExporter_default.export(GY, { includeRecords: WY });
|
|
1820923
|
+
break;
|
|
1820924
|
+
case "mysql":
|
|
1820925
|
+
KY = MysqlExporter_default.export(GY);
|
|
1820926
|
+
break;
|
|
1820927
|
+
case "postgres":
|
|
1820928
|
+
KY = PostgresExporter_default.export(GY);
|
|
1820929
|
+
break;
|
|
1820930
|
+
case "json":
|
|
1820931
|
+
KY = JsonExporter_default.export(_, { isNormalized: UY });
|
|
1820932
|
+
break;
|
|
1820933
|
+
case "mssql":
|
|
1820934
|
+
KY = SqlServerExporter_default.export(GY);
|
|
1820935
|
+
break;
|
|
1820936
|
+
case "oracle":
|
|
1820937
|
+
KY = OracleExporter_default.export(GY);
|
|
1820938
|
+
break;
|
|
1820939
|
+
default: break;
|
|
1820940
|
+
}
|
|
1820941
|
+
return KY;
|
|
1820942
|
+
}
|
|
1820920
1820943
|
}, parseJSONToDatabase = (_) => new database_default(_), createRefs = (_) => _.map((_) => {
|
|
1820921
1820944
|
let { name: HY, endpoints: P, onDelete: UY, onUpdate: WY } = _;
|
|
1820922
1820945
|
return new Ref({
|
|
@@ -1820981,9 +1821004,9 @@ var init_actions = __esmMin((() => {
|
|
|
1820981
1821004
|
throw Error(_);
|
|
1820982
1821005
|
}
|
|
1820983
1821006
|
};
|
|
1820984
|
-
function _import(_, HY) {
|
|
1820985
|
-
let P = new Parser_default().parse(_, HY);
|
|
1820986
|
-
return ModelExporter_default.export(
|
|
1821007
|
+
function _import(_, HY, P = { includeRecords: !0 }) {
|
|
1821008
|
+
let { includeRecords: UY = !0 } = P, WY = new Parser_default().parse(_, HY);
|
|
1821009
|
+
return ModelExporter_default.export(WY.normalize(), "dbml", { includeRecords: UY });
|
|
1820987
1821010
|
}
|
|
1820988
1821011
|
function generateDbml(_) {
|
|
1820989
1821012
|
let HY = generateDatabase(_);
|
|
@@ -1820993,14 +1821016,9 @@ var import_default = {
|
|
|
1820993
1821016
|
import: _import,
|
|
1820994
1821017
|
generateDbml
|
|
1820995
1821018
|
};
|
|
1820996
|
-
function _export(_, HY, P = {}) {
|
|
1820997
|
-
let UY = new Parser_default().parse(_, "dbmlv2");
|
|
1820998
|
-
return ModelExporter_default.export(UY.normalize(), HY, P);
|
|
1820999
|
-
}
|
|
1821000
|
-
var export_default = { export: _export };
|
|
1821001
1821019
|
function renameTable(_, HY, P) {
|
|
1821002
1821020
|
let UY = new Compiler();
|
|
1821003
1821021
|
return UY.setSource(P), UY.renameTable(_, HY);
|
|
1821004
1821022
|
}
|
|
1821005
|
-
const VERSION = "6.4.0
|
|
1821023
|
+
const VERSION = "6.4.0";
|
|
1821006
1821024
|
export { CompilerError, ModelExporter_default as ModelExporter, Parser_default as Parser, VERSION, addDoubleQuoteIfNeeded, export_default as exporter, formatRecordValue, import_default as importer, isBinaryType, isBooleanType, isDateTimeType, isFloatType, isIntegerType, isNumericType, isSerialType, isStringType, renameTable, tryExtractBoolean, tryExtractDateTime, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString };
|