Skip to content
Browse files

Merge pull request #4 from kalifg/where-parenthesis

Where parenthesis
  • Loading branch information...
2 parents a4068c0 + bd30c66 commit ba044b12067ab96bd87353967ed8cc2c383f9433 @DrBenton committed
Showing with 58 additions and 19 deletions.
  1. +18 −5 README.md
  2. +2 −2 lib/dbSelect.js
  3. +38 −12 test/db-select.js
View
23 README.md
@@ -17,6 +17,7 @@ Node-DBI imitates the API of the great Open Source PHP database abstraction laye
The example below demonstates the Node-DBI usage:
(you can also look this [Gist](https://gist.github.com/923149) for the same code with syntax highlighting)
+
```javascript
var DBWrapper = require('node-dbi').DBWrapper;
var DBExpr = require('node-dbi').DBExpr;
@@ -125,17 +126,29 @@ All these methods returns exactly the sames results, whatever the chosen databas
Furthermore, Node-DBI provides a DBSelect class which allows easy and readable SQL "SELECT" Strings building. At the moment, it provides the following methods :
* __from( tableName, fieldsArray )__ : adds a table in the FROM clause, and adds its fields to the SELECT
- * __where( whereStr, value )__: adds a WHERE clause using AND ; if __value__ is not null, all the "?" occurences in __whereStr__ will be replaced with the safely escaped value
+ * __where( whereStr, value )__:
+ * adds a WHERE clause using AND
+ * if __value__ is not null, all the "?" occurences in __whereStr__ will be replaced with the safely escaped value
+ * __value__ may be an array, it will be mapped to a parenthesized SQL list
+ * the clause will be surrounded with parenthesis in the generated SQL, this way ```.where('id=? OR name=?')``` will work like it does in ZendDb.
* __orWhere( whereStr, value )__ : just like __where__ but adds a WHERE clause using OR
- * __whereGroup( num )__ : opens __num__ parenthetical groupings to WHERE clause (ie adds __num__ open parentheses) ; __num__ defaults to 1
+ * __whereGroup( num )__ :
+ * opens __num__ parenthetical groupings to WHERE clause (ie adds __num__ open parentheses)
+ * __num__ defaults to 1
* __whereGroupClose( num )__ :
* closes __num__ parenthetical groupings of WHERE clause (ie adds __num__ closed parentheses)
* __num__ defaults to 1
* will not close groups that do not exist
* open groups will be closed automatically
- * __limit( nbResults, startIndex )__ : set the LIMIT clause ; __startIndex__ param is optionnal
- * __order( fieldName, direction )__ : adds a ORDER BY clause ; if __direction__ is not set, it will be set to "ASC"
- * __join( tableName, joinStr, fieldsArray, joinType )__ : adds a JOIN clause ; if __joinType__ is not set, it will be set to "INNER"
+ * __limit( nbResults, startIndex )__ :
+ * set the LIMIT clause
+ * __startIndex__ param is optional
+ * __order( fieldName, direction )__ :
+ * adds a ORDER BY clause
+ * if __direction__ is not set, it will be set to "ASC"
+ * __join( tableName, joinStr, fieldsArray, joinType )__ :
+ * adds a JOIN clause
+ * if __joinType__ is not set, it will be set to "INNER"
* __distinct()__ : adds a DISTINCT() to the query
* __groupyBy( fieldName )__ : adds a GROUPY BY clause
* __assemble()__ : converts ou DBSelect object to an SQL SELECT string.
View
4 lib/dbSelect.js
@@ -102,7 +102,7 @@ DBSelect.prototype.where = function( whereStr, value, clauseType )
if( (clauseType == 'OR' || clauseType == 'AND') && whereStr && value ) {
if(_.isArray(value))
- value = value.map((function (v) { return this._adapter.escape(v); }).bind(this)).join(', ');
+ value = '(' + value.map((function (v) { return this._adapter.escape(v); }).bind(this)).join(', ') + ')';
else
value = this._adapter.escape(value);
@@ -364,7 +364,7 @@ DBSelect.prototype.assemble = DBSelect.prototype.toString = function()
}
startGroups = 0;
- sql += whereString;
+ sql += '(' + whereString + ')';
}
}
View
50 test/db-select.js
@@ -150,7 +150,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1)' );
}
},
@@ -167,7 +167,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10)' );
}
},
@@ -187,7 +187,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10 AND first_name=\'Dr.\' AND last_name LIKE \'%Benton%\' AND nickname='+dbWrapper.escape('"`\'éàèç') );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10) AND (first_name=\'Dr.\') AND (last_name LIKE \'%Benton%\') AND (nickname='+dbWrapper.escape('"`\'éàèç')+')' );
}
},
@@ -207,7 +207,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10 AND first_name=\'Dr.\' AND last_name LIKE \'%Benton%\' OR nickname='+dbWrapper.escape('"`\'éàèç') );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10) AND (first_name=\'Dr.\') AND (last_name LIKE \'%Benton%\') OR (nickname='+dbWrapper.escape('"`\'éàèç')+')' );
}
},
@@ -228,7 +228,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10 AND first_name=\'Dr.\' AND (last_name LIKE \'%Benton%\' OR nickname='+dbWrapper.escape('"`\'éàèç')+')' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10) AND (first_name=\'Dr.\') AND ((last_name LIKE \'%Benton%\') OR (nickname='+dbWrapper.escape('"`\'éàèç')+'))' );
}
},
@@ -254,7 +254,33 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (((enabled=1 AND id=10) AND first_name=\'Dr.\' AND (last_name LIKE \'%Benton%\' OR nickname='+dbWrapper.escape('"`\'éàèç')+')))' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE ((((enabled=1) AND (id=10)) AND (first_name=\'Dr.\') AND ((last_name LIKE \'%Benton%\') OR (nickname='+dbWrapper.escape('"`\'éàèç')+'))))' );
+ }
+
+ },
+
+ 'an advanced WHERE clause, with more parenthetical grouping, compound statements and an array value': {
+ topic: function()
+ {
+ return dbWrapper.getSelect()
+ .from('user')
+ .whereGroupClose() // Erroneous whereGroupClose to be handled
+ .whereGroup(3) // Multiple group start
+ .where('enabled=1')
+ .where( 'id=?', 10 )
+ .whereGroupClose()
+ .where( 'first_name=\'Dr.\' OR first_name IN ?', ['Bob', 'Mike'])
+ .whereGroup()
+ .where( 'last_name LIKE ?', '%Benton%' )
+ .orWhere( 'nickname=?', '"`\'éàèç' )
+ .whereGroupClose(2);
+ // Automatic closing of leftover groups
+ },
+
+ 'assembled Select is OK': function( select )
+ {
+ var user = dbWrapper._adapter.escapeTable('user');
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE ((((enabled=1) AND (id=10)) AND (first_name=\'Dr.\' OR first_name IN (\'Bob\', \'Mike\')) AND ((last_name LIKE \'%Benton%\') OR (nickname='+dbWrapper.escape('"`\'éàèç')+'))))' );
}
},
@@ -270,7 +296,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
- assert.equal( select.assemble(), 'enabled=1 AND id=10 AND first_name=\'Dr.\'' );
+ assert.equal( select.assemble(), '(enabled=1) AND (id=10) AND (first_name=\'Dr.\')' );
}
},
@@ -286,7 +312,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE DAY(date_created)=DAY( NOW() )' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (DAY(date_created)=DAY( NOW() ))' );
}
},
@@ -304,7 +330,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10 LIMIT 10' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10) LIMIT 10' );
}
},
@@ -322,7 +348,7 @@ var adapterTestSuite = function( adapterName, callback )
'assembled Select is OK': function( select )
{
var user = dbWrapper._adapter.escapeTable('user');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10 LIMIT 30, 10' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10) LIMIT 30, 10' );
}
},
@@ -342,7 +368,7 @@ var adapterTestSuite = function( adapterName, callback )
{
var user = dbWrapper._adapter.escapeTable('user')
, first_name = dbWrapper._adapter.escapeField('first_name');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10 ORDER BY '+first_name+' ASC LIMIT 10' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10) ORDER BY '+first_name+' ASC LIMIT 10' );
}
},
@@ -362,7 +388,7 @@ var adapterTestSuite = function( adapterName, callback )
{
var user = dbWrapper._adapter.escapeTable('user')
, first_name = dbWrapper._adapter.escapeField('first_name');
- assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE enabled=1 AND id=10 ORDER BY '+first_name+' DESC LIMIT 10' );
+ assert.equal( select.assemble(), 'SELECT '+user+'.* FROM '+user+' WHERE (enabled=1) AND (id=10) ORDER BY '+first_name+' DESC LIMIT 10' );
}
}

0 comments on commit ba044b1

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