Add MariaDB
This commit is contained in:
parent
f9dd80f39c
commit
7e1c8fc1ca
@ -45,7 +45,8 @@ import {
|
|||||||
dataURItoBlob,
|
dataURItoBlob,
|
||||||
jsonToMySQL,
|
jsonToMySQL,
|
||||||
jsonToPostgreSQL,
|
jsonToPostgreSQL,
|
||||||
jsonToSQLite
|
jsonToSQLite,
|
||||||
|
jsonToMariaDB
|
||||||
} from "../utils";
|
} from "../utils";
|
||||||
import {
|
import {
|
||||||
AreaContext,
|
AreaContext,
|
||||||
@ -994,6 +995,21 @@ export default function ControlPanel({
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
MariaDB: () => {
|
||||||
|
setVisible(MODAL.CODE);
|
||||||
|
const src = jsonToMariaDB({
|
||||||
|
tables: tables,
|
||||||
|
references: relationships,
|
||||||
|
types: types,
|
||||||
|
});
|
||||||
|
setExportData((prev) => ({
|
||||||
|
...prev,
|
||||||
|
data: src,
|
||||||
|
extension: "sql",
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
},
|
||||||
{ DBML: () => { } },
|
{ DBML: () => { } },
|
||||||
],
|
],
|
||||||
function: () => { },
|
function: () => { },
|
||||||
@ -1069,9 +1085,9 @@ export default function ControlPanel({
|
|||||||
},
|
},
|
||||||
"Presentation mode": {
|
"Presentation mode": {
|
||||||
function: () => {
|
function: () => {
|
||||||
setLayout(prev => ({
|
setLayout(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
header: false,
|
header: false,
|
||||||
sidebar: false,
|
sidebar: false,
|
||||||
toolbar: false,
|
toolbar: false,
|
||||||
}));
|
}));
|
||||||
|
@ -49,14 +49,10 @@ function dataURItoBlob(dataUrl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getJsonType(f) {
|
function getJsonType(f) {
|
||||||
if (!sqlDataTypes.includes(f.type)) {
|
|
||||||
return '{ "type" : "object", additionalProperties : true }';
|
|
||||||
}
|
|
||||||
switch (f.type) {
|
switch (f.type) {
|
||||||
case "INT":
|
case "INT":
|
||||||
case "SMALLINT":
|
case "SMALLINT":
|
||||||
case "BIGINT":
|
case "BIGINT":
|
||||||
return '{ "type" : "integer" }';
|
|
||||||
case "DECIMAL":
|
case "DECIMAL":
|
||||||
case "NUMERIC":
|
case "NUMERIC":
|
||||||
case "REAL":
|
case "REAL":
|
||||||
@ -80,7 +76,7 @@ function getJsonType(f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function generateSchema(type) {
|
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
|
return `{\n\t\t\t"type": "object",\n\t\t\t"properties": {\n\t\t\t\t${type.fields
|
||||||
.map((f) => `"${f.name}" : ${getJsonType(f)}`)
|
.map((f) => `"${f.name}" : ${getJsonType(f)}`)
|
||||||
.join(
|
.join(
|
||||||
",\n\t\t\t\t"
|
",\n\t\t\t\t"
|
||||||
@ -378,6 +374,62 @@ function jsonToSQLite(obj) {
|
|||||||
).join("\n");
|
).join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function jsonToMariaDB(obj) {
|
||||||
|
return `${obj.tables
|
||||||
|
.map(
|
||||||
|
(table) =>
|
||||||
|
`${table.comment === "" ? "" : `/* ${table.comment} */\n`
|
||||||
|
}CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields
|
||||||
|
.map(
|
||||||
|
(field) =>
|
||||||
|
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${field.name
|
||||||
|
}\` ${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.check === "" || !hasCheck(field.type)
|
||||||
|
? !sqlDataTypes.includes(field.type)
|
||||||
|
? ` CHECK(\n\t\tJSON_SCHEMA_VALID('${generateSchema(
|
||||||
|
obj.types.find(
|
||||||
|
(t) => t.name === field.type.toLowerCase()
|
||||||
|
)
|
||||||
|
)}', \`${field.name}\`))`
|
||||||
|
: ""
|
||||||
|
: ` CHECK(${field.check})`
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
.join(",\n")}${table.fields.filter((f) => f.primary).length > 0
|
||||||
|
? `,\n\tPRIMARY KEY(${table.fields
|
||||||
|
.filter((f) => f.primary)
|
||||||
|
.map((f) => `\`${f.name}\``)
|
||||||
|
.join(", ")})`
|
||||||
|
: ""
|
||||||
|
}\n);${table.indices.length > 0
|
||||||
|
? `\n${table.indices.map(
|
||||||
|
(i) =>
|
||||||
|
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX \`${i.name
|
||||||
|
}\`\nON \`${table.name}\` (${i.fields
|
||||||
|
.map((f) => `\`${f}\``)
|
||||||
|
.join(", ")});`
|
||||||
|
)}`
|
||||||
|
: ""
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
.join("\n")}\n${obj.references
|
||||||
|
.map(
|
||||||
|
(r) =>
|
||||||
|
`ALTER TABLE \`${obj.tables[r.startTableId].name
|
||||||
|
}\`\nADD FOREIGN KEY(\`${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()};`
|
||||||
|
)
|
||||||
|
.join("\n")}`;
|
||||||
|
}
|
||||||
|
|
||||||
function arrayIsEqual(arr1, arr2) {
|
function arrayIsEqual(arr1, arr2) {
|
||||||
return JSON.stringify(arr1) === JSON.stringify(arr2);
|
return JSON.stringify(arr1) === JSON.stringify(arr2);
|
||||||
}
|
}
|
||||||
@ -758,5 +810,6 @@ export {
|
|||||||
validateDateStr,
|
validateDateStr,
|
||||||
hasCheck,
|
hasCheck,
|
||||||
calcPath,
|
calcPath,
|
||||||
jsonToSQLite
|
jsonToSQLite,
|
||||||
|
jsonToMariaDB,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user