mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-04 14:38:50 +00:00
fix(execute): revert to promise api
Several reports of increased chances to hit a deadlock since v2.2.2, and three reports of a timeout during prepare since v2.2.4. Simpler to revert to the old prepare function and api (with improvements) than to work out the solution. Look into rewriting prepare in the future or including the mariadb connector, which doesn't suffer from the same perf issues.
This commit is contained in:
1 parent
fa1cdcc668
commit
050eef58b9
4 files changed
+21
-41
No files matched your search
+1
-1
@@ -3,7 +3,7 @@ game 'common'
|
|||||||
|
|
||||||
name 'oxmysql'
|
name 'oxmysql'
|
||||||
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
||||||
version '2.2.4'
|
version '2.2.5'
|
||||||
url 'https://github.com/overextended/oxmysql'
|
url 'https://github.com/overextended/oxmysql'
|
||||||
author 'overextended'
|
author 'overextended'
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "oxmysql",
|
"name": "oxmysql",
|
||||||
"version": "2.2.4",
|
"version": "2.2.5",
|
||||||
"description": "FXServer to MySQL communication via node-mysql2",
|
"description": "FXServer to MySQL communication via node-mysql2",
|
||||||
"repository": "[email protected]:overextended/oxmysql.git",
|
"repository": "[email protected]:overextended/oxmysql.git",
|
||||||
"author": "dunak-debug <[email protected]>",
|
"author": "dunak-debug <[email protected]>",
|
||||||
|
|||||||
+18
-39
@@ -14,28 +14,24 @@ export const rawExecute = async (
|
|||||||
) => {
|
) => {
|
||||||
const type = executeType(query);
|
const type = executeType(query);
|
||||||
parameters = parseExecute(parameters);
|
parameters = parseExecute(parameters);
|
||||||
|
const response = [] as RowDataPacket;
|
||||||
let single: boolean;
|
let single: boolean;
|
||||||
|
|
||||||
if (!parameters.every(Array.isArray)) parameters = [[...parameters]];
|
if (!parameters.every(Array.isArray)) parameters = [[...parameters]];
|
||||||
|
|
||||||
const length = parameters.length - 1;
|
|
||||||
|
|
||||||
await scheduleTick();
|
await scheduleTick();
|
||||||
|
|
||||||
await new Promise((resolve, reject) => {
|
const connection = await pool.promise().getConnection();
|
||||||
pool.getConnection((err, connection) => {
|
|
||||||
if (err) return reject([connection, err]);
|
|
||||||
|
|
||||||
const execute = () => {
|
try {
|
||||||
|
if (type !== null) await connection.beginTransaction();
|
||||||
const placeholders = query.split('?').length - 1;
|
const placeholders = query.split('?').length - 1;
|
||||||
const response = [] as RowDataPacket;
|
|
||||||
|
|
||||||
parameters.forEach((values, index) => {
|
for (let values of parameters as CFXParameters[]) {
|
||||||
const executionTime = process.hrtime();
|
const executionTime = process.hrtime();
|
||||||
values = parseValues(placeholders, values);
|
values = parseValues(placeholders, values);
|
||||||
|
|
||||||
connection.execute(query, values, (err, results: RowDataPacket[][]) => {
|
const [results, _] = (await connection.execute(query, values)) as RowDataPacket[][];
|
||||||
if (err) return reject([connection, err]);
|
|
||||||
|
|
||||||
if (cb) {
|
if (cb) {
|
||||||
if (results.length > 1) {
|
if (results.length > 1) {
|
||||||
@@ -46,48 +42,31 @@ export const rawExecute = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, values as typeof parameters);
|
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();
|
|
||||||
|
|
||||||
resolve(response);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (type === null) {
|
single = response.length === 1;
|
||||||
execute();
|
if (type !== null) connection.commit();
|
||||||
} else
|
} catch (err: any) {
|
||||||
connection.beginTransaction((err) => {
|
if (connection && type !== null) connection.rollback();
|
||||||
if (err) return reject(err);
|
|
||||||
execute();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(([connection, err]) => {
|
|
||||||
if (!single && type !== null) connection.rollback();
|
|
||||||
|
|
||||||
throw new Error(`${invokingResource} was unable to execute a query!
|
throw new Error(`${invokingResource} was unable to execute a query!
|
||||||
${err.message}
|
${err.message}
|
||||||
${err.sql}`);
|
${err.sql}`);
|
||||||
})
|
} finally {
|
||||||
// somebody who cares about types can do this
|
connection.release();
|
||||||
.then((results: any) => {
|
}
|
||||||
|
|
||||||
if (cb) {
|
if (cb) {
|
||||||
if (single) {
|
if (single) {
|
||||||
if (type === null) {
|
if (type === null) {
|
||||||
//@ts-expect-error
|
//@ts-expect-error
|
||||||
if (results[0][0] && Object.keys(results[0][0]).length === 1) cb(Object.values(results[0][0])[0]);
|
if (response[0][0] && Object.keys(response[0][0]).length === 1) cb(Object.values(response[0][0])[0]);
|
||||||
else cb(results[0][0]);
|
else cb(response[0][0]);
|
||||||
} else {
|
} else {
|
||||||
cb(results[0]);
|
cb(response[0]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cb(results);
|
cb(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
};
|
};
|
||||||
@@ -17,6 +17,7 @@ export const rawQuery = async (
|
|||||||
[query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb);
|
[query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb);
|
||||||
|
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
|
//@ts-expect-error todo: patch type with executionTime
|
||||||
pool.query(query, parameters, (err, result, _, executionTime) => {
|
pool.query(query, parameters, (err, result, _, executionTime) => {
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user