Fixes #155
This commit is contained in:
parent
966450d454
commit
cba61cb346
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
* $Id$
|
* $Id$
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
@ -601,7 +601,12 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function sqlExplode($str,$d = " ",$e1 = '(',$e2 = ')') {
|
public static function sqlExplode($str,$d = " ",$e1 = '(',$e2 = ')') {
|
||||||
$str = explode("$d",$str);
|
if(is_array($d)) {
|
||||||
|
$str = preg_split('/('.implode('|', $d).')/', $str);
|
||||||
|
$d = stripslashes($d[0]);
|
||||||
|
} else
|
||||||
|
$str = explode("$d",$str);
|
||||||
|
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$term = array();
|
$term = array();
|
||||||
foreach($str as $key => $val) {
|
foreach($str as $key => $val) {
|
||||||
|
@ -12,17 +12,20 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
|||||||
public function load($where) {
|
public function load($where) {
|
||||||
|
|
||||||
$e = Doctrine_Query::sqlExplode($where);
|
$e = Doctrine_Query::sqlExplode($where);
|
||||||
|
|
||||||
|
if(count($e) < 3) {
|
||||||
|
$e = Doctrine_Query::sqlExplode($where, array('=', '<', '>', '!='));
|
||||||
|
}
|
||||||
$r = array_shift($e);
|
$r = array_shift($e);
|
||||||
|
|
||||||
$a = explode(".",$r);
|
$a = explode(".",$r);
|
||||||
|
|
||||||
if(count($a) > 1) {
|
if(count($a) > 1) {
|
||||||
$field = array_pop($a);
|
$field = array_pop($a);
|
||||||
$count = count($e);
|
$count = count($e);
|
||||||
$slice = array_slice($e, 0, ($count - 1));
|
|
||||||
$operator = implode(' ', $slice);
|
|
||||||
|
|
||||||
$slice = array_slice($e, -1, 1);
|
$slice = array_slice($e, -1, 1);
|
||||||
$value = implode('', $slice);
|
$value = implode('', $slice);
|
||||||
|
$operator = trim(substr($where, strlen($r), -strlen($value)));
|
||||||
|
|
||||||
$reference = implode(".",$a);
|
$reference = implode(".",$a);
|
||||||
$count = count($a);
|
$count = count($a);
|
||||||
@ -99,7 +102,8 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
|
|||||||
$where = $this->query->getTableAlias($reference).'.'.$field.' '.$operator.' '.$value;
|
$where = $this->query->getTableAlias($reference).'.'.$field.' '.$operator.' '.$value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $where;
|
return $where;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
|
|||||||
$this->tables = array('entity');
|
$this->tables = array('entity');
|
||||||
parent::prepareTables();
|
parent::prepareTables();
|
||||||
}
|
}
|
||||||
public function testQueryWithDirectParameterSetting() {
|
public function testDirectParameterSetting() {
|
||||||
$this->connection->clear();
|
$this->connection->clear();
|
||||||
|
|
||||||
$user = new User();
|
$user = new User();
|
||||||
@ -13,28 +13,68 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
|
|||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
$q = new Doctrine_Query();
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
$q->from('User(id)')->addWhere('User.id = ?',1);
|
$q->from('User(id)')->addWhere('User.id = ?',1);
|
||||||
|
|
||||||
$users = $q->execute();
|
$users = $q->execute();
|
||||||
|
|
||||||
$this->assertEqual($users->count(), 1);
|
$this->assertEqual($users->count(), 1);
|
||||||
$this->assertEqual($users[0]->name, 'someone');
|
$this->assertEqual($users[0]->name, 'someone');
|
||||||
}
|
}
|
||||||
public function testQueryWithDirectMultipleParameterSetting() {
|
public function testDirectMultipleParameterSetting() {
|
||||||
$user = new User();
|
$user = new User();
|
||||||
$user->name = 'someone 2';
|
$user->name = 'someone.2';
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
$q = new Doctrine_Query();
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
$q->from('User(id)')->addWhere('User.id IN (?, ?)',array(1,2));
|
$q->from('User(id)')->addWhere('User.id IN (?, ?)',array(1,2));
|
||||||
|
|
||||||
$users = $q->execute();
|
$users = $q->execute();
|
||||||
|
|
||||||
$this->assertEqual($users->count(), 2);
|
$this->assertEqual($users->count(), 2);
|
||||||
$this->assertEqual($users[0]->name, 'someone');
|
$this->assertEqual($users[0]->name, 'someone');
|
||||||
$this->assertEqual($users[1]->name, 'someone 2');
|
$this->assertEqual($users[1]->name, 'someone.2');
|
||||||
|
}
|
||||||
|
public function testOperatorWithNoTrailingSpaces() {
|
||||||
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
$q->from('User(id)')->where("User.name='someone'");
|
||||||
|
|
||||||
|
$users = $q->execute();
|
||||||
|
$this->assertEqual($users->count(), 1);
|
||||||
|
|
||||||
|
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'someone' AND (entity.type = 0)");
|
||||||
|
}
|
||||||
|
public function testOperatorWithNoTrailingSpaces2() {
|
||||||
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
$q->from('User(id)')->where("User.name='foo.bar'");
|
||||||
|
|
||||||
|
$users = $q->execute();
|
||||||
|
$this->assertEqual($users->count(), 0);
|
||||||
|
|
||||||
|
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)");
|
||||||
|
}
|
||||||
|
public function testOperatorWithSingleTrailingSpace() {
|
||||||
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
$q->from('User(id)')->where("User.name= 'foo.bar'");
|
||||||
|
|
||||||
|
$users = $q->execute();
|
||||||
|
$this->assertEqual($users->count(), 0);
|
||||||
|
|
||||||
|
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)");
|
||||||
|
}
|
||||||
|
public function testOperatorWithSingleTrailingSpace2() {
|
||||||
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
$q->from('User(id)')->where("User.name ='foo.bar'");
|
||||||
|
|
||||||
|
$users = $q->execute();
|
||||||
|
$this->assertEqual($users->count(), 0);
|
||||||
|
|
||||||
|
$this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id FROM entity WHERE entity.name = 'foo.bar' AND (entity.type = 0)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user