SelectSqlGenerationTest.php 52KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. <?php
  2. namespace Doctrine\Tests\ORM\Query;
  3. use Doctrine\ORM\Query;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
  6. {
  7. private $_em;
  8. protected function setUp()
  9. {
  10. $this->_em = $this->_getTestEntityManager();
  11. }
  12. /**
  13. * Assert a valid SQL generation.
  14. *
  15. * @param string $dqlToBeTested
  16. * @param string $sqlToBeConfirmed
  17. * @param array $queryHints
  18. * @param array $queryParams
  19. */
  20. public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed, array $queryHints = array(), array $queryParams = array())
  21. {
  22. try {
  23. $query = $this->_em->createQuery($dqlToBeTested);
  24. foreach ($queryParams AS $name => $value) {
  25. $query->setParameter($name, $value);
  26. }
  27. $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
  28. ->useQueryCache(false);
  29. foreach ($queryHints AS $name => $value) {
  30. $query->setHint($name, $value);
  31. }
  32. parent::assertEquals($sqlToBeConfirmed, $query->getSQL());
  33. $query->free();
  34. } catch (\Exception $e) {
  35. $this->fail($e->getMessage() ."\n".$e->getTraceAsString());
  36. }
  37. }
  38. /**
  39. * Asser an invalid SQL generation.
  40. *
  41. * @param string $dqlToBeTested
  42. * @param string $expectedException
  43. * @param array $queryHints
  44. * @param array $queryParams
  45. */
  46. public function assertInvalidSqlGeneration($dqlToBeTested, $expectedException, array $queryHints = array(Query::HINT_FORCE_PARTIAL_LOAD => true), array $queryParams = array())
  47. {
  48. $this->setExpectedException($expectedException);
  49. $query = $this->_em->createQuery($dqlToBeTested);
  50. foreach ($queryParams AS $name => $value) {
  51. $query->setParameter($name, $value);
  52. }
  53. $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
  54. ->useQueryCache(false);
  55. foreach ($queryHints AS $name => $value) {
  56. $query->setHint($name, $value);
  57. }
  58. $sql = $query->getSql();
  59. $query->free();
  60. // If we reached here, test failed
  61. $this->fail($sql);
  62. }
  63. public function testSupportsSelectForAllFields()
  64. {
  65. $this->assertSqlGeneration(
  66. 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u',
  67. 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_'
  68. );
  69. }
  70. public function testSupportsSelectForOneField()
  71. {
  72. $this->assertSqlGeneration(
  73. 'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u',
  74. 'SELECT c0_.id AS id0 FROM cms_users c0_'
  75. );
  76. }
  77. public function testSupportsSelectForOneNestedField()
  78. {
  79. $this->assertSqlGeneration(
  80. 'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u',
  81. 'SELECT c0_.id AS id0 FROM cms_articles c1_ INNER JOIN cms_users c0_ ON c1_.user_id = c0_.id'
  82. );
  83. }
  84. public function testSupportsSelectForAllNestedField()
  85. {
  86. $this->assertSqlGeneration(
  87. 'SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u ORDER BY u.name ASC',
  88. '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'
  89. );
  90. }
  91. public function testSupportsSelectForMultipleColumnsOfASingleComponent()
  92. {
  93. $this->assertSqlGeneration(
  94. 'SELECT u.username, u.name FROM Doctrine\Tests\Models\CMS\CmsUser u',
  95. 'SELECT c0_.username AS username0, c0_.name AS name1 FROM cms_users c0_'
  96. );
  97. }
  98. public function testSupportsSelectUsingMultipleFromComponents()
  99. {
  100. $this->assertSqlGeneration(
  101. 'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE u = p.user',
  102. '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'
  103. );
  104. }
  105. public function testSupportsSelectWithCollectionAssociationJoin()
  106. {
  107. $this->assertSqlGeneration(
  108. 'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p',
  109. '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'
  110. );
  111. }
  112. public function testSupportsSelectWithSingleValuedAssociationJoin()
  113. {
  114. $this->assertSqlGeneration(
  115. 'SELECT u, a FROM Doctrine\Tests\Models\Forum\ForumUser u JOIN u.avatar a',
  116. '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'
  117. );
  118. }
  119. public function testSelectCorrelatedSubqueryComplexMathematicalExpression()
  120. {
  121. $this->assertSqlGeneration(
  122. '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',
  123. '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_'
  124. );
  125. }
  126. public function testSelectComplexMathematicalExpression()
  127. {
  128. $this->assertSqlGeneration(
  129. 'SELECT (count(p.phonenumber)+5)*10 FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p JOIN p.user ui WHERE ui.id = ?1',
  130. '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 = ?'
  131. );
  132. }
  133. /* NOT (YET?) SUPPORTED.
  134. Can be supported if SimpleSelectExpresion supports SingleValuedPathExpression instead of StateFieldPathExpression.
  135. public function testSingleAssociationPathExpressionInSubselect()
  136. {
  137. $this->assertSqlGeneration(
  138. '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',
  139. 'SELECT (SELECT c0_.user_id FROM cms_phonenumbers c0_ WHERE c0_.user_id = c1_.id) AS sclr0 FROM cms_users c1_ WHERE c1_.id = ?'
  140. );
  141. }*/
  142. /**
  143. * @group DDC-1077
  144. */
  145. public function testConstantValueInSelect()
  146. {
  147. $this->assertSqlGeneration(
  148. "SELECT u.name, 'foo' AS bar FROM Doctrine\Tests\Models\CMS\CmsUser u",
  149. "SELECT c0_.name AS name0, 'foo' AS sclr1 FROM cms_users c0_"
  150. );
  151. }
  152. public function testSupportsOrderByWithAscAsDefault()
  153. {
  154. $this->assertSqlGeneration(
  155. 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id',
  156. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
  157. );
  158. }
  159. public function testSupportsOrderByAsc()
  160. {
  161. $this->assertSqlGeneration(
  162. 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id asc',
  163. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
  164. );
  165. }
  166. public function testSupportsOrderByDesc()
  167. {
  168. $this->assertSqlGeneration(
  169. 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id desc',
  170. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id DESC'
  171. );
  172. }
  173. public function testSupportsSelectDistinct()
  174. {
  175. $this->assertSqlGeneration(
  176. 'SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u',
  177. 'SELECT DISTINCT c0_.name AS name0 FROM cms_users c0_'
  178. );
  179. }
  180. public function testSupportsAggregateFunctionInSelectedFields()
  181. {
  182. $this->assertSqlGeneration(
  183. 'SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id',
  184. 'SELECT COUNT(c0_.id) AS sclr0 FROM cms_users c0_ GROUP BY c0_.id'
  185. );
  186. }
  187. public function testSupportsAggregateFunctionWithSimpleArithmetic()
  188. {
  189. $this->assertSqlGeneration(
  190. 'SELECT MAX(u.id + 4) * 2 FROM Doctrine\Tests\Models\CMS\CmsUser u',
  191. 'SELECT MAX(c0_.id + 4) * 2 AS sclr0 FROM cms_users c0_'
  192. );
  193. }
  194. public function testSupportsWhereClauseWithPositionalParameter()
  195. {
  196. $this->assertSqlGeneration(
  197. 'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.id = ?1',
  198. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.id = ?'
  199. );
  200. }
  201. public function testSupportsWhereClauseWithNamedParameter()
  202. {
  203. $this->assertSqlGeneration(
  204. 'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name',
  205. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ?'
  206. );
  207. }
  208. public function testSupportsWhereAndClauseWithNamedParameters()
  209. {
  210. $this->assertSqlGeneration(
  211. 'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name and u.username = :name2',
  212. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ? AND f0_.username = ?'
  213. );
  214. }
  215. public function testSupportsCombinedWhereClauseWithNamedParameter()
  216. {
  217. $this->assertSqlGeneration(
  218. 'select u from Doctrine\Tests\Models\Forum\ForumUser u where (u.username = :name OR u.username = :name2) AND u.id = :id',
  219. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE (f0_.username = ? OR f0_.username = ?) AND f0_.id = ?'
  220. );
  221. }
  222. public function testSupportsAggregateFunctionInASelectDistinct()
  223. {
  224. $this->assertSqlGeneration(
  225. 'SELECT COUNT(DISTINCT u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u',
  226. 'SELECT COUNT(DISTINCT c0_.name) AS sclr0 FROM cms_users c0_'
  227. );
  228. }
  229. // Ticket #668
  230. public function testSupportsASqlKeywordInAStringLiteralParam()
  231. {
  232. $this->assertSqlGeneration(
  233. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name LIKE '%foo OR bar%'",
  234. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.name LIKE '%foo OR bar%'"
  235. );
  236. }
  237. public function testSupportsArithmeticExpressionsInWherePart()
  238. {
  239. $this->assertSqlGeneration(
  240. 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000',
  241. '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'
  242. );
  243. }
  244. public function testSupportsMultipleEntitiesInFromClause()
  245. {
  246. $this->assertSqlGeneration(
  247. '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',
  248. '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'
  249. );
  250. }
  251. public function testSupportsMultipleEntitiesInFromClauseUsingPathExpression()
  252. {
  253. $this->assertSqlGeneration(
  254. 'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsArticle a WHERE u.id = a.user',
  255. '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'
  256. );
  257. }
  258. public function testSupportsPlainJoinWithoutClause()
  259. {
  260. $this->assertSqlGeneration(
  261. 'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a',
  262. 'SELECT c0_.id AS id0, c1_.id AS id1 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id'
  263. );
  264. $this->assertSqlGeneration(
  265. 'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a',
  266. 'SELECT c0_.id AS id0, c1_.id AS id1 FROM cms_users c0_ INNER JOIN cms_articles c1_ ON c0_.id = c1_.user_id'
  267. );
  268. }
  269. /**
  270. * @group DDC-135
  271. */
  272. public function testSupportsJoinAndWithClauseRestriction()
  273. {
  274. $this->assertSqlGeneration(
  275. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic LIKE '%foo%'",
  276. "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%')"
  277. );
  278. $this->assertSqlGeneration(
  279. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a WITH a.topic LIKE '%foo%'",
  280. "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%')"
  281. );
  282. }
  283. /**
  284. * @group DDC-135
  285. * @group DDC-177
  286. */
  287. public function testJoinOnClause_NotYetSupported_ThrowsException()
  288. {
  289. $this->setExpectedException('Doctrine\ORM\Query\QueryException');
  290. $sql = $this->_em->createQuery(
  291. "SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a ON a.topic LIKE '%foo%'"
  292. )->getSql();
  293. }
  294. public function testSupportsMultipleJoins()
  295. {
  296. $this->assertSqlGeneration(
  297. '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',
  298. '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'
  299. );
  300. }
  301. public function testSupportsTrimFunction()
  302. {
  303. $this->assertSqlGeneration(
  304. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(TRAILING ' ' FROM u.name) = 'someone'",
  305. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(TRAILING ' ' FROM c0_.name) = 'someone'"
  306. );
  307. }
  308. // Ticket 894
  309. public function testSupportsBetweenClauseWithPositionalParameters()
  310. {
  311. $this->assertSqlGeneration(
  312. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id BETWEEN ?1 AND ?2",
  313. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.id BETWEEN ? AND ?"
  314. );
  315. }
  316. public function testSupportsFunctionalExpressionsInWherePart()
  317. {
  318. $this->assertSqlGeneration(
  319. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(u.name) = 'someone'",
  320. // String quoting in the SQL usually depends on the database platform.
  321. // This test works with a mock connection which uses ' for string quoting.
  322. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(c0_.name) = 'someone'"
  323. );
  324. }
  325. public function testSupportsInstanceOfExpressionsInWherePart()
  326. {
  327. $this->assertSqlGeneration(
  328. "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee",
  329. "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr = 'employee'"
  330. );
  331. }
  332. /**
  333. * @group DDC-1194
  334. */
  335. public function testSupportsInstanceOfExpressionsInWherePartPrefixedSlash()
  336. {
  337. $this->assertSqlGeneration(
  338. "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\Company\CompanyEmployee",
  339. "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr = 'employee'"
  340. );
  341. }
  342. /**
  343. * @group DDC-1194
  344. */
  345. public function testSupportsInstanceOfExpressionsInWherePartWithUnrelatedClass()
  346. {
  347. $this->assertInvalidSqlGeneration(
  348. "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\CMS\CmsUser",
  349. "Doctrine\ORM\Query\QueryException"
  350. );
  351. }
  352. public function testSupportsInstanceOfExpressionsInWherePartInDeeperLevel()
  353. {
  354. $this->assertSqlGeneration(
  355. "SELECT u FROM Doctrine\Tests\Models\Company\CompanyEmployee u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyManager",
  356. "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 = 'manager'"
  357. );
  358. }
  359. public function testSupportsInstanceOfExpressionsInWherePartInDeepestLevel()
  360. {
  361. $this->assertSqlGeneration(
  362. "SELECT u FROM Doctrine\Tests\Models\Company\CompanyManager u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyManager",
  363. "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 = 'manager'"
  364. );
  365. }
  366. public function testSupportsInstanceOfExpressionsUsingInputParameterInWherePart()
  367. {
  368. $this->assertSqlGeneration(
  369. "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF ?1",
  370. "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr = 'employee'",
  371. array(), array(1 => $this->_em->getClassMetadata('Doctrine\Tests\Models\Company\CompanyEmployee'))
  372. );
  373. }
  374. // Ticket #973
  375. public function testSupportsSingleValuedInExpressionWithoutSpacesInWherePart()
  376. {
  377. $this->assertSqlGeneration(
  378. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN(46)",
  379. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.id IN (46)"
  380. );
  381. }
  382. public function testSupportsMultipleValuedInExpressionInWherePart()
  383. {
  384. $this->assertSqlGeneration(
  385. 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN (1, 2)',
  386. '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)'
  387. );
  388. }
  389. public function testSupportsNotInExpressionInWherePart()
  390. {
  391. $this->assertSqlGeneration(
  392. 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN (1)',
  393. '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)'
  394. );
  395. }
  396. public function testInExpressionWithSingleValuedAssociationPathExpressionInWherePart()
  397. {
  398. $this->assertSqlGeneration(
  399. 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.avatar IN (?1, ?2)',
  400. 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.avatar_id IN (?, ?)'
  401. );
  402. }
  403. public function testInvalidInExpressionWithSingleValuedAssociationPathExpressionOnInverseSide()
  404. {
  405. // We do not support SingleValuedAssociationPathExpression on inverse side
  406. $this->assertInvalidSqlGeneration(
  407. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address IN (?1, ?2)",
  408. "Doctrine\ORM\Query\QueryException"
  409. );
  410. }
  411. public function testSupportsConcatFunctionForMysqlAndPostgresql()
  412. {
  413. $connMock = $this->_em->getConnection();
  414. $orgPlatform = $connMock->getDatabasePlatform();
  415. $connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform);
  416. $this->assertSqlGeneration(
  417. "SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, 's') = ?1",
  418. "SELECT c0_.id AS id0 FROM cms_users c0_ WHERE CONCAT(c0_.name, 's') = ?"
  419. );
  420. $this->assertSqlGeneration(
  421. "SELECT CONCAT(u.id, u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
  422. "SELECT CONCAT(c0_.id, c0_.name) AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
  423. );
  424. $connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
  425. $this->assertSqlGeneration(
  426. "SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, 's') = ?1",
  427. "SELECT c0_.id AS id0 FROM cms_users c0_ WHERE c0_.name || 's' = ?"
  428. );
  429. $this->assertSqlGeneration(
  430. "SELECT CONCAT(u.id, u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
  431. "SELECT c0_.id || c0_.name AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
  432. );
  433. $connMock->setDatabasePlatform($orgPlatform);
  434. }
  435. public function testSupportsExistsExpressionInWherePartWithCorrelatedSubquery()
  436. {
  437. $this->assertSqlGeneration(
  438. '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)',
  439. 'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT c1_.phonenumber FROM cms_phonenumbers c1_ WHERE c1_.phonenumber = c0_.id)'
  440. );
  441. }
  442. /**
  443. * @group DDC-593
  444. */
  445. public function testSubqueriesInComparisonExpression()
  446. {
  447. $this->assertSqlGeneration(
  448. '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))',
  449. '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 = ?))'
  450. );
  451. }
  452. public function testSupportsMemberOfExpression()
  453. {
  454. // "Get all users who have $phone as a phonenumber." (*cough* doesnt really make sense...)
  455. $q1 = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.phonenumbers');
  456. $q1->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  457. $phone = new \Doctrine\Tests\Models\CMS\CmsPhonenumber;
  458. $phone->phonenumber = 101;
  459. $q1->setParameter('param', $phone);
  460. $this->assertEquals(
  461. '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 = ?)',
  462. $q1->getSql()
  463. );
  464. // "Get all users who are members of $group."
  465. $q2 = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.groups');
  466. $q2->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  467. $group = new \Doctrine\Tests\Models\CMS\CmsGroup;
  468. $group->id = 101;
  469. $q2->setParameter('param', $group);
  470. $this->assertEquals(
  471. '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 = ?)',
  472. $q2->getSql()
  473. );
  474. // "Get all persons who have $person as a friend."
  475. // Tough one: Many-many self-referencing ("friends") with class table inheritance
  476. $q3 = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p WHERE :param MEMBER OF p.friends');
  477. $person = new \Doctrine\Tests\Models\Company\CompanyPerson;
  478. $this->_em->getClassMetadata(get_class($person))->setIdentifierValues($person, array('id' => 101));
  479. $q3->setParameter('param', $person);
  480. $this->assertEquals(
  481. '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, c2_.startDate AS startDate6, c0_.discr AS discr7, c0_.spouse_id AS spouse_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 = ?)',
  482. $q3->getSql()
  483. );
  484. }
  485. public function testSupportsCurrentDateFunction()
  486. {
  487. $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_date()');
  488. $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  489. $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_datetime > CURRENT_DATE', $q->getSql());
  490. }
  491. public function testSupportsCurrentTimeFunction()
  492. {
  493. $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.time > current_time()');
  494. $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  495. $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_time > CURRENT_TIME', $q->getSql());
  496. }
  497. public function testSupportsCurrentTimestampFunction()
  498. {
  499. $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_timestamp()');
  500. $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  501. $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_datetime > CURRENT_TIMESTAMP', $q->getSql());
  502. }
  503. public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition()
  504. {
  505. $this->assertSqlGeneration(
  506. // DQL
  507. // The result of this query consists of all employees whose spouses are also employees.
  508. 'SELECT DISTINCT emp FROM Doctrine\Tests\Models\CMS\CmsEmployee emp
  509. WHERE EXISTS (
  510. SELECT spouseEmp
  511. FROM Doctrine\Tests\Models\CMS\CmsEmployee spouseEmp
  512. WHERE spouseEmp = emp.spouse)',
  513. // SQL
  514. 'SELECT DISTINCT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_'
  515. . ' WHERE EXISTS ('
  516. . 'SELECT c1_.id FROM cms_employees c1_ WHERE c1_.id = c0_.spouse_id'
  517. . ')'
  518. );
  519. }
  520. public function testExistsExpressionWithSimpleSelectReturningScalar()
  521. {
  522. $this->assertSqlGeneration(
  523. // DQL
  524. // The result of this query consists of all employees whose spouses are also employees.
  525. 'SELECT DISTINCT emp FROM Doctrine\Tests\Models\CMS\CmsEmployee emp
  526. WHERE EXISTS (
  527. SELECT 1
  528. FROM Doctrine\Tests\Models\CMS\CmsEmployee spouseEmp
  529. WHERE spouseEmp = emp.spouse)',
  530. // SQL
  531. 'SELECT DISTINCT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_'
  532. . ' WHERE EXISTS ('
  533. . 'SELECT 1 AS sclr2 FROM cms_employees c1_ WHERE c1_.id = c0_.spouse_id'
  534. . ')'
  535. );
  536. }
  537. public function testLimitFromQueryClass()
  538. {
  539. $q = $this->_em
  540. ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u')
  541. ->setMaxResults(10);
  542. $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());
  543. }
  544. public function testLimitAndOffsetFromQueryClass()
  545. {
  546. $q = $this->_em
  547. ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u')
  548. ->setMaxResults(10)
  549. ->setFirstResult(0);
  550. $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());
  551. }
  552. public function testSizeFunction()
  553. {
  554. $this->assertSqlGeneration(
  555. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.phonenumbers) > 1",
  556. "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"
  557. );
  558. }
  559. public function testSizeFunctionSupportsManyToMany()
  560. {
  561. $this->assertSqlGeneration(
  562. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) > 1",
  563. "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"
  564. );
  565. }
  566. public function testEmptyCollectionComparisonExpression()
  567. {
  568. $this->assertSqlGeneration(
  569. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IS EMPTY",
  570. "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"
  571. );
  572. $this->assertSqlGeneration(
  573. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IS NOT EMPTY",
  574. "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"
  575. );
  576. }
  577. public function testNestedExpressions()
  578. {
  579. $this->assertSqlGeneration(
  580. "select u from Doctrine\Tests\Models\CMS\CmsUser u where u.id > 10 and u.id < 42 and ((u.id * 2) > 5)",
  581. "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)"
  582. );
  583. }
  584. public function testNestedExpressions2()
  585. {
  586. $this->assertSqlGeneration(
  587. "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",
  588. "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"
  589. );
  590. }
  591. public function testNestedExpressions3()
  592. {
  593. $this->assertSqlGeneration(
  594. "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))",
  595. "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))"
  596. );
  597. }
  598. public function testOrderByCollectionAssociationSize()
  599. {
  600. $this->assertSqlGeneration(
  601. "select u, size(u.articles) as numArticles from Doctrine\Tests\Models\CMS\CmsUser u order by numArticles",
  602. "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"
  603. );
  604. }
  605. public function testBooleanLiteralInWhereOnSqlite()
  606. {
  607. $oldPlat = $this->_em->getConnection()->getDatabasePlatform();
  608. $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SqlitePlatform);
  609. $this->assertSqlGeneration(
  610. "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true",
  611. "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 1"
  612. );
  613. $this->assertSqlGeneration(
  614. "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false",
  615. "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 0"
  616. );
  617. $this->_em->getConnection()->setDatabasePlatform($oldPlat);
  618. }
  619. public function testBooleanLiteralInWhereOnPostgres()
  620. {
  621. $oldPlat = $this->_em->getConnection()->getDatabasePlatform();
  622. $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
  623. $this->assertSqlGeneration(
  624. "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true",
  625. "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 'true'"
  626. );
  627. $this->assertSqlGeneration(
  628. "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false",
  629. "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 'false'"
  630. );
  631. $this->_em->getConnection()->setDatabasePlatform($oldPlat);
  632. }
  633. public function testSingleValuedAssociationFieldInWhere()
  634. {
  635. $this->assertSqlGeneration(
  636. "SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user = ?1",
  637. "SELECT c0_.phonenumber AS phonenumber0 FROM cms_phonenumbers c0_ WHERE c0_.user_id = ?"
  638. );
  639. }
  640. public function testSingleValuedAssociationNullCheckOnOwningSide()
  641. {
  642. $this->assertSqlGeneration(
  643. "SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.user IS NULL",
  644. "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"
  645. );
  646. }
  647. // Null check on inverse side has to happen through explicit JOIN.
  648. // "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address IS NULL"
  649. // where the CmsUser is the inverse side is not supported.
  650. public function testSingleValuedAssociationNullCheckOnInverseSide()
  651. {
  652. $this->assertSqlGeneration(
  653. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.address a WHERE a.id IS NULL",
  654. "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"
  655. );
  656. }
  657. /**
  658. * @group DDC-339
  659. */
  660. public function testStringFunctionLikeExpression()
  661. {
  662. $this->assertSqlGeneration(
  663. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) LIKE '%foo OR bar%'",
  664. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) LIKE '%foo OR bar%'"
  665. );
  666. $this->assertSqlGeneration(
  667. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) LIKE :str",
  668. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) LIKE ?"
  669. );
  670. $this->assertSqlGeneration(
  671. "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(UPPER(u.name), '_moo') LIKE :str",
  672. "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(c0_.name) || '_moo' LIKE ?"
  673. );
  674. }
  675. /**
  676. * @group DDC-338
  677. */
  678. public function testOrderedCollectionFetchJoined()
  679. {
  680. $this->assertSqlGeneration(
  681. "SELECT r, l FROM Doctrine\Tests\Models\Routing\RoutingRoute r JOIN r.legs l",
  682. "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 ".
  683. "ORDER BY r1_.departureDate ASC"
  684. );
  685. }
  686. public function testSubselectInSelect()
  687. {
  688. $this->assertSqlGeneration(
  689. "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'",
  690. "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'"
  691. );
  692. }
  693. /**
  694. * @group locking
  695. * @group DDC-178
  696. */
  697. public function testPessimisticWriteLockQueryHint()
  698. {
  699. if ($this->_em->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
  700. $this->markTestSkipped('SqLite does not support Row locking at all.');
  701. }
  702. $this->assertSqlGeneration(
  703. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
  704. "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
  705. "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR UPDATE",
  706. array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE)
  707. );
  708. }
  709. /**
  710. * @group locking
  711. * @group DDC-178
  712. */
  713. public function testPessimisticReadLockQueryHintPostgreSql()
  714. {
  715. $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
  716. $this->assertSqlGeneration(
  717. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
  718. "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
  719. "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR SHARE",
  720. array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
  721. );
  722. }
  723. /**
  724. * @group DDC-430
  725. */
  726. public function testSupportSelectWithMoreThan10InputParameters()
  727. {
  728. $this->assertSqlGeneration(
  729. "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",
  730. "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 = ?"
  731. );
  732. }
  733. /**
  734. * @group locking
  735. * @group DDC-178
  736. */
  737. public function testPessimisticReadLockQueryHintMySql()
  738. {
  739. $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform);
  740. $this->assertSqlGeneration(
  741. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
  742. "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
  743. "FROM cms_users c0_ WHERE c0_.username = 'gblanco' LOCK IN SHARE MODE",
  744. array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
  745. );
  746. }
  747. /**
  748. * @group locking
  749. * @group DDC-178
  750. */
  751. public function testPessimisticReadLockQueryHintOracle()
  752. {
  753. $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform);
  754. $this->assertSqlGeneration(
  755. "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
  756. "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
  757. "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR UPDATE",
  758. array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
  759. );
  760. }
  761. /**
  762. * @group DDC-431
  763. */
  764. public function testSupportToCustomDQLFunctions()
  765. {
  766. $config = $this->_em->getConfiguration();
  767. $config->addCustomNumericFunction('MYABS', 'Doctrine\Tests\ORM\Query\MyAbsFunction');
  768. $this->assertSqlGeneration(
  769. 'SELECT MYABS(p.phonenumber) FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p',
  770. 'SELECT ABS(c0_.phonenumber) AS sclr0 FROM cms_phonenumbers c0_'
  771. );
  772. $config->setCustomNumericFunctions(array());
  773. }
  774. /**
  775. * @group DDC-826
  776. */
  777. public function testMappedSuperclassAssociationJoin()
  778. {
  779. $this->assertSqlGeneration(
  780. 'SELECT f FROM Doctrine\Tests\Models\DirectoryTree\File f JOIN f.parentDirectory d WHERE f.id = ?1',
  781. '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 = ?'
  782. );
  783. }
  784. /**
  785. * @group DDC-1053
  786. */
  787. public function testGroupBy()
  788. {
  789. $this->assertSqlGeneration(
  790. 'SELECT g.id, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g.id',
  791. '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'
  792. );
  793. }
  794. /**
  795. * @group DDC-1053
  796. */
  797. public function testGroupByIdentificationVariable()
  798. {
  799. $this->assertSqlGeneration(
  800. 'SELECT g, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g',
  801. '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'
  802. );
  803. }
  804. public function testCaseContainingNullIf()
  805. {
  806. $this->assertSqlGeneration(
  807. "SELECT NULLIF(g.id, g.name) AS NullIfEqual FROM Doctrine\Tests\Models\CMS\CmsGroup g",
  808. 'SELECT NULLIF(c0_.id, c0_.name) AS sclr0 FROM cms_groups c0_'
  809. );
  810. }
  811. public function testCaseContainingCoalesce()
  812. {
  813. $this->assertSqlGeneration(
  814. "SELECT COALESCE(NULLIF(u.name, ''), u.username) as Display FROM Doctrine\Tests\Models\CMS\CmsUser u",
  815. "SELECT COALESCE(NULLIF(c0_.name, ''), c0_.username) AS sclr0 FROM cms_users c0_"
  816. );
  817. }
  818. /**
  819. * @group DDC-1298
  820. */
  821. public function testSelectForeignKeyPKWithoutFields()
  822. {
  823. $this->assertSqlGeneration(
  824. "SELECT t, s, l FROM Doctrine\Tests\Models\DDC117\DDC117Link l INNER JOIN l.target t INNER JOIN l.source s",
  825. "SELECT d0_.article_id AS article_id0, d0_.title AS title1, d1_.article_id AS article_id2, d1_.title AS title3 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"
  826. );
  827. }
  828. /**
  829. * @group DDC-1389
  830. */
  831. public function testInheritanceTypeJoinInRootClassWithDisabledForcePartialLoad()
  832. {
  833. $this->assertSqlGeneration(
  834. 'SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p',
  835. '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, c2_.startDate AS startDate6, c0_.discr AS discr7, c0_.spouse_id AS spouse_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',
  836. array(Query::HINT_FORCE_PARTIAL_LOAD => false)
  837. );
  838. }
  839. /**
  840. * @group DDC-1389
  841. */
  842. public function testInheritanceTypeJoinInRootClassWithEnabledForcePartialLoad()
  843. {
  844. $this->assertSqlGeneration(
  845. 'SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p',
  846. 'SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_',
  847. array(Query::HINT_FORCE_PARTIAL_LOAD => true)
  848. );
  849. }
  850. /**
  851. * @group DDC-1389
  852. */
  853. public function testInheritanceTypeJoinInChildClassWithDisabledForcePartialLoad()
  854. {
  855. $this->assertSqlGeneration(
  856. 'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e',
  857. '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, c2_.car_id AS car_id6, c0_.discr AS discr7, c0_.spouse_id AS spouse_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',
  858. array(Query::HINT_FORCE_PARTIAL_LOAD => false)
  859. );
  860. }
  861. public function testSubSelectAliasesFromOuterQueryReuseInWhereClause()
  862. {
  863. $this->assertSqlGeneration(
  864. "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",
  865. "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 = ?"
  866. );
  867. }
  868. /**
  869. * @group DDC-1389
  870. */
  871. public function testInheritanceTypeJoinInChildClassWithEnabledForcePartialLoad()
  872. {
  873. $this->assertSqlGeneration(
  874. 'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e',
  875. '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',
  876. array(Query::HINT_FORCE_PARTIAL_LOAD => true)
  877. );
  878. }
  879. /**
  880. * @group DDC-1389
  881. */
  882. public function testInheritanceTypeJoinInLeafClassWithDisabledForcePartialLoad()
  883. {
  884. $this->assertSqlGeneration(
  885. 'SELECT m FROM Doctrine\Tests\Models\Company\CompanyManager m',
  886. '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',
  887. array(Query::HINT_FORCE_PARTIAL_LOAD => false)
  888. );
  889. }
  890. /**
  891. * @group DDC-1389
  892. */
  893. public function testInheritanceTypeJoinInLeafClassWithEnabledForcePartialLoad()
  894. {
  895. $this->assertSqlGeneration(
  896. 'SELECT m FROM Doctrine\Tests\Models\Company\CompanyManager m',
  897. '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',
  898. array(Query::HINT_FORCE_PARTIAL_LOAD => true)
  899. );
  900. }
  901. /**
  902. * @group DDC-1389
  903. */
  904. public function testInheritanceTypeSingleTableInRootClassWithDisabledForcePartialLoad()
  905. {
  906. $this->assertSqlGeneration(
  907. 'SELECT c FROM Doctrine\Tests\Models\Company\CompanyContract c',
  908. "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')",
  909. array(Query::HINT_FORCE_PARTIAL_LOAD => false)
  910. );
  911. }
  912. /**
  913. * @group DDC-1389
  914. */
  915. public function testInheritanceTypeSingleTableInRootClassWithEnabledForcePartialLoad()
  916. {
  917. $this->assertSqlGeneration(
  918. 'SELECT c FROM Doctrine\Tests\Models\Company\CompanyContract c',
  919. "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')",
  920. array(Query::HINT_FORCE_PARTIAL_LOAD => true)
  921. );
  922. }
  923. /**
  924. * @group DDC-1389
  925. */
  926. public function testInheritanceTypeSingleTableInChildClassWithDisabledForcePartialLoad()
  927. {
  928. $this->assertSqlGeneration(
  929. 'SELECT fc FROM Doctrine\Tests\Models\Company\CompanyFlexContract fc',
  930. "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')",
  931. array(Query::HINT_FORCE_PARTIAL_LOAD => false)
  932. );
  933. }
  934. /**
  935. * @group DDC-1389
  936. */
  937. public function testInheritanceTypeSingleTableInChildClassWithEnabledForcePartialLoad()
  938. {
  939. $this->assertSqlGeneration(
  940. 'SELECT fc FROM Doctrine\Tests\Models\Company\CompanyFlexContract fc',
  941. "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')",
  942. array(Query::HINT_FORCE_PARTIAL_LOAD => true)
  943. );
  944. }
  945. /**
  946. * @group DDC-1389
  947. */
  948. public function testInheritanceTypeSingleTableInLeafClassWithDisabledForcePartialLoad()
  949. {
  950. $this->assertSqlGeneration(
  951. 'SELECT fuc FROM Doctrine\Tests\Models\Company\CompanyFlexUltraContract fuc',
  952. "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')",
  953. array(Query::HINT_FORCE_PARTIAL_LOAD => false)
  954. );
  955. }
  956. /**
  957. * @group DDC-1389
  958. */
  959. public function testInheritanceTypeSingleTableInLeafClassWithEnabledForcePartialLoad()
  960. {
  961. $this->assertSqlGeneration(
  962. 'SELECT fuc FROM Doctrine\Tests\Models\Company\CompanyFlexUltraContract fuc',
  963. "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')",
  964. array(Query::HINT_FORCE_PARTIAL_LOAD => true)
  965. );
  966. }
  967. /**
  968. * @group DDC-1435
  969. */
  970. public function testForeignKeyAsPrimaryKeySubselect()
  971. {
  972. $this->assertSqlGeneration(
  973. "SELECT s FROM Doctrine\Tests\Models\DDC117\DDC117Article s WHERE EXISTS (SELECT r FROM Doctrine\Tests\Models\DDC117\DDC117Reference r WHERE r.source = s)",
  974. "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)"
  975. );
  976. }
  977. }
  978. class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
  979. {
  980. public $simpleArithmeticExpression;
  981. /**
  982. * @override
  983. */
  984. public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
  985. {
  986. return 'ABS(' . $sqlWalker->walkSimpleArithmeticExpression(
  987. $this->simpleArithmeticExpression
  988. ) . ')';
  989. }
  990. /**
  991. * @override
  992. */
  993. public function parse(\Doctrine\ORM\Query\Parser $parser)
  994. {
  995. $lexer = $parser->getLexer();
  996. $parser->match(\Doctrine\ORM\Query\Lexer::T_IDENTIFIER);
  997. $parser->match(\Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS);
  998. $this->simpleArithmeticExpression = $parser->SimpleArithmeticExpression();
  999. $parser->match(\Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS);
  1000. }
  1001. }