@surfaces-ui/components 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +48 -2
- package/dist/appComponents.d.ts +86 -0
- package/dist/appComponents.js +555 -0
- package/dist/appComponents.js.map +1 -0
- package/dist/chart/BulletGraph.d.ts +44 -0
- package/dist/chart/BulletGraph.js +40 -0
- package/dist/chart/BulletGraph.js.map +1 -0
- package/dist/chart/Chart.d.ts +177 -0
- package/dist/chart/Chart.js +279 -0
- package/dist/chart/Chart.js.map +1 -0
- package/dist/chart/Sparkline.d.ts +35 -0
- package/dist/chart/Sparkline.js +37 -0
- package/dist/chart/Sparkline.js.map +1 -0
- package/dist/chart/advice.d.ts +28 -0
- package/dist/chart/advice.js +39 -0
- package/dist/chart/advice.js.map +1 -0
- package/dist/chart/option.d.ts +90 -0
- package/dist/chart/option.js +252 -0
- package/dist/chart/option.js.map +1 -0
- package/dist/controls/Combobox.d.ts +39 -0
- package/dist/controls/Combobox.js +75 -0
- package/dist/controls/Combobox.js.map +1 -0
- package/dist/controls/ConfirmDialog.d.ts +35 -0
- package/dist/controls/ConfirmDialog.js +84 -0
- package/dist/controls/ConfirmDialog.js.map +1 -0
- package/dist/controls/ItemControls.d.ts +115 -0
- package/dist/controls/ItemControls.js +109 -0
- package/dist/controls/ItemControls.js.map +1 -0
- package/dist/controls/Popover.d.ts +31 -0
- package/dist/controls/Popover.js +114 -0
- package/dist/controls/Popover.js.map +1 -0
- package/dist/controls/ResizeHandle.d.ts +48 -0
- package/dist/controls/ResizeHandle.js +91 -0
- package/dist/controls/ResizeHandle.js.map +1 -0
- package/dist/controls/SegmentedControl.d.ts +27 -0
- package/dist/controls/SegmentedControl.js +17 -0
- package/dist/controls/SegmentedControl.js.map +1 -0
- package/dist/controls/StatusBadge.d.ts +17 -0
- package/dist/controls/StatusBadge.js +10 -0
- package/dist/controls/StatusBadge.js.map +1 -0
- package/dist/controls/context.d.ts +17 -0
- package/dist/controls/context.js +2 -0
- package/dist/controls/context.js.map +1 -0
- package/dist/controls/placement.d.ts +43 -0
- package/dist/controls/placement.js +19 -0
- package/dist/controls/placement.js.map +1 -0
- package/dist/controls/resize.d.ts +23 -0
- package/dist/controls/resize.js +49 -0
- package/dist/controls/resize.js.map +1 -0
- package/dist/data/DataSourceProvider.d.ts +11 -0
- package/dist/data/DataSourceProvider.js +21 -0
- package/dist/data/DataSourceProvider.js.map +1 -0
- package/dist/data/export.d.ts +33 -0
- package/dist/data/export.js +77 -0
- package/dist/data/export.js.map +1 -0
- package/dist/data/format.d.ts +44 -0
- package/dist/data/format.js +127 -0
- package/dist/data/format.js.map +1 -0
- package/dist/data/messages.d.ts +13 -0
- package/dist/data/messages.js +2 -0
- package/dist/data/messages.js.map +1 -0
- package/dist/data/useRowWindow.d.ts +70 -0
- package/dist/data/useRowWindow.js +175 -0
- package/dist/data/useRowWindow.js.map +1 -0
- package/dist/discovery/AskData.d.ts +22 -0
- package/dist/discovery/AskData.js +48 -0
- package/dist/discovery/AskData.js.map +1 -0
- package/dist/discovery/ContextBuilder.d.ts +49 -0
- package/dist/discovery/ContextBuilder.js +167 -0
- package/dist/discovery/ContextBuilder.js.map +1 -0
- package/dist/discovery/DataSearch.d.ts +27 -0
- package/dist/discovery/DataSearch.js +73 -0
- package/dist/discovery/DataSearch.js.map +1 -0
- package/dist/discovery/ObjectDetail.d.ts +38 -0
- package/dist/discovery/ObjectDetail.js +129 -0
- package/dist/discovery/ObjectDetail.js.map +1 -0
- package/dist/discovery/RelationshipGraph.d.ts +19 -0
- package/dist/discovery/RelationshipGraph.js +64 -0
- package/dist/discovery/RelationshipGraph.js.map +1 -0
- package/dist/discovery/shared.d.ts +38 -0
- package/dist/discovery/shared.js +64 -0
- package/dist/discovery/shared.js.map +1 -0
- package/dist/editor/SqlEditor.d.ts +41 -0
- package/dist/editor/SqlEditor.js +181 -0
- package/dist/editor/SqlEditor.js.map +1 -0
- package/dist/explore/Explorer.d.ts +110 -0
- package/dist/explore/Explorer.js +471 -0
- package/dist/explore/Explorer.js.map +1 -0
- package/dist/explore/ModelLineage.d.ts +22 -0
- package/dist/explore/ModelLineage.js +70 -0
- package/dist/explore/ModelLineage.js.map +1 -0
- package/dist/explore/ReportBuilder.d.ts +39 -0
- package/dist/explore/ReportBuilder.js +98 -0
- package/dist/explore/ReportBuilder.js.map +1 -0
- package/dist/explore/SemanticModelEditor.d.ts +60 -0
- package/dist/explore/SemanticModelEditor.js +220 -0
- package/dist/explore/SemanticModelEditor.js.map +1 -0
- package/dist/explore/SemanticRows.d.ts +42 -0
- package/dist/explore/SemanticRows.js +117 -0
- package/dist/explore/SemanticRows.js.map +1 -0
- package/dist/explore/SemanticsProvider.d.ts +31 -0
- package/dist/explore/SemanticsProvider.js +72 -0
- package/dist/explore/SemanticsProvider.js.map +1 -0
- package/dist/explore/model.d.ts +151 -0
- package/dist/explore/model.js +344 -0
- package/dist/explore/model.js.map +1 -0
- package/dist/explore/report.d.ts +60 -0
- package/dist/explore/report.js +92 -0
- package/dist/explore/report.js.map +1 -0
- package/dist/explore/semantic-proposal.d.ts +44 -0
- package/dist/explore/semantic-proposal.js +67 -0
- package/dist/explore/semantic-proposal.js.map +1 -0
- package/dist/explore/semantic.d.ts +44 -0
- package/dist/explore/semantic.js +154 -0
- package/dist/explore/semantic.js.map +1 -0
- package/dist/files/FileBrowser.d.ts +42 -0
- package/dist/files/FileBrowser.js +314 -0
- package/dist/files/FileBrowser.js.map +1 -0
- package/dist/files/files.d.ts +65 -0
- package/dist/files/files.js +165 -0
- package/dist/files/files.js.map +1 -0
- package/dist/filter/FilterBar.d.ts +49 -0
- package/dist/filter/FilterBar.js +153 -0
- package/dist/filter/FilterBar.js.map +1 -0
- package/dist/filter/QueryFilters.d.ts +25 -0
- package/dist/filter/QueryFilters.js +58 -0
- package/dist/filter/QueryFilters.js.map +1 -0
- package/dist/form/Form.d.ts +76 -0
- package/dist/form/Form.js +131 -0
- package/dist/form/Form.js.map +1 -0
- package/dist/graph/NodeGraph.d.ts +30 -0
- package/dist/graph/NodeGraph.js +86 -0
- package/dist/graph/NodeGraph.js.map +1 -0
- package/dist/graph/model.d.ts +68 -0
- package/dist/graph/model.js +122 -0
- package/dist/graph/model.js.map +1 -0
- package/dist/grid/DataGrid.d.ts +98 -0
- package/dist/grid/DataGrid.js +399 -0
- package/dist/grid/DataGrid.js.map +1 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +86 -0
- package/dist/index.js.map +1 -0
- package/dist/locale/SurfacesText.d.ts +17 -0
- package/dist/locale/SurfacesText.js +17 -0
- package/dist/locale/SurfacesText.js.map +1 -0
- package/dist/locale/locale.d.ts +305 -0
- package/dist/locale/locale.js +359 -0
- package/dist/locale/locale.js.map +1 -0
- package/dist/menu/ContextMenu.d.ts +31 -0
- package/dist/menu/ContextMenu.js +95 -0
- package/dist/menu/ContextMenu.js.map +1 -0
- package/dist/metric/Metric.d.ts +33 -0
- package/dist/metric/Metric.js +63 -0
- package/dist/metric/Metric.js.map +1 -0
- package/dist/metric/useMeasure.d.ts +23 -0
- package/dist/metric/useMeasure.js +43 -0
- package/dist/metric/useMeasure.js.map +1 -0
- package/dist/model/ModelDiagram.d.ts +142 -0
- package/dist/model/ModelDiagram.js +699 -0
- package/dist/model/ModelDiagram.js.map +1 -0
- package/dist/panzoom/usePanZoom.d.ts +117 -0
- package/dist/panzoom/usePanZoom.js +313 -0
- package/dist/panzoom/usePanZoom.js.map +1 -0
- package/dist/pipeline/DatasetReview.d.ts +48 -0
- package/dist/pipeline/DatasetReview.js +157 -0
- package/dist/pipeline/DatasetReview.js.map +1 -0
- package/dist/pipeline/IngestWizard.d.ts +51 -0
- package/dist/pipeline/IngestWizard.js +79 -0
- package/dist/pipeline/IngestWizard.js.map +1 -0
- package/dist/pipeline/Pipeline.d.ts +171 -0
- package/dist/pipeline/Pipeline.js +203 -0
- package/dist/pipeline/Pipeline.js.map +1 -0
- package/dist/pipeline/PipelineView.d.ts +63 -0
- package/dist/pipeline/PipelineView.js +239 -0
- package/dist/pipeline/PipelineView.js.map +1 -0
- package/dist/pivot/PivotTable.d.ts +94 -0
- package/dist/pivot/PivotTable.js +171 -0
- package/dist/pivot/PivotTable.js.map +1 -0
- package/dist/plan/QueryPlan.d.ts +29 -0
- package/dist/plan/QueryPlan.js +79 -0
- package/dist/plan/QueryPlan.js.map +1 -0
- package/dist/result/QueryResult.d.ts +51 -0
- package/dist/result/QueryResult.js +255 -0
- package/dist/result/QueryResult.js.map +1 -0
- package/dist/review/ActionReceipt.d.ts +52 -0
- package/dist/review/ActionReceipt.js +32 -0
- package/dist/review/ActionReceipt.js.map +1 -0
- package/dist/review/PiiReview.d.ts +37 -0
- package/dist/review/PiiReview.js +29 -0
- package/dist/review/PiiReview.js.map +1 -0
- package/dist/review/PlanReview.d.ts +42 -0
- package/dist/review/PlanReview.js +37 -0
- package/dist/review/PlanReview.js.map +1 -0
- package/dist/review/StepFlow.d.ts +25 -0
- package/dist/review/StepFlow.js +21 -0
- package/dist/review/StepFlow.js.map +1 -0
- package/dist/review/pii.d.ts +21 -0
- package/dist/review/pii.js +67 -0
- package/dist/review/pii.js.map +1 -0
- package/dist/rows/RowList.d.ts +41 -0
- package/dist/rows/RowList.js +71 -0
- package/dist/rows/RowList.js.map +1 -0
- package/dist/schema/ObjectBrowser.d.ts +78 -0
- package/dist/schema/ObjectBrowser.js +467 -0
- package/dist/schema/ObjectBrowser.js.map +1 -0
- package/dist/schema/ObjectInfo.d.ts +25 -0
- package/dist/schema/ObjectInfo.js +69 -0
- package/dist/schema/ObjectInfo.js.map +1 -0
- package/dist/schema/SchemaExplorer.d.ts +32 -0
- package/dist/schema/SchemaExplorer.js +175 -0
- package/dist/schema/SchemaExplorer.js.map +1 -0
- package/dist/schema/refs.d.ts +32 -0
- package/dist/schema/refs.js +65 -0
- package/dist/schema/refs.js.map +1 -0
- package/dist/site/Site.d.ts +191 -0
- package/dist/site/Site.js +138 -0
- package/dist/site/Site.js.map +1 -0
- package/dist/source/ConnectionBuilder.d.ts +54 -0
- package/dist/source/ConnectionBuilder.js +115 -0
- package/dist/source/ConnectionBuilder.js.map +1 -0
- package/dist/source/SourceList.d.ts +17 -0
- package/dist/source/SourceList.js +71 -0
- package/dist/source/SourceList.js.map +1 -0
- package/dist/styles.css +9882 -0
- package/dist/styles.layer.css +9886 -0
- package/dist/table/DataTable.d.ts +72 -0
- package/dist/table/DataTable.js +68 -0
- package/dist/table/DataTable.js.map +1 -0
- package/dist/table/Pagination.d.ts +20 -0
- package/dist/table/Pagination.js +16 -0
- package/dist/table/Pagination.js.map +1 -0
- package/dist/table/SelectionBar.d.ts +43 -0
- package/dist/table/SelectionBar.js +18 -0
- package/dist/table/SelectionBar.js.map +1 -0
- package/dist/text/Markdown.d.ts +71 -0
- package/dist/text/Markdown.js +187 -0
- package/dist/text/Markdown.js.map +1 -0
- package/dist/text/highlight.d.ts +23 -0
- package/dist/text/highlight.js +196 -0
- package/dist/text/highlight.js.map +1 -0
- package/dist/theme/ThemePicker.d.ts +16 -0
- package/dist/theme/ThemePicker.js +49 -0
- package/dist/theme/ThemePicker.js.map +1 -0
- package/dist/theme/ThemeProvider.d.ts +43 -0
- package/dist/theme/ThemeProvider.js +107 -0
- package/dist/theme/ThemeProvider.js.map +1 -0
- package/dist/workspace/NavBar.d.ts +23 -0
- package/dist/workspace/NavBar.js +171 -0
- package/dist/workspace/NavBar.js.map +1 -0
- package/dist/workspace/SideNav.d.ts +61 -0
- package/dist/workspace/SideNav.js +149 -0
- package/dist/workspace/SideNav.js.map +1 -0
- package/dist/workspace/Workspace.d.ts +50 -0
- package/dist/workspace/Workspace.js +243 -0
- package/dist/workspace/Workspace.js.map +1 -0
- package/dist/workspace/icons.d.ts +34 -0
- package/dist/workspace/icons.js +37 -0
- package/dist/workspace/icons.js.map +1 -0
- package/dist/workspace/layout.d.ts +74 -0
- package/dist/workspace/layout.js +276 -0
- package/dist/workspace/layout.js.map +1 -0
- package/package.json +85 -3
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { mostRowsPerKey, typeFamily } from "@surfaces-ui/data";
|
|
3
|
+
import { ObjectKind, RelationKind } from "@surfaces-ui/proto";
|
|
4
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { useDataSource } from "../data/DataSourceProvider.js";
|
|
6
|
+
import { messageOf } from "../data/useRowWindow.js";
|
|
7
|
+
import { CertificationBadge, useOptionalDiscovery } from "../discovery/shared.js";
|
|
8
|
+
import { PanZoomControls, usePanZoom } from "../panzoom/usePanZoom.js";
|
|
9
|
+
import { SCHEMA_REF_MIME } from "../schema/SchemaExplorer.js";
|
|
10
|
+
import { ControlIcon } from "../controls/ItemControls.js";
|
|
11
|
+
export const JOIN_KINDS = ["inner", "left", "right", "full"];
|
|
12
|
+
const CARD_WIDTH = 240;
|
|
13
|
+
const HEAD_HEIGHT = 34;
|
|
14
|
+
const ROW_HEIGHT = 24;
|
|
15
|
+
const MAX_ROWS = 10;
|
|
16
|
+
const GAP = 60;
|
|
17
|
+
const lower = (name) => name.toLowerCase();
|
|
18
|
+
const same = (a, b) => lower(a) === lower(b);
|
|
19
|
+
const quote = (name) => (/^[A-Za-z_]\w*$/.test(name) ? name : `"${name.replace(/"/g, '""')}"`);
|
|
20
|
+
/** A table's name in SQL. One written with its database or schema (`sales.orders`) is already SQL and is left as it is, not quoted into one name. */
|
|
21
|
+
const quoteTable = (name) => (/^[A-Za-z_]\w*(\.[A-Za-z_]\w*){1,2}$/.test(name) ? name : quote(name));
|
|
22
|
+
/** A join's identity, whichever way round it was drawn. */
|
|
23
|
+
export function joinId(join) {
|
|
24
|
+
const forward = lower(join.left) <= lower(join.right);
|
|
25
|
+
const [a, b] = forward ? [join.left, join.right] : [join.right, join.left];
|
|
26
|
+
const keys = join.keys.map((k) => (forward ? `${k.leftColumn}=${k.rightColumn}` : `${k.rightColumn}=${k.leftColumn}`)).sort();
|
|
27
|
+
return lower(`${a}|${b}|${keys.join("&")}`);
|
|
28
|
+
}
|
|
29
|
+
/** The text of a join condition, e.g. `orders.customer_id = customers.customer_id`. */
|
|
30
|
+
export function joinText(join) {
|
|
31
|
+
return join.keys.map((k) => `${quoteTable(join.left)}.${quote(k.leftColumn)} = ${quoteTable(join.right)}.${quote(k.rightColumn)}`).join(" AND ");
|
|
32
|
+
}
|
|
33
|
+
const KEYWORD = { inner: "JOIN", left: "LEFT OUTER JOIN", right: "RIGHT OUTER JOIN", full: "FULL OUTER JOIN" };
|
|
34
|
+
const FLIPPED = { inner: "inner", left: "right", right: "left", full: "full" };
|
|
35
|
+
/** Which rows a join of this kind keeps, in words, named from the join's left and right tables. */
|
|
36
|
+
export function kindText(join, kind) {
|
|
37
|
+
return kind === "inner" ? "Only rows that match" : kind === "left" ? `Every ${join.left} row` : kind === "right" ? `Every ${join.right} row` : "Every row of both";
|
|
38
|
+
}
|
|
39
|
+
/** The same join drawn the other way round: tables, columns and the side it keeps all swapped, so it keeps the same rows. */
|
|
40
|
+
export function flipJoin(join) {
|
|
41
|
+
return {
|
|
42
|
+
...join,
|
|
43
|
+
left: join.right,
|
|
44
|
+
right: join.left,
|
|
45
|
+
keys: join.keys.map((k) => ({ leftColumn: k.rightColumn, rightColumn: k.leftColumn })),
|
|
46
|
+
...(join.type ? { type: FLIPPED[join.type] } : {}),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** A join as the query reads it from `from`: drawn with `from` on its left. */
|
|
50
|
+
export function joinFrom(join, from) {
|
|
51
|
+
return same(join.left, from) ? join : flipJoin(join);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The names the joined tables' columns take in the result, in the order the
|
|
55
|
+
* query reads the tables: the first use of a name keeps it, and a name a
|
|
56
|
+
* later table repeats gets `_1`, `_2` and so on (`store_id_1`). The query
|
|
57
|
+
* writes these as aliases, so the names are the same on every engine.
|
|
58
|
+
*/
|
|
59
|
+
export function outputColumns(tables, columnsOf) {
|
|
60
|
+
const uses = new Map();
|
|
61
|
+
const taken = new Set();
|
|
62
|
+
const out = [];
|
|
63
|
+
for (const table of tables) {
|
|
64
|
+
for (const column of columnsOf(table)) {
|
|
65
|
+
let n = uses.get(lower(column)) ?? 0;
|
|
66
|
+
let name = n === 0 ? column : `${column}_${n}`;
|
|
67
|
+
// A table with a real column called store_id_1 does not lose it to a renamed one.
|
|
68
|
+
while (taken.has(lower(name)))
|
|
69
|
+
name = `${column}_${++n}`;
|
|
70
|
+
uses.set(lower(column), n + 1);
|
|
71
|
+
taken.add(lower(name));
|
|
72
|
+
out.push({ table, column, name });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
/** A column name in SQL, always quoted, so a name such as `format` or `Region` reads the same on every engine. */
|
|
78
|
+
const quoteAlways = (name) => `"${name.replace(/"/g, '""')}"`;
|
|
79
|
+
/**
|
|
80
|
+
* SQL that joins the diagram's tables, starting from the first and following
|
|
81
|
+
* joins outwards, each with the join kind it was given (an inner join when
|
|
82
|
+
* none). Tables no join reaches are returned in `unjoined`, not cross-joined.
|
|
83
|
+
*
|
|
84
|
+
* With `columnsOf` knowing every table's columns and two tables sharing a
|
|
85
|
+
* column name, the query names each
|
|
86
|
+
* column, giving a name a later table repeats its own alias
|
|
87
|
+
* (`outputColumns`). Engines differ over `SELECT *` across tables that share
|
|
88
|
+
* a column name (DuckDB renames, Teradata and others refuse it inside a
|
|
89
|
+
* query), so the named form is the one that runs everywhere. Without the
|
|
90
|
+
* columns it falls back to `SELECT *`.
|
|
91
|
+
*/
|
|
92
|
+
export function modelSql(model, columnsOf) {
|
|
93
|
+
const [first, ...rest] = model.tables;
|
|
94
|
+
if (!first)
|
|
95
|
+
return { sql: "", unjoined: [], order: [] };
|
|
96
|
+
const reached = [first];
|
|
97
|
+
const pending = new Set(rest.map(lower));
|
|
98
|
+
const lines = [`FROM ${quoteTable(first)}`];
|
|
99
|
+
for (let progress = true; progress && pending.size > 0;) {
|
|
100
|
+
progress = false;
|
|
101
|
+
for (const join of model.joins) {
|
|
102
|
+
const leftIn = reached.some((t) => same(t, join.left));
|
|
103
|
+
const rightIn = reached.some((t) => same(t, join.right));
|
|
104
|
+
if (leftIn === rightIn)
|
|
105
|
+
continue;
|
|
106
|
+
const next = leftIn ? join.right : join.left;
|
|
107
|
+
if (!pending.has(lower(next)))
|
|
108
|
+
continue;
|
|
109
|
+
// Every join between the new table and those already reached goes into one ON clause.
|
|
110
|
+
const conditions = model.joins
|
|
111
|
+
.filter((j) => (same(j.left, next) && reached.some((t) => same(t, j.right))) || (same(j.right, next) && reached.some((t) => same(t, j.left))))
|
|
112
|
+
.map(joinText);
|
|
113
|
+
// Read from the side already reached, so "every orders row" stays that whichever way the line was drawn.
|
|
114
|
+
const kind = joinFrom(join, leftIn ? join.left : join.right).type ?? "inner";
|
|
115
|
+
lines.push(`${KEYWORD[kind]} ${quoteTable(next)} ON ${conditions.join(" AND ")}`);
|
|
116
|
+
reached.push(next);
|
|
117
|
+
pending.delete(lower(next));
|
|
118
|
+
progress = true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const unjoined = model.tables.filter((t) => pending.has(lower(t)));
|
|
122
|
+
const known = columnsOf ? reached.map((t) => columnsOf(t)) : [];
|
|
123
|
+
if (!columnsOf || known.some((c) => !c || c.length === 0))
|
|
124
|
+
return { sql: [`SELECT *`, ...lines].join("\n"), unjoined, order: reached };
|
|
125
|
+
const columns = outputColumns(reached, (t) => known[reached.indexOf(t)] ?? []);
|
|
126
|
+
// With no name repeated, every engine reads SELECT * the same way, and it stays short.
|
|
127
|
+
if (columns.every((c) => c.name === c.column))
|
|
128
|
+
return { sql: [`SELECT *`, ...lines].join("\n"), unjoined, order: reached, columns };
|
|
129
|
+
const list = columns.map((c) => `${quoteTable(c.table)}.${quoteAlways(c.column)}${c.name === c.column ? "" : ` AS ${quoteAlways(c.name)}`}`);
|
|
130
|
+
return { sql: [`SELECT ${list.join(",\n ")}`, ...lines].join("\n"), unjoined, order: reached, columns };
|
|
131
|
+
}
|
|
132
|
+
export function joinShape(join, from, columnsOf, foreignKeysOf = () => undefined) {
|
|
133
|
+
const read = joinFrom(join, from);
|
|
134
|
+
const near = read.keys.map((k) => k.leftColumn);
|
|
135
|
+
const far = read.keys.map((k) => k.rightColumn);
|
|
136
|
+
const key = (columnsOf(read.right) ?? []).filter((c) => c.primaryKey).map((c) => c.name);
|
|
137
|
+
if (key.length > 0 && key.every((k) => far.some((f) => same(f, k))))
|
|
138
|
+
return "one";
|
|
139
|
+
const declared = (foreignKeysOf(read.left) ?? []).some((fk) => same(fk.references?.table ?? "", read.right) && fk.columns.length > 0 && fk.columns.every((c) => near.some((n) => same(n, c))) && fk.referencedColumns.every((c) => far.some((f) => same(f, c))));
|
|
140
|
+
if (declared)
|
|
141
|
+
return "one";
|
|
142
|
+
return key.length > 0 ? "many" : "unknown";
|
|
143
|
+
}
|
|
144
|
+
/** How far each table is from the first through joins: 0 for the first, 1 for those it joins, and so on. Tables no join reaches come after the rest. */
|
|
145
|
+
export function joinDepths(tables, joins) {
|
|
146
|
+
const depth = new Map();
|
|
147
|
+
if (tables[0])
|
|
148
|
+
depth.set(tables[0], 0);
|
|
149
|
+
for (let grew = true; grew;) {
|
|
150
|
+
grew = false;
|
|
151
|
+
for (const join of joins) {
|
|
152
|
+
const l = tables.find((t) => same(t, join.left));
|
|
153
|
+
const r = tables.find((t) => same(t, join.right));
|
|
154
|
+
if (!l || !r)
|
|
155
|
+
continue;
|
|
156
|
+
const [a, b] = depth.has(l) && !depth.has(r) ? [l, r] : depth.has(r) && !depth.has(l) ? [r, l] : [];
|
|
157
|
+
if (a && b) {
|
|
158
|
+
depth.set(b, depth.get(a) + 1);
|
|
159
|
+
grew = true;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const last = Math.max(0, ...depth.values()) + 1;
|
|
164
|
+
for (const t of tables)
|
|
165
|
+
if (!depth.has(t))
|
|
166
|
+
depth.set(t, last);
|
|
167
|
+
return depth;
|
|
168
|
+
}
|
|
169
|
+
const FAMILY_TEXT = { number: "a number", text: "text", time: "a date or time", boolean: "true or false", binary: "binary data", nested: "nested data", unknown: "of an unknown type" };
|
|
170
|
+
export { typeFamily };
|
|
171
|
+
/**
|
|
172
|
+
* Whether a join can work at all. It refuses a table joined to itself, a
|
|
173
|
+
* column the table does not have, keys that cannot be compared (nested or
|
|
174
|
+
* binary data) and keys of different kinds, such as a number against text.
|
|
175
|
+
* `columnsOf` returns undefined for a table whose columns are not known yet;
|
|
176
|
+
* such a table is given the benefit of the doubt.
|
|
177
|
+
*/
|
|
178
|
+
export function checkJoin(join, columnsOf) {
|
|
179
|
+
if (same(join.left, join.right))
|
|
180
|
+
return { ok: false, reason: "A join needs two different tables." };
|
|
181
|
+
if (join.keys.length === 0)
|
|
182
|
+
return { ok: false, reason: "A join needs a column from each table." };
|
|
183
|
+
for (const key of join.keys) {
|
|
184
|
+
const sides = [
|
|
185
|
+
{ table: join.left, name: key.leftColumn },
|
|
186
|
+
{ table: join.right, name: key.rightColumn },
|
|
187
|
+
].map((side) => {
|
|
188
|
+
const columns = columnsOf(side.table);
|
|
189
|
+
const column = columns?.find((c) => same(c.name, side.name));
|
|
190
|
+
return { ...side, known: columns !== undefined, column, family: column ? typeFamily(column) : "unknown" };
|
|
191
|
+
});
|
|
192
|
+
for (const side of sides) {
|
|
193
|
+
if (side.known && !side.column)
|
|
194
|
+
return { ok: false, reason: `${side.table} has no column ${side.name}.` };
|
|
195
|
+
if (side.family === "nested" || side.family === "binary")
|
|
196
|
+
return { ok: false, reason: `${side.table}.${side.name} holds ${FAMILY_TEXT[side.family]}, which cannot be matched.` };
|
|
197
|
+
}
|
|
198
|
+
const [a, b] = sides;
|
|
199
|
+
if (a.family !== "unknown" && b.family !== "unknown" && a.family !== b.family) {
|
|
200
|
+
return { ok: false, reason: `${a.table}.${a.name} is ${FAMILY_TEXT[a.family]} and ${b.table}.${b.name} is ${FAMILY_TEXT[b.family]}, so they can never match.` };
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return { ok: true };
|
|
204
|
+
}
|
|
205
|
+
/** True when `from` already reaches `to` through joins, so one more join between them closes a loop. */
|
|
206
|
+
export function connected(joins, from, to) {
|
|
207
|
+
const seen = new Set([lower(from)]);
|
|
208
|
+
for (let grew = true; grew;) {
|
|
209
|
+
grew = false;
|
|
210
|
+
for (const join of joins) {
|
|
211
|
+
const [l, r] = [lower(join.left), lower(join.right)];
|
|
212
|
+
if (seen.has(l) !== seen.has(r)) {
|
|
213
|
+
seen.add(l);
|
|
214
|
+
seen.add(r);
|
|
215
|
+
grew = true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return seen.has(lower(to));
|
|
220
|
+
}
|
|
221
|
+
/** Where a new table goes: the next free slot in rows of three. */
|
|
222
|
+
function slot(index) {
|
|
223
|
+
return { x: 16 + (index % 3) * (CARD_WIDTH + GAP), y: 16 + Math.floor(index / 3) * (HEAD_HEIGHT + ROW_HEIGHT * (MAX_ROWS + 2) + 24) };
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* A relationship diagram of tables: each table is a card listing its
|
|
227
|
+
* columns, and lines connect the columns that join. Drag a card to arrange
|
|
228
|
+
* it, drag a column onto a column of another table to draw a join, and drop
|
|
229
|
+
* a table from ObjectBrowser to add it. Suggested joins are dashed and
|
|
230
|
+
* labelled "Inferred" until a person confirms them.
|
|
231
|
+
*
|
|
232
|
+
* Everything the canvas does is also available from the keyboard through
|
|
233
|
+
* the lists and the "Add join" form beneath it.
|
|
234
|
+
*/
|
|
235
|
+
export function ModelDiagram(props) {
|
|
236
|
+
const { sourceId, height = 420, suggestJoins = true, rowsFrom = false } = props;
|
|
237
|
+
const dataSource = useDataSource(props.dataSource);
|
|
238
|
+
const discovery = useOptionalDiscovery(props.discovery);
|
|
239
|
+
const id = useId();
|
|
240
|
+
const startKey = JSON.stringify([props.tables ?? [], props.joins ?? []]);
|
|
241
|
+
const [tables, setTables] = useState(props.tables ?? []);
|
|
242
|
+
const [joins, setJoins] = useState(props.joins ?? []);
|
|
243
|
+
const [positions, setPositions] = useState({});
|
|
244
|
+
const [details, setDetails] = useState({});
|
|
245
|
+
const [marks, setMarks] = useState({});
|
|
246
|
+
// What each table declares about itself: its description and foreign keys.
|
|
247
|
+
const [about, setAbout] = useState({});
|
|
248
|
+
const [expanded, setExpanded] = useState(new Set());
|
|
249
|
+
const [link, setLink] = useState();
|
|
250
|
+
const [search, setSearch] = useState("");
|
|
251
|
+
const [matches, setMatches] = useState([]);
|
|
252
|
+
const [draft, setDraft] = useState({ left: "", leftColumn: "", right: "", rightColumn: "" });
|
|
253
|
+
const [status, setStatus] = useState("");
|
|
254
|
+
const [problem, setProblem] = useState("");
|
|
255
|
+
const [retries, setRetries] = useState(0);
|
|
256
|
+
// The engine's answer, per join and direction, to how many rows of the far table share one key.
|
|
257
|
+
const [counts, setCounts] = useState({});
|
|
258
|
+
const canvas = useRef(null);
|
|
259
|
+
const moving = useRef(undefined);
|
|
260
|
+
const considered = useRef(new Set());
|
|
261
|
+
const onChange = useRef(props.onChange);
|
|
262
|
+
onChange.current = props.onChange;
|
|
263
|
+
// New starting tables or joins from the host start the diagram again.
|
|
264
|
+
const started = useRef(startKey);
|
|
265
|
+
useEffect(() => {
|
|
266
|
+
if (started.current === startKey)
|
|
267
|
+
return;
|
|
268
|
+
started.current = startKey;
|
|
269
|
+
considered.current = new Set();
|
|
270
|
+
setTables(props.tables ?? []);
|
|
271
|
+
setJoins(props.joins ?? []);
|
|
272
|
+
setPositions({});
|
|
273
|
+
}, [startKey]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
274
|
+
const model = useMemo(() => ({ tables, joins }), [tables, joins]);
|
|
275
|
+
/** The side of a join nearer the first table: the one the query reaches first. */
|
|
276
|
+
const nearOf = (join, current = model) => {
|
|
277
|
+
const order = modelSql(current).order;
|
|
278
|
+
const rank = (t) => {
|
|
279
|
+
const i = order.findIndex((o) => same(o, t));
|
|
280
|
+
return i >= 0 ? i : order.length + current.tables.findIndex((x) => same(x, t));
|
|
281
|
+
};
|
|
282
|
+
return rank(join.left) <= rank(join.right) ? join.left : join.right;
|
|
283
|
+
};
|
|
284
|
+
/** With rowsFrom, a join no one gave a kind keeps every row of its nearer side, so adding detail never drops the first table's rows. */
|
|
285
|
+
const orient = (join, current) => (!rowsFrom || join.type ? join : { ...joinFrom(join, nearOf(join, current)), type: "left" });
|
|
286
|
+
// Replies are kept while the source they were asked of is still the one shown,
|
|
287
|
+
// even if this effect runs again before they arrive (it does not ask twice).
|
|
288
|
+
const live = useRef({ sourceId, dataSource });
|
|
289
|
+
live.current = { sourceId, dataSource };
|
|
290
|
+
// Columns, certification and suggested joins for each table as it arrives.
|
|
291
|
+
useEffect(() => {
|
|
292
|
+
const current = () => live.current.sourceId === sourceId && live.current.dataSource === dataSource;
|
|
293
|
+
for (const table of tables) {
|
|
294
|
+
if (details[table] !== undefined)
|
|
295
|
+
continue;
|
|
296
|
+
setDetails((previous) => ({ ...previous, [table]: "loading" }));
|
|
297
|
+
dataSource
|
|
298
|
+
.describeTable({ sourceId, ref: { table } })
|
|
299
|
+
.then((response) => {
|
|
300
|
+
if (!current())
|
|
301
|
+
return;
|
|
302
|
+
setDetails((previous) => ({ ...previous, [table]: response.table?.columns ?? [] }));
|
|
303
|
+
setAbout((previous) => ({ ...previous, [table]: { comment: response.table?.summary?.comment ?? "", foreignKeys: response.table?.foreignKeys ?? [] } }));
|
|
304
|
+
})
|
|
305
|
+
.catch((error) => current() && setDetails((previous) => ({ ...previous, [table]: { error: messageOf(error) } })));
|
|
306
|
+
if (!discovery)
|
|
307
|
+
continue;
|
|
308
|
+
const ref = { sourceId, kind: ObjectKind.TABLE, table };
|
|
309
|
+
discovery
|
|
310
|
+
.getObject({ ref })
|
|
311
|
+
.then((response) => {
|
|
312
|
+
const mark = response.profile?.object?.certification;
|
|
313
|
+
if (current() && mark)
|
|
314
|
+
setMarks((previous) => ({ ...previous, [table]: mark }));
|
|
315
|
+
})
|
|
316
|
+
.catch(() => { });
|
|
317
|
+
}
|
|
318
|
+
}, [tables, dataSource, discovery, sourceId, retries]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
319
|
+
useEffect(() => {
|
|
320
|
+
if (!discovery || !suggestJoins)
|
|
321
|
+
return;
|
|
322
|
+
let current = true;
|
|
323
|
+
for (const table of tables) {
|
|
324
|
+
discovery
|
|
325
|
+
.getRelated({ ref: { sourceId, kind: ObjectKind.TABLE, table }, kinds: [RelationKind.JOIN] })
|
|
326
|
+
.then((response) => {
|
|
327
|
+
if (!current)
|
|
328
|
+
return;
|
|
329
|
+
const found = [];
|
|
330
|
+
for (const relation of response.relations) {
|
|
331
|
+
const other = relation.object?.ref?.table;
|
|
332
|
+
if (!other || !tables.some((t) => same(t, other)) || relation.keys.length === 0)
|
|
333
|
+
continue;
|
|
334
|
+
const join = { left: table, right: other, keys: relation.keys.map((k) => ({ leftColumn: k.leftColumn, rightColumn: k.rightColumn })), inferred: relation.inferred };
|
|
335
|
+
// A suggestion is offered once; removing it does not bring it back.
|
|
336
|
+
if (considered.current.has(joinId(join)))
|
|
337
|
+
continue;
|
|
338
|
+
considered.current.add(joinId(join));
|
|
339
|
+
found.push(join);
|
|
340
|
+
}
|
|
341
|
+
if (found.length > 0)
|
|
342
|
+
setJoins((previous) => [...previous, ...found.filter((j) => !previous.some((p) => joinId(p) === joinId(j))).map((j) => orient(j, { tables, joins: previous }))]);
|
|
343
|
+
})
|
|
344
|
+
.catch(() => { });
|
|
345
|
+
}
|
|
346
|
+
return () => {
|
|
347
|
+
current = false;
|
|
348
|
+
};
|
|
349
|
+
}, [tables, discovery, sourceId, suggestJoins]);
|
|
350
|
+
// Foreign keys a table declares become joins once both tables are on the diagram.
|
|
351
|
+
// They are facts from the backend, not guesses, so they are not marked "Inferred".
|
|
352
|
+
useEffect(() => {
|
|
353
|
+
const found = [];
|
|
354
|
+
for (const table of tables) {
|
|
355
|
+
for (const fk of about[table]?.foreignKeys ?? []) {
|
|
356
|
+
const other = tables.find((t) => same(t, fk.references?.table ?? ""));
|
|
357
|
+
if (!other || fk.columns.length === 0)
|
|
358
|
+
continue;
|
|
359
|
+
const join = { left: table, right: other, keys: fk.columns.map((c, i) => ({ leftColumn: c, rightColumn: fk.referencedColumns[i] ?? c })), declared: true };
|
|
360
|
+
if (considered.current.has(joinId(join)))
|
|
361
|
+
continue;
|
|
362
|
+
considered.current.add(joinId(join));
|
|
363
|
+
found.push(join);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
if (found.length === 0)
|
|
367
|
+
return;
|
|
368
|
+
setJoins((previous) => {
|
|
369
|
+
// A declared key settles a guess about the same columns.
|
|
370
|
+
const kept = previous.map((p) => {
|
|
371
|
+
const match = found.find((f) => joinId(f) === joinId(p));
|
|
372
|
+
return match ? { ...p, inferred: false, declared: true } : p;
|
|
373
|
+
});
|
|
374
|
+
return [...kept, ...found.filter((f) => !previous.some((p) => joinId(p) === joinId(f))).map((f) => orient(f, { tables, joins: kept }))];
|
|
375
|
+
});
|
|
376
|
+
}, [tables, about]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
377
|
+
// Table search runs in the backend, so it works on catalogs too large to list.
|
|
378
|
+
useEffect(() => {
|
|
379
|
+
const text = search.trim();
|
|
380
|
+
if (!text)
|
|
381
|
+
return setMatches([]);
|
|
382
|
+
let current = true;
|
|
383
|
+
const timer = setTimeout(() => {
|
|
384
|
+
dataSource
|
|
385
|
+
.listTables({ sourceId, search: text, pageSize: 8 })
|
|
386
|
+
.then((response) => current && setMatches(response.tables.map((t) => t.ref?.table ?? "").filter((name) => name && !tables.some((t) => same(t, name)))))
|
|
387
|
+
.catch((error) => {
|
|
388
|
+
if (!current)
|
|
389
|
+
return;
|
|
390
|
+
setMatches([]);
|
|
391
|
+
setProblem(`Could not search for tables: ${messageOf(error)}`);
|
|
392
|
+
});
|
|
393
|
+
}, 180);
|
|
394
|
+
return () => {
|
|
395
|
+
current = false;
|
|
396
|
+
clearTimeout(timer);
|
|
397
|
+
};
|
|
398
|
+
}, [search, dataSource, sourceId, tables]);
|
|
399
|
+
const positionOf = (table) => positions[table] ?? layout?.[table] ?? slot(tables.findIndex((t) => t === table));
|
|
400
|
+
const columnsOf = (table) => (Array.isArray(details[table]) ? details[table] : []);
|
|
401
|
+
const visibleColumns = (table) => (expanded.has(table) ? columnsOf(table) : columnsOf(table).slice(0, MAX_ROWS));
|
|
402
|
+
const cardHeight = (table) => {
|
|
403
|
+
const all = columnsOf(table).length;
|
|
404
|
+
const shown = visibleColumns(table).length;
|
|
405
|
+
return HEAD_HEIGHT + Math.max(1, shown + (all > MAX_ROWS ? 1 : 0)) * ROW_HEIGHT + 6;
|
|
406
|
+
};
|
|
407
|
+
const addTable = (name, at) => {
|
|
408
|
+
if (!name || tables.some((t) => same(t, name)))
|
|
409
|
+
return;
|
|
410
|
+
if (at)
|
|
411
|
+
setPositions((previous) => ({ ...previous, [name]: at }));
|
|
412
|
+
setTables((previous) => [...previous, name]);
|
|
413
|
+
setStatus(`Added ${name}.`);
|
|
414
|
+
setSearch("");
|
|
415
|
+
};
|
|
416
|
+
const removeTable = (name) => {
|
|
417
|
+
setTables((previous) => previous.filter((t) => t !== name));
|
|
418
|
+
setJoins((previous) => previous.filter((j) => !same(j.left, name) && !same(j.right, name)));
|
|
419
|
+
setStatus(`Removed ${name}.`);
|
|
420
|
+
};
|
|
421
|
+
const knownColumns = (table) => (Array.isArray(details[table]) ? details[table] : undefined);
|
|
422
|
+
const check = (join) => checkJoin(join, knownColumns);
|
|
423
|
+
const refuse = (reason) => {
|
|
424
|
+
setProblem(`Not joined. ${reason}`);
|
|
425
|
+
setStatus("");
|
|
426
|
+
};
|
|
427
|
+
const removeKey = (join, key) => {
|
|
428
|
+
setJoins((previous) => previous.flatMap((j) => {
|
|
429
|
+
if (joinId(j) !== joinId(join))
|
|
430
|
+
return [j];
|
|
431
|
+
const keys = j.keys.filter((k) => !(same(k.leftColumn, key.leftColumn) && same(k.rightColumn, key.rightColumn)));
|
|
432
|
+
return keys.length > 0 ? [{ ...j, keys }] : [];
|
|
433
|
+
}));
|
|
434
|
+
setProblem("");
|
|
435
|
+
setStatus(`Removed ${joinText({ ...join, keys: [key] })}.`);
|
|
436
|
+
};
|
|
437
|
+
const addJoin = (join) => {
|
|
438
|
+
const result = check(join);
|
|
439
|
+
if (!result.ok)
|
|
440
|
+
return refuse(result.reason);
|
|
441
|
+
const isNew = !joins.some((j) => joinId(j) === joinId(join));
|
|
442
|
+
// A second route between two tables is allowed, but it narrows the result, so say so.
|
|
443
|
+
const loop = isNew && connected(joins.filter(present).filter((j) => joinId({ ...j, keys: [] }) !== joinId({ ...join, keys: [] })), join.left, join.right);
|
|
444
|
+
setProblem("");
|
|
445
|
+
considered.current.add(joinId(join));
|
|
446
|
+
setJoins((previous) => {
|
|
447
|
+
const existing = previous.findIndex((j) => joinId(j) === joinId(join));
|
|
448
|
+
// Drawing a join that was only suggested confirms it.
|
|
449
|
+
if (existing >= 0)
|
|
450
|
+
return previous.map((j, i) => (i === existing ? { ...j, inferred: false } : j));
|
|
451
|
+
return [...previous, orient(join, { tables, joins: previous })];
|
|
452
|
+
});
|
|
453
|
+
setStatus(`Joined ${joinText(join)}.${loop ? ` ${join.left} and ${join.right} were already connected another way, so rows must now match on both routes.` : ""}`);
|
|
454
|
+
};
|
|
455
|
+
/** Set which rows a join keeps, `read` being the join as the person saw it (its left side first). */
|
|
456
|
+
const setKind = (join, read, kind) => {
|
|
457
|
+
setJoins((previous) => previous.map((j) => (joinId(j) === joinId(join) ? { ...read, type: kind } : j)));
|
|
458
|
+
setStatus(`${joinText(read)} now keeps ${kindText(read, kind).toLowerCase()}.`);
|
|
459
|
+
};
|
|
460
|
+
/** Make another table the one whose rows the result's rows are. Joins that kept every row of their nearer side go on doing so from the new first table. */
|
|
461
|
+
const makeBase = (table) => {
|
|
462
|
+
const nextTables = [table, ...tables.filter((t) => !same(t, table))];
|
|
463
|
+
setJoins((previous) => previous.map((j) => {
|
|
464
|
+
const kept = joinFrom(j, nearOf(j, { tables, joins: previous })).type === "left";
|
|
465
|
+
return kept ? { ...joinFrom(j, nearOf(j, { tables: nextTables, joins: previous })), type: "left" } : j;
|
|
466
|
+
}));
|
|
467
|
+
setTables(nextTables);
|
|
468
|
+
setStatus(`The rows now come from ${table}: one row of the result is one ${table} row.`);
|
|
469
|
+
};
|
|
470
|
+
const countKey = (join, far) => `${joinId(join)}>${lower(far)}`;
|
|
471
|
+
/** Ask the engine how many rows of the far table share one key: one grouped count, no values back. */
|
|
472
|
+
const checkRows = (join, near) => {
|
|
473
|
+
const read = joinFrom(join, near);
|
|
474
|
+
const key = countKey(join, read.right);
|
|
475
|
+
setCounts((previous) => ({ ...previous, [key]: "checking" }));
|
|
476
|
+
mostRowsPerKey(dataSource, { sourceId, sql: `SELECT * FROM ${quoteTable(read.right)}`, columns: read.keys.map((k) => k.rightColumn) })
|
|
477
|
+
.then((most) => {
|
|
478
|
+
setCounts((previous) => ({ ...previous, [key]: most }));
|
|
479
|
+
setStatus(most > 1 ? `Up to ${most} ${read.right} rows share one ${read.keys.map((k) => k.rightColumn).join(" and ")}, so this join can repeat ${read.left} rows.` : `Each ${read.left} row finds at most one ${read.right} row.`);
|
|
480
|
+
})
|
|
481
|
+
.catch((error) => setCounts((previous) => ({ ...previous, [key]: { error: messageOf(error) } })));
|
|
482
|
+
};
|
|
483
|
+
const present = (join) => tables.some((t) => same(t, join.left)) && tables.some((t) => same(t, join.right));
|
|
484
|
+
// With rowsFrom the first table stands at the left and each table sits one column further out than the table it adds detail to.
|
|
485
|
+
const layout = rowsFrom
|
|
486
|
+
? (() => {
|
|
487
|
+
const depths = joinDepths(tables, joins);
|
|
488
|
+
const next = new Map();
|
|
489
|
+
const out = {};
|
|
490
|
+
for (const table of tables) {
|
|
491
|
+
const depth = depths.get(table) ?? 0;
|
|
492
|
+
const y = next.get(depth) ?? 16;
|
|
493
|
+
out[table] = { x: 16 + depth * (CARD_WIDTH + GAP * 2), y };
|
|
494
|
+
next.set(depth, y + cardHeight(table) + 24);
|
|
495
|
+
}
|
|
496
|
+
return out;
|
|
497
|
+
})()
|
|
498
|
+
: undefined;
|
|
499
|
+
// The drawing's extent on the plane, which fit to view frames.
|
|
500
|
+
const extent = {
|
|
501
|
+
width: tables.length === 0 ? 0 : Math.max(...tables.map((t) => positionOf(t).x + CARD_WIDTH)) + 16,
|
|
502
|
+
height: tables.length === 0 ? 0 : Math.max(...tables.map((t) => positionOf(t).y + cardHeight(t))) + 16,
|
|
503
|
+
};
|
|
504
|
+
const panZoom = usePanZoom({ content: extent });
|
|
505
|
+
const setViewport = panZoom.viewportRef;
|
|
506
|
+
const canvasRef = useCallback((element) => {
|
|
507
|
+
canvas.current = element;
|
|
508
|
+
setViewport(element);
|
|
509
|
+
}, [setViewport]);
|
|
510
|
+
// A pointer on the plane, whatever the pan and zoom.
|
|
511
|
+
const canvasPoint = (event) => panZoom.pointToWorld(event);
|
|
512
|
+
const startMove = (table) => (event) => {
|
|
513
|
+
if (event.target.closest("button"))
|
|
514
|
+
return;
|
|
515
|
+
const point = canvasPoint(event);
|
|
516
|
+
const at = positionOf(table);
|
|
517
|
+
moving.current = { table, dx: point.x - at.x, dy: point.y - at.y };
|
|
518
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
519
|
+
};
|
|
520
|
+
const startLink = (table, column) => (event) => {
|
|
521
|
+
event.stopPropagation();
|
|
522
|
+
setLink({ table, column, to: canvasPoint(event) });
|
|
523
|
+
canvas.current?.setPointerCapture?.(event.pointerId);
|
|
524
|
+
};
|
|
525
|
+
const onPointerMove = (event) => {
|
|
526
|
+
const point = canvasPoint(event);
|
|
527
|
+
const move = moving.current;
|
|
528
|
+
if (move)
|
|
529
|
+
setPositions((previous) => ({ ...previous, [move.table]: { x: Math.max(0, point.x - move.dx), y: Math.max(0, point.y - move.dy) } }));
|
|
530
|
+
else if (link)
|
|
531
|
+
setLink({ ...link, to: point });
|
|
532
|
+
};
|
|
533
|
+
const onPointerUp = (event) => {
|
|
534
|
+
moving.current = undefined;
|
|
535
|
+
if (!link)
|
|
536
|
+
return;
|
|
537
|
+
canvas.current?.releasePointerCapture?.(event.pointerId);
|
|
538
|
+
const target = document.elementFromPoint?.(event.clientX, event.clientY)?.closest("[data-model-column]");
|
|
539
|
+
const table = target?.dataset.modelTable;
|
|
540
|
+
const column = target?.dataset.modelColumn;
|
|
541
|
+
if (table && column)
|
|
542
|
+
addJoin({ left: link.table, right: table, keys: [{ leftColumn: link.column, rightColumn: column }] });
|
|
543
|
+
else
|
|
544
|
+
setStatus("Drop the column on a column of another table to join them.");
|
|
545
|
+
setLink(undefined);
|
|
546
|
+
};
|
|
547
|
+
const onDrop = (event) => {
|
|
548
|
+
const raw = event.dataTransfer.getData(SCHEMA_REF_MIME);
|
|
549
|
+
if (!raw)
|
|
550
|
+
return;
|
|
551
|
+
event.preventDefault();
|
|
552
|
+
try {
|
|
553
|
+
const ref = JSON.parse(raw);
|
|
554
|
+
const point = canvasPoint(event);
|
|
555
|
+
addTable(ref.table, { x: Math.max(0, point.x - CARD_WIDTH / 2), y: Math.max(0, point.y - HEAD_HEIGHT / 2) });
|
|
556
|
+
}
|
|
557
|
+
catch {
|
|
558
|
+
// Not one of ours.
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
/** Where a join line meets a table: beside the column's row, on the side facing the other table. */
|
|
562
|
+
const anchor = (table, column, other) => {
|
|
563
|
+
const at = positionOf(table);
|
|
564
|
+
const index = visibleColumns(table).findIndex((c) => same(c.name, column));
|
|
565
|
+
const row = index >= 0 ? index : visibleColumns(table).length;
|
|
566
|
+
const facingRight = positionOf(other).x + CARD_WIDTH / 2 >= at.x + CARD_WIDTH / 2;
|
|
567
|
+
return { x: at.x + (facingRight ? CARD_WIDTH : 0), y: at.y + HEAD_HEIGHT + row * ROW_HEIGHT + ROW_HEIGHT / 2 };
|
|
568
|
+
};
|
|
569
|
+
const curve = (from, to) => {
|
|
570
|
+
const bend = Math.max(40, Math.abs(to.x - from.x) / 2);
|
|
571
|
+
const direction = to.x >= from.x ? 1 : -1;
|
|
572
|
+
return `M ${from.x} ${from.y} C ${from.x + bend * direction} ${from.y}, ${to.x - bend * direction} ${to.y}, ${to.x} ${to.y}`;
|
|
573
|
+
};
|
|
574
|
+
const width = Math.max(extent.width + 16, 320);
|
|
575
|
+
const depth = Math.max(extent.height + 16, 120);
|
|
576
|
+
const shown = joins.filter(present);
|
|
577
|
+
const broken = new Map(shown.flatMap((j) => { const r = check(j); return r.ok ? [] : [[joinId(j), r.reason]]; }));
|
|
578
|
+
const unreadable = tables.filter((t) => typeof details[t] === "object" && !Array.isArray(details[t]));
|
|
579
|
+
// SQL is built only from joins that can work, over tables that could be read.
|
|
580
|
+
// With rowsFrom a guess no one confirmed stays out of the query too.
|
|
581
|
+
const generated = modelSql({ tables: tables.filter((t) => !unreadable.includes(t)), joins: shown.filter((j) => !broken.has(joinId(j)) && !(rowsFrom && j.inferred)) }, (t) => (Array.isArray(details[t]) ? details[t].map((c) => c.name) : undefined));
|
|
582
|
+
// Report the tables and joins, and the query again once the columns are read and it can name them.
|
|
583
|
+
const reported = useRef({ model, sql: generated.sql });
|
|
584
|
+
useEffect(() => {
|
|
585
|
+
if (reported.current.model === model && reported.current.sql === generated.sql)
|
|
586
|
+
return;
|
|
587
|
+
reported.current = { model, sql: generated.sql };
|
|
588
|
+
onChange.current?.(model, generated);
|
|
589
|
+
});
|
|
590
|
+
const outputName = new Map((generated.columns ?? []).map((c) => [lower(`${c.table}.${c.column}`), c.name]));
|
|
591
|
+
const used = props.usedColumns ? new Set(props.usedColumns.map(lower)) : undefined;
|
|
592
|
+
const base = tables[0] ?? "";
|
|
593
|
+
/** What a join does to the rows, read from its nearer side: its shape (from the tables' keys, or the engine's count) and what its kind keeps. */
|
|
594
|
+
const effectOf = (join) => {
|
|
595
|
+
const near = rowsFrom ? nearOf(join) : join.left;
|
|
596
|
+
const read = joinFrom(join, near);
|
|
597
|
+
const far = read.right;
|
|
598
|
+
const counted = counts[countKey(join, far)];
|
|
599
|
+
const shape = typeof counted === "number" ? (counted > 1 ? "many" : "one") : counted === "checking" ? "checking" : joinShape(join, near, knownColumns, (t) => about[t]?.foreignKeys);
|
|
600
|
+
const shapeText = shape === "one"
|
|
601
|
+
? `One ${far} row for each ${near} row${typeof counted === "number" ? ", checked by the engine" : ""}.`
|
|
602
|
+
: shape === "many"
|
|
603
|
+
? typeof counted === "number"
|
|
604
|
+
? `Can repeat ${near} rows: up to ${counted} ${far} rows share one ${read.keys.map((k) => k.rightColumn).join(" and ")}.`
|
|
605
|
+
: `Can repeat ${near} rows: ${far} has a key this join does not use, so more than one row may match.`
|
|
606
|
+
: shape === "checking"
|
|
607
|
+
? `Counting ${far} rows per key…`
|
|
608
|
+
: `Not known whether ${far} has more than one row per ${near} row.`;
|
|
609
|
+
const kind = read.type ?? "inner";
|
|
610
|
+
const kindNote = kind === "inner"
|
|
611
|
+
? `${near} rows with no ${far} row are dropped.`
|
|
612
|
+
: kind === "right" || kind === "full"
|
|
613
|
+
? `Adds ${far} rows that match no ${near} row${rowsFrom ? `, so not every row is a ${base} row` : ""}.`
|
|
614
|
+
: "";
|
|
615
|
+
const error = typeof counted === "object" ? counted.error : "";
|
|
616
|
+
return { near, far, read, shape, shapeText, kind, kindNote, error, chip: shape === "one" ? "1 per row" : shape === "many" ? "▲ can repeat" : shape === "checking" ? "Counting…" : "? not known" };
|
|
617
|
+
};
|
|
618
|
+
// The one on the canvas is for a pointer; the keyboard reaches the same choice in the list beneath, once.
|
|
619
|
+
const kindSelect = (join, read, className, onCanvas = false) => (_jsx("select", { className: className, tabIndex: onCanvas ? -1 : undefined, value: read.type ?? "inner", "aria-label": `Rows kept by ${joinText(read)}${onCanvas ? ", on the diagram" : ""}`, onPointerDown: (event) => event.stopPropagation(), onChange: (e) => setKind(join, read, e.target.value), children: JOIN_KINDS.map((k) => (_jsx("option", { value: k, children: kindText(read, k) }, k))) }));
|
|
620
|
+
// The form offers only columns that could match the one already chosen.
|
|
621
|
+
const draftFamily = draft.left && draft.leftColumn ? typeFamily(columnsOf(draft.left).find((c) => c.name === draft.leftColumn) ?? { nativeType: "", arrowType: "" }) : "unknown";
|
|
622
|
+
const joinable = (table, against) => columnsOf(table)
|
|
623
|
+
.filter((c) => { const f = typeFamily(c); return f !== "nested" && f !== "binary" && (against === "unknown" || f === "unknown" || f === against); })
|
|
624
|
+
.map((c) => c.name);
|
|
625
|
+
const referenceOf = (table, column) => {
|
|
626
|
+
const fk = about[table]?.foreignKeys.find((f) => f.columns.some((c) => same(c, column)));
|
|
627
|
+
if (!fk)
|
|
628
|
+
return undefined;
|
|
629
|
+
const at = fk.columns.findIndex((c) => same(c, column));
|
|
630
|
+
return `${fk.references?.table}.${fk.referencedColumns[at] ?? column}`;
|
|
631
|
+
};
|
|
632
|
+
const keyColumns = new Set(joins.flatMap((j) => j.keys.flatMap((k) => [lower(`${j.left}.${k.leftColumn}`), lower(`${j.right}.${k.rightColumn}`)])));
|
|
633
|
+
const pick = (label, value, options, change) => (_jsxs("label", { children: [_jsx("span", { children: label }), _jsxs("select", { className: "sf-input", value: value, onChange: (e) => change(e.target.value), children: [_jsx("option", { value: "", children: "Choose\u2026" }), options.map((o) => (_jsx("option", { children: o }, o)))] })] }));
|
|
634
|
+
return (_jsxs("div", { className: ["sf-model", props.className].filter(Boolean).join(" "), children: [_jsxs("div", { className: "sf-model-bar", children: [_jsx("input", { className: "sf-input", type: "search", placeholder: "Add a table", "aria-label": "Add a table", "aria-controls": `${id}-matches`, value: search, onChange: (e) => setSearch(e.target.value) }), _jsx("span", { id: `${id}-matches`, className: "sf-model-matches", children: matches.map((name) => (_jsxs("button", { type: "button", className: "sf-button", onClick: () => addTable(name), children: ["Add ", name] }, name))) }), props.onUseSql && (_jsx("button", { type: "button", className: "sf-button", "data-variant": "primary", disabled: generated.sql === "", onClick: () => props.onUseSql?.(generated.sql, model), children: "Use as query" }))] }), tables.length > 0 && _jsx(PanZoomControls, { panZoom: panZoom, label: "Zoom the data model" }), _jsxs("div", { ref: canvasRef, className: "sf-model-canvas sf-panzoom", style: { height }, role: "group", "aria-label": props.label ?? "Data model", "aria-describedby": `${id}-keys`, "data-linking": link ? "" : undefined, ...panZoom.viewportProps, onPointerMove: (event) => {
|
|
635
|
+
onPointerMove(event);
|
|
636
|
+
panZoom.viewportProps.onPointerMove(event);
|
|
637
|
+
}, onPointerUp: (event) => {
|
|
638
|
+
onPointerUp(event);
|
|
639
|
+
panZoom.viewportProps.onPointerUp(event);
|
|
640
|
+
}, onPointerCancel: (event) => {
|
|
641
|
+
onPointerUp(event);
|
|
642
|
+
panZoom.viewportProps.onPointerCancel(event);
|
|
643
|
+
}, onDragOver: (event) => event.dataTransfer.types.includes(SCHEMA_REF_MIME) && event.preventDefault(), onDrop: onDrop, children: [_jsx("span", { id: `${id}-keys`, className: "sf-a2-visually-hidden", children: "Drag the background or scroll to move around. Plus and minus zoom, 0 fits the diagram to view, arrow keys move it." }), _jsxs("div", { className: "sf-model-plane sf-panzoom-plane", style: { width, height: depth, ...panZoom.planeStyle }, "data-animate": panZoom.animate || undefined, children: [_jsxs("svg", { className: "sf-model-lines", width: width, height: depth, "aria-hidden": "true", children: [joins.filter(present).flatMap((join) => join.keys.map((k) => (_jsx("path", { d: curve(anchor(join.left, k.leftColumn, join.right), anchor(join.right, k.rightColumn, join.left)), "data-inferred": join.inferred || undefined, "data-broken": broken.has(joinId(join)) || undefined, "data-fanout": (rowsFrom && effectOf(join).shape === "many") || undefined }, `${joinId(join)}:${k.leftColumn}:${k.rightColumn}`)))), link && _jsx("path", { d: curve(anchor(link.table, link.column, link.table), link.to), "data-drawing": "" })] }), tables.map((table) => {
|
|
644
|
+
const at = positionOf(table);
|
|
645
|
+
const detail = details[table];
|
|
646
|
+
const all = columnsOf(table);
|
|
647
|
+
const isBase = rowsFrom && table === base;
|
|
648
|
+
const inQuery = generated.order.some((t) => same(t, table));
|
|
649
|
+
const usedHere = used ? all.filter((c) => used.has(lower(outputName.get(lower(`${table}.${c.name}`)) ?? c.name))).length : 0;
|
|
650
|
+
return (_jsxs("section", { className: "sf-model-card", "data-pan-ignore": "", "data-base": isBase || undefined, "data-outside": (rowsFrom && !inQuery) || undefined, style: { left: at.x, top: at.y, width: CARD_WIDTH }, "aria-label": `Table ${table}${isBase ? ", where the rows come from" : ""}`, children: [_jsxs("header", { className: "sf-model-head", style: { height: HEAD_HEIGHT }, onPointerDown: startMove(table), children: [_jsxs("span", { className: "sf-model-name", title: about[table]?.comment || undefined, children: [table, about[table]?.comment && _jsxs("span", { className: "sf-a2-visually-hidden", children: [". ", about[table].comment] })] }), isBase && (_jsx("span", { className: "sf-schema-tag", title: "One row of the result is one row of this table", children: "Rows" })), used && Array.isArray(detail) && (_jsxs("span", { className: "sf-model-used", title: `${usedHere} of ${all.length} columns are used by a field`, children: [usedHere, "/", all.length, _jsx("span", { className: "sf-a2-visually-hidden", children: " columns used" })] })), _jsx(CertificationBadge, { certification: marks[table] }), _jsx("button", { type: "button", className: "sf-model-remove", "aria-label": `Remove ${table} from the diagram`, onClick: () => removeTable(table), children: _jsx(ControlIcon, { icon: "close" }) })] }), detail === undefined || detail === "loading" ? (_jsx("p", { className: "sf-model-note", style: { height: ROW_HEIGHT }, children: "Loading\u2026" })) : !Array.isArray(detail) ? (_jsxs("p", { className: "sf-model-note", role: "alert", children: ["Could not read this table: ", detail.error, " ", _jsx("button", { type: "button", className: "sf-button", onClick: () => {
|
|
651
|
+
setDetails((previous) => Object.fromEntries(Object.entries(previous).filter(([name]) => name !== table)));
|
|
652
|
+
setRetries((n) => n + 1);
|
|
653
|
+
}, children: "Try again" })] })) : (_jsxs("ul", { className: "sf-model-columns", children: [visibleColumns(table).map((column) => {
|
|
654
|
+
const refers = referenceOf(table, column.name);
|
|
655
|
+
const facts = [
|
|
656
|
+
column.primaryKey && "primary key",
|
|
657
|
+
refers && `refers to ${refers}`,
|
|
658
|
+
column.partitionKey && "partition key",
|
|
659
|
+
!column.nullable && !column.primaryKey && "never empty",
|
|
660
|
+
].filter(Boolean);
|
|
661
|
+
const named = outputName.get(lower(`${table}.${column.name}`));
|
|
662
|
+
const renamed = rowsFrom && named !== undefined && named !== column.name;
|
|
663
|
+
return (_jsxs("li", { style: { height: ROW_HEIGHT }, "data-model-table": table, "data-model-column": column.name, "data-key": keyColumns.has(lower(`${table}.${column.name}`)) || undefined, "data-unused": (used && !used.has(lower(named ?? column.name))) || undefined, title: [`${column.name} ${column.nativeType.toLowerCase()}${facts.length ? ` · ${facts.join(" · ")}` : ""}`, column.comment, "Drag onto a column of another table to join"].filter(Boolean).join("\n"), onPointerDown: startLink(table, column.name), children: [_jsxs("span", { className: "sf-model-keys", "aria-hidden": "true", children: [column.primaryKey && _jsx("abbr", { className: "sf-model-key", "data-kind": "pk", children: "PK" }), refers && _jsx("abbr", { className: "sf-model-key", "data-kind": "fk", children: "FK" })] }), _jsx("span", { className: "sf-model-column", children: column.name }), _jsx("span", { className: "sf-model-type", children: renamed ? _jsxs("span", { className: "sf-model-as", children: ["as ", named] }) : column.nativeType.toLowerCase() }), renamed && _jsxs("span", { className: "sf-a2-visually-hidden", children: [", named ", named, " in the result because an earlier table has a column of this name"] }), (facts.length > 0 || column.comment) && _jsx("span", { className: "sf-a2-visually-hidden", children: [...facts, column.comment].filter(Boolean).join(". ") })] }, column.name));
|
|
664
|
+
}), all.length > MAX_ROWS && (_jsx("li", { style: { height: ROW_HEIGHT }, children: _jsx("button", { type: "button", className: "sf-model-more", onClick: () => setExpanded((previous) => {
|
|
665
|
+
const next = new Set(previous);
|
|
666
|
+
if (next.has(table))
|
|
667
|
+
next.delete(table);
|
|
668
|
+
else
|
|
669
|
+
next.add(table);
|
|
670
|
+
return next;
|
|
671
|
+
}), children: expanded.has(table) ? "Show fewer" : `${all.length - MAX_ROWS} more columns` }) }))] }))] }, table));
|
|
672
|
+
}), shown.flatMap((join) => join.keys.map((k) => {
|
|
673
|
+
const from = anchor(join.left, k.leftColumn, join.right);
|
|
674
|
+
const to = anchor(join.right, k.rightColumn, join.left);
|
|
675
|
+
return (_jsx("button", { type: "button", className: "sf-model-cut", style: { left: (from.x + to.x) / 2, top: (from.y + to.y) / 2 }, "aria-label": `Remove the line ${joinText({ ...join, keys: [k] })}`, title: "Remove this join", onPointerDown: (event) => event.stopPropagation(), onClick: () => removeKey(join, k), children: _jsx(ControlIcon, { icon: "close" }) }, `cut:${joinId(join)}:${k.leftColumn}:${k.rightColumn}`));
|
|
676
|
+
})), shown.map((join) => {
|
|
677
|
+
const k = join.keys[0];
|
|
678
|
+
if (!k || broken.has(joinId(join)))
|
|
679
|
+
return null;
|
|
680
|
+
const from = anchor(join.left, k.leftColumn, join.right);
|
|
681
|
+
const to = anchor(join.right, k.rightColumn, join.left);
|
|
682
|
+
const effect = effectOf(join);
|
|
683
|
+
return (_jsxs("div", { className: "sf-model-joinbox", "data-pan-ignore": "", "data-shape": rowsFrom ? effect.shape : undefined, style: { left: (from.x + to.x) / 2, top: (from.y + to.y) / 2 }, onPointerDown: (event) => event.stopPropagation(), children: [kindSelect(join, effect.read, "sf-model-kind", true), rowsFrom && (_jsx("span", { className: "sf-model-shape", title: `${effect.shapeText} ${effect.kindNote}`.trim(), children: effect.chip }))] }, `box:${joinId(join)}`));
|
|
684
|
+
}), tables.length === 0 && _jsx("p", { className: "sf-model-empty", children: "Add a table above, or drag one here from the browser." })] })] }), tables.length > 0 && (_jsxs("p", { className: "sf-model-legend", children: [_jsx("abbr", { className: "sf-model-key", "data-kind": "pk", children: "PK" }), " primary key ", _jsx("abbr", { className: "sf-model-key", "data-kind": "fk", children: "FK" }), " refers to another table. Point at a column for its description."] })), _jsx("p", { className: "sf-model-status", "aria-live": "polite", children: status }), problem && (_jsxs("p", { className: "sf-model-problem", role: "alert", children: [problem, " ", _jsx("button", { type: "button", className: "sf-button", onClick: () => setProblem(""), children: "Dismiss" })] })), _jsxs("section", { className: "sf-model-joins", "aria-label": "Joins", children: [rowsFrom && tables.length > 1 && (_jsxs("label", { className: "sf-model-base", children: [_jsx("span", { "aria-hidden": "true", children: "Rows come from" }), _jsx("select", { className: "sf-input", "aria-label": "Rows come from", value: base, onChange: (e) => makeBase(e.target.value), children: tables.map((t) => (_jsx("option", { children: t }, t))) }), _jsxs("span", { className: "sf-object-muted", children: ["One row of the result is one ", base, " row; the other tables add detail to it."] })] })), _jsx("h4", { className: "sf-object-label", children: "Joins" }), joins.filter(present).length === 0 ? (_jsx("p", { className: "sf-object-muted", children: "None yet. Drag a column onto a column of another table, or add one below." })) : (_jsx("ul", { children: joins.filter(present).map((join) => {
|
|
685
|
+
const effect = effectOf(join);
|
|
686
|
+
return (_jsxs("li", { "data-shape": rowsFrom ? effect.shape : undefined, children: [_jsx("code", { children: joinText(effect.read) }), join.inferred && _jsx("span", { className: "sf-schema-tag", children: "Inferred" }), join.declared && !join.inferred && _jsx("span", { className: "sf-schema-tag", children: "Declared" }), kindSelect(join, effect.read, "sf-input sf-model-kind-field"), rowsFrom && !broken.has(joinId(join)) && (_jsxs("span", { className: "sf-model-effect", children: [effect.shape === "many" && _jsx("span", { "aria-hidden": "true", children: "\u25B2 " }), effect.shapeText, " ", effect.kindNote, join.inferred && " Not in the query until confirmed.", effect.error && _jsxs("span", { role: "alert", children: [" The engine could not count: ", effect.error] })] })), rowsFrom && !broken.has(joinId(join)) && effect.shape !== "checking" && (effect.shape === "unknown" || effect.shape === "many" || effect.error) && (_jsx("button", { type: "button", className: "sf-button", onClick: () => checkRows(join, effect.near), children: "Check with the engine" })), broken.has(joinId(join)) && _jsxs("span", { className: "sf-model-broken", children: ["\u25B2 Will not work, left out of the SQL. ", broken.get(joinId(join))] }), join.inferred && !broken.has(joinId(join)) && (_jsx("button", { type: "button", className: "sf-button", onClick: () => addJoin(join), children: "Confirm" })), _jsx("button", { type: "button", className: "sf-button", "aria-label": `Remove join ${joinText(join)}`, onClick: () => {
|
|
687
|
+
setJoins((previous) => previous.filter((j) => joinId(j) !== joinId(join)));
|
|
688
|
+
setProblem("");
|
|
689
|
+
setStatus("Removed the join.");
|
|
690
|
+
}, children: "Remove" })] }, joinId(join)));
|
|
691
|
+
}) })), tables.length > 1 && (_jsxs("form", { className: "sf-model-add", onSubmit: (event) => {
|
|
692
|
+
event.preventDefault();
|
|
693
|
+
if (!draft.left || !draft.leftColumn || !draft.right || !draft.rightColumn)
|
|
694
|
+
return setStatus("Choose two tables and a column in each.");
|
|
695
|
+
addJoin({ left: draft.left, right: draft.right, keys: [{ leftColumn: draft.leftColumn, rightColumn: draft.rightColumn }] });
|
|
696
|
+
setDraft({ left: "", leftColumn: "", right: "", rightColumn: "" });
|
|
697
|
+
}, children: [pick("Table", draft.left, tables, (left) => setDraft({ ...draft, left, leftColumn: "" })), pick("Column", draft.leftColumn, joinable(draft.left, "unknown"), (leftColumn) => setDraft({ ...draft, leftColumn, rightColumn: "" })), pick("Joins table", draft.right, tables.filter((t) => t !== draft.left), (right) => setDraft({ ...draft, right, rightColumn: "" })), pick("On column", draft.rightColumn, joinable(draft.right, draftFamily), (rightColumn) => setDraft({ ...draft, rightColumn })), _jsx("button", { type: "submit", className: "sf-button", children: "Add join" })] }))] }), generated.sql && (_jsxs("details", { className: "sf-result-sql", children: [_jsx("summary", { children: "SQL for this model" }), _jsx("pre", { tabIndex: 0, children: generated.sql }), generated.unjoined.length > 0 && _jsxs("p", { className: "sf-result-notice", children: ["Not joined to the rest, so left out: ", generated.unjoined.join(", "), "."] }), unreadable.length > 0 && _jsxs("p", { className: "sf-result-notice", children: ["Could not be read, so left out: ", unreadable.join(", "), "."] }), broken.size > 0 && _jsxs("p", { className: "sf-result-notice", children: [broken.size === 1 ? "One join" : `${broken.size} joins`, " cannot work and ", broken.size === 1 ? "is" : "are", " left out. See the list above."] })] }))] }));
|
|
698
|
+
}
|
|
699
|
+
//# sourceMappingURL=ModelDiagram.js.map
|