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

Consistent SQL query formatting

This commit is contained in:
2022-02-25 14:44:35 +00:00
parent 2fac6e7aba
commit 57af81cc98
4 changed files with 87 additions and 23 deletions

View File

@@ -18,7 +18,8 @@ class PasswordReset {
token,
nonce,
UNIX_TIMESTAMP(expires) as expires
from passwordReset
from
passwordReset
where
userId = ?
and token = ?;

View File

@@ -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, [

View File

@@ -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' ];