2008-05-24 22:18:37 +04:00
< ? php
2009-01-22 22:38:10 +03:00
namespace Doctrine\Tests\ORM\Query ;
2009-01-24 19:56:44 +03:00
require_once __DIR__ . '/../../TestInit.php' ;
2009-01-22 22:38:10 +03:00
class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
2008-05-24 22:18:37 +04:00
{
2009-01-19 21:40:12 +03:00
private $_em ;
2009-02-18 10:59:11 +03:00
protected function setUp ()
{
2009-01-19 21:40:12 +03:00
$this -> _em = $this -> _getTestEntityManager ();
}
2008-05-27 08:52:50 +04:00
public function assertSqlGeneration ( $dqlToBeTested , $sqlToBeConfirmed )
2008-05-24 22:18:37 +04:00
{
2008-05-27 08:52:50 +04:00
try {
2009-01-19 21:40:12 +03:00
$query = $this -> _em -> createQuery ( $dqlToBeTested );
2008-05-27 08:52:50 +04:00
parent :: assertEquals ( $sqlToBeConfirmed , $query -> getSql ());
$query -> free ();
} catch ( Doctrine_Exception $e ) {
2009-01-20 20:07:07 +03:00
echo $e -> getMessage ();
2009-01-19 21:40:12 +03:00
echo $e -> getTraceAsString (); die ();
2008-05-27 08:52:50 +04:00
$this -> fail ( $e -> getMessage ());
}
}
2008-05-28 10:01:04 +04:00
public function testPlainFromClauseWithoutAlias ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.id AS c0__id, c0.status AS c0__status, c0.username AS c0__username, c0.name AS c0__name FROM cms_users c0'
2008-05-28 10:01:04 +04:00
);
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.id AS c0__id FROM cms_users c0'
2008-05-28 10:01:04 +04:00
);
}
2009-01-19 21:40:12 +03:00
public function testSelectSingleComponentWithMultipleColumns ()
2008-05-28 10:01:04 +04:00
{
2008-05-27 08:52:50 +04:00
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u.username, u.name FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.username AS c0__username, c0.name AS c0__name FROM cms_users c0'
2008-05-27 08:52:50 +04:00
);
2008-05-24 22:18:37 +04:00
}
2009-01-19 21:40:12 +03:00
public function testSelectWithCollectionAssociationJoin ()
2008-05-28 10:01:04 +04:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.id AS c0__id, c0.status AS c0__status, c0.username AS c0__username, c0.name AS c0__name, c1.phonenumber AS c1__phonenumber FROM cms_users c0 INNER JOIN cms_phonenumbers c1 ON c0.id = c1.user_id'
2008-05-28 10:01:04 +04:00
);
}
2009-01-19 21:40:12 +03:00
public function testSelectWithSingleValuedAssociationJoin ()
2008-05-28 10:01:04 +04:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u, a FROM Doctrine\Tests\Models\Forum\ForumUser u JOIN u.avatar a' ,
2009-02-17 13:54:18 +03:00
'SELECT f0.id AS f0__id, f0.username AS f0__username, f1.id AS f1__id FROM forum_users f0 INNER JOIN forum_avatars f1 ON f0.avatar_id = f1.id'
2008-05-28 10:01:04 +04:00
);
}
2008-06-02 19:00:50 +04:00
2008-05-28 10:01:04 +04:00
public function testSelectDistinctIsSupported ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-02-17 13:54:18 +03:00
'SELECT DISTINCT c0.name AS c0__name FROM cms_users c0'
2008-05-28 10:01:04 +04:00
);
}
public function testAggregateFunctionInSelect ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id' ,
2009-02-17 13:54:18 +03:00
'SELECT COUNT(c0.id) AS dctrn__0 FROM cms_users c0 GROUP BY c0.id'
2008-05-28 10:01:04 +04:00
);
}
2009-01-21 21:25:05 +03:00
public function testWhereClauseInSelectWithPositionalParameter ()
2009-01-19 21:40:12 +03:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.id = ?1' ,
2009-02-17 13:54:18 +03:00
'SELECT f0.id AS f0__id, f0.username AS f0__username FROM forum_users f0 WHERE f0.id = ?'
2009-01-19 21:40:12 +03:00
);
}
2009-01-19 22:24:40 +03:00
2009-01-21 21:25:05 +03:00
public function testWhereClauseInSelectWithNamedParameter ()
2008-05-28 10:01:04 +04:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name' ,
2009-02-17 13:54:18 +03:00
'SELECT f0.id AS f0__id, f0.username AS f0__username FROM forum_users f0 WHERE f0.username = :name'
2008-05-28 10:01:04 +04:00
);
}
2009-01-21 21:25:05 +03:00
public function testWhereANDClauseInSelectWithNamedParameter ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name and u.username = :name2' ,
2009-02-17 13:54:18 +03:00
'SELECT f0.id AS f0__id, f0.username AS f0__username FROM forum_users f0 WHERE f0.username = :name AND f0.username = :name2'
2009-01-21 21:25:05 +03:00
);
}
2008-05-28 10:01:04 +04:00
2009-01-21 21:25:05 +03:00
public function testCombinedWhereClauseInSelectWithNamedParameter ()
2008-05-28 10:01:04 +04:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'select u from Doctrine\Tests\Models\Forum\ForumUser u where (u.username = :name OR u.username = :name2) AND u.id = :id' ,
2009-02-17 13:54:18 +03:00
'SELECT f0.id AS f0__id, f0.username AS f0__username FROM forum_users f0 WHERE (f0.username = :name OR f0.username = :name2) AND f0.id = :id'
2008-05-28 10:01:04 +04:00
);
}
2009-01-21 21:25:05 +03:00
public function testAggregateFunctionWithDistinctInSelect ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT COUNT(DISTINCT u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-02-17 13:54:18 +03:00
'SELECT COUNT(DISTINCT c0.name) AS dctrn__0 FROM cms_users c0'
2009-01-21 21:25:05 +03:00
);
}
2008-05-28 10:01:04 +04:00
2008-08-23 20:42:23 +04:00
// Ticket #668
public function testKeywordUsageInStringParam ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
" SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.name LIKE '%foo OR bar%' " ,
2009-02-17 13:54:18 +03:00
" SELECT c0.name AS c0__name FROM cms_users c0 WHERE c0.name LIKE '%foo OR bar%' "
2008-08-23 20:42:23 +04:00
);
}
2009-01-21 21:25:05 +03:00
public function testArithmeticExpressionsSupportedInWherePart ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.id AS c0__id, c0.status AS c0__status, c0.username AS c0__username, c0.name AS c0__name FROM cms_users c0 WHERE ((c0.id + 5000) * c0.id + 3) < 10000000'
2009-01-21 21:25:05 +03:00
);
}
public function testPlainJoinWithoutClause ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.id AS c0__id, c1.id AS c1__id FROM cms_users c0 LEFT JOIN cms_articles c1 ON c0.id = c1.user_id'
2009-01-21 21:25:05 +03:00
);
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.id AS c0__id, c1.id AS c1__id FROM cms_users c0 INNER JOIN cms_articles c1 ON c0.id = c1.user_id'
2009-01-21 21:25:05 +03:00
);
}
2008-08-23 20:42:23 +04:00
2009-01-21 21:25:05 +03:00
public function testDeepJoin ()
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u.id, a.id, p, c.id from Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a JOIN u.phonenumbers p JOIN a.comments c' ,
2009-02-17 13:54:18 +03:00
'SELECT c0.id AS c0__id, c1.id AS c1__id, c2.phonenumber AS c2__phonenumber, c3.id AS c3__id FROM cms_users c0 INNER JOIN cms_articles c1 ON c0.id = c1.user_id INNER JOIN cms_phonenumbers c2 ON c0.id = c2.user_id INNER JOIN cms_comments c3 ON c1.id = c3.article_id'
2009-01-21 21:25:05 +03:00
);
}
2009-03-23 20:39:33 +03:00
public function testTrimFunction ()
{
$this -> assertSqlGeneration (
" SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE TRIM(TRAILING ' ' FROM u.name) = 'someone' " ,
" SELECT c0.name AS c0__name FROM cms_users c0 WHERE TRIM(TRAILING ' ' FROM c0.name) = 'someone' "
);
}
2009-01-21 21:25:05 +03:00
2009-03-28 20:10:41 +03:00
// Ticket 894
public function testBetweenDeclarationWithInputParameter ()
{
$this -> assertSqlGeneration (
" SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id BETWEEN ?1 AND ?2 " ,
" SELECT c0.name AS c0__name FROM cms_users c0 WHERE c0.id BETWEEN ? AND ? "
);
}
public function testFunctionalExpressionsSupportedInWherePart ()
2009-01-21 21:25:05 +03:00
{
$this -> assertSqlGeneration (
2009-03-28 20:10:41 +03:00
" SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE TRIM(u.name) = 'someone' " ,
2009-01-21 21:25:05 +03:00
// String quoting in the SQL usually depends on the database platform.
// This test works with a mock connection which uses ' for string quoting.
2009-03-28 20:10:41 +03:00
" SELECT c0.name AS c0__name FROM cms_users c0 WHERE TRIM(FROM c0.name) = 'someone' "
2009-01-21 21:25:05 +03:00
);
2009-03-28 20:10:41 +03:00
}
2009-01-21 21:25:05 +03:00
2008-08-23 20:42:23 +04:00
// Ticket #973
public function testSingleInValueWithoutSpace ()
{
$this -> assertSqlGeneration (
2009-03-28 20:10:41 +03:00
" SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id IN(46) " ,
" SELECT c0.name AS c0__name FROM cms_users c0 WHERE c0.id IN (46) "
2008-08-23 20:42:23 +04:00
);
}
2009-03-28 20:10:41 +03:00
public function testInExpressionSupportedInWherePart ()
2008-08-23 20:42:23 +04:00
{
$this -> assertSqlGeneration (
2009-03-28 20:10:41 +03:00
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN (1, 2)' ,
'SELECT c0.id AS c0__id, c0.status AS c0__status, c0.username AS c0__username, c0.name AS c0__name FROM cms_users c0 WHERE c0.id IN (1, 2)'
2008-08-23 20:42:23 +04:00
);
}
2009-03-28 20:10:41 +03:00
public function testNotInExpressionSupportedInWherePart ()
2008-05-28 10:01:04 +04:00
{
$this -> assertSqlGeneration (
2009-03-28 20:10:41 +03:00
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN (1)' ,
'SELECT c0.id AS c0__id, c0.status AS c0__status, c0.username AS c0__username, c0.name AS c0__name FROM cms_users c0 WHERE c0.id NOT IN (1)'
2008-05-28 10:01:04 +04:00
);
}
2009-03-28 20:10:41 +03:00
public function testConcatFunction ()
2008-05-24 22:18:37 +04:00
{
2009-03-28 20:10:41 +03:00
$connMock = $this -> _em -> getConnection ();
$orgPlatform = $connMock -> getDatabasePlatform ();
$connMock -> setDatabasePlatform ( new \Doctrine\DBAL\Platforms\MySqlPlatform );
2008-05-27 08:52:50 +04:00
$this -> assertSqlGeneration (
2009-03-28 20:10:41 +03:00
" SELECT u.id FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE CONCAT(u.name, 's') = ?1 " ,
" SELECT c0.id AS c0__id FROM cms_users c0 WHERE CONCAT(c0.name, 's') = ? "
);
$this -> assertSqlGeneration (
" SELECT CONCAT(u.id, u.name) FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " ,
" SELECT CONCAT(c0.id, c0.name) AS dctrn__0 FROM cms_users c0 WHERE c0.id = ? "
2008-05-27 08:52:50 +04:00
);
2009-03-28 20:10:41 +03:00
$connMock -> setDatabasePlatform ( new \Doctrine\DBAL\Platforms\PostgreSqlPlatform );
$this -> assertSqlGeneration (
" SELECT u.id FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE CONCAT(u.name, 's') = ?1 " ,
" SELECT c0.id AS c0__id FROM cms_users c0 WHERE c0.name || 's' = ? "
);
$this -> assertSqlGeneration (
" SELECT CONCAT(u.id, u.name) FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " ,
" SELECT c0.id || c0.name AS dctrn__0 FROM cms_users c0 WHERE c0.id = ? "
);
$connMock -> setDatabasePlatform ( $orgPlatform );
}
2008-05-24 22:18:37 +04:00
}