-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerybuilder.js
226 lines (218 loc) · 7.21 KB
/
querybuilder.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const commands = {
select: "SELECT",
insert: "INSERT",
delete: "DELETE",
update: "UPDATE"
};
const tables = {
users: {
tableName: "ps_users",
all: function(){
return ["id", "username", "password", "email", "dateJoined", "associated_groups", "security_token", "security_token_expiry", "fk_user_type", "user_id"];
},
allExceptID: function () {
return this.all().filter(function (c, i, a) {
return i != 0;
});
},
id: function () {
return this.all()[0];
},
username: function () {
return this.all()[1];
},
password: function () {
return this.all()[2];
},
email: function () {
return this.all()[3];
},
datejoined: function () {
return this.all()[4];
},
associated_groups: function () {
return this.all()[5];
},
security_token: function () {
return this.all()[6];
},
security_token_expiry :function(){
return this.all()[7];
},
user_type: function () {
return this.all()[8];
},
user_id: function () {
return this.all()[9];
},
},
usertypes: {
tableName: "ps_usertypes",
all: function(){
return ["id", "name", "level"];
},
allExceptID: function () {
return this.all().filter(function (c, i, a) {
return i != 0;
});
},
id: function () {
return this.all()[0];
},
name: function () {
return this.all()[1];
},
level: function () {
return this.all()[2];
},
},
geo: {
tableName: "ps_geo",
all: function(){
return ["id", "user_id", "group_id", "lati", "longi", "time_entered"];
},
allExceptID: function(){
return this.all().filter(function(c, i, a){
return (i != 0);
});
},
id: function(){
return this.all()[0];
},
userID: function(){
return this.all()[1];
},
groupID: function(){
return this.all()[2];
},
latitude: function(){
return this.all()[3];
},
longitude: function(){
return this.all()[4];
},
datetime: function(){
return this.all()[5];
}
},
campinglist: {
tableName: "ps_campinglist"
},
messages: {
tableName: "ps_messages"
},
invites: {
tableName: "ps_invites"
}
};
function buildQuery(options) {
/**
* Expected properties based on command:
*
* SELECT:
* command: (the command, commands.select)
* tableName: (the name of the table to select from, tables.[THE_TABLE_YOU_WANT].tableName)
* cols: (the columns to select from, this is an array of strings)
* where: (A string to be placed exactly after the WHERE keyword)
*
* INSERT:
* command: (the command, commands.insert)
* tableName: (the name of the table to insert into, tables.[THE_TABLE_YOU_WANT].tableName)
* cols: (the columns to insert into, this is an array of strings)
* values: (the values you want to add in the same order as the columns were specified. This is a mixed type array where the type matches that of the column it is being inserted into)
*
* DELETE:
* command: (the command, commands.delete)
* tableName: (the name of the table to delete from, tables.[THE_TABLE_YOU_WANT].tableName)
* where: (a string which is evaluated by the DB to determine what to delete ex. WHERE (thisCol=thisValue))
*
* UPDATE:
* command: (the command, commands.update)
* tableName: (the name of the table to update, tables.[THE_TABLE_YOU_WANT].tableName)
* cols: (the columns you would like to update, this is an array of strings)
* values: (the updated values for the columns, this is also an array of strings matching one-to-one with cols)
* where: (a string which is evaluated by the DB to determine what to update ex. WHERE (thisCol=thisValue))
*/
// SELECT
if (options.command == commands.select) {
if(options.tableName == null || options.tableName == "" || options.cols == null || options.cols == [] || options.where == null || options.where == ""){
console.log("Could not build query, invalid parameters provided");
return null;
}
var result = commands.select;
result += " ";
options.cols.forEach(function (c, i, a) {
if (a.length - 1 == i) {
result += c + "";
} else {
result += c + ", ";
}
});
result += " FROM " + options.tableName + " WHERE (";
result += options.where + ");";
return result;
// INSERT
} else if (options.command == commands.insert) {
var result = commands.insert + " INTO ";
result += options.tableName + " (";
options.cols.forEach(function (c, i, a) {
if (i == a.length - 1) {
result += c + ")";
} else {
result += c + ", ";
}
});
result += " VALUES (";
options.values.forEach(function (c, i, a) {
if (i == a.length - 1) {
result += c + ")";
} else {
result += c + ", ";
}
});
result += ";";
return result;
// DELETE
}else if(options.command == commands.delete){
var result = options.command + " FROM " + options.tableName + " WHERE (";
result += options.where + ");";
return result;
// UPDATE
}else if(options.command == commands.update){
if(options.cols.length != options.values.length){
console.log("COL and VALUE properties must be the same size");
return null;
}
var result = options.command + " " + options.tableName + " SET ("
options.cols.forEach(function(c, i, a){
if(i == a.length-1){
if(typeof(options.values[i]) == 'string'){
result += options.cols[i] + "='" + options.values[i] + "')";
}else{
result += options.cols[i] + "=" + options.values[i] + ")";
}
}else{
if(typeof(options.values[i]) == 'string'){
result += options.cols[i] + "='" + options.values[i] + "', ";
}else{
result += options.cols[i] + "=" + options.values[i] + ", ";
}
}
});
result += " WHERE (" + options.where + ");";
return result;
// INVALID
}else{
return null;
}
}
function cleanInputs(vals) {
var newMap = vals.map(function (curr, index, arr) {
return curr.replace(/\W/g, "");
});
return newMap;
}
exports.commands = commands;
exports.tables = tables;
exports.cleanInputs = cleanInputs;
exports.buildQuery = buildQuery;