-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
88 lines (80 loc) · 2.86 KB
/
db.js
File metadata and controls
88 lines (80 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var mysql = require("mysql");
var util = require("util");
// First you need to create a connection to the db
var pool = mysql.createPool({
connectionLimit : 10,
host: '127.0.0.1',
user: 'root',
password: '',
database: 'uber_hack'
});
var select_query = 'SELECT id, loc FROM location where status <> 0 order by id limit 1';
//var insert_query = 'INSERT INTO booking_requests (user_id, status, company_id, created_at) VALUES (%d, 1, %d, now())';
var update_query = 'UPDATE location set status = 0 where id = %d';
var update_all_query = 'UPDATE location set status = 1';
module.exports.get_active_location = function(callback) {
console.log("In get_active_location");
pool.getConnection(function (err, connection) {
var selectQuery = select_query;
connection.query(selectQuery, function(err, rows) {
connection.release();
if(!err) {
console.log('Data received from Db:');
console.log(rows);
callback(null, rows);
} else {
console.log('Something went wrong while updating data, error ' + err);
callback(true, err);
}
});
});
};
module.exports.expire_location = function(id, callback) {
console.log("In expire_location for id : ", id);
pool.getConnection(function (err, connection) {
var updateQuery = util.format(update_query, id);
connection.query(updateQuery, function(err,rows){
connection.release();
if(!err) {
console.log('Data received from Db:\n');
console.log(rows);
callback(null, rows);
} else {
console.log('Something went wrong while updating data, error ' + err);
callback(true, err)
}
});
});
};
module.exports.activate_all = function(callback) {
console.log("In activate_all");
pool.getConnection(function (err, connection) {
var updateQuery = update_all_query;
connection.query(updateQuery, function(err,rows){
connection.release();
if(!err) {
console.log('Data received from Db:\n');
console.log(rows);
callback(null, rows);
} else {
console.log('Something went wrong while updating data, error ' + err);
callback(true, err)
}
});
});
};
/**
* Listen to SIGINT and close the connection poll.
* Exit the process once poll end returns
*/
process.on('SIGINT', function () {
console.log('Process is exiting, clearing pool');
pool.end(function(err) {
if(!err) {
console.log('Successfully closed the connection poll.');
} else {
console.log('!!!!!!!! Error trying to close connection poll !!!!!!!!');
}
process.exit();
});
});