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