Fix having and where methods
This commit is contained in:
parent
b52f0762e4
commit
1f03c8c0dd
@ -305,7 +305,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-381">line 381</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-388">line 388</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -550,7 +550,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-708">line 708</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-719">line 719</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -741,7 +741,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-818">line 818</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-829">line 829</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -940,7 +940,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-796">line 796</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-807">line 807</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -1141,7 +1141,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-780">line 780</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-791">line 791</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -1332,7 +1332,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-807">line 807</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-818">line 818</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -1427,7 +1427,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-328">line 328</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-335">line 335</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -1574,7 +1574,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-591">line 591</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-602">line 602</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -1661,7 +1661,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-689">line 689</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-700">line 700</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -1748,7 +1748,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-653">line 653</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-664">line 664</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -1933,7 +1933,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-454">line 454</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-461">line 461</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -2143,7 +2143,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-731">line 731</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-742">line 742</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -2408,7 +2408,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-577">line 577</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-588">line 588</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -2634,7 +2634,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-403">line 403</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-410">line 410</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -2813,7 +2813,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-641">line 641</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-652">line 652</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -3039,7 +3039,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-416">line 416</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-423">line 423</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -3230,7 +3230,7 @@
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-614">line 614</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-625">line 625</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -3318,7 +3318,7 @@ prefixed with 'OR'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-666">line 666</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-677">line 677</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -3503,7 +3503,7 @@ prefixed with 'OR'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-466">line 466</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-475">line 475</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -3729,7 +3729,7 @@ prefixed with 'OR'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-429">line 429</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-436">line 436</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -3817,7 +3817,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-678">line 678</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-689">line 689</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -4043,7 +4043,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-442">line 442</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-449">line 449</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -4228,7 +4228,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-490">line 490</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-501">line 501</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -4387,7 +4387,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-514">line 514</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-525">line 525</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -4546,7 +4546,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-538">line 538</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-549">line 549</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -4685,7 +4685,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-347">line 347</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-354">line 354</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -4867,7 +4867,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-550">line 550</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-561">line 561</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -5077,7 +5077,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-748">line 748</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-759">line 759</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -5262,7 +5262,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-478">line 478</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-489">line 489</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -5421,7 +5421,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-502">line 502</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-513">line 513</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
@ -5580,7 +5580,7 @@ prefixed with 'OR NOT'</p>
|
||||
<ul class="dummy">
|
||||
<li>
|
||||
<a href="query-builder.js.html">query-builder.js</a>,
|
||||
<a href="query-builder.js.html#sunlight-1-line-526">line 526</a>
|
||||
<a href="query-builder.js.html#sunlight-1-line-537">line 537</a>
|
||||
</li>
|
||||
</ul>
|
||||
</dd>
|
||||
|
@ -72,7 +72,7 @@
|
||||
class="sunlight-highlight-javascript linenums">'use strict';
|
||||
|
||||
/** @module query-builder */
|
||||
|
||||
require('./es6-polyfill');
|
||||
var getArgs = require('getargs'),
|
||||
helpers = require('./helpers');
|
||||
|
||||
@ -218,12 +218,12 @@ var QueryBuilder = function(driver, adapter) {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
mixedSet: function(/* varName, key, [val], valType */) {
|
||||
var args = getArgs('varName:string, key:string|array, [val]:string, valType:string', arguments);
|
||||
mixedSet: function(/* varName, valType, key, [val] */) {
|
||||
var args = getArgs('varName:string, valType:string, key:string|object, [val]:string|number|boolean', arguments);
|
||||
|
||||
var obj = {};
|
||||
|
||||
if (helpers.isString(args.key) && helpers.isString(args.val))
|
||||
if (helpers.isScalar(args.key) && !helpers.isUndefined(args.val) && !helpers.isNull(args.val))
|
||||
{
|
||||
obj[args.key] = args.val;
|
||||
}
|
||||
@ -233,11 +233,11 @@ var QueryBuilder = function(driver, adapter) {
|
||||
}
|
||||
|
||||
Object.keys(obj).forEach(function(k) {
|
||||
if (args.valType != 'both')
|
||||
// If a single value for the return
|
||||
if (['key','value'].indexOf(args.valType) !== -1)
|
||||
{
|
||||
state[args.varName].push(
|
||||
(args.valType === 'key')? k: obj[k]
|
||||
);
|
||||
var pushVal = (args.valType === 'key') ? k : obj[k];
|
||||
state[args.varName].push(pushVal);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -247,14 +247,18 @@ var QueryBuilder = function(driver, adapter) {
|
||||
|
||||
return state[args.varName];
|
||||
},
|
||||
whereMixedSet: function(key, val) {
|
||||
_p.mixedSet('whereValues', key, val, 'value');
|
||||
_p.mixedSet('whereMap', key, val, 'both');
|
||||
whereMixedSet: function(/*key, val*/) {
|
||||
var args = getArgs('key:string|object, [val]', arguments);
|
||||
|
||||
state.whereMap = [];
|
||||
|
||||
_p.mixedSet('whereMap', 'both', args.key, args.val);
|
||||
_p.mixedSet('whereValues', 'value', args.key, args.val);
|
||||
},
|
||||
where: function(key, val, conj) {
|
||||
conj = conj || 'AND';
|
||||
|
||||
// Get an object to iterate over
|
||||
// Normalize key and value and insert into state.whereMap
|
||||
_p.whereMixedSet(key, val);
|
||||
|
||||
Object.keys(state.whereMap).forEach(function(field) {
|
||||
@ -271,7 +275,7 @@ var QueryBuilder = function(driver, adapter) {
|
||||
lastItem = state.queryMap[state.queryMap.length - 1];
|
||||
|
||||
// Determine the correct conjunction
|
||||
if (state.queryMap.length < 1 || firstItem.contains('JOIN'))
|
||||
if (state.queryMap.length < 1 || firstItem.conjunction.contains('JOIN'))
|
||||
{
|
||||
conj = " WHERE ";
|
||||
}
|
||||
@ -287,10 +291,13 @@ var QueryBuilder = function(driver, adapter) {
|
||||
_p.appendMap(conj, item, 'where');
|
||||
});
|
||||
},
|
||||
having: function(key, val, conj) {
|
||||
conj = conj || 'AND';
|
||||
having: function(/*key, val, conj*/) {
|
||||
var args = getArgs('key:string|object, [val]:string|number, [conj]:string', arguments);
|
||||
args.conj = args.conj || 'AND';
|
||||
args.val = args.val || null;
|
||||
|
||||
_p.whereMixedSet(key, val);
|
||||
// Normalize key/val and put in state.whereMap
|
||||
_p.whereMixedSet(args.key, args.val);
|
||||
|
||||
Object.keys(state.whereMap).forEach(function(field) {
|
||||
// Split each key by spaces, in case there
|
||||
@ -304,7 +311,7 @@ var QueryBuilder = function(driver, adapter) {
|
||||
|
||||
// Put in the having map
|
||||
state.havingMap.push({
|
||||
conjunction: (state.havingMap.length > 0) ? " " + conj + " " : ' HAVING ',
|
||||
conjunction: (state.havingMap.length > 0) ? " " + args.conj + " " : ' HAVING ',
|
||||
string: item
|
||||
});
|
||||
});
|
||||
@ -353,7 +360,7 @@ var QueryBuilder = function(driver, adapter) {
|
||||
resetState: function() {
|
||||
state = {
|
||||
// Arrays/Maps
|
||||
queryMap: {},
|
||||
queryMap: [],
|
||||
values: [],
|
||||
whereValues: [],
|
||||
setArrayKeys: [],
|
||||
@ -522,8 +529,10 @@ var QueryBuilder = function(driver, adapter) {
|
||||
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
||||
* @return this
|
||||
*/
|
||||
this.having = function(key, val) {
|
||||
_p.having(key, val, 'AND');
|
||||
this.having = function(/*key, [val]*/) {
|
||||
var args = getArgs('key:string|object, [val]:string|number', arguments);
|
||||
|
||||
_p.having(args.key, args.val, 'AND');
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -534,8 +543,10 @@ var QueryBuilder = function(driver, adapter) {
|
||||
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
||||
* @return this
|
||||
*/
|
||||
this.orHaving = function(key, val) {
|
||||
_p.having(key, val, 'OR');
|
||||
this.orHaving = function(/*key, [val]*/) {
|
||||
var args = getArgs('key:string|object, [val]:string|number', arguments);
|
||||
|
||||
_p.having(args.key, args.val, 'OR');
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -622,8 +633,8 @@ var QueryBuilder = function(driver, adapter) {
|
||||
var args = getArgs('key:string|object, [val]:string', arguments);
|
||||
|
||||
// Set the appropriate state variables
|
||||
_p.mixedSet('setArrayKeys', args.key, args.val, 'key');
|
||||
_p.mixedSet('values', args.key, args.val, 'value');
|
||||
_p.mixedSet('setArrayKeys', 'key', args.key, args.val);
|
||||
_p.mixedSet('values', 'value', args.key, args.val);
|
||||
|
||||
// Use the keys of the array to make the insert/update string
|
||||
// and escape the field names
|
||||
|
@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
/** @module query-builder */
|
||||
|
||||
var getArgs = require('getargs'),
|
||||
helpers = require('./helpers');
|
||||
|
||||
@ -147,12 +146,12 @@ var QueryBuilder = function(driver, adapter) {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
mixedSet: function(/* varName, key, [val], valType */) {
|
||||
var args = getArgs('varName:string, key:string|array, [val]:string, valType:string', arguments);
|
||||
mixedSet: function(/* varName, valType, key, [val] */) {
|
||||
var args = getArgs('varName:string, valType:string, key:string|object, [val]:string|number|boolean', arguments);
|
||||
|
||||
var obj = {};
|
||||
|
||||
if (helpers.isString(args.key) && helpers.isString(args.val))
|
||||
if (helpers.isScalar(args.key) && !helpers.isUndefined(args.val) && !helpers.isNull(args.val))
|
||||
{
|
||||
obj[args.key] = args.val;
|
||||
}
|
||||
@ -162,11 +161,11 @@ var QueryBuilder = function(driver, adapter) {
|
||||
}
|
||||
|
||||
Object.keys(obj).forEach(function(k) {
|
||||
if (args.valType != 'both')
|
||||
// If a single value for the return
|
||||
if (['key','value'].indexOf(args.valType) !== -1)
|
||||
{
|
||||
state[args.varName].push(
|
||||
(args.valType === 'key')? k: obj[k]
|
||||
);
|
||||
var pushVal = (args.valType === 'key') ? k : obj[k];
|
||||
state[args.varName].push(pushVal);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -176,14 +175,18 @@ var QueryBuilder = function(driver, adapter) {
|
||||
|
||||
return state[args.varName];
|
||||
},
|
||||
whereMixedSet: function(key, val) {
|
||||
_p.mixedSet('whereValues', key, val, 'value');
|
||||
_p.mixedSet('whereMap', key, val, 'both');
|
||||
whereMixedSet: function(/*key, val*/) {
|
||||
var args = getArgs('key:string|object, [val]', arguments);
|
||||
|
||||
state.whereMap = [];
|
||||
|
||||
_p.mixedSet('whereMap', 'both', args.key, args.val);
|
||||
_p.mixedSet('whereValues', 'value', args.key, args.val);
|
||||
},
|
||||
where: function(key, val, conj) {
|
||||
conj = conj || 'AND';
|
||||
|
||||
// Get an object to iterate over
|
||||
// Normalize key and value and insert into state.whereMap
|
||||
_p.whereMixedSet(key, val);
|
||||
|
||||
Object.keys(state.whereMap).forEach(function(field) {
|
||||
@ -200,7 +203,7 @@ var QueryBuilder = function(driver, adapter) {
|
||||
lastItem = state.queryMap[state.queryMap.length - 1];
|
||||
|
||||
// Determine the correct conjunction
|
||||
if (state.queryMap.length < 1 || firstItem.contains('JOIN'))
|
||||
if (state.queryMap.length < 1 || firstItem.conjunction.contains('JOIN'))
|
||||
{
|
||||
conj = " WHERE ";
|
||||
}
|
||||
@ -216,10 +219,13 @@ var QueryBuilder = function(driver, adapter) {
|
||||
_p.appendMap(conj, item, 'where');
|
||||
});
|
||||
},
|
||||
having: function(key, val, conj) {
|
||||
conj = conj || 'AND';
|
||||
having: function(/*key, val, conj*/) {
|
||||
var args = getArgs('key:string|object, [val]:string|number, [conj]:string', arguments);
|
||||
args.conj = args.conj || 'AND';
|
||||
args.val = args.val || null;
|
||||
|
||||
_p.whereMixedSet(key, val);
|
||||
// Normalize key/val and put in state.whereMap
|
||||
_p.whereMixedSet(args.key, args.val);
|
||||
|
||||
Object.keys(state.whereMap).forEach(function(field) {
|
||||
// Split each key by spaces, in case there
|
||||
@ -233,7 +239,7 @@ var QueryBuilder = function(driver, adapter) {
|
||||
|
||||
// Put in the having map
|
||||
state.havingMap.push({
|
||||
conjunction: (state.havingMap.length > 0) ? " " + conj + " " : ' HAVING ',
|
||||
conjunction: (state.havingMap.length > 0) ? " " + args.conj + " " : ' HAVING ',
|
||||
string: item
|
||||
});
|
||||
});
|
||||
@ -282,7 +288,7 @@ var QueryBuilder = function(driver, adapter) {
|
||||
resetState: function() {
|
||||
state = {
|
||||
// Arrays/Maps
|
||||
queryMap: {},
|
||||
queryMap: [],
|
||||
values: [],
|
||||
whereValues: [],
|
||||
setArrayKeys: [],
|
||||
@ -451,8 +457,10 @@ var QueryBuilder = function(driver, adapter) {
|
||||
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
||||
* @return this
|
||||
*/
|
||||
this.having = function(key, val) {
|
||||
_p.having(key, val, 'AND');
|
||||
this.having = function(/*key, [val]*/) {
|
||||
var args = getArgs('key:string|object, [val]:string|number', arguments);
|
||||
|
||||
_p.having(args.key, args.val, 'AND');
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -463,8 +471,10 @@ var QueryBuilder = function(driver, adapter) {
|
||||
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
||||
* @return this
|
||||
*/
|
||||
this.orHaving = function(key, val) {
|
||||
_p.having(key, val, 'OR');
|
||||
this.orHaving = function(/*key, [val]*/) {
|
||||
var args = getArgs('key:string|object, [val]:string|number', arguments);
|
||||
|
||||
_p.having(args.key, args.val, 'OR');
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -551,8 +561,8 @@ var QueryBuilder = function(driver, adapter) {
|
||||
var args = getArgs('key:string|object, [val]:string', arguments);
|
||||
|
||||
// Set the appropriate state variables
|
||||
_p.mixedSet('setArrayKeys', args.key, args.val, 'key');
|
||||
_p.mixedSet('values', args.key, args.val, 'value');
|
||||
_p.mixedSet('setArrayKeys', 'key', args.key, args.val);
|
||||
_p.mixedSet('values', 'value', args.key, args.val);
|
||||
|
||||
// Use the keys of the array to make the insert/update string
|
||||
// and escape the field names
|
||||
|
@ -44,10 +44,36 @@ module.exports = (function() {
|
||||
'Get with limit and offset': function(test) {
|
||||
base.qb.get('create_test', 2, 1, base.testCallback.bind(test, test));
|
||||
test.done();
|
||||
},
|
||||
'Test get with having': function(test) {
|
||||
base.qb.select('id')
|
||||
.from('create_test')
|
||||
.groupBy('id')
|
||||
.having({'id >':1})
|
||||
.having('id !=', 3)
|
||||
.get(base.testCallback.bind(test, test));
|
||||
|
||||
test.done();
|
||||
},
|
||||
"Test get with 'orHaving'": function(test) {
|
||||
base.qb.select('id')
|
||||
.from('create_test')
|
||||
.groupBy('id')
|
||||
.having({'id >':1})
|
||||
.orHaving('id !=', 3)
|
||||
.get(base.testCallback.bind(test, test));
|
||||
|
||||
test.done();
|
||||
}
|
||||
},
|
||||
'Select tests' : {
|
||||
'Select where get': function(test) {
|
||||
base.qb.select('id, key as k, val')
|
||||
.where('id !=', 1)
|
||||
.get('create_test', 2, 1, base.testCallback.bind(test, test));
|
||||
|
||||
test.done();
|
||||
}
|
||||
},
|
||||
'Grouping tests' : {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user