Export mysql for mysql diagrams
This commit is contained in:
parent
3f6dfe6437
commit
9ddc4316ea
@ -1,5 +1,6 @@
|
||||
import { DB } from "../../data/constants";
|
||||
import { toMariaDB } from "./mariadb";
|
||||
import { toMySQL } from "./mysql";
|
||||
import { toSqlite } from "./sqlite";
|
||||
|
||||
export function exportSQL(diagram) {
|
||||
@ -7,7 +8,7 @@ export function exportSQL(diagram) {
|
||||
case DB.SQLITE:
|
||||
return toSqlite(diagram);
|
||||
case DB.MYSQL:
|
||||
return "hi from mysql";
|
||||
return toMySQL(diagram);
|
||||
case DB.POSTGRES:
|
||||
return "hi from postgres";
|
||||
case DB.MARIADB:
|
||||
|
60
src/utils/exportSQL/mysql.js
Normal file
60
src/utils/exportSQL/mysql.js
Normal file
@ -0,0 +1,60 @@
|
||||
import { dbToTypes } from "../../data/datatypes";
|
||||
import { parseDefault } from "./shared";
|
||||
|
||||
export function toMySQL(diagram) {
|
||||
return `${diagram.tables
|
||||
.map(
|
||||
(table) =>
|
||||
`${
|
||||
table.comment === "" ? "" : `/* ${table.comment} */\n`
|
||||
}CREATE TABLE \`${table.name}\` (\n${table.fields
|
||||
.map(
|
||||
(field) =>
|
||||
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${
|
||||
field.name
|
||||
}\` ${field.type}${field.notNull ? " NOT NULL" : ""}${
|
||||
field.increment ? " AUTO_INCREMENT" : ""
|
||||
}${field.unique ? " UNIQUE" : ""}${
|
||||
field.default !== ""
|
||||
? ` DEFAULT ${parseDefault(field, diagram.database)}`
|
||||
: ""
|
||||
}${
|
||||
field.check === "" ||
|
||||
!dbToTypes[diagram.database][field.type].hasCheck
|
||||
? ""
|
||||
: ` CHECK(${field.check})`
|
||||
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`,
|
||||
)
|
||||
.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.comment ? ` COMMENT='${table.comment}'` : ""};\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${diagram.references
|
||||
.map(
|
||||
(r) =>
|
||||
`ALTER TABLE \`${
|
||||
diagram.tables[r.startTableId].name
|
||||
}\`\nADD FOREIGN KEY(\`${
|
||||
diagram.tables[r.startTableId].fields[r.startFieldId].name
|
||||
}\`) REFERENCES \`${diagram.tables[r.endTableId].name}\`(\`${
|
||||
diagram.tables[r.endTableId].fields[r.endFieldId].name
|
||||
}\`)\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`,
|
||||
)
|
||||
.join("\n")}`;
|
||||
}
|
Loading…
Reference in New Issue
Block a user