2012-03-06 16:24:44 +01:00
< ? php
namespace Doctrine\Tests\ORM\Tools\Pagination ;
2014-12-05 14:54:26 +01:00
use Doctrine\DBAL\Platforms\MySqlPlatform ;
2014-12-05 14:53:42 +01:00
use Doctrine\DBAL\Platforms\OraclePlatform ;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform ;
2012-03-06 16:24:44 +01:00
use Doctrine\ORM\Query ;
2012-03-12 08:33:35 +01:00
class LimitSubqueryOutputWalkerTest extends PaginationTestCase
2012-03-06 16:24:44 +01:00
{
public function testLimitSubquery ()
{
$query = $this -> entityManager -> createQuery (
2013-01-26 23:27:38 +02:00
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a' );
2012-09-18 13:56:32 +02:00
$query -> expireQueryCache ( true );
2012-03-06 16:24:44 +01:00
$limitQuery = clone $query ;
2012-03-12 08:33:35 +01:00
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
2012-03-06 16:24:44 +01:00
$this -> assertEquals (
2012-09-18 13:56:32 +02:00
" SELECT DISTINCT id0 FROM (SELECT m0_.id AS id0, m0_.title AS title1, c1_.id AS id2, a2_.id AS id3, a2_.name AS name4, m0_.author_id AS author_id5, m0_.category_id AS category_id6 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id) dctrn_result " , $limitQuery -> getSql ()
2012-03-06 16:24:44 +01:00
);
}
2012-09-18 13:56:32 +02:00
public function testLimitSubqueryWithSortPg ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new PostgreSqlPlatform );
2012-09-18 13:56:32 +02:00
$query = $this -> entityManager -> createQuery (
2013-01-26 23:27:38 +02:00
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title' );
2012-09-18 13:56:32 +02:00
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
" SELECT DISTINCT id0, title1 FROM (SELECT m0_.id AS id0, m0_.title AS title1, c1_.id AS id2, a2_.id AS id3, a2_.name AS name4, m0_.author_id AS author_id5, m0_.category_id AS category_id6 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id ORDER BY m0_.title ASC) dctrn_result ORDER BY title1 ASC " , $limitQuery -> getSql ()
);
2013-01-26 21:21:09 +02:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
public function testLimitSubqueryWithScalarSortPg ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new PostgreSqlPlatform );
2013-01-26 21:21:09 +02:00
$query = $this -> entityManager -> createQuery (
2013-01-27 11:09:26 +02:00
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity'
2013-01-26 21:21:09 +02:00
);
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
2013-01-26 21:31:45 +02:00
" SELECT DISTINCT id1, sclr0 FROM (SELECT COUNT(g0_.id) AS sclr0, u1_.id AS id1, g0_.id AS id2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY sclr0 ASC) dctrn_result ORDER BY sclr0 ASC " ,
2013-01-26 21:21:09 +02:00
$limitQuery -> getSql ()
);
2013-01-27 11:40:40 +02:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
public function testLimitSubqueryWithMixedSortPg ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new PostgreSqlPlatform );
2013-01-27 11:40:40 +02:00
$query = $this -> entityManager -> createQuery (
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
);
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
" SELECT DISTINCT id1, sclr0 FROM (SELECT COUNT(g0_.id) AS sclr0, u1_.id AS id1, g0_.id AS id2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY sclr0 ASC, u1_.id DESC) dctrn_result ORDER BY sclr0 ASC, id1 DESC " ,
$limitQuery -> getSql ()
);
2012-09-18 13:56:32 +02:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
2013-04-03 17:21:51 +09:00
public function testLimitSubqueryWithHiddenScalarSortPg ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new PostgreSqlPlatform );
2013-04-03 17:21:51 +09:00
$query = $this -> entityManager -> createQuery (
'SELECT u, g, COUNT(g.id) AS hidden g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
);
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
" SELECT DISTINCT id1, sclr0 FROM (SELECT COUNT(g0_.id) AS sclr0, u1_.id AS id1, g0_.id AS id2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY sclr0 ASC, u1_.id DESC) dctrn_result ORDER BY sclr0 ASC, id1 DESC " ,
$limitQuery -> getSql ()
);
2012-09-18 13:56:32 +02:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
public function testLimitSubqueryPg ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new PostgreSqlPlatform );
2012-09-18 13:56:32 +02:00
$this -> testLimitSubquery ();
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
2013-04-10 13:07:09 -03:00
public function testLimitSubqueryWithSortOracle ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new OraclePlatform );
2013-04-10 13:07:09 -03:00
$query = $this -> entityManager -> createQuery (
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title' );
$query -> expireQueryCache ( true );
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
" SELECT DISTINCT ID0, TITLE1 FROM (SELECT m0_.id AS ID0, m0_.title AS TITLE1, c1_.id AS ID2, a2_.id AS ID3, a2_.name AS NAME4, m0_.author_id AS AUTHOR_ID5, m0_.category_id AS CATEGORY_ID6 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id ORDER BY m0_.title ASC) dctrn_result ORDER BY TITLE1 ASC " , $limitQuery -> getSql ()
);
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
public function testLimitSubqueryWithScalarSortOracle ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new OraclePlatform );
2013-04-10 13:07:09 -03:00
$query = $this -> entityManager -> createQuery (
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity'
);
$query -> expireQueryCache ( true );
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
" SELECT DISTINCT ID1, SCLR0 FROM (SELECT COUNT(g0_.id) AS SCLR0, u1_.id AS ID1, g0_.id AS ID2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY SCLR0 ASC) dctrn_result ORDER BY SCLR0 ASC " ,
$limitQuery -> getSql ()
);
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
public function testLimitSubqueryWithMixedSortOracle ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new OraclePlatform );
2013-04-10 13:07:09 -03:00
$query = $this -> entityManager -> createQuery (
'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
);
$query -> expireQueryCache ( true );
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
" SELECT DISTINCT ID1, SCLR0 FROM (SELECT COUNT(g0_.id) AS SCLR0, u1_.id AS ID1, g0_.id AS ID2 FROM User u1_ INNER JOIN user_group u2_ ON u1_.id = u2_.user_id INNER JOIN groups g0_ ON g0_.id = u2_.group_id ORDER BY SCLR0 ASC, u1_.id DESC) dctrn_result ORDER BY SCLR0 ASC, ID1 DESC " ,
$limitQuery -> getSql ()
);
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
public function testLimitSubqueryOracle ()
{
$odp = $this -> entityManager -> getConnection () -> getDatabasePlatform ();
2014-12-05 14:53:42 +01:00
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new OraclePlatform );
2013-04-10 13:07:09 -03:00
$query = $this -> entityManager -> createQuery (
'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a' );
$query -> expireQueryCache ( true );
$limitQuery = clone $query ;
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertEquals (
" SELECT DISTINCT ID0 FROM (SELECT m0_.id AS ID0, m0_.title AS TITLE1, c1_.id AS ID2, a2_.id AS ID3, a2_.name AS NAME4, m0_.author_id AS AUTHOR_ID5, m0_.category_id AS CATEGORY_ID6 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id INNER JOIN Author a2_ ON m0_.author_id = a2_.id) dctrn_result " , $limitQuery -> getSql ()
);
$this -> entityManager -> getConnection () -> setDatabasePlatform ( $odp );
}
2012-09-18 13:56:32 +02:00
2014-12-05 14:53:42 +01:00
public function testCountQueryMixedResultsWithName ()
2012-03-06 16:24:44 +01:00
{
$query = $this -> entityManager -> createQuery (
2012-03-07 08:57:51 +01:00
'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a' );
2012-03-06 16:24:44 +01:00
$limitQuery = clone $query ;
2012-03-12 08:33:35 +01:00
$limitQuery -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
2012-03-06 16:24:44 +01:00
$this -> assertEquals (
2012-05-27 13:22:48 +02:00
" SELECT DISTINCT id0 FROM (SELECT a0_.id AS id0, a0_.name AS name1, sum(a0_.name) AS sclr2 FROM Author a0_) dctrn_result " , $limitQuery -> getSql ()
2012-03-06 16:24:44 +01:00
);
}
2014-12-05 14:54:26 +01:00
/**
* @ group DDC - 3336
*/
2014-12-05 14:55:26 +01:00
public function testCountQueryWithArithmeticOrderByCondition ()
2014-12-05 14:54:26 +01:00
{
$query = $this -> entityManager -> createQuery (
'SELECT a FROM Doctrine\\Tests\\ORM\\Tools\\Pagination\\Author a ORDER BY (1 - 1000) * 1 DESC'
);
$this -> entityManager -> getConnection () -> setDatabasePlatform ( new MySqlPlatform ());
$query -> setHint ( Query :: HINT_CUSTOM_OUTPUT_WALKER , 'Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker' );
$this -> assertSame (
2014-12-05 17:29:18 +01:00
'SELECT DISTINCT id0 FROM (SELECT a0_.id AS id0, a0_.name AS name1 FROM Author a0_ ORDER BY (1 - 1000) * 1 DESC) dctrn_result' ,
2014-12-05 14:54:26 +01:00
$query -> getSQL ()
);
}
2012-03-06 16:24:44 +01:00
}