Skip to content

Commit

Permalink
fix(utils): convertNamedPlaceholders requires parameters
Browse files Browse the repository at this point in the history
Resolves unexpected behaviour when resources send concatenated queries containing : and @

i.e. esx_status case query
  • Loading branch information
thelindat committed Dec 28, 2021
1 parent 47a33bb commit 64fd624
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ game 'common'

name 'oxmysql'
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
version '1.9.1'
version '1.9.2'
url 'https://github.com/overextended/oxmysql'
author 'overextended'

Expand Down
39 changes: 23 additions & 16 deletions src/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ export let serverReady = async () => {
export const parseArguments = (invokingResource, query, parameters, cb) => {
if (typeof query !== 'string') throw new Error(`Query expected a string but received ${typeof query} instead`);

const queryParams = query.match(/\?(?!\?)/g);

if (query.includes(':') || query.includes('@')) {
if (
parameters &&
typeof parameters === 'object' &&
!Array.isArray(parameters) &&
(query.includes(':') || query.includes('@'))
) {
const placeholders = convertNamedPlaceholders(query, parameters);
query = placeholders[0];
parameters = placeholders[1];
Expand All @@ -57,19 +60,23 @@ export const parseArguments = (invokingResource, query, parameters, cb) => {
arr[key - 1] = value;
});
parameters = arr;
} else if (queryParams !== null) {
if (parameters.length === 0) {
for (let i = 0; i < queryParams.length; i++) parameters[i] = null;
return [query, parameters, cb];
}
const diff = queryParams.length - parameters.length;

if (diff > 0) {
for (let i = 0; i < diff; i++) parameters[queryParams.length + i] = null;
} else if (diff < 0) {
throw new Error(`${invokingResource} was unable to execute a query!
Expected ${queryParams.length} parameters, but received ${parameters.length}.
${`${query} ${JSON.stringify(parameters)}`}`);
} else {
const queryParams = query.match(/\?(?!\?)/g);

if (queryParams !== null) {
if (parameters.length === 0) {
for (let i = 0; i < queryParams.length; i++) parameters[i] = null;
return [query, parameters, cb];
}
const diff = queryParams.length - parameters.length;

if (diff > 0) {
for (let i = 0; i < diff; i++) parameters[queryParams.length + i] = null;
} else if (diff < 0) {
throw new Error(`${invokingResource} was unable to execute a query!
Expected ${queryParams.length} parameters, but received ${parameters.length}.
${`${query} ${JSON.stringify(parameters)}`}`);
}
}
}

Expand Down

0 comments on commit 64fd624

Please sign in to comment.