Skip to content

Commit

Permalink
refactor(execute): split into execute and query
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thelindat committed Dec 23, 2021
1 parent 860af3f commit e5c5fc3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 123 deletions.
123 changes: 0 additions & 123 deletions src/execute.js

This file was deleted.

59 changes: 59 additions & 0 deletions src/server/db/execute.js
Original file line number Diff line number Diff line change
@@ -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();
}
};
27 changes: 27 additions & 0 deletions src/server/db/query.js
Original file line number Diff line number Diff line change
@@ -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)}`}`);
}
};

0 comments on commit e5c5fc3

Please sign in to comment.