-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
86 additions
and
123 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`}`); | ||
} | ||
}; |