fix SQL export for MySQL specific date format;

fix PublisherForm
This commit is contained in:
Dobromir Popov
2024-04-30 15:03:08 +03:00
parent e6883428c2
commit 8aac5231ef
2 changed files with 11 additions and 7 deletions

View File

@ -18,28 +18,33 @@ const parseQueryParams = (query: any) => {
};
// Helper function to escape and quote strings, handle nulls and other types
const serializeValue = (value) => {
if (value === null || value === undefined) {
return 'NULL';
}
if (typeof value === 'string') {
return `'${value.replace(/'/g, "''")}'`; // escape single quotes in SQL string
// Escape single quotes and backslashes for MySQL
return `'${value.replace(/\\/g, "\\\\").replace(/'/g, "''")}'`;
}
if (typeof value === 'boolean') {
return value ? 'true' : 'false';
// MySQL uses 1 and 0 for TRUE and FALSE
return value ? '1' : '0';
}
if (typeof value === 'number') {
return value;
}
if (value instanceof Date) {
// Format date objects to MySQL date strings
return `'${value.toISOString().slice(0, 19).replace('T', ' ')}'`;
}
if (Array.isArray(value) || typeof value === 'object') {
// Convert arrays and objects to JSON strings and escape them
return `'${JSON.stringify(value).replace(/'/g, "''")}'`;
return `'${JSON.stringify(value).replace(/\\/g, "\\\\").replace(/'/g, "''")}'`;
}
return value;
};
// Function to generate SQL INSERT statements from data
// Function to generate SQL INSERT statements for MySQL from data
const generateSQL = (data, tableName) => {
return data.map(item => {
const columns = Object.keys(item).join(", ");
@ -47,7 +52,6 @@ const generateSQL = (data, tableName) => {
return `INSERT INTO ${tableName} (${columns}) VALUES (${values});`;
}).join("\n");
};
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const prisma: PrismaClient = common.getPrismaClient();
const modelArray = (req.query.model || (req.body && req.body.model)) as string[];