@dbml/core 6.0.0 → 6.2.0-alpha.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/README.md +2 -2
- package/lib/export/DbmlExporter.js +53 -1
- package/lib/export/MysqlExporter.js +47 -1
- package/lib/export/OracleExporter.js +52 -3
- package/lib/export/PostgresExporter.js +53 -1
- package/lib/export/SqlServerExporter.js +48 -1
- package/lib/index.js +103 -0
- package/lib/model_structure/field.js +2 -2
- package/lib/parse/ANTLR/ASTGeneration/mssql/MssqlASTGen.js +21 -7
- package/lib/parse/ANTLR/ASTGeneration/mysql/MySQLASTGen.js +5 -2
- package/lib/parse/ANTLR/ASTGeneration/oraclesql/OracleSQLASTGen.js +85 -4
- package/lib/parse/ANTLR/ASTGeneration/oraclesql/README.md +5 -5
- package/lib/parse/ANTLR/ASTGeneration/postgres/PostgresASTGen.js +10 -0
- package/lib/parse/ANTLR/ASTGeneration/snowflake/SnowflakeASTGen.js +90 -5
- package/lib/parse/ANTLR/parsers/mysql/MySqlLexerBase.js +1 -1
- package/package.json +3 -3
- package/types/export/index.d.ts +1 -0
- package/types/index.d.ts +68 -2
- package/types/model_structure/check.d.ts +12 -10
- package/types/model_structure/database.d.ts +51 -41
- package/types/model_structure/endpoint.d.ts +24 -11
- package/types/model_structure/enum.d.ts +13 -10
- package/types/model_structure/enumValue.d.ts +11 -8
- package/types/model_structure/field.d.ts +23 -17
- package/types/model_structure/index.d.ts +91 -0
- package/types/model_structure/indexColumn.d.ts +11 -8
- package/types/model_structure/indexes.d.ts +16 -12
- package/types/model_structure/ref.d.ts +15 -12
- package/types/model_structure/schema.d.ts +17 -14
- package/types/model_structure/stickyNote.d.ts +15 -10
- package/types/model_structure/table.d.ts +17 -15
- package/types/model_structure/tableGroup.d.ts +13 -10
- package/types/model_structure/tablePartial.d.ts +13 -10
package/README.md
CHANGED
|
@@ -160,13 +160,13 @@ For more detailed documentation on each SQL parser, see the individual parser RE
|
|
|
160
160
|
| Feature | PostgreSQL | MySQL | MSSQL | Oracle | Snowflake |
|
|
161
161
|
|---------|------------|-------|-------|--------|-----------|
|
|
162
162
|
| Basic `INSERT` ... VALUES | ✓ | ✓ | ✓ | ✓ | ✗ |
|
|
163
|
-
| Multi-row `INSERT` | ✓ | ✓ | ✓ |
|
|
163
|
+
| Multi-row `INSERT` | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
164
164
|
| `INSERT` ... SELECT | ✗ | ✗ | ✗ | ✗ | ✗ |
|
|
165
165
|
| `INSERT` ... with returned rows (`RETURNING`, `OUTPUT`) | ◐ | — | ◐ | ◐ | — |
|
|
166
166
|
| `INSERT` ... ON CONFLICT/DUPLICATE KEY | ◐ | ◐ | — | — | — |
|
|
167
167
|
|
|
168
168
|
**Notes:**
|
|
169
|
-
- **Oracle**:
|
|
169
|
+
- **Oracle**: Multi-row inserts use `INSERT ALL ... SELECT * FROM dual` syntax, which is fully supported.
|
|
170
170
|
|
|
171
171
|
---
|
|
172
172
|
|
|
@@ -227,7 +227,7 @@ var DbmlExporter = /*#__PURE__*/function () {
|
|
|
227
227
|
if ((0, _utils.shouldPrintSchema)(schema, model)) tableName = "\"".concat(schema.name, "\".\"").concat(table.name, "\"");
|
|
228
228
|
|
|
229
229
|
// Include alias if present
|
|
230
|
-
var aliasStr = table.alias ? " as ".concat((0, _parse.
|
|
230
|
+
var aliasStr = table.alias ? " as ".concat((0, _parse.addDoubleQuoteIfNeeded)(table.alias)) : '';
|
|
231
231
|
var fieldStr = tableContent.fieldContents.map(function (field) {
|
|
232
232
|
return " ".concat(field, "\n");
|
|
233
233
|
}).join('');
|
|
@@ -342,6 +342,57 @@ var DbmlExporter = /*#__PURE__*/function () {
|
|
|
342
342
|
return result ? "".concat(result, "\n").concat(stickyNote) : stickyNote;
|
|
343
343
|
}, '');
|
|
344
344
|
}
|
|
345
|
+
}, {
|
|
346
|
+
key: "exportRecords",
|
|
347
|
+
value: function exportRecords(model) {
|
|
348
|
+
var records = model.records;
|
|
349
|
+
if (!records || (0, _lodash.isEmpty)(records)) {
|
|
350
|
+
return '';
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Group records by schemaName and tableName
|
|
354
|
+
var recordGroups = (0, _lodash.groupBy)(Object.values(records), function (record) {
|
|
355
|
+
return "".concat(record.schemaName || '', ".").concat(record.tableName);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// Process each group
|
|
359
|
+
var recordStrs = Object.values(recordGroups).map(function (groupRecords) {
|
|
360
|
+
var _groupRecords$ = groupRecords[0],
|
|
361
|
+
schemaName = _groupRecords$.schemaName,
|
|
362
|
+
tableName = _groupRecords$.tableName;
|
|
363
|
+
|
|
364
|
+
// Build table reference
|
|
365
|
+
var tableRef = schemaName ? "".concat((0, _parse.addDoubleQuoteIfNeeded)(schemaName), ".").concat((0, _parse.addDoubleQuoteIfNeeded)(tableName)) : (0, _parse.addDoubleQuoteIfNeeded)(tableName);
|
|
366
|
+
|
|
367
|
+
// Collect all unique columns in order
|
|
368
|
+
var allColumns = _toConsumableArray(new Set(groupRecords.flatMap(function (r) {
|
|
369
|
+
return r.columns;
|
|
370
|
+
})));
|
|
371
|
+
var columnList = allColumns.map(_parse.addDoubleQuoteIfNeeded).join(', ');
|
|
372
|
+
|
|
373
|
+
// Merge all rows
|
|
374
|
+
var allRows = groupRecords.flatMap(function (record) {
|
|
375
|
+
var allColumnIndexes = allColumns.map(function (col) {
|
|
376
|
+
return record.columns.indexOf(col);
|
|
377
|
+
});
|
|
378
|
+
return record.values.map(function (row) {
|
|
379
|
+
return allColumnIndexes.map(function (colIdx) {
|
|
380
|
+
return colIdx === -1 ? {
|
|
381
|
+
value: null,
|
|
382
|
+
type: 'expression'
|
|
383
|
+
} : row[colIdx];
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// Build data rows
|
|
389
|
+
var rowStrs = allRows.map(function (row) {
|
|
390
|
+
return " ".concat(row.map(_parse.formatRecordValue).join(', '));
|
|
391
|
+
});
|
|
392
|
+
return "Records ".concat(tableRef, "(").concat(columnList, ") {\n").concat(rowStrs.join('\n'), "\n}\n");
|
|
393
|
+
});
|
|
394
|
+
return recordStrs.join('\n');
|
|
395
|
+
}
|
|
345
396
|
}, {
|
|
346
397
|
key: "export",
|
|
347
398
|
value: function _export(model) {
|
|
@@ -359,6 +410,7 @@ var DbmlExporter = /*#__PURE__*/function () {
|
|
|
359
410
|
if (!(0, _lodash.isEmpty)(refIds)) elementStrs.push(DbmlExporter.exportRefs(refIds, model));
|
|
360
411
|
});
|
|
361
412
|
if (!(0, _lodash.isEmpty)(model.notes)) elementStrs.push(DbmlExporter.exportStickyNotes(model));
|
|
413
|
+
if (!(0, _lodash.isEmpty)(model.records)) elementStrs.push(DbmlExporter.exportRecords(model));
|
|
362
414
|
|
|
363
415
|
// all elements already end with 1 '\n', so join('\n') to separate them with 1 blank line
|
|
364
416
|
return elementStrs.join('\n');
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = void 0;
|
|
7
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
8
|
var _utils = require("./utils");
|
|
9
|
+
var _parse = require("@dbml/parse");
|
|
9
10
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
10
11
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
12
|
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
@@ -24,6 +25,46 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
24
25
|
_classCallCheck(this, MySQLExporter);
|
|
25
26
|
}
|
|
26
27
|
return _createClass(MySQLExporter, null, [{
|
|
28
|
+
key: "exportRecords",
|
|
29
|
+
value: function exportRecords(model) {
|
|
30
|
+
var records = Object.values(model.records || {});
|
|
31
|
+
if (_lodash["default"].isEmpty(records)) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
var insertStatements = records.map(function (record) {
|
|
35
|
+
var schemaName = record.schemaName,
|
|
36
|
+
tableName = record.tableName,
|
|
37
|
+
columns = record.columns,
|
|
38
|
+
values = record.values;
|
|
39
|
+
|
|
40
|
+
// Build the table reference with schema if present
|
|
41
|
+
var tableRef = schemaName ? "`".concat(schemaName, "`.`").concat(tableName, "`") : "`".concat(tableName, "`");
|
|
42
|
+
|
|
43
|
+
// Build the column list
|
|
44
|
+
var columnList = columns.length > 0 ? "(`".concat(columns.join('`, `'), "`)") : '';
|
|
45
|
+
|
|
46
|
+
// Value formatter for MySQL
|
|
47
|
+
var formatValue = function formatValue(val) {
|
|
48
|
+
if (val.value === null) return 'NULL';
|
|
49
|
+
if (val.type === 'expression') return val.value;
|
|
50
|
+
if ((0, _parse.isNumericType)(val.type)) return val.value;
|
|
51
|
+
if ((0, _parse.isBooleanType)(val.type)) return String(val.value).toUpperCase() === 'TRUE' ? 'TRUE' : 'FALSE';
|
|
52
|
+
if ((0, _parse.isStringType)(val.type) || (0, _parse.isBinaryType)(val.type) || (0, _parse.isDateTimeType)(val.type)) return "'".concat(val.value.replace(/'/g, "''").replace(/\\/g, '\\\\'), "'");
|
|
53
|
+
// Unknown type - use CAST
|
|
54
|
+
return "CAST('".concat(val.value.replace(/'/g, "''").replace(/\\/g, '\\\\'), "' AS ").concat(val.type, ")");
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Build the VALUES clause
|
|
58
|
+
var valueRows = values.map(function (row) {
|
|
59
|
+
var valueStrs = row.map(formatValue);
|
|
60
|
+
return "(".concat(valueStrs.join(', '), ")");
|
|
61
|
+
});
|
|
62
|
+
var valuesClause = valueRows.join(',\n ');
|
|
63
|
+
return "INSERT INTO ".concat(tableRef, " ").concat(columnList, "\nVALUES\n ").concat(valuesClause, ";");
|
|
64
|
+
});
|
|
65
|
+
return insertStatements;
|
|
66
|
+
}
|
|
67
|
+
}, {
|
|
27
68
|
key: "getFieldLines",
|
|
28
69
|
value: function getFieldLines(tableId, model) {
|
|
29
70
|
var table = model.tables[tableId];
|
|
@@ -337,7 +378,12 @@ var MySQLExporter = /*#__PURE__*/function () {
|
|
|
337
378
|
comments: [],
|
|
338
379
|
refs: []
|
|
339
380
|
});
|
|
340
|
-
|
|
381
|
+
|
|
382
|
+
// Export INSERT statements
|
|
383
|
+
// Note: MySQL does not support DEFERRED constraints, so foreign key checks are disabled
|
|
384
|
+
var insertStatements = MySQLExporter.exportRecords(model);
|
|
385
|
+
var recordsSection = !_lodash["default"].isEmpty(insertStatements) ? ['-- Disable foreign key checks for INSERT', 'SET FOREIGN_KEY_CHECKS = 0;', ''].concat(_toConsumableArray(insertStatements), ['', '-- Re-enable foreign key checks', 'SET FOREIGN_KEY_CHECKS = 1;']) : [];
|
|
386
|
+
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs, recordsSection).join('\n');
|
|
341
387
|
return res;
|
|
342
388
|
}
|
|
343
389
|
}]);
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = void 0;
|
|
7
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
8
|
var _utils = require("./utils");
|
|
9
|
+
var _parse = require("@dbml/parse");
|
|
9
10
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
10
11
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
12
|
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
@@ -27,6 +28,50 @@ var OracleExporter = /*#__PURE__*/function () {
|
|
|
27
28
|
_classCallCheck(this, OracleExporter);
|
|
28
29
|
}
|
|
29
30
|
return _createClass(OracleExporter, null, [{
|
|
31
|
+
key: "exportRecords",
|
|
32
|
+
value: function exportRecords(model) {
|
|
33
|
+
var records = Object.values(model.records || {});
|
|
34
|
+
if (_lodash["default"].isEmpty(records)) {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
var insertStatements = records.map(function (record) {
|
|
38
|
+
var schemaName = record.schemaName,
|
|
39
|
+
tableName = record.tableName,
|
|
40
|
+
columns = record.columns,
|
|
41
|
+
values = record.values;
|
|
42
|
+
|
|
43
|
+
// Build the table reference with schema if present
|
|
44
|
+
var tableRef = schemaName ? "\"".concat(schemaName, "\".\"").concat(tableName, "\"") : "\"".concat(tableName, "\"");
|
|
45
|
+
|
|
46
|
+
// Build the column list
|
|
47
|
+
var columnList = columns.length > 0 ? "(\"".concat(columns.join('", "'), "\")") : '';
|
|
48
|
+
var formatValue = function formatValue(val) {
|
|
49
|
+
if (val.value === null) return 'NULL';
|
|
50
|
+
if (val.type === 'expression') return val.value;
|
|
51
|
+
if ((0, _parse.isNumericType)(val.type)) return val.value;
|
|
52
|
+
if ((0, _parse.isBooleanType)(val.type)) return String(val.value).toUpperCase() === 'TRUE' ? '1' : '0';
|
|
53
|
+
if ((0, _parse.isStringType)(val.type) || (0, _parse.isDateTimeType)(val.type)) return "'".concat(val.value.replace(/'/g, "''"), "'");
|
|
54
|
+
if ((0, _parse.isBinaryType)(val.type)) return "HEXTORAW('".concat(val.value, "')");
|
|
55
|
+
// Unknown type - use CAST
|
|
56
|
+
return "CAST('".concat(val.value.replace(/'/g, "''"), "' AS ").concat(val.type, ")");
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Build the INSERT ALL statement for multiple rows
|
|
60
|
+
if (values.length > 1) {
|
|
61
|
+
var intoStatements = values.map(function (row) {
|
|
62
|
+
var valueStrs = row.map(formatValue);
|
|
63
|
+
return " INTO ".concat(tableRef, " ").concat(columnList, " VALUES (").concat(valueStrs.join(', '), ")");
|
|
64
|
+
});
|
|
65
|
+
return "INSERT ALL\n".concat(intoStatements.join('\n'), "\nSELECT * FROM dual;");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Single row INSERT
|
|
69
|
+
var valueStrs = values[0].map(formatValue);
|
|
70
|
+
return "INSERT INTO ".concat(tableRef, " ").concat(columnList, "\nVALUES (").concat(valueStrs.join(', '), ");");
|
|
71
|
+
});
|
|
72
|
+
return insertStatements;
|
|
73
|
+
}
|
|
74
|
+
}, {
|
|
30
75
|
key: "buildSchemaToTableNameSetMap",
|
|
31
76
|
value: function buildSchemaToTableNameSetMap(model) {
|
|
32
77
|
var schemaToTableNameSetMap = new Map();
|
|
@@ -225,7 +270,7 @@ var OracleExporter = /*#__PURE__*/function () {
|
|
|
225
270
|
key: "buildForeignKeyManyToMany",
|
|
226
271
|
value: function buildForeignKeyManyToMany(foreignEndpointTableName, foreignEndpointFields, refEndpointTableName, refEndpointFieldsString) {
|
|
227
272
|
var foreignEndpointFieldsString = _toConsumableArray(foreignEndpointFields.keys()).join('`, `');
|
|
228
|
-
var line = "ALTER TABLE ".concat(foreignEndpointTableName, " ADD FOREIGN KEY (\"").concat(foreignEndpointFieldsString, "\") REFERENCES ").concat(refEndpointTableName, " ").concat(refEndpointFieldsString, ";\n");
|
|
273
|
+
var line = "ALTER TABLE ".concat(foreignEndpointTableName, " ADD FOREIGN KEY (\"").concat(foreignEndpointFieldsString, "\") REFERENCES ").concat(refEndpointTableName, " ").concat(refEndpointFieldsString, " DEFERRABLE INITIALLY IMMEDIATE;\n");
|
|
229
274
|
return line;
|
|
230
275
|
}
|
|
231
276
|
}, {
|
|
@@ -269,7 +314,7 @@ var OracleExporter = /*#__PURE__*/function () {
|
|
|
269
314
|
if (ref.onDelete) {
|
|
270
315
|
line += " ON DELETE ".concat(ref.onDelete.toUpperCase());
|
|
271
316
|
}
|
|
272
|
-
line += ';\n';
|
|
317
|
+
line += ' DEFERRABLE INITIALLY IMMEDIATE;\n';
|
|
273
318
|
result.refs.push(line);
|
|
274
319
|
return;
|
|
275
320
|
}
|
|
@@ -482,7 +527,11 @@ var OracleExporter = /*#__PURE__*/function () {
|
|
|
482
527
|
referenceGrants: [],
|
|
483
528
|
refs: []
|
|
484
529
|
});
|
|
485
|
-
|
|
530
|
+
|
|
531
|
+
// Export INSERT statements with constraint checking disabled
|
|
532
|
+
var insertStatements = this.exportRecords(model);
|
|
533
|
+
var recordsSection = !_lodash["default"].isEmpty(insertStatements) ? ['-- Disable constraint checking for INSERT', 'SET CONSTRAINTS ALL DEFERRED;', ''].concat(_toConsumableArray(insertStatements), ['', 'COMMIT;']) : [];
|
|
534
|
+
var res = _lodash["default"].concat(statements.schemas, statements.tables, statements.indexes, statements.comments, statements.referenceGrants, statements.refs, recordsSection).join('\n');
|
|
486
535
|
return res;
|
|
487
536
|
}
|
|
488
537
|
}]);
|
|
@@ -7,6 +7,7 @@ exports["default"] = void 0;
|
|
|
7
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
8
|
var _utils = require("./utils");
|
|
9
9
|
var _utils2 = require("../model_structure/utils");
|
|
10
|
+
var _parse = require("@dbml/parse");
|
|
10
11
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
11
12
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
12
13
|
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
@@ -66,6 +67,53 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
66
67
|
_classCallCheck(this, PostgresExporter);
|
|
67
68
|
}
|
|
68
69
|
return _createClass(PostgresExporter, null, [{
|
|
70
|
+
key: "exportRecords",
|
|
71
|
+
value: function exportRecords(model) {
|
|
72
|
+
var records = Object.values(model.records || {});
|
|
73
|
+
if (_lodash["default"].isEmpty(records)) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
var insertStatements = records.map(function (record) {
|
|
77
|
+
var schemaName = record.schemaName,
|
|
78
|
+
tableName = record.tableName,
|
|
79
|
+
columns = record.columns,
|
|
80
|
+
values = record.values;
|
|
81
|
+
|
|
82
|
+
// Skip if no values
|
|
83
|
+
if (!values || values.length === 0) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Build the table reference with schema if present
|
|
88
|
+
var tableRef = schemaName ? "\"".concat(schemaName, "\".\"").concat(tableName, "\"") : "\"".concat(tableName, "\"");
|
|
89
|
+
|
|
90
|
+
// Build the column list
|
|
91
|
+
var columnList = columns.length > 0 ? "(".concat(columns.map(function (col) {
|
|
92
|
+
return "\"".concat(col, "\"");
|
|
93
|
+
}).join(', '), ")") : '';
|
|
94
|
+
|
|
95
|
+
// Value formatter for PostgreSQL
|
|
96
|
+
var formatValue = function formatValue(val) {
|
|
97
|
+
if (val.value === null) return 'NULL';
|
|
98
|
+
if (val.type === 'expression') return val.value;
|
|
99
|
+
if ((0, _parse.isNumericType)(val.type)) return val.value;
|
|
100
|
+
if ((0, _parse.isBooleanType)(val.type)) return String(val.value).toUpperCase() === 'TRUE' ? 'TRUE' : 'FALSE';
|
|
101
|
+
if ((0, _parse.isStringType)(val.type) || (0, _parse.isDateTimeType)(val.type) || (0, _parse.isBinaryType)(val.type)) return "'".concat(String(val.value).replace(/'/g, "''"), "'");
|
|
102
|
+
// Unknown type - use CAST
|
|
103
|
+
return "CAST('".concat(String(val.value).replace(/'/g, "''"), "' AS ").concat(val.type, ")");
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// Build the VALUES clause
|
|
107
|
+
var valueRows = values.map(function (row) {
|
|
108
|
+
var valueStrs = row.map(formatValue);
|
|
109
|
+
return "(".concat(valueStrs.join(', '), ")");
|
|
110
|
+
});
|
|
111
|
+
var valuesClause = valueRows.join(',\n ');
|
|
112
|
+
return "INSERT INTO ".concat(tableRef, " ").concat(columnList, "\nVALUES\n ").concat(valuesClause, ";");
|
|
113
|
+
}).filter(Boolean);
|
|
114
|
+
return insertStatements;
|
|
115
|
+
}
|
|
116
|
+
}, {
|
|
69
117
|
key: "exportEnums",
|
|
70
118
|
value: function exportEnums(enumIds, model) {
|
|
71
119
|
return enumIds.map(function (enumId) {
|
|
@@ -451,7 +499,11 @@ var PostgresExporter = /*#__PURE__*/function () {
|
|
|
451
499
|
}
|
|
452
500
|
return prevStatements;
|
|
453
501
|
}, schemaEnumStatements);
|
|
454
|
-
|
|
502
|
+
|
|
503
|
+
// Export INSERT statements with constraint checking disabled
|
|
504
|
+
var insertStatements = PostgresExporter.exportRecords(model);
|
|
505
|
+
var recordsSection = !_lodash["default"].isEmpty(insertStatements) ? ['-- Disable constraint checking for INSERT', 'BEGIN;', 'SET session_replication_role = replica;', ''].concat(_toConsumableArray(insertStatements), ['', 'SET session_replication_role = DEFAULT;', 'COMMIT;']) : [];
|
|
506
|
+
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs, recordsSection).join('\n');
|
|
455
507
|
return res;
|
|
456
508
|
}
|
|
457
509
|
}]);
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports["default"] = void 0;
|
|
7
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
8
|
var _utils = require("./utils");
|
|
9
|
+
var _parse = require("@dbml/parse");
|
|
9
10
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
10
11
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
12
|
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
@@ -24,6 +25,47 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
24
25
|
_classCallCheck(this, SqlServerExporter);
|
|
25
26
|
}
|
|
26
27
|
return _createClass(SqlServerExporter, null, [{
|
|
28
|
+
key: "exportRecords",
|
|
29
|
+
value: function exportRecords(model) {
|
|
30
|
+
var records = Object.values(model.records || {});
|
|
31
|
+
if (_lodash["default"].isEmpty(records)) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
var insertStatements = records.map(function (record) {
|
|
35
|
+
var schemaName = record.schemaName,
|
|
36
|
+
tableName = record.tableName,
|
|
37
|
+
columns = record.columns,
|
|
38
|
+
values = record.values;
|
|
39
|
+
|
|
40
|
+
// Build the table reference with schema if present
|
|
41
|
+
var tableRef = schemaName ? "[".concat(schemaName, "].[").concat(tableName, "]") : "[".concat(tableName, "]");
|
|
42
|
+
|
|
43
|
+
// Build the column list
|
|
44
|
+
var columnList = columns.length > 0 ? "([".concat(columns.join('], ['), "])") : '';
|
|
45
|
+
|
|
46
|
+
// Value formatter for SQL Server
|
|
47
|
+
var formatValue = function formatValue(val) {
|
|
48
|
+
if (val.value === null) return 'NULL';
|
|
49
|
+
if (val.type === 'expression') return val.value;
|
|
50
|
+
if ((0, _parse.isNumericType)(val.type)) return val.value;
|
|
51
|
+
if ((0, _parse.isBooleanType)(val.type)) return String(val.value).toUpperCase() === 'TRUE' ? '1' : '0';
|
|
52
|
+
if ((0, _parse.isStringType)(val.type) || (0, _parse.isDateTimeType)(val.type)) return "'".concat(val.value.replace(/'/g, "''"), "'");
|
|
53
|
+
if ((0, _parse.isBinaryType)(val.type)) return "0x".concat(val.value); // SQL Server binary as hex
|
|
54
|
+
// Unknown type - use CAST
|
|
55
|
+
return "CAST('".concat(val.value.replace(/'/g, "''"), "' AS ").concat(val.type, ")");
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Build the VALUES clause
|
|
59
|
+
var valueRows = values.map(function (row) {
|
|
60
|
+
var valueStrs = row.map(formatValue);
|
|
61
|
+
return "(".concat(valueStrs.join(', '), ")");
|
|
62
|
+
});
|
|
63
|
+
var valuesClause = valueRows.join(',\n ');
|
|
64
|
+
return "INSERT INTO ".concat(tableRef, " ").concat(columnList, "\nVALUES\n ").concat(valuesClause, ";\nGO");
|
|
65
|
+
});
|
|
66
|
+
return insertStatements;
|
|
67
|
+
}
|
|
68
|
+
}, {
|
|
27
69
|
key: "getFieldLines",
|
|
28
70
|
value: function getFieldLines(tableId, model) {
|
|
29
71
|
var table = model.tables[tableId];
|
|
@@ -364,7 +406,12 @@ var SqlServerExporter = /*#__PURE__*/function () {
|
|
|
364
406
|
comments: [],
|
|
365
407
|
refs: []
|
|
366
408
|
});
|
|
367
|
-
|
|
409
|
+
|
|
410
|
+
// Export INSERT statements
|
|
411
|
+
// Note: SQL Server does not support DEFERRED constraints, so constraint checks are disabled
|
|
412
|
+
var insertStatements = SqlServerExporter.exportRecords(model);
|
|
413
|
+
var recordsSection = !_lodash["default"].isEmpty(insertStatements) ? ['-- Disable constraint checks for INSERT', 'EXEC sp_MSforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";', 'GO', ''].concat(_toConsumableArray(insertStatements), ['', '-- Re-enable constraint checks', 'EXEC sp_MSforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";', 'GO']) : [];
|
|
414
|
+
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs, recordsSection).join('\n');
|
|
368
415
|
return res;
|
|
369
416
|
}
|
|
370
417
|
}]);
|
package/lib/index.js
CHANGED
|
@@ -21,30 +21,132 @@ Object.defineProperty(exports, "Parser", {
|
|
|
21
21
|
return _Parser["default"];
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
|
+
Object.defineProperty(exports, "SqlDialect", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function get() {
|
|
27
|
+
return _parse.SqlDialect;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
24
30
|
Object.defineProperty(exports, "VERSION", {
|
|
25
31
|
enumerable: true,
|
|
26
32
|
get: function get() {
|
|
27
33
|
return _version.VERSION;
|
|
28
34
|
}
|
|
29
35
|
});
|
|
36
|
+
Object.defineProperty(exports, "addDoubleQuoteIfNeeded", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function get() {
|
|
39
|
+
return _parse.addDoubleQuoteIfNeeded;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
30
42
|
Object.defineProperty(exports, "exporter", {
|
|
31
43
|
enumerable: true,
|
|
32
44
|
get: function get() {
|
|
33
45
|
return _export["default"];
|
|
34
46
|
}
|
|
35
47
|
});
|
|
48
|
+
Object.defineProperty(exports, "formatRecordValue", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function get() {
|
|
51
|
+
return _parse.formatRecordValue;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
36
54
|
Object.defineProperty(exports, "importer", {
|
|
37
55
|
enumerable: true,
|
|
38
56
|
get: function get() {
|
|
39
57
|
return _import["default"];
|
|
40
58
|
}
|
|
41
59
|
});
|
|
60
|
+
Object.defineProperty(exports, "isBinaryType", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function get() {
|
|
63
|
+
return _parse.isBinaryType;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(exports, "isBooleanType", {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function get() {
|
|
69
|
+
return _parse.isBooleanType;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(exports, "isDateTimeType", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function get() {
|
|
75
|
+
return _parse.isDateTimeType;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(exports, "isFloatType", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function get() {
|
|
81
|
+
return _parse.isFloatType;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperty(exports, "isIntegerType", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
get: function get() {
|
|
87
|
+
return _parse.isIntegerType;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
Object.defineProperty(exports, "isNumericType", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
get: function get() {
|
|
93
|
+
return _parse.isNumericType;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
Object.defineProperty(exports, "isSerialType", {
|
|
97
|
+
enumerable: true,
|
|
98
|
+
get: function get() {
|
|
99
|
+
return _parse.isSerialType;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
Object.defineProperty(exports, "isStringType", {
|
|
103
|
+
enumerable: true,
|
|
104
|
+
get: function get() {
|
|
105
|
+
return _parse.isStringType;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
42
108
|
Object.defineProperty(exports, "renameTable", {
|
|
43
109
|
enumerable: true,
|
|
44
110
|
get: function get() {
|
|
45
111
|
return _transform.renameTable;
|
|
46
112
|
}
|
|
47
113
|
});
|
|
114
|
+
Object.defineProperty(exports, "tryExtractBoolean", {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
get: function get() {
|
|
117
|
+
return _parse.tryExtractBoolean;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
Object.defineProperty(exports, "tryExtractDateTime", {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
get: function get() {
|
|
123
|
+
return _parse.tryExtractDateTime;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
Object.defineProperty(exports, "tryExtractEnum", {
|
|
127
|
+
enumerable: true,
|
|
128
|
+
get: function get() {
|
|
129
|
+
return _parse.tryExtractEnum;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
Object.defineProperty(exports, "tryExtractInteger", {
|
|
133
|
+
enumerable: true,
|
|
134
|
+
get: function get() {
|
|
135
|
+
return _parse.tryExtractInteger;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
Object.defineProperty(exports, "tryExtractNumeric", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
get: function get() {
|
|
141
|
+
return _parse.tryExtractNumeric;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
Object.defineProperty(exports, "tryExtractString", {
|
|
145
|
+
enumerable: true,
|
|
146
|
+
get: function get() {
|
|
147
|
+
return _parse.tryExtractString;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
48
150
|
var _ModelExporter = _interopRequireDefault(require("./export/ModelExporter"));
|
|
49
151
|
var _Parser = _interopRequireDefault(require("./parse/Parser"));
|
|
50
152
|
var _error = require("./parse/error");
|
|
@@ -52,4 +154,5 @@ var _import = _interopRequireDefault(require("./import"));
|
|
|
52
154
|
var _export = _interopRequireDefault(require("./export"));
|
|
53
155
|
var _transform = require("./transform");
|
|
54
156
|
var _version = require("./utils/version");
|
|
157
|
+
var _parse = require("@dbml/parse");
|
|
55
158
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
@@ -138,7 +138,7 @@ var Field = /*#__PURE__*/function (_Element) {
|
|
|
138
138
|
}, {
|
|
139
139
|
key: "shallowExport",
|
|
140
140
|
value: function shallowExport() {
|
|
141
|
-
var _this$injectedPartial;
|
|
141
|
+
var _this$injectedPartial, _this$injectedPartial2;
|
|
142
142
|
return {
|
|
143
143
|
name: this.name,
|
|
144
144
|
type: this.type,
|
|
@@ -148,7 +148,7 @@ var Field = /*#__PURE__*/function (_Element) {
|
|
|
148
148
|
note: this.note,
|
|
149
149
|
dbdefault: this.dbdefault,
|
|
150
150
|
increment: this.increment,
|
|
151
|
-
injectedPartialId: (_this$injectedPartial = this.injectedPartial) === null || _this$
|
|
151
|
+
injectedPartialId: (_this$injectedPartial = (_this$injectedPartial2 = this.injectedPartial) === null || _this$injectedPartial2 === void 0 ? void 0 : _this$injectedPartial2.id) !== null && _this$injectedPartial !== void 0 ? _this$injectedPartial : null,
|
|
152
152
|
checkIds: this.checks.map(function (check) {
|
|
153
153
|
return check.id;
|
|
154
154
|
})
|
|
@@ -37,6 +37,7 @@ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length)
|
|
|
37
37
|
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
38
38
|
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
39
39
|
var ADD_DESCRIPTION_FUNCTION_NAME = 'sp_addextendedproperty';
|
|
40
|
+
var DEFAULT_SCHEMA = 'dbo';
|
|
40
41
|
var getSchemaAndTableName = function getSchemaAndTableName(names) {
|
|
41
42
|
var tableName = (0, _lodash.last)(names);
|
|
42
43
|
var schemaName = names.length > 1 ? (0, _lodash.nth)(names, -2) : undefined;
|
|
@@ -138,13 +139,23 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
|
|
|
138
139
|
};
|
|
139
140
|
return _this;
|
|
140
141
|
}
|
|
141
|
-
|
|
142
|
-
// tsql_file
|
|
143
|
-
// : batch* EOF
|
|
144
|
-
// | execute_body_batch go_statement* EOF
|
|
145
|
-
// ;
|
|
146
142
|
_inherits(MssqlASTGen, _TSqlParserVisitor);
|
|
147
143
|
return _createClass(MssqlASTGen, [{
|
|
144
|
+
key: "findTable",
|
|
145
|
+
value: function findTable(schemaName, tableName) {
|
|
146
|
+
var realSchemaName = schemaName || DEFAULT_SCHEMA;
|
|
147
|
+
var table = this.data.tables.find(function (t) {
|
|
148
|
+
var targetSchemaName = t.schemaName || DEFAULT_SCHEMA;
|
|
149
|
+
return targetSchemaName === realSchemaName && t.name === tableName;
|
|
150
|
+
});
|
|
151
|
+
return table;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// tsql_file
|
|
155
|
+
// : batch* EOF
|
|
156
|
+
// | execute_body_batch go_statement* EOF
|
|
157
|
+
// ;
|
|
158
|
+
}, {
|
|
148
159
|
key: "visitTsql_file",
|
|
149
160
|
value: function visitTsql_file(ctx) {
|
|
150
161
|
var _this2 = this;
|
|
@@ -219,6 +230,7 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
|
|
|
219
230
|
}, {
|
|
220
231
|
key: "visitInsert_statement",
|
|
221
232
|
value: function visitInsert_statement(ctx) {
|
|
233
|
+
var _this$findTable;
|
|
222
234
|
// [ 'users' ]
|
|
223
235
|
// [ 'test', 'users' ]
|
|
224
236
|
// [ 'db', 'test', 'users' ]
|
|
@@ -228,7 +240,9 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
|
|
|
228
240
|
var names = ctx.ddl_object().accept(this);
|
|
229
241
|
var tableName = (0, _lodash.last)(names);
|
|
230
242
|
var schemaName = names.length > 1 ? (0, _lodash.nth)(names, -2) : undefined;
|
|
231
|
-
var columns = ctx.insert_column_name_list() ? ctx.insert_column_name_list().accept(this) :
|
|
243
|
+
var columns = ctx.insert_column_name_list() ? ctx.insert_column_name_list().accept(this) : ((_this$findTable = this.findTable(schemaName, tableName)) === null || _this$findTable === void 0 ? void 0 : _this$findTable.fields.map(function (field) {
|
|
244
|
+
return field.name;
|
|
245
|
+
})) || [];
|
|
232
246
|
var values = ctx.insert_statement_value().accept(this);
|
|
233
247
|
var record = new _AST.TableRecord({
|
|
234
248
|
tableName: tableName,
|
|
@@ -1335,7 +1349,7 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
|
|
|
1335
1349
|
// https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addextendedproperty-transact-sql?view=sql-server-ver16#----level0type
|
|
1336
1350
|
var level0Type = argsObj.level0type.toLowerCase();
|
|
1337
1351
|
if (!level0Type.includes('schema')) return;
|
|
1338
|
-
var schemaName = argsObj.level0name !==
|
|
1352
|
+
var schemaName = argsObj.level0name !== DEFAULT_SCHEMA ? argsObj.level0name : undefined;
|
|
1339
1353
|
var level1Type = argsObj.level1type.toLowerCase();
|
|
1340
1354
|
var tableName = level1Type.includes('table') ? argsObj.level1name : null;
|
|
1341
1355
|
var table = this.data.tables.find(function (t) {
|
|
@@ -1226,12 +1226,15 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
1226
1226
|
}, {
|
|
1227
1227
|
key: "visitInsertStatement",
|
|
1228
1228
|
value: function visitInsertStatement(ctx) {
|
|
1229
|
+
var _this$findTable;
|
|
1229
1230
|
var names = ctx.tableName().accept(this);
|
|
1230
1231
|
var tableName = (0, _lodash.last)(names);
|
|
1231
1232
|
var schemaName = names.length > 1 ? names[names.length - 2] : undefined;
|
|
1232
1233
|
|
|
1233
|
-
//
|
|
1234
|
-
var columns = ctx.fullColumnNameList() ? ctx.fullColumnNameList().accept(this) :
|
|
1234
|
+
// Get explicit columns if specified, otherwise lookup table definition
|
|
1235
|
+
var columns = ctx.fullColumnNameList() ? ctx.fullColumnNameList().accept(this) : ((_this$findTable = this.findTable(schemaName, tableName)) === null || _this$findTable === void 0 ? void 0 : _this$findTable.fields.map(function (field) {
|
|
1236
|
+
return field.name;
|
|
1237
|
+
})) || [];
|
|
1235
1238
|
var values = ctx.insertStatementValue().accept(this);
|
|
1236
1239
|
var record = new _AST.TableRecord({
|
|
1237
1240
|
schemaName: schemaName,
|