@dbml/core 2.4.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.
@@ -1,11 +1,50 @@
1
- alter_table = alter_table_action:alter_table_action {
1
+ alter_table = alter_sub_syntax:(
2
+ alter_table_action /
3
+ alter_table_rename /
4
+ alter_table_set_schema /
5
+ alter_table_set_tablespace /
6
+ alter_table_attach /
7
+ alter_table_detach
8
+ ) {
2
9
  return {
3
10
  command_name: "alter_table",
4
- value: alter_table_action
11
+ value: alter_sub_syntax
5
12
  }
6
13
  }
7
14
 
8
- alter_table_action = _ ALTER __ TABLE (__ IF_EXISTS)? (__ ONLY)? __ name:table_name __
15
+ alter_table_rename = _ ALTER __ TABLE (__ IF_EXISTS)? (__ ONLY)? __ table_name (_ "*")? __ RENAME (cmt / !semicolon .)* _ semicolon _ {
16
+ return {
17
+ syntax_name: "alter_table_rename",
18
+ }
19
+ }
20
+
21
+ alter_table_set_schema = _ ALTER __ TABLE (__ IF_EXISTS)? __ table_name __ SET __ SCHEMA (cmt / !semicolon .)* _ semicolon _ {
22
+ return {
23
+ syntax_name: "alter_table_set_schema",
24
+ }
25
+ }
26
+
27
+ alter_table_set_tablespace = _ ALTER __ TABLE __ ALL __ IN __ TABLESPACE __ table_name
28
+ (__ OWNED __ BY __ identifier (_ comma _ identifier)*)? __ SET __ TABLESPACE
29
+ (cmt / !semicolon .)* _ semicolon _ {
30
+ return {
31
+ syntax_name: "alter_set_tablespace",
32
+ }
33
+ }
34
+
35
+ alter_table_attach = _ ALTER __ TABLE (__ IF_EXISTS)? __ table_name __ ATTACH __ PARTITION (cmt / !semicolon .)* _ semicolon _ {
36
+ return {
37
+ syntax_name: "alter_table_attach",
38
+ }
39
+ }
40
+
41
+ alter_table_detach = _ ALTER __ TABLE (__ IF_EXISTS)? __ table_name __ DETACH __ PARTITION (cmt / !semicolon .)* _ semicolon _ {
42
+ return {
43
+ syntax_name: "alter_table_detach",
44
+ }
45
+ }
46
+
47
+ alter_table_action = _ ALTER __ TABLE (__ IF_EXISTS)? (__ ONLY)? __ name:table_name (_ "*")? __
9
48
  actions:actions _ semicolon _ {
10
49
  actions.forEach(({ type, t_value}) => {
11
50
  switch(type.toLowerCase()) {
@@ -79,6 +118,11 @@ action = ADD __ table_constraint:table_constraint (__ NOT __ VALID)? { // reuse
79
118
  value: value.toLowerCase()
80
119
  }
81
120
  }
121
+ / (cmt / !semicolon .)* {
122
+ return {
123
+ type: "unknown",
124
+ }
125
+ }
82
126
 
83
127
 
84
128
  set_attribute_options = first:set_attribute_option rest:(_ comma _ set_attribute_option)* {
@@ -2,9 +2,7 @@ comment = _ COMMENT __ ON __ comment_option:comment_option __ IS __ text:(string
2
2
  _ semicolon _ {
3
3
  if (text.toLowerCase() !== "null") {
4
4
  comment_option.value.text = text;
5
- } else {
6
- comment_option.value.syntax_name = "remove_comment";
7
- }
5
+ } else comment_option.value.text = null;
8
6
 
9
7
  return {
10
8
  command_name: "comment",
@@ -13,12 +11,26 @@ comment = _ COMMENT __ ON __ comment_option:comment_option __ IS __ text:(string
13
11
  }
14
12
 
15
13
  comment_option = (
16
- COLUMN __ relation_name:identifier "." column_name:column_name {
14
+ COLUMN __ path:(identifier '.')+ column_name:column_name {
15
+ let dbName = null, schemaName = null, tableName;
16
+ if (path.length === 1) {
17
+ tableName = path[0][0];
18
+ } else if (path.length === 2) {
19
+ schemaName = path[0][0];
20
+ tableName = path[1][0];
21
+ }
22
+ else {
23
+ dbName = path[0][0];
24
+ schemaName = path[1][0];
25
+ tableName = path[2][0];
26
+ }
17
27
  return {
18
28
  syntax_name: "column",
19
29
  value: {
20
- relation_name: relation_name,
21
- column_name
30
+ dbName,
31
+ schemaName,
32
+ tableName,
33
+ columnName: column_name
22
34
  }
23
35
  }
24
36
  }
@@ -8,10 +8,19 @@ ignore_syntax = _ value:(
8
8
  / CREATE __ SEQUENCE [^;]* { return { syntax_name: "create_sequence" } }
9
9
  / CREATE __ SCHEMA [^;]* { return { syntax_name: "create_schema" } }
10
10
  / CREATE __ VIEW [^;]* { return { syntax_name: "create_view" } }
11
+ / ALTER __ (cmt / !(semicolon) .)* { return { syntax_name: "alter_not_table" } }
11
12
  / __ { return { syntax_name: "comment_and_space" } }
12
13
  ) semicolon _ {
14
+ const loc = location();
15
+ const t = text();
13
16
  return {
14
17
  command_name: "ignore_syntax",
15
- value
18
+ value,
19
+ warning: {
20
+ type: 'ignore',
21
+ location: loc,
22
+ text: t,
23
+ message: `ignoring "${t}" at line: ${loc.start.line}`,
24
+ },
16
25
  }
17
26
  }
@@ -1,8 +1,8 @@
1
1
  {
2
2
  // intput:
3
- // `
3
+ // `
4
4
  // 'created'
5
- // ,
5
+ // ,
6
6
  // 'pending', 'done'
7
7
  // `
8
8
  // => `'created', 'pending', 'done'`
@@ -24,4 +24,6 @@
24
24
  });
25
25
  return table;
26
26
  };
27
+
28
+ const findField = (table, fieldName) => table.fields.find(field => field.name === fieldName);
27
29
  }
@@ -110,4 +110,8 @@ SELECT = "SELECT"i
110
110
  USE = "USE"i
111
111
  SEQUENCE = "SEQUENCE"i
112
112
  SCHEMA = "SCHEMA"i
113
- VIEW = "VIEW"i
113
+ VIEW = "VIEW"i
114
+ RENAME = "RENAME"i
115
+ OWNED = "OWNED"i
116
+ ATTACH = "ATTACH"i
117
+ DETACH = "DETACH"i
@@ -2,10 +2,12 @@
2
2
  const tables = [];
3
3
  const refs = [];
4
4
  const enums = [];
5
+ const warnings = [];
5
6
  }
6
7
 
7
8
  parser = commands:command* {
8
- commands.forEach(({ command_name, value: { syntax_name, value } }) => {
9
+ commands.forEach((cmd) => {
10
+ const { command_name, value: { syntax_name, value }, warning } = cmd;
9
11
  switch(command_name.toLowerCase()){
10
12
  case "create_table":
11
13
  const table = value;
@@ -104,18 +106,29 @@ parser = commands:command* {
104
106
  break;
105
107
  case "comment":
106
108
  switch(syntax_name.toLowerCase()) {
107
- case "column":
108
- const table_comment = tables.find(table => table.name === value.relation_name);
109
- const field_comment = table_comment.fields.find(field => field.name === value.column_name);
110
- field_comment.note = value.text;
109
+ case "column": {
110
+ const { schemaName, tableName, columnName } = value;
111
+ const foundTable = findTable(schemaName, tableName);
112
+ if (foundTable) {
113
+ const foundField = findField(foundTable, columnName);
114
+ if (foundField) foundField.note = value.text;
115
+ }
116
+ break;
117
+ }
118
+ case "table": {
119
+ const { schemaName, name: tableName } = value.table_name;
120
+ const foundTable = findTable(schemaName, tableName);
121
+ if (foundTable) foundTable.note = value.text;
111
122
  break;
123
+ }
112
124
  }
113
125
  break;
114
- case "ignore_commands":
126
+ case "ignore_syntax":
127
+ // warnings.push(warning);
115
128
  break;
116
129
  }
117
130
  })
118
- // console.log({tables, refs, enums, indexes});
131
+
119
132
  return {tables, refs, enums};
120
133
  }
121
134
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbml/core",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "description": "> TODO: description",
5
5
  "author": "Holistics <dev@holistics.io>",
6
6
  "license": "Apache-2.0",
@@ -56,5 +56,5 @@
56
56
  "\\.(?!json$)[^.]*$": "jest-raw-loader"
57
57
  }
58
58
  },
59
- "gitHead": "bbc1d589c6dcefce089bd6007a24cad2808a3818"
59
+ "gitHead": "4a4ec93db569e677bf15eb53964bd6b26762a421"
60
60
  }