Handle check expressions

This commit is contained in:
1ilit 2023-09-19 15:51:25 +03:00
parent 028e80a487
commit 69aabaefa9
3 changed files with 118 additions and 67 deletions

View File

@ -41,7 +41,7 @@ import {
TableContext, TableContext,
UndoRedoContext, UndoRedoContext,
} from "../pages/editor"; } from "../pages/editor";
import { getSize, hasPrecision, isSized } from "../utils"; import { getSize, hasCheck, hasPrecision, isSized } from "../utils";
export default function Table(props) { export default function Table(props) {
const [isHovered, setIsHovered] = useState(false); const [isHovered, setIsHovered] = useState(false);
@ -405,6 +405,12 @@ export default function Table(props) {
size: "", size: "",
values: [], values: [],
}); });
} else if (hasCheck(value)) {
updateField(props.tableData.id, j, {
type: value,
check: "",
increment: incr,
});
} else { } else {
updateField(props.tableData.id, j, { updateField(props.tableData.id, j, {
type: value, type: value,
@ -633,34 +639,45 @@ export default function Table(props) {
/> />
</> </>
)} )}
<div className="font-semibold">Check Expression</div> {hasCheck(f.type) && (
<Input <>
className="my-2" <div className="font-semibold">Check Expression</div>
placeholder="Set constraint" <Input
value={f.check} className="my-2"
disabled={f.increment} placeholder="Set constraint"
onChange={(value) => value={f.check}
updateField(props.tableData.id, j, { check: value }) disabled={f.increment}
} onChange={(value) =>
onFocus={(e) => setEditField({ check: e.target.value })} updateField(props.tableData.id, j, {
onBlur={(e) => { check: value,
if (e.target.value === editField.check) return; })
setUndoStack((prev) => [ }
...prev, onFocus={(e) =>
{ setEditField({ check: e.target.value })
action: Action.EDIT, }
element: ObjectType.TABLE, onBlur={(e) => {
component: "field", if (e.target.value === editField.check) return;
tid: props.tableData.id, setUndoStack((prev) => [
fid: j, ...prev,
undo: editField, {
redo: { check: e.target.value }, action: Action.EDIT,
message: `Edit table field check expression to "${e.target.value}"`, element: ObjectType.TABLE,
}, component: "field",
]); tid: props.tableData.id,
setRedoStack([]); 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="flex justify-between items-center my-3">
<div className="font-medium">Unique</div> <div className="font-medium">Unique</div>
<Checkbox <Checkbox
@ -732,8 +749,8 @@ export default function Table(props) {
]); ]);
setRedoStack([]); setRedoStack([]);
updateField(props.tableData.id, j, { updateField(props.tableData.id, j, {
[checkedValues.target.value]: increment: !f.increment,
checkedValues.target.checked, check: f.increment ? f.check : "",
}); });
}} }}
></Checkbox> ></Checkbox>

View File

@ -36,7 +36,7 @@ import {
IllustrationNoContentDark, IllustrationNoContentDark,
} from "@douyinfe/semi-illustrations"; } from "@douyinfe/semi-illustrations";
import { SelectContext, TableContext, UndoRedoContext } from "../pages/editor"; 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) { export default function TableOverview(props) {
const [indexActiveKey, setIndexActiveKey] = useState(""); const [indexActiveKey, setIndexActiveKey] = useState("");
@ -251,6 +251,12 @@ export default function TableOverview(props) {
size: "", size: "",
values: [], values: [],
}); });
} else if (hasCheck(value)) {
updateField(i, j, {
type: value,
check: "",
increment: incr,
});
} else { } else {
updateField(i, j, { updateField(i, j, {
type: value, type: value,
@ -478,38 +484,46 @@ export default function TableOverview(props) {
/> />
</> </>
)} )}
<div className="font-semibold"> {hasCheck(f.type) && (
Check Expression <>
</div> <div className="font-semibold">
<Input Check Expression
className="my-2" </div>
placeholder="Set constraint" <Input
value={f.check} className="mt-2"
disabled={f.increment} placeholder="Set constraint"
onChange={(value) => value={f.check}
updateField(i, j, { check: value }) disabled={f.increment}
} onChange={(value) =>
onFocus={(e) => updateField(i, j, { check: value })
setEditField({ check: e.target.value }) }
} onFocus={(e) =>
onBlur={(e) => { setEditField({ check: e.target.value })
if (e.target.value === editField.check) return; }
setUndoStack((prev) => [ onBlur={(e) => {
...prev, if (e.target.value === editField.check)
{ return;
action: Action.EDIT, setUndoStack((prev) => [
element: ObjectType.TABLE, ...prev,
component: "field", {
tid: i, action: Action.EDIT,
fid: j, element: ObjectType.TABLE,
undo: editField, component: "field",
redo: { check: e.target.value }, tid: i,
message: `Edit table field check expression to ${e.target.value}`, fid: j,
}, undo: editField,
]); redo: { check: e.target.value },
setRedoStack([]); 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="flex justify-between items-center my-3">
<div className="font-medium">Unique</div> <div className="font-medium">Unique</div>
<Checkbox <Checkbox
@ -578,8 +592,8 @@ export default function TableOverview(props) {
]); ]);
setRedoStack([]); setRedoStack([]);
updateField(i, j, { updateField(i, j, {
[checkedValues.target.value]: increment: !f.increment,
checkedValues.target.checked, check: f.increment ? f.check : "",
}); });
}} }}
></Checkbox> ></Checkbox>

View File

@ -99,7 +99,11 @@ function jsonToSQL(obj) {
: `${field.default}` : `${field.default}`
}` }`
: "" : ""
}${field.check === "" ? "" : ` CHECK (${field.check})`}` }${
field.check === "" || !hasCheck(field.type)
? ""
: ` CHECK(${field.check})`
}`
) )
.join(",\n")}${ .join(",\n")}${
table.fields.filter((f) => f.primary).length > 0 table.fields.filter((f) => f.primary).length > 0
@ -149,6 +153,21 @@ function hasPrecision(type) {
return ["DOUBLE", "NUMERIC", "DECIMAL"].includes(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) { function getSize(type) {
switch (type) { switch (type) {
case "CHAR": case "CHAR":
@ -371,4 +390,5 @@ export {
getSize, getSize,
hasPrecision, hasPrecision,
validateDateStr, validateDateStr,
hasCheck,
}; };