Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data typed fixed #2

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added src/assets/oracle-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ export const DB = {
MSSQL: "transactsql",
SQLITE: "sqlite",
MARIADB: "mariadb",
ORACLE: "oracledb",
GENERIC: "generic",
};
7 changes: 7 additions & 0 deletions src/data/databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import postgresImage from "../assets/postgres-icon.png";
import sqliteImage from "../assets/sqlite-icon.png";
import mariadbImage from "../assets/mariadb-icon.png";
import mssqlImage from "../assets/mssql-icon.png";
import oracleImage from "../assets/oracle-icon.png";
import i18n from "../i18n/i18n";
import { DB } from "./constants";

Expand Down Expand Up @@ -42,6 +43,12 @@ export const databases = new Proxy(
image: mssqlImage,
hasTypes: false,
},
[DB.ORACLE]: {
name: "Oracle",
label: DB.ORACLE,
image: oracleImage,
hasTypes: true,
},
[DB.GENERIC]: {
name: i18n.t("generic"),
label: DB.GENERIC,
Expand Down
197 changes: 195 additions & 2 deletions src/data/datatypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ const binaryRegex = /^[01]+$/;

/* eslint-disable no-unused-vars */
const defaultTypesBase = {
NUMBER: {
type: "NUMBER",
checkDefault: (field) => {
return intRegex.test(field.default);
},
hasCheck: true,
isSized: false,
hasPrecision: true,
canIncrement: true,
},
INT: {
type: "INT",
checkDefault: (field) => {
Expand Down Expand Up @@ -263,12 +273,193 @@ const defaultTypesBase = {
hasPrecision: false,
noDefault: true,
},
VARCHAR2: {
type: "VARCHAR2",
checkDefault: (field) => {
if (strHasQuotes(field.default)) {
return field.default.length - 2 <= field.size;
}
return field.default.length <= field.size;
},
hasCheck: true,
isSized: true,
hasPrecision: false,
defaultSize: 10,
},
};

export const defaultTypes = new Proxy(defaultTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : false),
});

const oracleTypesBase = {
VARCHAR2: {
type: "VARCHAR2",
checkDefault: (field) => {
if (strHasQuotes(field.default)) {
return field.default.length - 2 <= field.size;
}
return field.default.length <= field.size;
},
hasCheck: true,
isSized: true,
hasPrecision: false,
defaultSize: 255,
hasQuotes: true,
},
NUMBER: {
type: "NUMBER",
checkDefault: (field) => {
return intRegex.test(field.default);
},
hasCheck: true,
isSized: false,
hasPrecision: true,
canIncrement: true,
},
FLOAT: {
type: "FLOAT",
checkDefault: (field) => {
return doubleRegex.test(field.default);
},
hasCheck: true,
isSized: false,
hasPrecision: true,
},
CHAR: {
type: "CHAR",
checkDefault: (field) => {
if (strHasQuotes(field.default)) {
return field.default.length - 2 <= field.size;
}
return field.default.length <= field.size;
},
hasCheck: true,
isSized: true,
hasPrecision: false,
defaultSize: 1,
hasQuotes: true,
},
VARCHAR: {
type: "VARCHAR",
checkDefault: (field) => {
if (strHasQuotes(field.default)) {
return field.default.length - 2 <= field.size;
}
return field.default.length <= field.size;
},
hasCheck: true,
isSized: true,
hasPrecision: false,
defaultSize: 255,
hasQuotes: true,
},
TEXT: {
type: "TEXT",
checkDefault: (field) => true,
hasCheck: false,
isSized: true,
hasPrecision: false,
defaultSize: 65535,
hasQuotes: true,
},
TIME: {
type: "TIME",
checkDefault: (field) => {
return /^(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d$/.test(field.default);
},
hasCheck: false,
isSized: false,
hasPrecision: false,
hasQuotes: true,
},
TIMESTAMP: {
type: "TIMESTAMP",
checkDefault: (field) => {
if (field.default.toUpperCase() === "CURRENT_TIMESTAMP") {
return true;
}
if (!/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(field.default)) {
return false;
}
const content = field.default.split(" ");
const date = content[0].split("-");
return parseInt(date[0]) >= 1970 && parseInt(date[0]) <= 2038;
},
hasCheck: false,
isSized: false,
hasPrecision: false,
hasQuotes: true,
},
DATE: {
type: "DATE",
checkDefault: (field) => {
return /^\d{4}-\d{2}-\d{2}$/.test(field.default);
},
hasCheck: false,
isSized: false,
hasPrecision: false,
hasQuotes: true,
},
DATETIME: {
type: "DATETIME",
checkDefault: (field) => {
if (field.default.toUpperCase() === "CURRENT_TIMESTAMP") {
return true;
}
if (!/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(field.default)) {
return false;
}
const c = field.default.split(" ");
const d = c[0].split("-");
return parseInt(d[0]) >= 1000 && parseInt(d[0]) <= 9999;
},
hasCheck: false,
isSized: false,
hasPrecision: false,
hasQuotes: true,
},
BOOLEAN: {
type: "BOOLEAN",
checkDefault: (field) => {
return (
field.default.toLowerCase() === "false" ||
field.default.toLowerCase() === "true" ||
field.default === "0" ||
field.default === "1"
);
},
hasCheck: false,
isSized: false,
hasPrecision: false,
},
BINARY: {
type: "BINARY",
checkDefault: (field) => {
return (
field.default.length <= field.size && binaryRegex.test(field.default)
);
},
hasCheck: false,
isSized: true,
hasPrecision: false,
defaultSize: 1,
hasQuotes: true,
},
BLOB: {
type: "BLOB",
checkDefault: (field) => true,
isSized: false,
hasCheck: false,
hasPrecision: false,
noDefault: true,
},
};

export const oracleTypes = new Proxy(oracleTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : false),
});

const mysqlTypesBase = {
TINYINT: {
type: "TINYINT",
Expand Down Expand Up @@ -913,8 +1104,9 @@ const postgresTypesBase = {
checkDefault: (field) => {
const specialValues = ["now", "allballs"];
return (
/^(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d([+-]\d{2}:\d{2})?$/.test(field.default) ||
specialValues.includes(field.default.toLowerCase())
/^(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d([+-]\d{2}:\d{2})?$/.test(
field.default,
) || specialValues.includes(field.default.toLowerCase())
);
},
hasCheck: false,
Expand Down Expand Up @@ -1698,6 +1890,7 @@ const dbToTypesBase = {
[DB.SQLITE]: sqliteTypes,
[DB.MSSQL]: mssqlTypes,
[DB.MARIADB]: mysqlTypes,
[DB.ORACLE]: oracleTypes,
};

export const dbToTypes = new Proxy(dbToTypesBase, {
Expand Down
61 changes: 61 additions & 0 deletions src/utils/exportSQL/oracle.js
Original file line number Diff line number Diff line change
@@ -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")}`;
}

Loading