Skip to content

Commit 67bd64c

Browse files
committed
Removed the logic of using BLOB columns for BINARY type of fields
1 parent acb8057 commit 67bd64c

File tree

12 files changed

+82
-162
lines changed

12 files changed

+82
-162
lines changed

UPGRADE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ The Drizzle project is abandoned and is therefore not supported by Doctrine DBAL
102102
- `Configuration::getSQLLogger()` does not return `null` anymore, but a `NullLogger` implementation.
103103
- `Configuration::setSQLLogger()` does not allow `null` anymore.
104104

105+
## BC BREAK: Changes to handling binary fields
106+
107+
- Binary fields whose length exceeds the maximum field size on a given platform are no longer represented as `BLOB`s.
108+
Use binary fields of a size which fits all target platforms, or use blob explicitly instead.
109+
- Binary fields are no longer represented as streams in PHP. They are represented as strings.
110+
105111
# Upgrade to 2.8
106112

107113
## Deprecated usage of binary fields whose length exceeds the platform maximum

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Doctrine\DBAL\TransactionIsolationLevel;
2727
use Doctrine\DBAL\Types;
2828
use Doctrine\DBAL\Types\Type;
29-
use const E_USER_DEPRECATED;
3029
use function addcslashes;
3130
use function array_map;
3231
use function array_merge;
@@ -311,20 +310,6 @@ public function getBinaryTypeDeclarationSQL(array $field)
311310

312311
$fixed = $field['fixed'] ?? false;
313312

314-
$maxLength = $this->getBinaryMaxLength();
315-
316-
if ($field['length'] > $maxLength) {
317-
if ($maxLength > 0) {
318-
@trigger_error(sprintf(
319-
'Binary field length %d is greater than supported by the platform (%d). Reduce the field length or use a BLOB field instead.',
320-
$field['length'],
321-
$maxLength
322-
), E_USER_DEPRECATED);
323-
}
324-
325-
return $this->getBlobTypeDeclarationSQL($field);
326-
}
327-
328313
return $this->getBinaryTypeDeclarationSQLSnippet($field['length'], $fixed);
329314
}
330315

lib/Doctrine/DBAL/Types/BinaryType.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
use Doctrine\DBAL\ParameterType;
66
use Doctrine\DBAL\Platforms\AbstractPlatform;
7-
use function fopen;
8-
use function fseek;
9-
use function fwrite;
107
use function is_resource;
118
use function is_string;
9+
use function stream_get_contents;
1210

1311
/**
1412
* Type that maps ab SQL BINARY/VARBINARY to a PHP resource stream.
@@ -35,14 +33,11 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
3533
return null;
3634
}
3735

38-
if (is_string($value)) {
39-
$fp = fopen('php://temp', 'rb+');
40-
fwrite($fp, $value);
41-
fseek($fp, 0);
42-
$value = $fp;
36+
if (is_resource($value)) {
37+
$value = stream_get_contents($value);
4338
}
4439

45-
if ( ! is_resource($value)) {
40+
if (! is_string($value)) {
4641
throw ConversionException::conversionFailed($value, self::BINARY);
4742
}
4843

tests/Doctrine/Tests/DBAL/Functional/Types/BinaryTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
88
use Doctrine\DBAL\ParameterType;
99
use Doctrine\DBAL\Schema\Table;
10+
use Doctrine\DBAL\Types\Type;
1011
use Doctrine\Tests\DbalFunctionalTestCase;
11-
use function is_resource;
1212
use function random_bytes;
1313
use function str_replace;
14-
use function stream_get_contents;
1514

1615
class BinaryTest extends DbalFunctionalTestCase
1716
{
@@ -74,14 +73,6 @@ private function select(string $id)
7473
[ParameterType::BINARY]
7574
);
7675

77-
// Currently, `BinaryType` mistakenly converts string values fetched from the DB to a stream.
78-
// It should be the opposite. Streams should be used to represent large objects, not binary
79-
// strings. The confusion comes from the PostgreSQL's type system where binary strings and
80-
// large objects are represented by the same BYTEA type
81-
if (is_resource($value)) {
82-
$value = stream_get_contents($value);
83-
}
84-
85-
return $value;
76+
return Type::getType('binary')->convertToPHPValue($value, $this->_conn->getDatabasePlatform());
8677
}
8778
}

tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -516,30 +516,24 @@ protected function getBinaryMaxLength()
516516

517517
public function testReturnsBinaryTypeDeclarationSQL()
518518
{
519-
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
520-
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
521-
self::assertSame('VARBINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 65535)));
522-
523-
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
524-
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
525-
self::assertSame('BINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 65535)));
526-
}
527-
528-
/**
529-
* @group legacy
530-
* @expectedDeprecation Binary field length 65536 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
531-
* @expectedDeprecation Binary field length 16777215 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
532-
* @expectedDeprecation Binary field length 16777216 is greater than supported by the platform (65535). Reduce the field length or use a BLOB field instead.
533-
*/
534-
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL()
535-
{
536-
self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 65536]));
537-
self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 16777215]));
538-
self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 16777216]));
539-
540-
self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 65536]));
541-
self::assertSame('MEDIUMBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777215]));
542-
self::assertSame('LONGBLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 16777216]));
519+
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL([]));
520+
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 0]));
521+
self::assertSame('VARBINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 65535]));
522+
self::assertSame('VARBINARY(65536)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 65536]));
523+
524+
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
525+
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL([
526+
'fixed' => true,
527+
'length' => 0,
528+
]));
529+
self::assertSame('BINARY(65535)', $this->_platform->getBinaryTypeDeclarationSQL([
530+
'fixed' => true,
531+
'length' => 65535,
532+
]));
533+
self::assertSame('BINARY(65536)', $this->_platform->getBinaryTypeDeclarationSQL([
534+
'fixed' => true,
535+
'length' => 65536,
536+
]));
543537
}
544538

