1
+ import { Cardinality , Constraint } from "../../data/constants" ;
2
+
1
3
export function fromOracleSQL ( ast ) {
2
4
const tables = [ ] ;
3
5
const relationships = [ ] ;
@@ -21,193 +23,89 @@ export function fromOracleSQL(ast) {
21
23
22
24
let type = d . type . type . toUpperCase ( ) ;
23
25
field . type = type ;
26
+
27
+ if ( d . type . scale && d . type . precision ) {
28
+ field . size = d . type . precision + "," + d . type . scale ;
29
+ } else if ( d . type . size || d . type . precision ) {
30
+ field . size = d . type . size || d . type . precision ;
31
+ }
32
+
24
33
field . comment = "" ;
34
+ field . check = "" ;
35
+ field . default = "" ;
25
36
field . unique = false ;
26
37
field . increment = false ;
27
38
field . notNull = false ;
28
39
field . primary = false ;
29
40
30
- field . default = "" ;
31
- // if (d.default_val) {
32
- // let defaultValue = "";
33
- // if (d.default_val.value.type === "function") {
34
- // defaultValue = d.default_val.value.name.name[0].value;
35
- // if (d.default_val.value.args) {
36
- // defaultValue +=
37
- // "(" +
38
- // d.default_val.value.args.value
39
- // .map((v) => {
40
- // if (
41
- // v.type === "single_quote_string" ||
42
- // v.type === "double_quote_string"
43
- // )
44
- // return "'" + v.value + "'";
45
- // return v.value;
46
- // })
47
- // .join(", ") +
48
- // ")";
49
- // }
50
- // } else if (d.default_val.value.type === "null") {
51
- // defaultValue = "NULL";
52
- // } else {
53
- // defaultValue = d.default_val.value.value.toString();
54
- // }
55
- // field.default = defaultValue;
56
- // }
57
- // if (d.definition["length"]) {
58
- // if (d.definition.scale) {
59
- // field.size = d.definition["length"] + "," + d.definition.scale;
60
- // } else {
61
- // field.size = d.definition["length"];
62
- // }
63
- // }
64
- // field.check = "";
65
- // if (d.check) {
66
- // field.check = buildSQLFromAST(d.check.definition[0], DB.MYSQL);
67
- // }
41
+ for ( const c of d . constraints ) {
42
+ if ( c . constraint . primary_key === "primary key" )
43
+ field . primary = true ;
44
+ if ( c . constraint . not_null === "not null" ) field . notNull = true ;
45
+ if ( c . constraint . unique === "unique" ) field . unique = true ;
46
+ }
47
+
48
+ if ( d . identity ) {
49
+ field . increment = true ;
50
+ }
51
+
52
+ // TODO: reconstruct default when implemented in parser
53
+ if ( d . default ) {
54
+ field . default = JSON . stringify ( d . default . expr ) ;
55
+ }
68
56
69
57
table . fields . push ( field ) ;
70
- }
71
- // else if (d.resource === "constraint") {
72
- // if (d.constraint_type === "primary key") {
73
- // d.definition.forEach((c) => {
74
- // table.fields.forEach((f) => {
75
- // if (f.name === c.column && !f.primary) {
76
- // f.primary = true;
77
- // }
78
- // });
79
- // });
80
- // } else if (d.constraint_type.toLowerCase() === "foreign key") {
81
- // const relationship = {};
82
- // const startTableId = table.id;
83
- // const startTable = e.table[0].table;
84
- // const startField = d.definition[0].column;
85
- // const endTable = d.reference_definition.table[0].table;
86
- // const endField = d.reference_definition.definition[0].column;
87
-
88
- // const endTableId = tables.findIndex((t) => t.name === endTable);
89
- // if (endTableId === -1) return;
90
-
91
- // const endFieldId = tables[endTableId].fields.findIndex(
92
- // (f) => f.name === endField,
93
- // );
94
- // if (endFieldId === -1) return;
95
-
96
- // const startFieldId = table.fields.findIndex(
97
- // (f) => f.name === startField,
98
- // );
99
- // if (startFieldId === -1) return;
100
-
101
- // relationship.name =
102
- // "fk_" + startTable + "_" + startField + "_" + endTable;
103
- // relationship.startTableId = startTableId;
104
- // relationship.endTableId = endTableId;
105
- // relationship.endFieldId = endFieldId;
106
- // relationship.startFieldId = startFieldId;
107
- // let updateConstraint = "No action";
108
- // let deleteConstraint = "No action";
109
- // d.reference_definition.on_action.forEach((c) => {
110
- // if (c.type === "on update") {
111
- // updateConstraint = c.value.value;
112
- // updateConstraint =
113
- // updateConstraint[0].toUpperCase() +
114
- // updateConstraint.substring(1);
115
- // } else if (c.type === "on delete") {
116
- // deleteConstraint = c.value.value;
117
- // deleteConstraint =
118
- // deleteConstraint[0].toUpperCase() +
119
- // deleteConstraint.substring(1);
120
- // }
121
- // });
122
-
123
- // relationship.updateConstraint = updateConstraint;
124
- // relationship.deleteConstraint = deleteConstraint;
125
-
126
- // if (table.fields[startFieldId].unique) {
127
- // relationship.cardinality = Cardinality.ONE_TO_ONE;
128
- // } else {
129
- // relationship.cardinality = Cardinality.MANY_TO_ONE;
130
- // }
131
-
132
- // relationships.push(relationship);
133
- // }
134
- // }
58
+ } else if ( d . resource === "constraint" ) {
59
+ const relationship = { } ;
60
+ const startTableId = table . id ;
61
+ const startField = d . constraint . columns [ 0 ] ;
62
+ const endField = d . constraint . reference . columns [ 0 ] ;
63
+ const endTable = d . constraint . reference . object . name ;
64
+
65
+ const endTableId = tables . findIndex ( ( t ) => t . name === endTable ) ;
66
+ if ( endTableId === - 1 ) return ;
67
+
68
+ const endFieldId = tables [ endTableId ] . fields . findIndex (
69
+ ( f ) => f . name === endField ,
70
+ ) ;
71
+ if ( endFieldId === - 1 ) return ;
72
+
73
+ const startFieldId = table . fields . findIndex (
74
+ ( f ) => f . name === startField ,
75
+ ) ;
76
+ if ( startFieldId === - 1 ) return ;
77
+
78
+ relationship . startTableId = startTableId ;
79
+ relationship . startFieldId = startFieldId ;
80
+ relationship . endTableId = endTableId ;
81
+ relationship . endFieldId = endFieldId ;
82
+ relationship . updateConstraint = Constraint . NONE ;
83
+ relationship . name =
84
+ d . name && Boolean ( d . name . trim ( ) )
85
+ ? d . name
86
+ : "fk_" + table . name + "_" + startField + "_" + endTable ;
87
+ relationship . deleteConstraint =
88
+ d . constraint . reference . on_delete &&
89
+ Boolean ( d . constraint . reference . on_delete . trim ( ) )
90
+ ? d . constraint . reference . on_delete [ 0 ] . toUpperCase ( ) +
91
+ d . constraint . reference . on_delete . substring ( 1 )
92
+ : Constraint . NONE ;
93
+
94
+ if ( table . fields [ startFieldId ] . unique ) {
95
+ relationship . cardinality = Cardinality . ONE_TO_ONE ;
96
+ } else {
97
+ relationship . cardinality = Cardinality . MANY_TO_ONE ;
98
+ }
99
+
100
+ relationships . push ( relationship ) ;
101
+ }
135
102
} ) ;
136
103
table . fields . forEach ( ( f , j ) => {
137
104
f . id = j ;
138
105
} ) ;
139
106
tables . push ( table ) ;
140
107
}
141
- }
142
- // else if (e.type === "alter") {
143
- // e.expr.forEach((expr) => {
144
- // if (
145
- // expr.action === "add" &&
146
- // expr.create_definitions.constraint_type.toLowerCase() ===
147
- // "foreign key"
148
- // ) {
149
- // const relationship = {};
150
- // const startTable = e.table[0].table;
151
- // const startField = expr.create_definitions.definition[0].column;
152
- // const endTable =
153
- // expr.create_definitions.reference_definition.table[0].table;
154
- // const endField =
155
- // expr.create_definitions.reference_definition.definition[0].column;
156
- // let updateConstraint = "No action";
157
- // let deleteConstraint = "No action";
158
- // expr.create_definitions.reference_definition.on_action.forEach(
159
- // (c) => {
160
- // if (c.type === "on update") {
161
- // updateConstraint = c.value.value;
162
- // updateConstraint =
163
- // updateConstraint[0].toUpperCase() +
164
- // updateConstraint.substring(1);
165
- // } else if (c.type === "on delete") {
166
- // deleteConstraint = c.value.value;
167
- // deleteConstraint =
168
- // deleteConstraint[0].toUpperCase() +
169
- // deleteConstraint.substring(1);
170
- // }
171
- // },
172
- // );
173
-
174
- // const startTableId = tables.findIndex((t) => t.name === startTable);
175
- // if (startTable === -1) return;
176
-
177
- // const endTableId = tables.findIndex((t) => t.name === endTable);
178
- // if (endTableId === -1) return;
179
-
180
- // const endFieldId = tables[endTableId].fields.findIndex(
181
- // (f) => f.name === endField,
182
- // );
183
- // if (endFieldId === -1) return;
184
-
185
- // const startFieldId = tables[startTableId].fields.findIndex(
186
- // (f) => f.name === startField,
187
- // );
188
- // if (startFieldId === -1) return;
189
-
190
- // relationship.name =
191
- // "fk_" + startTable + "_" + startField + "_" + endTable;
192
- // relationship.startTableId = startTableId;
193
- // relationship.startFieldId = startFieldId;
194
- // relationship.endTableId = endTableId;
195
- // relationship.endFieldId = endFieldId;
196
- // relationship.updateConstraint = updateConstraint;
197
- // relationship.deleteConstraint = deleteConstraint;
198
-
199
- // if (tables[startTableId].fields[startFieldId].unique) {
200
- // relationship.cardinality = Cardinality.ONE_TO_ONE;
201
- // } else {
202
- // relationship.cardinality = Cardinality.MANY_TO_ONE;
203
- // }
204
-
205
- // relationships.push(relationship);
206
-
207
- // relationships.forEach((r, i) => (r.id = i));
208
- // }
209
- // });
210
- // }
108
+ }
211
109
} ;
212
110
213
111
ast . forEach ( ( e ) => parseSingleStatement ( e ) ) ;
0 commit comments