Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

Handle check expressions

1ilit69aabaefa959
diff --git a/src/components/table.jsx b/src/components/table.jsx
index 293bd196..347b1d50 100644
--- a/src/components/table.jsx
+++ b/src/components/table.jsx
@@ -41,7 +41,7 @@ import {
TableContext,
UndoRedoContext,
} from "../pages/editor";
-import { getSize, hasPrecision, isSized } from "../utils";
+import { getSize, hasCheck, hasPrecision, isSized } from "../utils";
export default function Table(props) {
const [isHovered, setIsHovered] = useState(false);
@@ -405,6 +405,12 @@ export default function Table(props) {
size: "",
values: [],
});
+ } else if (hasCheck(value)) {
+ updateField(props.tableData.id, j, {
+ type: value,
+ check: "",
+ increment: incr,
+ });
} else {
updateField(props.tableData.id, j, {
type: value,
@@ -633,34 +639,45 @@ export default function Table(props) {
/>
</>
)}
- <div className="font-semibold">Check Expression</div>
- <Input
- className="my-2"
- placeholder="Set constraint"
- value={f.check}
- disabled={f.increment}
- onChange={(value) =>
- updateField(props.tableData.id, j, { check: value })
- }
- onFocus={(e) => setEditField({ check: e.target.value })}
- onBlur={(e) => {
- if (e.target.value === editField.check) return;
- setUndoStack((prev) => [
- ...prev,
- {
- action: Action.EDIT,
- element: ObjectType.TABLE,
- component: "field",
- tid: props.tableData.id,
- fid: j,
- undo: editField,
- redo: { check: e.target.value },
- message: `Edit table field check expression to "${e.target.value}"`,
- },
- ]);
- setRedoStack([]);
- }}
- />
+ {hasCheck(f.type) && (
+ <>
+ <div className="font-semibold">Check Expression</div>
+ <Input
+ className="my-2"
+ placeholder="Set constraint"
+ value={f.check}
+ disabled={f.increment}
+ onChange={(value) =>
+ updateField(props.tableData.id, j, {
+ check: value,
+ })
+ }
+ onFocus={(e) =>
+ setEditField({ check: e.target.value })
+ }
+ onBlur={(e) => {
+ if (e.target.value === editField.check) return;
+ setUndoStack((prev) => [
+ ...prev,
+ {
+ action: Action.EDIT,
+ element: ObjectType.TABLE,
+ component: "field",
+ tid: props.tableData.id,
+ fid: j,
+ undo: editField,
+ redo: { check: e.target.value },
+ message: `Edit table field check expression to ${e.target.value}`,
+ },
+ ]);
+ setRedoStack([]);
+ }}
+ />
+ <div className="text-xs mt-1">
+ *This will be in the script as is.
+ </div>
+ </>
+ )}
<div className="flex justify-between items-center my-3">
<div className="font-medium">Unique</div>
<Checkbox
@@ -732,8 +749,8 @@ export default function Table(props) {
]);
setRedoStack([]);
updateField(props.tableData.id, j, {
- [checkedValues.target.value]:
- checkedValues.target.checked,
+ increment: !f.increment,
+ check: f.increment ? f.check : "",
});
}}
></Checkbox>
diff --git a/src/components/table_overview.jsx b/src/components/table_overview.jsx
index c204b43a..08ee4e8a 100644
--- a/src/components/table_overview.jsx
+++ b/src/components/table_overview.jsx
@@ -36,7 +36,7 @@ import {
IllustrationNoContentDark,
} from "@douyinfe/semi-illustrations";
import { SelectContext, TableContext, UndoRedoContext } from "../pages/editor";
-import { getSize, hasPrecision, isSized } from "../utils";
+import { getSize, hasCheck, hasPrecision, isSized } from "../utils";
export default function TableOverview(props) {
const [indexActiveKey, setIndexActiveKey] = useState("");
@@ -251,6 +251,12 @@ export default function TableOverview(props) {
size: "",
values: [],
});
+ } else if (hasCheck(value)) {
+ updateField(i, j, {
+ type: value,
+ check: "",
+ increment: incr,
+ });
} else {
updateField(i, j, {
type: value,
@@ -478,38 +484,46 @@ export default function TableOverview(props) {
/>
</>
)}
- <div className="font-semibold">
- Check Expression
- </div>
- <Input
- className="my-2"
- placeholder="Set constraint"
- value={f.check}
- disabled={f.increment}
- onChange={(value) =>
- updateField(i, j, { check: value })
- }
- onFocus={(e) =>
- setEditField({ check: e.target.value })
- }
- onBlur={(e) => {
- if (e.target.value === editField.check) return;
- setUndoStack((prev) => [
- ...prev,
- {
- action: Action.EDIT,
- element: ObjectType.TABLE,
- component: "field",
- tid: i,
- fid: j,
- undo: editField,
- redo: { check: e.target.value },
- message: `Edit table field check expression to ${e.target.value}`,
- },
- ]);
- setRedoStack([]);
- }}
- />
+ {hasCheck(f.type) && (
+ <>
+ <div className="font-semibold">
+ Check Expression
+ </div>
+ <Input
+ className="mt-2"
+ placeholder="Set constraint"
+ value={f.check}
+ disabled={f.increment}
+ onChange={(value) =>
+ updateField(i, j, { check: value })
+ }
+ onFocus={(e) =>
+ setEditField({ check: e.target.value })
+ }
+ onBlur={(e) => {
+ if (e.target.value === editField.check)
+ return;
+ setUndoStack((prev) => [
+ ...prev,
+ {
+ action: Action.EDIT,
+ element: ObjectType.TABLE,
+ component: "field",
+ tid: i,
+ fid: j,
+ undo: editField,
+ redo: { check: e.target.value },
+ message: `Edit table field check expression to ${e.target.value}`,
+ },
+ ]);
+ setRedoStack([]);
+ }}
+ />
+ <div className="text-xs mt-1">
+ *This will be in the script as is.
+ </div>
+ </>
+ )}
<div className="flex justify-between items-center my-3">
<div className="font-medium">Unique</div>
<Checkbox
@@ -578,8 +592,8 @@ export default function TableOverview(props) {
]);
setRedoStack([]);
updateField(i, j, {
- [checkedValues.target.value]:
- checkedValues.target.checked,
+ increment: !f.increment,
+ check: f.increment ? f.check : "",
});
}}
></Checkbox>
diff --git a/src/utils/index.js b/src/utils/index.js
index 6f5a7d3d..eb35dfbc 100644
--- a/src/utils/index.js
+++ b/src/utils/index.js
@@ -99,7 +99,11 @@ function jsonToSQL(obj) {
: `${field.default}`
}`
: ""
- }${field.check === "" ? "" : ` CHECK (${field.check})`}`
+ }${
+ field.check === "" || !hasCheck(field.type)
+ ? ""
+ : ` CHECK(${field.check})`
+ }`
)
.join(",\n")}${
table.fields.filter((f) => f.primary).length > 0
@@ -149,6 +153,21 @@ function hasPrecision(type) {
return ["DOUBLE", "NUMERIC", "DECIMAL"].includes(type);
}
+function hasCheck(type) {
+ return [
+ "INT",
+ "SMALLINT",
+ "BIGINT",
+ "CHAR",
+ "VARCHAR",
+ "FLOAT",
+ "DECIMAL",
+ "DOUBLE",
+ "NUMERIC",
+ "REAL",
+ ].includes(type);
+}
+
function getSize(type) {
switch (type) {
case "CHAR":
@@ -371,4 +390,5 @@ export {
getSize,
hasPrecision,
validateDateStr,
+ hasCheck,
};