Handle index parsing
This commit is contained in:
parent
b0d262e2a0
commit
d32ee38d05
@ -1222,85 +1222,6 @@ export default function ControlPanel({
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* {
|
||||
"id": 0,
|
||||
"name": "table_4",
|
||||
"x": 50,
|
||||
"y": 83,
|
||||
"fields": [
|
||||
{
|
||||
"name": "id",
|
||||
"type": "INT",
|
||||
"default": "",
|
||||
"check": "",
|
||||
"primary": true,
|
||||
"unique": true,
|
||||
"notNull": true,
|
||||
"increment": true,
|
||||
"comment": "",
|
||||
"id": 0
|
||||
},
|
||||
{
|
||||
"name": "name",
|
||||
"type": "NUMERIC",
|
||||
"default": "",
|
||||
"check": "",
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"notNull": false,
|
||||
"increment": false,
|
||||
"comment": "",
|
||||
"id": 1,
|
||||
"size": ""
|
||||
}
|
||||
],
|
||||
"comment": "",
|
||||
"indices": [],
|
||||
"color": "#175e7a"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "table_1",
|
||||
"x": 360,
|
||||
"y": 181,
|
||||
"fields": [
|
||||
{
|
||||
"name": "id",
|
||||
"type": "INT",
|
||||
"default": "",
|
||||
"check": "",
|
||||
"primary": true,
|
||||
"unique": true,
|
||||
"notNull": true,
|
||||
"increment": true,
|
||||
"comment": "",
|
||||
"id": 0
|
||||
},
|
||||
{
|
||||
"name": "kk",
|
||||
"type": "INT",
|
||||
"default": "",
|
||||
"check": "",
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"notNull": false,
|
||||
"increment": false,
|
||||
"comment": "",
|
||||
"id": 1
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"size": "12"
|
||||
}
|
||||
],
|
||||
"comment": "",
|
||||
"indices": [],
|
||||
"color": "#175e7a"
|
||||
}
|
||||
*/
|
||||
|
||||
const parseSQLAndLoadDiagram = () => {
|
||||
const parser = new Parser();
|
||||
let ast = null;
|
||||
@ -1312,13 +1233,16 @@ export default function ControlPanel({
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
const tables = [];
|
||||
|
||||
ast.forEach(((e) => {
|
||||
console.log(JSON.stringify(e))
|
||||
if (e.type === "create" && e.keyword === "table") {
|
||||
if (e.type === "create") {
|
||||
if (e.keyword === "table") {
|
||||
const table = {};
|
||||
table.name = e.table[0].table;
|
||||
table.comment = "";
|
||||
table.color = "#175e7a";
|
||||
table.fields = [];
|
||||
table.indices = [];
|
||||
@ -1341,13 +1265,19 @@ export default function ControlPanel({
|
||||
field.default = "";
|
||||
if (d.default_val) field.default = d.default_val.value.value;
|
||||
if (d.definition["length"]) field.size = d.definition["length"];
|
||||
|
||||
field.check = "";
|
||||
if (d.check) {
|
||||
let check = "";
|
||||
if (d.check.definition[0].left.column) {
|
||||
check = d.check.definition[0].left.column + " " + d.check.definition[0].operator + " " + d.check.definition[0].right.value;
|
||||
let value = d.check.definition[0].right.value;
|
||||
if (d.check.definition[0].right.type === "double_quote_string" || d.check.definition[0].right.type === "single_quote_string")
|
||||
value = '\'' + value + '\''
|
||||
check = d.check.definition[0].left.column + " " + d.check.definition[0].operator + " " + value;
|
||||
} else {
|
||||
check = d.check.definition[0].left.value + " " + d.check.definition[0].operator + " " + d.check.definition[0].right.column;
|
||||
let value = d.check.definition[0].right.value;
|
||||
if (d.check.definition[0].left.type === "double_quote_string" || d.check.definition[0].left.type === "single_quote_string")
|
||||
value = '\'' + value + '\''
|
||||
check = value + " " + d.check.definition[0].operator + " " + d.check.definition[0].right.column;
|
||||
}
|
||||
field.check = check;
|
||||
}
|
||||
@ -1365,9 +1295,29 @@ export default function ControlPanel({
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tables.push(table);
|
||||
}
|
||||
else if (e.keyword === "index") {
|
||||
const index = {};
|
||||
index.name = e.index;
|
||||
index.unique = false;
|
||||
if (e.index_type === "unique") index.unique = true;
|
||||
index.fields = [];
|
||||
e.index_columns.forEach(f => index.fields.push(f.column));
|
||||
|
||||
let found = -1;
|
||||
tables.forEach((t, i) => {
|
||||
if (found !== -1) return;
|
||||
if (t.name === e.table.table) {
|
||||
t.indices.push(index);
|
||||
found = i;
|
||||
}
|
||||
});
|
||||
|
||||
if (found !== -1)
|
||||
tables[found].indices.forEach((i, j) => i.id = j);
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
tables.forEach((e, i) => {
|
||||
|
Loading…
Reference in New Issue
Block a user