Commit 5e4d1c3f authored by zYne's avatar zYne

added ORDER BY RANDOM()

parent 24d694bf
......@@ -34,7 +34,14 @@ Doctrine::autoload('Doctrine_Access');
class Doctrine_Db_EventListener_Chain extends Doctrine_Access implements Doctrine_Overloadable
{
private $listeners = array();
/**
* add
* adds a listener to the chain of listeners
*
* @param object $listener
* @param string $name
* @return void
*/
public function add($listener, $name = null)
{
if ( ! ($listener instanceof Doctrine_Db_EventListener_Interface)
......
......@@ -179,6 +179,13 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$this->neededTables[] = $tableAlias;
}
/**
* parseSelect
* parses the query select part and
* adds selected fields to pendingFields array
*
* @param string $dql
*/
public function parseSelect($dql)
{
$refs = Doctrine_Query::bracketExplode($dql, ',');
......@@ -234,21 +241,39 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$owner = $e3[0];
}
// a function without parameters eg. RANDOM()
if ($owner === '') {
$owner = 0;
}
$this->pendingAggregates[$owner][] = array($name, $args, $distinct, $alias);
} else {
throw new Doctrine_Query_Exception('Unknown aggregate function '.$name);
throw new Doctrine_Query_Exception('Unknown function '.$name);
}
}
public function processPendingAggregates($componentAlias)
{
$tableAlias = $this->getTableAlias($componentAlias);
if( ! isset($this->tables[$tableAlias]))
throw new Doctrine_Query_Exception('Unknown component path '.$componentPath);
if ( ! isset($this->tables[$tableAlias])) {
throw new Doctrine_Query_Exception('Unknown component path ' . $componentPath);
}
$root = current($this->tables);
$table = $this->tables[$tableAlias];
$aggregates = array();
if(isset($this->pendingAggregates[$componentAlias])) {
$aggregates = $this->pendingAggregates[$componentAlias];
}
if ($root === $table) {
if (isset($this->pendingAggregates[0])) {
$aggregates += $this->pendingAggregates[0];
}
}
foreach($this->pendingAggregates[$componentAlias] as $parts) {
foreach($aggregates as $parts) {
list($name, $args, $distinct, $alias) = $parts;
$arglist = array();
......@@ -257,7 +282,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
if(count($e) > 1) {
$tableAlias = $this->getTableAlias($e[0]);
//$tableAlias = $this->getTableAlias($e[0]);
$table = $this->tables[$tableAlias];
$e[1] = $table->getColumnName($e[1]);
......@@ -320,7 +345,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$params = array_merge($this->params, $params);
$a = $this->getConnection()->execute($q, $params)->fetch(PDO::FETCH_NUM);
$a = $this->getConnection()->fetchOne($q, $params);
return $a[0];
}
/**
......@@ -1384,7 +1409,9 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$this->processPendingFields($componentAlias);
$skip = true;
}
if(isset($this->pendingAggregates[$componentAlias])) {
if(isset($this->pendingAggregates[$componentAlias]) ||
(current($this->tables) === $table && isset($this->pendingAggregates[0]))
) {
$this->processPendingAggregates($componentAlias);
$skip = true;
}
......
......@@ -67,7 +67,7 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part
$r = $field;
}
if (isset($e[1])) {
$r .= ' '.$e[1];
$r .= ' ' . $e[1];
}
$ret[] = $r;
}
......
......@@ -80,7 +80,23 @@ class Doctrine_Cache_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($data, $resultSet);
$this->assertEqual($this->dbh->getAdapter()->count(), $count);
}
/**
public function testFetchAdvancesCacheDataPointer()
{
$query = 'SELECT * FROM user WHERE id = ?';
$count = $this->dbh->getAdapter()->count();
$params = array(1);
$stmt = $this->dbh->prepare($query);
$stmt->execute($params);
$row1 = $stmt->fetch();
$row2 = $stmt->fetch();
$this->assertEqual($row1, array('name' => 'John'));
$this->assertEqual($row2, array('name' => 'Arnold'));
$this->assertEqual($this->dbh->getAdapter()->count(), $count);
}
public function testAdapterStatementExecuteAddsQueriesToCacheStack()
{
$stmt = $this->dbh->prepare('SELECT * FROM user');
......@@ -97,14 +113,14 @@ class Doctrine_Cache_TestCase extends Doctrine_UnitTestCase
$a = $stmt->fetchAll();
}
*/
public function setUp()
{
parent::setUp();
if ( ! isset($this->cache)) {
$this->cache = new Doctrine_Cache('Array');
$this->cache->setOption('cacheFile', false);
$this->dbh->setAdapter(new Doctrine_Adapter_Mock());
$this->dbh->addListener($this->cache);
}
......
......@@ -31,17 +31,26 @@
* @version $Revision$
*/
class Doctrine_Query_Orderby_TestCase extends Doctrine_UnitTestCase
{
{
public function testOrderByAggregateValueIsSupported()
{
$q = new Doctrine_Query();
$q->select('u.name, COUNT(p.phonenumber) count')
->from('User u')->leftJoin('u.Phonenumber p')
->from('User u')
->leftJoin('u.Phonenumber p')
->orderby('count DESC');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, COUNT(p.phonenumber) AS p__0 FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0) ORDER BY p__0 DESC');
}
public function testOrderByRandomIsSupported()
{
$q = new Doctrine_Query();
$users = $q->execute();
$q->select('u.name, RANDOM() rand')
->from('User u')
->orderby('rand DESC');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, RANDOM() AS e__0 FROM entity e WHERE (e.type = 0) ORDER BY e__0 DESC');
}
}
......@@ -30,12 +30,18 @@
* @since 1.0
* @version $Revision$
*/
class Doctrine_Relation_OneToOne_TestCase extends Doctrine_UnitTestCase {
public function testOneToOneAggregateRelationWithAliasesIsSupported() {
class Doctrine_Relation_OneToOne_TestCase extends Doctrine_UnitTestCase
{
public function prepareData()
{ }
public function prepareTables()
{ }
public function testOneToOneAggregateRelationWithAliasesIsSupported()
{
$city = new Record_City();
$country = $city->Country;
$this->assertTrue($country instanceof Record_Country);
$this->assertTrue($country instanceof Record_Country);
}
}
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