Fix parsing alter statements
This commit is contained in:
parent
11e83bb12b
commit
ca8ee61ee9
@ -8,7 +8,6 @@ import {
|
|||||||
export function astToDiagram(ast) {
|
export function astToDiagram(ast) {
|
||||||
const tables = [];
|
const tables = [];
|
||||||
const relationships = [];
|
const relationships = [];
|
||||||
const inlineForeignKeys = [];
|
|
||||||
|
|
||||||
ast.forEach((e) => {
|
ast.forEach((e) => {
|
||||||
if (e.type === "create") {
|
if (e.type === "create") {
|
||||||
@ -19,6 +18,7 @@ export function astToDiagram(ast) {
|
|||||||
table.color = "#175e7a";
|
table.color = "#175e7a";
|
||||||
table.fields = [];
|
table.fields = [];
|
||||||
table.indices = [];
|
table.indices = [];
|
||||||
|
table.id = tables.length;
|
||||||
e.create_definitions.forEach((d) => {
|
e.create_definitions.forEach((d) => {
|
||||||
if (d.resource === "column") {
|
if (d.resource === "column") {
|
||||||
const field = {};
|
const field = {};
|
||||||
@ -112,14 +112,57 @@ export function astToDiagram(ast) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if (d.constraint_type === "FOREIGN KEY") {
|
} else if (d.constraint_type === "FOREIGN KEY") {
|
||||||
inlineForeignKeys.push({ ...d, startTable: e.table[0].table });
|
const relationship = {};
|
||||||
|
const startTableId = table.id;
|
||||||
|
const startTable = e.table[0].table;
|
||||||
|
const startField = d.definition[0].column;
|
||||||
|
const endTable = d.reference_definition.table[0].table;
|
||||||
|
const endField = d.reference_definition.definition[0].column;
|
||||||
|
|
||||||
|
const endTableId = tables.findIndex((t) => t.name === endTable);
|
||||||
|
if (endTableId === -1) return;
|
||||||
|
|
||||||
|
const endFieldId = tables[endTableId].fields.findIndex(
|
||||||
|
(f) => f.name === endField,
|
||||||
|
);
|
||||||
|
if (endField === -1) return;
|
||||||
|
|
||||||
|
const startFieldId = table.fields.findIndex(
|
||||||
|
(f) => f.name === startField,
|
||||||
|
);
|
||||||
|
if (startFieldId === -1) return;
|
||||||
|
|
||||||
|
relationship.name = startTable + "_" + startField + "_fk";
|
||||||
|
relationship.startTableId = startTableId;
|
||||||
|
relationship.endTableId = endTableId;
|
||||||
|
relationship.endFieldId = endFieldId;
|
||||||
|
relationship.startFieldId = startFieldId;
|
||||||
|
let updateConstraint = "No action";
|
||||||
|
let deleteConstraint = "No action";
|
||||||
|
d.reference_definition.on_action.forEach((c) => {
|
||||||
|
if (c.type === "on update") {
|
||||||
|
updateConstraint = c.value.value;
|
||||||
|
updateConstraint =
|
||||||
|
updateConstraint[0].toUpperCase() +
|
||||||
|
updateConstraint.substring(1);
|
||||||
|
} else if (c.type === "on delete") {
|
||||||
|
deleteConstraint = c.value.value;
|
||||||
|
deleteConstraint =
|
||||||
|
deleteConstraint[0].toUpperCase() +
|
||||||
|
deleteConstraint.substring(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
relationship.updateConstraint = updateConstraint;
|
||||||
|
relationship.deleteConstraint = deleteConstraint;
|
||||||
|
relationship.cardinality = Cardinality.ONE_TO_ONE;
|
||||||
|
relationships.push(relationship);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
table.fields.forEach((f, j) => {
|
table.fields.forEach((f, j) => {
|
||||||
f.id = j;
|
f.id = j;
|
||||||
});
|
});
|
||||||
table.id = tables.length;
|
|
||||||
tables.push(table);
|
tables.push(table);
|
||||||
} else if (e.keyword === "index") {
|
} else if (e.keyword === "index") {
|
||||||
const index = {};
|
const index = {};
|
||||||
@ -141,21 +184,22 @@ export function astToDiagram(ast) {
|
|||||||
if (found !== -1) tables[found].indices.forEach((i, j) => (i.id = j));
|
if (found !== -1) tables[found].indices.forEach((i, j) => (i.id = j));
|
||||||
}
|
}
|
||||||
} else if (e.type === "alter") {
|
} else if (e.type === "alter") {
|
||||||
|
e.expr.forEach((expr) => {
|
||||||
if (
|
if (
|
||||||
e.expr[0].action === "add" &&
|
expr.action === "add" &&
|
||||||
e.expr[0].create_definitions.constraint_type === "FOREIGN KEY"
|
expr.create_definitions.constraint_type === "FOREIGN KEY"
|
||||||
) {
|
) {
|
||||||
|
console.log(e);
|
||||||
const relationship = {};
|
const relationship = {};
|
||||||
const startTable = e.table[0].table;
|
const startTable = e.table[0].table;
|
||||||
const startField = e.expr[0].create_definitions.definition[0].column;
|
const startField = expr.create_definitions.definition[0].column;
|
||||||
const endTable =
|
const endTable =
|
||||||
e.expr[0].create_definitions.reference_definition.table[0].table;
|
expr.create_definitions.reference_definition.table[0].table;
|
||||||
const endField =
|
const endField =
|
||||||
e.expr[0].create_definitions.reference_definition.definition[0]
|
expr.create_definitions.reference_definition.definition[0].column;
|
||||||
.column;
|
|
||||||
let updateConstraint = "No action";
|
let updateConstraint = "No action";
|
||||||
let deleteConstraint = "No action";
|
let deleteConstraint = "No action";
|
||||||
e.expr[0].create_definitions.reference_definition.on_action.forEach(
|
expr.create_definitions.reference_definition.on_action.forEach(
|
||||||
(c) => {
|
(c) => {
|
||||||
if (c.type === "on update") {
|
if (c.type === "on update") {
|
||||||
updateConstraint = c.value.value;
|
updateConstraint = c.value.value;
|
||||||
@ -171,36 +215,21 @@ export function astToDiagram(ast) {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let startTableId = -1;
|
const startTableId = tables.findIndex((t) => t.name === startTable);
|
||||||
let startFieldId = -1;
|
if (startTable === -1) return;
|
||||||
let endTableId = -1;
|
|
||||||
let endFieldId = -1;
|
|
||||||
|
|
||||||
tables.forEach((t) => {
|
const endTableId = tables.findIndex((t) => t.name === endTable);
|
||||||
if (t.name === startTable) {
|
if (endTableId === -1) return;
|
||||||
startTableId = t.id;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.name === endTable) {
|
const endFieldId = tables[endTableId].fields.findIndex(
|
||||||
endTableId = t.id;
|
(f) => f.name === endField,
|
||||||
}
|
);
|
||||||
});
|
if (endField === -1) return;
|
||||||
|
|
||||||
if (startTableId === -1 || endTableId === -1) return;
|
const startFieldId = tables[startTableId].fields.findIndex(
|
||||||
|
(f) => f.name === startField,
|
||||||
tables[startTableId].fields.forEach((f) => {
|
);
|
||||||
if (f.name === startField) {
|
if (startFieldId === -1) return;
|
||||||
startFieldId = f.id;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f.name === endField) {
|
|
||||||
endFieldId = f.id;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (startFieldId === -1 || endFieldId === -1) return;
|
|
||||||
|
|
||||||
relationship.name = startTable + "_" + startField + "_fk";
|
relationship.name = startTable + "_" + startField + "_fk";
|
||||||
relationship.startTableId = startTableId;
|
relationship.startTableId = startTableId;
|
||||||
@ -211,74 +240,14 @@ export function astToDiagram(ast) {
|
|||||||
relationship.deleteConstraint = deleteConstraint;
|
relationship.deleteConstraint = deleteConstraint;
|
||||||
relationship.cardinality = Cardinality.ONE_TO_ONE;
|
relationship.cardinality = Cardinality.ONE_TO_ONE;
|
||||||
relationships.push(relationship);
|
relationships.push(relationship);
|
||||||
|
console.log(relationship);
|
||||||
|
|
||||||
relationships.forEach((r, i) => (r.id = i));
|
relationships.forEach((r, i) => (r.id = i));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
inlineForeignKeys.forEach((fk) => {
|
|
||||||
const relationship = {};
|
|
||||||
const startTable = fk.startTable;
|
|
||||||
const startField = fk.definition[0].column;
|
|
||||||
const endTable = fk.reference_definition.table[0].table;
|
|
||||||
const endField = fk.reference_definition.definition[0].column;
|
|
||||||
let updateConstraint = "No action";
|
|
||||||
let deleteConstraint = "No action";
|
|
||||||
fk.reference_definition.on_action.forEach((c) => {
|
|
||||||
if (c.type === "on update") {
|
|
||||||
updateConstraint = c.value.value;
|
|
||||||
updateConstraint =
|
|
||||||
updateConstraint[0].toUpperCase() + updateConstraint.substring(1);
|
|
||||||
} else if (c.type === "on delete") {
|
|
||||||
deleteConstraint = c.value.value;
|
|
||||||
deleteConstraint =
|
|
||||||
deleteConstraint[0].toUpperCase() + deleteConstraint.substring(1);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let startTableId = -1;
|
|
||||||
let startFieldId = -1;
|
|
||||||
let endTableId = -1;
|
|
||||||
let endFieldId = -1;
|
|
||||||
|
|
||||||
tables.forEach((t) => {
|
|
||||||
if (t.name === startTable) {
|
|
||||||
startTableId = t.id;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.name === endTable) {
|
|
||||||
endTableId = t.id;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (startTableId === -1 || endTableId === -1) return;
|
|
||||||
|
|
||||||
tables[startTableId].fields.forEach((f) => {
|
|
||||||
if (f.name === startField) {
|
|
||||||
startFieldId = f.id;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f.name === endField) {
|
|
||||||
endFieldId = f.id;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (startFieldId === -1 || endFieldId === -1) return;
|
|
||||||
|
|
||||||
relationship.name = startTable + "_" + startField + "_fk";
|
|
||||||
relationship.startTableId = startTableId;
|
|
||||||
relationship.startFieldId = startFieldId;
|
|
||||||
relationship.endTableId = endTableId;
|
|
||||||
relationship.endFieldId = endFieldId;
|
|
||||||
relationship.updateConstraint = updateConstraint;
|
|
||||||
relationship.deleteConstraint = deleteConstraint;
|
|
||||||
relationship.cardinality = Cardinality.ONE_TO_ONE;
|
|
||||||
relationships.push(relationship);
|
|
||||||
});
|
|
||||||
|
|
||||||
relationships.forEach((r, i) => (r.id = i));
|
relationships.forEach((r, i) => (r.id = i));
|
||||||
|
|
||||||
let maxHeight = -1;
|
let maxHeight = -1;
|
||||||
|
Loading…
Reference in New Issue
Block a user