Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi line field comments in SQL export. #206

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/utils/exportSQL/mariadb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";

import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";

export function toMariaDB(diagram) {
return `${diagram.tables
Expand All @@ -10,7 +11,7 @@ export function toMariaDB(diagram) {
}CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields
.map(
(field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${
`${exportFieldComment(field.comment)}\t\`${
field.name
}\` ${field.type}${field.notNull ? " NOT NULL" : ""}${
field.increment ? " AUTO_INCREMENT" : ""
Expand Down
5 changes: 3 additions & 2 deletions src/utils/exportSQL/mssql.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";

import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";

export function toMSSQL(diagram) {
return `${diagram.tables
Expand All @@ -10,7 +11,7 @@ export function toMSSQL(diagram) {
}CREATE TABLE [${table.name}] (\n${table.fields
.map(
(field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t[${
`${exportFieldComment(field.comment)}\t[${
field.name
}] ${field.type}${
field.notNull ? " NOT NULL" : ""
Expand Down
7 changes: 4 additions & 3 deletions src/utils/exportSQL/mysql.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";

import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";

export function toMySQL(diagram) {
return `${diagram.tables
Expand All @@ -10,9 +11,9 @@ export function toMySQL(diagram) {
}CREATE TABLE \`${table.name}\` (\n${table.fields
.map(
(field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${
`${exportFieldComment(field.comment)}\t\`${
field.name
}\` ${field.type}${(field.size !== undefined && field.size !== "")? "(" + field.size + ")" : ""}${
}\` ${field.type}${field.size !== undefined && field.size !== "" ? "(" + field.size + ")" : ""}${
field.notNull ? " NOT NULL" : ""
}${
field.increment ? " AUTO_INCREMENT" : ""
Expand Down
5 changes: 3 additions & 2 deletions src/utils/exportSQL/postgres.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";

import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";

export function toPostgres(diagram) {
const enumStatements = diagram.enums
Expand Down Expand Up @@ -28,7 +29,7 @@ export function toPostgres(diagram) {
`CREATE TABLE "${table.name}" (\n${table.fields
.map(
(field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${
`${exportFieldComment(field.comment)}\t"${
field.name
}" ${field.type}${field.isArray ? " ARRAY" : ""}${field.notNull ? " NOT NULL" : ""}${field.unique ? " UNIQUE" : ""}${
field.default.trim() !== ""
Expand Down
14 changes: 13 additions & 1 deletion src/utils/exportSQL/shared.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isFunction, isKeyword, strHasQuotes } from "../utils";

import { DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes";
import { isFunction, isKeyword, strHasQuotes } from "../utils";

export function parseDefault(field, database = DB.GENERIC) {
if (
Expand All @@ -14,3 +15,14 @@ export function parseDefault(field, database = DB.GENERIC) {

return `'${field.default}'`;
}

export function exportFieldComment(comment) {
if (comment === "") {
return "";
}

return comment
.split("\n")
.map((commentLine) => `\t-- ${commentLine}\n`)
.join("");
}
5 changes: 3 additions & 2 deletions src/utils/exportSQL/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";

import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";

export function toSqlite(diagram) {
return diagram.tables
Expand All @@ -10,7 +11,7 @@ export function toSqlite(diagram) {
}CREATE TABLE IF NOT EXISTS "${table.name}" (\n${table.fields
.map(
(field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${
`${exportFieldComment(field.comment)}\t"${
field.name
}" ${field.type}${field.notNull ? " NOT NULL" : ""}${
field.unique ? " UNIQUE" : ""
Expand Down
Loading