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,
|
token,
|
||||||
nonce,
|
nonce,
|
||||||
UNIX_TIMESTAMP(expires) as expires
|
UNIX_TIMESTAMP(expires) as expires
|
||||||
from passwordReset
|
from
|
||||||
|
passwordReset
|
||||||
where
|
where
|
||||||
userId = ?
|
userId = ?
|
||||||
and token = ?;
|
and token = ?;
|
||||||
|
|||||||
28
lib/User.js
28
lib/User.js
@@ -38,7 +38,8 @@ class User {
|
|||||||
lastName,
|
lastName,
|
||||||
password,
|
password,
|
||||||
${type}Id as id
|
${type}Id as id
|
||||||
from ${type}
|
from
|
||||||
|
${type}
|
||||||
where
|
where
|
||||||
${type}Id = ?;
|
${type}Id = ?;
|
||||||
`;
|
`;
|
||||||
@@ -90,9 +91,12 @@ class User {
|
|||||||
const conn = await new DatabaseConnectionPool();
|
const conn = await new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `
|
const sql = `
|
||||||
update ${this.type}
|
update
|
||||||
set password = ?
|
${this.type}
|
||||||
where ${this.type}Id = ?;
|
set
|
||||||
|
password = ?
|
||||||
|
where
|
||||||
|
${this.type}Id = ?;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
await conn.runQuery(sql, [ newPassword, this.id ]);
|
await conn.runQuery(sql, [ newPassword, this.id ]);
|
||||||
@@ -134,10 +138,10 @@ class User {
|
|||||||
|
|
||||||
switch (range) {
|
switch (range) {
|
||||||
case 'before':
|
case 'before':
|
||||||
sql += `and t.testDate < CURDATE()`;
|
sql += `and t.testDate < curdate()`;
|
||||||
break;
|
break;
|
||||||
case 'after':
|
case 'after':
|
||||||
sql += `and t.testDate > CURDATE()`;
|
sql += `and t.testDate > curdate()`;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +175,8 @@ class User {
|
|||||||
otherNames,
|
otherNames,
|
||||||
lastName,
|
lastName,
|
||||||
password)
|
password)
|
||||||
VALUES (?, ?, ?, ?, ?, ?);
|
values
|
||||||
|
(?, ?, ?, ?, ?, ?);
|
||||||
`;
|
`;
|
||||||
|
|
||||||
await conn.runQuery(sql, [
|
await conn.runQuery(sql, [
|
||||||
@@ -202,9 +207,12 @@ class User {
|
|||||||
|
|
||||||
for (const type of types) {
|
for (const type of types) {
|
||||||
const sql = `
|
const sql = `
|
||||||
select ${type}id as id
|
select
|
||||||
from ${type}
|
${type}id as id
|
||||||
where email = ?;
|
from
|
||||||
|
${type}
|
||||||
|
where
|
||||||
|
email = ?;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const id = (await conn.runQuery(sql, [
|
const id = (await conn.runQuery(sql, [
|
||||||
|
|||||||
@@ -14,7 +14,13 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Non prepared query should not execute with params', () => {
|
test('Non prepared query should not execute with params', () => {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class;`;
|
const sql = `
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class;
|
||||||
|
`;
|
||||||
|
|
||||||
dbp.runQuery(sql);
|
dbp.runQuery(sql);
|
||||||
|
|
||||||
expect(dbp.runQuery.mock.results[0].value).toEqual({
|
expect(dbp.runQuery.mock.results[0].value).toEqual({
|
||||||
@@ -25,7 +31,15 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Prepared query should execute along with params', () => {
|
test('Prepared query should execute along with params', () => {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class where name = ?;`;
|
const sql = `
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class
|
||||||
|
where
|
||||||
|
name = ?;
|
||||||
|
`;
|
||||||
|
|
||||||
const params = [ 'bob' ];
|
const params = [ 'bob' ];
|
||||||
dbp.runQuery(sql, params);
|
dbp.runQuery(sql, params);
|
||||||
|
|
||||||
@@ -38,7 +52,15 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Query without trailing ; should throw error', () => {
|
test('Query without trailing ; should throw error', () => {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class where name = ?`;
|
const sql = `
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class
|
||||||
|
where
|
||||||
|
name = ?
|
||||||
|
`;
|
||||||
|
|
||||||
const params = [ 'bob' ];
|
const params = [ 'bob' ];
|
||||||
|
|
||||||
expect(() => dbp.runQuery(sql, params)).toThrow();
|
expect(() => dbp.runQuery(sql, params)).toThrow();
|
||||||
@@ -47,7 +69,12 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Query with whitespace after ; should not fail', ()=> {
|
test('Query with whitespace after ; should not fail', ()=> {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class; `;
|
const sql = `
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class;
|
||||||
|
`;
|
||||||
|
|
||||||
expect(() => dbp.runQuery(sql)).not.toThrow();
|
expect(() => dbp.runQuery(sql)).not.toThrow();
|
||||||
});
|
});
|
||||||
@@ -55,7 +82,14 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Prepared query should fail if no params are given', () => {
|
test('Prepared query should fail if no params are given', () => {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class where name = ?;`;
|
const sql = `
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class
|
||||||
|
where
|
||||||
|
name = ?;
|
||||||
|
`;
|
||||||
|
|
||||||
expect(() => dbp.runQuery(sql)).toThrow();
|
expect(() => dbp.runQuery(sql)).toThrow();
|
||||||
});
|
});
|
||||||
@@ -63,7 +97,14 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Prepared query should fail if too many params given', () => {
|
test('Prepared query should fail if too many params given', () => {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class where name = ?;`;
|
const sql = `
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class
|
||||||
|
where
|
||||||
|
name = ?;
|
||||||
|
`;
|
||||||
const params = [ 'bob', 'jim' ];
|
const params = [ 'bob', 'jim' ];
|
||||||
|
|
||||||
expect(() => dbp.runQuery(sql, params)).toThrow();
|
expect(() => dbp.runQuery(sql, params)).toThrow();
|
||||||
@@ -72,8 +113,16 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Prepared query should fail if too few params given', () => {
|
test('Prepared query should fail if too few params given', () => {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class where name = ?
|
const sql = `
|
||||||
and classId = ? and subjectId = ?;`;
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class
|
||||||
|
where
|
||||||
|
name = ?
|
||||||
|
and classId = ?
|
||||||
|
and subjectId = ?;
|
||||||
|
`;
|
||||||
|
|
||||||
const params = [ 'bob' ];
|
const params = [ 'bob' ];
|
||||||
|
|
||||||
@@ -83,7 +132,12 @@ describe('DatabaseConnectionPool', () => {
|
|||||||
test('Non-prepared query should fail if params given', () => {
|
test('Non-prepared query should fail if params given', () => {
|
||||||
const dbp = new DatabaseConnectionPool();
|
const dbp = new DatabaseConnectionPool();
|
||||||
|
|
||||||
const sql = `SELECT * FROM class;`;
|
const sql = `
|
||||||
|
select
|
||||||
|
*
|
||||||
|
from
|
||||||
|
class;
|
||||||
|
`;
|
||||||
|
|
||||||
const params = [ 'bob', 'jim' ];
|
const params = [ 'bob', 'jim' ];
|
||||||
|
|
||||||
|
|||||||
@@ -549,9 +549,10 @@ async function insertData(dbOptions) {
|
|||||||
const qs = '?, '.repeat(Object.keys(
|
const qs = '?, '.repeat(Object.keys(
|
||||||
dataToInsert).length).slice(0, -2);
|
dataToInsert).length).slice(0, -2);
|
||||||
|
|
||||||
const sql = `INSERT INTO ${table} ` +
|
const sql = `
|
||||||
|
insert into ${table} ` +
|
||||||
`(${Object.keys(dataToInsert)})` +
|
`(${Object.keys(dataToInsert)})` +
|
||||||
` VALUES ` +
|
` values ` +
|
||||||
`(${qs});`;
|
`(${qs});`;
|
||||||
|
|
||||||
console.log(sql);
|
console.log(sql);
|
||||||
|
|||||||
Reference in New Issue
Block a user