Skip to content

Commit 81b3eea

Browse files
committed
Import from sql
1 parent 1499bbe commit 81b3eea

File tree

2 files changed

+74
-176
lines changed

2 files changed

+74
-176
lines changed

src/components/Workspace.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,12 @@ export default function WorkSpace() {
462462
}}
463463
okButtonProps={{ disabled: selectedDb === "" }}
464464
>
465-
<div className="grid grid-cols-3 gap-4 place-content-center">
465+
<div className="grid grid-cols-4 gap-4 place-content-center">
466466
{Object.values(databases).map((x) => (
467467
<div
468468
key={x.name}
469469
onClick={() => setSelectedDb(x.label)}
470-
className={`space-y-3 py-3 px-4 rounded-md border-2 select-none ${
470+
className={`space-y-3 p-3 rounded-md border-2 select-none ${
471471
settings.mode === "dark"
472472
? "bg-zinc-700 hover:bg-zinc-600"
473473
: "bg-zinc-100 hover:bg-zinc-200"
@@ -477,7 +477,7 @@ export default function WorkSpace() {
477477
{x.image && (
478478
<img
479479
src={x.image}
480-
className="h-10"
480+
className="h-8"
481481
style={{
482482
filter:
483483
"opacity(0.4) drop-shadow(0 0 0 white) drop-shadow(0 0 0 white)",

src/utils/importSQL/oraclesql.js

+71-173
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Cardinality, Constraint } from "../../data/constants";
2+
13
export function fromOracleSQL(ast) {
24
const tables = [];
35
const relationships = [];
@@ -21,193 +23,89 @@ export function fromOracleSQL(ast) {
2123

2224
let type = d.type.type.toUpperCase();
2325
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+
2433
field.comment = "";
34+
field.check = "";
35+
field.default = "";
2536
field.unique = false;
2637
field.increment = false;
2738
field.notNull = false;
2839
field.primary = false;
2940

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+
}
6856

6957
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+
}
135102
});
136103
table.fields.forEach((f, j) => {
137104
f.id = j;
138105
});
139106
tables.push(table);
140107
}
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+
}
211109
};
212110

213111
ast.forEach((e) => parseSingleStatement(e));

0 commit comments

Comments
 (0)