Skip to content

Commit

Permalink
Merge pull request #252 from csc530/main
Browse files Browse the repository at this point in the history
Fix export for enum and set types for mysql
  • Loading branch information
1ilit authored Oct 11, 2024
2 parents 2bf3bd6 + cd873ec commit 7d3d1dd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 64 deletions.
55 changes: 23 additions & 32 deletions src/utils/exportSQL/mariadb.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,40 @@ export function toMariaDB(diagram) {
`CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields
.map(
(field) =>
`\t\`${
field.name
}\` ${field.type}${field.unsigned ? " UNSIGNED" : ""}${field.notNull ? " NOT NULL" : ""}${
field.increment ? " AUTO_INCREMENT" : ""
}${field.unique ? " UNIQUE" : ""}${
field.default !== ""
? ` DEFAULT ${parseDefault(field, diagram.database)}`
: ""
}${
field.check === "" ||
`\t\`${field.name
}\` ${field.type}${field.values ? "(" + field.values.map(value => "'" + value + "'").join(", ") + ")" : ""}${field.unsigned ? " UNSIGNED" : ""}${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})`
? ""
: ` 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(", ")})`
: ""
.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
.map(
(i) =>
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${
i.name
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${i.name
}\`\nON \`${table.name}\` (${i.fields
.map((f) => `\`${f}\``)
.join(", ")});`,
)
.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")}`;
.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")}`;
}
55 changes: 23 additions & 32 deletions src/utils/exportSQL/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,40 @@ export function toMySQL(diagram) {
`CREATE TABLE \`${table.name}\` (\n${table.fields
.map(
(field) =>
`\t\`${field.name}\` ${field.type}${field.unsigned ? " UNSIGNED" : ""}${field.size !== undefined && field.size !== "" ? "(" + field.size + ")" : ""}${
field.notNull ? " NOT NULL" : ""
}${
field.increment ? " AUTO_INCREMENT" : ""
}${field.unique ? " UNIQUE" : ""}${
field.default !== ""
? ` DEFAULT ${parseDefault(field, diagram.database)}`
: ""
}${
field.check === "" ||
`\t\`${field.name}\` ${field.type}${field.values ? "(" + field.values.map(value => "'" + value + "'").join(", ") + ")" : ""}${field.unsigned ? " UNSIGNED" : ""}${field.size !== undefined && field.size !== "" ? "(" + field.size + ")" : ""}${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})`
? ""
: ` 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(", ")})`
: ""
.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${`\n${table.indices
.map(
(i) =>
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${
i.name
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${i.name
}\`\nON \`${table.name}\` (${i.fields
.map((f) => `\`${f}\``)
.join(", ")});`,
)
.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")}`;
.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")}`;
}

0 comments on commit 7d3d1dd

Please sign in to comment.