mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-05-23 17:59:19 +00:00
Escape quotes in comments and default strings (#452)
This commit is contained in:
parent
80fb9e5080
commit
8f9aae14ca
@ -1,6 +1,6 @@
|
||||
import { DB } from "../../data/constants";
|
||||
import { dbToTypes, defaultTypes } from "../../data/datatypes";
|
||||
import { getInlineFK, parseDefault } from "./shared";
|
||||
import { escapeQuotes, getInlineFK, parseDefault } from "./shared";
|
||||
|
||||
export function getJsonType(f) {
|
||||
if (!Object.keys(defaultTypes).includes(f.type)) {
|
||||
@ -205,7 +205,7 @@ export function jsonToMySQL(obj) {
|
||||
)}", \`${field.name}\`))`
|
||||
: ""
|
||||
: ` CHECK(${field.check})`
|
||||
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`,
|
||||
}${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
|
||||
)
|
||||
.join(",\n")}${
|
||||
table.fields.filter((f) => f.primary).length > 0
|
||||
@ -214,7 +214,7 @@ export function jsonToMySQL(obj) {
|
||||
.map((f) => `\`${f.name}\``)
|
||||
.join(", ")})`
|
||||
: ""
|
||||
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};\n${`\n${table.indices
|
||||
}\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};\n${`\n${table.indices
|
||||
.map(
|
||||
(i) =>
|
||||
`CREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${i.name}\`\nON \`${table.name}\` (${i.fields
|
||||
@ -270,7 +270,7 @@ export function jsonToPostgreSQL(obj) {
|
||||
)
|
||||
.join(",\n")}\n);\n${
|
||||
type.comment && type.comment.trim() != ""
|
||||
? `\nCOMMENT ON TYPE ${type.name} IS '${type.comment}';\n`
|
||||
? `\nCOMMENT ON TYPE ${type.name} IS '${escapeQuotes(type.comment)}';\n`
|
||||
: ""
|
||||
}`;
|
||||
}
|
||||
@ -313,10 +313,10 @@ export function jsonToPostgreSQL(obj) {
|
||||
.map((f) => `"${f.name}"`)
|
||||
.join(", ")})`
|
||||
: ""
|
||||
}\n);\n${table.comment != "" ? `\nCOMMENT ON TABLE ${table.name} IS '${table.comment}';\n` : ""}${table.fields
|
||||
}\n);\n${table.comment != "" ? `\nCOMMENT ON TABLE ${table.name} IS '${escapeQuotes(table.comment)}';\n` : ""}${table.fields
|
||||
.map((field) =>
|
||||
field.comment.trim() !== ""
|
||||
? `COMMENT ON COLUMN ${table.name}.${field.name} IS '${field.comment}';\n`
|
||||
? `COMMENT ON COLUMN ${table.name}.${field.name} IS '${escapeQuotes(field.comment)}';\n`
|
||||
: "",
|
||||
)
|
||||
.join("")}\n${table.indices
|
||||
@ -448,7 +448,7 @@ export function jsonToMariaDB(obj) {
|
||||
)}', \`${field.name}\`))`
|
||||
: ""
|
||||
: ` CHECK(${field.check})`
|
||||
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`,
|
||||
}${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
|
||||
)
|
||||
.join(",\n")}${
|
||||
table.fields.filter((f) => f.primary).length > 0
|
||||
@ -457,7 +457,7 @@ export function jsonToMariaDB(obj) {
|
||||
.map((f) => `\`${f.name}\``)
|
||||
.join(", ")})`
|
||||
: ""
|
||||
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};${`\n${table.indices
|
||||
}\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};${`\n${table.indices
|
||||
.map(
|
||||
(i) =>
|
||||
`CREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { parseDefault } from "./shared";
|
||||
import { escapeQuotes, parseDefault } from "./shared";
|
||||
|
||||
import { dbToTypes } from "../../data/datatypes";
|
||||
import { DB } from "../../data/constants";
|
||||
@ -35,7 +35,7 @@ export function toMariaDB(diagram) {
|
||||
!dbToTypes[diagram.database][field.type].hasCheck
|
||||
? ""
|
||||
: ` CHECK(${field.check})`
|
||||
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`,
|
||||
}${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
|
||||
)
|
||||
.join(",\n")}${
|
||||
table.fields.filter((f) => f.primary).length > 0
|
||||
@ -44,7 +44,7 @@ export function toMariaDB(diagram) {
|
||||
.map((f) => `\`${f.name}\``)
|
||||
.join(", ")})`
|
||||
: ""
|
||||
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};${`\n${table.indices
|
||||
}\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};${`\n${table.indices
|
||||
.map(
|
||||
(i) =>
|
||||
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { parseDefault } from "./shared";
|
||||
import { escapeQuotes, parseDefault } from "./shared";
|
||||
|
||||
import { dbToTypes } from "../../data/datatypes";
|
||||
import { DB } from "../../data/constants";
|
||||
@ -37,7 +37,7 @@ export function toMySQL(diagram) {
|
||||
!dbToTypes[diagram.database][field.type].hasCheck
|
||||
? ""
|
||||
: ` CHECK(${field.check})`
|
||||
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`,
|
||||
}${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
|
||||
)
|
||||
.join(",\n")}${
|
||||
table.fields.filter((f) => f.primary).length > 0
|
||||
@ -46,7 +46,7 @@ export function toMySQL(diagram) {
|
||||
.map((f) => `\`${f.name}\``)
|
||||
.join(", ")})`
|
||||
: ""
|
||||
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};\n${`\n${table.indices
|
||||
}\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};\n${`\n${table.indices
|
||||
.map(
|
||||
(i) =>
|
||||
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { exportFieldComment, parseDefault } from "./shared";
|
||||
import { escapeQuotes, exportFieldComment, parseDefault } from "./shared";
|
||||
|
||||
import { dbToTypes } from "../../data/datatypes";
|
||||
|
||||
@ -17,7 +17,7 @@ export function toPostgres(diagram) {
|
||||
.map((f) => `\t${f.name} ${f.type}`)
|
||||
.join(",\n")}\n);\n\n${
|
||||
type.comment && type.comment.trim() !== ""
|
||||
? `\nCOMMENT ON TYPE "${type.name}" IS '${type.comment}';\n\n`
|
||||
? `\nCOMMENT ON TYPE "${type.name}" IS '${escapeQuotes(type.comment)}';\n\n`
|
||||
: ""
|
||||
}`,
|
||||
)
|
||||
@ -57,12 +57,12 @@ export function toPostgres(diagram) {
|
||||
: ""
|
||||
}\n);${
|
||||
table.comment.trim() !== ""
|
||||
? `\nCOMMENT ON TABLE "${table.name}" IS '${table.comment}';\n`
|
||||
? `\nCOMMENT ON TABLE "${table.name}" IS '${escapeQuotes(table.comment)}';\n`
|
||||
: ""
|
||||
}${table.fields
|
||||
.map((field) =>
|
||||
field.comment.trim() !== ""
|
||||
? `COMMENT ON COLUMN ${table.name}.${field.name} IS '${field.comment}';\n`
|
||||
? `COMMENT ON COLUMN ${table.name}.${field.name} IS '${escapeQuotes(field.comment)}';\n`
|
||||
: "",
|
||||
)
|
||||
.join("")}${table.indices
|
||||
|
@ -1,11 +1,10 @@
|
||||
import { isFunction, isKeyword, strHasQuotes } from "../utils";
|
||||
import { isFunction, isKeyword } from "../utils";
|
||||
|
||||
import { DB } from "../../data/constants";
|
||||
import { dbToTypes } from "../../data/datatypes";
|
||||
|
||||
export function parseDefault(field, database = DB.GENERIC) {
|
||||
if (
|
||||
strHasQuotes(field.default) ||
|
||||
isFunction(field.default) ||
|
||||
isKeyword(field.default) ||
|
||||
!dbToTypes[database][field.type].hasQuotes
|
||||
@ -13,7 +12,11 @@ export function parseDefault(field, database = DB.GENERIC) {
|
||||
return field.default;
|
||||
}
|
||||
|
||||
return `'${field.default}'`;
|
||||
return `'${escapeQuotes(field.default)}'`;
|
||||
}
|
||||
|
||||
export function escapeQuotes(str) {
|
||||
return str.replace(/[']/g, "'$&");
|
||||
}
|
||||
|
||||
export function exportFieldComment(comment) {
|
||||
|
Loading…
Reference in New Issue
Block a user