Escape quotes

This commit is contained in:
1ilit 2025-05-18 18:13:45 +04:00
parent 80fb9e5080
commit a4e473eb65
5 changed files with 24 additions and 21 deletions

View File

@ -1,6 +1,6 @@
import { DB } from "../../data/constants"; import { DB } from "../../data/constants";
import { dbToTypes, defaultTypes } from "../../data/datatypes"; import { dbToTypes, defaultTypes } from "../../data/datatypes";
import { getInlineFK, parseDefault } from "./shared"; import { escapeQuotes, getInlineFK, parseDefault } from "./shared";
export function getJsonType(f) { export function getJsonType(f) {
if (!Object.keys(defaultTypes).includes(f.type)) { if (!Object.keys(defaultTypes).includes(f.type)) {
@ -205,7 +205,7 @@ export function jsonToMySQL(obj) {
)}", \`${field.name}\`))` )}", \`${field.name}\`))`
: "" : ""
: ` CHECK(${field.check})` : ` CHECK(${field.check})`
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`, }${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
) )
.join(",\n")}${ .join(",\n")}${
table.fields.filter((f) => f.primary).length > 0 table.fields.filter((f) => f.primary).length > 0
@ -214,7 +214,7 @@ export function jsonToMySQL(obj) {
.map((f) => `\`${f.name}\``) .map((f) => `\`${f.name}\``)
.join(", ")})` .join(", ")})`
: "" : ""
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};\n${`\n${table.indices }\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};\n${`\n${table.indices
.map( .map(
(i) => (i) =>
`CREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${i.name}\`\nON \`${table.name}\` (${i.fields `CREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${i.name}\`\nON \`${table.name}\` (${i.fields
@ -270,7 +270,7 @@ export function jsonToPostgreSQL(obj) {
) )
.join(",\n")}\n);\n${ .join(",\n")}\n);\n${
type.comment && type.comment.trim() != "" 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}"`) .map((f) => `"${f.name}"`)
.join(", ")})` .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) => .map((field) =>
field.comment.trim() !== "" 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 .join("")}\n${table.indices
@ -448,7 +448,7 @@ export function jsonToMariaDB(obj) {
)}', \`${field.name}\`))` )}', \`${field.name}\`))`
: "" : ""
: ` CHECK(${field.check})` : ` CHECK(${field.check})`
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`, }${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
) )
.join(",\n")}${ .join(",\n")}${
table.fields.filter((f) => f.primary).length > 0 table.fields.filter((f) => f.primary).length > 0
@ -457,7 +457,7 @@ export function jsonToMariaDB(obj) {
.map((f) => `\`${f.name}\``) .map((f) => `\`${f.name}\``)
.join(", ")})` .join(", ")})`
: "" : ""
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};${`\n${table.indices }\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};${`\n${table.indices
.map( .map(
(i) => (i) =>
`CREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${ `CREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${

View File

@ -1,4 +1,4 @@
import { parseDefault } from "./shared"; import { escapeQuotes, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { DB } from "../../data/constants"; import { DB } from "../../data/constants";
@ -35,7 +35,7 @@ export function toMariaDB(diagram) {
!dbToTypes[diagram.database][field.type].hasCheck !dbToTypes[diagram.database][field.type].hasCheck
? "" ? ""
: ` CHECK(${field.check})` : ` CHECK(${field.check})`
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`, }${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
) )
.join(",\n")}${ .join(",\n")}${
table.fields.filter((f) => f.primary).length > 0 table.fields.filter((f) => f.primary).length > 0
@ -44,7 +44,7 @@ export function toMariaDB(diagram) {
.map((f) => `\`${f.name}\``) .map((f) => `\`${f.name}\``)
.join(", ")})` .join(", ")})`
: "" : ""
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};${`\n${table.indices }\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};${`\n${table.indices
.map( .map(
(i) => (i) =>
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${ `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${

View File

@ -1,4 +1,4 @@
import { parseDefault } from "./shared"; import { escapeQuotes, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
import { DB } from "../../data/constants"; import { DB } from "../../data/constants";
@ -37,7 +37,7 @@ export function toMySQL(diagram) {
!dbToTypes[diagram.database][field.type].hasCheck !dbToTypes[diagram.database][field.type].hasCheck
? "" ? ""
: ` CHECK(${field.check})` : ` CHECK(${field.check})`
}${field.comment ? ` COMMENT '${field.comment}'` : ""}`, }${field.comment ? ` COMMENT '${escapeQuotes(field.comment)}'` : ""}`,
) )
.join(",\n")}${ .join(",\n")}${
table.fields.filter((f) => f.primary).length > 0 table.fields.filter((f) => f.primary).length > 0
@ -46,7 +46,7 @@ export function toMySQL(diagram) {
.map((f) => `\`${f.name}\``) .map((f) => `\`${f.name}\``)
.join(", ")})` .join(", ")})`
: "" : ""
}\n)${table.comment ? ` COMMENT='${table.comment}'` : ""};\n${`\n${table.indices }\n)${table.comment ? ` COMMENT='${escapeQuotes(table.comment)}'` : ""};\n${`\n${table.indices
.map( .map(
(i) => (i) =>
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${ `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${

View File

@ -1,4 +1,4 @@
import { exportFieldComment, parseDefault } from "./shared"; import { escapeQuotes, exportFieldComment, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
@ -17,7 +17,7 @@ export function toPostgres(diagram) {
.map((f) => `\t${f.name} ${f.type}`) .map((f) => `\t${f.name} ${f.type}`)
.join(",\n")}\n);\n\n${ .join(",\n")}\n);\n\n${
type.comment && type.comment.trim() !== "" 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);${ }\n);${
table.comment.trim() !== "" table.comment.trim() !== ""
? `\nCOMMENT ON TABLE "${table.name}" IS '${table.comment}';\n` ? `\nCOMMENT ON TABLE "${table.name}" IS '${escapeQuotes(table.comment)}';\n`
: "" : ""
}${table.fields }${table.fields
.map((field) => .map((field) =>
field.comment.trim() !== "" 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 .join("")}${table.indices

View File

@ -1,11 +1,10 @@
import { isFunction, isKeyword, strHasQuotes } from "../utils"; import { isFunction, isKeyword } from "../utils";
import { DB } from "../../data/constants"; import { DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
export function parseDefault(field, database = DB.GENERIC) { export function parseDefault(field, database = DB.GENERIC) {
if ( if (
strHasQuotes(field.default) ||
isFunction(field.default) || isFunction(field.default) ||
isKeyword(field.default) || isKeyword(field.default) ||
!dbToTypes[database][field.type].hasQuotes !dbToTypes[database][field.type].hasQuotes
@ -13,7 +12,11 @@ export function parseDefault(field, database = DB.GENERIC) {
return field.default; return field.default;
} }
return `'${field.default}'`; return `'${escapeQuotes(field.default)}'`;
}
export function escapeQuotes(str) {
return str.replace(/[']/g, "'$&");
} }
export function exportFieldComment(comment) { export function exportFieldComment(comment) {