Fix multi line field comments

This commit is contained in:
nktfh100 2024-08-03 14:22:56 +03:00
parent a62a6ba295
commit 432df7f1da
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 { 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" : ""

View File

@ -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" : ""

View File

@ -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" : ""

View File

@ -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() !== ""

View File

@ -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("");
}

View File

@ -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" : ""