diff --git a/lib/DatabaseConnectionPool.js b/lib/DatabaseConnectionPool.js index 752af64..e1003ec 100644 --- a/lib/DatabaseConnectionPool.js +++ b/lib/DatabaseConnectionPool.js @@ -46,8 +46,10 @@ class DatabaseConnectionPool { * @return {(Array|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') { diff --git a/lib/DatabaseConnectionPool.test.js b/lib/DatabaseConnectionPool.test.js index 99ff0aa..6e5f023 100644 --- a/lib/DatabaseConnectionPool.test.js +++ b/lib/DatabaseConnectionPool.test.js @@ -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 + }); + }); }); diff --git a/lib/__mocks__/DatabaseConnectionPool.js b/lib/__mocks__/DatabaseConnectionPool.js index db07518..3faf3d2 100644 --- a/lib/__mocks__/DatabaseConnectionPool.js +++ b/lib/__mocks__/DatabaseConnectionPool.js @@ -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') {