mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 20:59:30 +00:00
Consistent SQL query formatting
This commit is contained in:
@@ -18,7 +18,8 @@ class PasswordReset {
|
||||
token,
|
||||
nonce,
|
||||
UNIX_TIMESTAMP(expires) as expires
|
||||
from passwordReset
|
||||
from
|
||||
passwordReset
|
||||
where
|
||||
userId = ?
|
||||
and token = ?;
|
||||
|
||||
28
lib/User.js
28
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;
|
||||
}
|
||||
|
||||
@@ -171,7 +175,8 @@ class User {
|
||||
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, [
|
||||
|
||||
@@ -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' ];
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user