mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-09-19 22:14:50 +00:00
parse tables
This commit is contained in:
@@ -35,6 +35,7 @@ import {
|
||||
exitFullscreen,
|
||||
ddbDiagramIsValid,
|
||||
dataURItoBlob,
|
||||
jsonToSQL,
|
||||
} from "../utils";
|
||||
import {
|
||||
AreaContext,
|
||||
@@ -53,6 +54,7 @@ import { useHotkeys } from "react-hotkeys-hook";
|
||||
import { Validator } from "jsonschema";
|
||||
import { areaSchema, noteSchema, tableSchema } from "../schemas";
|
||||
import { Editor } from "@monaco-editor/react";
|
||||
// import { Parser } from "node-sql-parser";
|
||||
|
||||
export default function ControlPanel(props) {
|
||||
const MODAL = {
|
||||
@@ -69,7 +71,7 @@ export default function ControlPanel(props) {
|
||||
};
|
||||
const [visible, setVisible] = useState(MODAL.NONE);
|
||||
const [exportData, setExportData] = useState({
|
||||
data: "",
|
||||
data: null,
|
||||
filename: `diagram_${new Date().toISOString()}`,
|
||||
extension: "",
|
||||
});
|
||||
@@ -726,7 +728,29 @@ export default function ControlPanel(props) {
|
||||
},
|
||||
"Export source": {
|
||||
children: [
|
||||
{ MySQL: () => {} },
|
||||
{
|
||||
MySQL: () => {
|
||||
setVisible(MODAL.CODE);
|
||||
const src = jsonToSQL({
|
||||
tables: tables,
|
||||
references: relationships,
|
||||
});
|
||||
// try{
|
||||
// const parser = new Parser();
|
||||
// const ast = parser.astify(src);
|
||||
// console.log(ast);
|
||||
// const sql = parser.sqlify(ast);
|
||||
// console.log(sql);
|
||||
// } catch(e){
|
||||
// console.log(e)
|
||||
// }
|
||||
setExportData((prev) => ({
|
||||
...prev,
|
||||
data: src,
|
||||
extension: "sql",
|
||||
}));
|
||||
},
|
||||
},
|
||||
{ PostgreSQL: () => {} },
|
||||
{ DBML: () => {} },
|
||||
],
|
||||
@@ -863,7 +887,10 @@ export default function ControlPanel(props) {
|
||||
"Tweet us": {
|
||||
function: () => {},
|
||||
},
|
||||
"Found a bug": {
|
||||
"Report a bug": {
|
||||
function: () => {},
|
||||
},
|
||||
"Suggest a feature": {
|
||||
function: () => {},
|
||||
},
|
||||
},
|
||||
@@ -1193,7 +1220,7 @@ export default function ControlPanel(props) {
|
||||
<Editor
|
||||
height="360px"
|
||||
value={exportData.data}
|
||||
language="json"
|
||||
language={exportData.extension}
|
||||
options={{ readOnly: true }}
|
||||
/>
|
||||
)}
|
||||
|
@@ -80,7 +80,7 @@ export default function Editor(props) {
|
||||
fields: [
|
||||
{
|
||||
name: "id",
|
||||
type: "UUID",
|
||||
type: "INT",
|
||||
default: "",
|
||||
check: "",
|
||||
primary: true,
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import { Validator } from "jsonschema";
|
||||
import { ddbSchema, jsonSchema } from "../schemas";
|
||||
|
||||
const enterFullscreen = () => {
|
||||
function enterFullscreen() {
|
||||
const element = document.documentElement;
|
||||
if (element.requestFullscreen) {
|
||||
element.requestFullscreen();
|
||||
@@ -12,9 +12,9 @@ const enterFullscreen = () => {
|
||||
} else if (element.msRequestFullscreen) {
|
||||
element.msRequestFullscreen();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const exitFullscreen = () => {
|
||||
function exitFullscreen() {
|
||||
if (document.exitFullscreen) {
|
||||
document.exitFullscreen();
|
||||
} else if (document.mozCancelFullScreen) {
|
||||
@@ -24,15 +24,15 @@ const exitFullscreen = () => {
|
||||
} else if (document.msExitFullscreen) {
|
||||
document.msExitFullscreen();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const jsonDiagramIsValid = (obj) => {
|
||||
function jsonDiagramIsValid(obj) {
|
||||
return new Validator().validate(obj, jsonSchema).valid;
|
||||
};
|
||||
}
|
||||
|
||||
const ddbDiagramIsValid = (obj) => {
|
||||
function ddbDiagramIsValid(obj) {
|
||||
return new Validator().validate(obj, ddbSchema).valid;
|
||||
};
|
||||
}
|
||||
|
||||
function dataURItoBlob(dataUrl) {
|
||||
const byteString = atob(dataUrl.split(",")[1]);
|
||||
@@ -47,10 +47,33 @@ function dataURItoBlob(dataUrl) {
|
||||
return new Blob([intArray], { type: mimeString });
|
||||
}
|
||||
|
||||
function jsonToSQL(obj) {
|
||||
return obj.tables
|
||||
.map(
|
||||
(table) =>
|
||||
`${
|
||||
table.comment === "" ? "" : `/* ${table.comment} */\n`
|
||||
}CREATE TABLE \`${table.name}\` (\n${table.fields
|
||||
.map(
|
||||
(field) =>
|
||||
`${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t\`${
|
||||
field.name
|
||||
}\` ${field.type} ${field.notNull ? "NOT NULL" : ""} ${
|
||||
field.increment ? "AUTO_INCREMENT" : ""
|
||||
} ${field.unique ? "UNIQUE" : ""},`
|
||||
)
|
||||
.join("\n")}\n\tPRIMARY KEY(${table.fields.map((f) =>
|
||||
f.primary ? `${f.name}` : ""
|
||||
)})\n);`
|
||||
)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export {
|
||||
enterFullscreen,
|
||||
exitFullscreen,
|
||||
jsonDiagramIsValid,
|
||||
ddbDiagramIsValid,
|
||||
dataURItoBlob,
|
||||
jsonToSQL,
|
||||
};
|
||||
|
Reference in New Issue
Block a user