-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
CommonTableExpressionQueryTests.php
446 lines (393 loc) · 13.1 KB
/
CommonTableExpressionQueryTests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Database\QueryTests;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Sqlite;
use Cake\Database\Driver\Sqlserver;
use Cake\Database\Expression\CommonTableExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\Query;
use Cake\Database\ValueBinder;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
class CommonTableExpressionQueryTests extends TestCase
{
/**
* @inheritDoc
*/
protected $fixtures = [
'core.Articles',
];
/**
* @inheritDoc
*/
public $autoFixtures = false;
/**
* @var \Cake\Database\Connection
*/
protected $connection;
/**
* @var bool
*/
protected $autoQuote;
public function setUp(): void
{
parent::setUp();
$this->connection = ConnectionManager::get('test');
$this->skipIf(
!$this->connection->getDriver()->supportsCTEs(),
'The current driver does not support common table expressions.'
);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->connection);
}
/**
* Tests with() sql generation.
*
* @return void
*/
public function testWithCte()
{
$query = $this->connection->newQuery()
->with(new CommonTableExpression('cte', function () {
return $this->connection->newQuery()->select(['col' => 1]);
}))
->select('col')
->from('cte');
$this->assertEqualsSql(
'WITH cte AS (SELECT 1 AS col) SELECT col FROM cte',
$query->sql(new ValueBinder())
);
$expected = [
[
'col' => '1',
],
];
$result = $query->execute();
$this->assertEquals($expected, $result->fetchAll('assoc'));
$result->closeCursor();
}
/**
* Tests calling with() with overwrite clears other CTEs.
*
* @return void
*/
public function testWithCteOverwrite()
{
$query = $this->connection->newQuery()
->with(new CommonTableExpression('cte', function () {
return $this->connection->newQuery()->select(['col' => '1']);
}))
->select('col')
->from('cte');
$this->assertEqualsSql(
'WITH cte AS (SELECT 1 AS col) SELECT col FROM cte',
$query->sql(new ValueBinder())
);
$query
->with(new CommonTableExpression('cte2', $this->connection->newQuery()), true)
->from('cte2', true);
$this->assertEqualsSql(
'WITH cte2 AS () SELECT col FROM cte2',
$query->sql(new ValueBinder())
);
}
/**
* Tests recursive CTE.
*
* @return void
*/
public function testWithRecursiveCte()
{
$query = $this->connection->newQuery()
->with(function (CommonTableExpression $cte, Query $query) {
$anchorQuery = $query->select(1);
$recursiveQuery = $query->getConnection()
->newQuery()
->select(function (Query $query) {
return $query->newExpr('col + 1');
})
->from('cte')
->where(['col !=' => 3], ['col' => 'integer']);
$cteQuery = $anchorQuery->unionAll($recursiveQuery);
return $cte
->name('cte')
->field(['col'])
->query($cteQuery)
->recursive();
})
->select('col')
->from('cte');
if ($this->connection->getDriver() instanceof Sqlserver) {
$expectedSql =
"WITH cte(col) AS " .
"(SELECT 1\nUNION ALL SELECT (col + 1) FROM cte WHERE col != :c0) " .
"SELECT col FROM cte";
} elseif ($this->connection->getDriver() instanceof Sqlite) {
$expectedSql =
"WITH RECURSIVE cte(col) AS " .
"(SELECT 1\nUNION ALL SELECT (col + 1) FROM cte WHERE col != :c0) " .
"SELECT col FROM cte";
} else {
$expectedSql =
"WITH RECURSIVE cte(col) AS " .
"((SELECT 1)\nUNION ALL (SELECT (col + 1) FROM cte WHERE col != :c0)) " .
"SELECT col FROM cte";
}
$this->assertEqualsSql(
$expectedSql,
$query->sql(new ValueBinder())
);
$expected = [
[
'col' => '1',
],
[
'col' => '2',
],
[
'col' => '3',
],
];
$result = $query->execute();
$this->assertEquals($expected, $result->fetchAll('assoc'));
$result->closeCursor();
}
/**
* Test inserting from CTE.
*
* @return void
*/
public function testWithInsertQuery()
{
$this->skipIf(
($this->connection->getDriver() instanceof Mysql),
'`WITH ... INSERT INTO` syntax is not supported in MySQL.'
);
$this->loadFixtures('Articles');
// test initial state
$result = $this->connection->newQuery()
->select('*')
->from('articles')
->where(['id' => 4])
->execute();
$this->assertFalse($result->fetch('assoc'));
$result->closeCursor();
$query = $this->connection
->newQuery()
->with(function (CommonTableExpression $cte, Query $query) {
return $cte
->name('cte')
->field(['title', 'body'])
->query($query->newExpr("SELECT 'Fourth Article', 'Fourth Article Body'"));
})
->insert(['title', 'body'])
->into('articles')
->values(
$this->connection
->newQuery()
->select('*')
->from('cte')
);
$this->assertStartsWithSql(
"WITH cte(title, body) AS (SELECT 'Fourth Article', 'Fourth Article Body') " .
"INSERT INTO articles (title, body)",
$query->sql(new ValueBinder())
);
// run insert
$query->execute()->closeCursor();
$expected = [
'id' => '4',
'author_id' => null,
'title' => 'Fourth Article',
'body' => 'Fourth Article Body',
'published' => 'N',
];
// test updated state
$result = $this->connection->newQuery()
->select('*')
->from('articles')
->where(['id' => 4])
->execute();
$this->assertEquals($expected, $result->fetch('assoc'));
$result->closeCursor();
}
/**
* Tests inserting from CTE as values list.
*
* @return void
*/
public function testWithInInsertWithValuesQuery()
{
$this->skipIf(
($this->connection->getDriver() instanceof Sqlserver),
'`INSERT INTO ... WITH` syntax is not supported in SQL Server.'
);
$this->loadFixtures('Articles');
$query = $this->connection->newQuery()
->insert(['title', 'body'])
->into('articles')
->values(
$this->connection->newQuery()
->with(function (CommonTableExpression $cte, Query $query) {
return $cte
->name('cte')
->field(['title', 'body'])
->query($query->newExpr("SELECT 'Fourth Article', 'Fourth Article Body'"));
})
->select('*')
->from('cte')
);
$this->assertStartsWithSql(
"INSERT INTO articles (title, body) " .
"WITH cte(title, body) AS (SELECT 'Fourth Article', 'Fourth Article Body') SELECT * FROM cte",
$query->sql(new ValueBinder())
);
// run insert
$query->execute()->closeCursor();
$expected = [
'id' => '4',
'author_id' => null,
'title' => 'Fourth Article',
'body' => 'Fourth Article Body',
'published' => 'N',
];
// test updated state
$result = $this->connection->newQuery()
->select('*')
->from('articles')
->where(['id' => 4])
->execute();
$this->assertEquals($expected, $result->fetch('assoc'));
$result->closeCursor();
}
/**
* Tests updating from CTE.
*
* @return void
*/
public function testWithInUpdateQuery()
{
$this->loadFixtures('Articles');
// test initial state
$result = $this->connection->newQuery()
->select(['count' => 'COUNT(*)'])
->from('articles')
->where(['published' => 'Y'])
->execute();
$this->assertEquals(['count' => '3'], $result->fetch('assoc'));
$result->closeCursor();
$query = $this->connection->newQuery()
->with(function (CommonTableExpression $cte, Query $query) {
$cteQuery = $query
->select('articles.id')
->from('articles')
->where(['articles.id !=' => 1]);
return $cte
->name('cte')
->query($cteQuery);
})
->update('articles')
->set('published', 'N')
->where(function (QueryExpression $exp, Query $query) {
return $exp->in(
'articles.id',
$query
->getConnection()
->newQuery()
->select('cte.id')
->from('cte')
);
});
$this->assertEqualsSql(
"WITH cte AS (SELECT articles.id FROM articles WHERE articles.id != :c0) " .
"UPDATE articles SET published = :c1 WHERE id IN (SELECT cte.id FROM cte)",
$query->sql(new ValueBinder())
);
// run update
$query->execute()->closeCursor();
// test updated state
$result = $this->connection->newQuery()
->select(['count' => 'COUNT(*)'])
->from('articles')
->where(['published' => 'Y'])
->execute();
$this->assertEquals(['count' => '1'], $result->fetch('assoc'));
$result->closeCursor();
}
/**
* Tests deleting from CTE.
*
* @return void
*/
public function testWithInDeleteQuery()
{
$this->loadFixtures('Articles');
// test initial state
$result = $this->connection
->newQuery()
->select(['count' => 'COUNT(*)'])
->from('articles')
->execute();
$this->assertEquals(['count' => '3'], $result->fetch('assoc'));
$result->closeCursor();
$query = $this->connection->newQuery()
->with(function (CommonTableExpression $cte, Query $query) {
$query->select('articles.id')
->from('articles')
->where(['articles.id !=' => 1]);
return $cte
->name('cte')
->query($query);
})
->delete()
->from(['a' => 'articles'])
->where(function (QueryExpression $exp, Query $query) {
return $exp->in(
'a.id',
$query
->getConnection()
->newQuery()
->select('cte.id')
->from('cte')
);
});
$this->assertEqualsSql(
"WITH cte AS (SELECT articles.id FROM articles WHERE articles.id != :c0) " .
"DELETE FROM articles WHERE id IN (SELECT cte.id FROM cte)",
$query->sql(new ValueBinder())
);
// run delete
$query->execute()->closeCursor();
$expected = [
'id' => '1',
'author_id' => '1',
'title' => 'First Article',
'body' => 'First Article Body',
'published' => 'Y',
];
// test updated state
$result = $this->connection->newQuery()
->select('*')
->from('articles')
->execute();
$this->assertEquals($expected, $result->fetch('assoc'));
$result->closeCursor();
}
}