2008-05-24 22:18:37 +04:00
< ? php
2009-01-22 22:38:10 +03:00
namespace Doctrine\Tests\ORM\Query ;
2010-01-01 01:48:51 +03:00
use Doctrine\ORM\Query ,
Doctrine\Common\DoctrineException ;
2009-10-15 18:39:43 +04:00
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 );
2009-10-15 18:39:43 +04:00
$query -> setHint ( Query :: HINT_FORCE_PARTIAL_LOAD , true );
2008-05-27 08:52:50 +04:00
parent :: assertEquals ( $sqlToBeConfirmed , $query -> getSql ());
$query -> free ();
2010-01-01 01:48:51 +03:00
} catch ( DoctrineException $e ) {
2008-05-27 08:52:50 +04:00
$this -> fail ( $e -> getMessage ());
}
}
2009-07-08 11:48:44 +04:00
public function testSupportsSelectForAllFields ()
2008-05-28 10:01:04 +04:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_'
2008-05-28 10:01:04 +04:00
);
2009-07-08 11:48:44 +04:00
}
2008-05-28 10:01:04 +04:00
2009-07-08 11:48:44 +04:00
public function testSupportsSelectForOneField ()
{
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-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0 FROM cms_users c0_'
2008-05-28 10:01:04 +04:00
);
}
2010-01-01 01:48:51 +03:00
public function testSupportsSelectForOneNestedField ()
{
$this -> assertSqlGeneration (
'SELECT a.user.id FROM Doctrine\Tests\Models\CMS\CmsArticle a' ,
'SELECT c0_.id AS id0 FROM cms_articles c1_ INNER JOIN cms_users c0_ ON c1_.user_id = c0_.id'
);
}
public function testSupportsSelectForAllNestedField ()
{
$this -> assertSqlGeneration (
'SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a ORDER BY a.user.name ASC' ,
'SELECT c0_.id AS id0, c0_.topic AS topic1, c0_.text AS text2 FROM cms_articles c0_ INNER JOIN cms_users c1_ ON c0_.user_id = c1_.id ORDER BY c1_.name ASC'
);
}
2008-05-28 10:01:04 +04:00
2009-07-08 11:48:44 +04:00
public function testSupportsSelectForMultipleColumnsOfASingleComponent ()
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-04-09 22:12:48 +04:00
'SELECT c0_.username AS username0, c0_.name AS name1 FROM cms_users c0_'
2008-05-27 08:52:50 +04:00
);
2008-05-24 22:18:37 +04:00
}
2009-07-08 11:48:44 +04:00
public function testSupportsSelectWithCollectionAssociationJoin ()
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-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.phonenumber AS phonenumber4 FROM cms_users c0_ INNER JOIN cms_phonenumbers c1_ ON c0_.id = c1_.user_id'
2008-05-28 10:01:04 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsSelectWithSingleValuedAssociationJoin ()
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-04-09 22:12:48 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1, f1_.id AS id2 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
2009-07-28 15:43:42 +04:00
public function testSupportsOrderByWithAscAsDefault ()
{
$this -> assertSqlGeneration (
'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id' ,
2009-07-28 20:36:24 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
2009-07-28 15:43:42 +04:00
);
}
public function testSupportsOrderByAsc ()
{
$this -> assertSqlGeneration (
'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id asc' ,
2009-07-28 20:36:24 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
2009-07-28 15:43:42 +04:00
);
}
public function testSupportsOrderByDesc ()
{
$this -> assertSqlGeneration (
'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id desc' ,
2009-07-28 20:36:24 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id DESC'
2009-07-28 15:43:42 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsSelectDistinct ()
2008-05-28 10:01:04 +04:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-04-09 22:12:48 +04:00
'SELECT DISTINCT c0_.name AS name0 FROM cms_users c0_'
2008-05-28 10:01:04 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsAggregateFunctionInSelectedFields ()
2008-05-28 10:01:04 +04:00
{
$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-04-09 22:12:48 +04:00
'SELECT COUNT(c0_.id) AS sclr0 FROM cms_users c0_ GROUP BY c0_.id'
2008-05-28 10:01:04 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsWhereClauseWithPositionalParameter ()
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-04-09 22:12:48 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.id = ?'
2009-01-19 21:40:12 +03:00
);
}
2009-01-19 22:24:40 +03:00
2009-07-08 11:48:44 +04:00
public function testSupportsWhereClauseWithNamedParameter ()
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-06-14 21:34:28 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ?'
2008-05-28 10:01:04 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsWhereAndClauseWithNamedParameters ()
2009-01-21 21:25:05 +03:00
{
$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-06-14 21:34:28 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ? AND f0_.username = ?'
2009-01-21 21:25:05 +03:00
);
}
2008-05-28 10:01:04 +04:00
2009-07-08 11:48:44 +04:00
public function testSupportsCombinedWhereClauseWithNamedParameter ()
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-06-14 21:34:28 +04:00
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE (f0_.username = ? OR f0_.username = ?) AND f0_.id = ?'
2008-05-28 10:01:04 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsAggregateFunctionInASelectDistinct ()
2009-01-21 21:25:05 +03:00
{
$this -> assertSqlGeneration (
2009-01-22 22:38:10 +03:00
'SELECT COUNT(DISTINCT u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u' ,
2009-04-09 22:12:48 +04:00
'SELECT COUNT(DISTINCT c0_.name) AS sclr0 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
2009-07-08 11:48:44 +04:00
public function testSupportsASqlKeywordInAStringLiteralParam ()
2008-08-23 20:42:23 +04:00
{
$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-04-09 22:12:48 +04:00
" SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.name LIKE '%foo OR bar%' "
2008-08-23 20:42:23 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsArithmeticExpressionsInWherePart ()
2009-01-21 21:25:05 +03:00
{
$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-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE ((c0_.id + 5000) * c0_.id + 3) < 10000000'
2009-01-21 21:25:05 +03:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsPlainJoinWithoutClause ()
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 LEFT JOIN u.articles a' ,
2009-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c1_.id AS id1 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-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c1_.id AS id1 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-07-08 11:48:44 +04:00
public function testSupportsMultipleJoins ()
2009-01-21 21:25:05 +03:00
{
$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-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c1_.id AS id1, c2_.phonenumber AS phonenumber2, c3_.id AS id3 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
2009-07-08 11:48:44 +04:00
public function testSupportsTrimFunction ()
2009-03-23 20:39:33 +03:00
{
$this -> assertSqlGeneration (
" SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE TRIM(TRAILING ' ' FROM u.name) = 'someone' " ,
2009-04-09 22:12:48 +04:00
" SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(TRAILING ' ' FROM c0_.name) = 'someone' "
2009-03-23 20:39:33 +03:00
);
}
2009-01-21 21:25:05 +03:00
2009-03-28 20:10:41 +03:00
// Ticket 894
2009-07-08 11:48:44 +04:00
public function testSupportsBetweenClauseWithPositionalParameters ()
2009-03-28 20:10:41 +03:00
{
$this -> assertSqlGeneration (
" SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id BETWEEN ?1 AND ?2 " ,
2009-04-09 22:12:48 +04:00
" SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.id BETWEEN ? AND ? "
2009-03-28 20:10:41 +03:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsFunctionalExpressionsInWherePart ()
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-04-09 22:12:48 +04:00
" SELECT c0_.name AS name0 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
2009-07-08 11:48:44 +04:00
public function testSupportsSingleValuedInExpressionWithoutSpacesInWherePart ()
2008-08-23 20:42:23 +04:00
{
$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) " ,
2009-04-09 22:12:48 +04:00
" SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.id IN (46) "
2008-08-23 20:42:23 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsMultipleValuedInExpressionInWherePart ()
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)' ,
2009-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id IN (1, 2)'
2008-08-23 20:42:23 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsNotInExpressionInWherePart ()
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)' ,
2009-04-09 22:12:48 +04:00
'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id NOT IN (1)'
2008-05-28 10:01:04 +04:00
);
}
2009-07-08 11:48:44 +04:00
public function testSupportsConcatFunctionForMysqlAndPostgresql ()
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 " ,
2009-04-09 22:12:48 +04:00
" SELECT c0_.id AS id0 FROM cms_users c0_ WHERE CONCAT(c0_.name, 's') = ? "
2009-03-28 20:10:41 +03:00
);
$this -> assertSqlGeneration (
" SELECT CONCAT(u.id, u.name) FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " ,
2009-04-09 22:12:48 +04:00
" SELECT CONCAT(c0_.id, c0_.name) AS sclr0 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 " ,
2009-04-09 22:12:48 +04:00
" SELECT c0_.id AS id0 FROM cms_users c0_ WHERE c0_.name || 's' = ? "
2009-03-28 20:10:41 +03:00
);
$this -> assertSqlGeneration (
" SELECT CONCAT(u.id, u.name) FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " ,
2009-04-09 22:12:48 +04:00
" SELECT c0_.id || c0_.name AS sclr0 FROM cms_users c0_ WHERE c0_.id = ? "
2009-03-28 20:10:41 +03:00
);
$connMock -> setDatabasePlatform ( $orgPlatform );
}
2009-05-17 23:27:12 +04:00
2009-07-08 11:48:44 +04:00
public function testSupportsExistsExpressionInWherePartWithCorrelatedSubquery ()
2009-05-17 23:27:12 +04:00
{
$this -> assertSqlGeneration (
'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.phonenumber = u.id)' ,
'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT c1_.phonenumber FROM cms_phonenumbers c1_ WHERE c1_.phonenumber = c0_.id)'
);
}
2009-06-14 21:34:28 +04:00
2009-07-08 11:48:44 +04:00
public function testSupportsMemberOfExpression ()
2009-06-14 21:34:28 +04:00
{
// "Get all users who have $phone as a phonenumber." (*cough* doesnt really make sense...)
$q1 = $this -> _em -> createQuery ( 'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.phonenumbers' );
2009-10-15 18:39:43 +04:00
$q1 -> setHint ( Query :: HINT_FORCE_PARTIAL_LOAD , true );
2009-06-14 21:34:28 +04:00
$phone = new \Doctrine\Tests\Models\CMS\CmsPhonenumber ;
$phone -> phonenumber = 101 ;
$q1 -> setParameter ( 'param' , $phone );
$this -> assertEquals (
'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT 1 FROM cms_phonenumbers c1_ WHERE c0_.id = c1_.user_id AND c1_.phonenumber = ?)' ,
$q1 -> getSql ()
);
// "Get all users who are members of $group."
$q2 = $this -> _em -> createQuery ( 'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.groups' );
2009-10-15 18:39:43 +04:00
$q2 -> setHint ( Query :: HINT_FORCE_PARTIAL_LOAD , true );
2009-06-14 21:34:28 +04:00
$group = new \Doctrine\Tests\Models\CMS\CmsGroup ;
$group -> id = 101 ;
$q2 -> setParameter ( 'param' , $group );
$this -> assertEquals (
'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT 1 FROM cms_users_groups c1_ INNER JOIN cms_groups c2_ ON c1_.user_id = c0_.id WHERE c1_.group_id = c2_.id AND c2_.id = ?)' ,
$q2 -> getSql ()
);
// "Get all persons who have $person as a friend."
// Tough one: Many-many self-referencing ("friends") with class table inheritance
2009-08-17 21:58:16 +04:00
$q3 = $this -> _em -> createQuery ( 'SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p WHERE :param MEMBER OF p.friends' );
2009-06-14 21:34:28 +04:00
$person = new \Doctrine\Tests\Models\Company\CompanyPerson ;
$this -> _em -> getClassMetadata ( get_class ( $person )) -> setIdentifierValues ( $person , 101 );
$q3 -> setParameter ( 'param' , $person );
$this -> assertEquals (
2009-10-22 23:12:00 +04:00
'SELECT c0_.id AS id0, c0_.name AS name1, c1_.title AS title2, c1_.car_id AS car_id3, c2_.salary AS salary4, c2_.department AS department5, c0_.discr AS discr6, c0_.spouse_id AS spouse_id7 FROM company_persons c0_ LEFT JOIN company_managers c1_ ON c0_.id = c1_.id LEFT JOIN company_employees c2_ ON c0_.id = c2_.id WHERE EXISTS (SELECT 1 FROM company_persons_friends c3_ INNER JOIN company_persons c4_ ON c3_.person_id = c0_.id WHERE c3_.friend_id = c4_.id AND c4_.id = ?)' ,
2009-06-14 21:34:28 +04:00
$q3 -> getSql ()
);
}
2009-05-17 23:27:12 +04:00
2009-07-08 11:48:44 +04:00
public function testSupportsCurrentDateFunction ()
2009-06-20 16:59:33 +04:00
{
2009-10-15 18:39:43 +04:00
$q = $this -> _em -> createQuery ( 'SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_date()' );
$q -> setHint ( Query :: HINT_FORCE_PARTIAL_LOAD , true );
$this -> assertEquals ( 'SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.datetime > CURRENT_DATE' , $q -> getSql ());
2009-06-20 16:59:33 +04:00
}
2009-07-08 11:48:44 +04:00
public function testSupportsCurrentTimeFunction ()
2009-06-20 18:24:21 +04:00
{
2009-10-15 18:39:43 +04:00
$q = $this -> _em -> createQuery ( 'SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.time > current_time()' );
$q -> setHint ( Query :: HINT_FORCE_PARTIAL_LOAD , true );
$this -> assertEquals ( 'SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.time > CURRENT_TIME' , $q -> getSql ());
2009-06-20 18:24:21 +04:00
}
2009-07-08 11:48:44 +04:00
public function testSupportsCurrentTimestampFunction ()
2009-06-20 18:24:21 +04:00
{
2009-10-15 18:39:43 +04:00
$q = $this -> _em -> createQuery ( 'SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_timestamp()' );
$q -> setHint ( Query :: HINT_FORCE_PARTIAL_LOAD , true );
$this -> assertEquals ( 'SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.datetime > CURRENT_TIMESTAMP' , $q -> getSql ());
2009-06-20 18:24:21 +04:00
}
2009-06-14 21:34:28 +04:00
/* public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition ()
2009-05-17 23:27:12 +04:00
{
$this -> assertSqlGeneration (
// DQL
// The result of this query consists of all employees whose spouses are also employees.
' SELECT DISTINCT emp FROM Doctrine\Tests\Models\CMS\CmsEmployee emp
WHERE EXISTS (
SELECT spouseEmp
FROM Doctrine\Tests\Models\CMS\CmsEmployee spouseEmp
WHERE spouseEmp = emp . spouse ) ' ,
// SQL
'SELECT DISTINCT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_'
. ' WHERE EXISTS ('
. 'SELECT c1_.id FROM cms_employees c1_ WHERE c1_.id = c0_.spouse_id'
. ')'
);
2009-06-14 21:34:28 +04:00
} */
2009-07-14 02:59:36 +04:00
public function testLimitFromQueryClass ()
{
$q = $this -> _em
-> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' )
-> setMaxResults ( 10 );
$this -> assertEquals ( 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LIMIT 10' , $q -> getSql ());
}
public function testLimitAndOffsetFromQueryClass ()
{
$q = $this -> _em
-> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' )
-> setMaxResults ( 10 )
-> setFirstResult ( 0 );
2009-10-12 23:10:41 +04:00
$this -> assertEquals ( 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LIMIT 10 OFFSET 0' , $q -> getSql ());
2009-07-14 02:59:36 +04:00
}
2009-08-05 19:47:41 +04:00
public function testSizeFunction ()
{
$this -> assertSqlGeneration (
" SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE SIZE(u.phonenumbers) > 1 " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(c1_.user_id) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) > 1 "
);
}
2010-02-10 05:31:55 +03:00
public function testSizeFunctionSupportsManyToMany ()
{
$this -> assertSqlGeneration (
" SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE SIZE(u.groups) > 1 " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(c1_.user_id, c1_.group_id) FROM cms_users_groups c1_ WHERE c1_.user_id = c0_.id) > 1 "
);
}
2009-08-05 19:47:41 +04:00
public function testEmptyCollectionComparisonExpression ()
{
$this -> assertSqlGeneration (
" SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.phonenumbers IS EMPTY " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(c1_.user_id) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) = 0 "
);
$this -> assertSqlGeneration (
" SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.phonenumbers IS NOT EMPTY " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(c1_.user_id) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) > 0 "
);
}
2009-08-13 14:13:06 +04:00
2009-08-16 20:36:00 +04:00
public function testNestedExpressions ()
{
$this -> assertSqlGeneration (
" select u from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.id > 10 and u.id < 42 and ((u.id * 2) > 5) " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id > 10 AND c0_.id < 42 AND ((c0_.id * 2) > 5) "
);
}
public function testNestedExpressions2 ()
{
$this -> assertSqlGeneration (
" select u from Doctrine \T ests \ Models \ CMS \ CmsUser u where (u.id > 10) and (u.id < 42 and ((u.id * 2) > 5)) or u.id <> 42 " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id > 10) AND (c0_.id < 42 AND ((c0_.id * 2) > 5)) OR c0_.id <> 42 "
);
}
public function testNestedExpressions3 ()
{
$this -> assertSqlGeneration (
" select u from Doctrine \T ests \ Models \ CMS \ CmsUser u where (u.id > 10) and (u.id between 1 and 10 or u.id in (1, 2, 3, 4, 5)) " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id > 10) AND (c0_.id BETWEEN 1 AND 10 OR c0_.id IN (1, 2, 3, 4, 5)) "
);
}
2009-09-11 23:50:48 +04:00
public function testOrderByCollectionAssociationSize ()
{
$this -> assertSqlGeneration (
" select u, size(u.articles) as numArticles from Doctrine \T ests \ Models \ CMS \ CmsUser u order by numArticles " ,
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT COUNT(c1_.user_id) FROM cms_articles c1_ WHERE c1_.user_id = c0_.id) AS sclr4 FROM cms_users c0_ ORDER BY sclr4 ASC "
);
}
2009-10-13 14:48:46 +04:00
public function testBooleanLiteralInWhereOnSqlite ()
{
$oldPlat = $this -> _em -> getConnection () -> getDatabasePlatform ();
$this -> _em -> getConnection () -> setDatabasePlatform ( new \Doctrine\DBAL\Platforms\SqlitePlatform );
$this -> assertSqlGeneration (
" SELECT b FROM Doctrine \T ests \ Models \ Generic \ BooleanModel b WHERE b.booleanField = true " ,
" SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 1 "
);
$this -> assertSqlGeneration (
" SELECT b FROM Doctrine \T ests \ Models \ Generic \ BooleanModel b WHERE b.booleanField = false " ,
" SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 0 "
);
$this -> _em -> getConnection () -> setDatabasePlatform ( $oldPlat );
}
public function testBooleanLiteralInWhereOnPostgres ()
{
$oldPlat = $this -> _em -> getConnection () -> getDatabasePlatform ();
$this -> _em -> getConnection () -> setDatabasePlatform ( new \Doctrine\DBAL\Platforms\PostgreSqlPlatform );
$this -> assertSqlGeneration (
" SELECT b FROM Doctrine \T ests \ Models \ Generic \ BooleanModel b WHERE b.booleanField = true " ,
" SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 'true' "
);
$this -> assertSqlGeneration (
" SELECT b FROM Doctrine \T ests \ Models \ Generic \ BooleanModel b WHERE b.booleanField = false " ,
" SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 'false' "
);
$this -> _em -> getConnection () -> setDatabasePlatform ( $oldPlat );
}
2009-12-27 06:26:15 +03:00
/* Not yet implemented, needs more thought */
2009-08-13 14:13:06 +04:00
public function testSingleValuedAssociationFieldInWhere ()
{
2009-12-27 06:26:15 +03:00
/* $this -> assertSqlGeneration (
2009-08-13 14:13:06 +04:00
" SELECT p FROM Doctrine \T ests \ Models \ CMS \ CmsPhonenumber p WHERE p.user = ?1 " ,
2009-12-27 06:26:15 +03:00
" SELECT c0_.id AS id0, c0_user_id AS user_id1, c0_.phonenumber AS phonenumber2 FROM cms_phonenumbers c0_ WHERE c0_.user_id = ? "
2009-08-13 14:13:06 +04:00
);
$this -> assertSqlGeneration (
" SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.address = ?1 " ,
2009-12-27 06:26:15 +03:00
//"SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id = (SELECT c1_.user_id FROM cms_addresses c1_ WHERE c1_.id = ?)"
" SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE EXISTS (SELECT 1 FROM cms_addresses c1_ WHERE c1_.user_id = c0_.id AND c1_.id = ?) "
); */
}
2009-07-28 15:43:42 +04:00
}