From 57af81cc987a52525ba6a827854026241b6aa13c Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 25 Feb 2022 14:44:35 +0000 Subject: [PATCH] Consistent SQL query formatting --- lib/PasswordReset.js | 3 +- lib/User.js | 30 +++++--- lib/__tests__/DatabaseConnectionPool.test.js | 72 +++++++++++++++++--- utility/db/dbTestData.js | 5 +- 4 files changed, 87 insertions(+), 23 deletions(-) diff --git a/lib/PasswordReset.js b/lib/PasswordReset.js index 73f7273..a38f182 100644 --- a/lib/PasswordReset.js +++ b/lib/PasswordReset.js @@ -18,7 +18,8 @@ class PasswordReset { token, nonce, UNIX_TIMESTAMP(expires) as expires - from passwordReset + from + passwordReset where userId = ? and token = ?; diff --git a/lib/User.js b/lib/User.js index 2cfa5b7..eff812e 100644 --- a/lib/User.js +++ b/lib/User.js @@ -38,7 +38,8 @@ class User { lastName, password, ${type}Id as id - from ${type} + from + ${type} where ${type}Id = ?; `; @@ -90,9 +91,12 @@ class User { const conn = await new DatabaseConnectionPool(); const sql = ` - update ${this.type} - set password = ? - where ${this.type}Id = ?; + update + ${this.type} + set + password = ? + where + ${this.type}Id = ?; `; await conn.runQuery(sql, [ newPassword, this.id ]); @@ -134,10 +138,10 @@ class User { switch (range) { case 'before': - sql += `and t.testDate < CURDATE()`; + sql += `and t.testDate < curdate()`; break; case 'after': - sql += `and t.testDate > CURDATE()`; + sql += `and t.testDate > curdate()`; break; } @@ -164,14 +168,15 @@ class User { const hashedPassword = await User.hashPassword(password); const sql = ` - insert into ${type} ( + insert into ${type}( ${type}Id, email, firstName, otherNames, lastName, password) - VALUES (?, ?, ?, ?, ?, ?); + values + (?, ?, ?, ?, ?, ?); `; await conn.runQuery(sql, [ @@ -202,9 +207,12 @@ class User { for (const type of types) { const sql = ` - select ${type}id as id - from ${type} - where email = ?; + select + ${type}id as id + from + ${type} + where + email = ?; `; const id = (await conn.runQuery(sql, [ diff --git a/lib/__tests__/DatabaseConnectionPool.test.js b/lib/__tests__/DatabaseConnectionPool.test.js index 78bf8a6..ff2657d 100644 --- a/lib/__tests__/DatabaseConnectionPool.test.js +++ b/lib/__tests__/DatabaseConnectionPool.test.js @@ -14,7 +14,13 @@ describe('DatabaseConnectionPool', () => { test('Non prepared query should not execute with params', () => { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class;`; + const sql = ` + select + * + from + class; + `; + dbp.runQuery(sql); expect(dbp.runQuery.mock.results[0].value).toEqual({ @@ -25,7 +31,15 @@ describe('DatabaseConnectionPool', () => { test('Prepared query should execute along with params', () => { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class where name = ?;`; + const sql = ` + select + * + from + class + where + name = ?; + `; + const params = [ 'bob' ]; dbp.runQuery(sql, params); @@ -38,7 +52,15 @@ describe('DatabaseConnectionPool', () => { test('Query without trailing ; should throw error', () => { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class where name = ?`; + const sql = ` + select + * + from + class + where + name = ? + `; + const params = [ 'bob' ]; expect(() => dbp.runQuery(sql, params)).toThrow(); @@ -47,7 +69,12 @@ describe('DatabaseConnectionPool', () => { test('Query with whitespace after ; should not fail', ()=> { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class; `; + const sql = ` + select + * + from + class; + `; expect(() => dbp.runQuery(sql)).not.toThrow(); }); @@ -55,7 +82,14 @@ describe('DatabaseConnectionPool', () => { test('Prepared query should fail if no params are given', () => { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class where name = ?;`; + const sql = ` + select + * + from + class + where + name = ?; + `; expect(() => dbp.runQuery(sql)).toThrow(); }); @@ -63,7 +97,14 @@ describe('DatabaseConnectionPool', () => { test('Prepared query should fail if too many params given', () => { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class where name = ?;`; + const sql = ` + select + * + from + class + where + name = ?; + `; const params = [ 'bob', 'jim' ]; expect(() => dbp.runQuery(sql, params)).toThrow(); @@ -72,8 +113,16 @@ describe('DatabaseConnectionPool', () => { test('Prepared query should fail if too few params given', () => { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class where name = ? - and classId = ? and subjectId = ?;`; + const sql = ` + select + * + from + class + where + name = ? + and classId = ? + and subjectId = ?; + `; const params = [ 'bob' ]; @@ -83,7 +132,12 @@ describe('DatabaseConnectionPool', () => { test('Non-prepared query should fail if params given', () => { const dbp = new DatabaseConnectionPool(); - const sql = `SELECT * FROM class;`; + const sql = ` + select + * + from + class; + `; const params = [ 'bob', 'jim' ]; diff --git a/utility/db/dbTestData.js b/utility/db/dbTestData.js index c925010..f0102d1 100644 --- a/utility/db/dbTestData.js +++ b/utility/db/dbTestData.js @@ -549,9 +549,10 @@ async function insertData(dbOptions) { const qs = '?, '.repeat(Object.keys( dataToInsert).length).slice(0, -2); - const sql = `INSERT INTO ${table} ` + + const sql = ` + insert into ${table} ` + `(${Object.keys(dataToInsert)})` + - ` VALUES ` + + ` values ` + `(${qs});`; console.log(sql);