Commit 8abb9795 authored by zYne's avatar zYne

Fixes #85

parent a1b4b0f7
......@@ -50,6 +50,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
* @var array $data fetched data
*/
protected $data = array();
protected $params = array();
/**
* @var Doctrine_Connection $connection Doctrine_Connection object
*/
......@@ -245,6 +247,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
public function execute($params = array(), $return = Doctrine::FETCH_RECORD) {
$this->collections = array();
$params = array_merge($this->params, $params);
array_walk($params, array(__CLASS__, 'convertBoolean'));
if( ! $this->view)
......
......@@ -165,11 +165,18 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
* addWhere
*
* @param string $where
* @param mixed $params
*/
public function addWhere($where) {
public function addWhere($where, $params = array()) {
$class = "Doctrine_Query_Where";
$parser = new $class($this);
$this->parts['where'][] = $parser->parse($where);
if(is_array($params)) {
$this->params = array_merge($this->params, $params);
} else {
$this->params[] = $params;
}
}
/**
* sets a query part
......
......@@ -29,88 +29,45 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
parent::prepareTables();
$this->connection->clear();
}
public function testValidLazyPropertyFetching() {
$q = new Doctrine_Query($this->connection);
$q->from("User(id, name)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
$this->assertTrue($users instanceof Doctrine_Collection);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertEqual($count + 1, count($this->dbh));
$this->connection->clear();
public function testBracktExplode() {
$str = "item OR item || item";
$parts = Doctrine_Query::bracketExplode($str, array(' \|\| ', ' OR '), "(", ")");
$this->assertEqual($parts, array('item','item','item'));
}
public function testConditionParser() {
$query = new Doctrine_Query($this->connection);
$query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE 'z%' OR entity.name LIKE 's%') AND (entity.type = 0)";
$this->assertEqual($query->getQuery(), $sql);
$query->where("(User.name LIKE 'z%') || (User.name LIKE 's%')");
$this->assertEqual($query->getQuery(), $sql);
$query->where("((User.name LIKE 'z%') || (User.name LIKE 's%'))");
$this->assertEqual($query->getQuery(), $sql);
$query->where("(((User.name LIKE 'z%') || (User.name LIKE 's%')))");
$this->assertEqual($query->getQuery(), $sql);
$query->where("(((User.name LIKE 'z%') || User.name LIKE 's%'))");
$this->assertEqual($query->getQuery(), $sql);
$query->where("(User.name LIKE 'z%') || User.name LIKE 's%' && User.name LIKE 'a%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE ((entity.name LIKE 'z%' OR entity.name LIKE 's%') AND entity.name LIKE 'a%') AND (entity.type = 0)";
$this->assertEqual($query->getQuery(), $sql);
$query->where("(((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%'");
$this->assertEqual($query->getQuery(), $sql);
$query->where("((((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%')");
$this->assertEqual($query->getQuery(), $sql);
$q->from("User(name)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue($users[0]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertTrue($users[0]->getState(), Doctrine_Record::STATE_CLEAN);
$query->where("(((((User.name LIKE 'z%') || User.name LIKE 's%')) && User.name LIKE 'a%'))");
$this->assertEqual($query->getQuery(), $sql);
$this->assertEqual($count + 1, count($this->dbh));
$this->assertTrue($users[1]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertTrue(is_numeric($users[1]->email_id));
$this->assertTrue($users[1]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($count + 2, count($this->dbh));
$this->assertTrue($users[2]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertTrue(is_numeric($users[2]->email_id));
$this->assertTrue($users[2]->getState(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($count + 3, count($this->dbh));
}
public function testConditionParser2() {
$query = new Doctrine_Query($this->connection);
$query->from("User(id)")->where("User.name LIKE 'z%' || User.name LIKE 's%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE (entity.name LIKE 'z%' OR entity.name LIKE 's%') AND (entity.type = 0)";
$this->assertEqual($query->getQuery(), $sql);
$query->where("(User.name LIKE 'z%') OR (User.name LIKE 's%')");
$this->assertEqual($query->getQuery(), $sql);
$query->where("((User.name LIKE 'z%') OR (User.name LIKE 's%'))");
$this->assertEqual($query->getQuery(), $sql);
$query->where("(((User.name LIKE 'z%') OR (User.name LIKE 's%')))");
$this->assertEqual($query->getQuery(), $sql);
$query->where("(((User.name LIKE 'z%') OR User.name LIKE 's%'))");
$this->assertEqual($query->getQuery(), $sql);
$query->where("(User.name LIKE 'z%') OR User.name LIKE 's%' AND User.name LIKE 'a%'");
$sql = "SELECT entity.id AS entity__id FROM entity WHERE ((entity.name LIKE 'z%' OR entity.name LIKE 's%') AND entity.name LIKE 'a%') AND (entity.type = 0)";
$this->assertEqual($query->getQuery(), $sql);
$query->where("(((User.name LIKE 'z%') OR User.name LIKE 's%')) AND User.name LIKE 'a%'");
$this->assertEqual($query->getQuery(), $sql);
$query->where("((((User.name LIKE 'z%') OR User.name LIKE 's%')) AND User.name LIKE 'a%')");
$this->assertEqual($query->getQuery(), $sql);
$query->where("(((((User.name LIKE 'z%') OR User.name LIKE 's%')) AND User.name LIKE 'a%'))");
$this->assertEqual($query->getQuery(), $sql);
}
public function testUnknownFunction() {
$q = new Doctrine_Query();
......@@ -888,36 +845,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($users[0]->Account->amount,3000);
}
public function testValidLazyPropertyFetching() {
$q = new Doctrine_Query($this->connection);
$q->from("User-l(name)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
$this->assertTrue($users instanceof Doctrine_Collection_Lazy);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertEqual($count + 1, count($this->dbh));
$users[0]->getTable()->clear();
$q->from("User-b(name)");
$users = $q->execute();
$this->assertEqual($users->count(), 8);
$this->assertTrue($users instanceof Doctrine_Collection_Batch);
$count = count($this->dbh);
$this->assertTrue(is_string($users[0]->name));
$this->assertEqual($count, count($this->dbh));
$count = count($this->dbh);
$this->assertTrue(is_numeric($users[0]->email_id));
$this->assertEqual($count + 1, count($this->dbh));
$this->assertTrue(is_numeric($users[1]->email_id));
$this->assertEqual($count + 1, count($this->dbh));
$this->assertTrue(is_numeric($users[2]->email_id));
$this->assertEqual($count + 1, count($this->dbh));
}
public function testQueryWithComplexAliases() {
$q = new Doctrine_Query($this->connection);
......
......@@ -18,15 +18,20 @@ require_once("PessimisticLockingTestCase.php");
require_once("EventListenerChainTestCase.php");
require_once("CacheSqliteTestCase.php");
require_once("CollectionOffsetTestCase.php");
require_once("QueryTestCase.php");
require_once("CacheQuerySqliteTestCase.php");
require_once("ViewTestCase.php");
require_once("RawSqlTestCase.php");
require_once("CustomPrimaryKeyTestCase.php");
require_once("FilterTestCase.php");
require_once("QueryTestCase.php");
require_once("QueryLimitTestCase.php");
require_once("QueryMultiJoinTestCase.php");
require_once("QueryReferenceModelTestCase.php");
require_once("QueryWhereTestCase.php");
require_once("QueryConditionTestCase.php");
require_once("DBTestCase.php");
require_once("SchemaTestCase.php");
require_once("ImportTestCase.php");
......@@ -97,14 +102,18 @@ $test->addTestCase(new Doctrine_DataDict_Sqlite_TestCase());
$test->addTestCase(new Doctrine_BooleanTestCase());
$test->addTestCase(new Doctrine_QueryTestCase());
$test->addTestCase(new Doctrine_EventListener_Chain_TestCase());
$test->addTestCase(new Doctrine_RelationAccessTestCase());
$test->addTestCase(new Doctrine_CustomResultSetOrderTestCase());
$test->addTestCase(new Doctrine_QueryTestCase());
$test->addTestCase(new Doctrine_Query_Where_TestCase());
$test->addTestCase(new Doctrine_Query_Condition_TestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
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