|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Common; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
9
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
|
10
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilder; |
|
11
|
|
|
use Yiisoft\Db\Schema\Schema; |
|
12
|
|
|
use Yiisoft\Db\Tests\AbstractQueryBuilderTest; |
|
13
|
|
|
use Yiisoft\Db\Tests\Provider\ColumnTypesProvider; |
|
14
|
|
|
use Yiisoft\Db\Tests\Support\DbHelper; |
|
15
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
|
16
|
|
|
|
|
17
|
|
|
use function is_array; |
|
18
|
|
|
use function str_replace; |
|
19
|
|
|
use function str_starts_with; |
|
20
|
|
|
use function strncmp; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @group mssql |
|
24
|
|
|
* @group mysql |
|
25
|
|
|
* @group pgsql |
|
26
|
|
|
* @group oracle |
|
27
|
|
|
* @group sqlite |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class CommonQueryBuilderTest extends AbstractQueryBuilderTest |
|
30
|
|
|
{ |
|
31
|
|
|
use TestTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropChecks() |
|
35
|
|
|
*/ |
|
36
|
|
|
public function testAddDropCheck(string $sql, Closure $builder): void |
|
37
|
|
|
{ |
|
38
|
|
|
$db = $this->getConnection(); |
|
39
|
|
|
|
|
40
|
|
|
$qb = $db->getQueryBuilder(); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropForeignKeys() |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testAddDropForeignKey(string $sql, Closure $builder): void |
|
49
|
|
|
{ |
|
50
|
|
|
$db = $this->getConnection(); |
|
51
|
|
|
|
|
52
|
|
|
$qb = $db->getQueryBuilder(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropPrimaryKeys() |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testAddDropPrimaryKey(string $sql, Closure $builder): void |
|
61
|
|
|
{ |
|
62
|
|
|
$db = $this->getConnection(); |
|
63
|
|
|
|
|
64
|
|
|
$qb = $db->getQueryBuilder(); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropUniques() |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testAddDropUnique(string $sql, Closure $builder): void |
|
73
|
|
|
{ |
|
74
|
|
|
$db = $this->getConnection(); |
|
75
|
|
|
|
|
76
|
|
|
$qb = $db->getQueryBuilder(); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::alterColumn() |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testAlterColumn( |
|
85
|
|
|
string $table, |
|
86
|
|
|
string $column, |
|
87
|
|
|
ColumnSchemaBuilder|string $type, |
|
88
|
|
|
string $expected |
|
89
|
|
|
): void { |
|
90
|
|
|
$db = $this->getConnection(); |
|
91
|
|
|
|
|
92
|
|
|
$qb = $db->getQueryBuilder(); |
|
93
|
|
|
$sql = $qb->alterColumn($table, $column, $type); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertSame($expected, $sql); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::batchInsert() |
|
100
|
|
|
*/ |
|
101
|
|
|
public function testBatchInsert( |
|
102
|
|
|
string $table, |
|
103
|
|
|
array $columns, |
|
104
|
|
|
array $value, |
|
105
|
|
|
string|null $expected, |
|
106
|
|
|
array $expectedParams = [] |
|
107
|
|
|
): void { |
|
108
|
|
|
$db = $this->getConnection(); |
|
109
|
|
|
|
|
110
|
|
|
$qb = $db->getQueryBuilder(); |
|
111
|
|
|
$params = []; |
|
112
|
|
|
$sql = $qb->batchInsert($table, $columns, $value, $params); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertSame($expected, $sql); |
|
115
|
|
|
$this->assertSame($expectedParams, $params); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildConditions() |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testBuildCondition( |
|
122
|
|
|
array|ExpressionInterface|string $conditions, |
|
123
|
|
|
string $expected, |
|
124
|
|
|
array $expectedParams = [] |
|
125
|
|
|
): void { |
|
126
|
|
|
$db = $this->getConnection(); |
|
127
|
|
|
|
|
128
|
|
|
$qb = $db->getQueryBuilder(); |
|
129
|
|
|
$query = $this->getQuery($db)->where($conditions); |
|
130
|
|
|
|
|
131
|
|
|
[$sql, $params] = $qb->build($query); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertSame( |
|
134
|
|
|
'SELECT *' . ( |
|
135
|
|
|
empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getName()) |
|
136
|
|
|
), |
|
137
|
|
|
$sql, |
|
138
|
|
|
); |
|
139
|
|
|
$this->assertSame($expectedParams, $params); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildFilterConditions() |
|
144
|
|
|
*/ |
|
145
|
|
|
public function testBuildFilterCondition(array $condition, string $expected, array $expectedParams): void |
|
146
|
|
|
{ |
|
147
|
|
|
$db = $this->getConnection(); |
|
148
|
|
|
|
|
149
|
|
|
$qb = $db->getQueryBuilder(); |
|
150
|
|
|
$query = $this->getQuery($db)->filterWhere($condition); |
|
151
|
|
|
|
|
152
|
|
|
[$sql, $params] = $qb->build($query); |
|
153
|
|
|
|
|
154
|
|
|
$this->assertSame( |
|
155
|
|
|
'SELECT *' . ( |
|
156
|
|
|
empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getName()) |
|
157
|
|
|
), |
|
158
|
|
|
$sql, |
|
159
|
|
|
); |
|
160
|
|
|
$this->assertSame($expectedParams, $params); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildFrom() |
|
165
|
|
|
*/ |
|
166
|
|
|
public function testBuildFrom(string $table, string $expected): void |
|
167
|
|
|
{ |
|
168
|
|
|
$db = $this->getConnection(); |
|
169
|
|
|
|
|
170
|
|
|
$qb = $db->getQueryBuilder(); |
|
171
|
|
|
$params = []; |
|
172
|
|
|
$sql = $qb->buildFrom([$table], $params); |
|
173
|
|
|
$replacedQuotes = DbHelper::replaceQuotes($expected, $db->getName()); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertIsString($replacedQuotes); |
|
176
|
|
|
$this->assertSame('FROM ' . $replacedQuotes, $sql); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildWhereExists() |
|
181
|
|
|
*/ |
|
182
|
|
|
public function testBuildWhereExists(string $cond, string $expectedQuerySql): void |
|
183
|
|
|
{ |
|
184
|
|
|
$db = $this->getConnection(); |
|
185
|
|
|
|
|
186
|
|
|
$qb = $db->getQueryBuilder(); |
|
187
|
|
|
$expectedQueryParams = []; |
|
188
|
|
|
$subQuery = $this->getQuery($db)->select('1')->from('Website w'); |
|
189
|
|
|
$query = $this->getQuery($db)->select('id')->from('TotalExample t')->where([$cond, $subQuery]); |
|
190
|
|
|
|
|
191
|
|
|
[$actualQuerySql, $actualQueryParams] = $qb->build($query); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertSame($expectedQuerySql, $actualQuerySql); |
|
194
|
|
|
$this->assertSame($expectedQueryParams, $actualQueryParams); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::createDropIndex() |
|
199
|
|
|
*/ |
|
200
|
|
|
public function testCreateDropIndex(string $sql, Closure $builder): void |
|
201
|
|
|
{ |
|
202
|
|
|
$db = $this->getConnection(); |
|
203
|
|
|
|
|
204
|
|
|
$qb = $db->getQueryBuilder(); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function testCreateTableColumnTypes(): void |
|
210
|
|
|
{ |
|
211
|
|
|
$db = $this->getConnectionWithData(); |
|
212
|
|
|
|
|
213
|
|
|
$qb = $db->getQueryBuilder(); |
|
214
|
|
|
|
|
215
|
|
|
if ($db->getTableSchema('column_type_table', true) !== null) { |
|
216
|
|
|
$db->createCommand($qb->dropTable('column_type_table'))->execute(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$columnTypes = (new ColumnTypesProvider())->columnTypes($db); |
|
220
|
|
|
$columns = []; |
|
221
|
|
|
$i = 0; |
|
222
|
|
|
|
|
223
|
|
|
foreach ($columnTypes as [$column, $builder, $expected]) { |
|
224
|
|
|
if ( |
|
225
|
|
|
!( |
|
226
|
|
|
strncmp($column, Schema::TYPE_PK, 2) === 0 || |
|
227
|
|
|
strncmp($column, Schema::TYPE_UPK, 3) === 0 || |
|
228
|
|
|
strncmp($column, Schema::TYPE_BIGPK, 5) === 0 || |
|
229
|
|
|
strncmp($column, Schema::TYPE_UBIGPK, 6) === 0 || |
|
230
|
|
|
str_starts_with(substr($column, -5), 'FIRST') |
|
231
|
|
|
) |
|
232
|
|
|
) { |
|
233
|
|
|
$columns['col' . ++$i] = str_replace('CHECK (value', 'CHECK ([[col' . $i . ']]', $column); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$db->createCommand($qb->createTable('column_type_table', $columns))->execute(); |
|
238
|
|
|
|
|
239
|
|
|
$this->assertNotEmpty($db->getTableSchema('column_type_table', true)); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::delete() |
|
244
|
|
|
*/ |
|
245
|
|
|
public function testDelete(string $table, array|string $condition, string $expectedSQL, array $expectedParams): void |
|
246
|
|
|
{ |
|
247
|
|
|
$db = $this->getConnection(); |
|
248
|
|
|
|
|
249
|
|
|
$qb = $db->getQueryBuilder(); |
|
250
|
|
|
$actualParams = []; |
|
251
|
|
|
$actualSQL = $qb->delete($table, $condition, $actualParams); |
|
252
|
|
|
|
|
253
|
|
|
$this->assertSame($expectedSQL, $actualSQL); |
|
254
|
|
|
$this->assertSame($expectedParams, $actualParams); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function testGetColumnType(): void |
|
258
|
|
|
{ |
|
259
|
|
|
$db = $this->getConnection(); |
|
260
|
|
|
|
|
261
|
|
|
$columnTypes = (new ColumnTypesProvider())->columnTypes($db); |
|
262
|
|
|
$qb = $db->getQueryBuilder(); |
|
263
|
|
|
|
|
264
|
|
|
foreach ($columnTypes as $item) { |
|
265
|
|
|
[$column, $builder, $expected] = $item; |
|
266
|
|
|
|
|
267
|
|
|
$driverName = $db->getName(); |
|
268
|
|
|
|
|
269
|
|
|
if (isset($item[3][$driverName])) { |
|
270
|
|
|
$expectedColumnSchemaBuilder = $item[3][$driverName]; |
|
271
|
|
|
} elseif (isset($item[3]) && !is_array($item[3])) { |
|
272
|
|
|
$expectedColumnSchemaBuilder = $item[3]; |
|
273
|
|
|
} else { |
|
274
|
|
|
$expectedColumnSchemaBuilder = $column; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$this->assertSame($expectedColumnSchemaBuilder, $builder->__toString()); |
|
278
|
|
|
$this->assertSame($expected, $qb->getColumnType($column)); |
|
279
|
|
|
$this->assertSame($expected, $qb->getColumnType($builder)); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::insert() |
|
285
|
|
|
*/ |
|
286
|
|
|
public function testInsert( |
|
287
|
|
|
string $table, |
|
288
|
|
|
array|QueryInterface $columns, |
|
289
|
|
|
array $params, |
|
290
|
|
|
string $expectedSQL, |
|
291
|
|
|
array $expectedParams |
|
292
|
|
|
): void { |
|
293
|
|
|
$db = $this->getConnection(); |
|
294
|
|
|
|
|
295
|
|
|
$qb = $db->getQueryBuilder(); |
|
296
|
|
|
|
|
297
|
|
|
$this->assertSame($expectedSQL, $qb->insert($table, $columns, $params)); |
|
298
|
|
|
$this->assertSame($expectedParams, $params); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::insertEx() |
|
303
|
|
|
*/ |
|
304
|
|
|
public function testInsertEx( |
|
305
|
|
|
string $table, |
|
306
|
|
|
array|QueryInterface $columns, |
|
307
|
|
|
array $params, |
|
308
|
|
|
string $expectedSQL, |
|
309
|
|
|
array $expectedParams |
|
310
|
|
|
): void { |
|
311
|
|
|
$db = $this->getConnection(); |
|
312
|
|
|
|
|
313
|
|
|
$qb = $db->getQueryBuilder(); |
|
314
|
|
|
|
|
315
|
|
|
$this->assertSame($expectedSQL, $qb->insertEx($table, $columns, $params)); |
|
316
|
|
|
$this->assertSame($expectedParams, $params); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::update() |
|
321
|
|
|
*/ |
|
322
|
|
|
public function testUpdate( |
|
323
|
|
|
string $table, |
|
324
|
|
|
array $columns, |
|
325
|
|
|
array|string $condition, |
|
326
|
|
|
string $expectedSQL, |
|
327
|
|
|
array $expectedParams |
|
328
|
|
|
): void { |
|
329
|
|
|
$db = $this->getConnection(); |
|
330
|
|
|
|
|
331
|
|
|
$qb = $db->getQueryBuilder(); |
|
332
|
|
|
$actualParams = []; |
|
333
|
|
|
|
|
334
|
|
|
$this->assertSame($expectedSQL, $qb->update($table, $columns, $condition, $actualParams)); |
|
335
|
|
|
$this->assertSame($expectedParams, $actualParams); |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|