1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 22:19:26 +00:00

Fix bug where trailing whitespace in sql query after ; would error

This commit is contained in:
2022-01-20 17:57:01 +00:00
parent 5854c3928a
commit ec3890fa7f
3 changed files with 17 additions and 2 deletions

View File

@@ -46,8 +46,10 @@ class DatabaseConnectionPool {
* @return {(Array<object>|object)} Data returned from the database * @return {(Array<object>|object)} Data returned from the database
*/ */
async runQuery(sql, params) { async runQuery(sql, params) {
sql = sql.trim();
if (sql.slice(-1) !== ';') 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 // Execute as non-prepared if no params are supplied
if (typeof params === 'undefined') { if (typeof params === 'undefined') {

View File

@@ -43,4 +43,15 @@ describe('DatabaseConnectionPool', () => {
expect(() => dbp.runQuery(sql, params)).toThrow(); 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
});
});
}); });

View File

@@ -3,8 +3,10 @@
const DatabaseConnectionPool = require('./DatabaseConnectionPool'); const DatabaseConnectionPool = require('./DatabaseConnectionPool');
const mockRunQuery = jest.fn((sql, params) => { const mockRunQuery = jest.fn((sql, params) => {
sql = sql.trim();
if (sql.slice(-1) !== ';') 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 // Execute as non-prepared if no params are supplied
if (typeof params === 'undefined') { if (typeof params === 'undefined') {