mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-05-24 10:29:11 +00:00
Fix invalid sql after swapping fields (#287)
This commit is contained in:
parent
cd2627332f
commit
317c3cc9ba
@ -14,11 +14,12 @@ import ColorPalette from "../../ColorPicker";
|
|||||||
import TableField from "./TableField";
|
import TableField from "./TableField";
|
||||||
import IndexDetails from "./IndexDetails";
|
import IndexDetails from "./IndexDetails";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { dbToTypes } from "../../../data/datatypes";
|
||||||
|
|
||||||
export default function TableInfo({ data }) {
|
export default function TableInfo({ data }) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [indexActiveKey, setIndexActiveKey] = useState("");
|
const [indexActiveKey, setIndexActiveKey] = useState("");
|
||||||
const { deleteTable, updateTable, updateField, setRelationships } =
|
const { deleteTable, updateTable, updateField, setRelationships, database } =
|
||||||
useDiagram();
|
useDiagram();
|
||||||
const { setUndoStack, setRedoStack } = useUndoRedo();
|
const { setUndoStack, setRedoStack } = useUndoRedo();
|
||||||
const [editField, setEditField] = useState({});
|
const [editField, setEditField] = useState({});
|
||||||
@ -106,8 +107,20 @@ export default function TableInfo({ data }) {
|
|||||||
const a = data.fields[index];
|
const a = data.fields[index];
|
||||||
const b = data.fields[j];
|
const b = data.fields[j];
|
||||||
|
|
||||||
updateField(data.id, index, { ...b, id: index });
|
updateField(data.id, index, {
|
||||||
updateField(data.id, j, { ...a, id: j });
|
...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) =>
|
setRelationships((prev) =>
|
||||||
prev.map((e) => {
|
prev.map((e) => {
|
||||||
|
@ -1,6 +1,21 @@
|
|||||||
import { parseDefault } from "./shared";
|
import { parseDefault } from "./shared";
|
||||||
|
|
||||||
import { dbToTypes } from "../../data/datatypes";
|
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) {
|
export function toMariaDB(diagram) {
|
||||||
return `${diagram.tables
|
return `${diagram.tables
|
||||||
@ -9,9 +24,7 @@ export function toMariaDB(diagram) {
|
|||||||
`CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields
|
`CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields
|
||||||
.map(
|
.map(
|
||||||
(field) =>
|
(field) =>
|
||||||
`\t\`${
|
`\t\`${field.name}\` ${parseType(field)}${field.unsigned ? " UNSIGNED" : ""}${field.notNull ? " NOT NULL" : ""}${
|
||||||
field.name
|
|
||||||
}\` ${field.type}${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}${field.unsigned ? " UNSIGNED" : ""}${field.notNull ? " NOT NULL" : ""}${
|
|
||||||
field.increment ? " AUTO_INCREMENT" : ""
|
field.increment ? " AUTO_INCREMENT" : ""
|
||||||
}${field.unique ? " UNIQUE" : ""}${
|
}${field.unique ? " UNIQUE" : ""}${
|
||||||
field.default !== ""
|
field.default !== ""
|
||||||
|
@ -1,6 +1,21 @@
|
|||||||
import { parseDefault } from "./shared";
|
import { parseDefault } from "./shared";
|
||||||
|
|
||||||
import { dbToTypes } from "../../data/datatypes";
|
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) {
|
export function toMySQL(diagram) {
|
||||||
return `${diagram.tables
|
return `${diagram.tables
|
||||||
@ -9,7 +24,7 @@ export function toMySQL(diagram) {
|
|||||||
`CREATE TABLE \`${table.name}\` (\n${table.fields
|
`CREATE TABLE \`${table.name}\` (\n${table.fields
|
||||||
.map(
|
.map(
|
||||||
(field) =>
|
(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.notNull ? " NOT NULL" : ""
|
||||||
}${
|
}${
|
||||||
field.increment ? " AUTO_INCREMENT" : ""
|
field.increment ? " AUTO_INCREMENT" : ""
|
||||||
|
Loading…
Reference in New Issue
Block a user