545539
public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines()

tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,6 @@ public function testReturnsBinaryTypeDeclarationSQL()
831831
$this->_platform->getBinaryTypeDeclarationSQL(array());
832832
}
833833

834-
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL()
835-
{
836-
$this->markTestSkipped('Not applicable to the platform');
837-
}
838-
839834
/**
840835
* @group DBAL-553
841836
*/

tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -978,23 +978,19 @@ protected function getBinaryMaxLength()
978978

979979
public function testReturnsBinaryTypeDeclarationSQL()
980980
{
981-
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
982-
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
983-
self::assertSame('VARBINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 8000)));
984-
985-
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
986-
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
987-
self::assertSame('BINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8000)));
988-
}
989-
990-
/**
991-
* @group legacy
992-
* @expectedDeprecation Binary field length 8001 is greater than supported by the platform (8000). Reduce the field length or use a BLOB field instead.
993-
*/
994-
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL()
995-
{
996-
self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 8001]));
997-
self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 8001]));
981+
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL([]));
982+
self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 0]));
983+
self::assertSame('VARBINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 8000]));
984+
985+
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
986+
self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL([
987+
'fixed' => true,
988+
'length' => 0,
989+
]));
990+
self::assertSame('BINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL([
991+
'fixed' => true,
992+
'length' => 8000,
993+
]));
998994
}
999995

1000996
/**

tests/Doctrine/Tests/DBAL/Platforms/DB2PlatformTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,6 @@ public function testReturnsBinaryTypeDeclarationSQL()
427427
self::assertSame('CHAR(254) FOR BIT DATA', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 0]));
428428
}
429429

430-
/**
431-
* @group legacy
432-
* @expectedDeprecation Binary field length 32705 is greater than supported by the platform (32704). Reduce the field length or use a BLOB field instead.
433-
*/
434-
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL()
435-
{
436-
self::assertSame('BLOB(1M)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 32705]));
437-
self::assertSame('BLOB(1M)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 32705]));
438-
}
439-
440430
/**
441431
* @group DBAL-234
442432
*/

tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -443,23 +443,19 @@ protected function getBinaryMaxLength()
443443

444444
public function testReturnsBinaryTypeDeclarationSQL()
445445
{
446-
self::assertSame('RAW(255)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
447-
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
448-
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 2000)));
449-
450-
self::assertSame('RAW(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
451-
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
452-
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 2000)));
453-
}
454-
455-
/**
456-
* @group legacy
457-
* @expectedDeprecation Binary field length 2001 is greater than supported by the platform (2000). Reduce the field length or use a BLOB field instead.
458-
*/
459-
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL()
460-
{
461-
self::assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 2001]));
462-
self::assertSame('BLOB', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 2001]));
446+
self::assertSame('RAW(255)', $this->_platform->getBinaryTypeDeclarationSQL([]));
447+
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 0]));
448+
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 2000]));
449+
450+
self::assertSame('RAW(255)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
451+
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL([
452+
'fixed' => true,
453+
'length' => 0,
454+
]));
455+
self::assertSame('RAW(2000)', $this->_platform->getBinaryTypeDeclarationSQL([
456+
'fixed' => true,
457+
'length' => 2000,
458+
]));
463459
}
464460

