This repository was archived by the owner on May 3, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathzsql.js
More file actions
589 lines (510 loc) · 12.4 KB
/
Copy pathzsql.js
File metadata and controls
589 lines (510 loc) · 12.4 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/**
* Utility function to quote identifier
*/
function quoteIdentifier(identifier) {
return '`' + identifier.replace('`', '``').replace('.', '`.`') + '`';
}
module.exports.quoteIdentifier = quoteIdentifier;
/**
* Utility function to quote identifier or convert to string if an Expr object
*/
function qexpr(expr) {
if( expr instanceof Expr ) {
return expr.toString();
} else {
return quoteIdentifier(expr);
}
}
module.exports.qexpr = qexpr;
/**
* http://phpjs.org/functions/substr_count/
*/
function substr_count(haystack, needle, offset, length) {
var cnt = 0;
haystack += '';
needle += '';
if (isNaN(offset)) {
offset = 0;
}
if (isNaN(length)) {
length = 0;
}
if( needle.length == 0 ) {
return false;
}
offset--;
while ((offset = haystack.indexOf(needle, offset + 1)) != -1) {
if (length > 0 && (offset + needle.length) > length) {
return false;
}
cnt++;
}
return cnt;
}
module.exports.substr_count = substr_count;
/**
* Expr
*/
var Expr = module.exports.Expr = function(expr) {
this.expr = expr;
}
module.exports.expr = function(expr) {
return new Expr(expr);
}
Expr.prototype.toString = function() {
return this.expr;
}
/**
* Query
*/
function Query() {}
module.exports.Query = Query;
Query.prototype.mysql = function(mysql) {
this._mysql = mysql;
return this;
}
Query.prototype.table = function(table, columns) {
if( table instanceof Expr ) {
this._table = table;
} else {
this._table = String(table);
}
if( columns ) {
this.columns(columns);
}
return this;
}
Query.prototype.from = Query.prototype.table;
Query.prototype.params = function() {
if( this._params && this._params.length ) {
return this._params;
} else {
return undefined;
}
}
Query.prototype.all = function(callback) {
var arr = [this.toString()];
if( this._params && this._params.length ) {
arr.push(this._params);
}
if( callback ) {
arr.push(callback);
}
return arr;
}
Query.prototype.execute = function(callback) {
if( !this._mysql ) {
callback(new Error('No mysql object provided'), null);
} else {
this._mysql.execute.apply(this._mysql, this.all(callback));
}
return this;
}
Query.prototype._push = function(str) {
this._parts.push(str);
return this;
}
Query.prototype._pushTable = function() {
if( this._table instanceof Expr ) {
this._parts.push(this._table);
} else {
this._parts.push(quoteIdentifier(this._table));
}
return this;
}
/**
* ExtendedQuery
*/
function ExtendedQuery() {}
module.exports.ExtendedQuery = ExtendedQuery;
ExtendedQuery.prototype = new Query;
ExtendedQuery.prototype.where = function(where, val) {
if( arguments.length >= 2 ) {
this._where.push([where, val]);
} else if( arguments.length >= 1 ) {
this._where.push([where]);
}
return this;
}
ExtendedQuery.prototype.whereIn = function(where, val) {
if( !(val instanceof Array) ) {
val = [val];
}
if( val.length <= 0 ) {
this._where.push([new Expr('FALSE')]);
} else {
this._where.push([where, val, 'IN']);
}
return this;
}
ExtendedQuery.prototype.whereExpr = function(where) {
this._where.push([new Expr(String(where))]);
return this;
}
ExtendedQuery.prototype.group = function(cols) {
this._group = cols;
return this;
}
ExtendedQuery.prototype.order = function(order, direction) {
this._order = order;
this._direction = direction;
return this;
}
ExtendedQuery.prototype.limit = function(limit, offset) {
this._limit = parseInt(limit);
if( typeof(offset) != 'undefined' ) {
this._offset = offset;
}
return this;
}
ExtendedQuery.prototype.offset = function(offset, limit) {
this._offset = parseInt(offset);
if( typeof(limit) != 'undefined' ) {
this._limit = parseInt(limit);
}
return this;
}
ExtendedQuery.prototype._pushWhere = function() {
if( this._where.length <= 0 ) {
return this;
}
this._parts.push('WHERE');
this._where.forEach(function(w) {
var where = w[0];
var val = w[1];
if( where instanceof Expr ) {
this._parts.push(String(where));
} else if( w.length == 3 ) {
//if( w[2] == 'IN' ) {
this._parts.push(quoteIdentifier(where));
this._parts.push(w[2]);
var tmp = '';
for( var i = 0; i < w[1].length; i++ ) {
tmp += '?, ';
}
this._parts.push('(' + tmp.substring(0, tmp.length - 2) + ')');
w[1].forEach(function(v) {
this._params.push(v);
}.bind(this));
//}
} else {
var cnt = substr_count(where, '?');
// Push where
if( cnt <= 0 ) {
this._parts.push(quoteIdentifier(where));
this._parts.push('=');
this._parts.push('?');
this._params.push(val);
} else {
this._parts.push(where);
this._params.push(val);
}
}
this._parts.push('&&');
}.bind(this));
this._parts.pop();
return this;
}
ExtendedQuery.prototype._pushGroup = function() {
if( this._group ) {
this._parts.push('GROUP BY');
this._parts.push(quoteIdentifier(this._group));
}
return this;
}
ExtendedQuery.prototype._pushOrder = function() {
if( this._order ) {
this._parts.push('ORDER BY');
this._parts.push(qexpr(this._order));
this._parts.push(this._direction == 'DESC' ? 'DESC' : 'ASC');
}
return this;
}
ExtendedQuery.prototype._pushLimit = function() {
if( null !== this._limit ) {
if( null !== this._offset ) {
this._parts.push('LIMIT ?, ?');
this._params.push(this._offset);
this._params.push(this._limit);
} else {
this._parts.push('LIMIT ?');
this._params.push(this._limit);
}
}
return this;
}
/**
* Select
*/
function Select(mysql) {
this._mysql = mysql;
this._table = null;
this._columns = null;
this._where = [];
this._group = null;
this._limit = null;
this._offset = null;
this._order = null;
this._direction = null;
this._parts = [];
this._params = [];
}
Select.prototype = new ExtendedQuery;
module.exports.Select = Select;
module.exports.select = function(mysql) {
return new Select(mysql);
}
Select.prototype.columns = function(columns) {
if( columns && (columns instanceof Array ||
columns instanceof Expr ||
typeof(columns) == 'string') ) {
this._columns = columns;
} else {
throw new Error('Invalid columns spec');
}
return this;
}
Select.prototype.hint = function(columns, mode) {
this._hint = columns;
this._hintMode = mode;
return this;
}
Select.prototype.distinct = function(distinct) {
if( arguments.length == 0 ) {
distinct = true;
}
this._distinct = Boolean(distinct);
return this;
}
Select.prototype._pushDistinct = function() {
if( this._distinct ) {
this._parts.push('DISTINCT');
}
return this;
}
Select.prototype._pushColumns = function() {
if( !this._columns ) {
this._parts.push('*');
} else if( this._columns instanceof Array ) {
var cols = [];
this._columns.forEach(function(col) {
cols.push(qexpr(col));
});
this._parts.push(cols.join(', '));
} else if( typeof(this._columns) == 'string' ) {
this._parts.push(quoteIdentifier(this._columns));
} else if( this._columns instanceof Expr ) {
this._parts.push(this._columns.toString());
}
return this;
}
Select.prototype._pushHint = function() {
if( this._hint ) {
this._parts.push(this._hintMode || 'USE');
this._parts.push('INDEX');
this._parts.push('(' + quoteIdentifier(this._hint) + ')');
}
return this;
}
Select.prototype.toString = function() {
this._parts = [];
this._params = [];
// Check table
if( !this._table ) {
throw Error('No table set in select object');
}
this._push('SELECT')
._pushDistinct()
._pushColumns()
._push('FROM')
._pushTable()
._pushHint()
._pushWhere()
._pushGroup()
._pushOrder()
._pushLimit();
return this._parts.join(' ');
}
Select.prototype.get_one = function(callback) {
if( !this._mysql ) {
callback(new Error('No mysql object provided'), null);
} else {
this._mysql.get_one.apply(this._mysql, this.all(callback));
}
return this;
}
Select.prototype.getOne = Select.prototype.get_one;
Select.prototype.fetchRow = Select.prototype.get_one;
Select.prototype.get_all = function(callback) {
if( !this._mysql ) {
callback(new Error('No mysql object provided'), null);
} else {
this._mysql.get_all.apply(this._mysql, this.all(callback));
}
return this;
}
Select.prototype.getAll = Select.prototype.get_all;
Select.prototype.fetchAll = Select.prototype.get_all;
Select.prototype.get_column = function(callback) {
if( !this._mysql ) {
callback(new Error('No mysql object provided'), null);
} else {
this.get_one(function(err, result) {
if( err ) {
callback(err, result);
} else {
callback(err, result[Object.keys(result)[0]]);
}
})
}
return this;
}
Select.prototype.getColumn = Select.prototype.get_column;
Select.prototype.fetchColumn = Select.prototype.get_column;
/**
* Insert
*/
function Insert(mysql) {
this._mysql = mysql;
this._table = null;
this._values = {};
this._parts = [];
this._params = [];
}
Insert.prototype = new Query;
module.exports.Insert = Insert;
module.exports.insert = function(mysql) {
return new Insert(mysql);
}
Insert.prototype.into = Insert.prototype.table;
Insert.prototype.value = function(key, value) {
this._values[key] = value;
return this;
}
Insert.prototype.values = function(values) {
this._values = values;
return this;
}
Insert.prototype.toString = function() {
var keys = [];
var vals = [];
var params = [];
Object.keys(this._values).forEach(function(key) {
if( Object.prototype.hasOwnProperty.call(this._values, key) ) {
var val = this._values[key];
keys.push(quoteIdentifier(key));
if( val instanceof Expr ) {
vals.push(val.toString());
} else {
vals.push('?');
params.push(val);
}
}
}.bind(this));
this._params = params;
return 'INSERT INTO ' + qexpr(this._table) + ' ('
+ keys.join(', ')
+ ') VALUES ('
+ vals.join(', ')
+ ')';
}
/**
* Delete
*/
function Delete(mysql) {
this._mysql = mysql;
this._table = null;
this._where = [];
this._limit = null;
this._offset = null;
this._order = null;
this._direction = null;
this._parts = [];
this._params = [];
}
Delete.prototype = new ExtendedQuery;
module.exports.Delete = Delete;
module.exports.del = function(mysql) {
return new Delete(mysql);
}
module.exports['delete'] = module.exports.del;
Delete.prototype.toString = function() {
this._parts = [];
this._params = [];
// Check table
if( !this._table ) {
throw Error('No table set in select object');
}
this._push('DELETE')
._push('FROM')
._pushTable()
._pushWhere()
._pushOrder()
._pushLimit();
return this._parts.join(' ');
}
/**
* Update
*/
function Update(mysql) {
this._mysql = mysql;
this._table = null;
this._values = {};
this._where = [];
this._limit = null;
this._offset = null;
this._order = null;
this._direction = null;
this._parts = [];
this._params = [];
}
Update.prototype = new ExtendedQuery;
module.exports.Update = Update;
module.exports.update = function(mysql) {
return new Update(mysql);
}
Update.prototype.values = function(values) {
this._values = values;
return this;
}
Update.prototype.value = function(key, value) {
this._values[key] = value;
return this;
}
Update.prototype.set = Update.prototype.value;
Update.prototype._pushValues = function() {
Object.keys(this._values).forEach(function(key) {
if( Object.prototype.hasOwnProperty.call(this._values, key) ) {
var val = this._values[key];
this._parts.push(qexpr(key));
this._parts.push('=');
if( val instanceof Expr ) {
this._parts.push(val.toString());
} else {
this._parts.push('?');
this._params.push(val);
}
}
this._parts.push(',');
}.bind(this));
this._parts.pop();
return this;
}
Update.prototype.toString = function() {
this._parts = [];
this._params = [];
// Check table
if( !this._table ) {
throw Error('No table set in object');
} else if( Object.keys(this._values).length <= 0 ) {
throw Error('No values set in object');
}
this._push('UPDATE')
._pushTable()
._push('SET')
._pushValues()
._pushWhere()
._pushOrder()
._pushLimit();
return this._parts.join(' ');
}