Skip to content

Commit

Permalink
Fix invalid sql after swapping fields (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Nov 2, 2024
1 parent cd26273 commit 317c3cc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/components/EditorSidePanel/TablesTab/TableInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down Expand Up @@ -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) => {
Expand Down
19 changes: 16 additions & 3 deletions src/utils/exportSQL/mariadb.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 !== ""
Expand Down
17 changes: 16 additions & 1 deletion src/utils/exportSQL/mysql.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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" : ""
Expand Down

0 comments on commit 317c3cc

Please sign in to comment.