465461
public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType()

tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -935,23 +935,19 @@ protected function getBinaryMaxLength()
935935

936936
public function testReturnsBinaryTypeDeclarationSQL()
937937
{
938-
self::assertSame('VARBINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
939-
self::assertSame('VARBINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
940-
self::assertSame('VARBINARY(32767)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 32767)));
938+
self::assertSame('VARBINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL([]));
939+
self::assertSame('VARBINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 0]));
940+
self::assertSame('VARBINARY(32767)', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 32767]));
941941

942-
self::assertSame('BINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
943-
self::assertSame('BINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
944-
self::assertSame('BINARY(32767)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 32767)));
945-
}
946-
947-
/**
948-
* @group legacy
949-
* @expectedDeprecation Binary field length 32768 is greater than supported by the platform (32767). Reduce the field length or use a BLOB field instead.
950-
*/
951-
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL()
952-
{
953-
self::assertSame('LONG BINARY', $this->_platform->getBinaryTypeDeclarationSQL(['length' => 32768]));
954-
self::assertSame('LONG BINARY', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 32768]));
942+
self::assertSame('BINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
943+
self::assertSame('BINARY(1)', $this->_platform->getBinaryTypeDeclarationSQL([
944+
'fixed' => true,
945+
'length' => 0,
946+
]));
947+
self::assertSame('BINARY(32767)', $this->_platform->getBinaryTypeDeclarationSQL([
948+
'fixed' => true,
949+
'length' => 32767,
950+
]));
955951
}
956952

957953
/**

tests/Doctrine/Tests/DBAL/Types/BinaryTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use Doctrine\DBAL\ParameterType;
66
use Doctrine\DBAL\Types\Type;
77
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
8+
use function array_map;
89
use function base64_encode;
910
use function fopen;
10-
use function stream_get_contents;
11+
use function implode;
12+
use function range;
1113

1214
class BinaryTest extends \Doctrine\Tests\DbalTestCase
1315
{
@@ -52,19 +54,26 @@ public function testBinaryNullConvertsToPHPValue()
5254

5355
public function testBinaryStringConvertsToPHPValue()
5456
{
55-
$databaseValue = 'binary string';
57+
$databaseValue = $this->getBinaryString();
5658
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
5759

58-
self::assertInternalType('resource', $phpValue);
59-
self::assertEquals($databaseValue, stream_get_contents($phpValue));
60+
self::assertSame($databaseValue, $phpValue);
6061
}
6162

6263
public function testBinaryResourceConvertsToPHPValue()
6364
{
6465
$databaseValue = fopen('data://text/plain;base64,' . base64_encode('binary string'), 'r');
6566
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
6667

67-
self::assertSame($databaseValue, $phpValue);
68+
self::assertSame('binary string', $phpValue);
69+
}
70+
71+
/**
72+
* Creates a binary string containing all possible byte values.
73+
*/
74+
private function getBinaryString() : string
75+
{
76+
return implode(array_map('chr', range(0, 255)));
6877
}
6978

7079
/**

tests/Doctrine/Tests/DBAL/Types/BlobTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,4 @@ public function testBlobNullConvertsToPHPValue()
3434
{
3535
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
3636
}
37-
38-
public function testBinaryStringConvertsToPHPValue()
39-
{
40-
$databaseValue = $this->getBinaryString();
41-
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
42-
43-
self::assertInternalType('resource', $phpValue);
44-
self::assertSame($databaseValue, stream_get_contents($phpValue));
45-
}
46-
47-
public function testBinaryResourceConvertsToPHPValue()
48-
{
49-
$databaseValue = fopen('data://text/plain;base64,' . base64_encode($this->getBinaryString()), 'r');
50-
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
51-
52-
self::assertSame($databaseValue, $phpValue);
53-
}
54-
55-
/**
56-
* Creates a binary string containing all possible byte values.
57-
*
58-
* @return string
59-
*/
60-
private function getBinaryString()
61-
{
62-
$string = '';
63-
64-
for ($i = 0; $i < 256; $i++) {
65-
$string .= chr($i);
66-
}
67-
68-
return $string;
69-
}
7037
}

0 commit comments

Comments
 (0)