Merge pull request #206 from nktfh100/fix-multi-line-field-comments

Fix multi line field comments in SQL export.
This commit is contained in:
1ilit 2024-08-04 13:41:42 +04:00 committed by GitHub
commit 318ed9a0ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 12 deletions

View File

@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";
export function toMariaDB(diagram) { export function toMariaDB(diagram) {
return `${diagram.tables return `${diagram.tables
@ -10,7 +11,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) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${ `${exportFieldComment(field.comment)}\t\`${
field.name field.name
}\` ${field.type}${field.notNull ? " NOT NULL" : ""}${ }\` ${field.type}${field.notNull ? " NOT NULL" : ""}${
field.increment ? " AUTO_INCREMENT" : "" field.increment ? " AUTO_INCREMENT" : ""

View File

@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";
export function toMSSQL(diagram) { export function toMSSQL(diagram) {
return `${diagram.tables return `${diagram.tables
@ -10,7 +11,7 @@ export function toMSSQL(diagram) {
}CREATE TABLE [${table.name}] (\n${table.fields }CREATE TABLE [${table.name}] (\n${table.fields
.map( .map(
(field) => (field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t[${ `${exportFieldComment(field.comment)}\t[${
field.name field.name
}] ${field.type}${ }] ${field.type}${
field.notNull ? " NOT NULL" : "" field.notNull ? " NOT NULL" : ""

View File

@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";
export function toMySQL(diagram) { export function toMySQL(diagram) {
return `${diagram.tables return `${diagram.tables
@ -10,9 +11,9 @@ export function toMySQL(diagram) {
}CREATE TABLE \`${table.name}\` (\n${table.fields }CREATE TABLE \`${table.name}\` (\n${table.fields
.map( .map(
(field) => (field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${ `${exportFieldComment(field.comment)}\t\`${
field.name 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.notNull ? " NOT NULL" : ""
}${ }${
field.increment ? " AUTO_INCREMENT" : "" field.increment ? " AUTO_INCREMENT" : ""

View File

@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";
export function toPostgres(diagram) { export function toPostgres(diagram) {
const enumStatements = diagram.enums const enumStatements = diagram.enums
@ -28,7 +29,7 @@ export function toPostgres(diagram) {
`CREATE TABLE "${table.name}" (\n${table.fields `CREATE TABLE "${table.name}" (\n${table.fields
.map( .map(
(field) => (field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${ `${exportFieldComment(field.comment)}\t"${
field.name field.name
}" ${field.type}${field.isArray ? " ARRAY" : ""}${field.notNull ? " NOT NULL" : ""}${field.unique ? " UNIQUE" : ""}${ }" ${field.type}${field.isArray ? " ARRAY" : ""}${field.notNull ? " NOT NULL" : ""}${field.unique ? " UNIQUE" : ""}${
field.default.trim() !== "" field.default.trim() !== ""

View File

@ -1,6 +1,7 @@
import { isFunction, isKeyword, strHasQuotes } from "../utils";
import { DB } from "../../data/constants"; import { DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { isFunction, isKeyword, strHasQuotes } from "../utils";
export function parseDefault(field, database = DB.GENERIC) { export function parseDefault(field, database = DB.GENERIC) {
if ( if (
@ -14,3 +15,14 @@ export function parseDefault(field, database = DB.GENERIC) {
return `'${field.default}'`; return `'${field.default}'`;
} }
export function exportFieldComment(comment) {
if (comment === "") {
return "";
}
return comment
.split("\n")
.map((commentLine) => `\t-- ${commentLine}\n`)
.join("");
}

View File

@ -1,5 +1,6 @@
import { exportFieldComment, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";
export function toSqlite(diagram) { export function toSqlite(diagram) {
return diagram.tables return diagram.tables
@ -10,7 +11,7 @@ export function toSqlite(diagram) {
}CREATE TABLE IF NOT EXISTS "${table.name}" (\n${table.fields }CREATE TABLE IF NOT EXISTS "${table.name}" (\n${table.fields
.map( .map(
(field) => (field) =>
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${ `${exportFieldComment(field.comment)}\t"${
field.name field.name
}" ${field.type}${field.notNull ? " NOT NULL" : ""}${ }" ${field.type}${field.notNull ? " NOT NULL" : ""}${
field.unique ? " UNIQUE" : "" field.unique ? " UNIQUE" : ""