1
0
mirror of synced 2025-01-18 06:21:40 +03:00
This commit is contained in:
zYne 2007-06-27 17:41:02 +00:00
parent e3719874bd
commit a6139bb381
7 changed files with 229 additions and 38 deletions

View File

@ -361,8 +361,11 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
if ( ! isset($this->parts[$name])) {
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $name);
}
if (is_array($part)) {
$this->parts[$name] = array_merge($this->parts[$name], $part);
} else {
$this->parts[$name][] = $part;
}
return $this;
}
/**
@ -419,7 +422,11 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
}
if ($name !== 'limit' && $name !== 'offset') {
if (is_array($part)) {
$this->parts[$name] = $part;
} else {
$this->parts[$name] = array($part);
}
} else {
$this->parts[$name] = $part;
}

View File

@ -848,19 +848,41 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
// initialize the base of the subquery
$subquery = 'SELECT DISTINCT ' . $primaryKey;
if ($this->_conn->getAttribute(Doctrine::ATTR_DRIVER_NAME) == 'pgsql') {
// pgsql needs the order by fields to be preserved in select clause
$driverName = $this->_conn->getAttribute(Doctrine::ATTR_DRIVER_NAME);
foreach ($this->parts['orderby'] as $part) {
$e = explode(' ', $part);
$part = trim($part);
$aggregate = false;
foreach ($this->parts['select'] as $select) {
$e = explode(' AS ', trim($select));
if (count($e) > 1) {
if ($part === $e[1]) {
$part = $select;
$aggregate = true;
break;
}
}
}
// don't add primarykey column (its already in the select clause)
if ($e[0] !== $primaryKey) {
$subquery .= ', ' . $e[0];
if ($part !== $primaryKey) {
if ($driverName == 'mysql') {
if ($aggregate) {
$subquery .= ', ' . $part;
}
} elseif ($driverName == 'pgsql') {
// pgsql needs the order by fields to be preserved in select clause
$subquery .= ', ' . $part;
}
}
}
$subquery .= ' FROM';
@ -898,14 +920,23 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$parts[$k] = $this->generateNewTableAlias($part);
}
$separator = false;
if (strpos($part, '.') !== false) {
$e = explode('.', $part);
$separator = '.';
}
if (strpos($part, '__') !== false) {
$separator = '__';
}
if ($separator) {
$e = explode($separator, $part);
$trimmed = ltrim($e[0], '( ');
$pos = strpos($e[0], $trimmed);
$e[0] = substr($e[0], 0, $pos) . $this->generateNewTableAlias($trimmed);
$parts[$k] = implode('.', $e);
$parts[$k] = implode($separator, $e);
}
}
$subquery = implode(' ', $parts);

View File

@ -69,6 +69,6 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part
}
$ret[] = $r;
}
return implode(', ', $ret);
return $ret;
}
}

View File

@ -1,32 +1,72 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Query_MysqlSubquery_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Query_MysqlSubquery_TestCase extends Doctrine_UnitTestCase
{
public function testInit()
public function setUp()
{
$this->dbh = new Doctrine_Adapter_Mock('mysql');
$this->conn = Doctrine_Manager::getInstance()->openConnection($this->dbh);
}
public function testGetLimitSubqueryOrderBy()
public function testGetLimitSubqueryWithOrderByOnAggregateValues()
{
$q = new Doctrine_Query();
$q->select('u.name, COUNT(DISTINCT a.id) num_albums');
$q->from('User u, u.Album a');
$q->orderby('num_albums');
$q->groupby('u.id');
// this causes getLimitSubquery() to be used, and it fails
$q->limit(5);
try {
$q->execute();
} catch (Doctrine_Exception $e) {
$this->fail();
$this->dbh->pop();
$this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0 LIMIT 5');
}
$queries = $this->dbh->getAll();
public function testGetLimitSubqueryWithOrderByOnAggregateValuesAndColumns()
{
$q = new Doctrine_Query();
$q->select('u.name, COUNT(DISTINCT a.id) num_albums');
$q->from('User u, u.Album a');
$q->orderby('num_albums, u.name');
$q->groupby('u.id');
$q->limit(5);
$this->assertEqual($queries[0], 'SELECT DISTINCT e2.id, COUNT(DISTINCT a.id) AS a__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a__0 LIMIT 5');
$q->execute();
$this->dbh->pop();
$this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0, e2.name LIMIT 5');
}
}

View File

@ -0,0 +1,73 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Query_PgsqlSubquery_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Query_PgsqlSubquery_TestCase extends Doctrine_UnitTestCase
{
public function setUp()
{
$this->dbh = new Doctrine_Adapter_Mock('pgsql');
$this->conn = Doctrine_Manager::getInstance()->openConnection($this->dbh);
}
/**
public function testGetLimitSubqueryWithOrderByOnAggregateValues()
{
$q = new Doctrine_Query();
$q->select('u.name, COUNT(DISTINCT a.id) num_albums');
$q->from('User u, u.Album a');
$q->orderby('num_albums');
$q->groupby('u.id');
$q->limit(5);
$q->execute();
$this->dbh->pop();
$this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0 FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0 LIMIT 5');
}
public function testGetLimitSubqueryWithOrderByOnAggregateValuesAndColumns()
{
$q = new Doctrine_Query();
$q->select('u.name, COUNT(DISTINCT a.id) num_albums');
$q->from('User u, u.Album a');
$q->orderby('num_albums, u.name');
$q->groupby('u.id');
$q->limit(5);
$q->execute();
$this->dbh->pop();
$this->assertEqual($this->dbh->pop(), 'SELECT DISTINCT e2.id, COUNT(DISTINCT a2.id) AS a2__0, e2.name FROM entity e2 LEFT JOIN album a2 ON e2.id = a2.user_id WHERE (e2.type = 0) GROUP BY e2.id ORDER BY a2__0, e2.name LIMIT 5');
}
*/
}

View File

@ -0,0 +1,39 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Template_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Template_TestCase extends Doctrine_UnitTestCase
{
public function testTemplateRelationsSupportConcreteInheritance()
{
$blog = new Blog();
}
}

View File

@ -50,11 +50,9 @@ spl_autoload_register(array('Doctrine', 'autoload'));
spl_autoload_register('autoload');
require_once dirname(__FILE__) . '/../models/location.php';
require_once dirname(__FILE__) . '/../models/Blog.php';
require_once dirname(__FILE__) . '/classes.php';
/**
require_once dirname(__FILE__) . '/../vendor/simpletest/unit_tester.php';
require_once dirname(__FILE__) . '/../vendor/simpletest/reporter.php';
*/
require_once dirname(__FILE__) . '/Test.php';
require_once dirname(__FILE__) . '/UnitTestCase.php';
@ -304,9 +302,12 @@ $test->addTestCase(new Doctrine_Query_TestCase());
$test->addTestCase(new Doctrine_Ticket364_TestCase());
$test->addTestCase(new Doctrine_Template_TestCase());
$test->addTestCase(new Doctrine_Query_MysqlSubquery_TestCase());
/** */
$test->addTestCase(new Doctrine_Query_PgsqlSubquery_TestCase());
//$test->addTestCase(new Doctrine_IntegrityAction_TestCase());
//$test->addTestCase(new Doctrine_AuditLog_TestCase());