Commit cba61cb3 authored by zYne's avatar zYne

Fixes #155

parent 966450d4
<?php
/*
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
......@@ -601,7 +601,12 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
* @return array
*/
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;
$term = array();
foreach($str as $key => $val) {
......
......@@ -12,17 +12,20 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
public function load($where) {
$e = Doctrine_Query::sqlExplode($where);
if(count($e) < 3) {
$e = Doctrine_Query::sqlExplode($where, array('=', '<', '>', '!='));
}
$r = array_shift($e);
$a = explode(".",$r);
if(count($a) > 1) {
$field = array_pop($a);
$count = count($e);
$slice = array_slice($e, 0, ($count - 1));
$operator = implode(' ', $slice);
$slice = array_slice($e, -1, 1);
$value = implode('', $slice);
$operator = trim(substr($where, strlen($r), -strlen($value)));
$reference = implode(".",$a);
$count = count($a);
......@@ -99,7 +102,8 @@ class Doctrine_Query_Where extends Doctrine_Query_Condition {
$where = $this->query->getTableAlias($reference).'.'.$field.' '.$operator.' '.$value;
}
}
}
}
return $where;
}
......
......@@ -5,7 +5,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
$this->tables = array('entity');
parent::prepareTables();
}
public function testQueryWithDirectParameterSetting() {
public function testDirectParameterSetting() {
$this->connection->clear();
$user = new User();
......@@ -13,28 +13,68 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase {
$user->save();
$q = new Doctrine_Query();
$q->from('User(id)')->addWhere('User.id = ?',1);
$users = $q->execute();
$this->assertEqual($users->count(), 1);
$this->assertEqual($users[0]->name, 'someone');
}
public function testQueryWithDirectMultipleParameterSetting() {
public function testDirectMultipleParameterSetting() {
$user = new User();
$user->name = 'someone 2';
$user->name = 'someone.2';
$user->save();
$q = new Doctrine_Query();
$q->from('User(id)')->addWhere('User.id IN (?, ?)',array(1,2));
$users = $q->execute();
$this->assertEqual($users->count(), 2);
$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)");
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment