Support empty strings and strings with quotes for default (#10)

This commit is contained in:
1ilit 2024-04-10 04:57:07 +03:00
parent 9c34e6c40d
commit 895059c76a
3 changed files with 66 additions and 77 deletions

View File

@ -1,6 +1,8 @@
import { strHasQuotes } from "./utils";
function validateDateStr(str) {
return /^(?!0000)(?!00)(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[01])|(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31))$/.test(
str
str,
);
}
@ -17,6 +19,9 @@ function checkDefault(field) {
return field.values.includes(field.default);
case "CHAR":
case "VARCHAR":
if (strHasQuotes(field.default)) {
return field.default.length - 2 <= field.size;
}
return field.default.length <= field.size;
case "BINARY":
case "VARBINARY":
@ -94,26 +99,26 @@ export function getIssues(diagram) {
} else if (field.type === "ENUM" || field.type === "SET") {
if (!field.values || field.values.length === 0) {
issues.push(
`"${field.name}" field of table "${table.name}" is of type ${field.type} but no values have been specified`
`"${field.name}" field of table "${table.name}" is of type ${field.type} but no values have been specified`,
);
}
}
if (!checkDefault(field)) {
issues.push(
`Default value for field "${field.name}" in table "${table.name}" does not match its type.`
`Default value for field "${field.name}" in table "${table.name}" does not match its type.`,
);
}
if (field.notNull && field.default.toLowerCase() === "null") {
issues.push(
`"${field.name}" field of table "${table.name}" is NOT NULL but has default NULL`
`"${field.name}" field of table "${table.name}" is NOT NULL but has default NULL`,
);
}
if (field.type === "DOUBLE" && field.size !== "") {
issues.push(
`Specifying number of digits for floating point data types is deprecated.`
`Specifying number of digits for floating point data types is deprecated.`,
);
}
@ -172,14 +177,14 @@ export function getIssues(diagram) {
} else if (field.type === "ENUM" || field.type === "SET") {
if (!field.values || field.values.length === 0) {
issues.push(
`"${field.name}" field of type "${type.name}" is of type ${field.type} but no values have been specified`
`"${field.name}" field of type "${type.name}" is of type ${field.type} but no values have been specified`,
);
}
}
if (field.type === "DOUBLE" && field.size !== "") {
issues.push(
`Specifying number of digits for floating point data types is deprecated.`
`Specifying number of digits for floating point data types is deprecated.`,
);
}
@ -217,7 +222,7 @@ export function getIssues(diagram) {
function checkCircularRelationships(tableId, visited = []) {
if (visited.includes(tableId)) {
issues.push(
`Circular relationship involving table: "${diagram.tables[tableId].name}"`
`Circular relationship involving table: "${diagram.tables[tableId].name}"`,
);
return;
}

View File

@ -1,4 +1,5 @@
import { sqlDataTypes } from "../data/constants";
import { strHasQuotes } from "./utils";
export function getJsonType(f) {
if (!sqlDataTypes.includes(f.type)) {
@ -34,7 +35,7 @@ export function generateSchema(type) {
return `{\n\t\t\t"$schema": "http://json-schema.org/draft-04/schema#",\n\t\t\t"type": "object",\n\t\t\t"properties": {\n\t\t\t\t${type.fields
.map((f) => `"${f.name}" : ${getJsonType(f)}`)
.join(
",\n\t\t\t\t"
",\n\t\t\t\t",
)}\n\t\t\t},\n\t\t\t"additionalProperties": false\n\t\t}`;
}
@ -145,6 +146,16 @@ export function hasQuotes(type) {
].includes(type);
}
export function parseDefault(field) {
if (strHasQuotes(field.default)) {
return field.default;
}
return hasQuotes(field.type) && field.default.toLowerCase() !== "null"
? `'${field.default}'`
: `${field.default}`;
}
export function jsonToMySQL(obj) {
return `${obj.tables
.map(
@ -159,25 +170,18 @@ export function jsonToMySQL(obj) {
}\` ${getTypeString(field)}${field.notNull ? " NOT NULL" : ""}${
field.increment ? " AUTO_INCREMENT" : ""
}${field.unique ? " UNIQUE" : ""}${
field.default !== ""
? ` DEFAULT ${
hasQuotes(field.type) &&
field.default.toLowerCase() !== "null"
? `"${field.default}"`
: `${field.default}`
}`
: ""
field.default !== "" ? ` DEFAULT ${parseDefault(field)}` : ""
}${
field.check === "" || !hasCheck(field.type)
? !sqlDataTypes.includes(field.type)
? ` CHECK(\n\t\tJSON_SCHEMA_VALID("${generateSchema(
obj.types.find(
(t) => t.name === field.type.toLowerCase()
)
(t) => t.name === field.type.toLowerCase(),
),
)}", \`${field.name}\`))`
: ""
: ` CHECK(${field.check})`
}`
}`,
)
.join(",\n")}${
table.fields.filter((f) => f.primary).length > 0
@ -194,10 +198,10 @@ export function jsonToMySQL(obj) {
i.name
}\`\nON \`${table.name}\` (${i.fields
.map((f) => `\`${f}\``)
.join(", ")});`
.join(", ")});`,
)}`
: ""
}`
}`,
)
.join("\n")}\n${obj.references
.map(
@ -208,7 +212,7 @@ export function jsonToMySQL(obj) {
obj.tables[r.startTableId].fields[r.startFieldId].name
}\`) REFERENCES \`${obj.tables[r.endTableId].name}\`(\`${
obj.tables[r.endTableId].fields[r.endFieldId].name
}\`)\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`
}\`)\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`,
)
.join("\n")}`;
}
@ -221,7 +225,7 @@ export function jsonToPostgreSQL(obj) {
(f) =>
`CREATE TYPE "${f.name}_t" AS ENUM (${f.values
.map((v) => `'${v}'`)
.join(", ")});\n`
.join(", ")});\n`,
);
if (typeStatements.length > 0) {
return (
@ -251,7 +255,7 @@ export function jsonToPostgreSQL(obj) {
(f) =>
`CREATE TYPE "${f.name}_t" AS ENUM (${f.values
.map((v) => `'${v}'`)
.join(", ")});\n\n`
.join(", ")});\n\n`,
)}`
: ""
}CREATE TABLE "${table.name}" (\n${table.fields
@ -262,19 +266,12 @@ export function jsonToPostgreSQL(obj) {
}" ${getTypeString(field, "postgres")}${
field.notNull ? " NOT NULL" : ""
}${
field.default !== ""
? ` DEFAULT ${
hasQuotes(field.type) &&
field.default.toLowerCase() !== "null"
? `'${field.default}'`
: `${field.default}`
}`
: ""
field.default !== "" ? ` DEFAULT ${parseDefault(field)}` : ""
}${
field.check === "" || !hasCheck(field.type)
? ""
: ` CHECK(${field.check})`
}`
}`,
)
.join(",\n")}${
table.fields.filter((f) => f.primary).length > 0
@ -291,10 +288,10 @@ export function jsonToPostgreSQL(obj) {
i.name
}"\nON "${table.name}" (${i.fields
.map((f) => `"${f}"`)
.join(", ")});`
.join(", ")});`,
)}`
: ""
}`
}`,
)
.join("\n")}\n${obj.references
.map(
@ -303,7 +300,7 @@ export function jsonToPostgreSQL(obj) {
obj.tables[r.startTableId].fields[r.startFieldId].name
}") REFERENCES "${obj.tables[r.endTableId].name}"("${
obj.tables[r.endTableId].fields[r.endFieldId].name
}")\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`
}")\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`,
)
.join("\n")}`;
}
@ -369,20 +366,11 @@ export function jsonToSQLite(obj) {
field.name
}" ${getSQLiteType(field)}${field.notNull ? " NOT NULL" : ""}${
field.unique ? " UNIQUE" : ""
}${
field.default !== ""
? ` DEFAULT ${
hasQuotes(field.type) &&
field.default.toLowerCase() !== "null"
? `'${field.default}'`
: `${field.default}`
}`
: ""
}${
}${field.default !== "" ? ` DEFAULT ${parseDefault(field)}` : ""}${
field.check === "" || !hasCheck(field.type)
? ""
: ` CHECK(${field.check})`
}`
}`,
)
.join(",\n")}${
table.fields.filter((f) => f.primary).length > 0
@ -400,7 +388,7 @@ export function jsonToSQLite(obj) {
i.name
}"\nON "${table.name}" (${i.fields
.map((f) => `"${f}"`)
.join(", ")});`
.join(", ")});`,
)
.join("\n")}`
: ""
@ -423,25 +411,18 @@ export function jsonToMariaDB(obj) {
}\` ${getTypeString(field)}${field.notNull ? " NOT NULL" : ""}${
field.increment ? " AUTO_INCREMENT" : ""
}${field.unique ? " UNIQUE" : ""}${
field.default !== ""
? ` DEFAULT ${
hasQuotes(field.type) &&
field.default.toLowerCase() !== "null"
? `"${field.default}"`
: `${field.default}`
}`
: ""
field.default !== "" ? ` DEFAULT ${parseDefault(field)}` : ""
}${
field.check === "" || !hasCheck(field.type)
? !sqlDataTypes.includes(field.type)
? ` CHECK(\n\t\tJSON_SCHEMA_VALID('${generateSchema(
obj.types.find(
(t) => t.name === field.type.toLowerCase()
)
(t) => t.name === field.type.toLowerCase(),
),
)}', \`${field.name}\`))`
: ""
: ` CHECK(${field.check})`
}`
}`,
)
.join(",\n")}${
table.fields.filter((f) => f.primary).length > 0
@ -458,10 +439,10 @@ export function jsonToMariaDB(obj) {
i.name
}\`\nON \`${table.name}\` (${i.fields
.map((f) => `\`${f}\``)
.join(", ")});`
.join(", ")});`,
)}`
: ""
}`
}`,
)
.join("\n")}\n${obj.references
.map(
@ -472,7 +453,7 @@ export function jsonToMariaDB(obj) {
obj.tables[r.startTableId].fields[r.startFieldId].name
}\`) REFERENCES \`${obj.tables[r.endTableId].name}\`(\`${
obj.tables[r.endTableId].fields[r.endFieldId].name
}\`)\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`
}\`)\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`,
)
.join("\n")}`;
}
@ -503,19 +484,12 @@ export function jsonToSQLServer(obj) {
}${field.increment ? " IDENTITY" : ""}${
field.unique ? " UNIQUE" : ""
}${
field.default !== ""
? ` DEFAULT ${
hasQuotes(field.type) &&
field.default.toLowerCase() !== "null"
? `'${field.default}'`
: `${field.default}`
}`
: ""
field.default !== "" ? ` DEFAULT ${parseDefault(field)}` : ""
}${
field.check === "" || !hasCheck(field.type)
? ""
: ` CHECK(${field.check})`
}`
}`,
)
.join(",\n")}${
table.fields.filter((f) => f.primary).length > 0
@ -532,10 +506,10 @@ export function jsonToSQLServer(obj) {
i.name
}]\nON [${table.name}] (${i.fields
.map((f) => `[${f}]`)
.join(", ")});\nGO\n`
.join(", ")});\nGO\n`,
)}`
: ""
}`
}`,
)
.join("\n")}\n${obj.references
.map(
@ -544,7 +518,7 @@ export function jsonToSQLServer(obj) {
obj.tables[r.startTableId].fields[r.startFieldId].name
}]) REFERENCES [${obj.tables[r.endTableId].name}]([${
obj.tables[r.endTableId].fields[r.endFieldId].name
}])\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};\nGO`
}])\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};\nGO`,
)
.join("\n")}`;
}

View File

@ -14,3 +14,13 @@ export function dataURItoBlob(dataUrl) {
export function arrayIsEqual(arr1, arr2) {
return JSON.stringify(arr1) === JSON.stringify(arr2);
}
export function strHasQuotes(str) {
if (str.length < 2) return false;
return (
(str[0] === str[str.length - 1] && str[0] === "'") ||
(str[0] === str[str.length - 1] && str[0] === '"') ||
(str[0] === str[str.length - 1] && str[0] === "`")
);
}