@dbml/core 2.3.1 → 2.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/export/DbmlExporter.js +9 -1
- package/lib/export/MysqlExporter.js +41 -35
- package/lib/export/PostgresExporter.js +55 -42
- package/lib/export/SqlServerExporter.js +43 -39
- package/lib/model_structure/database.js +53 -12
- package/lib/model_structure/endpoint.js +2 -2
- package/lib/model_structure/field.js +31 -1
- package/lib/model_structure/ref.js +1 -2
- package/lib/model_structure/schema.js +3 -19
- package/lib/model_structure/tableGroup.js +1 -1
- package/lib/model_structure/utils.js +5 -0
- package/lib/parse/dbml/parser.pegjs +61 -19
- package/lib/parse/dbmlParser.js +1219 -883
- package/lib/parse/mssql/fk_definition/actions.js +10 -3
- package/lib/parse/mssql/statements/actions.js +9 -6
- package/lib/parse/mssql/statements/statement_types/alter_table/actions.js +11 -5
- package/lib/parse/mssql/statements/statement_types/create_index/actions.js +6 -1
- package/lib/parse/mssql/statements/statement_types/create_table/actions.js +11 -9
- package/lib/parse/mssql/utils.js +15 -0
- package/lib/parse/mysql/parser.pegjs +46 -14
- package/lib/parse/mysqlParser.js +215 -167
- package/lib/parse/postgresParser.js +11 -10
- package/lib/parse/postgresql/Base_rules.pegjs +24 -3
- package/lib/parse/postgresql/Commands/Alter_table/Alter_table.pegjs +2 -1
- package/lib/parse/postgresql/Commands/Create_table/Create_table_normal.pegjs +5 -3
- package/lib/parse/postgresql/Commands/Create_table/Create_table_of.pegjs +1 -1
- package/lib/parse/postgresql/Commands/Create_table/Create_table_partition_of.pegjs +1 -1
- package/lib/parse/postgresql/Commands/Create_type/Create_type_enum.pegjs +2 -2
- package/lib/parse/postgresql/InitializerUtils.pegjs +10 -0
- package/lib/parse/postgresql/parser.pegjs +2 -1
- package/package.json +2 -2
|
@@ -88,10 +88,10 @@ var Schema = /*#__PURE__*/function (_Element) {
|
|
|
88
88
|
|
|
89
89
|
_this.generateId();
|
|
90
90
|
|
|
91
|
-
_this.processTables(tables);
|
|
92
|
-
|
|
93
91
|
_this.processEnums(enums);
|
|
94
92
|
|
|
93
|
+
_this.processTables(tables);
|
|
94
|
+
|
|
95
95
|
_this.processRefs(refs);
|
|
96
96
|
|
|
97
97
|
_this.processTableGroups(tableGroups);
|
|
@@ -134,7 +134,7 @@ var Schema = /*#__PURE__*/function (_Element) {
|
|
|
134
134
|
key: "findTable",
|
|
135
135
|
value: function findTable(tableName) {
|
|
136
136
|
return this.tables.find(function (t) {
|
|
137
|
-
return t.name === tableName
|
|
137
|
+
return t.name === tableName;
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
140
|
}, {
|
|
@@ -153,7 +153,6 @@ var Schema = /*#__PURE__*/function (_Element) {
|
|
|
153
153
|
value: function pushEnum(_enum) {
|
|
154
154
|
this.checkEnum(_enum);
|
|
155
155
|
this.enums.push(_enum);
|
|
156
|
-
this.bindEnumToField(_enum);
|
|
157
156
|
}
|
|
158
157
|
}, {
|
|
159
158
|
key: "checkEnum",
|
|
@@ -164,21 +163,6 @@ var Schema = /*#__PURE__*/function (_Element) {
|
|
|
164
163
|
_enum.error("Enum ".concat((0, _utils.shouldPrintSchema)(this) ? "\"".concat(this.name, "\".") : '', "\"").concat(_enum.name, "\" existed"));
|
|
165
164
|
}
|
|
166
165
|
}
|
|
167
|
-
}, {
|
|
168
|
-
key: "bindEnumToField",
|
|
169
|
-
value: function bindEnumToField(_enum) {
|
|
170
|
-
this.database.schemas.forEach(function (schema) {
|
|
171
|
-
schema.tables.forEach(function (table) {
|
|
172
|
-
table.fields.forEach(function (field) {
|
|
173
|
-
if (_enum.name === field.type.type_name && (field.type.schemaName || _config.DEFAULT_SCHEMA_NAME) === schema.name) {
|
|
174
|
-
field._enum = _enum;
|
|
175
|
-
|
|
176
|
-
_enum.pushField(field);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
166
|
}, {
|
|
183
167
|
key: "processRefs",
|
|
184
168
|
value: function processRefs(rawRefs) {
|
|
@@ -80,7 +80,7 @@ var TableGroup = /*#__PURE__*/function (_Element) {
|
|
|
80
80
|
var _this2 = this;
|
|
81
81
|
|
|
82
82
|
rawTables.forEach(function (rawTable) {
|
|
83
|
-
var table = _this2.schema.database.findTable(rawTable);
|
|
83
|
+
var table = _this2.schema.database.findTable(rawTable.schemaName, rawTable.name);
|
|
84
84
|
|
|
85
85
|
if (!table) {
|
|
86
86
|
_this2.error("Table ".concat(rawTable.schemaName ? "\"".concat(rawTable.schemaName, "\".") : '').concat(rawTable.name, " don't exist"));
|
|
@@ -4,9 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.shouldPrintSchema = shouldPrintSchema;
|
|
7
|
+
exports.shouldPrintSchemaName = shouldPrintSchemaName;
|
|
7
8
|
|
|
8
9
|
var _config = require("./config");
|
|
9
10
|
|
|
10
11
|
function shouldPrintSchema(schema) {
|
|
11
12
|
return schema.name !== _config.DEFAULT_SCHEMA_NAME || schema.name === _config.DEFAULT_SCHEMA_NAME && schema.database.hasDefaultSchema;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function shouldPrintSchemaName(schemaName) {
|
|
16
|
+
return schemaName !== _config.DEFAULT_SCHEMA_NAME;
|
|
12
17
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
refs: [],
|
|
6
6
|
enums: [],
|
|
7
7
|
tableGroups: [],
|
|
8
|
+
aliases: [],
|
|
8
9
|
project: {},
|
|
9
10
|
};
|
|
10
11
|
let projectCnt = 0;
|
|
@@ -122,23 +123,31 @@ ProjectField
|
|
|
122
123
|
}
|
|
123
124
|
}
|
|
124
125
|
|
|
125
|
-
TableGroupSyntax = table_group sp+ name:name _ "{" _ body:table_group_body _ "}" {
|
|
126
|
+
TableGroupSyntax = table_group sp+ schemaName:schema_name? name:name _ "{" _ body:table_group_body _ "}" {
|
|
126
127
|
return {
|
|
127
128
|
name: name,
|
|
129
|
+
schemaName,
|
|
128
130
|
tables: body,
|
|
129
131
|
token: location()
|
|
130
132
|
}
|
|
131
133
|
}
|
|
132
134
|
|
|
133
|
-
table_group_body = tables:(name __)* {
|
|
135
|
+
table_group_body = tables:(schema_name? name __)* {
|
|
134
136
|
return tables.map(t => ({
|
|
135
|
-
name: t[
|
|
137
|
+
name: t[1],
|
|
138
|
+
schemaName: t[0]
|
|
136
139
|
}));
|
|
137
140
|
}
|
|
138
141
|
|
|
139
142
|
// References
|
|
140
143
|
RefSyntax
|
|
141
|
-
= r: (ref_long / ref_short) {
|
|
144
|
+
= r: (ref_long / ref_short) {
|
|
145
|
+
const schemaName = r.endpoints[0].schemaName;
|
|
146
|
+
return {
|
|
147
|
+
...r,
|
|
148
|
+
schemaName,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
142
151
|
|
|
143
152
|
ref_long
|
|
144
153
|
= ref name:(__ name)? _ "{" _ body:ref_body _ "}" {
|
|
@@ -163,17 +172,19 @@ ref_short
|
|
|
163
172
|
}
|
|
164
173
|
|
|
165
174
|
ref_body
|
|
166
|
-
=
|
|
175
|
+
= field1:field_identifier sp+ relation:relation sp+ field2:field_identifier sp* ref_settings:RefSettings? {
|
|
167
176
|
const endpoints = [
|
|
168
177
|
{
|
|
169
|
-
|
|
170
|
-
|
|
178
|
+
schemaName: field1.schemaName,
|
|
179
|
+
tableName: field1.tableName,
|
|
180
|
+
fieldNames: field1.fieldNames,
|
|
171
181
|
relation: relation === ">" ? "*" : "1",
|
|
172
182
|
token: location()
|
|
173
183
|
},
|
|
174
184
|
{
|
|
175
|
-
|
|
176
|
-
|
|
185
|
+
schemaName: field2.schemaName,
|
|
186
|
+
tableName: field2.tableName,
|
|
187
|
+
fieldNames: field2.fieldNames,
|
|
177
188
|
relation: relation === "<" ? "*" : "1",
|
|
178
189
|
token: location()
|
|
179
190
|
}
|
|
@@ -224,7 +235,7 @@ OnDelete
|
|
|
224
235
|
|
|
225
236
|
// Tables
|
|
226
237
|
TableSyntax
|
|
227
|
-
= table sp+ name:name alias:alias_def? sp* table_settings:TableSettings? _ "{" body:TableBody "}" {
|
|
238
|
+
= table sp+ schemaName:schema_name? name:name alias:alias_def? sp* table_settings:TableSettings? _ "{" body:TableBody "}" {
|
|
228
239
|
let fields = body.fields || [];
|
|
229
240
|
let indexes = body.indexes || [];
|
|
230
241
|
// Handle list of partial inline_refs
|
|
@@ -234,12 +245,14 @@ TableSyntax
|
|
|
234
245
|
(field.inline_refs || []).forEach((iref) => {
|
|
235
246
|
const endpoints = [
|
|
236
247
|
{
|
|
248
|
+
schemaName: iref.schemaName,
|
|
237
249
|
tableName: iref.tableName,
|
|
238
250
|
fieldNames: iref.fieldNames,
|
|
239
251
|
relation: iref.relation === "<" ? "*" : "1",
|
|
240
252
|
token: iref.token
|
|
241
253
|
},
|
|
242
254
|
{
|
|
255
|
+
schemaName: schemaName,
|
|
243
256
|
tableName: name,
|
|
244
257
|
fieldNames: [field.name],
|
|
245
258
|
relation: iref.relation === ">" ? "*" : "1",
|
|
@@ -247,6 +260,7 @@ TableSyntax
|
|
|
247
260
|
}];
|
|
248
261
|
|
|
249
262
|
let ref = {
|
|
263
|
+
schemaName,
|
|
250
264
|
name: null, // no name
|
|
251
265
|
endpoints: endpoints,
|
|
252
266
|
token: iref.token
|
|
@@ -255,8 +269,21 @@ TableSyntax
|
|
|
255
269
|
})
|
|
256
270
|
});
|
|
257
271
|
|
|
272
|
+
if (alias) {
|
|
273
|
+
if (data.aliases.find(a => a.name === alias)) error(`Alias "${alias}" is already defined`);
|
|
274
|
+
data.aliases.push({
|
|
275
|
+
name: alias,
|
|
276
|
+
kind: 'table',
|
|
277
|
+
value: {
|
|
278
|
+
tableName: name,
|
|
279
|
+
schemaName: schemaName,
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
|
|
258
284
|
let res = {
|
|
259
285
|
name: name,
|
|
286
|
+
schemaName,
|
|
260
287
|
alias: alias,
|
|
261
288
|
fields: fields,
|
|
262
289
|
token: location(),
|
|
@@ -323,10 +350,13 @@ TableElement
|
|
|
323
350
|
}
|
|
324
351
|
|
|
325
352
|
Field
|
|
326
|
-
= _ name:name sp+ type:type constrains:(sp+ constrain)* field_settings:(sp+ FieldSettings)? sp* comment? newline {
|
|
353
|
+
= _ name:name sp+ typeSchemaName:schema_name? type:type constrains:(sp+ constrain)* field_settings:(sp+ FieldSettings)? sp* comment? newline {
|
|
327
354
|
const field = {
|
|
328
355
|
name: name,
|
|
329
|
-
type:
|
|
356
|
+
type: {
|
|
357
|
+
schemaName: typeSchemaName,
|
|
358
|
+
...type,
|
|
359
|
+
},
|
|
330
360
|
token: location(),
|
|
331
361
|
inline_refs: []
|
|
332
362
|
}
|
|
@@ -338,9 +368,10 @@ Field
|
|
|
338
368
|
}
|
|
339
369
|
|
|
340
370
|
EnumSyntax
|
|
341
|
-
= enum sp+ name:name _ "{" body: EnumBody "}" {
|
|
371
|
+
= enum sp+ schemaName:schema_name? name:name _ "{" body: EnumBody "}" {
|
|
342
372
|
return {
|
|
343
373
|
name: name,
|
|
374
|
+
schemaName,
|
|
344
375
|
token: location(),
|
|
345
376
|
values: body.enum_values
|
|
346
377
|
};
|
|
@@ -437,7 +468,7 @@ FieldSetting
|
|
|
437
468
|
/ _ a:"unique"i _ { return a }
|
|
438
469
|
/ _ a:"increment" _ { return a }
|
|
439
470
|
/ _ v:ObjectNote _ { return { type: 'note', value: v } }
|
|
440
|
-
/ _ v:RefInline { return { type: 'ref_inline', value: v } }
|
|
471
|
+
/ _ v:RefInline _ { return { type: 'ref_inline', value: v } }
|
|
441
472
|
/ _ v:Default _ {return {type: 'default', value: v} }
|
|
442
473
|
|
|
443
474
|
Indexes
|
|
@@ -523,10 +554,11 @@ ObjectNote
|
|
|
523
554
|
IndexType
|
|
524
555
|
= "type:"i _ val:(btree/hash) { return val }
|
|
525
556
|
RefInline
|
|
526
|
-
= "ref:" sp* relation:relation sp+
|
|
557
|
+
= "ref:" sp* relation:relation sp+ field:inline_field_identifier{
|
|
527
558
|
return {
|
|
528
|
-
|
|
529
|
-
|
|
559
|
+
schemaName: field.schemaName,
|
|
560
|
+
tableName: field.tableName,
|
|
561
|
+
fieldNames: [field.fieldName],
|
|
530
562
|
relation: relation,
|
|
531
563
|
token: location(),
|
|
532
564
|
}
|
|
@@ -584,6 +616,16 @@ name "valid name"
|
|
|
584
616
|
= c:(character+) { return c.join("") }
|
|
585
617
|
/ quote c:[^\"\n]+ quote { return c.join("") }
|
|
586
618
|
|
|
619
|
+
schema_name "schema name" = name:name "." { return name }
|
|
620
|
+
|
|
621
|
+
field_identifier =
|
|
622
|
+
schemaName:name "." tableName:name "." fieldNames:RefField { return { schemaName, tableName, fieldNames } } /
|
|
623
|
+
tableName:name "." fieldNames:RefField { return { schemaName: null, tableName, fieldNames } }
|
|
624
|
+
|
|
625
|
+
inline_field_identifier =
|
|
626
|
+
schemaName:name "." tableName:name "." fieldName:name { return { schemaName, tableName, fieldName } } /
|
|
627
|
+
tableName:name "." fieldName:name { return { schemaName: null, tableName, fieldName } }
|
|
628
|
+
|
|
587
629
|
type_name "valid name"
|
|
588
630
|
= c:(type_character+) { return c.join("") }
|
|
589
631
|
/ quote c:[^\"\n]+ quote { return c.join("") }
|
|
@@ -707,10 +749,10 @@ BooleanLiteral = boolean: ('true'i/'false'i/'null'i) {
|
|
|
707
749
|
value: boolean
|
|
708
750
|
};
|
|
709
751
|
}
|
|
710
|
-
NumberLiteral = number:(float / integer) {
|
|
752
|
+
NumberLiteral = minus:"-"? number:(float / integer) {
|
|
711
753
|
return {
|
|
712
754
|
type: 'number',
|
|
713
|
-
value: number
|
|
755
|
+
value: minus ? -number : number
|
|
714
756
|
};
|
|
715
757
|
}
|
|
716
758
|
float
|