-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
360 lines (303 loc) · 9.32 KB
/
index.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"use strict";
const xss = require('xss');
function objkeys(obj) {
var data = [];
for(var key in obj)
data.push(key);
return data;
}
function objvalues(obj) {
var data = [];
for(var key in obj)
data.push(obj[key]);
return data;
}
function xssClear(data) {
if (typeof data === 'string')
return xss(data);
return data;
}
var SQLMasterProvider = function (obj) {
this.query = "";
this.tableName = "";
this.wValues = null;
var prepareType = obj.prepareType || '$';
var xssFilter = obj.xssFilter;
var clear = function () {
this.query = "";
this.tableName = "";
this.wValues = null;
}.bind(this)
/**
* Sorgunun yapılacağı tablo ismini belirler
* @param array table
*/
this.from = function (table = null) {
this.query = "";
this.tableName = table;
return this;
}
/**
* Seçilecek sütunları belirler
* @param array cols
*/
this.select = function (cols = null) {
var name = this.tableName;
if (!cols) {
this.query = "SELECT * FROM " + name + this.query;
return this;
}
var select = "SELECT ";
var i = 0;
for (var key in cols) {
if (typeof cols[key] === "string")
select += cols[key];
else
select += cols[key][1] + " AS " + cols[key][0] + "";
if (++i != cols.length)
select += ", ";
}
select += " FROM " + name;
this.query = select + this.query;
return this;
}
/**
* Tablo ismine ve değerlerine göre join çeker
* @param array table
* @param string equals
*/
this.join = function(table, equals) {
if (!table || !equals) {
throw new Error("table or equals exists");
}
var name = this.tableName;
this.query += " LEFT JOIN " + table + " ON";
var i = 0;
for (var key in equals) {
if (equals[key] instanceof Object) {
if (typeof equals[key][0] === "string" && typeof equals[key][0] === "string") {
this.query += " " + equals[key][0] + " = " + equals[key][1];
} else {
this.query += " " + name + "." + objkeys(equals[key])[0] + " = " + table + "." + objvalues(equals[key]);
}
}
if (++i != objkeys(equals).length)
this.query += " AND";
}
return this;
}
/**
* Sorgunun Koşulu
* @param string where
*/
this.where = function (where, wValues = null) {
if (!where) {
throw new Error("where exists");
}
if (wValues != null) {
if (this.wValues != null && typeof this.wValues === 'object')
this.wValues = Object.assign(this.wValues, wValues);
else
this.wValues = wValues;
}
var values = objkeys(this.wValues);
var prLen = values.length;
if (wValues != null) {
prLen = prLen - objkeys(wValues).length + 1;
}
for(var i = 0; i < values.length; i++) {
if (where.match(values[i]) !== null) {
where = where.replace(new RegExp(values[i], 'g'), prepareType + (prLen++));
}
}
this.query += " WHERE " + where;
return this;
}
/**
* Sorguya group parametreleri ekler
* @param string values
*/
this.groupBy = function (values) {
if (!values) {
throw new Error("value exists");
}
this.query += " GROUP BY " + values;
return this;
}
/**
* Sorguya sıra parametreleri ekler
* @param string values
*/
this.orderBy = function (values) {
if (!values) {
throw new Error("value exists");
}
this.query += " ORDER BY " + values;
return this;
}
/**
* Sorgu sıralaması tipini desc yapar
*/
this.desc = function() {
this.query += " DESC";
return this;
}
/**
* Sorgu sıralaması tipini asc yapar
*/
this.asc = function() {
this.query += " ASC";
return this;
}
/**
* Sorgunun limitini belirler default olarak 10'dur.
* @param integer start
* @param integer end
*/
this.limit = function (start = 10, end = '') {
var limit = " LIMIT " + start;
if (end) {
limit += ", " + end;
}
this.query += limit;
return this;
}
/**
* Sorgunun offset değerini belirler
* @param integer offset
*/
this.offset = function (offset = 10) {
this.query += " OFFSET " + offset;
return this;
}
/**
* Sorgunun limitini belirler default olarak 10'dur.
* @param integer limit
*/
this.returning = function (text) {
this.query += " RETURNING " + text;
return this;
}
/**
* data içindeki keylere göre insert işlemi yapar
* @param integer limit
*/
this.insert = function (data) {
// çoklu insert
if (data instanceof Array && data[0] instanceof Object) {
this.query = "INSERT INTO " + this.tableName + " (" + objkeys(data[0]).join(', ') + ") VALUES ";
var values = {};
var prLen = this.wValues ? this.wValues.length : 0;
for (var i = 0; i < data.length; i++) {
var row = [];
for (var key in data[i]) {
values[':' + key + i] = xssFilter ? xssClear(data[i][key]) : data[i][key];
if (prepareType === 'tag')
row.push(':' + key + i);
else
row.push(prepareType + (++prLen));
}
this.query += "(" + row.join(',') + ")";
if (i != data.length - 1)
this.query += ", ";
}
if (this.wValues && typeof this.wValues === 'object')
this.wValues = Object.assign(this.wValues, values);
else
this.wValues = values;
}
// tekli insert
else {
this.query = "INSERT INTO " + this.tableName + " (" + objkeys(data).join(', ') + ") VALUES(";
var values = {};
var i = 0, prLen = this.wValues ? this.wValues.length : 0;
for (var key in data) {
values[':' + key] = xssFilter ? xssClear(data[key]) : data[key];
if (prepareType === 'tag')
this.query += ":" + key;
else
this.query += prepareType + (++prLen);
if (i++ != objvalues(data).length - 1)
this.query += ", ";
}
this.query += ")"
if (this.wValues && typeof this.wValues === 'object')
this.wValues = Object.assign(this.wValues, values);
else
this.wValues = values;
}
return this;
}
/**
* data içindeki keylere göre insert işlemi yapar
* where işlemi ilede hangi sütunların işleme tutulacağı belirlenir
* @param integer limit
*/
this.update = function (data) {
this.query = "UPDATE " + this.tableName + " SET";
if (typeof data === 'string') {
this.query += " " + data;
return this;
}
var values = {};
var i = 0, prLen = this.wValues ? this.wValues.length : 0;
for (var key in data) {
values[':' + key] = xssFilter ? xssClear(data[key]) : data[key];
if (this.wValues && typeof this.wValues === 'object')
this.wValues = Object.assign(this.wValues, values);
if (prepareType === 'tag')
this.query += " " + key + " = :" + key;
else
this.query += " " + key + " = " + prepareType + (++prLen);
if (i++ != objvalues(data).length - 1)
this.query += ",";
}
if (this.wValues && typeof this.wValues === 'object')
this.wValues = Object.assign(this.wValues, values);
else
this.wValues = values;
return this;
}
/**
* where ile koşullu silme yapar
* @param integer limit
*/
this.delete = function () {
this.query = "DELETE FROM " + this.tableName;
return this;
}
/**
* çıktıyı verir
* @param integer limit
*/
this.exec = function (type) {
type = type || '';
switch(type) {
default:
var text = this.query.trim();
var values = objvalues(this.wValues);
var objectValues = {};
values.forEach((x, i) => {
objectValues[prepareType + (i + 1)] = x;
});
clear();
return {
text: text,
values: values,
objectValues: objectValues
};
}
}
};
var SQLMaster = function () {
var options = {
xssFilter: true,
};
this.from = function (tableName, customOpt) {
return new SQLMasterProvider(customOpt || options).from(tableName);
};
this.init = function (obj) {
options = obj;
};
}
module.exports = new SQLMaster();