mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-05-24 10:29:11 +00:00
Boton de oracle funciona pero no add table
This commit is contained in:
parent
20a2553b3d
commit
390db6814b
26
package-lock.json
generated
26
package-lock.json
generated
@ -15,7 +15,6 @@
|
|||||||
"@uiw/codemirror-theme-github": "^4.21.25",
|
"@uiw/codemirror-theme-github": "^4.21.25",
|
||||||
"@uiw/codemirror-theme-vscode": "^4.21.25",
|
"@uiw/codemirror-theme-vscode": "^4.21.25",
|
||||||
"@uiw/react-codemirror": "^4.21.25",
|
"@uiw/react-codemirror": "^4.21.25",
|
||||||
"@vercel/analytics": "^1.2.2",
|
|
||||||
"axios": "^1.7.4",
|
"axios": "^1.7.4",
|
||||||
"classnames": "^2.5.1",
|
"classnames": "^2.5.1",
|
||||||
"dexie": "^3.2.4",
|
"dexie": "^3.2.4",
|
||||||
@ -2253,26 +2252,6 @@
|
|||||||
"integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==",
|
"integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/@vercel/analytics": {
|
|
||||||
"version": "1.2.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/@vercel/analytics/-/analytics-1.2.2.tgz",
|
|
||||||
"integrity": "sha512-X0rctVWkQV1e5Y300ehVNqpOfSOufo7ieA5PIdna8yX/U7Vjz0GFsGf4qvAhxV02uQ2CVt7GYcrFfddXXK2Y4A==",
|
|
||||||
"dependencies": {
|
|
||||||
"server-only": "^0.0.1"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"next": ">= 13",
|
|
||||||
"react": "^18 || ^19"
|
|
||||||
},
|
|
||||||
"peerDependenciesMeta": {
|
|
||||||
"next": {
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"react": {
|
|
||||||
"optional": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@vitejs/plugin-react": {
|
"node_modules/@vitejs/plugin-react": {
|
||||||
"version": "4.2.1",
|
"version": "4.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.2.1.tgz",
|
||||||
@ -5816,11 +5795,6 @@
|
|||||||
"semver": "bin/semver.js"
|
"semver": "bin/semver.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/server-only": {
|
|
||||||
"version": "0.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/server-only/-/server-only-0.0.1.tgz",
|
|
||||||
"integrity": "sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA=="
|
|
||||||
},
|
|
||||||
"node_modules/set-function-length": {
|
"node_modules/set-function-length": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.1.1.tgz",
|
||||||
|
BIN
src/assets/oracle-icon.png
Normal file
BIN
src/assets/oracle-icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 MiB |
@ -112,5 +112,6 @@ export const DB = {
|
|||||||
MSSQL: "transactsql",
|
MSSQL: "transactsql",
|
||||||
SQLITE: "sqlite",
|
SQLITE: "sqlite",
|
||||||
MARIADB: "mariadb",
|
MARIADB: "mariadb",
|
||||||
|
ORACLE: "oracledb",
|
||||||
GENERIC: "generic",
|
GENERIC: "generic",
|
||||||
};
|
};
|
||||||
|
@ -3,6 +3,7 @@ import postgresImage from "../assets/postgres-icon.png";
|
|||||||
import sqliteImage from "../assets/sqlite-icon.png";
|
import sqliteImage from "../assets/sqlite-icon.png";
|
||||||
import mariadbImage from "../assets/mariadb-icon.png";
|
import mariadbImage from "../assets/mariadb-icon.png";
|
||||||
import mssqlImage from "../assets/mssql-icon.png";
|
import mssqlImage from "../assets/mssql-icon.png";
|
||||||
|
import oracleImage from "../assets/oracle-icon.png";
|
||||||
import i18n from "../i18n/i18n";
|
import i18n from "../i18n/i18n";
|
||||||
import { DB } from "./constants";
|
import { DB } from "./constants";
|
||||||
|
|
||||||
@ -42,6 +43,12 @@ export const databases = new Proxy(
|
|||||||
image: mssqlImage,
|
image: mssqlImage,
|
||||||
hasTypes: false,
|
hasTypes: false,
|
||||||
},
|
},
|
||||||
|
[DB.ORACLE]: {
|
||||||
|
name: "Oracle",
|
||||||
|
label: DB.ORACLE,
|
||||||
|
image: oracleImage,
|
||||||
|
hasTypes: true,
|
||||||
|
},
|
||||||
[DB.GENERIC]: {
|
[DB.GENERIC]: {
|
||||||
name: i18n.t("generic"),
|
name: i18n.t("generic"),
|
||||||
label: DB.GENERIC,
|
label: DB.GENERIC,
|
||||||
|
@ -7,6 +7,16 @@ const binaryRegex = /^[01]+$/;
|
|||||||
|
|
||||||
/* eslint-disable no-unused-vars */
|
/* eslint-disable no-unused-vars */
|
||||||
const defaultTypesBase = {
|
const defaultTypesBase = {
|
||||||
|
NUMBER: {
|
||||||
|
type: "NUMBER",
|
||||||
|
checkDefault: (field) => {
|
||||||
|
return intRegex.test(field.default);
|
||||||
|
},
|
||||||
|
hasCheck: true,
|
||||||
|
isSized: false,
|
||||||
|
hasPrecision: true,
|
||||||
|
canIncrement: true,
|
||||||
|
},
|
||||||
INT: {
|
INT: {
|
||||||
type: "INT",
|
type: "INT",
|
||||||
checkDefault: (field) => {
|
checkDefault: (field) => {
|
||||||
|
61
src/utils/exportSQL/oracle.js
Normal file
61
src/utils/exportSQL/oracle.js
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
// Export to Oracle SQL based on mariadb.js
|
||||||
|
// The difference is that Oracle SQL can not handle the ON UPDATE constraint so it is removed from the script.
|
||||||
|
|
||||||
|
import { parseDefault } from "./shared";
|
||||||
|
|
||||||
|
import { dbToTypes } from "../../data/datatypes";
|
||||||
|
|
||||||
|
export function toOracle(diagram) {
|
||||||
|
return `${diagram.tables
|
||||||
|
.map(
|
||||||
|
(table) =>
|
||||||
|
`CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields
|
||||||
|
.map(
|
||||||
|
(field) =>
|
||||||
|
`\t\`${
|
||||||
|
field.name
|
||||||
|
}\` ${field.type}${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}${field.unsigned ? " UNSIGNED" : ""}${field.notNull ? " NOT NULL" : ""}${
|
||||||
|
field.increment ? " AUTO_INCREMENT" : ""
|
||||||
|
}${field.unique ? " UNIQUE" : ""}${
|
||||||
|
field.default !== ""
|
||||||
|
? ` DEFAULT ${parseDefault(field, diagram.database)}`
|
||||||
|
: ""
|
||||||
|
}${
|
||||||
|
field.check === "" ||
|
||||||
|
!dbToTypes[diagram.database][field.type].hasCheck
|
||||||
|
? ""
|
||||||
|
: ` CHECK(${field.check})`
|
||||||
|
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`,
|
||||||
|
)
|
||||||
|
.join(",\n")}${
|
||||||
|
table.fields.filter((f) => f.primary).length > 0
|
||||||
|
? `,\n\tPRIMARY KEY(${table.fields
|
||||||
|
.filter((f) => f.primary)
|
||||||
|
.map((f) => `\`${f.name}\``)
|
||||||
|
.join(", ")})`
|
||||||
|
: ""
|
||||||
|
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};${`\n${table.indices
|
||||||
|
.map(
|
||||||
|
(i) =>
|
||||||
|
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${
|
||||||
|
i.name
|
||||||
|
}\`\nON \`${table.name}\` (${i.fields
|
||||||
|
.map((f) => `\`${f}\``)
|
||||||
|
.join(", ")});`,
|
||||||
|
)
|
||||||
|
.join("")}`}`,
|
||||||
|
)
|
||||||
|
.join("\n")}\n${diagram.references
|
||||||
|
.map(
|
||||||
|
(r) =>
|
||||||
|
`ALTER TABLE \`${
|
||||||
|
diagram.tables[r.startTableId].name
|
||||||
|
}\`\nADD FOREIGN KEY(\`${
|
||||||
|
diagram.tables[r.startTableId].fields[r.startFieldId].name
|
||||||
|
}\`) REFERENCES \`${diagram.tables[r.endTableId].name}\`(\`${
|
||||||
|
diagram.tables[r.endTableId].fields[r.endFieldId].name
|
||||||
|
}\`)\nON DELETE ${r.deleteConstraint.toUpperCase()};`,
|
||||||
|
)
|
||||||
|
.join("\n")}`;
|
||||||
|
}
|
||||||
|
|
268
src/utils/importSQL/oracle.js
Normal file
268
src/utils/importSQL/oracle.js
Normal file
@ -0,0 +1,268 @@
|
|||||||
|
// Import SQL files from Oracle database
|
||||||
|
|
||||||
|
import { Cardinality, DB } from "../../data/constants";
|
||||||
|
import { dbToTypes } from "../../data/datatypes";
|
||||||
|
import { buildSQLFromAST } from "./shared";
|
||||||
|
|
||||||
|
const affinity = {
|
||||||
|
[DB.MARIADB]: new Proxy(
|
||||||
|
{ INT: "INTEGER" },
|
||||||
|
{ get: (target, prop) => (prop in target ? target[prop] : "BLOB") },
|
||||||
|
),
|
||||||
|
[DB.GENERIC]: new Proxy(
|
||||||
|
{
|
||||||
|
INT: "INTEGER",
|
||||||
|
TINYINT: "SMALLINT",
|
||||||
|
MEDIUMINT: "INTEGER",
|
||||||
|
BIT: "BOOLEAN",
|
||||||
|
YEAR: "INTEGER",
|
||||||
|
},
|
||||||
|
{ get: (target, prop) => (prop in target ? target[prop] : "BLOB") },
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
export function fromOracle(ast, diagramDb = DB.GENERIC) {
|
||||||
|
const tables = [];
|
||||||
|
const relationships = [];
|
||||||
|
|
||||||
|
const parseSingleStatement = (e) => {
|
||||||
|
if (e.type === "create") {
|
||||||
|
if (e.keyword === "table") {
|
||||||
|
const table = {};
|
||||||
|
table.name = e.table[0].table;
|
||||||
|
table.comment = "";
|
||||||
|
table.color = "#175e7a";
|
||||||
|
table.fields = [];
|
||||||
|
table.indices = [];
|
||||||
|
table.id = tables.length;
|
||||||
|
e.create_definitions.forEach((d) => {
|
||||||
|
if (d.resource === "column") {
|
||||||
|
const field = {};
|
||||||
|
field.name = d.column.column;
|
||||||
|
|
||||||
|
let type = d.definition.dataType;
|
||||||
|
if (!dbToTypes[diagramDb][type]) {
|
||||||
|
type = affinity[diagramDb][type];
|
||||||
|
}
|
||||||
|
field.type = type;
|
||||||
|
|
||||||
|
if (d.definition.expr && d.definition.expr.type === "expr_list") {
|
||||||
|
field.values = d.definition.expr.value.map((v) => v.value);
|
||||||
|
}
|
||||||
|
field.comment = d.comment ? d.comment.value.value : "";
|
||||||
|
field.unique = false;
|
||||||
|
if (d.unique) field.unique = true;
|
||||||
|
field.increment = false;
|
||||||
|
if (d.auto_increment) field.increment = true;
|
||||||
|
field.notNull = false;
|
||||||
|
if (d.nullable) field.notNull = true;
|
||||||
|
field.primary = false;
|
||||||
|
if (d.primary_key) field.primary = true;
|
||||||
|
field.default = "";
|
||||||
|
if (d.default_val) {
|
||||||
|
let defaultValue = "";
|
||||||
|
if (d.default_val.value.type === "function") {
|
||||||
|
defaultValue = d.default_val.value.name.name[0].value;
|
||||||
|
if (d.default_val.value.args) {
|
||||||
|
defaultValue +=
|
||||||
|
"(" +
|
||||||
|
d.default_val.value.args.value
|
||||||
|
.map((v) => {
|
||||||
|
if (
|
||||||
|
v.type === "single_quote_string" ||
|
||||||
|
v.type === "double_quote_string"
|
||||||
|
)
|
||||||
|
return "'" + v.value + "'";
|
||||||
|
return v.value;
|
||||||
|
})
|
||||||
|
.join(", ") +
|
||||||
|
")";
|
||||||
|
}
|
||||||
|
} else if (d.default_val.value.type === "null") {
|
||||||
|
defaultValue = "NULL";
|
||||||
|
} else {
|
||||||
|
defaultValue = d.default_val.value.value.toString();
|
||||||
|
}
|
||||||
|
field.default = defaultValue;
|
||||||
|
}
|
||||||
|
if (d.definition["length"]) {
|
||||||
|
if (d.definition.scale) {
|
||||||
|
field.size = d.definition["length"] + "," + d.definition.scale;
|
||||||
|
} else {
|
||||||
|
field.size = d.definition["length"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
field.check = "";
|
||||||
|
if (d.check) {
|
||||||
|
field.check = buildSQLFromAST(d.check.definition[0], DB.MARIADB);
|
||||||
|
}
|
||||||
|
|
||||||
|
table.fields.push(field);
|
||||||
|
} else if (d.resource === "constraint") {
|
||||||
|
if (d.constraint_type === "primary key") {
|
||||||
|
d.definition.forEach((c) => {
|
||||||
|
table.fields.forEach((f) => {
|
||||||
|
if (f.name === c.column && !f.primary) {
|
||||||
|
f.primary = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else if (d.constraint_type.toLowerCase() === "foreign key") {
|
||||||
|
const relationship = {};
|
||||||
|
const startTableId = table.id;
|
||||||
|
const startTable = e.table[0].table;
|
||||||
|
const startField = d.definition[0].column;
|
||||||
|
const endTable = d.reference_definition.table[0].table;
|
||||||
|
const endField = d.reference_definition.definition[0].column;
|
||||||
|
|
||||||
|
const endTableId = tables.findIndex((t) => t.name === endTable);
|
||||||
|
if (endTableId === -1) return;
|
||||||
|
|
||||||
|
const endFieldId = tables[endTableId].fields.findIndex(
|
||||||
|
(f) => f.name === endField,
|
||||||
|
);
|
||||||
|
if (endFieldId === -1) return;
|
||||||
|
|
||||||
|
const startFieldId = table.fields.findIndex(
|
||||||
|
(f) => f.name === startField,
|
||||||
|
);
|
||||||
|
if (startFieldId === -1) return;
|
||||||
|
|
||||||
|
relationship.name = startTable + "_" + startField + "_fk";
|
||||||
|
relationship.startTableId = startTableId;
|
||||||
|
relationship.endTableId = endTableId;
|
||||||
|
relationship.endFieldId = endFieldId;
|
||||||
|
relationship.startFieldId = startFieldId;
|
||||||
|
let updateConstraint = "No action";
|
||||||
|
let deleteConstraint = "No action";
|
||||||
|
d.reference_definition.on_action.forEach((c) => {
|
||||||
|
if (c.type === "on update") {
|
||||||
|
updateConstraint = c.value.value;
|
||||||
|
updateConstraint =
|
||||||
|
updateConstraint[0].toUpperCase() +
|
||||||
|
updateConstraint.substring(1);
|
||||||
|
} else if (c.type === "on delete") {
|
||||||
|
deleteConstraint = c.value.value;
|
||||||
|
deleteConstraint =
|
||||||
|
deleteConstraint[0].toUpperCase() +
|
||||||
|
deleteConstraint.substring(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
relationship.updateConstraint = updateConstraint;
|
||||||
|
relationship.deleteConstraint = deleteConstraint;
|
||||||
|
|
||||||
|
if (table.fields[startFieldId].unique) {
|
||||||
|
relationship.cardinality = Cardinality.ONE_TO_ONE;
|
||||||
|
} else {
|
||||||
|
relationship.cardinality = Cardinality.MANY_TO_ONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
relationships.push(relationship);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
table.fields.forEach((f, j) => {
|
||||||
|
f.id = j;
|
||||||
|
});
|
||||||
|
tables.push(table);
|
||||||
|
} else if (e.keyword === "index") {
|
||||||
|
const index = {};
|
||||||
|
index.name = e.index;
|
||||||
|
index.unique = false;
|
||||||
|
if (e.index_type === "unique") index.unique = true;
|
||||||
|
index.fields = [];
|
||||||
|
e.index_columns.forEach((f) => index.fields.push(f.column));
|
||||||
|
|
||||||
|
let found = -1;
|
||||||
|
tables.forEach((t, i) => {
|
||||||
|
if (found !== -1) return;
|
||||||
|
if (t.name === e.table.table) {
|
||||||
|
t.indices.push(index);
|
||||||
|
found = i;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (found !== -1) tables[found].indices.forEach((i, j) => (i.id = j));
|
||||||
|
}
|
||||||
|
} else if (e.type === "alter") {
|
||||||
|
e.expr.forEach((expr) => {
|
||||||
|
if (
|
||||||
|
expr.action === "add" &&
|
||||||
|
expr.create_definitions.constraint_type.toLowerCase() === "foreign key"
|
||||||
|
) {
|
||||||
|
const relationship = {};
|
||||||
|
const startTable = e.table[0].table;
|
||||||
|
const startField = expr.create_definitions.definition[0].column;
|
||||||
|
const endTable =
|
||||||
|
expr.create_definitions.reference_definition.table[0].table;
|
||||||
|
const endField =
|
||||||
|
expr.create_definitions.reference_definition.definition[0].column;
|
||||||
|
let updateConstraint = "No action";
|
||||||
|
let deleteConstraint = "No action";
|
||||||
|
expr.create_definitions.reference_definition.on_action.forEach(
|
||||||
|
(c) => {
|
||||||
|
if (c.type === "on update") {
|
||||||
|
updateConstraint = c.value.value;
|
||||||
|
updateConstraint =
|
||||||
|
updateConstraint[0].toUpperCase() +
|
||||||
|
updateConstraint.substring(1);
|
||||||
|
} else if (c.type === "on delete") {
|
||||||
|
deleteConstraint = c.value.value;
|
||||||
|
deleteConstraint =
|
||||||
|
deleteConstraint[0].toUpperCase() +
|
||||||
|
deleteConstraint.substring(1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const startTableId = tables.findIndex((t) => t.name === startTable);
|
||||||
|
if (startTable === -1) return;
|
||||||
|
|
||||||
|
const endTableId = tables.findIndex((t) => t.name === endTable);
|
||||||
|
if (endTableId === -1) return;
|
||||||
|
|
||||||
|
const endFieldId = tables[endTableId].fields.findIndex(
|
||||||
|
(f) => f.name === endField,
|
||||||
|
);
|
||||||
|
if (endFieldId === -1) return;
|
||||||
|
|
||||||
|
const startFieldId = tables[startTableId].fields.findIndex(
|
||||||
|
(f) => f.name === startField,
|
||||||
|
);
|
||||||
|
if (startFieldId === -1) return;
|
||||||
|
|
||||||
|
relationship.name = startTable + "_" + startField + "_fk";
|
||||||
|
relationship.startTableId = startTableId;
|
||||||
|
relationship.startFieldId = startFieldId;
|
||||||
|
relationship.endTableId = endTableId;
|
||||||
|
relationship.endFieldId = endFieldId;
|
||||||
|
relationship.updateConstraint = updateConstraint;
|
||||||
|
relationship.deleteConstraint = deleteConstraint;
|
||||||
|
|
||||||
|
if (tables[startTableId].fields[startFieldId].unique) {
|
||||||
|
relationship.cardinality = Cardinality.ONE_TO_ONE;
|
||||||
|
} else {
|
||||||
|
relationship.cardinality = Cardinality.MANY_TO_ONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
relationships.push(relationship);
|
||||||
|
|
||||||
|
relationships.forEach((r, i) => (r.id = i));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (Array.isArray(ast)) {
|
||||||
|
ast.forEach((e) => parseSingleStatement(e));
|
||||||
|
} else {
|
||||||
|
parseSingleStatement(ast);
|
||||||
|
}
|
||||||
|
|
||||||
|
relationships.forEach((r, i) => (r.id = i));
|
||||||
|
|
||||||
|
return { tables, relationships };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user