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,
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,6 +639,8 @@ export default function Table(props) {
/>
</>
)}
{hasCheck(f.type) && (
<>
<div className="font-semibold">Check Expression</div>
<Input
className="my-2"
@ -640,9 +648,13 @@ export default function Table(props) {
value={f.check}
disabled={f.increment}
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) => {
if (e.target.value === editField.check) return;
setUndoStack((prev) => [
@ -655,12 +667,17 @@ export default function Table(props) {
fid: j,
undo: editField,
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([]);
}}
/>
<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>

View File

@ -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,11 +484,13 @@ export default function TableOverview(props) {
/>
</>
)}
{hasCheck(f.type) && (
<>
<div className="font-semibold">
Check Expression
</div>
<Input
className="my-2"
className="mt-2"
placeholder="Set constraint"
value={f.check}
disabled={f.increment}
@ -493,7 +501,8 @@ export default function TableOverview(props) {
setEditField({ check: e.target.value })
}
onBlur={(e) => {
if (e.target.value === editField.check) return;
if (e.target.value === editField.check)
return;
setUndoStack((prev) => [
...prev,
{
@ -510,6 +519,11 @@ export default function TableOverview(props) {
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>

View File

@ -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,
};