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,6 +639,8 @@ export default function Table(props) {
/> />
</> </>
)} )}
{hasCheck(f.type) && (
<>
<div className="font-semibold">Check Expression</div> <div className="font-semibold">Check Expression</div>
<Input <Input
className="my-2" className="my-2"
@ -640,9 +648,13 @@ export default function Table(props) {
value={f.check} value={f.check}
disabled={f.increment} disabled={f.increment}
onChange={(value) => onChange={(value) =>
updateField(props.tableData.id, j, { check: value }) updateField(props.tableData.id, j, {
check: value,
})
}
onFocus={(e) =>
setEditField({ check: e.target.value })
} }
onFocus={(e) => setEditField({ check: e.target.value })}
onBlur={(e) => { onBlur={(e) => {
if (e.target.value === editField.check) return; if (e.target.value === editField.check) return;
setUndoStack((prev) => [ setUndoStack((prev) => [
@ -655,12 +667,17 @@ export default function Table(props) {
fid: j, fid: j,
undo: editField, undo: editField,
redo: { check: e.target.value }, redo: { check: e.target.value },
message: `Edit table field check expression to "${e.target.value}"`, message: `Edit table field check expression to ${e.target.value}`,
}, },
]); ]);
setRedoStack([]); 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,11 +484,13 @@ export default function TableOverview(props) {
/> />
</> </>
)} )}
{hasCheck(f.type) && (
<>
<div className="font-semibold"> <div className="font-semibold">
Check Expression Check Expression
</div> </div>
<Input <Input
className="my-2" className="mt-2"
placeholder="Set constraint" placeholder="Set constraint"
value={f.check} value={f.check}
disabled={f.increment} disabled={f.increment}
@ -493,7 +501,8 @@ export default function TableOverview(props) {
setEditField({ check: e.target.value }) setEditField({ check: e.target.value })
} }
onBlur={(e) => { onBlur={(e) => {
if (e.target.value === editField.check) return; if (e.target.value === editField.check)
return;
setUndoStack((prev) => [ setUndoStack((prev) => [
...prev, ...prev,
{ {
@ -510,6 +519,11 @@ export default function TableOverview(props) {
setRedoStack([]); 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,
}; };