1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Query; |
6
|
|
|
|
7
|
|
|
use Doctrine\Tests\OrmTestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Test case for testing the saving and referencing of query identifiers. |
11
|
|
|
* |
12
|
|
|
* @author Guilherme Blanco <[email protected]> |
13
|
|
|
* @author Janne Vanhala <[email protected]> |
14
|
|
|
* @author Konsta Vesterinen <[email protected]> |
15
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
16
|
|
|
* @link http://www.phpdoctrine.org |
17
|
|
|
* @since 2.0 |
18
|
|
|
* @version $Revision$ |
19
|
|
|
* @todo 1) [romanb] We might want to split the SQL generation tests into multiple |
20
|
|
|
* testcases later since we'll have a lot of them and we might want to have special SQL |
21
|
|
|
* generation tests for some dbms specific SQL syntaxes. |
22
|
|
|
*/ |
23
|
|
|
class DeleteSqlGenerationTest extends OrmTestCase |
24
|
|
|
{ |
25
|
|
|
private $em; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->em = $this->getTestEntityManager(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) |
33
|
|
|
{ |
34
|
|
|
try { |
35
|
|
|
$query = $this->em->createQuery($dqlToBeTested); |
36
|
|
|
|
37
|
|
|
$sqlGenerated = $query->getSql(); |
38
|
|
|
|
39
|
|
|
$query->free(); |
40
|
|
|
} catch (\Exception $e) { |
41
|
|
|
$this->fail($e->getMessage()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
self::assertEquals($sqlToBeConfirmed, $sqlGenerated); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testSupportsDeleteWithoutWhereAndAlias() : void |
48
|
|
|
{ |
49
|
|
|
$this->assertSqlGeneration( |
50
|
|
|
'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser', |
51
|
|
|
'DELETE FROM "cms_users"' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testSupportsDeleteWithoutWhereAndFrom() |
56
|
|
|
{ |
57
|
|
|
$this->assertSqlGeneration( |
58
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u', |
59
|
|
|
'DELETE FROM "cms_users"' |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testSupportsDeleteWithoutWhere() |
64
|
|
|
{ |
65
|
|
|
$this->assertSqlGeneration( |
66
|
|
|
'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u', |
67
|
|
|
'DELETE FROM "cms_users"' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testSupportsWhereClause() |
72
|
|
|
{ |
73
|
|
|
$this->assertSqlGeneration( |
74
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1', |
75
|
|
|
'DELETE FROM "cms_users" WHERE "id" = ?' |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSupportsWhereOrExpressions() |
80
|
|
|
{ |
81
|
|
|
$this->assertSqlGeneration( |
82
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 OR u.name = ?2', |
83
|
|
|
'DELETE FROM "cms_users" WHERE "username" = ? OR "name" = ?' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testSupportsWhereNestedConditionalExpressions() |
88
|
|
|
{ |
89
|
|
|
$this->assertSqlGeneration( |
90
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1 OR ( u.username = ?2 OR u.name = ?3)', |
91
|
|
|
'DELETE FROM "cms_users" WHERE "id" = ? OR ("username" = ? OR "name" = ?)' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testIsCaseAgnostic() |
96
|
|
|
{ |
97
|
|
|
$this->assertSqlGeneration( |
98
|
|
|
'delete from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1', |
99
|
|
|
'DELETE FROM "cms_users" WHERE "username" = ?' |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testSupportsAndCondition() |
104
|
|
|
{ |
105
|
|
|
$this->assertSqlGeneration( |
106
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 AND u.name = ?2', |
107
|
|
|
'DELETE FROM "cms_users" WHERE "username" = ? AND "name" = ?' |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testSupportsWhereNot() |
112
|
|
|
{ |
113
|
|
|
$this->assertSqlGeneration( |
114
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT u.id != ?1', |
115
|
|
|
'DELETE FROM "cms_users" WHERE NOT "id" <> ?' |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testSupportsWhereNotWithParentheses() |
120
|
|
|
{ |
121
|
|
|
$this->assertSqlGeneration( |
122
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 )', |
123
|
|
|
'DELETE FROM "cms_users" WHERE NOT ("id" <> ?)' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testSupportsWhereNotWithAndExpression() |
128
|
|
|
{ |
129
|
|
|
$this->assertSqlGeneration( |
130
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 AND u.username = ?2 )', |
131
|
|
|
'DELETE FROM "cms_users" WHERE NOT ("id" <> ? AND "username" = ?)' |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// ConditionalPrimary was already tested (see testSupportsWhereClause() and testSupportsWhereNot()) |
136
|
|
|
|
137
|
|
|
public function testSupportsGreaterThanComparisonClause() |
138
|
|
|
{ |
139
|
|
|
// "id" = ? was already tested (see testDeleteWithWhere()) |
140
|
|
|
$this->assertSqlGeneration( |
141
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ?1', |
142
|
|
|
'DELETE FROM "cms_users" WHERE "id" > ?' |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testSupportsGreaterThanOrEqualToComparisonClause() |
147
|
|
|
{ |
148
|
|
|
$this->assertSqlGeneration( |
149
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id >= ?1', |
150
|
|
|
'DELETE FROM "cms_users" WHERE "id" >= ?' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testSupportsLessThanComparisonClause() |
155
|
|
|
{ |
156
|
|
|
$this->assertSqlGeneration( |
157
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < ?1', |
158
|
|
|
'DELETE FROM "cms_users" WHERE "id" < ?' |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testSupportsLessThanOrEqualToComparisonClause() |
163
|
|
|
{ |
164
|
|
|
$this->assertSqlGeneration( |
165
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <= ?1', |
166
|
|
|
'DELETE FROM "cms_users" WHERE "id" <= ?' |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testSupportsNotEqualToComparisonClause() |
171
|
|
|
{ |
172
|
|
|
$this->assertSqlGeneration( |
173
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <> ?1', |
174
|
|
|
'DELETE FROM "cms_users" WHERE "id" <> ?' |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testSupportsNotEqualToComparisonClauseExpressedWithExclamationMark() |
179
|
|
|
{ |
180
|
|
|
$this->assertSqlGeneration( |
181
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id != ?1', |
182
|
|
|
'DELETE FROM "cms_users" WHERE "id" <> ?' |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testSupportsNotBetweenClause() |
187
|
|
|
{ |
188
|
|
|
$this->assertSqlGeneration( |
189
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT BETWEEN ?1 AND ?2', |
190
|
|
|
'DELETE FROM "cms_users" WHERE "id" NOT BETWEEN ? AND ?' |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testSupportsBetweenClauseUsedWithAndClause() |
195
|
|
|
{ |
196
|
|
|
$this->assertSqlGeneration( |
197
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id BETWEEN ?1 AND ?2 AND u.username != ?3', |
198
|
|
|
'DELETE FROM "cms_users" WHERE "id" BETWEEN ? AND ? AND "username" <> ?' |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testSupportsNotLikeClause() |
203
|
|
|
{ |
204
|
|
|
// "WHERE" Expression LikeExpression |
205
|
|
|
$this->assertSqlGeneration( |
206
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username NOT LIKE ?1', |
207
|
|
|
'DELETE FROM "cms_users" WHERE "username" NOT LIKE ?' |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testSupportsLikeClauseWithEscapeExpression() |
212
|
|
|
{ |
213
|
|
|
$this->assertSqlGeneration( |
214
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username LIKE ?1 ESCAPE \'\\\'', |
215
|
|
|
'DELETE FROM "cms_users" WHERE "username" LIKE ? ESCAPE \'\\\'' |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testSupportsIsNullClause() |
220
|
|
|
{ |
221
|
|
|
// "WHERE" Expression NullComparisonExpression |
222
|
|
|
$this->assertSqlGeneration( |
223
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IS NULL', |
224
|
|
|
'DELETE FROM "cms_users" WHERE "name" IS NULL' |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testSupportsIsNotNullClause() |
229
|
|
|
{ |
230
|
|
|
$this->assertSqlGeneration( |
231
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IS NOT NULL', |
232
|
|
|
'DELETE FROM "cms_users" WHERE "name" IS NOT NULL' |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function testSupportsAtomExpressionAsClause() |
237
|
|
|
{ |
238
|
|
|
$this->assertSqlGeneration( |
239
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE 1 = 1', |
240
|
|
|
'DELETE FROM "cms_users" WHERE 1 = 1' |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function testSupportsParameterizedAtomExpression() |
245
|
|
|
{ |
246
|
|
|
$this->assertSqlGeneration( |
247
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE ?1 = 1', |
248
|
|
|
'DELETE FROM "cms_users" WHERE ? = 1' |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testSupportsInClause() |
253
|
|
|
{ |
254
|
|
|
$this->assertSqlGeneration( |
255
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN ( ?1, ?2, ?3, ?4 )', |
256
|
|
|
'DELETE FROM "cms_users" WHERE "id" IN (?, ?, ?, ?)' |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function testSupportsNotInClause() |
261
|
|
|
{ |
262
|
|
|
$this->assertSqlGeneration( |
263
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN ( ?1, ?2 )', |
264
|
|
|
'DELETE FROM "cms_users" WHERE "id" NOT IN (?, ?)' |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @group DDC-980 |
270
|
|
|
*/ |
271
|
|
|
public function testSubselectTableAliasReferencing() |
272
|
|
|
{ |
273
|
|
|
$this->assertSqlGeneration( |
274
|
|
|
'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10', |
275
|
|
|
'DELETE FROM "cms_users" WHERE (SELECT COUNT(*) FROM "cms_users_groups" t0 WHERE t0."user_id" = "cms_users"."id") = 10' |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|