From 7e1c8fc1ca4eb7b967eb1576a415085c151becea Mon Sep 17 00:00:00 2001 From: 1ilit Date: Mon, 5 Feb 2024 13:40:59 +0200 Subject: [PATCH] Add MariaDB --- src/components/ControlPanel.jsx | 22 +++++++++-- src/utils/index.js | 65 ++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/components/ControlPanel.jsx b/src/components/ControlPanel.jsx index d138794..a4470b1 100644 --- a/src/components/ControlPanel.jsx +++ b/src/components/ControlPanel.jsx @@ -45,7 +45,8 @@ import { dataURItoBlob, jsonToMySQL, jsonToPostgreSQL, - jsonToSQLite + jsonToSQLite, + jsonToMariaDB } from "../utils"; import { AreaContext, @@ -994,6 +995,21 @@ export default function ControlPanel({ })); }, }, + { + MariaDB: () => { + setVisible(MODAL.CODE); + const src = jsonToMariaDB({ + tables: tables, + references: relationships, + types: types, + }); + setExportData((prev) => ({ + ...prev, + data: src, + extension: "sql", + })); + }, + }, { DBML: () => { } }, ], function: () => { }, @@ -1069,9 +1085,9 @@ export default function ControlPanel({ }, "Presentation mode": { function: () => { - setLayout(prev => ({ + setLayout(prev => ({ ...prev, - header: false, + header: false, sidebar: false, toolbar: false, })); diff --git a/src/utils/index.js b/src/utils/index.js index a92f7e0..2c29abc 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -49,14 +49,10 @@ function dataURItoBlob(dataUrl) { } function getJsonType(f) { - if (!sqlDataTypes.includes(f.type)) { - return '{ "type" : "object", additionalProperties : true }'; - } switch (f.type) { case "INT": case "SMALLINT": case "BIGINT": - return '{ "type" : "integer" }'; case "DECIMAL": case "NUMERIC": case "REAL": @@ -80,7 +76,7 @@ function getJsonType(f) { } function generateSchema(type) { - return `{\n\t\t\t"$schema": "http://json-schema.org/draft-04/schema#",\n\t\t\t"type": "object",\n\t\t\t"properties": {\n\t\t\t\t${type.fields + return `{\n\t\t\t"type": "object",\n\t\t\t"properties": {\n\t\t\t\t${type.fields .map((f) => `"${f.name}" : ${getJsonType(f)}`) .join( ",\n\t\t\t\t" @@ -378,6 +374,62 @@ function jsonToSQLite(obj) { ).join("\n"); } +function jsonToMariaDB(obj) { + return `${obj.tables + .map( + (table) => + `${table.comment === "" ? "" : `/* ${table.comment} */\n` + }CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields + .map( + (field) => + `${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${field.name + }\` ${getTypeString(field)}${field.notNull ? " NOT NULL" : ""}${field.increment ? " AUTO_INCREMENT" : "" + }${field.unique ? " UNIQUE" : ""}${field.default !== "" + ? ` DEFAULT ${hasQuotes(field.type) && + field.default.toLowerCase() !== "null" + ? `"${field.default}"` + : `${field.default}` + }` + : "" + }${field.check === "" || !hasCheck(field.type) + ? !sqlDataTypes.includes(field.type) + ? ` CHECK(\n\t\tJSON_SCHEMA_VALID('${generateSchema( + obj.types.find( + (t) => t.name === field.type.toLowerCase() + ) + )}', \`${field.name}\`))` + : "" + : ` 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);${table.indices.length > 0 + ? `\n${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); } @@ -758,5 +810,6 @@ export { validateDateStr, hasCheck, calcPath, - jsonToSQLite + jsonToSQLite, + jsonToMariaDB, };