Skip to content
Browse files

The function DBSelect.where now has an optional third parameter that

specifies whether to use OR instead of AND for the current clause.

Added the function DBSelect.orWhere to give it parity with ZendDB.

Modified the function DBSelect.assemble to take advantage of this
feature.
  • Loading branch information...
1 parent b7ddfc3 commit ec670ebcfc9f8766e1d6a98c3c57fd14e5fc05bd @kalifg kalifg committed Aug 2, 2012
Showing with 20 additions and 4 deletions.
  1. +20 −4 lib/dbSelect.js
View
24 lib/dbSelect.js
@@ -88,9 +88,12 @@ DBSelect.prototype.from = function( tableName, fieldsArray )
* @param {String|Number|Array|null} value
* @returns {DBSelect}
*/
-DBSelect.prototype.where = function( whereStr, value )
+DBSelect.prototype.where = function( whereStr, value, orClause )
{
-
+ if (typeof orClause === 'undefined') {
+ orClause = false;
+ }
+
if( ! whereStr )
throw new Error('WHERE String is mandatory !');
@@ -103,12 +106,20 @@ DBSelect.prototype.where = function( whereStr, value )
whereStr = whereStr.replace( /\?/g, value );
}
- this._where.push( whereStr );
+ this._where.push( { orClause: orClause, string: whereStr } );
return this;
};
+/**
+ * Adds a WHERE clause with an OR instead of AND.
+ *
+ * @returns {DBSelect}
+ */
+DBSelect.prototype.orWhere = function(whereStr, value) {
+ return this.where(whereStr, value, true);
+};
/**
* Adds a LIMIT clause.
@@ -267,7 +278,12 @@ DBSelect.prototype.assemble = DBSelect.prototype.toString = function()
if( this._where.length > 0 )
{
this._where = _.unique( this._where );
- sql += ( (completeQuery) ? ' WHERE ' : '' ) + this._where.join(' AND ');
+ sql += ( (completeQuery) ? ' WHERE ' : '' ) + this._where[0].string;
+
+ for (var i = 1; i < this._where.length; i++) {
+ sql += ' ' + (this._where[i].orClause ? 'OR' : 'AND') + ' '
+ + this._where[i].string;
+ }
}
// "GROUP BY" clause : this._group

0 comments on commit ec670eb

Please sign in to comment.
Something went wrong with that request. Please try again.