@dbml/core 2.4.1 → 2.4.2
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/export/MysqlExporter.js +87 -14
- package/lib/export/PostgresExporter.js +88 -14
- package/lib/export/SqlServerExporter.js +88 -14
- package/lib/export/utils.js +40 -0
- package/lib/parse/dbml/parser.pegjs +13 -5
- package/lib/parse/dbmlParser.js +349 -296
- package/package.json +2 -2
|
@@ -154,16 +154,64 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
154
154
|
}).join(', ');
|
|
155
155
|
return "(".concat(fieldNames, ")");
|
|
156
156
|
}
|
|
157
|
+
}, {
|
|
158
|
+
key: "buildTableManyToMany",
|
|
159
|
+
value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName) {
|
|
160
|
+
var line = "CREATE TABLE `".concat(tableName, "` (\n");
|
|
161
|
+
|
|
162
|
+
var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('`, `');
|
|
163
|
+
|
|
164
|
+
var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('`, `');
|
|
165
|
+
|
|
166
|
+
firstTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
167
|
+
line += " `".concat(fieldName, "` ").concat(fieldType, " NOT NULL,\n");
|
|
168
|
+
});
|
|
169
|
+
secondTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
170
|
+
line += " `".concat(fieldName, "` ").concat(fieldType, " NOT NULL,\n");
|
|
171
|
+
});
|
|
172
|
+
line += " PRIMARY KEY (`".concat(key1s, "`, `").concat(key2s, "`)\n");
|
|
173
|
+
line += ');\n\n';
|
|
174
|
+
return line;
|
|
175
|
+
}
|
|
176
|
+
}, {
|
|
177
|
+
key: "buildForeignKeyManyToMany",
|
|
178
|
+
value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, schema, model) {
|
|
179
|
+
var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('`, `');
|
|
180
|
+
|
|
181
|
+
var line = "ALTER TABLE `".concat(refEndpointTableName, "` ADD FOREIGN KEY (`").concat(refEndpointFields, "`) REFERENCES ").concat((0, _utils.shouldPrintSchema)(schema, model) ? "`".concat(schema.name, "`.") : '', "`").concat(foreignEndpointTableName, "` ").concat(foreignEndpointFields, ";\n\n");
|
|
182
|
+
return line;
|
|
183
|
+
}
|
|
184
|
+
}, {
|
|
185
|
+
key: "buildIndexManytoMany",
|
|
186
|
+
value: function buildIndexManytoMany(fieldsMap, newTableName, tableRefName, usedIndexNames) {
|
|
187
|
+
var newIndexName = "".concat(newTableName, "_").concat(tableRefName);
|
|
188
|
+
var count = 1;
|
|
189
|
+
|
|
190
|
+
while (usedIndexNames.has(newIndexName)) {
|
|
191
|
+
newIndexName = "".concat(newTableName, "_").concat(tableRefName, "(").concat(count, ")");
|
|
192
|
+
count += 1;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
usedIndexNames.add(newIndexName);
|
|
196
|
+
|
|
197
|
+
var indexFields = _toConsumableArray(fieldsMap.keys()).join('`, `');
|
|
198
|
+
|
|
199
|
+
var line = "CREATE INDEX `idx_".concat(newIndexName, "` ON `").concat(newTableName, "` (");
|
|
200
|
+
line += "`".concat(indexFields, "`);\n\n");
|
|
201
|
+
return line;
|
|
202
|
+
}
|
|
157
203
|
}, {
|
|
158
204
|
key: "exportRefs",
|
|
159
|
-
value: function exportRefs(refIds, model) {
|
|
205
|
+
value: function exportRefs(refIds, model, usedTableNames, usedIndexNames) {
|
|
160
206
|
var _this = this;
|
|
161
207
|
|
|
162
208
|
var strArr = refIds.map(function (refId) {
|
|
209
|
+
var line = '';
|
|
163
210
|
var ref = model.refs[refId];
|
|
164
|
-
var
|
|
211
|
+
var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
|
|
165
212
|
return model.endpoints[endpointId].relation === '1';
|
|
166
213
|
});
|
|
214
|
+
var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
|
|
167
215
|
var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
|
|
168
216
|
var refEndpointId = ref.endpointIds[refEndpointIndex];
|
|
169
217
|
var foreignEndpoint = model.endpoints[foreignEndpointId];
|
|
@@ -180,23 +228,42 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
180
228
|
|
|
181
229
|
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mysql');
|
|
182
230
|
|
|
183
|
-
|
|
231
|
+
if (refOneIndex === -1) {
|
|
232
|
+
var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
|
|
233
|
+
var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
|
|
234
|
+
var newTableName = (0, _utils.buildNewTableName)(refEndpointTable.name, foreignEndpointTable.name, usedTableNames);
|
|
235
|
+
line += _this.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, newTableName);
|
|
184
236
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
237
|
+
if (firstTableFieldsMap.size > 1) {
|
|
238
|
+
line += _this.buildIndexManytoMany(firstTableFieldsMap, newTableName, refEndpointTable.name, usedIndexNames);
|
|
239
|
+
}
|
|
188
240
|
|
|
189
|
-
|
|
241
|
+
if (secondTableFieldsMap.size > 1) {
|
|
242
|
+
line += _this.buildIndexManytoMany(secondTableFieldsMap, newTableName, foreignEndpointTable.name, usedIndexNames);
|
|
243
|
+
}
|
|
190
244
|
|
|
191
|
-
|
|
192
|
-
line +=
|
|
193
|
-
}
|
|
245
|
+
line += _this.buildForeignKeyManyToMany(firstTableFieldsMap, refEndpointFieldName, newTableName, refEndpointTable.name, refEndpointSchema, model);
|
|
246
|
+
line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, foreignEndpointSchema, model);
|
|
247
|
+
} else {
|
|
248
|
+
line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "`".concat(foreignEndpointSchema.name, "`.") : '', "`").concat(foreignEndpointTable.name, "` ADD ");
|
|
194
249
|
|
|
195
|
-
|
|
196
|
-
|
|
250
|
+
if (ref.name) {
|
|
251
|
+
line += "CONSTRAINT `".concat(ref.name, "` ");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(refEndpointTable.name, "` ").concat(refEndpointFieldName);
|
|
255
|
+
|
|
256
|
+
if (ref.onDelete) {
|
|
257
|
+
line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (ref.onUpdate) {
|
|
261
|
+
line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
line += ';\n';
|
|
197
265
|
}
|
|
198
266
|
|
|
199
|
-
line += ';\n';
|
|
200
267
|
return line;
|
|
201
268
|
});
|
|
202
269
|
return strArr;
|
|
@@ -263,6 +330,12 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
263
330
|
key: "export",
|
|
264
331
|
value: function _export(model) {
|
|
265
332
|
var database = model.database['1'];
|
|
333
|
+
var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
|
|
334
|
+
return table.name;
|
|
335
|
+
}));
|
|
336
|
+
var usedIndexNames = new Set(Object.values(model.indexes).map(function (index) {
|
|
337
|
+
return index.name;
|
|
338
|
+
}));
|
|
266
339
|
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
|
|
267
340
|
var schema = model.schemas[schemaId];
|
|
268
341
|
var tableIds = schema.tableIds,
|
|
@@ -305,7 +378,7 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
305
378
|
if (!_lodash["default"].isEmpty(refIds)) {
|
|
306
379
|
var _prevStatements$refs;
|
|
307
380
|
|
|
308
|
-
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(MySQLExporter.exportRefs(refIds, model)));
|
|
381
|
+
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(MySQLExporter.exportRefs(refIds, model, usedTableNames, usedIndexNames)));
|
|
309
382
|
}
|
|
310
383
|
|
|
311
384
|
return prevStatements;
|
|
@@ -167,16 +167,64 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
167
167
|
}).join(', ');
|
|
168
168
|
return "(".concat(fieldNames, ")");
|
|
169
169
|
}
|
|
170
|
+
}, {
|
|
171
|
+
key: "buildTableManyToMany",
|
|
172
|
+
value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName) {
|
|
173
|
+
var line = "CREATE TABLE \"".concat(tableName, "\" (\n");
|
|
174
|
+
|
|
175
|
+
var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('", "');
|
|
176
|
+
|
|
177
|
+
var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('", "');
|
|
178
|
+
|
|
179
|
+
firstTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
180
|
+
line += " \"".concat(fieldName, "\" ").concat(fieldType, " NOT NULL,\n");
|
|
181
|
+
});
|
|
182
|
+
secondTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
183
|
+
line += " \"".concat(fieldName, "\" ").concat(fieldType, " NOT NULL,\n");
|
|
184
|
+
});
|
|
185
|
+
line += " PRIMARY KEY (\"".concat(key1s, "\", \"").concat(key2s, "\")\n");
|
|
186
|
+
line += ');\n\n';
|
|
187
|
+
return line;
|
|
188
|
+
}
|
|
189
|
+
}, {
|
|
190
|
+
key: "buildForeignKeyManyToMany",
|
|
191
|
+
value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, schema, model) {
|
|
192
|
+
var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('", "');
|
|
193
|
+
|
|
194
|
+
var line = "ALTER TABLE \"".concat(refEndpointTableName, "\" ADD FOREIGN KEY (\"").concat(refEndpointFields, "\") REFERENCES ").concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(foreignEndpointTableName, "\" ").concat(foreignEndpointFields, ";\n\n");
|
|
195
|
+
return line;
|
|
196
|
+
}
|
|
197
|
+
}, {
|
|
198
|
+
key: "buildIndexManytoMany",
|
|
199
|
+
value: function buildIndexManytoMany(fieldsMap, newTableName, tableRefName, usedIndexNames) {
|
|
200
|
+
var newIndexName = "".concat(newTableName, "_").concat(tableRefName);
|
|
201
|
+
var count = 1;
|
|
202
|
+
|
|
203
|
+
while (usedIndexNames.has(newIndexName)) {
|
|
204
|
+
newIndexName = "".concat(newTableName, "_").concat(tableRefName, "(").concat(count, ")");
|
|
205
|
+
count += 1;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
usedIndexNames.add(newIndexName);
|
|
209
|
+
|
|
210
|
+
var indexFields = _toConsumableArray(fieldsMap.keys()).join('", "');
|
|
211
|
+
|
|
212
|
+
var line = "CREATE INDEX \"idx_".concat(newIndexName, "\" ON \"").concat(newTableName, "\" (");
|
|
213
|
+
line += "\"".concat(indexFields, "\");\n\n");
|
|
214
|
+
return line;
|
|
215
|
+
}
|
|
170
216
|
}, {
|
|
171
217
|
key: "exportRefs",
|
|
172
|
-
value: function exportRefs(refIds, model) {
|
|
218
|
+
value: function exportRefs(refIds, model, usedTableNames, usedIndexNames) {
|
|
173
219
|
var _this = this;
|
|
174
220
|
|
|
175
221
|
var strArr = refIds.map(function (refId) {
|
|
222
|
+
var line = '';
|
|
176
223
|
var ref = model.refs[refId];
|
|
177
|
-
var
|
|
224
|
+
var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
|
|
178
225
|
return model.endpoints[endpointId].relation === '1';
|
|
179
226
|
});
|
|
227
|
+
var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
|
|
180
228
|
var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
|
|
181
229
|
var refEndpointId = ref.endpointIds[refEndpointIndex];
|
|
182
230
|
var foreignEndpoint = model.endpoints[foreignEndpointId];
|
|
@@ -193,23 +241,43 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
193
241
|
|
|
194
242
|
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'postgres');
|
|
195
243
|
|
|
196
|
-
|
|
244
|
+
if (refOneIndex === -1) {
|
|
245
|
+
// many to many relationship
|
|
246
|
+
var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
|
|
247
|
+
var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
|
|
248
|
+
var newTableName = (0, _utils.buildNewTableName)(refEndpointTable.name, foreignEndpointTable.name, usedTableNames);
|
|
249
|
+
line += _this.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, newTableName);
|
|
197
250
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
251
|
+
if (firstTableFieldsMap.size > 1) {
|
|
252
|
+
line += _this.buildIndexManytoMany(firstTableFieldsMap, newTableName, refEndpointTable.name, usedIndexNames);
|
|
253
|
+
}
|
|
201
254
|
|
|
202
|
-
|
|
255
|
+
if (secondTableFieldsMap.size > 1) {
|
|
256
|
+
line += _this.buildIndexManytoMany(secondTableFieldsMap, newTableName, foreignEndpointTable.name, usedIndexNames);
|
|
257
|
+
}
|
|
203
258
|
|
|
204
|
-
|
|
205
|
-
line +=
|
|
206
|
-
}
|
|
259
|
+
line += _this.buildForeignKeyManyToMany(firstTableFieldsMap, refEndpointFieldName, newTableName, refEndpointTable.name, refEndpointSchema, model);
|
|
260
|
+
line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, foreignEndpointSchema, model);
|
|
261
|
+
} else {
|
|
262
|
+
line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTable.name, "\" ADD ");
|
|
207
263
|
|
|
208
|
-
|
|
209
|
-
|
|
264
|
+
if (ref.name) {
|
|
265
|
+
line += "CONSTRAINT \"".concat(ref.name, "\" ");
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\" ").concat(refEndpointFieldName);
|
|
269
|
+
|
|
270
|
+
if (ref.onDelete) {
|
|
271
|
+
line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (ref.onUpdate) {
|
|
275
|
+
line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
line += ';\n';
|
|
210
279
|
}
|
|
211
280
|
|
|
212
|
-
line += ';\n';
|
|
213
281
|
return line;
|
|
214
282
|
});
|
|
215
283
|
return strArr;
|
|
@@ -297,6 +365,12 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
297
365
|
key: "export",
|
|
298
366
|
value: function _export(model) {
|
|
299
367
|
var database = model.database['1'];
|
|
368
|
+
var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
|
|
369
|
+
return table.name;
|
|
370
|
+
}));
|
|
371
|
+
var usedIndexNames = new Set(Object.values(model.indexes).map(function (index) {
|
|
372
|
+
return index.name;
|
|
373
|
+
}));
|
|
300
374
|
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
|
|
301
375
|
var schema = model.schemas[schemaId];
|
|
302
376
|
var tableIds = schema.tableIds,
|
|
@@ -357,7 +431,7 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
357
431
|
if (!_lodash["default"].isEmpty(refIds)) {
|
|
358
432
|
var _prevStatements$refs;
|
|
359
433
|
|
|
360
|
-
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model)));
|
|
434
|
+
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model, usedTableNames, usedIndexNames)));
|
|
361
435
|
}
|
|
362
436
|
|
|
363
437
|
return prevStatements;
|
|
@@ -143,6 +143,52 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
143
143
|
});
|
|
144
144
|
return tableStrs;
|
|
145
145
|
}
|
|
146
|
+
}, {
|
|
147
|
+
key: "buildTableManyToMany",
|
|
148
|
+
value: function buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, tableName) {
|
|
149
|
+
var line = "CREATE TABLE [".concat(tableName, "] (\n");
|
|
150
|
+
|
|
151
|
+
var key1s = _toConsumableArray(firstTableFieldsMap.keys()).join('], [');
|
|
152
|
+
|
|
153
|
+
var key2s = _toConsumableArray(secondTableFieldsMap.keys()).join('], [');
|
|
154
|
+
|
|
155
|
+
firstTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
156
|
+
line += " [".concat(fieldName, "] ").concat(fieldType, " NOT NULL,\n");
|
|
157
|
+
});
|
|
158
|
+
secondTableFieldsMap.forEach(function (fieldType, fieldName) {
|
|
159
|
+
line += " [".concat(fieldName, "] ").concat(fieldType, " NOT NULL,\n");
|
|
160
|
+
});
|
|
161
|
+
line += " PRIMARY KEY ([".concat(key1s, "], [").concat(key2s, "])\n");
|
|
162
|
+
line += ');\nGO\n\n';
|
|
163
|
+
return line;
|
|
164
|
+
}
|
|
165
|
+
}, {
|
|
166
|
+
key: "buildForeignKeyManyToMany",
|
|
167
|
+
value: function buildForeignKeyManyToMany(fieldsMap, foreignEndpointFields, refEndpointTableName, foreignEndpointTableName, schema, model) {
|
|
168
|
+
var refEndpointFields = _toConsumableArray(fieldsMap.keys()).join('], [');
|
|
169
|
+
|
|
170
|
+
var line = "ALTER TABLE [".concat(refEndpointTableName, "] ADD FOREIGN KEY ([").concat(refEndpointFields, "]) REFERENCES ").concat((0, _utils.shouldPrintSchema)(schema, model) ? "[".concat(schema.name, "].") : '', "[").concat(foreignEndpointTableName, "] ").concat(foreignEndpointFields, ";\nGO\n\n");
|
|
171
|
+
return line;
|
|
172
|
+
}
|
|
173
|
+
}, {
|
|
174
|
+
key: "buildIndexManytoMany",
|
|
175
|
+
value: function buildIndexManytoMany(fieldsMap, newTableName, tableRefName, usedIndexNames) {
|
|
176
|
+
var newIndexName = "".concat(newTableName, "_").concat(tableRefName);
|
|
177
|
+
var count = 1;
|
|
178
|
+
|
|
179
|
+
while (usedIndexNames.has(newIndexName)) {
|
|
180
|
+
newIndexName = "".concat(newTableName, "_").concat(tableRefName, "(").concat(count, ")");
|
|
181
|
+
count += 1;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
usedIndexNames.add(newIndexName);
|
|
185
|
+
|
|
186
|
+
var indexFields = _toConsumableArray(fieldsMap.keys()).join('", "');
|
|
187
|
+
|
|
188
|
+
var line = "CREATE INDEX [idx_".concat(newIndexName, "] ON [").concat(newTableName, "] (");
|
|
189
|
+
line += "\"".concat(indexFields, "\");\nGO\n\n");
|
|
190
|
+
return line;
|
|
191
|
+
}
|
|
146
192
|
}, {
|
|
147
193
|
key: "buildFieldName",
|
|
148
194
|
value: function buildFieldName(fieldIds, model) {
|
|
@@ -153,14 +199,16 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
153
199
|
}
|
|
154
200
|
}, {
|
|
155
201
|
key: "exportRefs",
|
|
156
|
-
value: function exportRefs(refIds, model) {
|
|
202
|
+
value: function exportRefs(refIds, model, usedTableNames, usedIndexNames) {
|
|
157
203
|
var _this = this;
|
|
158
204
|
|
|
159
205
|
var strArr = refIds.map(function (refId) {
|
|
206
|
+
var line = '';
|
|
160
207
|
var ref = model.refs[refId];
|
|
161
|
-
var
|
|
208
|
+
var refOneIndex = ref.endpointIds.findIndex(function (endpointId) {
|
|
162
209
|
return model.endpoints[endpointId].relation === '1';
|
|
163
210
|
});
|
|
211
|
+
var refEndpointIndex = refOneIndex === -1 ? 0 : refOneIndex;
|
|
164
212
|
var foreignEndpointId = ref.endpointIds[1 - refEndpointIndex];
|
|
165
213
|
var refEndpointId = ref.endpointIds[refEndpointIndex];
|
|
166
214
|
var foreignEndpoint = model.endpoints[foreignEndpointId];
|
|
@@ -177,23 +225,43 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
177
225
|
|
|
178
226
|
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mssql');
|
|
179
227
|
|
|
180
|
-
|
|
228
|
+
if (refOneIndex === -1) {
|
|
229
|
+
// many to many relationship
|
|
230
|
+
var firstTableFieldsMap = (0, _utils.buildJunctionFields1)(refEndpoint.fieldIds, model);
|
|
231
|
+
var secondTableFieldsMap = (0, _utils.buildJunctionFields2)(foreignEndpoint.fieldIds, model, firstTableFieldsMap);
|
|
232
|
+
var newTableName = (0, _utils.buildNewTableName)(refEndpointTable.name, foreignEndpointTable.name, usedTableNames);
|
|
233
|
+
line += _this.buildTableManyToMany(firstTableFieldsMap, secondTableFieldsMap, newTableName);
|
|
181
234
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
235
|
+
if (firstTableFieldsMap.size > 1) {
|
|
236
|
+
line += _this.buildIndexManytoMany(firstTableFieldsMap, newTableName, refEndpointTable.name, usedIndexNames);
|
|
237
|
+
}
|
|
185
238
|
|
|
186
|
-
|
|
239
|
+
if (secondTableFieldsMap.size > 1) {
|
|
240
|
+
line += _this.buildIndexManytoMany(secondTableFieldsMap, newTableName, foreignEndpointTable.name, usedIndexNames);
|
|
241
|
+
}
|
|
187
242
|
|
|
188
|
-
|
|
189
|
-
line +=
|
|
190
|
-
}
|
|
243
|
+
line += _this.buildForeignKeyManyToMany(firstTableFieldsMap, refEndpointFieldName, newTableName, refEndpointTable.name, refEndpointSchema, model);
|
|
244
|
+
line += _this.buildForeignKeyManyToMany(secondTableFieldsMap, foreignEndpointFieldName, newTableName, foreignEndpointTable.name, foreignEndpointSchema, model);
|
|
245
|
+
} else {
|
|
246
|
+
line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "[".concat(foreignEndpointSchema.name, "].") : '', "[").concat(foreignEndpointTable.name, "] ADD ");
|
|
191
247
|
|
|
192
|
-
|
|
193
|
-
|
|
248
|
+
if (ref.name) {
|
|
249
|
+
line += "CONSTRAINT [".concat(ref.name, "] ");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTable.name, "] ").concat(refEndpointFieldName);
|
|
253
|
+
|
|
254
|
+
if (ref.onDelete) {
|
|
255
|
+
line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (ref.onUpdate) {
|
|
259
|
+
line += " ON UPDATE ".concat(ref.onUpdate.toUpperCase());
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
line += '\nGO\n';
|
|
194
263
|
}
|
|
195
264
|
|
|
196
|
-
line += '\nGO\n';
|
|
197
265
|
return line;
|
|
198
266
|
});
|
|
199
267
|
return strArr;
|
|
@@ -278,6 +346,12 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
278
346
|
key: "export",
|
|
279
347
|
value: function _export(model) {
|
|
280
348
|
var database = model.database['1'];
|
|
349
|
+
var usedTableNames = new Set(Object.values(model.tables).map(function (table) {
|
|
350
|
+
return table.name;
|
|
351
|
+
}));
|
|
352
|
+
var usedIndexNames = new Set(Object.values(model.indexes).map(function (index) {
|
|
353
|
+
return index.name;
|
|
354
|
+
}));
|
|
281
355
|
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) {
|
|
282
356
|
var schema = model.schemas[schemaId];
|
|
283
357
|
var tableIds = schema.tableIds,
|
|
@@ -331,7 +405,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
331
405
|
if (!_lodash["default"].isEmpty(refIds)) {
|
|
332
406
|
var _prevStatements$refs;
|
|
333
407
|
|
|
334
|
-
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(SqlServerExporter.exportRefs(refIds, model)));
|
|
408
|
+
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(SqlServerExporter.exportRefs(refIds, model, usedTableNames, usedIndexNames)));
|
|
335
409
|
}
|
|
336
410
|
|
|
337
411
|
return prevStatements;
|
package/lib/export/utils.js
CHANGED
|
@@ -5,6 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.hasWhiteSpace = hasWhiteSpace;
|
|
7
7
|
exports.shouldPrintSchema = shouldPrintSchema;
|
|
8
|
+
exports.buildJunctionFields1 = buildJunctionFields1;
|
|
9
|
+
exports.buildJunctionFields2 = buildJunctionFields2;
|
|
10
|
+
exports.buildNewTableName = buildNewTableName;
|
|
8
11
|
|
|
9
12
|
var _config = require("../model_structure/config");
|
|
10
13
|
|
|
@@ -14,4 +17,41 @@ function hasWhiteSpace(s) {
|
|
|
14
17
|
|
|
15
18
|
function shouldPrintSchema(schema, model) {
|
|
16
19
|
return schema.name !== _config.DEFAULT_SCHEMA_NAME || schema.name === _config.DEFAULT_SCHEMA_NAME && model.database['1'].hasDefaultSchema;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function buildJunctionFields1(fieldIds, model) {
|
|
23
|
+
var fieldsMap = new Map();
|
|
24
|
+
fieldIds.map(function (fieldId) {
|
|
25
|
+
return fieldsMap.set("".concat(model.tables[model.fields[fieldId].tableId].name, "_").concat(model.fields[fieldId].name), model.fields[fieldId].type.type_name);
|
|
26
|
+
});
|
|
27
|
+
return fieldsMap;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function buildJunctionFields2(fieldIds, model, firstTableFieldsMap) {
|
|
31
|
+
var fieldsMap = new Map();
|
|
32
|
+
fieldIds.map(function (fieldId) {
|
|
33
|
+
var fieldName = "".concat(model.tables[model.fields[fieldId].tableId].name, "_").concat(model.fields[fieldId].name);
|
|
34
|
+
var count = 1;
|
|
35
|
+
|
|
36
|
+
while (firstTableFieldsMap.has(fieldName)) {
|
|
37
|
+
fieldName = "".concat(model.tables[model.fields[fieldId].tableId].name, "_").concat(model.fields[fieldId].name, "(").concat(count, ")");
|
|
38
|
+
count += 1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fieldsMap.set(fieldName, model.fields[fieldId].type.type_name);
|
|
42
|
+
});
|
|
43
|
+
return fieldsMap;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function buildNewTableName(firstTable, secondTable, usedTableNames) {
|
|
47
|
+
var newTableName = "".concat(firstTable, "_").concat(secondTable);
|
|
48
|
+
var count = 1;
|
|
49
|
+
|
|
50
|
+
while (usedTableNames.has(newTableName)) {
|
|
51
|
+
newTableName = "".concat(firstTable, "_").concat(secondTable, "(").concat(count, ")");
|
|
52
|
+
count += 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
usedTableNames.add(newTableName);
|
|
56
|
+
return newTableName;
|
|
17
57
|
}
|
|
@@ -9,6 +9,12 @@
|
|
|
9
9
|
project: {},
|
|
10
10
|
};
|
|
11
11
|
let projectCnt = 0;
|
|
12
|
+
function getRelations(operator) {
|
|
13
|
+
if(operator === '<>') return ['*','*'];
|
|
14
|
+
if(operator === '>') return ['*','1'];
|
|
15
|
+
if(operator === '<') return ['1','*'];
|
|
16
|
+
if(operator === '-') return ['1','1'];
|
|
17
|
+
}
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
rules
|
|
@@ -173,19 +179,20 @@ ref_short
|
|
|
173
179
|
|
|
174
180
|
ref_body
|
|
175
181
|
= field1:field_identifier sp+ relation:relation sp+ field2:field_identifier sp* ref_settings:RefSettings? {
|
|
182
|
+
const rel = getRelations(relation);
|
|
176
183
|
const endpoints = [
|
|
177
184
|
{
|
|
178
185
|
schemaName: field1.schemaName,
|
|
179
186
|
tableName: field1.tableName,
|
|
180
187
|
fieldNames: field1.fieldNames,
|
|
181
|
-
relation:
|
|
188
|
+
relation: rel[0],
|
|
182
189
|
token: location()
|
|
183
190
|
},
|
|
184
191
|
{
|
|
185
192
|
schemaName: field2.schemaName,
|
|
186
193
|
tableName: field2.tableName,
|
|
187
194
|
fieldNames: field2.fieldNames,
|
|
188
|
-
relation:
|
|
195
|
+
relation: rel[1],
|
|
189
196
|
token: location()
|
|
190
197
|
}
|
|
191
198
|
];
|
|
@@ -243,19 +250,20 @@ TableSyntax
|
|
|
243
250
|
|
|
244
251
|
fields.forEach((field) => {
|
|
245
252
|
(field.inline_refs || []).forEach((iref) => {
|
|
253
|
+
const rel = getRelations(iref.relation);
|
|
246
254
|
const endpoints = [
|
|
247
255
|
{
|
|
248
256
|
schemaName: iref.schemaName,
|
|
249
257
|
tableName: iref.tableName,
|
|
250
258
|
fieldNames: iref.fieldNames,
|
|
251
|
-
relation:
|
|
259
|
+
relation: rel[1],
|
|
252
260
|
token: iref.token
|
|
253
261
|
},
|
|
254
262
|
{
|
|
255
263
|
schemaName: schemaName,
|
|
256
264
|
tableName: name,
|
|
257
265
|
fieldNames: [field.name],
|
|
258
|
-
relation:
|
|
266
|
+
relation: rel[0],
|
|
259
267
|
token: iref.token
|
|
260
268
|
}];
|
|
261
269
|
|
|
@@ -611,7 +619,7 @@ set_null "set null" = "set null"i
|
|
|
611
619
|
set_default "set default" = "set default"i
|
|
612
620
|
|
|
613
621
|
// Commonly used tokens
|
|
614
|
-
relation ">, - or <" =
|
|
622
|
+
relation "<>, >, - or <" = '<>' / '>' / '<' / '-'
|
|
615
623
|
name "valid name"
|
|
616
624
|
= c:(character+) { return c.join("") }
|
|
617
625
|
/ quote c:[^\"\n]+ quote { return c.join("") }
|