PageRenderTime 9ms CodeModel.GetById 3ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php

https://github.com/dev4apps/chamilo-lms
PHP | 1807 lines | 1331 code | 220 blank | 256 comment | 12 complexity | 093e6a5d4a921e5b6a6ac3e087e411a8 MD5 | raw file
   1<?php
   2
   3namespace Doctrine\Tests\ORM\Query;
   4
   5use Doctrine\DBAL\Types\Type as DBALType;
   6use Doctrine\ORM\Query;
   7
   8require_once __DIR__ . '/../../TestInit.php';
   9
  10class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
  11{
  12    private $_em;
  13
  14    protected function setUp()
  15    {
  16        $this->_em = $this->_getTestEntityManager();
  17    }
  18
  19    /**
  20     * Assert a valid SQL generation.
  21     *
  22     * @param string $dqlToBeTested
  23     * @param string $sqlToBeConfirmed
  24     * @param array $queryHints
  25     * @param array $queryParams
  26     */
  27    public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed, array $queryHints = array(), array $queryParams = array())
  28    {
  29        try {
  30            $query = $this->_em->createQuery($dqlToBeTested);
  31
  32            foreach ($queryParams AS $name => $value) {
  33                $query->setParameter($name, $value);
  34            }
  35
  36            $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
  37                  ->useQueryCache(false);
  38
  39            foreach ($queryHints AS $name => $value) {
  40                $query->setHint($name, $value);
  41            }
  42
  43            $sqlGenerated = $query->getSQL();
  44
  45            parent::assertEquals(
  46                $sqlToBeConfirmed,
  47                $sqlGenerated,
  48                sprintf('"%s" is not equal of "%s"', $sqlGenerated, $sqlToBeConfirmed)
  49            );
  50
  51            $query->free();
  52        } catch (\Exception $e) {
  53            $this->fail($e->getMessage() ."\n".$e->getTraceAsString());
  54        }
  55    }
  56
  57    /**
  58     * Asser an invalid SQL generation.
  59     *
  60     * @param string $dqlToBeTested
  61     * @param string $expectedException
  62     * @param array $queryHints
  63     * @param array $queryParams
  64     */
  65    public function assertInvalidSqlGeneration($dqlToBeTested, $expectedException, array $queryHints = array(), array $queryParams = array())
  66    {
  67        $this->setExpectedException($expectedException);
  68
  69        $query = $this->_em->createQuery($dqlToBeTested);
  70
  71        foreach ($queryParams AS $name => $value) {
  72            $query->setParameter($name, $value);
  73        }
  74
  75        $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
  76              ->useQueryCache(false);
  77
  78        foreach ($queryHints AS $name => $value) {
  79            $query->setHint($name, $value);
  80        }
  81
  82        $sql = $query->getSql();
  83        $query->free();
  84
  85        // If we reached here, test failed
  86        $this->fail($sql);
  87    }
  88
  89
  90    public function testSupportsSelectForAllFields()
  91    {
  92        $this->assertSqlGeneration(
  93            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u',
  94            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_'
  95        );
  96    }
  97
  98    public function testSupportsSelectForOneField()
  99    {
 100        $this->assertSqlGeneration(
 101            'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u',
 102            'SELECT c0_.id AS id0 FROM cms_users c0_'
 103        );
 104    }
 105
 106    public function testSupportsSelectForOneNestedField()
 107    {
 108        $this->assertSqlGeneration(
 109            'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u',
 110            'SELECT c0_.id AS id0 FROM cms_articles c1_ INNER JOIN cms_users c0_ ON c1_.user_id = c0_.id'
 111        );
 112    }
 113
 114    public function testSupportsSelectForAllNestedField()
 115    {
 116        $this->assertSqlGeneration(
 117            'SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u ORDER BY u.name ASC',
 118            'SELECT c0_.id AS id0, c0_.topic AS topic1, c0_.text AS text2, c0_.version AS version3 FROM cms_articles c0_ INNER JOIN cms_users c1_ ON c0_.user_id = c1_.id ORDER BY c1_.name ASC'
 119        );
 120    }
 121
 122    public function testSupportsSelectForMultipleColumnsOfASingleComponent()
 123    {
 124        $this->assertSqlGeneration(
 125            'SELECT u.username, u.name FROM Doctrine\Tests\Models\CMS\CmsUser u',
 126            'SELECT c0_.username AS username0, c0_.name AS name1 FROM cms_users c0_'
 127        );
 128    }
 129
 130    public function testSupportsSelectUsingMultipleFromComponents()
 131    {
 132        $this->assertSqlGeneration(
 133            'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE u = p.user',
 134            '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_, cms_phonenumbers c1_ WHERE c0_.id = c1_.user_id'
 135        );
 136    }
 137
 138    public function testSupportsJoinOnMultipleComponents()
 139    {
 140        $this->assertSqlGeneration(
 141            'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN Doctrine\Tests\Models\CMS\CmsPhonenumber p WITH u = p.user',
 142            '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)'
 143        );
 144    }
 145
 146    public function testSupportsJoinOnMultipleComponentsWithJoinedInheritanceType()
 147    {
 148        $this->assertSqlGeneration(
 149            'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
 150            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c0_.discr AS discr5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id INNER JOIN company_managers c2_ INNER JOIN company_employees c3_ ON c2_.id = c3_.id INNER JOIN company_persons c4_ ON c2_.id = c4_.id AND (c0_.id = c4_.id)'
 151        );
 152
 153        $this->assertSqlGeneration(
 154            'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e LEFT JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
 155            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c0_.discr AS discr5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id LEFT JOIN company_managers c2_ INNER JOIN company_employees c3_ ON c2_.id = c3_.id INNER JOIN company_persons c4_ ON c2_.id = c4_.id ON (c0_.id = c4_.id)'
 156        );
 157    }
 158
 159    public function testSupportsSelectWithCollectionAssociationJoin()
 160    {
 161        $this->assertSqlGeneration(
 162            'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p',
 163            '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'
 164        );
 165    }
 166
 167    public function testSupportsSelectWithSingleValuedAssociationJoin()
 168    {
 169        $this->assertSqlGeneration(
 170            'SELECT u, a FROM Doctrine\Tests\Models\Forum\ForumUser u JOIN u.avatar a',
 171            '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'
 172        );
 173    }
 174
 175    public function testSelectCorrelatedSubqueryComplexMathematicalExpression()
 176    {
 177        $this->assertSqlGeneration(
 178            'SELECT (SELECT (count(p.phonenumber)+5)*10 FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p JOIN p.user ui WHERE ui.id = u.id) AS c FROM Doctrine\Tests\Models\CMS\CmsUser u',
 179            'SELECT (SELECT (count(c0_.phonenumber) + 5) * 10 AS sclr1 FROM cms_phonenumbers c0_ INNER JOIN cms_users c1_ ON c0_.user_id = c1_.id WHERE c1_.id = c2_.id) AS sclr0 FROM cms_users c2_'
 180        );
 181    }
 182
 183    public function testSelectComplexMathematicalExpression()
 184    {
 185        $this->assertSqlGeneration(
 186            'SELECT (count(p.phonenumber)+5)*10 FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p JOIN p.user ui WHERE ui.id = ?1',
 187            'SELECT (count(c0_.phonenumber) + 5) * 10 AS sclr0 FROM cms_phonenumbers c0_ INNER JOIN cms_users c1_ ON c0_.user_id = c1_.id WHERE c1_.id = ?'
 188        );
 189    }
 190
 191    /* NOT (YET?) SUPPORTED.
 192       Can be supported if SimpleSelectExpresion supports SingleValuedPathExpression instead of StateFieldPathExpression.
 193
 194    public function testSingleAssociationPathExpressionInSubselect()
 195    {
 196        $this->assertSqlGeneration(
 197            'SELECT (SELECT p.user FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user = u) user_id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1',
 198            'SELECT (SELECT c0_.user_id FROM cms_phonenumbers c0_ WHERE c0_.user_id = c1_.id) AS sclr0 FROM cms_users c1_ WHERE c1_.id = ?'
 199        );
 200    }*/
 201
 202    /**
 203     * @group DDC-1077
 204     */
 205    public function testConstantValueInSelect()
 206    {
 207        $this->assertSqlGeneration(
 208            "SELECT u.name, 'foo' AS bar FROM Doctrine\Tests\Models\CMS\CmsUser u",
 209            "SELECT c0_.name AS name0, 'foo' AS sclr1 FROM cms_users c0_"
 210        );
 211    }
 212
 213    public function testSupportsOrderByWithAscAsDefault()
 214    {
 215        $this->assertSqlGeneration(
 216            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id',
 217            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
 218        );
 219    }
 220
 221    public function testSupportsOrderByAsc()
 222    {
 223        $this->assertSqlGeneration(
 224            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id asc',
 225            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
 226        );
 227    }
 228    public function testSupportsOrderByDesc()
 229    {
 230        $this->assertSqlGeneration(
 231            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id desc',
 232            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id DESC'
 233        );
 234    }
 235
 236    public function testSupportsSelectDistinct()
 237    {
 238        $this->assertSqlGeneration(
 239            'SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u',
 240            'SELECT DISTINCT c0_.name AS name0 FROM cms_users c0_'
 241        );
 242    }
 243
 244    public function testSupportsAggregateFunctionInSelectedFields()
 245    {
 246        $this->assertSqlGeneration(
 247            'SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id',
 248            'SELECT COUNT(c0_.id) AS sclr0 FROM cms_users c0_ GROUP BY c0_.id'
 249        );
 250    }
 251
 252    public function testSupportsAggregateFunctionWithSimpleArithmetic()
 253    {
 254        $this->assertSqlGeneration(
 255            'SELECT MAX(u.id + 4) * 2 FROM Doctrine\Tests\Models\CMS\CmsUser u',
 256            'SELECT MAX(c0_.id + 4) * 2 AS sclr0 FROM cms_users c0_'
 257        );
 258    }
 259
 260    public function testSupportsWhereClauseWithPositionalParameter()
 261    {
 262        $this->assertSqlGeneration(
 263            'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.id = ?1',
 264            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.id = ?'
 265        );
 266    }
 267
 268    public function testSupportsWhereClauseWithNamedParameter()
 269    {
 270        $this->assertSqlGeneration(
 271            'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name',
 272            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ?'
 273        );
 274    }
 275
 276    public function testSupportsWhereAndClauseWithNamedParameters()
 277    {
 278        $this->assertSqlGeneration(
 279            'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name and u.username = :name2',
 280            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ? AND f0_.username = ?'
 281        );
 282    }
 283
 284    public function testSupportsCombinedWhereClauseWithNamedParameter()
 285    {
 286        $this->assertSqlGeneration(
 287            'select u from Doctrine\Tests\Models\Forum\ForumUser u where (u.username = :name OR u.username = :name2) AND u.id = :id',
 288            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE (f0_.username = ? OR f0_.username = ?) AND f0_.id = ?'
 289        );
 290    }
 291
 292    public function testSupportsAggregateFunctionInASelectDistinct()
 293    {
 294        $this->assertSqlGeneration(
 295            'SELECT COUNT(DISTINCT u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u',
 296            'SELECT COUNT(DISTINCT c0_.name) AS sclr0 FROM cms_users c0_'
 297        );
 298    }
 299
 300    // Ticket #668
 301    public function testSupportsASqlKeywordInAStringLiteralParam()
 302    {
 303        $this->assertSqlGeneration(
 304            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name LIKE '%foo OR bar%'",
 305            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.name LIKE '%foo OR bar%'"
 306        );
 307    }
 308
 309    public function testSupportsArithmeticExpressionsInWherePart()
 310    {
 311        $this->assertSqlGeneration(
 312            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000',
 313            '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'
 314        );
 315    }
 316
 317    public function testSupportsMultipleEntitiesInFromClause()
 318    {
 319        $this->assertSqlGeneration(
 320            'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u2 WHERE u.id = u2.id',
 321            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.id AS id4, c1_.topic AS topic5, c1_.text AS text6, c1_.version AS version7 FROM cms_users c0_, cms_articles c1_ INNER JOIN cms_users c2_ ON c1_.user_id = c2_.id WHERE c0_.id = c2_.id'
 322        );
 323    }
 324
 325    public function testSupportsMultipleEntitiesInFromClauseUsingPathExpression()
 326    {
 327        $this->assertSqlGeneration(
 328            'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsArticle a WHERE u.id = a.user',
 329            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.id AS id4, c1_.topic AS topic5, c1_.text AS text6, c1_.version AS version7 FROM cms_users c0_, cms_articles c1_ WHERE c0_.id = c1_.user_id'
 330        );
 331    }
 332
 333    public function testSupportsPlainJoinWithoutClause()
 334    {
 335        $this->assertSqlGeneration(
 336            'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a',
 337            'SELECT c0_.id AS id0, c1_.id AS id1 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id'
 338        );
 339        $this->assertSqlGeneration(
 340            'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a',
 341            'SELECT c0_.id AS id0, c1_.id AS id1 FROM cms_users c0_ INNER JOIN cms_articles c1_ ON c0_.id = c1_.user_id'
 342        );
 343    }
 344
 345    /**
 346     * @group DDC-135
 347     */
 348    public function testSupportsJoinAndWithClauseRestriction()
 349    {
 350        $this->assertSqlGeneration(
 351            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic LIKE '%foo%'",
 352            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic LIKE '%foo%')"
 353        );
 354        $this->assertSqlGeneration(
 355            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a WITH a.topic LIKE '%foo%'",
 356            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ INNER JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic LIKE '%foo%')"
 357        );
 358    }
 359
 360    /**
 361     * @group DDC-135
 362     * @group DDC-177
 363     */
 364    public function testJoinOnClause_NotYetSupported_ThrowsException()
 365    {
 366        $this->setExpectedException('Doctrine\ORM\Query\QueryException');
 367
 368        $sql = $this->_em->createQuery(
 369            "SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a ON a.topic LIKE '%foo%'"
 370        )->getSql();
 371    }
 372
 373    public function testSupportsMultipleJoins()
 374    {
 375        $this->assertSqlGeneration(
 376            'SELECT u.id, a.id, p.phonenumber, c.id from Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a JOIN u.phonenumbers p JOIN a.comments c',
 377            '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'
 378        );
 379    }
 380
 381    public function testSupportsTrimFunction()
 382    {
 383        $this->assertSqlGeneration(
 384            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(TRAILING ' ' FROM u.name) = 'someone'",
 385            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(TRAILING ' ' FROM c0_.name) = 'someone'"
 386        );
 387    }
 388
 389    // Ticket 894
 390    public function testSupportsBetweenClauseWithPositionalParameters()
 391    {
 392        $this->assertSqlGeneration(
 393            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id BETWEEN ?1 AND ?2",
 394            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.id BETWEEN ? AND ?"
 395        );
 396    }
 397
 398    /**
 399     * @group DDC-1802
 400     */
 401    public function testSupportsNotBetweenForSizeFunction()
 402    {
 403        $this->assertSqlGeneration(
 404            "SELECT m.name FROM Doctrine\Tests\Models\StockExchange\Market m WHERE SIZE(m.stocks) NOT BETWEEN ?1 AND ?2",
 405            "SELECT e0_.name AS name0 FROM exchange_markets e0_ WHERE (SELECT COUNT(*) FROM exchange_stocks e1_ WHERE e1_.market_id = e0_.id) NOT BETWEEN ? AND ?"
 406        );
 407    }
 408
 409    public function testSupportsFunctionalExpressionsInWherePart()
 410    {
 411        $this->assertSqlGeneration(
 412            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(u.name) = 'someone'",
 413            // String quoting in the SQL usually depends on the database platform.
 414            // This test works with a mock connection which uses ' for string quoting.
 415            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(c0_.name) = 'someone'"
 416        );
 417    }
 418
 419    public function testSupportsInstanceOfExpressionsInWherePart()
 420    {
 421        $this->assertSqlGeneration(
 422            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee",
 423            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN ('employee')"
 424        );
 425    }
 426
 427    public function testSupportsInstanceOfExpressionInWherePartWithMultipleValues()
 428    {
 429        $this->assertSqlGeneration(
 430            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF (Doctrine\Tests\Models\Company\CompanyEmployee, \Doctrine\Tests\Models\Company\CompanyManager)",
 431            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN ('employee', 'manager')"
 432        );
 433    }
 434
 435    /**
 436     * @group DDC-1194
 437     */
 438    public function testSupportsInstanceOfExpressionsInWherePartPrefixedSlash()
 439    {
 440        $this->assertSqlGeneration(
 441            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\Company\CompanyEmployee",
 442            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN ('employee')"
 443        );
 444    }
 445
 446    /**
 447     * @group DDC-1194
 448     */
 449    public function testSupportsInstanceOfExpressionsInWherePartWithUnrelatedClass()
 450    {
 451        $this->assertInvalidSqlGeneration(
 452            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\CMS\CmsUser",
 453            "Doctrine\ORM\Query\QueryException"
 454        );
 455    }
 456
 457    public function testSupportsInstanceOfExpressionsInWherePartInDeeperLevel()
 458    {
 459        $this->assertSqlGeneration(
 460            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyEmployee u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyManager",
 461            "SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c0_.discr AS discr5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id WHERE c0_.discr IN ('manager')"
 462        );
 463    }
 464
 465    public function testSupportsInstanceOfExpressionsInWherePartInDeepestLevel()
 466    {
 467        $this->assertSqlGeneration(
 468            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyManager u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyManager",
 469            "SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c2_.title AS title5, c0_.discr AS discr6 FROM company_managers c2_ INNER JOIN company_employees c1_ ON c2_.id = c1_.id INNER JOIN company_persons c0_ ON c2_.id = c0_.id WHERE c0_.discr IN ('manager')"
 470        );
 471    }
 472
 473    public function testSupportsInstanceOfExpressionsUsingInputParameterInWherePart()
 474    {
 475        $this->assertSqlGeneration(
 476            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF ?1",
 477            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN (?)",
 478            array(), array(1 => $this->_em->getClassMetadata('Doctrine\Tests\Models\Company\CompanyEmployee'))
 479        );
 480    }
 481
 482    // Ticket #973
 483    public function testSupportsSingleValuedInExpressionWithoutSpacesInWherePart()
 484    {
 485        $this->assertSqlGeneration(
 486            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE IDENTITY(u.email) IN(46)",
 487            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.email_id IN (46)"
 488        );
 489    }
 490
 491    public function testSupportsMultipleValuedInExpressionInWherePart()
 492    {
 493        $this->assertSqlGeneration(
 494            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN (1, 2)',
 495            '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)'
 496        );
 497    }
 498
 499    public function testSupportsNotInExpressionInWherePart()
 500    {
 501        $this->assertSqlGeneration(
 502            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :id NOT IN (1)',
 503            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE ? NOT IN (1)'
 504        );
 505    }
 506
 507    /**
 508     * @group DDC-1802
 509     */
 510    public function testSupportsNotInExpressionForModFunction()
 511    {
 512        $this->assertSqlGeneration(
 513            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE MOD(u.id, 5) NOT IN(1,3,4)",
 514            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE MOD(c0_.id, 5) NOT IN (1, 3, 4)"
 515        );
 516    }
 517
 518    public function testInExpressionWithSingleValuedAssociationPathExpressionInWherePart()
 519    {
 520        $this->assertSqlGeneration(
 521            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.avatar IN (?1, ?2)',
 522            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.avatar_id IN (?, ?)'
 523        );
 524    }
 525
 526    public function testInvalidInExpressionWithSingleValuedAssociationPathExpressionOnInverseSide()
 527    {
 528        // We do not support SingleValuedAssociationPathExpression on inverse side
 529        $this->assertInvalidSqlGeneration(
 530            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address IN (?1, ?2)",
 531            "Doctrine\ORM\Query\QueryException"
 532        );
 533    }
 534
 535    public function testSupportsConcatFunctionForMysqlAndPostgresql()
 536    {
 537        $connMock = $this->_em->getConnection();
 538        $orgPlatform = $connMock->getDatabasePlatform();
 539
 540        $connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform);
 541        $this->assertSqlGeneration(
 542            "SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, 's') = ?1",
 543            "SELECT c0_.id AS id0 FROM cms_users c0_ WHERE CONCAT(c0_.name, 's') = ?"
 544        );
 545        $this->assertSqlGeneration(
 546            "SELECT CONCAT(u.id, u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
 547            "SELECT CONCAT(c0_.id, c0_.name) AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
 548        );
 549
 550        $connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
 551        $this->assertSqlGeneration(
 552            "SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, 's') = ?1",
 553            "SELECT c0_.id AS id0 FROM cms_users c0_ WHERE c0_.name || 's' = ?"
 554        );
 555        $this->assertSqlGeneration(
 556            "SELECT CONCAT(u.id, u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
 557            "SELECT c0_.id || c0_.name AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
 558        );
 559
 560        $connMock->setDatabasePlatform($orgPlatform);
 561    }
 562
 563    public function testSupportsExistsExpressionInWherePartWithCorrelatedSubquery()
 564    {
 565        $this->assertSqlGeneration(
 566            '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)',
 567            'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT c1_.phonenumber FROM cms_phonenumbers c1_ WHERE c1_.phonenumber = c0_.id)'
 568        );
 569    }
 570
 571    /**
 572     * @group DDC-593
 573     */
 574    public function testSubqueriesInComparisonExpression()
 575    {
 576        $this->assertSqlGeneration(
 577            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id >= (SELECT u2.id FROM Doctrine\Tests\Models\CMS\CmsUser u2 WHERE u2.name = :name)) AND (u.id <= (SELECT u3.id FROM Doctrine\Tests\Models\CMS\CmsUser u3 WHERE u3.name = :name))',
 578            '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_.id FROM cms_users c1_ WHERE c1_.name = ?)) AND (c0_.id <= (SELECT c2_.id FROM cms_users c2_ WHERE c2_.name = ?))'
 579        );
 580    }
 581
 582    public function testSupportsMemberOfExpressionOneToMany()
 583    {
 584        // "Get all users who have $phone as a phonenumber." (*cough* doesnt really make sense...)
 585        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.phonenumbers');
 586        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 587
 588        $phone = new \Doctrine\Tests\Models\CMS\CmsPhonenumber;
 589        $phone->phonenumber = 101;
 590        $q->setParameter('param', $phone);
 591
 592        $this->assertEquals(
 593            '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 = ?)',
 594            $q->getSql()
 595        );
 596    }
 597
 598    public function testSupportsMemberOfExpressionManyToMany()
 599    {
 600        // "Get all users who are members of $group."
 601        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.groups');
 602        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 603
 604        $group = new \Doctrine\Tests\Models\CMS\CmsGroup;
 605        $group->id = 101;
 606        $q->setParameter('param', $group);
 607
 608        $this->assertEquals(
 609            '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_.group_id = c2_.id WHERE c1_.user_id = c0_.id AND c2_.id = ?)',
 610            $q->getSql()
 611        );
 612    }
 613
 614    public function testSupportsMemberOfExpressionSelfReferencing()
 615    {
 616        // "Get all persons who have $person as a friend."
 617        // Tough one: Many-many self-referencing ("friends") with class table inheritance
 618        $q = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p WHERE :param MEMBER OF p.friends');
 619        $person = new \Doctrine\Tests\Models\Company\CompanyPerson;
 620        $this->_em->getClassMetadata(get_class($person))->setIdentifierValues($person, array('id' => 101));
 621        $q->setParameter('param', $person);
 622        $this->assertEquals(
 623            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.title AS title2, c2_.salary AS salary3, c2_.department AS department4, c2_.startDate AS startDate5, c0_.discr AS discr6, c0_.spouse_id AS spouse_id7, c1_.car_id AS car_id8 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_.friend_id = c4_.id WHERE c3_.person_id = c0_.id AND c4_.id = ?)',
 624            $q->getSql()
 625        );
 626    }
 627
 628    public function testSupportsMemberOfWithSingleValuedAssociation()
 629    {
 630        // Impossible example, but it illustrates the purpose
 631        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.email MEMBER OF u.groups');
 632
 633        $this->assertEquals(
 634            '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_.group_id = c2_.id WHERE c1_.user_id = c0_.id AND c2_.id = c0_.email_id)',
 635            $q->getSql()
 636        );
 637    }
 638
 639    public function testSupportsMemberOfWithIdentificationVariable()
 640    {
 641        // Impossible example, but it illustrates the purpose
 642        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u MEMBER OF u.groups');
 643
 644        $this->assertEquals(
 645            '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_.group_id = c2_.id WHERE c1_.user_id = c0_.id AND c2_.id = c0_.id)',
 646            $q->getSql()
 647        );
 648    }
 649
 650    public function testSupportsCurrentDateFunction()
 651    {
 652        $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_date()');
 653        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 654        $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_datetime > CURRENT_DATE', $q->getSql());
 655    }
 656
 657    public function testSupportsCurrentTimeFunction()
 658    {
 659        $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.time > current_time()');
 660        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 661        $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_time > CURRENT_TIME', $q->getSql());
 662    }
 663
 664    public function testSupportsCurrentTimestampFunction()
 665    {
 666        $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_timestamp()');
 667        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 668        $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_datetime > CURRENT_TIMESTAMP', $q->getSql());
 669    }
 670
 671    public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition()
 672    {
 673        $this->assertSqlGeneration(
 674            // DQL
 675            // The result of this query consists of all employees whose spouses are also employees.
 676            'SELECT DISTINCT emp FROM Doctrine\Tests\Models\CMS\CmsEmployee emp
 677                WHERE EXISTS (
 678                    SELECT spouseEmp
 679                    FROM Doctrine\Tests\Models\CMS\CmsEmployee spouseEmp
 680                    WHERE spouseEmp = emp.spouse)',
 681            // SQL
 682            'SELECT DISTINCT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_'
 683                . ' WHERE EXISTS ('
 684                    . 'SELECT c1_.id FROM cms_employees c1_ WHERE c1_.id = c0_.spouse_id'
 685                    . ')'
 686        );
 687    }
 688
 689    public function testExistsExpressionWithSimpleSelectReturningScalar()
 690    {
 691        $this->assertSqlGeneration(
 692            // DQL
 693            // The result of this query consists of all employees whose spouses are also employees.
 694            'SELECT DISTINCT emp FROM Doctrine\Tests\Models\CMS\CmsEmployee emp
 695                WHERE EXISTS (
 696                    SELECT 1
 697                    FROM Doctrine\Tests\Models\CMS\CmsEmployee spouseEmp
 698                    WHERE spouseEmp = emp.spouse)',
 699            // SQL
 700            'SELECT DISTINCT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_'
 701                . ' WHERE EXISTS ('
 702                    . 'SELECT 1 AS sclr2 FROM cms_employees c1_ WHERE c1_.id = c0_.spouse_id'
 703                    . ')'
 704        );
 705    }
 706
 707    public function testLimitFromQueryClass()
 708    {
 709        $q = $this->_em
 710            ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u')
 711            ->setMaxResults(10);
 712
 713        $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ LIMIT 10', $q->getSql());
 714    }
 715
 716    public function testLimitAndOffsetFromQueryClass()
 717    {
 718        $q = $this->_em
 719            ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u')
 720            ->setMaxResults(10)
 721            ->setFirstResult(0);
 722
 723        $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ LIMIT 10 OFFSET 0', $q->getSql());
 724    }
 725
 726    public function testSizeFunction()
 727    {
 728        $this->assertSqlGeneration(
 729            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.phonenumbers) > 1",
 730            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) > 1"
 731        );
 732    }
 733
 734    public function testSizeFunctionSupportsManyToMany()
 735    {
 736        $this->assertSqlGeneration(
 737            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) > 1",
 738            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_users_groups c1_ WHERE c1_.user_id = c0_.id) > 1"
 739        );
 740    }
 741
 742    public function testEmptyCollectionComparisonExpression()
 743    {
 744        $this->assertSqlGeneration(
 745            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IS EMPTY",
 746            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) = 0"
 747        );
 748        $this->assertSqlGeneration(
 749            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IS NOT EMPTY",
 750            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) > 0"
 751        );
 752    }
 753
 754    public function testNestedExpressions()
 755    {
 756        $this->assertSqlGeneration(
 757            "select u from Doctrine\Tests\Models\CMS\CmsUser u where u.id > 10 and u.id < 42 and ((u.id * 2) > 5)",
 758            "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)"
 759        );
 760    }
 761
 762    public function testNestedExpressions2()
 763    {
 764        $this->assertSqlGeneration(
 765            "select u from Doctrine\Tests\Models\CMS\CmsUser u where (u.id > 10) and (u.id < 42 and ((u.id * 2) > 5)) or u.id <> 42",
 766            "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"
 767        );
 768    }
 769
 770    public function testNestedExpressions3()
 771    {
 772        $this->assertSqlGeneration(
 773            "select u from Doctrine\Tests\Models\CMS\CmsUser u where (u.id > 10) and (u.id between 1 and 10 or u.id in (1, 2, 3, 4, 5))",
 774            "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))"
 775        );
 776    }
 777
 778    public function testOrderByCollectionAssociationSize()
 779    {
 780        $this->assertSqlGeneration(
 781            "select u, size(u.articles) as numArticles from Doctrine\Tests\Models\CMS\CmsUser u order by numArticles",
 782            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT COUNT(*) FROM cms_articles c1_ WHERE c1_.user_id = c0_.id) AS sclr4 FROM cms_users c0_ ORDER BY sclr4 ASC"
 783        );
 784    }
 785
 786    public function testOrderBySupportsSingleValuedPathExpressionOwningSide()
 787    {
 788        $this->assertSqlGeneration(
 789            "select a from Doctrine\Tests\Models\CMS\CmsArticle a order by a.user",
 790            "SELECT c0_.id AS id0, c0_.topic AS topic1, c0_.text AS text2, c0_.version AS version3 FROM cms_articles c0_ ORDER BY c0_.user_id ASC"
 791        );
 792    }
 793
 794    /**
 795     * @expectedException Doctrine\ORM\Query\QueryException
 796     */
 797    public function testOrderBySupportsSingleValuedPathExpressionInverseSide()
 798    {
 799        $q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u order by u.address");
 800        $q->getSQL();
 801    }
 802
 803    public function testBooleanLiteralInWhereOnSqlite()
 804    {
 805        $oldPlat = $this->_em->getConnection()->getDatabasePlatform();
 806        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SqlitePlatform);
 807
 808        $this->assertSqlGeneration(
 809            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true",
 810            "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 1"
 811        );
 812
 813        $this->assertSqlGeneration(
 814            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false",
 815            "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 0"
 816        );
 817
 818        $this->_em->getConnection()->setDatabasePlatform($oldPlat);
 819    }
 820
 821    public function testBooleanLiteralInWhereOnPostgres()
 822    {
 823        $oldPlat = $this->_em->getConnection()->getDatabasePlatform();
 824        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
 825
 826        $this->assertSqlGeneration(
 827            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true",
 828            "SELECT b0_.id AS id0, b0_.booleanField AS booleanfield1 FROM boolean_model b0_ WHERE b0_.booleanField = true"
 829        );
 830
 831        $this->assertSqlGeneration(
 832            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false",
 833            "SELECT b0_.id AS id0, b0_.booleanField AS booleanfield1 FROM boolean_model b0_ WHERE b0_.booleanField = false"
 834        );
 835
 836        $this->_em->getConnection()->setDatabasePlatform($oldPlat);
 837    }
 838
 839    public function testSingleValuedAssociationFieldInWhere()
 840    {
 841        $this->assertSqlGeneration(
 842            "SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user = ?1",
 843            "SELECT c0_.phonenumber AS phonenumber0 FROM cms_phonenumbers c0_ WHERE c0_.user_id = ?"
 844        );
 845    }
 846
 847    public function testSingleValuedAssociationNullCheckOnOwningSide()
 848    {
 849        $this->assertSqlGeneration(
 850            "SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.user IS NULL",
 851            "SELECT c0_.id AS id0, c0_.country AS country1, c0_.zip AS zip2, c0_.city AS city3 FROM cms_addresses c0_ WHERE c0_.user_id IS NULL"
 852        );
 853    }
 854
 855    // Null check on inverse side has to happen through explicit JOIN.
 856    // "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address IS NULL"
 857    // where the CmsUser is the inverse side is not supported.
 858    public function testSingleValuedAssociationNullCheckOnInverseSide()
 859    {
 860        $this->assertSqlGeneration(
 861            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.address a WHERE a.id IS NULL",
 862            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id WHERE c1_.id IS NULL"
 863        );
 864    }
 865
 866    /**
 867     * @group DDC-339
 868     * @group DDC-1572
 869     */
 870    public function testStringFunctionLikeExpression()
 871    {
 872        $this->assertSqlGeneration(
 873            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) LIKE '%foo OR bar%'",
 874            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) LIKE '%foo OR bar%'"
 875        );
 876        $this->assertSqlGeneration(
 877            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) LIKE :str",
 878            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) LIKE ?"
 879        );
 880        $this->assertSqlGeneration(
 881            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(UPPER(u.name), '_moo') LIKE :str",
 882            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(c0_.name) || '_moo' LIKE ?"
 883        );
 884
 885        // DDC-1572
 886        $this->assertSqlGeneration(
 887            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE UPPER(u.name) LIKE UPPER(:str)",
 888            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(c0_.name) LIKE UPPER(?)"
 889        );
 890        $this->assertSqlGeneration(
 891            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE UPPER(LOWER(u.name)) LIKE UPPER(LOWER(:str))",
 892            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(LOWER(c0_.name)) LIKE UPPER(LOWER(?))"
 893        );
 894        $this->assertSqlGeneration(
 895            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic LIKE u.name",
 896            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic LIKE c0_.name)"
 897        );
 898    }
 899
 900    /**
 901     * @group DDC-1802
 902     */
 903    public function testStringFunctionNotLikeExpression()
 904    {
 905        $this->assertSqlGeneration(
 906                "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) NOT LIKE '%foo OR bar%'",
 907                "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) NOT LIKE '%foo OR bar%'"
 908        );
 909
 910        $this->assertSqlGeneration(
 911                "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE UPPER(LOWER(u.name)) NOT LIKE UPPER(LOWER(:str))",
 912                "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(LOWER(c0_.name)) NOT LIKE UPPER(LOWER(?))"
 913        );
 914        $this->assertSqlGeneration(
 915                "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic NOT LIKE u.name",
 916                "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic NOT LIKE c0_.name)"
 917        );
 918    }
 919
 920    /**
 921     * @group DDC-338
 922     */
 923    public function testOrderedCollectionFetchJoined()
 924    {
 925        $this->assertSqlGeneration(
 926            "SELECT r, l FROM Doctrine\Tests\Models\Routing\RoutingRoute r JOIN r.legs l",
 927            "SELECT r0_.id AS id0, r1_.id AS id1, r1_.departureDate AS departureDate2, r1_.arrivalDate AS arrivalDate3 FROM RoutingRoute r0_ INNER JOIN RoutingRouteLegs r2_ ON r0_.id = r2_.route_id INNER JOIN RoutingLeg r1_ ON r1_.id = r2_.leg_id ".
 928            "ORDER BY r1_.departureDate ASC"
 929        );
 930    }
 931
 932    public function testSubselectInSelect()
 933    {
 934        $this->assertSqlGeneration(
 935            "SELECT u.name, (SELECT COUNT(p.phonenumber) FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.phonenumber = 1234) pcount FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = 'jon'",
 936            "SELECT c0_.name AS name0, (SELECT COUNT(c1_.phonenumber) AS dctrn__1 FROM cms_phonenumbers c1_ WHERE c1_.phonenumber = 1234) AS sclr1 FROM cms_users c0_ WHERE c0_.name = 'jon'"
 937        );
 938    }
 939
 940    /**
 941     * @group locking
 942     * @group DDC-178
 943     */
 944    public function testPessimisticWriteLockQueryHint()
 945    {
 946        if ($this->_em->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
 947            $this->markTestSkipped('SqLite does not support Row locking at all.');
 948        }
 949
 950        $this->assertSqlGeneration(
 951            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
 952            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
 953            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR UPDATE",
 954            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE)
 955        );
 956    }
 957
 958    /**
 959     * @group locking
 960     * @group DDC-178
 961     */
 962    public function testPessimisticReadLockQueryHintPostgreSql()
 963    {
 964        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
 965
 966        $this->assertSqlGeneration(
 967            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
 968            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
 969            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR SHARE",
 970            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
 971                );
 972    }
 973
 974    /**
 975     * @group DDC-1693
 976     * @group locking
 977     */
 978    public function testLockModeNoneQueryHint()
 979    {
 980        $this->assertSqlGeneration(
 981            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
 982            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
 983            "FROM cms_users c0_ WHERE c0_.username = 'gblanco'",
 984            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::NONE)
 985                );
 986    }
 987
 988    /**
 989     * @group DDC-430
 990     */
 991    public function testSupportSelectWithMoreThan10InputParameters()
 992    {
 993        $this->assertSqlGeneration(
 994            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1 OR u.id = ?2 OR u.id = ?3 OR u.id = ?4 OR u.id = ?5 OR u.id = ?6 OR u.id = ?7 OR u.id = ?8 OR u.id = ?9 OR u.id = ?10 OR u.id = ?11",
 995            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ?"
 996        );
 997    }
 998
 999    /**
1000     * @group locking
1001     * @group DDC-178
1002     */
1003    public function testPessimisticReadLockQueryHintMySql()
1004    {
1005        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform);
1006
1007        $this->assertSqlGeneration(
1008            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
1009            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
1010            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' LOCK IN SHARE MODE",
1011            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
1012        );
1013    }
1014
1015    /**
1016     * @group locking
1017     * @group DDC-178
1018     */
1019    public function testPessimisticReadLockQueryHintOracle()
1020    {
1021        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform);
1022
1023        $this->assertSqlGeneration(
1024            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
1025            "SELECT c0_.id AS ID0, c0_.status AS STATUS1, c0_.username AS USERNAME2, c0_.name AS NAME3 ".
1026            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR UPDATE",
1027            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
1028        );
1029    }
1030
1031    /**
1032     * @group DDC-431
1033     */
1034    public function testSupportToCustomDQLFunctions()
1035    {
1036        $config = $this->_em->getConfiguration();
1037        $config->addCustomNumericFunction('MYABS', 'Doctrine\Tests\ORM\Query\MyAbsFunction');
1038
1039        $this->assertSqlGeneration(
1040            'SELECT MYABS(p.phonenumber) FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p',
1041            'SELECT ABS(c0_.phonenumber) AS sclr0 FROM cms_phonenumbers c0_'
1042        );
1043
1044        $config->setCustomNumericFunctions(array());
1045    }
1046
1047    /**
1048     * @group DDC-826
1049     */
1050    public function testMappedSuperclassAssociationJoin()
1051    {
1052        $this->assertSqlGeneration(
1053            'SELECT f FROM Doctrine\Tests\Models\DirectoryTree\File f JOIN f.parentDirectory d WHERE f.id = ?1',
1054            'SELECT f0_.id AS id0, f0_.extension AS extension1, f0_.name AS name2 FROM "file" f0_ INNER JOIN Directory d1_ ON f0_.parentDirectory_id = d1_.id WHERE f0_.id = ?'
1055        );
1056    }
1057
1058    /**
1059     * @group DDC-1053
1060     */
1061    public function testGroupBy()
1062    {
1063        $this->assertSqlGeneration(
1064            'SELECT g.id, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g.id',
1065            'SELECT c0_.id AS id0, count(c1_.id) AS sclr1 FROM cms_groups c0_ INNER JOIN cms_users_groups c2_ ON c0_.id = c2_.group_id INNER JOIN cms_users c1_ ON c1_.id = c2_.user_id GROUP BY c0_.id'
1066        );
1067    }
1068
1069    /**
1070     * @group DDC-1053
1071     */
1072    public function testGroupByIdentificationVariable()
1073    {
1074        $this->assertSqlGeneration(
1075            'SELECT g, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g',
1076            'SELECT c0_.id AS id0, c0_.name AS name1, count(c1_.id) AS sclr2 FROM cms_groups c0_ INNER JOIN cms_users_groups c2_ ON c0_.id = c2_.group_id INNER JOIN cms_users c1_ ON c1_.id = c2_.user_id GROUP BY c0_.id, c0_.name'
1077        );
1078    }
1079
1080    public function testCaseContainingNullIf()
1081    {
1082        $this->assertSqlGeneration(
1083            "SELECT NULLIF(g.id, g.name) AS NullIfEqual FROM Doctrine\Tests\Models\CMS\CmsGroup g",
1084            'SELECT NULLIF(c0_.id, c0_.name) AS sclr0 FROM cms_groups c0_'
1085        );
1086    }
1087
1088    public function testCaseContainingCoalesce()
1089    {
1090        $this->assertSqlGeneration(
1091            "SELECT COALESCE(NULLIF(u.name, ''), u.username) as Display FROM Doctrine\Tests\Models\CMS\CmsUser u",
1092            "SELECT COALESCE(NULLIF(c0_.name, ''), c0_.username) AS sclr0 FROM cms_users c0_"
1093        );
1094    }
1095
1096    /**
1097     * Test that the right discriminator data is inserted in a subquery.
1098     */
1099    public function testSubSelectDiscriminator()
1100    {
1101        $this->assertSqlGeneration(
1102            "SELECT u.name, (SELECT COUNT(cfc.id) total FROM Doctrine\Tests\Models\Company\CompanyFixContract cfc) as cfc_count FROM Doctrine\Tests\Models\CMS\CmsUser u",
1103            "SELECT c0_.name AS name0, (SELECT COUNT(c1_.id) AS dctrn__total FROM company_contracts c1_ WHERE c1_.discr IN ('fix')) AS sclr1 FROM cms_users c0_"
1104        );
1105    }
1106
1107    public function testIdVariableResultVariableReuse()
1108    {
1109        $exceptionThrown = false;
1110        try {
1111            $query = $this->_em->createQuery("SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IN (SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u)");
1112
1113            $query->getSql();
1114            $query->free();
1115        } catch (\Exception $e) {
1116            $exceptionThrown = true;
1117        }
1118
1119        $this->assertTrue($exceptionThrown);
1120
1121    }
1122
1123    public function testSubSelectAliasesFromOuterQuery()
1124    {
1125        $this->assertSqlGeneration(
1126            "SELECT uo, (SELECT ui.name FROM Doctrine\Tests\Models\CMS\CmsUser ui WHERE ui.id = uo.id) AS bar FROM Doctrine\Tests\Models\CMS\CmsUser uo",
1127            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT c1_.name FROM cms_users c1_ WHERE c1_.id = c0_.id) AS sclr4 FROM cms_users c0_"
1128        );
1129    }
1130
1131    public function testSubSelectAliasesFromOuterQueryWithSubquery()
1132    {
1133        $this->assertSqlGeneration(
1134            "SELECT uo, (SELECT ui.name FROM Doctrine\Tests\Models\CMS\CmsUser ui WHERE ui.id = uo.id AND ui.name IN (SELECT uii.name FROM Doctrine\Tests\Models\CMS\CmsUser uii)) AS bar FROM Doctrine\Tests\Models\CMS\CmsUser uo",
1135            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT c1_.name FROM cms_users c1_ WHERE c1_.id = c0_.id AND c1_.name IN (SELECT c2_.name FROM cms_users c2_)) AS sclr4 FROM cms_users c0_"
1136        );
1137    }
1138
1139    public function testSubSelectAliasesFromOuterQueryReuseInWhereClause()
1140    {
1141        $this->assertSqlGeneration(
1142            "SELECT uo, (SELECT ui.name FROM Doctrine\Tests\Models\CMS\CmsUser ui WHERE ui.id = uo.id) AS bar FROM Doctrine\Tests\Models\CMS\CmsUser uo WHERE bar = ?0",
1143            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT c1_.name FROM cms_users c1_ WHERE c1_.id = c0_.id) AS sclr4 FROM cms_users c0_ WHERE sclr4 = ?"
1144        );
1145    }
1146
1147    /**
1148     * @group DDC-1298
1149     */
1150    public function testSelectForeignKeyPKWithoutFields()
1151    {
1152        $this->assertSqlGeneration(
1153            "SELECT t, s, l FROM Doctrine\Tests\Models\DDC117\DDC117Link l INNER JOIN l.target t INNER JOIN l.source s",
1154            "SELECT d0_.article_id AS article_id0, d0_.title AS title1, d1_.article_id AS article_id2, d1_.title AS title3, d2_.source_id AS source_id4, d2_.target_id AS target_id5 FROM DDC117Link d2_ INNER JOIN DDC117Article d0_ ON d2_.target_id = d0_.article_id INNER JOIN DDC117Article d1_ ON d2_.source_id = d1_.article_id"
1155        );
1156    }
1157
1158    public function testGeneralCaseWithSingleWhenClause()
1159    {
1160        $this->assertSqlGeneration(
1161            "SELECT g.id, CASE WHEN ((g.id / 2) > 18) THEN 1 ELSE 0 END AS test FROM Doctrine\Tests\Models\CMS\CmsGroup g",
1162            "SELECT c0_.id AS id0, CASE WHEN (c0_.id / 2 > 18) THEN 1 ELSE 0 END AS sclr1 FROM cms_groups c0_"
1163        );
1164    }
1165
1166    public function testGeneralCaseWithMultipleWhenClause()
1167    {
1168        $this->assertSqlGeneration(
1169            "SELECT g.id, CASE WHEN (g.id / 2 < 10) THEN 2 WHEN ((g.id / 2) > 20) THEN 1 ELSE 0 END AS test FROM Doctrine\Tests\Models\CMS\CmsGroup g",
1170            "SELECT c0_.id AS id0, CASE WHEN (c0_.id / 2 < 10) THEN 2 WHEN (c0_.id / 2 > 20) THEN 1 ELSE 0 END AS sclr1 FROM cms_groups c0_"
1171        );
1172    }
1173
1174    public function testSimpleCaseWithSingleWhenClause()
1175    {
1176        $this->assertSqlGeneration(
1177            "SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id = CASE g.name WHEN 'admin' THEN 1 ELSE 2 END",
1178            "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id = CASE c0_.name WHEN 'admin' THEN 1 ELSE 2 END"
1179        );
1180    }
1181
1182    public function testSimpleCaseWithMultipleWhenClause()
1183    {
1184        $this->assertSqlGeneration(
1185            "SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id = (CASE g.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END)",
1186            "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id = CASE c0_.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END"
1187        );
1188    }
1189
1190    public function testGeneralCaseWithSingleWhenClauseInSubselect()
1191    {
1192        $this->assertSqlGeneration(
1193            "SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id IN (SELECT CASE WHEN ((g2.id / 2) > 18) THEN 2 ELSE 1 END FROM Doctrine\Tests\Models\CMS\CmsGroup g2)",
1194            "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE WHEN (c1_.id / 2 > 18) THEN 2 ELSE 1 END AS sclr2 FROM cms_groups c1_)"
1195        );
1196    }
1197
1198    public function testGeneralCaseWithMultipleWhenClauseInSubselect()
1199    {
1200        $this->assertSqlGeneration(
1201            "SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id IN (SELECT CASE WHEN (g.id / 2 < 10) THEN 3 WHEN ((g.id / 2) > 20) THEN 2 ELSE 1 END FROM Doctrine\Tests\Models\CMS\CmsGroup g2)",
1202            "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE WHEN (c0_.id / 2 < 10) THEN 3 WHEN (c0_.id / 2 > 20) THEN 2 ELSE 1 END AS sclr2 FROM cms_groups c1_)"
1203        );
1204    }
1205
1206    public function testSimpleCaseWithSingleWhenClauseInSubselect()
1207    {
1208        $this->assertSqlGeneration(
1209            "SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id IN (SELECT CASE g2.name WHEN 'admin' THEN 1 ELSE 2 END FROM Doctrine\Tests\Models\CMS\CmsGroup g2)",
1210            "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE c1_.name WHEN 'admin' THEN 1 ELSE 2 END AS sclr2 FROM cms_groups c1_)"
1211        );
1212    }
1213
1214    public function testSimpleCaseWithMultipleWhenClauseInSubselect()
1215    {
1216        $this->assertSqlGeneration(
1217            "SELECT g FROM Doctrine\Tests\Models\CMS\CmsGroup g WHERE g.id IN (SELECT CASE g2.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END FROM Doctrine\Tests\Models\CMS\CmsGroup g2)",
1218            "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id IN (SELECT CASE c1_.name WHEN 'admin' THEN 1 WHEN 'moderator' THEN 2 ELSE 3 END AS sclr2 FROM cms_groups c1_)"
1219        );
1220    }
1221
1222    /**
1223     * @group DDC-1696
1224     */
1225    public function testSimpleCaseWithStringPrimary()
1226    {
1227        $this->assertSqlGeneration(
1228            "SELECT g.id, CASE WHEN ((g.id / 2) > 18) THEN 'Foo' ELSE 'Bar' END AS test FROM Doctrine\Tests\Models\CMS\CmsGroup g",
1229            "SELECT c0_.id AS id0, CASE WHEN (c0_.id / 2 > 18) THEN 'Foo' ELSE 'Bar' END AS sclr1 FROM cms_groups c0_"
1230        );
1231    }
1232
1233    /**
1234     * @group DDC-1339
1235     */
1236    public function testIdentityFunctionInSelectClause()
1237    {
1238        $this->assertSqlGeneration(
1239            "SELECT IDENTITY(u.email) as email_id FROM Doctrine\Tests\Models\CMS\CmsUser u",
1240            "SELECT c0_.email_id AS sclr0 FROM cms_users c0_"
1241        );
1242    }
1243
1244    /**
1245     * @group DDC-1339
1246     */
1247    public function testIdentityFunctionDoesNotAcceptStateField()
1248    {
1249        $this->assertInvalidSqlGeneration(
1250            "SELECT IDENTITY(u.name) as name FROM Doctrine\Tests\Models\CMS\CmsUser u",
1251            "Doctrine\ORM\Query\QueryException"
1252        );
1253    }
1254
1255    /**
1256     * @group DDC-1389
1257     */
1258    public function testInheritanceTypeJoinInRootClassWithDisabledForcePartialLoad()
1259    {
1260        $this->assertSqlGeneration(
1261            'SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p',
1262            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.title AS title2, c2_.salary AS salary3, c2_.department AS department4, c2_.startDate AS startDate5, c0_.discr AS discr6, c0_.spouse_id AS spouse_id7, c1_.car_id AS car_id8 FROM company_persons c0_ LEFT JOIN company_managers c1_ ON c0_.id = c1_.id LEFT JOIN company_employees c2_ ON c0_.id = c2_.id',
1263            array(Query::HINT_FORCE_PARTIAL_LOAD => false)
1264        );
1265    }
1266
1267    /**
1268     * @group DDC-1389
1269     */
1270    public function testInheritanceTypeJoinInRootClassWithEnabledForcePartialLoad()
1271    {
1272        $this->assertSqlGeneration(
1273            'SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p',
1274            'SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_',
1275            array(Query::HINT_FORCE_PARTIAL_LOAD => true)
1276        );
1277    }
1278
1279    /**
1280     * @group DDC-1389
1281     */
1282    public function testInheritanceTypeJoinInChildClassWithDisabledForcePartialLoad()
1283    {
1284        $this->assertSqlGeneration(
1285            'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e',
1286            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c2_.title AS title5, c0_.discr AS discr6, c0_.spouse_id AS spouse_id7, c2_.car_id AS car_id8 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id LEFT JOIN company_managers c2_ ON c1_.id = c2_.id',
1287            array(Query::HINT_FORCE_PARTIAL_LOAD => false)
1288        );
1289    }
1290
1291    /**
1292     * @group DDC-1389
1293     */
1294    public function testInheritanceTypeJoinInChildClassWithEnabledForcePartialLoad()
1295    {
1296        $this->assertSqlGeneration(
1297            'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e',
1298            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c0_.discr AS discr5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id',
1299            array(Query::HINT_FORCE_PARTIAL_LOAD => true)
1300        );
1301    }
1302
1303    /**
1304     * @group DDC-1389
1305     */
1306    public function testInheritanceTypeJoinInLeafClassWithDisabledForcePartialLoad()
1307    {
1308        $this->assertSqlGeneration(
1309            'SELECT m FROM Doctrine\Tests\Models\Company\CompanyManager m',
1310            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c2_.title AS title5, c0_.discr AS discr6, c0_.spouse_id AS spouse_id7, c2_.car_id AS car_id8 FROM company_managers c2_ INNER JOIN company_employees c1_ ON c2_.id = c1_.id INNER JOIN company_persons c0_ ON c2_.id = c0_.id',
1311            array(Query::HINT_FORCE_PARTIAL_LOAD => false)
1312        );
1313    }
1314
1315    /**
1316     * @group DDC-1389
1317     */
1318    public function testInheritanceTypeJoinInLeafClassWithEnabledForcePartialLoad()
1319    {
1320        $this->assertSqlGeneration(
1321            'SELECT m FROM Doctrine\Tests\Models\Company\CompanyManager m',
1322            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c2_.title AS title5, c0_.discr AS discr6 FROM company_managers c2_ INNER JOIN company_employees c1_ ON c2_.id = c1_.id INNER JOIN company_persons c0_ ON c2_.id = c0_.id',
1323            array(Query::HINT_FORCE_PARTIAL_LOAD => true)
1324        );
1325    }
1326
1327    /**
1328     * @group DDC-1389
1329     */
1330    public function testInheritanceTypeSingleTableInRootClassWithDisabledForcePartialLoad()
1331    {
1332        $this->assertSqlGeneration(
1333            'SELECT c FROM Doctrine\Tests\Models\Company\CompanyContract c',
1334            "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.fixPrice AS fixPrice2, c0_.hoursWorked AS hoursWorked3, c0_.pricePerHour AS pricePerHour4, c0_.maxPrice AS maxPrice5, c0_.discr AS discr6, c0_.salesPerson_id AS salesPerson_id7 FROM company_contracts c0_ WHERE c0_.discr IN ('fix', 'flexible', 'flexultra')",
1335            array(Query::HINT_FORCE_PARTIAL_LOAD => false)
1336        );
1337    }
1338
1339    /**
1340     * @group DDC-1389
1341     */
1342    public function testInheritanceTypeSingleTableInRootClassWithEnabledForcePartialLoad()
1343    {
1344        $this->assertSqlGeneration(
1345            'SELECT c FROM Doctrine\Tests\Models\Company\CompanyContract c',
1346            "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.fixPrice AS fixPrice2, c0_.hoursWorked AS hoursWorked3, c0_.pricePerHour AS pricePerHour4, c0_.maxPrice AS maxPrice5, c0_.discr AS discr6 FROM company_contracts c0_ WHERE c0_.discr IN ('fix', 'flexible', 'flexultra')",
1347            array(Query::HINT_FORCE_PARTIAL_LOAD => true)
1348        );
1349    }
1350
1351    /**
1352     * @group DDC-1389
1353     */
1354    public function testInheritanceTypeSingleTableInChildClassWithDisabledForcePartialLoad()
1355    {
1356        $this->assertSqlGeneration(
1357            'SELECT fc FROM Doctrine\Tests\Models\Company\CompanyFlexContract fc',
1358            "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.hoursWorked AS hoursWorked2, c0_.pricePerHour AS pricePerHour3, c0_.maxPrice AS maxPrice4, c0_.discr AS discr5, c0_.salesPerson_id AS salesPerson_id6 FROM company_contracts c0_ WHERE c0_.discr IN ('flexible', 'flexultra')",
1359            array(Query::HINT_FORCE_PARTIAL_LOAD => false)
1360        );
1361    }
1362
1363    /**
1364     * @group DDC-1389
1365     */
1366    public function testInheritanceTypeSingleTableInChildClassWithEnabledForcePartialLoad()
1367    {
1368        $this->assertSqlGeneration(
1369            'SELECT fc FROM Doctrine\Tests\Models\Company\CompanyFlexContract fc',
1370            "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.hoursWorked AS hoursWorked2, c0_.pricePerHour AS pricePerHour3, c0_.maxPrice AS maxPrice4, c0_.discr AS discr5 FROM company_contracts c0_ WHERE c0_.discr IN ('flexible', 'flexultra')",
1371            array(Query::HINT_FORCE_PARTIAL_LOAD => true)
1372        );
1373    }
1374
1375    /**
1376     * @group DDC-1389
1377     */
1378    public function testInheritanceTypeSingleTableInLeafClassWithDisabledForcePartialLoad()
1379    {
1380        $this->assertSqlGeneration(
1381            'SELECT fuc FROM Doctrine\Tests\Models\Company\CompanyFlexUltraContract fuc',
1382            "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.hoursWorked AS hoursWorked2, c0_.pricePerHour AS pricePerHour3, c0_.maxPrice AS maxPrice4, c0_.discr AS discr5, c0_.salesPerson_id AS salesPerson_id6 FROM company_contracts c0_ WHERE c0_.discr IN ('flexultra')",
1383            array(Query::HINT_FORCE_PARTIAL_LOAD => false)
1384        );
1385    }
1386
1387    /**
1388     * @group DDC-1389
1389     */
1390    public function testInheritanceTypeSingleTableInLeafClassWithEnabledForcePartialLoad()
1391    {
1392        $this->assertSqlGeneration(
1393            'SELECT fuc FROM Doctrine\Tests\Models\Company\CompanyFlexUltraContract fuc',
1394            "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.hoursWorked AS hoursWorked2, c0_.pricePerHour AS pricePerHour3, c0_.maxPrice AS maxPrice4, c0_.discr AS discr5 FROM company_contracts c0_ WHERE c0_.discr IN ('flexultra')",
1395            array(Query::HINT_FORCE_PARTIAL_LOAD => true)
1396        );
1397    }
1398
1399    /**
1400     * @group DDC-1161
1401     */
1402    public function testSelfReferenceWithOneToOneDoesNotDuplicateAlias()
1403    {
1404        $this->assertSqlGeneration(
1405            'SELECT p, pp FROM Doctrine\Tests\Models\Company\CompanyPerson p JOIN p.spouse pp',
1406            "SELECT c0_.id AS id0, c0_.name AS name1, c1_.title AS title2, c2_.salary AS salary3, c2_.department AS department4, c2_.startDate AS startDate5, c3_.id AS id6, c3_.name AS name7, c4_.title AS title8, c5_.salary AS salary9, c5_.department AS department10, c5_.startDate AS startDate11, c0_.discr AS discr12, c0_.spouse_id AS spouse_id13, c1_.car_id AS car_id14, c3_.discr AS discr15, c3_.spouse_id AS spouse_id16, c4_.car_id AS car_id17 FROM company_persons c0_ LEFT JOIN company_managers c1_ ON c0_.id = c1_.id LEFT JOIN company_employees c2_ ON c0_.id = c2_.id INNER JOIN company_persons c3_ ON c0_.spouse_id = c3_.id LEFT JOIN company_managers c4_ ON c3_.id = c4_.id LEFT JOIN company_employees c5_ ON c3_.id = c5_.id",
1407            array(Query::HINT_FORCE_PARTIAL_LOAD => false)
1408        );
1409    }
1410
1411    /**
1412     * @group DDC-1384
1413     */
1414    public function testAliasDoesNotExceedPlatformDefinedLength()
1415    {
1416        $this->assertSqlGeneration(
1417            'SELECT m FROM ' . __NAMESPACE__ .  '\\DDC1384Model m',
1418            "SELECT d0_.aVeryLongIdentifierThatShouldBeShortenedByTheSQLWalker_fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo AS fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo0 FROM DDC1384Model d0_"
1419        );
1420    }
1421
1422    /**
1423     * @group DDC-331
1424     * @group DDC-1384
1425     */
1426    public function testIssue331()
1427    {
1428        $this->assertSqlGeneration(
1429            'SELECT e.name FROM Doctrine\Tests\Models\Company\CompanyEmployee e',
1430            'SELECT c0_.name AS name0 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id'
1431        );
1432    }
1433    /**
1434     * @group DDC-1435
1435     */
1436    public function testForeignKeyAsPrimaryKeySubselect()
1437    {
1438        $this->assertSqlGeneration(
1439            "SELECT s FROM Doctrine\Tests\Models\DDC117\DDC117Article s WHERE EXISTS (SELECT r FROM Doctrine\Tests\Models\DDC117\DDC117Reference r WHERE r.source = s)",
1440            "SELECT d0_.article_id AS article_id0, d0_.title AS title1 FROM DDC117Article d0_ WHERE EXISTS (SELECT d1_.source_id, d1_.target_id FROM DDC117Reference d1_ WHERE d1_.source_id = d0_.article_id)"
1441        );
1442    }
1443
1444    /**
1445     * @group DDC-1474
1446     */
1447    public function testSelectWithArithmeticExpressionBeforeField()
1448    {
1449        $this->assertSqlGeneration(
1450            'SELECT - e.value AS value, e.id FROM ' . __NAMESPACE__ . '\DDC1474Entity e',
1451            'SELECT -d0_.value AS sclr0, d0_.id AS id1 FROM DDC1474Entity d0_'
1452        );
1453
1454        $this->assertSqlGeneration(
1455            'SELECT e.id, + e.value AS value FROM ' . __NAMESPACE__ . '\DDC1474Entity e',
1456            'SELECT d0_.id AS id0, +d0_.value AS sclr1 FROM DDC1474Entity d0_'
1457        );
1458    }
1459
1460     /**
1461     * @group DDC-1430
1462     */
1463    public function testGroupByAllFieldsWhenObjectHasForeignKeys()
1464    {
1465        $this->assertSqlGeneration(
1466            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u',
1467            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ GROUP BY c0_.id, c0_.status, c0_.username, c0_.name, c0_.email_id'
1468        );
1469
1470        $this->assertSqlGeneration(
1471            'SELECT e FROM Doctrine\Tests\Models\CMS\CmsEmployee e GROUP BY e',
1472            'SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_ GROUP BY c0_.id, c0_.name, c0_.spouse_id'
1473        );
1474    }
1475
1476    /**
1477     * @group DDC-1236
1478     */
1479    public function testGroupBySupportsResultVariable()
1480    {
1481        $this->assertSqlGeneration(
1482            'SELECT u, u.status AS st FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY st',
1483            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.status AS status4 FROM cms_users c0_ GROUP BY status4'
1484        );
1485    }
1486
1487    /**
1488     * @group DDC-1236
1489     */
1490    public function testGroupBySupportsIdentificationVariable()
1491    {
1492        $this->assertSqlGeneration(
1493            'SELECT u AS user FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY user',
1494            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ GROUP BY id0, status1, username2, name3'
1495        );
1496    }
1497
1498    /**
1499     * @group DDC-1213
1500     */
1501    public function testSupportsBitComparison()
1502    {
1503        $this->assertSqlGeneration(
1504            'SELECT BIT_OR(4,2), BIT_AND(4,2), u FROM Doctrine\Tests\Models\CMS\CmsUser u',
1505            'SELECT (4 | 2) AS sclr0, (4 & 2) AS sclr1, c0_.id AS id2, c0_.status AS status3, c0_.username AS username4, c0_.name AS name5 FROM cms_users c0_'
1506        );
1507        $this->assertSqlGeneration(
1508            'SELECT BIT_OR(u.id,2), BIT_AND(u.id,2) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE BIT_OR(u.id,2) > 0',
1509            'SELECT (c0_.id | 2) AS sclr0, (c0_.id & 2) AS sclr1 FROM cms_users c0_ WHERE (c0_.id | 2) > 0'
1510        );
1511        $this->assertSqlGeneration(
1512            'SELECT BIT_OR(u.id,2), BIT_AND(u.id,2) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE BIT_AND(u.id , 4) > 0',
1513            'SELECT (c0_.id | 2) AS sclr0, (c0_.id & 2) AS sclr1 FROM cms_users c0_ WHERE (c0_.id & 4) > 0'
1514        );
1515        $this->assertSqlGeneration(
1516            'SELECT BIT_OR(u.id,2), BIT_AND(u.id,2) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE BIT_OR(u.id , 2) > 0 OR BIT_AND(u.id , 4) > 0',
1517            'SELECT (c0_.id | 2) AS sclr0, (c0_.id & 2) AS sclr1 FROM cms_users c0_ WHERE (c0_.id | 2) > 0 OR (c0_.id & 4) > 0'
1518        );
1519    }
1520
1521    /**
1522     * @group DDC-1539
1523     */
1524    public function testParenthesesOnTheLeftHandOfComparison()
1525    {
1526        $this->assertSqlGeneration(
1527            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u where ( (u.id + u.id) * u.id ) > 100',
1528            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id + c0_.id) * c0_.id > 100'
1529        );
1530        $this->assertSqlGeneration(
1531            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u where (u.id + u.id) * u.id > 100',
1532            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id + c0_.id) * c0_.id > 100'
1533        );
1534        $this->assertSqlGeneration(
1535            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u where 100 < (u.id + u.id) * u.id ',
1536            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE 100 < (c0_.id + c0_.id) * c0_.id'
1537        );
1538    }
1539
1540    /**
1541     * @group DDC-1557
1542     */
1543    public function testSupportsSubSqlFunction()
1544    {
1545        $this->assertSqlGeneration(
1546            'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.name IN ( SELECT TRIM(u2.name) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
1547            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.name IN (SELECT TRIM(c1_.name) AS sclr4 FROM cms_users c1_)'
1548        );
1549        $this->assertSqlGeneration(
1550            'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.name IN ( SELECT TRIM(u2.name) FROM Doctrine\Tests\Models\CMS\CmsUser u2  WHERE LOWER(u2.name) LIKE \'%fabio%\')',
1551            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.name IN (SELECT TRIM(c1_.name) AS sclr4 FROM cms_users c1_ WHERE LOWER(c1_.name) LIKE \'%fabio%\')'
1552        );
1553        $this->assertSqlGeneration(
1554            'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.email IN ( SELECT TRIM(IDENTITY(u2.email)) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
1555            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.email_id IN (SELECT TRIM(c1_.email_id) AS sclr4 FROM cms_users c1_)'
1556        );
1557        $this->assertSqlGeneration(
1558            'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.email IN ( SELECT IDENTITY(u2.email) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
1559            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.email_id IN (SELECT c1_.email_id AS sclr4 FROM cms_users c1_)'
1560        );
1561        $this->assertSqlGeneration(
1562            'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE COUNT(u1.id) = ( SELECT SUM(u2.id) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
1563            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE COUNT(c0_.id) = (SELECT SUM(c1_.id) AS dctrn__1 FROM cms_users c1_)'
1564        );
1565        $this->assertSqlGeneration(
1566            'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE COUNT(u1.id) <= ( SELECT SUM(u2.id) + COUNT(u2.email) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
1567            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE COUNT(c0_.id) <= (SELECT SUM(c1_.id) + COUNT(c1_.email_id) AS sclr4 FROM cms_users c1_)'
1568        );
1569    }
1570
1571    public function testCustomTypeValueSql()
1572    {
1573        if (DBALType::hasType('negative_to_positive')) {
1574            DBALType::overrideType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1575        } else {
1576            DBALType::addType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1577        }
1578
1579        $this->assertSqlGeneration(
1580            'SELECT p.customInteger FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p WHERE p.id = 1',
1581            'SELECT -(c0_.customInteger) AS customInteger0 FROM customtype_parents c0_ WHERE c0_.id = 1'
1582        );
1583    }
1584
1585    public function testCustomTypeValueSqlIgnoresIdentifierColumn()
1586    {
1587        if (DBALType::hasType('negative_to_positive')) {
1588            DBALType::overrideType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1589        } else {
1590            DBALType::addType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1591        }
1592
1593        $this->assertSqlGeneration(
1594            'SELECT p.id FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p WHERE p.id = 1',
1595            'SELECT c0_.id AS id0 FROM customtype_parents c0_ WHERE c0_.id = 1'
1596        );
1597    }
1598
1599    public function testCustomTypeValueSqlForAllFields()
1600    {
1601        if (DBALType::hasType('negative_to_positive')) {
1602            DBALType::overrideType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1603        } else {
1604            DBALType::addType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1605        }
1606
1607        $this->assertSqlGeneration(
1608            'SELECT p FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p',
1609            'SELECT c0_.id AS id0, -(c0_.customInteger) AS customInteger1 FROM customtype_parents c0_'
1610        );
1611    }
1612
1613    public function testCustomTypeValueSqlForPartialObject()
1614    {
1615        if (DBALType::hasType('negative_to_positive')) {
1616            DBALType::overrideType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1617        } else {
1618            DBALType::addType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
1619        }
1620
1621        $this->assertSqlGeneration(
1622            'SELECT partial p.{id, customInteger} FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p',
1623            'SELECT c0_.id AS id0, -(c0_.customInteger) AS customInteger1 FROM customtype_parents c0_'
1624        );
1625    }
1626
1627    /**
1628     * @group DDC-1529
1629     */
1630    public function testMultipleFromAndInheritanceCondition()
1631    {
1632        $this->assertSqlGeneration(
1633            'SELECT fix, flex FROM Doctrine\Tests\Models\Company\CompanyFixContract fix, Doctrine\Tests\Models\Company\CompanyFlexContract flex',
1634            "SELECT c0_.id AS id0, c0_.completed AS completed1, c0_.fixPrice AS fixPrice2, c1_.id AS id3, c1_.completed AS completed4, c1_.hoursWorked AS hoursWorked5, c1_.pricePerHour AS pricePerHour6, c1_.maxPrice AS maxPrice7, c0_.discr AS discr8, c1_.discr AS discr9 FROM company_contracts c0_, company_contracts c1_ WHERE (c0_.discr IN ('fix') AND c1_.discr IN ('flexible', 'flexultra'))"
1635        );
1636    }
1637
1638    /**
1639     * @group DDC-775
1640     */
1641    public function testOrderByClauseSupportsSimpleArithmeticExpression()
1642    {
1643        $this->assertSqlGeneration(
1644            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.id + 1 ',
1645            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY c0_.id + 1 ASC'
1646        );
1647        $this->assertSqlGeneration(
1648            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY ( ( (u.id + 1) * (u.id - 1) ) / 2)',
1649            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY (c0_.id + 1) * (c0_.id - 1) / 2 ASC'
1650        );
1651        $this->assertSqlGeneration(
1652            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY ((u.id + 5000) * u.id + 3) ',
1653            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ ORDER BY (c0_.id + 5000) * c0_.id + 3 ASC'
1654        );
1655    }
1656
1657    /**
1658     * @group DDC-1719
1659     */
1660    public function testStripNonAlphanumericCharactersFromAlias()
1661    {
1662        $this->assertSqlGeneration(
1663            'SELECT e FROM Doctrine\Tests\Models\Quote\SimpleEntity e',
1664            'SELECT d0_."simple-entity-id" AS simpleentityid0, d0_."simple-entity-value" AS simpleentityvalue1 FROM "ddc-1719-simple-entity" d0_'
1665        );
1666
1667        $this->assertSqlGeneration(
1668            'SELECT e.value FROM Doctrine\Tests\Models\Quote\SimpleEntity e ORDER BY e.value',
1669            'SELECT d0_."simple-entity-value" AS simpleentityvalue0 FROM "ddc-1719-simple-entity" d0_ ORDER BY d0_."simple-entity-value" ASC'
1670        );
1671
1672        $this->assertSqlGeneration(
1673            'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\Quote\SimpleEntity e ORDER BY e.value',
1674            'SELECT TRIM(d0_."simple-entity-value") AS sclr0 FROM "ddc-1719-simple-entity" d0_ ORDER BY d0_."simple-entity-value" ASC'
1675        );
1676    }
1677
1678   /**
1679    * @group DDC-1845
1680    */
1681    public function testQuotedWalkJoinVariableDeclaration()
1682    {
1683        $this->assertSqlGeneration(
1684            'SELECT u, a FROM Doctrine\Tests\Models\Quote\User u JOIN u.address a',
1685            'SELECT q0_."user-id" AS userid0, q0_."user-name" AS username1, q1_."address-id" AS addressid2, q1_."address-zip" AS addresszip3 FROM "quote-user" q0_ INNER JOIN "quote-address" q1_ ON q0_."address-id" = q1_."address-id"'
1686        );
1687
1688        $this->assertSqlGeneration(
1689            'SELECT u, p FROM Doctrine\Tests\Models\Quote\User u JOIN u.phones p',
1690            'SELECT q0_."user-id" AS userid0, q0_."user-name" AS username1, q1_."phone-number" AS phonenumber2 FROM "quote-user" q0_ INNER JOIN "quote-phone" q1_ ON q0_."user-id" = q1_."user-id"'
1691        );
1692
1693        $this->assertSqlGeneration(
1694            'SELECT u, g FROM Doctrine\Tests\Models\Quote\User u JOIN u.groups g',
1695            'SELECT q0_."user-id" AS userid0, q0_."user-name" AS username1, q1_."group-id" AS groupid2, q1_."group-name" AS groupname3 FROM "quote-user" q0_ INNER JOIN "quote-users-groups" q2_ ON q0_."user-id" = q2_."user-id" INNER JOIN "quote-group" q1_ ON q1_."group-id" = q2_."group-id"'
1696        );
1697
1698        $this->assertSqlGeneration(
1699            'SELECT a, u FROM Doctrine\Tests\Models\Quote\Address a JOIN a.user u',
1700            'SELECT q0_."address-id" AS addressid0, q0_."address-zip" AS addresszip1, q1_."user-id" AS userid2, q1_."user-name" AS username3 FROM "quote-address" q0_ INNER JOIN "quote-user" q1_ ON q0_."user-id" = q1_."user-id"'
1701        );
1702
1703        $this->assertSqlGeneration(
1704            'SELECT g, u FROM Doctrine\Tests\Models\Quote\Group g JOIN g.users u',
1705            'SELECT q0_."group-id" AS groupid0, q0_."group-name" AS groupname1, q1_."user-id" AS userid2, q1_."user-name" AS username3 FROM "quote-group" q0_ INNER JOIN "quote-users-groups" q2_ ON q0_."group-id" = q2_."group-id" INNER JOIN "quote-user" q1_ ON q1_."user-id" = q2_."user-id"'
1706        );
1707
1708        $this->assertSqlGeneration(
1709            'SELECT g, p FROM Doctrine\Tests\Models\Quote\Group g JOIN g.parent p',
1710            'SELECT q0_."group-id" AS groupid0, q0_."group-name" AS groupname1, q1_."group-id" AS groupid2, q1_."group-name" AS groupname3 FROM "quote-group" q0_ INNER JOIN "quote-group" q1_ ON q0_."parent-id" = q1_."group-id"'
1711        );
1712    }
1713}
1714
1715class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
1716{
1717    public $simpleArithmeticExpression;
1718
1719    /**
1720     * @override
1721     */
1722    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
1723    {
1724        return 'ABS(' . $sqlWalker->walkSimpleArithmeticExpression($this->simpleArithmeticExpression) . ')';
1725    }
1726
1727    /**
1728     * @override
1729     */
1730    public function parse(\Doctrine\ORM\Query\Parser $parser)
1731    {
1732        $lexer = $parser->getLexer();
1733
1734        $parser->match(\Doctrine\ORM\Query\Lexer::T_IDENTIFIER);
1735        $parser->match(\Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS);
1736
1737        $this->simpleArithmeticExpression = $parser->SimpleArithmeticExpression();
1738
1739        $parser->match(\Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS);
1740    }
1741}
1742/**
1743 * @Entity
1744 */
1745class DDC1384Model
1746{
1747    /**
1748     * @Id
1749     * @Column(type="integer")
1750     * @GeneratedValue
1751     */
1752    protected $aVeryLongIdentifierThatShouldBeShortenedByTheSQLWalker_fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo;
1753}
1754
1755
1756/**
1757 * @Entity
1758 */
1759class DDC1474Entity
1760{
1761
1762    /**
1763     * @Id
1764     * @Column(type="integer")
1765     * @GeneratedValue()
1766     */
1767    protected $id;
1768
1769    /**
1770     * @column(type="float")
1771     */
1772    private $value;
1773
1774    /**
1775     * @param string $float
1776     */
1777    public function __construct($float)
1778    {
1779        $this->value = $float;
1780    }
1781
1782    /**
1783     * @return integer
1784     */
1785    public function getId()
1786    {
1787        return $this->id;
1788    }
1789
1790    /**
1791     * @return float
1792     */
1793    public function getValue()
1794    {
1795        return $this->value;
1796    }
1797
1798    /**
1799     * @param float $value
1800     */
1801    public function setValue($value)
1802    {
1803        $this->value = $value;
1804    }
1805
1806}
1807