-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.js
175 lines (173 loc) · 5.97 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var mysql = require('mysql');
function mysql_real_escape_string (str) {
return str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function (char) {
switch (char) {
case "\0":
return "\\0";
case "\x08":
return "\\b";
case "\x09":
return "\\t";
case "\x1a":
return "\\z";
case "\n":
return "\\n";
case "\r":
return "\\r";
case "\"":
case "'":
case "\\":
case "%":
return "\\"+char; // prepends a backslash to backslash, percent,
// and double/single quotes
}
});
}
//adding data
exports.addtodb = function(datarows) {
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'bclass',
});
//assume that the table is set up beforehand
connection.connect();
var datarow;
var values;
var k = 0;
for(var i = 0; i < datarows.length; i++) {
datarow = datarows[i];
values = "('" + mysql_real_escape_string(datarow.dept) + ", " +
mysql_real_escape_string(datarow.title) + ", " +
mysql_real_escape_string(datarow.courseno) + ", " +
mysql_real_escape_string(datarow.sectionno) + ", " +
mysql_real_escape_string(datarow.controlno) + ", " +
mysql_real_escape_string(datarow.time) + ", " +
mysql_real_escape_string(datarow.room) + ", " +
mysql_real_escape_string(datarow.units) + ", " +
mysql_real_escape_string(datarow.instructor) +", " +
mysql_real_escape_string(datarow.examgroup) + ", " +
mysql_real_escape_string(datarow.restrictions) + ", " +
mysql_real_escape_string(datarow.note) +"')";
connection.query('INSERT INTO courses (dept, title, courseno, sectionno, controlno, time, room, units, instructor, examgroup, restrictions, note) VALUES ' + values, function(err, rows, fields) {
if (err) throw err;
});
}
connection.end();
};
exports.addorupdate = function(datarows) {
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'bclass',
});
//assume that the table is set up beforehand
connection.connect();
var datarow;
var values;
for(var i = 0; i < datarows.length; i++) {
datarow = datarows[i];
connection.query('SELECT * FROM courses where controlno = '+datarow.controlno, function(err, rows, fields) {
var olddata = true;
if(rows.length != 0) { //update data
values = " dept = " + mysql_real_escape_string(datarow.dept) + ", title = " +
mysql_real_escape_string(datarow.title) + ", courseno = " +
mysql_real_escape_string(datarow.courseno) + ", sectionno = " +
mysql_real_escape_string(datarow.sectionno) + ", controlno = " +
mysql_real_escape_string(datarow.controlno) + ", time = " +
mysql_real_escape_string(datarow.time) + ", room = " +
mysql_real_escape_string(datarow.room) + ", units = " +
mysql_real_escape_string(datarow.units) + ", instructor = " +
mysql_real_escape_string(datarow.instructor) +", examgroup = " +
mysql_real_escape_string(datarow.examgroup) + ", restrictions = " +
mysql_real_escape_string(datarow.restrictions) + ", note = " +
mysql_real_escape_string(datarow.note);
connection.query('UPDATE courses SET' + values+' where controlno = '+datarow.controlno, function(err, rows, fields) {
if (err) throw err;
});
}else{
/**for(var j = 0; j < rows[0].length; j++) {
switch(j) {
case 0:
if(row[j] != datarow.dept) {
olddata = false;
}
break;
case 1:
if(row[j] != datarow.title) {
olddata = false;
}
break;
case 2:
if(row[j] != datarow.courseno) {
olddata = false;
}
break;
case 3:
if(row[j] != datarow.sectionno) {
olddata = false;
}
break;
case 4:
if(row[j] != datarow.controlno) {
olddata = false;
}
break;
case 5:
if(row[j] != datarow.time) {
olddata = false;
}
break;
case 6:
if(row[j] != datarow.room) {
olddata = false;
}
break;
case 7:
if(row[j] != datarow.units) {
olddata = false;
}
break;
case 8:
if(row[j] != datarow.instructor) {
olddata = false;
}
break;
case 9:
if(row[j] != datarow.examgroup) {
olddata = false;
}
break;
case 10:
if(row[j] != datarow.restrictions) {
olddata = false;
}
break;
case 11:
if(row[j] != datarow.note) {
olddata = false;
}
break;
}
} **/
values = "('" + mysql_real_escape_string(datarow.dept) + "', '" +
mysql_real_escape_string(datarow.title) + "', '" +
mysql_real_escape_string(datarow.courseno) + "', '" +
mysql_real_escape_string(datarow.sectionno) + "', '" +
mysql_real_escape_string(datarow.controlno) + "', '" +
mysql_real_escape_string(datarow.time) + "', '" +
mysql_real_escape_string(datarow.room) + "', '" +
mysql_real_escape_string(datarow.units) + "', '" +
mysql_real_escape_string(datarow.instructor) +"', '" +
mysql_real_escape_string(datarow.examgroup) + "', '" +
mysql_real_escape_string(datarow.restrictions) + "', '" +
mysql_real_escape_string(datarow.note) +"')";
connection.query('INSERT INTO courses (dept, title, courseno, sectionno, controlno, time, room, units, instructor, examgroup, restrictions, note) VALUES ' + values, function(err, rows, fields) {
if (err) throw err;
});
}
}
connection.end();
};