Skip to content

Commit

Permalink
Fix blob binding overwrite on DB2
Browse files Browse the repository at this point in the history
On DB2 when binding more than 1 blob in the same statement each subsequent blob overwrites previous one.
So instead of [1, 2, 3] we get [2, 3, 3]
This happens because of reference between loop variable `$value` and the item in `$this->parameters[]`,
which remains after iteration, see `bindLobs` and `bind` methods in `\Doctrine\DBAL\Driver\IBMDB2\Statement`
  • Loading branch information
melekhovets committed Jul 16, 2023
1 parent 1c462ee commit 8eee35b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Driver/IBMDB2/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ private function bindLobs(): array
} else {
$this->bind($param, $value, DB2_PARAM_IN, DB2_CHAR);
}

unset($value);
}

return $handles;
Expand Down
28 changes: 28 additions & 0 deletions tests/Functional/BlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,34 @@ public function testBindParamProcessesStream(): void
$this->assertBlobContains('test');
}

public function testBlobBindingDoesNotOverwritePrevious(): void
{
$table = new Table('blob_table');
$table->addColumn('id', 'integer');
$table->addColumn('blobcolumn1', 'blob', ['notnull' => false]);
$table->addColumn('blobcolumn2', 'blob', ['notnull' => false]);
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);

$params = ['test1', 'test2'];
$this->connection->executeStatement(
'INSERT INTO blob_table(id, blobcolumn1, blobcolumn2) VALUES (1, ?, ?)',
$params,
[ParameterType::LARGE_OBJECT, ParameterType::LARGE_OBJECT],
);

$blobs = $this->connection->fetchNumeric('SELECT blobcolumn1, blobcolumn2 FROM blob_table');
self::assertIsArray($blobs);

$actual = [];
foreach ($blobs as $blob) {
$blob = Type::getType('blob')->convertToPHPValue($blob, $this->connection->getDatabasePlatform());
$actual[] = stream_get_contents($blob);
}

self::assertEquals(['test1', 'test2'], $actual);
}

private function assertBlobContains(string $text): void
{
[, $blobValue] = $this->fetchRow();
Expand Down

0 comments on commit 8eee35b

Please sign in to comment.