Skip to content

Commit

Permalink
Add 'execute' method for prepared queries
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisveness committed Apr 13, 2020
1 parent 9ef0c19 commit 4726018
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/mysqldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ class MysqlDb {
return [ rows, fields ];
}

/**
* Prepare & execute a query.
*
* @param {string} sql - The SQL command to be executed.
* @param {Array} values - Values to be substituted in SQL placeholders.
* @returns Array containing array of result rows and array of fields.
*
* @example
* const [ books ] = await Db.execute('Select * From Books Where Author = :author', { author: 'David' });
*/
static async execute(sql, values) {
if (!connectionPool) await setupConnectionPool();

const t1 = performance.now();

const [ rows, fields ] = await connectionPool.execute(sql, values);

const t2 = performance.now();
debug('execute', `${(t2-t1).toFixed(0).padStart(3, ' ')}ms`, sql.trim().split('\n')[0]+(sql.trim().split('\n').length>1?'...':''), ${rows.length}`);

return [ rows, fields ];
}

/**
* Get a connection to the database.
*
Expand Down

0 comments on commit 4726018

Please sign in to comment.