Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into remove-load-from-column
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Dsn.php
  • Loading branch information
Tigrov committed Oct 18, 2024
2 parents 641fbdf + 51becbe commit 51dcaf4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- New #280: Realize `ColumnBuilder` class (@Tigrov)
- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #282: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov)
- Enh #283: Refactor `Dsn` class (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
8 changes: 3 additions & 5 deletions src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ public function insertBatch(string $table, iterable $rows, array $columns = [],
return '';
}

$tableAndColumns = ' INTO ' . $this->quoter->quoteTableName($table);
$query = 'INSERT INTO ' . $this->quoter->quoteTableName($table);

if (count($columns) > 0) {
$quotedColumnNames = array_map($this->quoter->quoteColumnName(...), $columns);

$tableAndColumns .= ' (' . implode(', ', $quotedColumnNames) . ')';
$query .= ' (' . implode(', ', $quotedColumnNames) . ')';
}

$tableAndColumns .= ' VALUES ';

return 'INSERT ALL' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL';
return $query . "\nSELECT " . implode(" FROM DUAL UNION ALL\nSELECT ", $values) . ' FROM DUAL';
}

public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string
Expand Down
2 changes: 1 addition & 1 deletion src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Dsn extends AbstractDsn
*/
public function __construct(
string $driver = 'oci',
string $host = 'localhost',
string $host = '127.0.0.1',
string|null $databaseName = null,
string $port = '1521',
array $options = []
Expand Down
29 changes: 29 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,35 @@ public function testBatchInsert(
parent::testBatchInsert($table, $values, $columns, $expected, $expectedParams, $insertedRow);
}

/** @link https://github.com/yiisoft/db-oracle/issues/284 */
public function testBatchInsertWithAutoincrement(): void
{
$db = $this->getConnection();
$command = $db->createCommand();

try {
$command->dropTable('test_batch_autoincrement')->execute();
} catch (Exception) {
}

$command->createTable('test_batch_autoincrement', [
'id' => PseudoType::PK,
'name' => ColumnType::STRING,
])->execute();

$command->insertBatch('test_batch_autoincrement', [['name' => 'John'], ['name' => 'Emma']])->execute();

$this->assertSame(
[
['id' => '1', 'name' => 'John'],
['id' => '2', 'name' => 'Emma'],
],
(new Query($db))->from('test_batch_autoincrement')->all()
);

$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
Expand Down
10 changes: 4 additions & 6 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ public static function batchInsert(): array
{
$batchInsert = parent::batchInsert();

$batchInsert['multirow']['expected'] = <<<SQL
INSERT ALL INTO "type" ("int_col", "float_col", "char_col", "bool_col") VALUES (:qp0, :qp1, :qp2, :qp3) INTO "type" ("int_col", "float_col", "char_col", "bool_col") VALUES (:qp4, :qp5, :qp6, :qp7) SELECT 1 FROM SYS.DUAL
SQL;
$batchInsert['multirow']['expectedParams'][':qp3'] = '1';
$batchInsert['multirow']['expectedParams'][':qp7'] = '0';

$replaceParams = [
'multirow' => [
':qp3' => '1',
':qp7' => '0',
],
'issue11242' => [
':qp3' => '1',
],
Expand Down
15 changes: 3 additions & 12 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,9 @@ public static function batchInsert(): array
{
$batchInsert = parent::batchInsert();

DbHelper::changeSqlForOracleBatchInsert($batchInsert['simple']['expected']);
DbHelper::changeSqlForOracleBatchInsert($batchInsert['escape-danger-chars']['expected']);
DbHelper::changeSqlForOracleBatchInsert($batchInsert['customer3']['expected']);
DbHelper::changeSqlForOracleBatchInsert($batchInsert['bool-false, bool2-null']['expected']);

$batchInsert['wrong']['expected'] = <<<SQL
INSERT ALL INTO {{%type}} ("float_col", "time") VALUES (:qp0, now()) INTO {{%type}} ("float_col", "time") VALUES (:qp1, now()) SELECT 1 FROM SYS.DUAL
SQL;

DbHelper::changeSqlForOracleBatchInsert($batchInsert['bool-false, time-now()']['expected']);
DbHelper::changeSqlForOracleBatchInsert($batchInsert['column table names are not checked']['expected']);
DbHelper::changeSqlForOracleBatchInsert($batchInsert['empty columns and non-exists table']['expected']);
foreach ($batchInsert as $key => $value) {
DbHelper::changeSqlForOracleBatchInsert($batchInsert[$key]['expected']);
}

$batchInsert['bool-false, bool2-null']['expectedParams'][':qp0'] = '0';
$batchInsert['bool-false, time-now()']['expectedParams'][':qp0'] = '0';
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface

protected static function getDb(): PdoConnectionInterface
{
$dsn = (new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']))->asString();
$dsn = (new Dsn(databaseName: 'XE', options: ['charset' => 'AL32UTF8']))->asString();

return new Connection(new Driver($dsn, 'system', 'root'), DbHelper::getSchemaCache());
}

protected function getDsn(): string
{
if ($this->dsn === '') {
$this->dsn = (new Dsn('oci', 'localhost', 'XE', '1521', ['charset' => 'AL32UTF8']))->asString();
$this->dsn = (new Dsn(databaseName: 'XE', options: ['charset' => 'AL32UTF8']))->asString();
}

return $this->dsn;
Expand Down

0 comments on commit 51dcaf4

Please sign in to comment.