fix(execute): Parse parameters as intended

This commit is contained in:
dunak-debug committed 2022-01-04 16:10:27 +01:00
1 parent 0e2d2b7551
commit 3a1462e0c0
5 files changed
+166 -131

No files matched your search

+21 -10
View File
@@ -2,7 +2,7 @@ import { QueryError } from 'mysql2';
import { pool } from '.';
import { scheduleTick } from '../config';
import { logQuery } from '../logger';
import { CFXCallback, CFXParameters } from '../types';
import { CFXCallback, CFXParameters, QueryResponse } from '../types';
import { parseExecute } from '../utils/parseExecute';
export const rawExecute = async (
@@ -15,26 +15,35 @@ export const rawExecute = async (
if (!type) throw new Error(`Prepared statements only accept SELECT, INSERT, UPDATE, and DELETE methods!`);
if (!Array.isArray(parameters))
throw new Error(`Parameters expected an array but received ${typeof parameters} instead`);
scheduleTick();
const connection = await pool.getConnection();
let result: QueryResponse;
try {
const results = [];
const executionTime = process.hrtime();
if (Array.isArray(parameters)) for (const param of parameters) results.push(await connection.execute(query, param));
else results.push(await connection.execute(query, parameters));
if (!parameters.every(Array.isArray)) parameters = [[...parameters]];
logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, parameters);
const results = [];
// TODO: dont look at me taso
let result;
for (const params of parameters) {
results.push((await connection.execute(query, params))[0]);
logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, params as typeof parameters);
}
result = results;
//TODO any giggle
if (results.length === 1) {
if (type === 'execute') {
if (results[0][0] && Object.keys(results[0][0]).length === 1) result = Object.values(results[0][0])[0];
else result = results[0][0];
if ((results as any)[0][0] && Object.keys((results as any)[0][0]).length === 1)
result = Object.values((results as any)[0][0])[0] as any;
else result = (results as any)[0][0];
} else {
result = results[0];
}
@@ -42,7 +51,7 @@ export const rawExecute = async (
result = results;
}
return cb ? cb(result) : result;
if (!cb) return result;
} catch (e) {
throw new Error(`${invokingResource} was unable to execute a query!
${(e as QueryError).message}
@@ -50,4 +59,6 @@ export const rawExecute = async (
} finally {
connection.release();
}
if (cb) cb(result);
};