@dbml/core 2.3.0 → 2.4.1
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 +17 -9
- package/lib/export/JsonExporter.js +1 -3
- package/lib/export/ModelExporter.js +1 -3
- package/lib/export/MysqlExporter.js +50 -42
- package/lib/export/PostgresExporter.js +64 -49
- package/lib/export/SqlServerExporter.js +52 -46
- package/lib/model_structure/database.js +73 -28
- package/lib/model_structure/dbState.js +1 -3
- package/lib/model_structure/element.js +13 -13
- package/lib/model_structure/endpoint.js +18 -14
- package/lib/model_structure/enum.js +18 -14
- package/lib/model_structure/enumValue.js +16 -12
- package/lib/model_structure/field.js +47 -13
- package/lib/model_structure/indexColumn.js +16 -12
- package/lib/model_structure/indexes.js +18 -14
- package/lib/model_structure/ref.js +19 -16
- package/lib/model_structure/schema.js +24 -36
- package/lib/model_structure/table.js +19 -15
- package/lib/model_structure/tableGroup.js +18 -14
- package/lib/model_structure/utils.js +5 -0
- package/lib/parse/Parser.js +2 -4
- package/lib/parse/buildParser.js +1 -3
- package/lib/parse/dbml/parser.pegjs +64 -20
- package/lib/parse/dbmlParser.js +1401 -899
- package/lib/parse/mssql/constraint_definition/actions.js +2 -2
- package/lib/parse/mssql/fk_definition/actions.js +10 -3
- package/lib/parse/mssql/keyword_parsers.js +12 -2
- package/lib/parse/mssql/statements/actions.js +37 -6
- package/lib/parse/mssql/statements/index.js +1 -1
- package/lib/parse/mssql/statements/statement_types/alter_table/actions.js +11 -5
- package/lib/parse/mssql/statements/statement_types/comments/actions.js +57 -0
- package/lib/parse/mssql/statements/statement_types/comments/index.js +97 -0
- package/lib/parse/mssql/statements/statement_types/create_index/actions.js +6 -1
- package/lib/parse/mssql/statements/statement_types/create_table/actions.js +12 -10
- package/lib/parse/mssql/statements/statement_types/index.js +4 -1
- package/lib/parse/mssql/utils.js +16 -1
- package/lib/parse/mysql/parser.pegjs +66 -40
- package/lib/parse/mysqlParser.js +534 -359
- package/lib/parse/postgresParser.js +15 -13
- package/lib/parse/postgresql/Base_rules.pegjs +45 -10
- package/lib/parse/postgresql/Commands/Alter_table/Alter_table.pegjs +49 -4
- package/lib/parse/postgresql/Commands/Comment.pegjs +18 -6
- 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/Commands/Ignore_syntax.pegjs +10 -1
- package/lib/parse/postgresql/InitializerUtils.pegjs +14 -2
- package/lib/parse/postgresql/Keywords.pegjs +5 -1
- package/lib/parse/postgresql/parser.pegjs +22 -8
- package/lib/parse/schemarbParser.js +3 -3
- package/package.json +2 -2
|
@@ -4,6 +4,15 @@
|
|
|
4
4
|
const refs = [];
|
|
5
5
|
const enums = [];
|
|
6
6
|
|
|
7
|
+
// TODO: support configurable default schema name other than 'public'
|
|
8
|
+
const findTable = (schemaName, tableName) => {
|
|
9
|
+
const realSchemaName = schemaName || 'public';
|
|
10
|
+
const table = tables.find(table => {
|
|
11
|
+
const targetSchemaName = table.schemaName || 'public';
|
|
12
|
+
return targetSchemaName === realSchemaName && table.name === tableName;
|
|
13
|
+
});
|
|
14
|
+
return table;
|
|
15
|
+
};
|
|
7
16
|
// intput:
|
|
8
17
|
// `
|
|
9
18
|
// 'created'
|
|
@@ -25,41 +34,26 @@ Rule = (Expr)* {
|
|
|
25
34
|
return {tables, refs, enums};
|
|
26
35
|
}
|
|
27
36
|
|
|
28
|
-
Expr =
|
|
37
|
+
Expr =
|
|
29
38
|
t:TableSyntax { tables.push(t) }
|
|
30
39
|
/AlterSyntax
|
|
31
40
|
/IndexSyntax
|
|
32
41
|
/IgnoreSyntax
|
|
33
42
|
/__
|
|
34
|
-
|
|
43
|
+
|
|
35
44
|
// TableSyntax: support "CREATE TABLE" syntax.
|
|
36
45
|
// Try to support as mush as possible syntax in MySQL offical documents.
|
|
37
46
|
// https://dev.mysql.com/doc/refman/8.0/en/create-table.html
|
|
38
|
-
// Return: table object: {name, fields, [,indexes]}
|
|
39
|
-
TableSyntax
|
|
40
|
-
= create_table (__ if_not_exist)? __
|
|
41
|
-
"(" _ body:TableBody _ ")" _ TableOptions? _ semicolon endline?
|
|
47
|
+
// Return: table object: {name, schemaName, fields, [,indexes]}
|
|
48
|
+
TableSyntax
|
|
49
|
+
= create_table (__ if_not_exist)? __ table_name:table_name _
|
|
50
|
+
"(" _ body:TableBody _ ")" _ options:TableOptions? _ semicolon endline?
|
|
42
51
|
{
|
|
43
52
|
const fields = body.fields;
|
|
44
53
|
const indexes = body.indexes;
|
|
45
|
-
|
|
54
|
+
const bodyRefs = body.refs;
|
|
55
|
+
|
|
46
56
|
fields.forEach((field)=>{
|
|
47
|
-
(field.inline_ref || []).forEach(ref => {
|
|
48
|
-
const endpoints = [
|
|
49
|
-
{
|
|
50
|
-
tableName: name,
|
|
51
|
-
fieldNames: [field.name],
|
|
52
|
-
relation: "*", //set by default
|
|
53
|
-
},
|
|
54
|
-
ref.endpoint,
|
|
55
|
-
]
|
|
56
|
-
refs.push({
|
|
57
|
-
endpoints,
|
|
58
|
-
onUpdate: ref.onUpdate,
|
|
59
|
-
onDelete: ref.onDelete,
|
|
60
|
-
});
|
|
61
|
-
})
|
|
62
|
-
|
|
63
57
|
// process enum: rename enum and push to array `enums`
|
|
64
58
|
if (field.type.type_name.toLowerCase() === 'enum') {
|
|
65
59
|
let enumValuesArr = field.type.args.split(/[\s\r\n\n]*,[\s\r\n\n]*/);
|
|
@@ -74,18 +68,28 @@ TableSyntax
|
|
|
74
68
|
});
|
|
75
69
|
|
|
76
70
|
const _enum = {
|
|
77
|
-
name: `${
|
|
71
|
+
name: `${table_name.schemaName
|
|
72
|
+
? `${table_name.schemaName}_` : ''}${table_name.name}_${field.name}_enum`,
|
|
78
73
|
values
|
|
79
74
|
};
|
|
80
75
|
enums.push(_enum);
|
|
81
76
|
field.type.type_name = _enum.name;
|
|
82
77
|
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
bodyRefs.forEach(ref => {
|
|
81
|
+
ref.endpoints[0].tableName = table_name.name;
|
|
82
|
+
ref.endpoints[0].schemaName = table_name.schemaName;
|
|
83
|
+
ref.endpoints[0].relation = '*';
|
|
84
|
+
refs.push(ref);
|
|
85
|
+
});
|
|
83
86
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
let res = {...table_name, fields};
|
|
88
|
+
|
|
89
|
+
if (options && options.comment) res.note = options.comment;
|
|
90
|
+
if (indexes) res.indexes = indexes;
|
|
91
|
+
// return statement
|
|
92
|
+
return res;
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
// TableBody: this is the part between parenthesis.
|
|
@@ -124,9 +128,9 @@ TableBody = _ lines:Line* _ {
|
|
|
124
128
|
onUpdate: key.onUpdate,
|
|
125
129
|
onDelete: key.onDelete,
|
|
126
130
|
});
|
|
127
|
-
})
|
|
131
|
+
});
|
|
128
132
|
|
|
129
|
-
return {fields, indexes}
|
|
133
|
+
return {fields, indexes, refs: fks}
|
|
130
134
|
}
|
|
131
135
|
|
|
132
136
|
// Line: is create_definition in MySQL documents.
|
|
@@ -158,7 +162,8 @@ FKSyntax = _ constraint:("CONSTRAINT"i _ name)? _ foreign_key _
|
|
|
158
162
|
relation: "*",
|
|
159
163
|
},
|
|
160
164
|
{
|
|
161
|
-
tableName: table2,
|
|
165
|
+
tableName: table2.name,
|
|
166
|
+
schemaName: table2.schemaName,
|
|
162
167
|
fieldNames: fields2,
|
|
163
168
|
relation: "1",
|
|
164
169
|
}
|
|
@@ -184,7 +189,7 @@ UniqueSyntax
|
|
|
184
189
|
// IndexInLineSyntax: Support "[UNIQUE] (INDEX/KEY) `indexName`(`field` (ASC/DESC)?)"
|
|
185
190
|
// "KEY is normally a synonym for INDEX".
|
|
186
191
|
IndexInLineSyntax = _ unique:index_in_line
|
|
187
|
-
_ name:name? _ type1:index_type?
|
|
192
|
+
_ name:name? _ type1:index_type? _
|
|
188
193
|
"(" _ columns:IndexColumnValues _")" IndexOption? type2:index_type?
|
|
189
194
|
{
|
|
190
195
|
const index = { columns };
|
|
@@ -304,16 +309,19 @@ CheckConstraintSyntax = _ ("CONSTRAINT"i name _)? "CHECK"i _ expression _ ("NOT"
|
|
|
304
309
|
// AlterSyntax: support "ALTER TABLE" syntax
|
|
305
310
|
// We will support ADD_COLUMN, ADD_FOREIGN_KEY, ADD_INDEX
|
|
306
311
|
// https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
|
|
307
|
-
AlterSyntax = alter_table _ table:
|
|
312
|
+
AlterSyntax = alter_table _ table:table_name _
|
|
308
313
|
options:(AddOptions/ChangeOptions/DropOptions) _
|
|
309
314
|
semicolon
|
|
310
315
|
{
|
|
311
316
|
const fks = _.flatten(options.filter(o => o.type === "add_fk").map(o => o.fks));
|
|
312
|
-
fks.forEach(fk => {
|
|
317
|
+
fks.forEach(fk => {
|
|
318
|
+
fk.endpoints[0].tableName = table.name;
|
|
319
|
+
fk.endpoints[0].schemaName = table.schemaName;
|
|
320
|
+
});
|
|
313
321
|
refs.push(...fks)
|
|
314
322
|
|
|
315
323
|
const pks = _.flatten(options.filter(o => o.type === "add_pk").map(o => o.pks));
|
|
316
|
-
const tableAlter =
|
|
324
|
+
const tableAlter = findTable(table.schemaName, table.name);
|
|
317
325
|
|
|
318
326
|
const index = {
|
|
319
327
|
columns: pks.map(field => ({
|
|
@@ -350,7 +358,7 @@ DropOptions = "DROP"i [^;]
|
|
|
350
358
|
|
|
351
359
|
// IndexSyntax: support "CREATE INDEX" syntax
|
|
352
360
|
IndexSyntax = constraint:create_index _ indexName:name _
|
|
353
|
-
"ON"i _ tableName:
|
|
361
|
+
"ON"i _ tableName:table_name _ indexType:index_type? _
|
|
354
362
|
columns: IndexColumn _
|
|
355
363
|
option:IndexOption? semicolon
|
|
356
364
|
{
|
|
@@ -367,7 +375,8 @@ IndexSyntax = constraint:create_index _ indexName:name _
|
|
|
367
375
|
if(type)
|
|
368
376
|
index.type = type;
|
|
369
377
|
|
|
370
|
-
const table =
|
|
378
|
+
const table = findTable(tableName.schemaName, tableName.name);
|
|
379
|
+
|
|
371
380
|
if(table.indexes) {
|
|
372
381
|
table.indexes.push(index);
|
|
373
382
|
} else {
|
|
@@ -409,6 +418,7 @@ IgnoreSyntax
|
|
|
409
418
|
/ BeginSyntax
|
|
410
419
|
/ CommitSyntax
|
|
411
420
|
/ RollbackSyntax
|
|
421
|
+
/ "ALTER"i _ (comment / !(semicolon) .)*
|
|
412
422
|
) semicolon newline?
|
|
413
423
|
|
|
414
424
|
// InsertSyntax: "INSERTO INTO" syntax
|
|
@@ -467,8 +477,24 @@ index_type "index type" = "USING"i _ type:("BTREE"i/"HASH"i) { return type.toUpp
|
|
|
467
477
|
name "valid name"
|
|
468
478
|
= c:(character)+ { return c.join("") }
|
|
469
479
|
/ quote c:[^`]+ quote { return c.join("") }
|
|
470
|
-
|
|
471
|
-
|
|
480
|
+
|
|
481
|
+
path_name = names:(name _ "." _)* {
|
|
482
|
+
let dbName = null;
|
|
483
|
+
let schemaName = null;
|
|
484
|
+
if (names && names.length > 0) {
|
|
485
|
+
if (names.length === 1) schemaName = names[0][0];
|
|
486
|
+
else {
|
|
487
|
+
dbName = names[0][0];
|
|
488
|
+
schemaName = names[1][0];
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return { dbName, schemaName }
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
table_name "valid table name" = pathName:path_name name:name {
|
|
495
|
+
return { ...pathName, name }
|
|
496
|
+
}
|
|
497
|
+
|
|
472
498
|
type "type" = c:type_name { return c }
|
|
473
499
|
/ c:name { return { type_name: c } }
|
|
474
500
|
type_name = type_name:name _ args:("(" _ expression _ ")")? {
|