mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 20:19:30 +00:00
Fix bug where trailing whitespace in sql query after ; would error
This commit is contained in:
@@ -46,8 +46,10 @@ class DatabaseConnectionPool {
|
||||
* @return {(Array<object>|object)} Data returned from the database
|
||||
*/
|
||||
async runQuery(sql, params) {
|
||||
sql = sql.trim();
|
||||
|
||||
if (sql.slice(-1) !== ';')
|
||||
throw new Error('Invalid query, needs ;');
|
||||
throw new Error('Invalid query, needs trailing ;');
|
||||
|
||||
// Execute as non-prepared if no params are supplied
|
||||
if (typeof params === 'undefined') {
|
||||
|
||||
@@ -43,4 +43,15 @@ describe('DatabaseConnectionPool', () => {
|
||||
|
||||
expect(() => dbp.runQuery(sql, params)).toThrow();
|
||||
});
|
||||
|
||||
test('Query with whitespace after ; should not fail', ()=> {
|
||||
const dbp = new DatabaseConnectionPool();
|
||||
|
||||
const sql = `SELECT * FROM class;`;
|
||||
dbp.runQuery(sql);
|
||||
|
||||
expect(dbp.runQuery.mock.results[0].value).toEqual({
|
||||
sql: sql
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
const DatabaseConnectionPool = require('./DatabaseConnectionPool');
|
||||
|
||||
const mockRunQuery = jest.fn((sql, params) => {
|
||||
sql = sql.trim();
|
||||
|
||||
if (sql.slice(-1) !== ';')
|
||||
throw new Error('Invalid query, needs ;');
|
||||
throw new Error('Invalid query, needs trailing ;');
|
||||
|
||||
// Execute as non-prepared if no params are supplied
|
||||
if (typeof params === 'undefined') {
|
||||
|
||||
Reference in New Issue
Block a user