Skip to content

Commit

Permalink
.execute => .query, remove scheduler.js
Browse files Browse the repository at this point in the history
This is necessary due to
sidorares/node-mysql2#649
We could just cast wherever we call execute, instead of using the
typecast option, but this is easier,
and we shouldn't really NEED prepared statements
(though they would be nice)
  • Loading branch information
BinaryFissionGames committed Jun 4, 2018
1 parent f616705 commit fb580d4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
28 changes: 14 additions & 14 deletions node/db_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module.exports = {
conn = await pool.getConnection();
try{
if(num_requests <= 0 ){
var [res, field] = await connection.execute('SELECT loginrequests.id, users.email, loginrequests.comment, loginrequests.date_requested FROM users INNER JOIN loginrequests ON users.loginreq_id = loginrequests.id WHERE email_validated=1', []);
var [res, field] = await connection.query('SELECT loginrequests.id, users.email, loginrequests.comment, loginrequests.date_requested FROM users INNER JOIN loginrequests ON users.loginreq_id = loginrequests.id WHERE email_validated=1', []);
}else{
var [res, field] = await connection.execute('SELECT loginrequests.id, users.email, loginrequests.comment, loginrequests.date_requested FROM users INNER JOIN loginrequests ON users.loginreq_id = loginrequests.id WHERE email_validated=1 LIMIT ? OFFSET ?', [num_requests, start_at]);
var [res, field] = await connection.query('SELECT loginrequests.id, users.email, loginrequests.comment, loginrequests.date_requested FROM users INNER JOIN loginrequests ON users.loginreq_id = loginrequests.id WHERE email_validated=1 LIMIT ? OFFSET ?', [num_requests, start_at]);
}
}finally{
connection.release();
Expand All @@ -38,10 +38,10 @@ module.exports = {
/*Gets info about user with given id. Will use given connection if provided, otherwise uses an independant conneciton */
get_user_by_id: async function(id, connection){
if(connection){
var [res, fields] = await connection.execute("SELECT * FROM users WHERE id=?", [id]);
var [res, fields] = await connection.query("SELECT * FROM users WHERE id=?", [id]);

}else{
var [res, fields] = await pool.execute("SELECT * FROM users WHERE id=?", [id]);
var [res, fields] = await pool.query("SELECT * FROM users WHERE id=?", [id]);
}

if(res.length < 1){
Expand All @@ -57,7 +57,7 @@ module.exports = {

let connection = await pool.getConnection();
try{
var [res, fields] = await connection.execute('SELECT id, start_time, duration, approved, user_id FROM timeslots WHERE start_time > ? AND start_time < ?', [beginDate, endDate]);
var [res, fields] = await connection.query('SELECT id, start_time, duration, approved, user_id FROM timeslots WHERE start_time > ? AND start_time < ?', [beginDate, endDate]);
}finally{
connection.release();
}
Expand Down Expand Up @@ -90,7 +90,7 @@ module.exports = {
admin_get_timeslot_requests: async function(beginDate, endDate){
let conn = await pool.getConnection();
try{
var [res, fields] = await connection.execute('SELECT users.email, timeslots.id, timeslots.start_time, timeslots.duration, timeslots.approved'+
var [res, fields] = await connection.query('SELECT users.email, timeslots.id, timeslots.start_time, timeslots.duration, timeslots.approved'+
' FROM timeslots INNER JOIN users ON timeslots.user_id=users.id'+
' WHERE start_time >= ? AND start_time < ?',
[beginDate, endDate]);
Expand Down Expand Up @@ -137,7 +137,7 @@ module.exports = {
let start_date = date;
let end_date = new Date(date.getTime() + duration*1000);

var [res, fields] = await connection.execute("SELECT count(*) FROM timeslots WHERE user_id=? AND"+
var [res, fields] = await connection.query("SELECT count(*) FROM timeslots WHERE user_id=? AND"+
" ((start_time <= ? AND DATE_ADD(start_time, INTERVAL duration SECOND) >= ?)"+
" OR (start_time <= ? AND DATE_ADD(start_time, INTERVAL duration SECOND) >= ?))",
[user_id, start_date, start_date, end_date, end_date]);
Expand All @@ -161,7 +161,7 @@ module.exports = {
let start_date = date;
let end_date = new Date(date.getTime() + duration*1000);

var [res, fields] = await connection.execute("SELECT count(*) FROM timeslots WHERE approved=1 AND"+
var [res, fields] = await connection.query("SELECT count(*) FROM timeslots WHERE approved=1 AND"+
" ((start_time <= ? AND DATE_ADD(start_time, INTERVAL duration SECOND) >= ?)"+
" OR (start_time <= ? AND DATE_ADD(start_time, INTERVAL duration SECOND) >= ?))",
[start_date, start_date, end_date, end_date]);
Expand Down Expand Up @@ -199,7 +199,7 @@ module.exports = {
};
}

await connection.execute("INSERT INTO timeslots (user_id, start_time, duration) VALUES (?, ?, ?)", [user_id, date, duration]);
await connection.query("INSERT INTO timeslots (user_id, start_time, duration) VALUES (?, ?, ?)", [user_id, date, duration]);
await connection.commit();
}catch(e){
//Rollback, let error bubble up.
Expand All @@ -212,7 +212,7 @@ module.exports = {
},
delete_request: async function(req_id, user_id){

let [res, fields] = await pool.execute("DELETE FROM timeslots WHERE user_id=? AND id=?", [user_id, req_id]);
let [res, fields] = await pool.query("DELETE FROM timeslots WHERE user_id=? AND id=?", [user_id, req_id]);

if(res.affectedRows <= 0){
throw {
Expand All @@ -231,7 +231,7 @@ module.exports = {
let connection = await pool.getConnection();

try{
let [res, fields] = await connection.execute("SELECT user_id FROM timeslots WHERE id=?", [id]);
let [res, fields] = await connection.query("SELECT user_id FROM timeslots WHERE id=?", [id]);


if(res.length < 1){
Expand All @@ -243,7 +243,7 @@ module.exports = {

var user_id = res[0].user_id;

await connection.execute("DELETE FROM timeslots WHERE id=?", [id]);
await connection.query("DELETE FROM timeslots WHERE id=?", [id]);

}finally{
connection.release();
Expand All @@ -257,10 +257,10 @@ module.exports = {
async function clean_db(){
let connection = await pool.getConnection();
try{
await connection.execute("DELETE FROM timeslots WHERE DATE_ADD(start_time, INTERVAL duration SECOND) <= ?", [new Date(Date.now())]);
await connection.query("DELETE FROM timeslots WHERE DATE_ADD(start_time, INTERVAL duration SECOND) <= ?", [new Date(Date.now())]);
//Deletes logins and requests from over 2 weeks ago, if they have not been approved yet.
//TODO send out an email to them if this happens?
await connection.execute("DELETE FROM loginrequests WHERE date_requested < ?", [new Date(Date.now() - 14*24*60*60*1000)]);
await connection.query("DELETE FROM loginrequests WHERE date_requested < ?", [new Date(Date.now() - 14*24*60*60*1000)]);
}finally{
connection.release();
}
Expand Down
3 changes: 1 addition & 2 deletions node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ let mysql_pool = mysql.createPool({
password: options['mysql_pass'],
database: options['mysql_db'],
/*This code snippet found from https://www.bennadel.com/blog/3188-casting-bit-fields-to-booleans-using-the-node-js-mysql-driver.htm*/
typeCast: function castField( field, useDefaultTypeCasting ) {
typeCast: function( field, useDefaultTypeCasting ) {
if (field.type === "BIT" && field.length === 1) {
let bytes = field.buffer();
return bytes[0] === 1;

}

return useDefaultTypeCasting();
Expand Down
11 changes: 0 additions & 11 deletions node/scheduler.js

This file was deleted.

10 changes: 5 additions & 5 deletions node/user_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
let connection = await pool.getConnection();

try{
var [res, field] = await connection.execute('SELECT id, passhash, passsalt, approved, admin from users where email = ?', [username]);
var [res, field] = await connection.query('SELECT id, passhash, passsalt, approved, admin from users where email = ?', [username]);
}finally{
connection.release();
}
Expand Down Expand Up @@ -65,7 +65,7 @@ module.exports = {

await connection.beginTransaction();
try{
let [res, fields] = await connection.execute('SELECT * FROM users WHERE email = ?', [email]);
let [res, fields] = await connection.query('SELECT * FROM users WHERE email = ?', [email]);

if(res.length > 0){
throw {
Expand All @@ -74,7 +74,7 @@ module.exports = {
};
}

await connection.execute('CALL user_request(?, ?, ?, ?, ?)', [email, hash, salt, email_tok, comment]);
await connection.query('CALL user_request(?, ?, ?, ?, ?)', [email, hash, salt, email_tok, comment]);
await connection.commit();
}catch(e){
await connection.rollback();
Expand All @@ -88,7 +88,7 @@ module.exports = {
email_verify: async function(email, email_tok){
let connection = await pool.getConnection();
try{
let [results, fields] = await connection.execute('SELECT loginreq_id FROM users WHERE email=?', [email]);
let [results, fields] = await connection.query('SELECT loginreq_id FROM users WHERE email=?', [email]);

if(results.length != 1){
throw {
Expand All @@ -106,7 +106,7 @@ module.exports = {
};
}

let [results1, fields1] = await connection.execute('SELECT email_token, email_validated FROM loginrequests where id=?', [request_ID]);
let [results1, fields1] = await connection.query('SELECT email_token, email_validated FROM loginrequests where id=?', [request_ID]);

if(results1.length < 1){
throw {
Expand Down

0 comments on commit fb580d4

Please sign in to comment.