diff --git a/src/utils/issues.js b/src/utils/issues.js index 0f4834f..b95fa74 100644 --- a/src/utils/issues.js +++ b/src/utils/issues.js @@ -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; } diff --git a/src/utils/toSQL.js b/src/utils/toSQL.js index d719f24..4720a6e 100644 --- a/src/utils/toSQL.js +++ b/src/utils/toSQL.js @@ -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}`; } @@ -83,8 +84,8 @@ export function getTypeString(field, dbms = "mysql", baseType = false) { field.type === "BINARY" ? "bit" : field.type === "VARBINARY" - ? "bit varying" - : field.type.toLowerCase(); + ? "bit varying" + : field.type.toLowerCase(); return `${type}(${field.size})`; } if (hasPrecision(field.type) && field.size !== "") { @@ -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")}`; } diff --git a/src/utils/utils.js b/src/utils/utils.js index ebebc3e..53226a6 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -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] === "`") + ); +}