From 317c3cc9bac6660a1a91a811a1934c4a14ba3026 Mon Sep 17 00:00:00 2001 From: 1ilit <1ilit@proton.me> Date: Sat, 2 Nov 2024 11:49:42 +0400 Subject: [PATCH] Fix invalid sql after swapping fields (#287) --- .../EditorSidePanel/TablesTab/TableInfo.jsx | 19 ++++++++++++++++--- src/utils/exportSQL/mariadb.js | 19 ++++++++++++++++--- src/utils/exportSQL/mysql.js | 17 ++++++++++++++++- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/components/EditorSidePanel/TablesTab/TableInfo.jsx b/src/components/EditorSidePanel/TablesTab/TableInfo.jsx index a832132..659669e 100644 --- a/src/components/EditorSidePanel/TablesTab/TableInfo.jsx +++ b/src/components/EditorSidePanel/TablesTab/TableInfo.jsx @@ -14,11 +14,12 @@ import ColorPalette from "../../ColorPicker"; import TableField from "./TableField"; import IndexDetails from "./IndexDetails"; import { useTranslation } from "react-i18next"; +import { dbToTypes } from "../../../data/datatypes"; export default function TableInfo({ data }) { const { t } = useTranslation(); const [indexActiveKey, setIndexActiveKey] = useState(""); - const { deleteTable, updateTable, updateField, setRelationships } = + const { deleteTable, updateTable, updateField, setRelationships, database } = useDiagram(); const { setUndoStack, setRedoStack } = useUndoRedo(); const [editField, setEditField] = useState({}); @@ -106,8 +107,20 @@ export default function TableInfo({ data }) { const a = data.fields[index]; const b = data.fields[j]; - updateField(data.id, index, { ...b, id: index }); - updateField(data.id, j, { ...a, id: j }); + updateField(data.id, index, { + ...b, + ...(!dbToTypes[database][b.type].isSized && { size: "" }), + ...(!dbToTypes[database][b.type].hasCheck && { check: "" }), + ...(dbToTypes[database][b.type].noDefault && { default: "" }), + id: index, + }); + updateField(data.id, j, { + ...a, + ...(!dbToTypes[database][a.type].isSized && { size: "" }), + ...(!dbToTypes[database][a.type].hasCheck && { check: "" }), + ...(!dbToTypes[database][a.type].noDefault && { default: "" }), + id: j, + }); setRelationships((prev) => prev.map((e) => { diff --git a/src/utils/exportSQL/mariadb.js b/src/utils/exportSQL/mariadb.js index b1cc3af..aebf4ab 100644 --- a/src/utils/exportSQL/mariadb.js +++ b/src/utils/exportSQL/mariadb.js @@ -1,6 +1,21 @@ import { parseDefault } from "./shared"; import { dbToTypes } from "../../data/datatypes"; +import { DB } from "../../data/constants"; + +function parseType(field) { + let res = field.type; + + if (field.type === "SET" || field.type === "ENUM") { + res += `${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}`; + } + + if (dbToTypes[DB.MARIADB][field.type].isSized) { + res += `${field.size && field.size !== "" ? "(" + field.size + ")" : ""}`; + } + + return res; +} export function toMariaDB(diagram) { return `${diagram.tables @@ -9,9 +24,7 @@ export function toMariaDB(diagram) { `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" : ""}${ + `\t\`${field.name}\` ${parseType(field)}${field.unsigned ? " UNSIGNED" : ""}${field.notNull ? " NOT NULL" : ""}${ field.increment ? " AUTO_INCREMENT" : "" }${field.unique ? " UNIQUE" : ""}${ field.default !== "" diff --git a/src/utils/exportSQL/mysql.js b/src/utils/exportSQL/mysql.js index e1228d4..28254ae 100644 --- a/src/utils/exportSQL/mysql.js +++ b/src/utils/exportSQL/mysql.js @@ -1,6 +1,21 @@ import { parseDefault } from "./shared"; import { dbToTypes } from "../../data/datatypes"; +import { DB } from "../../data/constants"; + +function parseType(field) { + let res = field.type; + + if (field.type === "SET" || field.type === "ENUM") { + res += `${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}`; + } + + if (dbToTypes[DB.MYSQL][field.type].isSized) { + res += `${field.size && field.size !== "" ? "(" + field.size + ")" : ""}`; + } + + return res; +} export function toMySQL(diagram) { return `${diagram.tables @@ -9,7 +24,7 @@ export function toMySQL(diagram) { `CREATE 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.size !== undefined && field.size !== "" ? "(" + field.size + ")" : ""}${ + `\t\`${field.name}\` ${parseType(field)}${field.unsigned ? " UNSIGNED" : ""}${ field.notNull ? " NOT NULL" : "" }${ field.increment ? " AUTO_INCREMENT" : ""