mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 22:21:51 +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
+45
-65
No files matched your search
+1
-1
@@ -3,7 +3,7 @@ game 'common'
|
||||
|
||||
name 'oxmysql'
|
||||
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'
|
||||
author 'overextended'
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "oxmysql",
|
||||
"version": "2.2.4",
|
||||
"version": "2.2.5",
|
||||
"description": "FXServer to MySQL communication via node-mysql2",
|
||||
"repository": "[email protected]:overextended/oxmysql.git",
|
||||
"author": "dunak-debug <[email protected]>",
|
||||
|
||||
+42
-63
@@ -14,80 +14,59 @@ export const rawExecute = async (
|
||||
) => {
|
||||
const type = executeType(query);
|
||||
parameters = parseExecute(parameters);
|
||||
const response = [] as RowDataPacket;
|
||||
let single: boolean;
|
||||
|
||||
if (!parameters.every(Array.isArray)) parameters = [[...parameters]];
|
||||
|
||||
const length = parameters.length - 1;
|
||||
|
||||
await scheduleTick();
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
pool.getConnection((err, connection) => {
|
||||
if (err) return reject([connection, err]);
|
||||
const connection = await pool.promise().getConnection();
|
||||
|
||||
const execute = () => {
|
||||
const placeholders = query.split('?').length - 1;
|
||||
const response = [] as RowDataPacket;
|
||||
try {
|
||||
if (type !== null) await connection.beginTransaction();
|
||||
const placeholders = query.split('?').length - 1;
|
||||
|
||||
parameters.forEach((values, index) => {
|
||||
const executionTime = process.hrtime();
|
||||
values = parseValues(placeholders, values);
|
||||
for (let values of parameters as CFXParameters[]) {
|
||||
const executionTime = process.hrtime();
|
||||
values = parseValues(placeholders, values);
|
||||
|
||||
connection.execute(query, values, (err, results: RowDataPacket[][]) => {
|
||||
if (err) return reject([connection, err]);
|
||||
const [results, _] = (await connection.execute(query, values)) as RowDataPacket[][];
|
||||
|
||||
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();
|
||||
|
||||
resolve(response);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
if (type === null) {
|
||||
execute();
|
||||
} else
|
||||
connection.beginTransaction((err) => {
|
||||
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!
|
||||
${err.message}
|
||||
${err.sql}`);
|
||||
})
|
||||
// somebody who cares about types can do this
|
||||
.then((results: any) => {
|
||||
if (cb) {
|
||||
if (single) {
|
||||
if (type === null) {
|
||||
//@ts-expect-error
|
||||
if (results[0][0] && Object.keys(results[0][0]).length === 1) cb(Object.values(results[0][0])[0]);
|
||||
else cb(results[0][0]);
|
||||
} else {
|
||||
cb(results[0]);
|
||||
if (results.length > 1) {
|
||||
for (const value of results) {
|
||||
response.push(parseResponse(type, value));
|
||||
}
|
||||
} else {
|
||||
cb(results);
|
||||
}
|
||||
} else response.push(parseResponse(type, results));
|
||||
}
|
||||
});
|
||||
|
||||
logQuery(invokingResource, query, process.hrtime(executionTime)[1] / 1e6, values as typeof parameters);
|
||||
}
|
||||
|
||||
single = response.length === 1;
|
||||
if (type !== null) connection.commit();
|
||||
} catch (err: any) {
|
||||
if (connection && type !== null) connection.rollback();
|
||||
|
||||
throw new Error(`${invokingResource} was unable to execute a query!
|
||||
${err.message}
|
||||
${err.sql}`);
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
|
||||
if (cb) {
|
||||
if (single) {
|
||||
if (type === null) {
|
||||
//@ts-expect-error
|
||||
if (response[0][0] && Object.keys(response[0][0]).length === 1) cb(Object.values(response[0][0])[0]);
|
||||
else cb(response[0][0]);
|
||||
} else {
|
||||
cb(response[0]);
|
||||
}
|
||||
} else {
|
||||
cb(response);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -17,6 +17,7 @@ export const rawQuery = async (
|
||||
[query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb);
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
//@ts-expect-error todo: patch type with executionTime
|
||||
pool.query(query, parameters, (err, result, _, executionTime) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
|
||||
Reference in new issue
Block a user