@dbml/core 2.2.0 → 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 +15 -4
- 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 +0 -1
- 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 +12 -10
- package/lib/parse/mssql/utils.js +16 -1
- package/lib/parse/mysql/parser.pegjs +57 -34
- package/lib/parse/mysqlParser.js +270 -218
- package/lib/parse/postgresParser.js +12 -10
- package/lib/parse/postgresql/Base_rules.pegjs +45 -10
- 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/lib/parse/schemarbParser.js +3 -3
- package/package.json +5 -3
- package/types/export/ModelExporter.d.ts +5 -0
- package/types/export/index.d.ts +5 -0
- package/types/import/index.d.ts +5 -0
- package/types/index.d.ts +5 -0
- package/types/model_structure/database.d.ts +195 -0
- package/types/model_structure/dbState.d.ts +14 -0
- package/types/model_structure/element.d.ts +21 -0
- package/types/model_structure/endpoint.d.ts +55 -0
- package/types/model_structure/enum.d.ts +67 -0
- package/types/model_structure/enumValue.d.ts +39 -0
- package/types/model_structure/field.d.ts +77 -0
- package/types/model_structure/indexColumn.d.ts +37 -0
- package/types/model_structure/indexes.d.ts +74 -0
- package/types/model_structure/ref.d.ts +66 -0
- package/types/model_structure/schema.d.ts +188 -0
- package/types/model_structure/table.d.ts +118 -0
- package/types/model_structure/tableGroup.d.ts +54 -0
- package/types/parse/Parser.d.ts +11 -0
- package/lib/export/Exporter.js +0 -80
- package/lib/export/SchemaExporter.js +0 -99
- package/lib/parse/dbml/multiline_content.txt +0 -7
- package/lib/parse/dbml/multiline_string.pegjs +0 -17
- package/lib/parse/dbml/parser1.json +0 -219
- package/lib/parse/dbml/parser1.pegjs +0 -702
- package/lib/parse/dbml/parser1_content.txt +0 -29
- package/lib/parse/dbml/test_multiline.js +0 -53
- package/lib/parse/dbml/test_parser1.js +0 -16
- package/lib/parse/mssql/base_utils.js +0 -28
- package/lib/parse/mssql/composite_parsers.js +0 -109
- package/lib/parse/mssql/statement_types/alter_table/actions.js +0 -29
- package/lib/parse/mssql/statement_types/alter_table/add/actions.js +0 -8
- package/lib/parse/mssql/statement_types/alter_table/add/index.js +0 -58
- package/lib/parse/mssql/statement_types/alter_table/index.js +0 -32
- package/lib/parse/mssql/statement_types/create_index/actions.js +0 -25
- package/lib/parse/mssql/statement_types/create_index/index.js +0 -50
- package/lib/parse/mssql/statement_types/create_table/actions.js +0 -95
- package/lib/parse/mssql/statement_types/create_table/index.js +0 -50
- package/lib/parse/mssql/statement_types/index.js +0 -13
- package/lib/schema/element.js +0 -84
- package/lib/schema/endpoint.js +0 -102
- package/lib/schema/enum.js +0 -102
- package/lib/schema/enumValue.js +0 -70
- package/lib/schema/field.js +0 -104
- package/lib/schema/indexes.js +0 -74
- package/lib/schema/ref.js +0 -93
- package/lib/schema/schema.js +0 -245
- package/lib/schema/table.js +0 -163
- package/lib/schema/tableGroup.js +0 -93
|
@@ -1,702 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
const data = {
|
|
3
|
-
schemas: [],
|
|
4
|
-
tables: [],
|
|
5
|
-
refs: [],
|
|
6
|
-
enums: [],
|
|
7
|
-
tableGroups: [],
|
|
8
|
-
project: {},
|
|
9
|
-
};
|
|
10
|
-
let projectCnt = 0;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
rules
|
|
14
|
-
= (expr)* {
|
|
15
|
-
return data;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
expr
|
|
19
|
-
= t:TableSyntax { data.tables.push(t) }
|
|
20
|
-
/ r:RefSyntax { data.refs.push(r) }
|
|
21
|
-
/ e:EnumSyntax { data.enums.push(e) }
|
|
22
|
-
/ tg:TableGroupSyntax { data.tableGroups.push(tg) }
|
|
23
|
-
/ p: ProjectSyntax {
|
|
24
|
-
projectCnt += 1;
|
|
25
|
-
if (projectCnt > 1) {
|
|
26
|
-
error('Project is already defined');
|
|
27
|
-
}
|
|
28
|
-
data.project = p;
|
|
29
|
-
data.tables = data.tables.concat(p.tables);
|
|
30
|
-
data.refs = data.refs.concat(p.refs);
|
|
31
|
-
data.enums = data.enums.concat(p.enums);
|
|
32
|
-
data.tableGroups = data.tableGroups.concat(p.tableGroups);
|
|
33
|
-
}
|
|
34
|
-
/ __
|
|
35
|
-
|
|
36
|
-
ProjectSyntax
|
|
37
|
-
= project name:(__ name)? _ "{" _ body:ProjectBody _ "}" {
|
|
38
|
-
return {
|
|
39
|
-
name: name ? name[1] : null,
|
|
40
|
-
...body
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
ProjectBody
|
|
45
|
-
= _ elements: ProjectElement* _ {
|
|
46
|
-
const tables = [];
|
|
47
|
-
const refs = [];
|
|
48
|
-
const enums = [];
|
|
49
|
-
const tableGroups = [];
|
|
50
|
-
let note = null;
|
|
51
|
-
const projectFields = {};
|
|
52
|
-
|
|
53
|
-
elements.forEach(ele => {
|
|
54
|
-
if (ele.type === 'table') {
|
|
55
|
-
tables.push(ele.value);
|
|
56
|
-
} else if (ele.type === 'ref') {
|
|
57
|
-
refs.push(ele.value);
|
|
58
|
-
} else if (ele.type === 'enum') {
|
|
59
|
-
enums.push(ele.value);
|
|
60
|
-
} else if (ele.type === 'table_group') {
|
|
61
|
-
tableGroups.push(ele.value);
|
|
62
|
-
} else if (ele.type === 'note') {
|
|
63
|
-
note = ele.value;
|
|
64
|
-
} else {
|
|
65
|
-
projectFields[ele.value.name] = ele.value.value;
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
tables,
|
|
71
|
-
refs,
|
|
72
|
-
enums,
|
|
73
|
-
tableGroups,
|
|
74
|
-
note,
|
|
75
|
-
...projectFields
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
ProjectElement
|
|
80
|
-
= _ note: ObjectNoteElement _ {
|
|
81
|
-
return {
|
|
82
|
-
type: 'note',
|
|
83
|
-
value: note
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/ _ t: TableSyntax _ {
|
|
87
|
-
return {
|
|
88
|
-
type: 'table',
|
|
89
|
-
value: t
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/ _ r: RefSyntax _ {
|
|
93
|
-
return {
|
|
94
|
-
type: 'ref',
|
|
95
|
-
value: r
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/ _ e: EnumSyntax _ {
|
|
99
|
-
return {
|
|
100
|
-
type: 'enum',
|
|
101
|
-
value: e
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/ tg: TableGroupSyntax _ {
|
|
105
|
-
return {
|
|
106
|
-
type: 'table_group',
|
|
107
|
-
value: tg
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
/ _ element: ProjectField _ {
|
|
111
|
-
return {
|
|
112
|
-
type: 'element',
|
|
113
|
-
value: element
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
ProjectField
|
|
118
|
-
= name:name _ ":" _ value: StringLiteral {
|
|
119
|
-
return {
|
|
120
|
-
name,
|
|
121
|
-
value: value.value
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
TableGroupSyntax = table_group sp+ name:name _ "{" _ body:table_group_body _ "}" {
|
|
126
|
-
return {
|
|
127
|
-
name: name,
|
|
128
|
-
tables: body,
|
|
129
|
-
token: location()
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
table_group_body = tables:(name __)* {
|
|
134
|
-
return tables.map(t => ({
|
|
135
|
-
name: t[0]
|
|
136
|
-
}));
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// References
|
|
140
|
-
RefSyntax
|
|
141
|
-
= r: (ref_long / ref_short) { return r; }
|
|
142
|
-
|
|
143
|
-
ref_long
|
|
144
|
-
= ref name:(__ name)? _ "{" _ body:ref_body _ "}" {
|
|
145
|
-
const ref = {
|
|
146
|
-
name: name? name[1] : null,
|
|
147
|
-
endpoints: body.endpoints,
|
|
148
|
-
token: location()
|
|
149
|
-
};
|
|
150
|
-
Object.assign(ref, body.settings);
|
|
151
|
-
return ref;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
ref_short
|
|
155
|
-
= ref name:(sp+ name)? sp* ":" sp* body:ref_body {
|
|
156
|
-
const ref = {
|
|
157
|
-
name: name? name[1] : null,
|
|
158
|
-
endpoints: body.endpoints,
|
|
159
|
-
token: location()
|
|
160
|
-
};
|
|
161
|
-
Object.assign(ref, body.settings);
|
|
162
|
-
return ref;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
ref_body
|
|
166
|
-
= table1:name "." field1:name sp+ relation:relation sp+ table2:name "." field2:name sp* ref_settings:RefSettings? {
|
|
167
|
-
const endpoints = [
|
|
168
|
-
{
|
|
169
|
-
tableName: table1,
|
|
170
|
-
fieldName: field1,
|
|
171
|
-
relation: relation === ">" ? "*" : "1",
|
|
172
|
-
token: location()
|
|
173
|
-
},
|
|
174
|
-
{
|
|
175
|
-
tableName: table2,
|
|
176
|
-
fieldName: field2,
|
|
177
|
-
relation: relation === "<" ? "*" : "1",
|
|
178
|
-
token: location()
|
|
179
|
-
}
|
|
180
|
-
];
|
|
181
|
-
return {
|
|
182
|
-
endpoints: endpoints,
|
|
183
|
-
settings: ref_settings
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
RefSettings
|
|
188
|
-
= "[" first:RefSetting rest:(Comma RefSetting)* "]" {
|
|
189
|
-
let arrSettings = [first].concat(rest.map(el => el[1]));
|
|
190
|
-
let res = {};
|
|
191
|
-
arrSettings.forEach((ele) => {
|
|
192
|
-
if (ele.type === "update") {
|
|
193
|
-
res.onUpdate = ele.value;
|
|
194
|
-
}
|
|
195
|
-
if (ele.type === "delete") {
|
|
196
|
-
res.onDelete = ele.value;
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
return res;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
RefSetting
|
|
203
|
-
= _ v:OnUpdate _ { return { type: 'update', value: v } }
|
|
204
|
-
/ _ v:OnDelete _ { return { type: 'delete', value: v } }
|
|
205
|
-
OnUpdate
|
|
206
|
-
= "update:"i _ val:(no_action/restrict/cascade/set_null/set_default) { return val }
|
|
207
|
-
OnDelete
|
|
208
|
-
= "delete:"i _ val:(no_action/restrict/cascade/set_null/set_default) { return val }
|
|
209
|
-
|
|
210
|
-
// Tables
|
|
211
|
-
TableSyntax
|
|
212
|
-
= table sp+ name:name alias:alias_def? sp* table_settings:TableSettings? sp* "{" body:TableBody "}" {
|
|
213
|
-
let fields = body.fields || [];
|
|
214
|
-
let indexes = body.indexes || [];
|
|
215
|
-
// Handle list of partial inline_refs
|
|
216
|
-
let refs = []
|
|
217
|
-
|
|
218
|
-
fields.forEach((field) => {
|
|
219
|
-
(field.inline_refs || []).forEach((iref) => {
|
|
220
|
-
const endpoints = [
|
|
221
|
-
{
|
|
222
|
-
tableName: iref.tableName,
|
|
223
|
-
fieldName: iref.fieldName,
|
|
224
|
-
relation: iref.relation === "<" ? "*" : "1",
|
|
225
|
-
token: iref.token
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
tableName: name,
|
|
229
|
-
fieldName: field.name,
|
|
230
|
-
relation: iref.relation === ">" ? "*" : "1",
|
|
231
|
-
token: iref.token
|
|
232
|
-
}];
|
|
233
|
-
|
|
234
|
-
let ref = {
|
|
235
|
-
name: null, // no name
|
|
236
|
-
endpoints: endpoints,
|
|
237
|
-
token: iref.token
|
|
238
|
-
}
|
|
239
|
-
data.refs.push(ref);
|
|
240
|
-
})
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
let res = {
|
|
244
|
-
name: name,
|
|
245
|
-
alias: alias,
|
|
246
|
-
fields: fields,
|
|
247
|
-
token: location(),
|
|
248
|
-
indexes: indexes,
|
|
249
|
-
...table_settings,
|
|
250
|
-
}
|
|
251
|
-
if (body.note) {
|
|
252
|
-
res = {
|
|
253
|
-
...res,
|
|
254
|
-
note: body.note
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return res;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
TableBody
|
|
261
|
-
= _ fields: Field+ _ elements: TableElement* _ {
|
|
262
|
-
// concat all indexes
|
|
263
|
-
const indexes = _.flatMap(elements.filter(ele => ele.type === 'indexes'), (ele => ele.value));
|
|
264
|
-
// pick the last note
|
|
265
|
-
const note = elements.slice().reverse().find(ele => ele.type === 'note');
|
|
266
|
-
|
|
267
|
-
// process field for composite primary key:
|
|
268
|
-
const primaryKeyList = [];
|
|
269
|
-
fields.forEach(field => {
|
|
270
|
-
if (field.pk) {
|
|
271
|
-
primaryKeyList.push(field);
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
if (primaryKeyList.length > 1) {
|
|
275
|
-
const columns = primaryKeyList.map(field => ({
|
|
276
|
-
value: field.name,
|
|
277
|
-
type: 'column'
|
|
278
|
-
}));
|
|
279
|
-
// remove property `pk` for each field in this list
|
|
280
|
-
primaryKeyList.forEach(field => delete field.pk);
|
|
281
|
-
|
|
282
|
-
indexes.push({
|
|
283
|
-
columns: columns,
|
|
284
|
-
token: _.head(primaryKeyList).token,
|
|
285
|
-
pk: true
|
|
286
|
-
})
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return {
|
|
290
|
-
fields,
|
|
291
|
-
indexes,
|
|
292
|
-
note: note ? note.value : null
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
TableElement
|
|
297
|
-
= _ indexes: Indexes _ {
|
|
298
|
-
return {
|
|
299
|
-
type: 'indexes',
|
|
300
|
-
value: indexes
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
/ _ note: ObjectNoteElement _ {
|
|
304
|
-
return {
|
|
305
|
-
type: 'note',
|
|
306
|
-
value: note
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
Field
|
|
311
|
-
= _ name:name sp+ type:type constrains:(sp+ constrain)* field_settings:(sp+ FieldSettings)? sp* comment? newline {
|
|
312
|
-
const field = {
|
|
313
|
-
name: name,
|
|
314
|
-
type: type,
|
|
315
|
-
token: location(),
|
|
316
|
-
inline_refs: []
|
|
317
|
-
}
|
|
318
|
-
Object.assign(field, ...constrains.map(c => c[1]));
|
|
319
|
-
if (field_settings) {
|
|
320
|
-
Object.assign(field, field_settings[1]);
|
|
321
|
-
}
|
|
322
|
-
return field;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
EnumSyntax
|
|
326
|
-
= enum sp+ name:name _ "{" body: EnumBody "}" {
|
|
327
|
-
return {
|
|
328
|
-
name: name,
|
|
329
|
-
token: location(),
|
|
330
|
-
values: body.enum_values
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
EnumBody
|
|
335
|
-
= _ enum_values: EnumValue+ _ {
|
|
336
|
-
return { enum_values: enum_values }
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
EnumValue
|
|
340
|
-
= _ name:name sp* enum_setting:EnumSetting? sp* comment? newline {
|
|
341
|
-
const enum_value = {
|
|
342
|
-
name: name,
|
|
343
|
-
token: location(),
|
|
344
|
-
}
|
|
345
|
-
Object.assign(enum_value, enum_setting);
|
|
346
|
-
return enum_value;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
EnumSetting
|
|
350
|
-
= "[" _ v:ObjectNote _ "]" {
|
|
351
|
-
return {
|
|
352
|
-
note: v
|
|
353
|
-
};
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// simply an array of FieldSetting(s)
|
|
357
|
-
FieldSettings
|
|
358
|
-
= "[" first:FieldSetting rest:(Comma FieldSetting)* "]" {
|
|
359
|
-
let arrSettings = [first].concat(rest.map(el => el[1]));
|
|
360
|
-
let res = {
|
|
361
|
-
inline_refs: [],
|
|
362
|
-
};
|
|
363
|
-
arrSettings.forEach((ele) => {
|
|
364
|
-
if (typeof ele === 'string') {
|
|
365
|
-
if (ele.toLowerCase() == "not null") {
|
|
366
|
-
res.not_null = true;
|
|
367
|
-
}
|
|
368
|
-
if (ele.toLowerCase() == "null") {
|
|
369
|
-
res.not_null = false;
|
|
370
|
-
}
|
|
371
|
-
if (ele.toLowerCase() == "primary key" || ele.toLowerCase() == 'pk') {
|
|
372
|
-
res.pk = true;
|
|
373
|
-
}
|
|
374
|
-
if (ele.toLowerCase() == "unique") {
|
|
375
|
-
res.unique = true;
|
|
376
|
-
}
|
|
377
|
-
if (ele.toLowerCase() == "increment") {
|
|
378
|
-
res.increment = true;
|
|
379
|
-
}
|
|
380
|
-
} else {
|
|
381
|
-
if (ele.type === "note") {
|
|
382
|
-
res.note = ele.value;
|
|
383
|
-
}
|
|
384
|
-
if (ele.type === "ref_inline") {
|
|
385
|
-
res.inline_refs.push(ele.value);
|
|
386
|
-
}
|
|
387
|
-
if (ele.type === "default") {
|
|
388
|
-
res.dbdefault = ele.value;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
return res;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
TableSettings
|
|
396
|
-
= "[" first:TableSetting rest:(Comma TableSetting)* "]" {
|
|
397
|
-
let settings = [first, ...rest.map(el => el[1])];
|
|
398
|
-
const result = {};
|
|
399
|
-
settings.forEach((el) => {
|
|
400
|
-
if (typeof el === 'string') {
|
|
401
|
-
if (el.startsWith('#')) {
|
|
402
|
-
result.headerColor = el.toUpperCase();
|
|
403
|
-
}
|
|
404
|
-
} else {
|
|
405
|
-
if (el.type === "note") {
|
|
406
|
-
result.note = el.value;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
});
|
|
410
|
-
return result;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
TableSetting
|
|
414
|
-
= _ v:ObjectNote _ { return { type: 'note', value: v } }
|
|
415
|
-
/ _ c: HeaderColor _ { return c }
|
|
416
|
-
|
|
417
|
-
FieldSetting
|
|
418
|
-
= _ a:"not null"i _ { return a }
|
|
419
|
-
/ _ a:"null"i _ { return a }
|
|
420
|
-
/ _ a:"primary key"i _ { return a }
|
|
421
|
-
/ _ a:"pk"i _ { return a }
|
|
422
|
-
/ _ a:"unique"i _ { return a }
|
|
423
|
-
/ _ a:"increment" _ { return a }
|
|
424
|
-
/ _ v:ObjectNote _ { return { type: 'note', value: v } }
|
|
425
|
-
/ _ v:RefInline { return { type: 'ref_inline', value: v } }
|
|
426
|
-
/ _ v:Default _ {return {type: 'default', value: v} }
|
|
427
|
-
|
|
428
|
-
Indexes
|
|
429
|
-
= _ indexes _ "{" body:IndexesBody "}"
|
|
430
|
-
{
|
|
431
|
-
return body;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
IndexesBody = _ index: Index+ _ {
|
|
435
|
-
return index;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
Index
|
|
439
|
-
= _ index:(SingleIndexSyntax/CompositeIndexSyntax) _ { return index }
|
|
440
|
-
|
|
441
|
-
SingleIndexSyntax = _ syntax:SingleIndex sp* index_settings:IndexSettings? {
|
|
442
|
-
const index = {
|
|
443
|
-
columns: [syntax],
|
|
444
|
-
token: location()
|
|
445
|
-
};
|
|
446
|
-
Object.assign(index, index_settings);
|
|
447
|
-
return index;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
// CompositeIndexSyntax includes normal composite index and composite primary key
|
|
451
|
-
CompositeIndexSyntax = _ syntax:CompositeIndex sp* index_settings:IndexSettings? {
|
|
452
|
-
const index = {
|
|
453
|
-
columns: syntax,
|
|
454
|
-
token: location()
|
|
455
|
-
};
|
|
456
|
-
Object.assign(index, index_settings);
|
|
457
|
-
return index;
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
SingleIndex
|
|
461
|
-
= column:name sp* {
|
|
462
|
-
const singleIndex = {
|
|
463
|
-
value: column,
|
|
464
|
-
type: 'column'
|
|
465
|
-
}
|
|
466
|
-
return singleIndex
|
|
467
|
-
}
|
|
468
|
-
/ Expression
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
Expression
|
|
472
|
-
= "`" text:([^\`])* "`" { return { value: text.join(""), type: 'expression'} }
|
|
473
|
-
|
|
474
|
-
CompositeIndex
|
|
475
|
-
= "(" sp* first:SingleIndex rest:(Comma sp* SingleIndex)* ")" {
|
|
476
|
-
let arrIndex = [first].concat(rest.map(el => el[2]));
|
|
477
|
-
return arrIndex;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
IndexSettings
|
|
481
|
-
= "[" sp* pk sp* "]" { return { pk: true } }
|
|
482
|
-
/ "[" first:IndexSetting rest:(Comma IndexSetting)* "]" {
|
|
483
|
-
let arrSettings = [first].concat(rest.map(el => el[1]));
|
|
484
|
-
let res = {};
|
|
485
|
-
arrSettings.forEach((ele) => {
|
|
486
|
-
if (typeof ele === 'string') {
|
|
487
|
-
if (ele.toLowerCase() == "unique") {
|
|
488
|
-
res.unique = true;
|
|
489
|
-
}
|
|
490
|
-
} else {
|
|
491
|
-
if (ele.type === "name") {
|
|
492
|
-
res.name = ele.value;
|
|
493
|
-
}
|
|
494
|
-
if (ele.type === "type") {
|
|
495
|
-
res.type = ele.value;
|
|
496
|
-
}
|
|
497
|
-
if (ele.type === "default") {
|
|
498
|
-
res.dbdefault = ele.value;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
});
|
|
502
|
-
return res;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
IndexSetting
|
|
506
|
-
=
|
|
507
|
-
_ a:"unique"i _ { return a }
|
|
508
|
-
/ _ v:IndexName _ { return { type: 'name', value: v } }
|
|
509
|
-
/ _ v:IndexType _ { return { type: 'type', value: v } }
|
|
510
|
-
IndexName
|
|
511
|
-
= "name:"i _ val:StringLiteral { return val.value }
|
|
512
|
-
ObjectNoteElement
|
|
513
|
-
= note: ObjectNote { return note }
|
|
514
|
-
/ "note"i _ "{" _ val:StringLiteral _ "}" { return val.value }
|
|
515
|
-
ObjectNote
|
|
516
|
-
= "note:"i _ val:StringLiteral { return val.value }
|
|
517
|
-
IndexType
|
|
518
|
-
= "type:"i _ val:(btree/hash) { return val }
|
|
519
|
-
RefInline
|
|
520
|
-
= "ref:" sp* relation:relation sp+ table2:name "." field2:name {
|
|
521
|
-
return {
|
|
522
|
-
tableName: table2,
|
|
523
|
-
fieldName: field2,
|
|
524
|
-
relation: relation,
|
|
525
|
-
token: location(),
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
Default
|
|
529
|
-
= "default:"i _ val: DefaultVal {return val}
|
|
530
|
-
|
|
531
|
-
DefaultVal = (StringLiteral / Expression / BooleanLiteral/ NumberLiteral)
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
alias_def
|
|
535
|
-
= sp+ "as" sp+ alias:name {
|
|
536
|
-
return alias
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
HeaderColor
|
|
540
|
-
= _ header_color ":" _ s:sharp color:hex_color _ {return s + color.join('')}
|
|
541
|
-
|
|
542
|
-
hex_color
|
|
543
|
-
= six_char / three_char
|
|
544
|
-
|
|
545
|
-
three_char
|
|
546
|
-
= hex_char hex_char hex_char
|
|
547
|
-
|
|
548
|
-
six_char
|
|
549
|
-
= hex_char hex_char hex_char hex_char hex_char hex_char
|
|
550
|
-
|
|
551
|
-
// To be deprecated
|
|
552
|
-
constrain
|
|
553
|
-
= unique
|
|
554
|
-
/ pk
|
|
555
|
-
|
|
556
|
-
// Keywords
|
|
557
|
-
project "project" = "project"i
|
|
558
|
-
table "table" = "table"i
|
|
559
|
-
as = "as"i
|
|
560
|
-
ref "references" = "ref"i
|
|
561
|
-
unique "unique" = "unique"i { return {unique: true} }
|
|
562
|
-
pk "PK" = "pk"i {return {pk: true}}
|
|
563
|
-
indexes "indexes" = "indexes"i
|
|
564
|
-
btree "btree" = "btree"i
|
|
565
|
-
hash "hash" = "hash"i
|
|
566
|
-
enum "enum" = "enum"i
|
|
567
|
-
header_color = "headercolor"i
|
|
568
|
-
table_group "Table Group" = "TableGroup"i
|
|
569
|
-
no_action "no action" = "no action"i
|
|
570
|
-
restrict "restrict" = "restrict"i
|
|
571
|
-
cascade "cascade" = "cascade"i
|
|
572
|
-
set_null "set null" = "set null"i
|
|
573
|
-
set_default "set default" = "set default"i
|
|
574
|
-
|
|
575
|
-
// Commonly used tokens
|
|
576
|
-
relation ">, - or <" = [>\-<]
|
|
577
|
-
name "valid name"
|
|
578
|
-
= c:(character+) { return c.join("") }
|
|
579
|
-
/ quote c:[^\"\n]+ quote { return c.join("") }
|
|
580
|
-
|
|
581
|
-
type_name "valid name"
|
|
582
|
-
= c:(type_character+) { return c.join("") }
|
|
583
|
-
/ quote c:[^\"\n]+ quote { return c.join("") }
|
|
584
|
-
|
|
585
|
-
type "type" = type_name:type_name args:(sp* "(" sp* expression sp* ")")? {
|
|
586
|
-
args = args ? args[3] : null;
|
|
587
|
-
|
|
588
|
-
if (type_name.toLowerCase() !== 'enum') {
|
|
589
|
-
type_name = args ? type_name + '(' + args + ')' : type_name;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
return {
|
|
593
|
-
type_name,
|
|
594
|
-
args
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
expression "expression" = factors:factor* {
|
|
598
|
-
return _.flattenDeep(factors).join("");
|
|
599
|
-
}
|
|
600
|
-
factor = factors:(character+ sp* "(" expression ")"
|
|
601
|
-
/ "(" expression ")"
|
|
602
|
-
/ (exprCharNoCommaSpace+ &(sp*/","/");"/endline");")) / exprChar+ &.) {
|
|
603
|
-
return _.flattenDeep(factors).join("");
|
|
604
|
-
}
|
|
605
|
-
exprChar = [\',.a-z0-9_+-\`]i
|
|
606
|
-
/ sp
|
|
607
|
-
/ newline
|
|
608
|
-
/ tab
|
|
609
|
-
exprCharNoCommaSpace = [\'.a-z0-9_+-]i
|
|
610
|
-
allowed_chars = (! ('{'/ '}'/ whitespace_quote)) . {return text()}
|
|
611
|
-
type_character = character / [\[\]]
|
|
612
|
-
character "letter, number or underscore" = [a-z0-9_]i
|
|
613
|
-
|
|
614
|
-
hex_char = c:[0-9a-fA-F] {return c.toLowerCase()}
|
|
615
|
-
quote = "\""
|
|
616
|
-
|
|
617
|
-
// Ignored
|
|
618
|
-
_ = (comment/whitespace)*
|
|
619
|
-
__ = (comment/whitespace)+
|
|
620
|
-
|
|
621
|
-
endline "endline" = sp* newline
|
|
622
|
-
tab = "\t"
|
|
623
|
-
comment "comment" = "//" [^\n]*
|
|
624
|
-
newline "newline" = "\r\n" / "\n"
|
|
625
|
-
whitespace "whitespace" = [ \t\r\n\r]
|
|
626
|
-
whitespace_quote "whitespace" = [ \t\r\n\r\"]
|
|
627
|
-
sp = " "
|
|
628
|
-
Comma = ","
|
|
629
|
-
sharp = "#" {return "#"}
|
|
630
|
-
|
|
631
|
-
// Copied from https://github.com/pegjs/pegjs/issues/292
|
|
632
|
-
StringLiteral "string"
|
|
633
|
-
= '"' chars:DoubleStringCharacter* '"' {
|
|
634
|
-
return { value: chars.join(''), type: 'string' } ;
|
|
635
|
-
}
|
|
636
|
-
/ "'''" chars: MultiLineStringCharacter* "'''" {
|
|
637
|
-
let str = chars.join('');
|
|
638
|
-
let lines = str.split(/\n|\r\n?/);
|
|
639
|
-
|
|
640
|
-
const leadingSpaces = (str) => {
|
|
641
|
-
let i = 0;
|
|
642
|
-
while (i < str.length && str[i] === ' ') {
|
|
643
|
-
i += 1;
|
|
644
|
-
}
|
|
645
|
-
return i;
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
const minLeadingSpaces = lines.filter(line => line.replace(/\s+/g, ''))
|
|
649
|
-
.reduce((acc, cur) => Math.min(acc, leadingSpaces(cur)), Number.MAX_SAFE_INTEGER);
|
|
650
|
-
lines = lines.map(line => line ? line.slice(minLeadingSpaces) : line);
|
|
651
|
-
|
|
652
|
-
const countLeadingEmptyLine = (lines) => {
|
|
653
|
-
let i = 0;
|
|
654
|
-
while (i < lines.length && !lines[i].replace(/\s+/g, '')) {
|
|
655
|
-
i += 1;
|
|
656
|
-
}
|
|
657
|
-
return i;
|
|
658
|
-
}
|
|
659
|
-
lines.splice(0, countLeadingEmptyLine(lines));
|
|
660
|
-
lines.splice(lines.length - countLeadingEmptyLine(lines.slice().reverse()));
|
|
661
|
-
|
|
662
|
-
const finalStr = lines.join('\n');
|
|
663
|
-
return { value: finalStr, type: 'string' } ;
|
|
664
|
-
}
|
|
665
|
-
/ "'" chars:SingleStringCharacter* "'" {
|
|
666
|
-
return { value: chars.join(''), type: 'string' } ;
|
|
667
|
-
}
|
|
668
|
-
DoubleStringCharacter
|
|
669
|
-
= '\\' '"' { return '"'; }
|
|
670
|
-
/ !'"' SourceCharacter { return text(); }
|
|
671
|
-
|
|
672
|
-
SingleStringCharacter
|
|
673
|
-
= "\\'" { return "'"; }
|
|
674
|
-
/ !"'" SourceCharacter { return text(); }
|
|
675
|
-
MultiLineStringCharacter
|
|
676
|
-
= "\\'" { return "'"; }
|
|
677
|
-
/ !"'''" SourceCharacter { return text(); }
|
|
678
|
-
|
|
679
|
-
SourceCharacter
|
|
680
|
-
= .
|
|
681
|
-
|
|
682
|
-
digit = [0-9]
|
|
683
|
-
decimal_point = dot
|
|
684
|
-
equal = '='
|
|
685
|
-
dot = '.'
|
|
686
|
-
BooleanLiteral = boolean: ('true'i/'false'i/'null'i) {
|
|
687
|
-
return {
|
|
688
|
-
type: 'boolean',
|
|
689
|
-
value: boolean
|
|
690
|
-
};
|
|
691
|
-
}
|
|
692
|
-
NumberLiteral = number:(float / integer) {
|
|
693
|
-
return {
|
|
694
|
-
type: 'number',
|
|
695
|
-
value: number
|
|
696
|
-
};
|
|
697
|
-
}
|
|
698
|
-
float
|
|
699
|
-
= left:[0-9]+ "." right:[0-9]+ { return parseFloat(left.join("") + "." + right.join("")); }
|
|
700
|
-
|
|
701
|
-
integer
|
|
702
|
-
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
Table users [note: 'note in table settings'] {
|
|
2
|
-
id int [pk]
|
|
3
|
-
name varchar [pk]
|
|
4
|
-
gender varchar
|
|
5
|
-
created_at datetime
|
|
6
|
-
|
|
7
|
-
Note: 'Short note'
|
|
8
|
-
|
|
9
|
-
Indexes {
|
|
10
|
-
id
|
|
11
|
-
(id, name)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
Indexes {
|
|
15
|
-
gender
|
|
16
|
-
created_at
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
Note {
|
|
20
|
-
'''
|
|
21
|
-
# Note
|
|
22
|
-
|
|
23
|
-
## Objective
|
|
24
|
-
* Support define element's note inside element body
|
|
25
|
-
* Make writing long note easier with the new syntax \\
|
|
26
|
-
|
|
27
|
-
'''
|
|
28
|
-
}
|
|
29
|
-
}
|