-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
157 lines (156 loc) · 4.6 KB
/
database.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
try{
var dotenv = require('dotenv')
dotenv.load();
}catch(err){
//do nothing if this fails, we are in dev
}
var pg = require('pg');
pg.defaults.poolIdleTimeout = 2000; //2 seconds
var counter = 0;
var logger = require('tracer').console({
format : [ "<{{title}}> {{file}}:{{line}}: {{message}}", {error: "<{{title}}> {{file}}:{{line}}: {{message}} \nCall Stack: {{stack}}"}],
preprocess: function(data){ data.title = data.title.toUpperCase()}
})
global.logger = logger
queryDatabasePerRow = function(query,callback){
if(query == undefined || query == null){
callback({error:"bad query"})
return;
}
pg.connect(process.env.DATABASE_URL, function(err, client) {
if (err) logger.error(err);
client
.query(query) //'SELECT * FROM clients_and_their_info')
.on('row', function(row) {
callback(row)
done();
return JSON.stringify(row)
});
});
}
sendQuery = function(query, params, callback){
if(query == undefined || query == null){
callback({error:"bad query"})
logger.error("bad query\nquery: %s\n params:%s", query, params)
return;
}
//console.log(query)
pg.connect(process.env.DATABASE_URL, function(err, client) {
if (err) {
logger.error("Error while connecting\n%j", err);
pg.end();
return;
}
//console.log('Connected to postgres! Getting schemas...');
client
.query(query, params, function(err, result) {
if(err) {
err["Error"] = true;
logger.error('Error running query\nquery: %s\n params: %s\nerror: %j', query, params, err);
callback(err)
client.end();
}
else{
callback(result);
client.end();
}
})
})
}
sendQuery2 = function(q,params, callback){
if(q == undefined || q == null){
logger.error("bad query\nquery: %s\n params:%s", query, params)
callback({error:"bad query"})
return;
}
var client = new pg.Client(process.env.DATABASE_URL);
client.connect();
var query = client.query(q,params);
query.on('row', function(row){
callback(row)
})
query.on('end', function(){
client.end();
})
}
viewTable = function(callback){
sendQuery("SELECT schools.name AS schoolName, session.name, session.id, session.school \
FROM schools, session \
WHERE schools.id = session.school \
AND (select count(*) from classes_1 where school = session.school and session = session.id) > 0" //eliminate all sessions with no classes
, [], callback)
}
testAddDataToTable = function(callback){
count = 0
var interval = setInterval( function(){
sendQuery('Insert into testTable (data) values (\'' + count + '\')',[], callback);
count += 1
if(count > 50)
clearInterval(interval)
}, 50)
};
getInstitutions = function(callback){
var tries = 0;
temp = function(){
viewTable(function(result){
try{
callback(result["rows"])
}
catch(err){
if(tries < 3){
tries += 1;
temp()
}
else{
callback([])
}
}
})
};
temp()
};
sendQueryNTimes = function(query,params,numberOfTimes,onError,callback){
var tries = 0;
temp = function(){
sendQuery( query,params,function(result){
try{
if(result["rows"].length > 0)
callback(result["rows"]);
else
callback(onError)
}
catch(err){
if(tries < numberOfTimes){
tries += 1;
temp();
}
else{
if(callback)
callback(onError);
}
}
})
};
temp()
}
getClassesWTopic = function (params, table, callback) {
queryDict = {
'classes' : 'select distinct * from classes_1 where school = $1 and session = $2',
'classes_1' : 'select distinct * from classes_2 where school = $1 and session = $2',
};
sendQueryNTimes(queryDict[table],params,3,[],callback);
};
getClasses = function(params, callback){
getClassesWTopic(params,'classes',callback);
};
getTopics = function (params,callback) {
sendQueryNTimes('select distinct topic from classes_1',[],3,[],callback)
};
getTeacherInfo = function(params,callback){
sql = "select distinct schools.name as schoolName from schools, session where schools.id = session.school and schools.id = $1";
sendQuery2(sql, [params[0]], function(data){
teacherSQL = "select distinct * from ratemyprofessor where inst like \'%"+ data["schoolname"] +"%\' and name = $1";
sendQueryNTimes(teacherSQL, [params[1]], 3, ["No Data"], callback);
})
};
//testAddDataToTable(function(result){console.log(result)})