query-generator.js
26.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
'use strict';
var Utils = require('../../utils')
, DataTypes = require('./data-types')
, SqlGenerator = require('./sql-generator')
, Model = require('../../model')
, _ = require('lodash')
, util = require('util');
module.exports = (function() {
var QueryGenerator = {
get options(){
return SqlGenerator.options;
},
set options (opt) {
SqlGenerator.options = opt;
},
get dialect(){
return SqlGenerator.dialect;
},
set dialect(dial) {
SqlGenerator.dialect = dial;
},
get sequelize(){
return SqlGenerator.sequelize;
},
set sequelize(seq) {
SqlGenerator.sequelize = seq;
},
addSchema: function(param) {
var self = this
, schema = (param.options && param.options.schema ? param.options.schema : undefined)
, schemaDelimiter = (param.options && param.options.schemaDelimiter ? param.options.schemaDelimiter : undefined);
if (!schema) return param.tableName || param;
return {
tableName: param.tableName || param,
table: param.tableName || param,
name: param.name || param,
schema: schema,
delimiter: schemaDelimiter || '.',
toString: function() {
return self.quoteTable(this);
}
};
},
/*
Returns a query for creating a table.
Parameters:
- tableName: Name of the new table.
- attributes: An object with containing attribute-attributeType-pairs.
Attributes should have the format:
{attributeName: type, attr2: type2}
--> e.g. {title: 'VARCHAR(255)'}
- options: An object with options.
Defaults: { engine: 'InnoDB', charset: null }
*/
/* istanbul ignore next */
createTableQuery: function(tableName, attributes, options) {
return SqlGenerator.getCreateTableSql(tableName, attributes, options);
},
renameTableQuery: function(before, after) {
throwMethodUndefined('renameTableQuery');
},
showTablesQuery: function () {
return SqlGenerator.showTableSql();
},
/*
Returns a rename table query.
Parameters:
- originalTableName: Name of the table before execution.
- futureTableName: Name of the table after execution.
*/
dropTableQuery: function(tableName, options) {
return SqlGenerator.dropTableSql(tableName,options);
},
addColumnQuery: function(tableName, key, dataType) {
var query = [
SqlGenerator.alterTableSql(tableName),
SqlGenerator.addColumnSql(key, dataType)
].join(' ') + ';';
return query;
},
removeColumnQuery: function(tableName, attributeName) {
var query = [
SqlGenerator.alterTableSql(tableName),
SqlGenerator.dropSql(attributeName)
].join(' ') + ';';
return query;
},
changeColumnQuery: function(tableName, attributes) {
var query = [
SqlGenerator.alterTableSql(tableName),
SqlGenerator.alterColumnSql(),
SqlGenerator.alterAttributesSql(attributes)
].join(' ') + ';';
return query;
},
renameColumnQuery: function(tableName, attrBefore, attributes) {
var newColumnName;
for (var attrName in attributes) {
newColumnName = attrName;
}
var query = [
SqlGenerator.renameColumnSql(tableName, attrBefore, newColumnName),
this.changeColumnQuery(tableName, attributes)
].join(' ');
return query;
},
/*
Returns an insert into command. Parameters: table name + hash of attribute-value-pairs.
*/
insertQuery: function(table, valueHash, modelAttributes, options) {
var modelAttributeMap = {};
if (modelAttributes) {
Utils._.each(modelAttributes, function(attribute, key) {
modelAttributeMap[key] = attribute;
if (attribute.field) {
modelAttributeMap[attribute.field] = attribute;
}
});
}
return SqlGenerator.insertSql(table,valueHash,modelAttributeMap);
},
/*
Returns an insert into command for multiple values.
Parameters: table name + list of hashes of attribute-value-pairs.
*/
/* istanbul ignore next */
bulkInsertQuery: function(tableName, attrValueHashes,options) {
var allAttributes = [],
ignoreKeys = options.fields;
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forOwn(attrValueHash, function(value, key, hash) {
if (allAttributes.indexOf(key) === -1) allAttributes.push(key);
if (value !== null && ignoreKeys.indexOf(key) > 0)
ignoreKeys.splice(ignoreKeys.indexOf(key),1);
});
});
for(var i = 0; i < ignoreKeys.length; i++){
allAttributes.splice(allAttributes.indexOf(ignoreKeys[i]), 1);
}
return SqlGenerator.bulkInsertSql(tableName, allAttributes, attrValueHashes,options);
},
/*
Returns an update query.
Parameters:
- tableName -> Name of the table
- values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
*/
updateQuery: function(tableName, attrValueHash, where, options, attributes) {
// if(where){
// throw new Error();
// }
//console.log('here', where);
var query = [
SqlGenerator.updateSql(tableName, attrValueHash, attributes),
'WHERE',
this.getWhereConditions(where)
].join(' ') + ';';
console.log(query);
return query;
},
/*
Returns a deletion query.
Parameters:
- tableName -> Name of the table
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
Options:
- limit -> Maximaum count of lines to delete
- truncate -> boolean - whether to use an 'optimized' mechanism (i.e. TRUNCATE) if available,
note that this should not be the default behaviour because TRUNCATE does not
always play nicely (e.g. InnoDB tables with FK constraints)
(@see http://dev.mysql.com/doc/refman/5.6/en/truncate-table.html).
Note that truncate must ignore limit and where
*/
/* istanbul ignore next */
deleteQuery: function(tableName, where, options) {
console.log('where:', where);
var query = SqlGenerator.deleteSql(tableName, where);
return query;
},
/*
Returns an update query.
Parameters:
- tableName -> Name of the table
- values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
*/
incrementQuery: function(tableName, attrValueHash, where, options) {
throwMethodUndefined('incrementQuery');
},
nameIndexes: function (indexes, rawTablename) {
return Utils._.map(indexes, function (index) {
if (!index.hasOwnProperty('name')) {
var onlyAttributeNames = index.fields.map(function(attribute) {
return (typeof attribute === 'string') ? attribute : attribute.attribute;
}.bind(this));
index.name = Utils.inflection.underscore(rawTablename + '_' + onlyAttributeNames.join('_'));
}
return index;
});
},
addIndexQuery: function(tableName, attributes, options, rawTablename) {
return SqlGenerator.addIndexSql(tableName, attributes, options, rawTablename);
},
showIndexQuery: function(tableName, options) {
return SqlGenerator.showIndexSql(tableName, options);
},
removeIndexQuery: function(tableName, indexNameOrAttributes) {
return SqlGenerator.removeIndexSql(tableName, indexNameOrAttributes);
},
attributesToSQL: function(attributes, options) {
var result = {}
, key
, attribute;
for (key in attributes) {
attribute = attributes[key];
if(key && !attribute.field)
attribute.field = key;
result[attribute.field || key] = SqlGenerator.attributeToSQL(attribute, options);
}
return result;
},
/*
Returns all auto increment fields of a factory.
*/
/* istanbul ignore next */
findAutoIncrementField: function(factory) {
var fields = [];
for (var name in factory.attributes) {
if (factory.attributes.hasOwnProperty(name)) {
var definition = factory.attributes[name];
if (definition && definition.autoIncrement) {
fields.push(name);
}
}
}
return fields;
},
quoteTable: function(param, as) {
throwMethodUndefined('quoteTable');
},
quote: function(obj, parent, force) {
throwMethodUndefined('quote');
},
/*
Create a trigger
*/
/* istanbul ignore next */
createTrigger: function(tableName, triggerName, timingType, fireOnArray, functionName, functionParams,
optionsArray) {
throwMethodUndefined('createTrigger');
},
/*
Drop a trigger
*/
/* istanbul ignore next */
dropTrigger: function(tableName, triggerName) {
throwMethodUndefined('dropTrigger');
},
/*
Rename a trigger
*/
/* istanbul ignore next */
renameTrigger: function(tableName, oldTriggerName, newTriggerName) {
throwMethodUndefined('renameTrigger');
},
/*
Create a function
*/
/* istanbul ignore next */
createFunction: function(functionName, params, returnType, language, body, options) {
throwMethodUndefined('createFunction');
},
describeTableQuery: function(tableName, schema, schemaDelimiter) {
return SqlGenerator.describeTableSql(tableName, schema, schemaDelimiter);
},
/*
Drop a function
*/
/* istanbul ignore next */
dropFunction: function(functionName, params) {
throwMethodUndefined('dropFunction');
},
/*
Rename a function
*/
/* istanbul ignore next */
renameFunction: function(oldFunctionName, params, newFunctionName) {
throwMethodUndefined('renameFunction');
},
/*
Escape an identifier (e.g. a table or attribute name)
*/
/* istanbul ignore next */
quoteIdentifier: function(identifier, force) {
throwMethodUndefined('quoteIdentifier');
},
/*
Split an identifier into .-separated tokens and quote each part
*/
quoteIdentifiers: function(identifiers, force) {
if (identifiers.indexOf('.') !== -1) {
identifiers = identifiers.split('.');
return SqlGenerator.quoteIdentifier(
identifiers.slice(0, identifiers.length - 1).join('.'))
+ '.' + SqlGenerator.quoteIdentifier(identifiers[identifiers.length - 1]);
} else {
return SqlGenerator.quoteIdentifier(identifiers);
}
},
/*
Escape a value (e.g. a string, number or date)
*/
escape: function(value, field) {
throwMethodUndefined('escape');
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
* @param {String} tableName The name of the table.
* @param {String} schemaName The name of the schema.
* @return {String} The generated sql query.
*/
getForeignKeysQuery: function(tableName, schemaName) {
return SqlGenerator.getForeignKeysSql(tableName);
},
/**
* Generates an SQL query that removes a foreign key from a table.
*
* @param {String} tableName The name of the table.
* @param {String} foreignKey The name of the foreign key constraint.
* @return {String} The generated sql query.
*/
dropForeignKeyQuery: function(tableName, foreignKey) {
return [
SqlGenerator.alterTableSql(tableName),
SqlGenerator.dropSql(foreignKey)
].join(' ') + ';';
},
/*
Returns a query for selecting elements in the table <tableName>.
Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
- where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer
OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own.
- order -> e.g. 'id DESC'
- group
- limit -> The maximum count you want to get.
- offset -> An offset value to start from. Only useable with limit!
*/
selectQuery: function(tableName, options, model) {
// Enter and change at your own peril -- Mick Hansen
//console.log('tablename', tableName);
//console.log('options', options);
//console.log('model', model.name);
//console.log(options.where);
options = options || {};
var query = [
SqlGenerator.getSelectorClause(model,options),
SqlGenerator.getFromClause(model.tableName, model.name)
];
if(options.include){
for(var i = 0; i < options.include.length; i ++){
query.push(SqlGenerator.getJoinClause(model, options.include[i]));
}
}
if(options.hasOwnProperty('where')){
query.push('WHERE');
query.push(this.getWhereConditions(options.where, model.name, model, options));
}
//console.log(query.join(' ') + ';');
return query.join(' ') + ';';
},
/**
* Returns a query that starts a transaction.
*
* @param {Boolean} value A boolean that states whether autocommit shall be done or not.
* @return {String} The generated sql query.
*/
setAutocommitQuery: function(value) {
return '';
//return 'SET autocommit = ' + (!!value ? 1 : 0) + ';';
},
/**
* Returns a query that sets the transaction isolation level.
*
* @param {String} value The isolation level.
* @param {Object} options An object with options.
* @return {String} The generated sql query.
*/
setIsolationLevelQuery: function(value, options) {
if (options.parent) {
return;
}
return 'SET TRANSACTION ISOLATION LEVEL ' + value + ';';
},
/**
* Returns a query that starts a transaction.
*
* @param {Transaction} transaction
* @param {Object} options An object with options.
* @return {String} The generated sql query.
*/
startTransactionQuery: function(transaction, options) {
if (options.parent) {
return 'SAVE TRANSACTION ' + this.quoteIdentifier(transaction.name) + ';';
}
return 'BEGIN TRANSACTION';
},
/**
* Returns a query that commits a transaction.
*
* @param {Object} options An object with options.
* @return {String} The generated sql query.
*/
commitTransactionQuery: function(options) {
if (options.parent) {
return;
}
return 'COMMIT TRANSACTION;';
},
/**
* Returns a query that rollbacks a transaction.
*
* @param {Transaction} transaction
* @param {Object} options An object with options.
* @return {String} The generated sql query.
*/
rollbackTransactionQuery: function(transaction, options) {
if (options.parent) {
return 'ROLLBACK TRANSACTION ' + this.quoteIdentifier(transaction.name) + ';';
}
return 'ROLLBACK TRANSACTION';
},
addLimitAndOffset: function(options, query) {
query = query || '';
if (options.offset && !options.limit) {
query += ' LIMIT ' + options.offset + ', ' + 10000000000000;
} else if (options.limit) {
if (options.offset) {
query += ' LIMIT ' + options.offset + ', ' + options.limit;
} else {
query += ' LIMIT ' + options.limit;
}
}
return query;
},
/*
Takes something and transforms it into values of a where condition.
*/
/*
Takes something and transforms it into values of a where condition.
*/
/*
Takes something and transforms it into values of a where condition.
*/
getWhereConditions: function(smth, tableName, factory, options, prepend) {
var result = null
, where = {}
, self = this;
if (Array.isArray(tableName)) {
tableName = tableName[0];
if (Array.isArray(tableName)) {
tableName = tableName[1];
}
}
options = options || {};
if (typeof prepend === 'undefined') {
prepend = true;
}
if (smth && smth._isSequelizeMethod === true) { // Checking a property is cheaper than a lot of instanceof calls
result = this.handleSequelizeMethod(smth, tableName, factory, options, prepend);
} else if (Utils._.isPlainObject(smth)) {
if (prepend) {
if (tableName) options.keysEscaped = true;
smth = this.prependTableNameToHash(tableName, smth);
}
result = this.hashToWhereConditions(smth, factory, options);
} else if (typeof smth === 'number') {
var primaryKeys = !!factory ? Object.keys(factory.primaryKeys) : [];
if (primaryKeys.length > 0) {
// Since we're just a number, assume only the first key
primaryKeys = primaryKeys[0];
} else {
primaryKeys = 'id';
}
where[primaryKeys] = smth;
if (tableName) options.keysEscaped = true;
smth = this.prependTableNameToHash(tableName, where);
result = this.hashToWhereConditions(smth);
} else if (typeof smth === 'string') {
result = smth;
} else if (Buffer.isBuffer(smth)) {
result = this.escape(smth);
} else if (Array.isArray(smth)) {
if (Utils.canTreatArrayAsAnd(smth)) {
var _smth = self.sequelize.and.apply(null, smth);
result = self.getWhereConditions(_smth, tableName, factory, options, prepend);
} else {
result = Utils.format(smth, this.dialect);
}
} else if (smth === null) {
result = '1=1';
}
return result ? result : '1=1';
},
// getWhereConditions: function(where, tableName, model, options, prepend) {
// //console.log('where:', model);
// console.log('logic', where);
// //console.log('options', options);
// if(where){
// return SqlGenerator.getWhereClause(where, tableName);
// }else{
// return '';
// }
// },
handleSequelizeMethod: function (smth, tableName, factory, options, prepend) {
var self = this
, result;
if ((smth instanceof Utils.and) || (smth instanceof Utils.or)) {
var connector = (smth instanceof Utils.and) ? ' AND ' : ' OR ';
result = smth.args.filter(function(arg) {
return arg !== undefined;
}).map(function(arg) {
return self.getWhereConditions(arg, tableName, factory, options, prepend);
}).join(connector);
result = result.length && '(' + result + ')' || undefined;
} else if (smth instanceof Utils.where) {
var value = smth.logic
, key
, logic
, _result = []
, _value;
if (smth.attribute._isSequelizeMethod) {
key = this.getWhereConditions(smth.attribute, tableName, factory, options, prepend);
} else {
key = this.quoteTable(smth.attribute.Model.name) + '.' + this.quoteIdentifier(smth.attribute.fieldName);
}
if (value._isSequelizeMethod) {
value = this.getWhereConditions(value, tableName, factory, options, prepend);
result = (value === 'NULL') ? key + ' IS NULL' : [key, value].join(smth.comparator);
} else if (_.isObject(value)) {
if (value.join) {
//using as sentinel for join column => value
result = [key, value.join].join('=');
} else {
for (logic in value) {
_result.push([key, this.escape(value[logic])].join(' ' + Utils.getWhereLogic(logic, value[logic]) + ' '));
}
result = _result.join(' AND ');
}
} else {
if (typeof value === 'boolean') {
value = this.booleanValue(value);
} else {
value = this.escape(value);
}
result = (value === 'NULL') ? key + ' IS NULL' : [key, value].join(' ' + smth.comparator + ' ');
}
} else if (smth instanceof Utils.literal) {
result = smth.val;
} else if (smth instanceof Utils.cast) {
if (smth.val._isSequelizeMethod) {
result = this.handleSequelizeMethod(smth.val, tableName, factory, options, prepend);
} else {
result = this.escape(smth.val);
}
result = 'CAST(' + result + ' AS ' + smth.type.toUpperCase() + ')';
} else if (smth instanceof Utils.fn) {
result = smth.fn + '(' + smth.args.map(function(arg) {
if (arg._isSequelizeMethod) {
return self.handleSequelizeMethod(arg, tableName, factory, options, prepend);
} else {
return self.escape(arg);
}
}).join(', ') + ')';
} else if (smth instanceof Utils.col) {
if (Array.isArray(smth.col)) {
if (!factory) {
throw new Error('Cannot call Sequelize.col() with array outside of order / group clause');
}
} else if (smth.col.indexOf('*') === 0) {
return '*';
}
return this.quote(smth.col, factory);
} else {
result = smth.toString(this, factory);
}
return result;
},
prependTableNameToHash: function(tableName, hash) {
if (tableName) {
var _hash = {};
for (var key in hash) {
if (key.indexOf('.') === -1) {
if (tableName instanceof Utils.literal) {
_hash[tableName.val + '.' + SqlGenerator.quoteIdentifier(key)] = hash[key];
} else {
_hash[SqlGenerator.quoteIdentifier(tableName) + '.' + SqlGenerator.quoteIdentifier(key)] = hash[key];
}
} else {
_hash[this.quoteIdentifiers(key)] = hash[key];
}
}
return _hash;
} else {
return hash;
}
},
findAssociation: function(attribute, dao) {
throwMethodUndefined('findAssociation');
},
getAssociationFilterDAO: function(filterStr, dao) {
throwMethodUndefined('getAssociationFilterDAO');
},
isAssociationFilter: function(filterStr, dao, options) {
if (!dao) {
return false;
}
var pattern = /^[a-z][a-zA-Z0-9]+(\.[a-z][a-zA-Z0-9]+)+$/;
if (!pattern.test(filterStr)) return false;
var associationParts = filterStr.split('.')
, attributePart = associationParts.pop()
, self = this;
return associationParts.every(function(attribute) {
var association = self.findAssociation(attribute, dao);
if (!association) return false;
dao = association.target;
return !!dao;
}) && dao.rawAttributes.hasOwnProperty(attributePart);
},
getAssociationFilterColumn: function(filterStr, dao, options) {
throwMethodUndefined('getAssociationFilterColumn');
},
getConditionalJoins: function(options, originalDao) {
throwMethodUndefined('getConditionalJoins');
},
arrayValue: function(value, key, _key, factory, logicResult) {
throwMethodUndefined('arrayValue');
},
/*
Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2
The values are transformed by the relevant datatype.
*/
hashToWhereConditions: function(hash, dao, options) {
var result = [];
options = options || {};
// Closures are nice
Utils._.each(hash, function(value, key) {
var _key
, _value = null;
if (value && value._isSequelizeMethod === true && (value instanceof Utils.literal)) {
result.push(value.val);
return;
}
if (options.keysEscaped) {
_key = key;
} else {
if (this.isAssociationFilter(key, dao, options)) {
_key = key = this.getAssociationFilterColumn(key, dao, options);
} else {
_key = this.quoteIdentifiers(key);
}
}
if (Array.isArray(value)) {
result.push(this.arrayValue(value, key, _key, dao, 'IN'));
} else if ((value) && (typeof value === 'object') && !(value instanceof Date) && !Buffer.isBuffer(value)) {
if (!!value.join) {
//using as sentinel for join column => value
_value = this.quoteIdentifiers(value.join);
result.push([_key, _value].join('='));
} else {
for (var logic in value) {
var logicResult = Utils.getWhereLogic(logic, hash[key][logic]);
if (logicResult === 'IN' || logicResult === 'NOT IN') {
var values = Array.isArray(value[logic]) ? value[logic] : [value[logic]];
result.push(this.arrayValue(values, key, _key, dao, logicResult));
}
else if (logicResult === 'BETWEEN' || logicResult === 'NOT BETWEEN') {
_value = SqlGenerator.escape(value[logic][0]);
var _value2 = SqlGenerator.escape(value[logic][1]);
result.push(' (' + _key + ' ' + logicResult + ' ' + _value + ' AND ' + _value2 + ') ');
} else {
_value = SqlGenerator.escape(value[logic]);
result.push([_key, _value].join(' ' + logicResult + ' '));
}
}
}
} else {
if (typeof value === 'boolean') {
_value = this.booleanValue(value);
} else {
_value = SqlGenerator.escape(value);
}
result.push((_value === 'NULL') ? _key + ' IS NULL' : [_key, _value].join('='));
}
}.bind(this));
return result.join(' AND ');
},
booleanValue: function(value) {
return value;
}
};
/* istanbul ignore next */
var throwMethodUndefined = function(methodName) {
throw new Error('The method "' + methodName + '" is not defined! Please add it to your sql dialect.');
};
return QueryGenerator;
})();