auto add reference on drop

This commit is contained in:
Jan Prochazka
2020-12-30 09:02:07 +01:00
parent 1de9b9f1fb
commit b87f51c5b5
2 changed files with 53 additions and 4 deletions

View File

@@ -41,10 +41,57 @@ export default function Designer({ value, onChange, conid, database }) {
json.left = e.clientX - rect.left;
json.top = e.clientY - rect.top;
onChange((current) => ({
...current,
tables: [...(current.tables || []), json],
}));
onChange((current) => {
const foreignKeys = _.compact([
...json.foreignKeys.map((fk) => {
const tables = (current.tables || []).filter(
(tbl) => fk.refTableName == tbl.pureName && fk.refSchemaName == tbl.schemaName
);
if (tables.length == 1)
return {
...fk,
sourceId: json.designerId,
targetId: tables[0].designerId,
};
return null;
}),
..._.flatten(
(current.tables || []).map((tbl) =>
(tbl.foreignKeys || []).map((fk) => {
if (fk.refTableName == json.pureName && fk.refSchemaName == json.schemaName) {
return {
...fk,
sourceId: tbl.designerId,
targetId: json.designerId,
};
}
return null;
})
)
),
]);
return {
...current,
tables: [...(current.tables || []), json],
references:
foreignKeys.length == 1
? [
...(current.references || []),
{
designerId: uuidv1(),
sourceId: foreignKeys[0].sourceId,
targetId: foreignKeys[0].targetId,
joinType: 'INNER JOIN',
columns: foreignKeys[0].columns.map((col) => ({
source: col.columnName,
target: col.refColumnName,
})),
},
]
: current.references,
};
});
};
const changeTable = React.useCallback(
@@ -75,6 +122,7 @@ export default function Designer({ value, onChange, conid, database }) {
references: (current.references || []).filter(
(x) => x.sourceId != table.designerId && x.targetId != table.designerId
),
columns: (current.columns || []).filter((x) => x.designerId != table.designerId),
}));
},
[onChange]

View File

@@ -73,6 +73,7 @@ export class DesignerQueryDumper {
for (const column of this.designer.columns || []) {
if (!column.filter) continue;
const table = (this.designer.tables || []).find((x) => x.designerId == column.designerId);
if (!table) continue;
if (!tables.find((x) => x.designerId == table.designerId)) continue;
const condition = parseFilter(column.filter, findDesignerFilterType(column, this.designer));