From 432df7f1dab9be193eb3a02138d06b78d0a56247 Mon Sep 17 00:00:00 2001 From: nktfh100 Date: Sat, 3 Aug 2024 14:22:56 +0300 Subject: [PATCH] Fix multi line field comments --- src/utils/exportSQL/mariadb.js | 5 +++-- src/utils/exportSQL/mssql.js | 5 +++-- src/utils/exportSQL/mysql.js | 7 ++++--- src/utils/exportSQL/postgres.js | 5 +++-- src/utils/exportSQL/shared.js | 14 +++++++++++++- src/utils/exportSQL/sqlite.js | 5 +++-- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/utils/exportSQL/mariadb.js b/src/utils/exportSQL/mariadb.js index 3f2ef0ad..58d35f8d 100644 --- a/src/utils/exportSQL/mariadb.js +++ b/src/utils/exportSQL/mariadb.js @@ -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 @@ -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" : "" diff --git a/src/utils/exportSQL/mssql.js b/src/utils/exportSQL/mssql.js index 3ac2f2af..2db0ad56 100644 --- a/src/utils/exportSQL/mssql.js +++ b/src/utils/exportSQL/mssql.js @@ -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 @@ -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" : "" diff --git a/src/utils/exportSQL/mysql.js b/src/utils/exportSQL/mysql.js index f8a5eff4..9f804011 100644 --- a/src/utils/exportSQL/mysql.js +++ b/src/utils/exportSQL/mysql.js @@ -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 @@ -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" : "" diff --git a/src/utils/exportSQL/postgres.js b/src/utils/exportSQL/postgres.js index 5265a550..4dd311b7 100644 --- a/src/utils/exportSQL/postgres.js +++ b/src/utils/exportSQL/postgres.js @@ -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 @@ -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() !== "" diff --git a/src/utils/exportSQL/shared.js b/src/utils/exportSQL/shared.js index 83c4dce4..c1df0c1a 100644 --- a/src/utils/exportSQL/shared.js +++ b/src/utils/exportSQL/shared.js @@ -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 ( @@ -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(""); +} diff --git a/src/utils/exportSQL/sqlite.js b/src/utils/exportSQL/sqlite.js index e3fa4fca..916c1514 100644 --- a/src/utils/exportSQL/sqlite.js +++ b/src/utils/exportSQL/sqlite.js @@ -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 @@ -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" : ""