Skip to content

Commit

Permalink
fix #368 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeparticle committed Nov 3, 2023
1 parent 9f1c208 commit 7516c07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
42 changes: 28 additions & 14 deletions ui/src/pages/Generator/Data/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FAKER_DATA_TYPES,
JSON_DATA_TYPES,
MYSQL_DATA_TYPES,
fakerMethod,
} from "./utils/constants";
import { convertToJSON } from "./utils/utils";
import Output from "./components/Output";
Expand Down Expand Up @@ -53,8 +54,7 @@ const DataGenerator: React.FC = () => {

const onButtonClick = () => {
let result = "";
const allColName = colNames.join("`, `");
const fakeDataMethods = [];
const fakeDataMethods: fakerMethod[] = [];
let sqlTable = `CREATE TABLE \`${tableName}\` (\n`;

for (let i = 0; i < colNames.length; i++) {
Expand All @@ -64,24 +64,38 @@ const DataGenerator: React.FC = () => {
sqlTable += `) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;`;

for (let j = 0; j < colNames.length; j++) {
fakeDataMethods.push(
FAKER_DATA_TYPES.find((faker) => {
return faker.value === fakeDataTypes[j];
})?.method
);
const method = FAKER_DATA_TYPES.find(
(faker) => faker.value === fakeDataTypes[j]
)?.method;
if (method === undefined) {
throw new Error(
`No faker method found for type ${fakeDataTypes[j]}`
);
}
fakeDataMethods.push(method);
}

const insertStatements = [];

for (let i = 0; i < rowNum; i++) {
const fakeData = [];
for (let j = 0; j < colNames.length; j++) {
fakeData.push(fakeDataMethods[j]?.());
}
const fakeData = colNames.map((_, j) => {
let value = fakeDataMethods[j]?.();
if (typeof value === "string") {
value = value.replace(/'/g, "''");
}
return typeof value === "string" ? `'${value}'` : value;
});

result += `INSERT INTO \`${tableName}\` (\`${allColName}\`) VALUES ('${fakeData.join(
"', '"
)}');\n`;
const valuesString = fakeData.join(", ");
insertStatements.push(
`INSERT INTO \`${tableName}\` (\`${colNames.join(
"`, `"
)}\`) VALUES (${valuesString});`
);
}

result += insertStatements.join("\n") + "\n";

setResult(`${sqlTable}\n\n\n\n\n${result}`);
};

Expand Down
10 changes: 9 additions & 1 deletion ui/src/pages/Generator/Data/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ export const JSON_DATA_TYPES = [
{ value: "NULL", label: "Null" },
];

export const FAKER_DATA_TYPES = [
export type fakerMethod = () => string | number | boolean;

interface FakerDataType {
value: string;
label: string;
method: fakerMethod;
}

export const FAKER_DATA_TYPES: FakerDataType[] = [
{
value: "buildingNumber",
label: "Building Number",
Expand Down

0 comments on commit 7516c07

Please sign in to comment.