Skip to content

Feat/postgres table inheritance fixed #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/components/EditorSidePanel/TablesTab/TableField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { dbToTypes } from "../../../data/datatypes";
import { DragHandle } from "../../SortableList/DragHandle";
import FieldDetails from "./FieldDetails";

export default function TableField({ data, tid, index }) {
export default function TableField({ data, tid, index, inherited }) {
const { updateField } = useDiagram();
const { types } = useTypes();
const { enums } = useEnums();
Expand All @@ -21,12 +21,15 @@ export default function TableField({ data, tid, index }) {
return (
<div className="hover-1 my-2 flex gap-2 items-center">
<DragHandle id={data.id} />

<div className="min-w-20 flex-1/3">
<Input
value={data.name}
id={`scroll_table_${tid}_input_${index}`}
validateStatus={data.name.trim() === "" ? "error" : "default"}
placeholder="Name"
validateStatus={
data.name.trim() === "" || inherited ? "error" : "default"
}
placeholder={t("name")}
onChange={(value) => updateField(tid, data.id, { name: value })}
onFocus={(e) => setEditField({ name: e.target.value })}
onBlur={(e) => {
Expand All @@ -51,13 +54,14 @@ export default function TableField({ data, tid, index }) {
}}
/>
</div>

<div className="min-w-24 flex-1/3">
<Select
className="w-full"
optionList={[
...Object.keys(dbToTypes[database]).map((value) => ({
label: value,
value: value,
value,
})),
...types.map((type) => ({
label: type.name.toUpperCase(),
Expand All @@ -71,7 +75,7 @@ export default function TableField({ data, tid, index }) {
filter
value={data.type}
validateStatus={data.type === "" ? "error" : "default"}
placeholder="Type"
placeholder={t("type")}
onChange={(value) => {
if (value === data.type) return;
setUndoStack((prev) => [
Expand Down Expand Up @@ -135,6 +139,7 @@ export default function TableField({ data, tid, index }) {
}}
/>
</div>

<div>
<Button
type={data.notNull ? "tertiary" : "primary"}
Expand Down Expand Up @@ -164,11 +169,13 @@ export default function TableField({ data, tid, index }) {
?
</Button>
</div>

<div>
<Button
type={data.primary ? "primary" : "tertiary"}
title={t("primary")}
theme={data.primary ? "solid" : "light"}
icon={<IconKeyStroked />}
onClick={() => {
setUndoStack((prev) => [
...prev,
Expand All @@ -189,9 +196,9 @@ export default function TableField({ data, tid, index }) {
setRedoStack([]);
updateField(tid, data.id, { primary: !data.primary });
}}
icon={<IconKeyStroked />}
/>
</div>

<div>
<Popover
content={
Expand Down
83 changes: 73 additions & 10 deletions src/components/EditorSidePanel/TablesTab/TableInfo.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { useState, useRef } from "react";
import { Collapse, Input, TextArea, Button, Card } from "@douyinfe/semi-ui";
import {
Collapse,
Input,
TextArea,
Button,
Card,
Select,
} from "@douyinfe/semi-ui";
import ColorPicker from "../ColorPicker";
import { IconDeleteStroked } from "@douyinfe/semi-icons";
import { useDiagram, useSaveState, useUndoRedo } from "../../../hooks";
import { Action, ObjectType, State } from "../../../data/constants";
import { Action, ObjectType, State, DB } from "../../../data/constants";
import TableField from "./TableField";
import IndexDetails from "./IndexDetails";
import { useTranslation } from "react-i18next";
import { SortableList } from "../../SortableList/SortableList";
import { nanoid } from "nanoid";

export default function TableInfo({ data }) {
const { tables, database } = useDiagram();
const { t } = useTranslation();
const [indexActiveKey, setIndexActiveKey] = useState("");
const { deleteTable, updateTable, setTables } = useDiagram();
Expand Down Expand Up @@ -56,10 +64,20 @@ export default function TableInfo({ data }) {
};
undefined;

const inheritedFieldNames =
Array.isArray(data.inherits) && data.inherits.length > 0
? data.inherits
.map((parentName) => {
const parent = tables.find((t) => t.name === parentName);
return parent ? parent.fields.map((f) => f.name) : [];
})
.flat()
: [];

return (
<div>
<div className="flex items-center mb-2.5">
<div className="text-md font-semibold break-keep">{t("name")}: </div>
<div className="text-md font-semibold break-keep">{t("name")}:</div>
<Input
value={data.name}
validateStatus={data.name.trim() === "" ? "error" : "default"}
Expand Down Expand Up @@ -88,21 +106,64 @@ export default function TableInfo({ data }) {
}}
/>
</div>

<SortableList
items={data.fields}
keyPrefix={`table-${data.id}`}
onChange={(newFields) => {
setTables((prev) => {
return prev.map((t) =>
onChange={(newFields) =>
setTables((prev) =>
prev.map((t) =>
t.id === data.id ? { ...t, fields: newFields } : t,
);
});
}}
),
)
}
afterChange={() => setSaveState(State.SAVING)}
renderItem={(item, i) => (
<TableField data={item} tid={data.id} index={i} />
<TableField
data={item}
tid={data.id}
index={i}
inherited={inheritedFieldNames.includes(item.name)}
/>
)}
/>

{database === DB.POSTGRES && (
<div className="mb-2">
<div className="text-md font-semibold break-keep">
{t("inherits")}:
</div>
<Select
multiple
value={data.inherits || []}
optionList={tables
.filter((t) => t.id !== data.id)
.map((t) => ({ label: t.name, value: t.name }))}
onChange={(value) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: data.id,
undo: { inherits: data.inherits },
redo: { inherits: value },
message: t("edit_table", {
tableName: data.name,
extra: "[inherits]",
}),
},
]);
setRedoStack([]);
updateTable(data.id, { inherits: value });
}}
placeholder={t("inherits")}
className="w-full"
/>
</div>
)}

{data.indices.length > 0 && (
<Card
bodyStyle={{ padding: "4px" }}
Expand Down Expand Up @@ -133,6 +194,7 @@ export default function TableInfo({ data }) {
</Collapse>
</Card>
)}

<Card
bodyStyle={{ padding: "4px" }}
style={{ marginTop: "12px", marginBottom: "12px" }}
Expand Down Expand Up @@ -173,6 +235,7 @@ export default function TableInfo({ data }) {
</Collapse.Panel>
</Collapse>
</Card>

<div className="flex justify-between items-center gap-1 mb-2">
<ColorPicker
usePopover={true}
Expand Down
4 changes: 4 additions & 0 deletions src/data/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export const tableSchema = {
},
color: { type: "string", pattern: "^#[0-9a-fA-F]{6}$" },
},
inherits: {
type: "array",
items: { type: ["string"] },
},
required: ["id", "name", "x", "y", "fields", "comment", "indices", "color"],
};

Expand Down
2 changes: 2 additions & 0 deletions src/i18n/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const en = {
translation: {
report_bug: "Report a bug",
import: "Import",
inherits: "Inherits",
merging_column_w_inherited_definition: "Column '{{fieldName}}' in table '{{tableName}}' with inherited definition will be merged",
import_from: "Import from",
file: "File",
new: "New",
Expand Down
Loading