mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 14:38:50 +00:00
fix(execute): handle nil values in nested arrays
Lua tables are msgpacked with the "without_hole" setting. Nested tables being sent with "prepare" failed to account for nil values in tables unpacking into objects.
This commit is contained in:
1 parent
b5190f98e9
commit
25f8feb793
2 files changed
+25
-7
No files matched your search
@@ -14,11 +14,10 @@ export const rawExecute = async (
|
||||
throwError?: boolean
|
||||
) => {
|
||||
const type = executeType(query);
|
||||
parameters = parseExecute(parameters);
|
||||
const placeholders = query.split('?').length - 1;
|
||||
parameters = parseExecute(placeholders, parameters);
|
||||
let response = [] as any;
|
||||
|
||||
if (!parameters.every(Array.isArray)) parameters = [[...parameters]];
|
||||
|
||||
await scheduleTick();
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
@@ -26,11 +25,8 @@ export const rawExecute = async (
|
||||
if (err) return reject(err.message);
|
||||
if (parameters.length === 0) return reject(`Query received no parameters.`);
|
||||
|
||||
const placeholders = query.split('?').length - 1;
|
||||
|
||||
parameters.forEach((values, index) => {
|
||||
const executionTime = process.hrtime();
|
||||
values = parseValues(placeholders, values);
|
||||
|
||||
connection.execute(query, values, (err, results: RowDataPacket[][]) => {
|
||||
if (err) {
|
||||
|
||||
@@ -15,7 +15,7 @@ export const executeType = (query: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const parseExecute = (parameters: CFXParameters) => {
|
||||
export const parseExecute = (placeholders: number, parameters: CFXParameters) => {
|
||||
if (!Array.isArray(parameters)) {
|
||||
if (typeof parameters === 'object') {
|
||||
const arr: unknown[] = [];
|
||||
@@ -24,6 +24,28 @@ export const parseExecute = (parameters: CFXParameters) => {
|
||||
} else throw new Error(`Parameters expected an array but received ${typeof parameters} instead`);
|
||||
}
|
||||
|
||||
if (!parameters.every(Array.isArray)) {
|
||||
if (parameters.every((item) => typeof item === 'object')) {
|
||||
const arr: unknown[][] = [];
|
||||
|
||||
parameters.forEach((value, index) => {
|
||||
arr[index] = new Array(placeholders);
|
||||
|
||||
if (!Array.isArray(value)) {
|
||||
Object.entries(value).forEach((entry) => {
|
||||
arr[index][parseInt(entry[0]) - 1] = entry[1];
|
||||
});
|
||||
} else arr[index] = parameters[index];
|
||||
|
||||
for (let i = 0; i < placeholders; i++) {
|
||||
if (!arr[index][i]) arr[index][i] = null;
|
||||
}
|
||||
|
||||
parameters = arr;
|
||||
});
|
||||
} else parameters = [[...parameters]];
|
||||
}
|
||||
|
||||
return parameters;
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user