mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 14:38:50 +00:00
refactor(execute): split into execute and query
query refers to the old execute function. preparedStatement renamed to execute. Parse arguments before establishing connection. Utilise response function to determine and return correct values to the export. execute now accepts an array of values when performing a single query, rather than requiring an array within an array.
This commit is contained in:
1 parent
860af3ff14
commit
e5c5fc3b1b
3 files changed
+86
-123
No files matched your search
@@ -0,0 +1,59 @@
|
||||
const hrtime = require('process').hrtime;
|
||||
import { mysql_debug, mysql_slow_query_warning } from '../config.js';
|
||||
import { isReady, scheduleTick, serverReady, preparedTypes, response } from '../utils.js';
|
||||
import pool from './pool.js';
|
||||
|
||||
export default async (invokingResource, query, parameters, cb) => {
|
||||
if (!isReady) serverReady();
|
||||
const type = preparedTypes(query);
|
||||
if (!type) throw new Error(`Prepared statements only accept SELECT, INSERT, UPDATE, and DELETE methods!`);
|
||||
|
||||
scheduleTick();
|
||||
const connection = await pool.getConnection();
|
||||
try {
|
||||
const queryCount = parameters.length;
|
||||
let results = [];
|
||||
let executionTime = hrtime();
|
||||
|
||||
if (typeof parameters[0] !== 'object') {
|
||||
const [result] = await connection.execute(query, parameters);
|
||||
results[0] = response(type, result);
|
||||
} else {
|
||||
for (let i = 0; i < queryCount; i++) {
|
||||
const [result] = await connection.execute(query, parameters[i]);
|
||||
results[i] = response(type, result);
|
||||
}
|
||||
}
|
||||
|
||||
executionTime = hrtime(executionTime)[1] / 1000000;
|
||||
|
||||
if (results.length === 1) {
|
||||
if (type === 'execute') {
|
||||
if (results[0][0] && Object.keys(results[0][0]).length === 1) results = Object.values(results[0][0])[0];
|
||||
else results = results[0][0];
|
||||
} else {
|
||||
results = results[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (executionTime >= mysql_slow_query_warning || mysql_debug)
|
||||
console.log(
|
||||
`^3[${mysql_debug ? 'DEBUG' : 'WARNING'}] ${invokingResource} took ${executionTime}ms to execute ${
|
||||
queryCount > 1 ? queryCount + ' queries' : 'a query'
|
||||
}!
|
||||
${query} ${JSON.stringify(parameters)}^0`
|
||||
);
|
||||
|
||||
if (cb) {
|
||||
cb(results);
|
||||
} else {
|
||||
return results;
|
||||
}
|
||||
} catch (err) {
|
||||
throw new Error(`${invokingResource} was unable to execute a query!
|
||||
${err.message}
|
||||
${err.sql || `${query} ${JSON.stringify(parameters)}`}`);
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
import { mysql_debug, mysql_slow_query_warning } from '../config.js';
|
||||
import { isReady, scheduleTick, serverReady, parseArguments, response } from '../utils.js';
|
||||
import pool from './pool.js';
|
||||
|
||||
export default async (type, invokingResource, query, parameters, cb) => {
|
||||
if (!isReady) serverReady();
|
||||
[query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb);
|
||||
scheduleTick();
|
||||
try {
|
||||
const [result, _, executionTime] = await pool.query(query, parameters);
|
||||
|
||||
if (executionTime >= mysql_slow_query_warning || mysql_debug)
|
||||
console.log(
|
||||
`^3[${mysql_debug ? 'DEBUG' : 'WARNING'}] ${invokingResource} took ${executionTime}ms to execute a query!
|
||||
${query} ${JSON.stringify(parameters)}^0`
|
||||
);
|
||||
if (cb) {
|
||||
cb(response(type, result));
|
||||
} else {
|
||||
return response(type, result);
|
||||
}
|
||||
} catch (err) {
|
||||
throw new Error(`${invokingResource} was unable to execute a query!
|
||||
${err.message}
|
||||
${err.sql || `${query} ${JSON.stringify(parameters)}`}`);
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user