-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCommandTest.php
586 lines (486 loc) · 16.8 KB
/
CommandTest.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Oracle\Tests;
use ReflectionException;
use Throwable;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Oracle\Connection;
use Yiisoft\Db\Oracle\Dsn;
use Yiisoft\Db\Oracle\Driver;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
use Yiisoft\Db\Tests\Support\Assert;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Transaction\TransactionInterface;
use function is_resource;
use function str_pad;
use function stream_get_contents;
/**
* @group oracle
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class CommandTest extends CommonCommandTest
{
use TestTrait;
protected string $upsertTestCharCast = 'CAST([[address]] AS VARCHAR(255))';
/**
* @throws Exception
* @throws InvalidConfigException
*/
public function testAddDefaultValue(): void
{
$db = $this->getConnection();
$command = $db->createCommand();
$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage(
'Yiisoft\Db\Oracle\DDLQueryBuilder::addDefaultValue is not supported by Oracle.'
);
$command->addDefaultValue('{{table}}', '{{name}}', 'column', 'value');
$db->close();
}
/**
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandProvider::batchInsert
*
* @throws Throwable
*/
public function testBatchInsert(
string $table,
array $columns,
array $values,
string $expected,
array $expectedParams = [],
int $insertedRow = 1
): void {
parent::testBatchInsert($table, $columns, $values, $expected, $expectedParams, $insertedRow);
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testCLOBStringInsertion(): void
{
$db = $this->getConnection();
$command = $db->createCommand();
$schema = $db->getSchema();
if ($schema->getTableSchema('longstring') !== null) {
$command->dropTable('longstring')->execute();
}
$command->createTable('longstring', ['message' => SchemaInterface::TYPE_TEXT])->execute();
$longData = str_pad('-', 4001, '-=', STR_PAD_LEFT);
$command->insert('longstring', ['message' => $longData])->execute();
$this->assertSame(
'1',
$command->setSql(
<<<SQL
SELECT count(*) FROM [[longstring]]
SQL,
)->queryScalar(),
);
$command->dropTable('longstring')->execute();
$db->close();
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testCreateTable(): void
{
$db = $this->getConnection(true);
$command = $db->createCommand();
$schema = $db->getSchema();
if ($schema->getTableSchema('testCreateTable') !== null) {
$command->setSql(
<<<SQL
DROP SEQUENCE testCreateTable_SEQ
SQL,
)->execute();
$command->dropTable('testCreateTable')->execute();
}
$command->createTable(
'{{testCreateTable}}',
['id' => SchemaInterface::TYPE_PK, 'bar' => SchemaInterface::TYPE_INTEGER]
)->execute();
$command->setSql(
<<<SQL
CREATE SEQUENCE testCreateTable_SEQ START with 1 INCREMENT BY 1
SQL,
)->execute();
$command->setSql(
<<<SQL
INSERT INTO [[testCreateTable]] ("id", "bar") VALUES(testCreateTable_SEQ.NEXTVAL, 1)
SQL,
)->execute();
$records = $command->setSql(
<<<SQL
SELECT [[id]], [[bar]] FROM [[testCreateTable]]
SQL,
)->queryAll();
$this->assertSame([['id' => '1', 'bar' => '1']], $records);
$db->close();
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testCreateView(): void
{
$db = $this->getConnection();
$command = $db->createCommand();
$schema = $db->getSchema();
if ($schema->getTableSchema('testCreateView') !== null) {
$command->dropView('testCreateView')->execute();
}
if ($schema->getTableSchema('testCreateViewTable') !== null) {
$command->setSql(
<<<SQL
DROP SEQUENCE testCreateViewTable_SEQ
SQL,
)->execute();
$command->dropTable('testCreateViewTable')->execute();
}
$subquery = (new Query($db))->select('{{bar}}')->from('{{testCreateViewTable}}')->where(['>', '{{bar}}', '5']);
$command->createTable(
'{{testCreateViewTable}}',
[
'[[id]]' => SchemaInterface::TYPE_PK,
'[[bar]]' => SchemaInterface::TYPE_INTEGER,
],
)->execute();
$command->setSql(
<<<SQL
CREATE SEQUENCE testCreateViewTable_SEQ START with 1 INCREMENT BY 1
SQL,
)->execute();
$command->setSql(
<<<SQL
INSERT INTO [[testCreateViewTable]] ("id", "bar") VALUES(testCreateTable_SEQ.NEXTVAL, 1)
SQL,
)->execute();
$command->setSql(
<<<SQL
INSERT INTO [[testCreateViewTable]] ("id", "bar") VALUES(testCreateTable_SEQ.NEXTVAL, 6)
SQL,
)->execute();
$command->createView('{{testCreateView}}', $subquery)->execute();
$records = $db->createCommand(
<<<SQL
SELECT [[bar]] FROM [[testCreateView]]
SQL,
)->queryAll();
$this->assertSame([['bar' => '6']], $records);
$command->dropView('testCreateView')->execute();
$db->close();
}
/**
* @throws Exception
* @throws InvalidConfigException
*/
public function testDropDefaultValue(): void
{
$db = $this->getConnection();
$command = $db->createCommand();
$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage(
'Yiisoft\Db\Oracle\DDLQueryBuilder::dropDefaultValue is not supported by Oracle.'
);
$command->dropDefaultValue('{{table}}', '{{name}}');
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws ReflectionException
* @throws Throwable
*/
public function testExecuteWithTransaction(): void
{
$db = $this->getConnection(true);
$this->assertNull($db->getTransaction());
$command = $db->createCommand(
<<<SQL
INSERT INTO {{profile}} ([[description]]) VALUES('command transaction 1')
SQL,
);
Assert::invokeMethod($command, 'requireTransaction');
$command->execute();
$this->assertNull($db->getTransaction());
$this->assertEquals(
1,
$db->createCommand(
<<<SQL
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command transaction 1'
SQL,
)->queryScalar(),
);
$command = $db->createCommand(
<<<SQL
INSERT INTO {{profile}} ([[description]]) VALUES('command transaction 2')
SQL,
);
Assert::invokeMethod($command, 'requireTransaction', [TransactionInterface::READ_COMMITTED]);
$command->execute();
$this->assertNull($db->getTransaction());
$this->assertEquals(
1,
$db->createCommand(
<<<SQL
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command transaction 2'
SQL,
)->queryScalar(),
);
$db->close();
}
/**
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandProvider::rawSql
*
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
*/
public function testGetRawSql(string $sql, array $params, string $expectedRawSql): void
{
parent::testGetRawSql($sql, $params, $expectedRawSql);
}
/**
* @throws Exception
* @throws InvalidCallException
* @throws InvalidConfigException
* @throws Throwable
*/
public function testsInsertQueryAsColumnValue(): void
{
$db = $this->getConnection(true);
$command = $db->createCommand();
$time = (string) time();
$command->delete('{{order_with_null_fk}}')->execute();
$command->insert('{{order}}', ['customer_id' => 1, 'created_at' => $time, 'total' => 42])->execute();
$columnValueQuery = new Query($db);
$orderId = $db->getLastInsertID('order_SEQ');
$columnValueQuery->select('created_at')->from('{{order}}')->where(['id' => $orderId]);
$command->insert(
'{{order_with_null_fk}}',
[
'customer_id' => $orderId,
'created_at' => $columnValueQuery,
'total' => 42,
],
)->execute();
$this->assertSame(
$time,
$command->setSql(
<<<SQL
SELECT [[created_at]] FROM {{order_with_null_fk}} WHERE [[customer_id]] = :orderId
SQL,
)->bindValues([':orderId' => $orderId])->queryScalar(),
);
$command->delete('{{order_with_null_fk}}')->execute();
$command->delete('{{order}}', ['id' => $orderId])->execute();
$db->close();
}
/**
* @throws Exception
* @throws InvalidCallException
* @throws InvalidConfigException
* @throws Throwable
*/
public function testInsertWithReturningPksWithPrimaryKeyString(): void
{
$db = $this->getConnection();
$command = $db->createCommand();
$schema = $db->getSchema();
if ($schema->getTableSchema('{{test_insert_ex_string}}') !== null) {
$command->dropTable('{{test_insert_ex_string}}')->execute();
}
$command->createTable(
'{{test_insert_ex_string}}',
['id' => 'varchar(10) primary key', 'name' => 'varchar(10)'],
)->execute();
$result = $command->insertWithReturningPks('{{test_insert_ex_string}}', ['id' => '1', 'name' => 'test']);
$this->assertSame(['id' => '1'], $result);
$db->close();
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testInsertSelectAlias(): void
{
$db = $this->getConnection();
$command = $db->createCommand();
$command->delete('{{customer}}')->execute();
$command->insert(
'{{customer}}',
[
'email' => '[email protected]',
'name' => 'test',
'address' => 'test address',
]
)->execute();
$query = $command->setSql(
<<<SQL
SELECT '[email protected]' AS [[email]], [[address]] AS [[name]], [[name]] AS [[address]] FROM [[customer]]
SQL,
);
$row = $query->queryOne();
$this->assertIsArray($row);
$command->insert('{{customer}}', $row)->execute();
$this->assertEquals(2, $command->setSql(
<<<SQL
SELECT COUNT(*) FROM [[customer]]
SQL,
)->queryScalar());
$record = $command->setSql(
<<<SQL
SELECT [[email]], [[name]], [[address]] FROM {{customer}}
SQL,
)->queryAll();
$this->assertEquals([
[
'email' => '[email protected]',
'name' => 'test',
'address' => 'test address',
],
[
'email' => '[email protected]',
'name' => 'test address',
'address' => 'test',
],
], $record);
$db->close();
}
/**
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandProvider::insertVarbinary
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testInsertVarbinary(mixed $expectedData, mixed $testData): void
{
$db = $this->getConnection(true);
$command = $db->createCommand();
$command->delete('{{T_upsert_varbinary}}')->execute();
$command->insert('{{T_upsert_varbinary}}', ['id' => 1, 'blob_col' => $testData])->execute();
$query = (new Query($db))->select(['blob_col'])->from('{{T_upsert_varbinary}}')->where(['id' => 1]);
$resultData = $query->createCommand()->queryOne();
$this->assertIsArray($resultData);
/** @var mixed $resultBlob */
$resultBlob = is_resource($resultData['blob_col']) ? stream_get_contents($resultData['blob_col']) : $resultData['blob_col'];
$this->assertSame($expectedData, $resultBlob);
$db->close();
}
/**
* @throws Exception
* @throws InvalidCallException
* @throws InvalidConfigException
* @throws Throwable
*/
public function testNoTablenameReplacement(): void
{
$db = $this->getConnection(true);
$command = $db->createCommand();
$command->insert(
'{{customer}}',
[
'name' => 'Some {{weird}} name',
'email' => '[email protected]',
'address' => 'Some {{%weird}} address',
],
)->execute();
$customerId = $db->getLastInsertID('customer_SEQ');
$customer = $command->setSql(
<<<SQL
SELECT * FROM {{customer}} WHERE
SQL . ' [[id]] = ' . $customerId,
)->queryOne();
$this->assertIsArray($customer);
$this->assertSame('Some {{weird}} name', $customer['name']);
$this->assertSame('Some {{%weird}} address', $customer['address']);
$command->update(
'{{customer}}',
[
'name' => 'Some {{updated}} name',
'address' => 'Some {{%updated}} address',
],
['id' => $customerId]
)->execute();
$customer = $command->setSql(
<<<SQL
SELECT * FROM {{customer}} WHERE
SQL . ' [[id]] = ' . $customerId
)->queryOne();
$this->assertIsArray($customer);
$this->assertSame('Some {{updated}} name', $customer['name']);
$this->assertSame('Some {{%updated}} address', $customer['address']);
$db->close();
}
/**
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandProvider::update
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testUpdate(
string $table,
array $columns,
array|string $conditions,
array $params,
array $expectedValues,
int $expectedCount,
): void {
parent::testUpdate($table, $columns, $conditions, $params, $expectedValues, $expectedCount);
}
/**
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\CommandProvider::upsert
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testUpsert(array $firstData, array $secondData): void
{
parent::testUpsert($firstData, $secondData);
}
/**
* @throws InvalidConfigException
* @throws InvalidArgumentException
* @throws NotSupportedException
* @throws Exception
* @throws Throwable
*/
public function testQueryScalarWithBlob(): void
{
$db = $this->getConnection(true);
$value = json_encode(['test'], JSON_THROW_ON_ERROR);
$db->createCommand()->insert('{{%T_upsert_varbinary}}', ['id' => 1, 'blob_col' => $value])->execute();
$scalarValue = $db->createCommand('SELECT [[blob_col]] FROM {{%T_upsert_varbinary}}')->queryScalar();
$this->assertEquals($value, $scalarValue);
}
public function testProfiler(string $sql = null): void
{
parent::testProfiler('SELECT 123 FROM DUAL');
}
public function testProfilerData(string $sql = null): void
{
parent::testProfilerData('SELECT 123 FROM DUAL');
}
public function testShowDatabases(): void
{
$dsn = new Dsn('oci', 'localhost');
$db = new Connection(new Driver($dsn->asString(), 'SYSTEM', 'root'), DbHelper::getSchemaCache());
$command = $db->createCommand();
$this->assertSame('oci:dbname=localhost:1521', $db->getDriver()->getDsn());
$this->assertSame(['YIITEST'], $command->showDatabases());
}
}