From 36ad44f0db1542e6448fa809634b3f4012385b5d Mon Sep 17 00:00:00 2001 From: 1ilit Date: Tue, 19 Sep 2023 15:51:26 +0300 Subject: [PATCH] Suppost postgres --- src/components/control_panel.jsx | 20 ++++- src/components/table.jsx | 2 +- src/components/table_overview.jsx | 2 +- src/utils/index.js | 138 ++++++++++++++++++++++++++---- 4 files changed, 142 insertions(+), 20 deletions(-) diff --git a/src/components/control_panel.jsx b/src/components/control_panel.jsx index e71e371..b31bbac 100644 --- a/src/components/control_panel.jsx +++ b/src/components/control_panel.jsx @@ -37,7 +37,8 @@ import { exitFullscreen, ddbDiagramIsValid, dataURItoBlob, - jsonToSQL, + jsonToMySQL, + jsonToPostgreSQL, } from "../utils"; import { AreaContext, @@ -728,7 +729,21 @@ export default function ControlPanel(props) { { MySQL: () => { setVisible(MODAL.CODE); - const src = jsonToSQL({ + const src = jsonToMySQL({ + tables: tables, + references: relationships, + }); + setExportData((prev) => ({ + ...prev, + data: src, + extension: "sql", + })); + }, + }, + { + PostgreSQL: () => { + setVisible(MODAL.CODE); + const src = jsonToPostgreSQL({ tables: tables, references: relationships, }); @@ -739,7 +754,6 @@ export default function ControlPanel(props) { })); }, }, - { PostgreSQL: () => {} }, { DBML: () => {} }, ], function: () => {}, diff --git a/src/components/table.jsx b/src/components/table.jsx index 347b1d5..4ad98be 100644 --- a/src/components/table.jsx +++ b/src/components/table.jsx @@ -382,7 +382,7 @@ export default function Table(props) { updateField(props.tableData.id, j, { type: value, default: "", - values: [], + values: f.values ? [...f.values] : [], increment: incr, }); } else if (isSized(value) || hasPrecision(value)) { diff --git a/src/components/table_overview.jsx b/src/components/table_overview.jsx index 08ee4e8..194270d 100644 --- a/src/components/table_overview.jsx +++ b/src/components/table_overview.jsx @@ -228,7 +228,7 @@ export default function TableOverview(props) { updateField(i, j, { type: value, default: "", - values: [], + values: f.values ? [...f.values] : [], increment: incr, }); } else if (isSized(value) || hasPrecision(value)) { diff --git a/src/utils/index.js b/src/utils/index.js index eb35dfb..bd338ef 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -47,20 +47,57 @@ function dataURItoBlob(dataUrl) { return new Blob([intArray], { type: mimeString }); } -function getTypeString(field) { - if (field.type === "UUID") { - return `VARCHAR(36)`; +function getTypeString(field, dbms = "mysql") { + if (dbms === "mysql") { + if (field.type === "UUID") { + return `VARCHAR(36)`; + } + if (isSized(field.type)) { + return `${field.type}(${field.size})`; + } + if (hasPrecision(field.type) && field.size !== "") { + return `${field.type}${field.size}`; + } + if (field.type === "SET" || field.type === "ENUM") { + return `${field.type}(${field.values.map((v) => `"${v}"`).join(", ")})`; + } + return field.type; + } else if (dbms === "postgres") { + if (field.type === "SMALLINT" && field.increment) { + return "smallserial"; + } + if (field.type === "INT" && field.increment) { + return "serial"; + } + if (field.type === "BIGINT" && field.increment) { + return "bigserial"; + } + if (field.type === "ENUM") { + return `${field.name}_t`; + } + if (field.type === "SET") { + return `${field.name}_t[]`; + } + if (field.type === "TIMESTAMP") { + return "TIMESTAMPTZ"; + } + if (field.type === "DATETIME") { + return `timestamp`; + } + if (isSized(field.type)) { + const type = + field.type === "BINARY" + ? "bit" + : field.type === "VARBINARY" + ? "bit varying" + : field.type.toLowerCase(); + return `${type}(${field.size})`; + } + if (hasPrecision(field.type) && field.size !== "") { + return `${field.type}${field.size}`; + } + return field.type.toLowerCase(); } - if (isSized(field.type)) { - return `${field.type}(${field.size})`; - } - if (hasPrecision(field.type) && field.size !== "") { - return `${field.type}${field.size}`; - } - if (field.type === "SET" || field.type === "ENUM") { - return `${field.type}(${field.values.map((v) => `"${v}"`).join(", ")})`; - } - return field.type; } function hasQuotes(type) { @@ -77,7 +114,7 @@ function hasQuotes(type) { ].includes(type); } -function jsonToSQL(obj) { +function jsonToMySQL(obj) { return `${obj.tables .map( (table) => @@ -139,6 +176,76 @@ function jsonToSQL(obj) { .join("\n")}`; } +function jsonToPostgreSQL(obj) { + return `${obj.tables + .map( + (table) => + `${table.comment === "" ? "" : `/**\n${table.comment}\n*/\n`}${ + table.fields.filter((f) => f.type === "ENUM" || f.type === "SET") + .length > 0 + ? `${table.fields + .filter((f) => f.type === "ENUM" || f.type === "SET") + .map( + (f) => + `CREATE TYPE "${f.name}_t" AS ENUM (${f.values + .map((v) => `'${v}'`) + .join(", ")});\n\n` + )}` + : "" + }CREATE TABLE "${table.name}" (\n${table.fields + .map( + (field) => + `${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${ + field.name + }" ${getTypeString(field, "postgres")}${ + field.notNull ? " NOT NULL" : "" + }${ + field.default !== "" + ? ` DEFAULT ${ + hasQuotes(field.type) && + field.default.toLowerCase() !== "null" + ? `'${field.default}'` + : `${field.default}` + }` + : "" + }${ + field.check === "" || !hasCheck(field.type) + ? "" + : ` CHECK(${field.check})` + }` + ) + .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);\n${ + table.indices.length > 0 + ? `${table.indices.map( + (i) => + `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX "${ + i.name + }"\nON "${table.name}" (${i.fields + .map((f) => `"${f}"`) + .join(", ")});` + )}` + : "" + }` + ) + .join("\n")}\n${obj.references + .map( + (r) => + `ALTER TABLE "${obj.tables[r.startTableId].name}"\nADD FOREIGN KEY("${ + obj.tables[r.startTableId].fields[r.startFieldId].name + }") REFERENCES "${obj.tables[r.endTableId].name}"("${ + obj.tables[r.endTableId].fields[r.endFieldId].name + }")\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};` + ) + .join("\n")}`; +} + function arrayIsEqual(arr1, arr2) { return JSON.stringify(arr1) === JSON.stringify(arr2); } @@ -383,7 +490,8 @@ export { jsonDiagramIsValid, ddbDiagramIsValid, dataURItoBlob, - jsonToSQL, + jsonToMySQL, + jsonToPostgreSQL, validateDiagram, arrayIsEqual, isSized,