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
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use function fopen;
use function str_repeat;
use function stream_get_contents;
class BlobTest extends FunctionalTestCase
{
protected function setUp(): void
{
if (TestUtil::isDriverOneOf('pdo_oci')) {
// inserting BLOBs as streams on Oracle requires Oracle-specific SQL syntax which is currently not supported
// see http://php.net/manual/en/pdo.lobs.php#example-1035
self::markTestSkipped("DBAL doesn't support storing LOBs represented as streams using PDO_OCI");
}
$table = Table::editor()
->setUnquotedName('blob_table')
->setColumns(
Column::editor()
->setUnquotedName('id')
->setTypeName(Types::INTEGER)
->create(),
Column::editor()
->setUnquotedName('clobcolumn')
->setTypeName(Types::TEXT)
->setNotNull(false)
->create(),
Column::editor()
->setUnquotedName('blobcolumn')
->setTypeName(Types::BLOB)
->setNotNull(false)
->create(),
)
->setPrimaryKeyConstraint(
PrimaryKeyConstraint::editor()
->setUnquotedColumnNames('id')
->create(),
)
->create();
$this->dropAndCreateTable($table);
}
public function testInsert(): void
{
$ret = $this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'test',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
self::assertEquals(1, $ret);
}
public function testInsertNull(): void
{
$ret = $this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => null,
'blobcolumn' => null,
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
self::assertEquals(1, $ret);
$row = $this->fetchRow();
self::assertCount(2, $row);
self::assertNull($row[0]);
self::assertNull($row[1]);
}
public function testInsertProcessesStream(): void
{
// https://github.com/doctrine/dbal/issues/3290
if (TestUtil::isDriverOneOf('oci8')) {
self::markTestIncomplete('The oci8 driver does not support stream resources as parameters');
}
$longBlob = str_repeat('x', 4 * 8192); // send 4 chunks
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'ignored',
'blobcolumn' => fopen('data://text/plain,' . $longBlob, 'r'),
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->assertBlobContains($longBlob);
}
public function testSelect(): void
{
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'test',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->assertBlobContains('test');
}
public function testUpdate(): void
{
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'test',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->connection->update('blob_table', ['blobcolumn' => 'test2'], ['id' => 1], [
ParameterType::LARGE_OBJECT,
ParameterType::INTEGER,
]);
$this->assertBlobContains('test2');
}
public function testUpdateProcessesStream(): void
{
// https://github.com/doctrine/dbal/issues/3290
if (TestUtil::isDriverOneOf('oci8')) {
self::markTestIncomplete('The oci8 driver does not support stream resources as parameters');
}
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'ignored',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->connection->update('blob_table', [
'id' => 1,
'blobcolumn' => fopen('data://text/plain,test2', 'r'),
], ['id' => 1], [
ParameterType::INTEGER,
ParameterType::LARGE_OBJECT,
]);
$this->assertBlobContains('test2');
}
public function testBindParamProcessesStream(): void
{
if (TestUtil::isDriverOneOf('oci8')) {
self::markTestIncomplete('The oci8 driver does not support stream resources as parameters');
}
$stmt = $this->connection->prepare(
"INSERT INTO blob_table(id, clobcolumn, blobcolumn) VALUES (1, 'ignored', ?)",
);
$stream = fopen('data://text/plain,test', 'r');
$stmt->bindValue(1, $stream, ParameterType::LARGE_OBJECT);
$stmt->executeStatement();
$this->assertBlobContains('test');
}
public function testBlobBindingDoesNotOverwritePrevious(): void
{
$table = Table::editor()
->setUnquotedName('blob_table')
->setColumns(
Column::editor()
->setUnquotedName('id')
->setTypeName(Types::INTEGER)
->create(),
Column::editor()
->setUnquotedName('blobcolumn1')
->setTypeName(Types::BLOB)
->setNotNull(false)
->create(),
Column::editor()
->setUnquotedName('blobcolumn2')
->setTypeName(Types::BLOB)
->setNotNull(false)
->create(),
)
->setPrimaryKeyConstraint(
PrimaryKeyConstraint::editor()
->setUnquotedColumnNames('id')
->create(),
)
->create();
$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();
$blobValue = Type::getType(Types::BLOB)->convertToPHPValue(
$blobValue,
$this->connection->getDatabasePlatform(),
);
self::assertIsResource($blobValue);
self::assertEquals($text, stream_get_contents($blobValue));
}
/** @return list<mixed> */
private function fetchRow(): array
{
$rows = $this->connection->fetchAllNumeric('SELECT clobcolumn, blobcolumn FROM blob_table');
self::assertCount(1, $rows);
return $rows[0];
}
}
|