From fa1cdcc66818ffab419b2a408becd12bf9746ce0 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sun, 3 Apr 2022 03:52:07 +1100 Subject: [PATCH] fix(execute): incorrect response parsing Fixes several regressions and issues from ef0dfaa. Sending a single parameter would set 'single' to true, ignoring multiple results from the database and returning 'null'. SELECT should not trigger transactions, not the other way around. Results should not be parsed when no callback is defined. --- fxmanifest.lua | 2 +- package.json | 2 +- src/database/rawExecute.ts | 21 ++++++++++----------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/fxmanifest.lua b/fxmanifest.lua index 90d5cf2..8b29f1f 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -3,7 +3,7 @@ game 'common' name 'oxmysql' description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.' -version '2.2.3' +version '2.2.4' url 'https://github.com/overextended/oxmysql' author 'overextended' diff --git a/package.json b/package.json index ab423d6..5d136c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oxmysql", - "version": "2.2.3", + "version": "2.2.4", "description": "FXServer to MySQL communication via node-mysql2", "repository": "git@github.com:overextended/oxmysql.git", "author": "dunak-debug <19434539+dunak-debug@users.noreply.github.com>", diff --git a/src/database/rawExecute.ts b/src/database/rawExecute.ts index b47c021..cc0590c 100644 --- a/src/database/rawExecute.ts +++ b/src/database/rawExecute.ts @@ -14,13 +14,9 @@ export const rawExecute = async ( ) => { const type = executeType(query); parameters = parseExecute(parameters); - let single: boolean; - if (!parameters.every(Array.isArray)) { - single = true; - parameters = [[...parameters]]; - } + if (!parameters.every(Array.isArray)) parameters = [[...parameters]]; const length = parameters.length - 1; @@ -41,15 +37,18 @@ export const rawExecute = async ( connection.execute(query, values, (err, results: RowDataPacket[][]) => { if (err) return reject([connection, err]); - if (results.length > 1) { - for (const value of results) { - response.push(parseResponse(type, value)); - } - } else response.push(parseResponse(type, results)); + if (cb) { + if (results.length > 1) { + for (const value of results) { + response.push(parseResponse(type, value)); + } + } else response.push(parseResponse(type, results)); + } logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, values as typeof parameters); if (index === length) { + single = response.length === 1; if (!single && type !== null) connection.commit(); connection.release(); @@ -59,7 +58,7 @@ export const rawExecute = async ( }); }; - if (single || type !== null) { + if (type === null) { execute(); } else connection.beginTransaction((err) => {