DeleteSqlGenerationTest.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\Tests\ORM\Query;
  22. require_once __DIR__ . '/../../TestInit.php';
  23. /**
  24. * Test case for testing the saving and referencing of query identifiers.
  25. *
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Janne Vanhala <jpvanhal@cc.hut.fi>
  28. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  30. * @link http://www.phpdoctrine.org
  31. * @since 2.0
  32. * @version $Revision$
  33. * @todo 1) [romanb] We might want to split the SQL generation tests into multiple
  34. * testcases later since we'll have a lot of them and we might want to have special SQL
  35. * generation tests for some dbms specific SQL syntaxes.
  36. */
  37. class DeleteSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
  38. {
  39. private $_em;
  40. protected function setUp() {
  41. $this->_em = $this->_getTestEntityManager();
  42. }
  43. public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
  44. {
  45. try {
  46. $query = $this->_em->createQuery($dqlToBeTested);
  47. parent::assertEquals($sqlToBeConfirmed, $query->getSql());
  48. $query->free();
  49. } catch (\Exception $e) {
  50. $this->fail($e->getMessage());
  51. }
  52. }
  53. public function testSupportsDeleteWithoutWhereAndFrom()
  54. {
  55. $this->assertSqlGeneration(
  56. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u',
  57. 'DELETE FROM cms_users'
  58. );
  59. }
  60. public function testSupportsDeleteWithoutWhere()
  61. {
  62. $this->assertSqlGeneration(
  63. 'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u',
  64. 'DELETE FROM cms_users'
  65. );
  66. }
  67. public function testSupportsWhereClause()
  68. {
  69. $this->assertSqlGeneration(
  70. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1',
  71. 'DELETE FROM cms_users WHERE id = ?'
  72. );
  73. }
  74. public function testSupportsWhereOrExpressions()
  75. {
  76. $this->assertSqlGeneration(
  77. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 OR u.name = ?2',
  78. 'DELETE FROM cms_users WHERE username = ? OR name = ?'
  79. );
  80. }
  81. public function testSupportsWhereNestedConditionalExpressions()
  82. {
  83. $this->assertSqlGeneration(
  84. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1 OR ( u.username = ?2 OR u.name = ?3)',
  85. 'DELETE FROM cms_users WHERE id = ? OR (username = ? OR name = ?)'
  86. );
  87. //$this->assertSqlGeneration(
  88. // 'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser WHERE id = ?1',
  89. // 'DELETE FROM cms_users WHERE id = ?'
  90. //);
  91. }
  92. public function testIsCaseAgnostic()
  93. {
  94. $this->assertSqlGeneration(
  95. "delete from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1",
  96. "DELETE FROM cms_users WHERE username = ?"
  97. );
  98. }
  99. public function testSupportsAndCondition()
  100. {
  101. $this->assertSqlGeneration(
  102. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 AND u.name = ?2",
  103. "DELETE FROM cms_users WHERE username = ? AND name = ?"
  104. );
  105. }
  106. public function testSupportsWhereNot()
  107. {
  108. $this->assertSqlGeneration(
  109. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT u.id != ?1",
  110. "DELETE FROM cms_users WHERE NOT id <> ?"
  111. );
  112. }
  113. public function testSupportsWhereNotWithParentheses()
  114. {
  115. $this->assertSqlGeneration(
  116. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 )",
  117. "DELETE FROM cms_users WHERE NOT (id <> ?)"
  118. );
  119. }
  120. public function testSupportsWhereNotWithAndExpression()
  121. {
  122. $this->assertSqlGeneration(
  123. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 AND u.username = ?2 )",
  124. "DELETE FROM cms_users WHERE NOT (id <> ? AND username = ?)"
  125. );
  126. }
  127. // ConditionalPrimary was already tested (see testSupportsWhereClause() and testSupportsWhereNot())
  128. public function testSupportsGreaterThanComparisonClause()
  129. {
  130. // id = ? was already tested (see testDeleteWithWhere())
  131. $this->assertSqlGeneration(
  132. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ?1",
  133. "DELETE FROM cms_users WHERE id > ?"
  134. );
  135. }
  136. public function testSupportsGreaterThanOrEqualToComparisonClause()
  137. {
  138. $this->assertSqlGeneration(
  139. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id >= ?1",
  140. "DELETE FROM cms_users WHERE id >= ?"
  141. );
  142. }
  143. public function testSupportsLessThanComparisonClause()
  144. {
  145. $this->assertSqlGeneration(
  146. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < ?1",
  147. "DELETE FROM cms_users WHERE id < ?"
  148. );
  149. }
  150. public function testSupportsLessThanOrEqualToComparisonClause()
  151. {
  152. $this->assertSqlGeneration(
  153. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <= ?1",
  154. "DELETE FROM cms_users WHERE id <= ?"
  155. );
  156. }
  157. public function testSupportsNotEqualToComparisonClause()
  158. {
  159. $this->assertSqlGeneration(
  160. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <> ?1",
  161. "DELETE FROM cms_users WHERE id <> ?"
  162. );
  163. }
  164. public function testSupportsNotEqualToComparisonClauseExpressedWithExclamationMark()
  165. {
  166. $this->assertSqlGeneration(
  167. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id != ?1",
  168. "DELETE FROM cms_users WHERE id <> ?"
  169. );
  170. }
  171. public function testSupportsNotBetweenClause()
  172. {
  173. $this->assertSqlGeneration(
  174. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT BETWEEN ?1 AND ?2",
  175. "DELETE FROM cms_users WHERE id NOT BETWEEN ? AND ?"
  176. );
  177. }
  178. public function testSupportsBetweenClauseUsedWithAndClause()
  179. {
  180. $this->assertSqlGeneration(
  181. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id BETWEEN ?1 AND ?2 AND u.username != ?3",
  182. "DELETE FROM cms_users WHERE id BETWEEN ? AND ? AND username <> ?"
  183. );
  184. }
  185. public function testSupportsNotLikeClause()
  186. {
  187. // "WHERE" Expression LikeExpression
  188. $this->assertSqlGeneration(
  189. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username NOT LIKE ?1',
  190. 'DELETE FROM cms_users WHERE username NOT LIKE ?'
  191. );
  192. }
  193. public function testSupportsLikeClauseWithEscapeExpression()
  194. {
  195. $this->assertSqlGeneration(
  196. "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username LIKE ?1 ESCAPE '\\'",
  197. "DELETE FROM cms_users WHERE username LIKE ? ESCAPE '\\'"
  198. );
  199. }
  200. public function testSupportsIsNullClause()
  201. {
  202. // "WHERE" Expression NullComparisonExpression
  203. $this->assertSqlGeneration(
  204. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IS NULL',
  205. 'DELETE FROM cms_users WHERE name IS NULL'
  206. );
  207. }
  208. public function testSupportsIsNotNullClause()
  209. {
  210. $this->assertSqlGeneration(
  211. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IS NOT NULL',
  212. 'DELETE FROM cms_users WHERE name IS NOT NULL'
  213. );
  214. }
  215. public function testSupportsAtomExpressionAsClause()
  216. {
  217. $this->assertSqlGeneration(
  218. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE 1 = 1',
  219. 'DELETE FROM cms_users WHERE 1 = 1'
  220. );
  221. }
  222. public function testSupportsParameterizedAtomExpression()
  223. {
  224. $this->assertSqlGeneration(
  225. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE ?1 = 1',
  226. 'DELETE FROM cms_users WHERE ? = 1'
  227. );
  228. }
  229. public function testSupportsInClause()
  230. {
  231. $this->assertSqlGeneration(
  232. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN ( ?1, ?2, ?3, ?4 )',
  233. 'DELETE FROM cms_users WHERE id IN (?, ?, ?, ?)'
  234. );
  235. }
  236. public function testSupportsNotInClause()
  237. {
  238. $this->assertSqlGeneration(
  239. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN ( ?1, ?2 )',
  240. 'DELETE FROM cms_users WHERE id NOT IN (?, ?)'
  241. );
  242. }
  243. /**
  244. * @group DDC-980
  245. */
  246. public function testSubselectTableAliasReferencing()
  247. {
  248. $this->assertSqlGeneration(
  249. 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10',
  250. 'DELETE FROM cms_users WHERE (SELECT COUNT(*) FROM cms_users_groups c0_ WHERE c0_.user_id = cms_users.id) = 10'
  251. );
  252. }
  253. }