PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Database » DB_DataObject » Bug #5881

Put parens around whereAdd's if 'or' is present

Details

Request #5881Put parens around whereAdd's if 'or' is present
Submitted2005-11-06 22:41 UTC
Fromken at restivo dot org
Assignedalan_k
StatusClosed
PackageDB_DataObject
PHP Version4.3.10
OSIrrelevant
Roadmaps(Not assigned)

Comments

[2005-11-06 22:41 UTC] ken at restivo dot org

Description:
------------
If an "or" is present in a whereAdd() condition, it wreaks havoc if the user forgets to enclose it in parens.

i.e. this will not do what the user thinks it'll do:
$obj->whereAdd('foo = "bar"');
$obj->whereAdd('baz = 2 or floop < 2934');

Test script:
---------------
It was an easy fix:

@@ -586,6 +586,9 @@ class DB_DataObject extends DB_DataObjec
return $this->raiseError("WhereAdd: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS);
}
$r = $this->_query['condition'];
+ if(stristr($cond, ' or ')){
+ $cond = "({$cond})";
+ }
if ($this->_query['condition']) {
$this->_query['condition'] .= " {$logic} {$cond}";
return $r;

Expected result:
----------------
Which would give the naive user what they'd expect to see:

WHERE foo = "bar" AND (baz = 2 or floop < 2934)'

Actual result:
--------------
Without the patch, whereAdd currently produces:

'WHERE foo = "bar" AND baz = 2 or floop < 2934'

... which (IMHO) certainly violates the principle of least surprise.

This caused some pretty major heartburn in my app, anyway.