-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Change BigIntType Casting #6143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,8 +7,11 @@ | |||||||||||||
use Doctrine\DBAL\ParameterType; | ||||||||||||||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||||||||||||||
|
||||||||||||||
use const PHP_INT_MAX; | ||||||||||||||
use const PHP_INT_MIN; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Type that maps a database BIGINT to a PHP string. | ||||||||||||||
* Type that maps a database BIGINT to a PHP int. | ||||||||||||||
*/ | ||||||||||||||
class BigIntType extends Type implements PhpIntegerMappingType | ||||||||||||||
{ | ||||||||||||||
|
@@ -28,12 +31,23 @@ public function getBindingType(): ParameterType | |||||||||||||
/** | ||||||||||||||
* @param T $value | ||||||||||||||
* | ||||||||||||||
* @return (T is null ? null : string) | ||||||||||||||
* @return (T is null ? null : int|string) | ||||||||||||||
* | ||||||||||||||
* @template T | ||||||||||||||
*/ | ||||||||||||||
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?string | ||||||||||||||
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): int|string|null | ||||||||||||||
{ | ||||||||||||||
return $value === null ? null : (string) $value; | ||||||||||||||
if ($value === null) { | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Let's exit here already if we already hold an integer in our hand. This prevents accidental int-to-string casts. This should also help us getting the tests to pass. |
||||||||||||||
|
||||||||||||||
if ( | ||||||||||||||
($value >= PHP_INT_MIN && $value < PHP_INT_MAX) || | ||||||||||||||
(string) $value === (string) PHP_INT_MAX | ||||||||||||||
) { | ||||||||||||||
return (int) $value; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return (string) $value; | ||||||||||||||
} | ||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional; | ||
|
||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\BigIntType; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
use const PHP_INT_MAX; | ||
use const PHP_INT_MIN; | ||
|
||
class BigIntConversionTest extends FunctionalTestCase | ||
{ | ||
private BigIntType $typeInstance; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->typeInstance = new BigIntType(); | ||
|
||
$table = new Table('bigint_conversion_test'); | ||
$table->addColumn('id', Types::SMALLINT, ['notnull' => true]); | ||
$table->addColumn('signed_integer', Types::BIGINT, ['notnull' => false]); | ||
$table->addColumn('unsigned_integer', Types::BIGINT, [ | ||
'notnull' => false, | ||
'unsigned' => true, | ||
]); | ||
$table->setPrimaryKey(['id']); | ||
$this->dropAndCreateTable($table); | ||
} | ||
|
||
public function testShouldConvertToZeroInteger(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 0, | ||
'signed_integer' => 0, | ||
]); | ||
$this->assertPHPValue( | ||
0, | ||
'SELECT signed_integer from bigint_conversion_test WHERE id = 0', | ||
); | ||
} | ||
|
||
public function testShouldConvertToPhpMinimumInteger(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 1, | ||
'signed_integer' => PHP_INT_MIN, | ||
]); | ||
$this->assertPHPValue( | ||
PHP_INT_MIN, | ||
'SELECT signed_integer from bigint_conversion_test WHERE id = 1', | ||
); | ||
} | ||
|
||
public function testShouldConvertToPhpMaximumInteger(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 2, | ||
'signed_integer' => PHP_INT_MAX, | ||
]); | ||
$this->assertPHPValue( | ||
PHP_INT_MAX, | ||
'SELECT signed_integer from bigint_conversion_test WHERE id = 2', | ||
); | ||
} | ||
|
||
public function testShouldConvertToPositiveIntegerNumber(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 3, | ||
'signed_integer' => PHP_INT_MAX - 1, | ||
]); | ||
$this->assertPHPValue( | ||
PHP_INT_MAX - 1, | ||
'SELECT signed_integer from bigint_conversion_test WHERE id = 3', | ||
); | ||
} | ||
|
||
public function testShouldConvertToNegativeIntegerNumber(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 4, | ||
'signed_integer' => PHP_INT_MIN + 1, | ||
]); | ||
$this->assertPHPValue( | ||
PHP_INT_MIN + 1, | ||
'SELECT signed_integer from bigint_conversion_test WHERE id = 4', | ||
); | ||
} | ||
|
||
public function testShouldConvertSlightlyOutOfPhpIntegerRangeUnsignedValueToString(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 5, | ||
'unsigned_integer' => '9223372036854775808', | ||
]); | ||
$this->assertPHPValue( | ||
'9223372036854775808', | ||
'SELECT unsigned_integer from bigint_conversion_test WHERE id = 5', | ||
); | ||
} | ||
|
||
public function testShouldConvertMaximumUnsignedIntegerValueToString(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 6, | ||
'unsigned_integer' => '18446744073709551615', | ||
]); | ||
$this->assertPHPValue( | ||
'18446744073709551615', | ||
'SELECT unsigned_integer from bigint_conversion_test WHERE id = 6', | ||
); | ||
} | ||
|
||
public function testShouldConvertNearlyMaximumUnsignedIntegerValueToString(): void | ||
{ | ||
$this->connection->insert('bigint_conversion_test', [ | ||
'id' => 7, | ||
'unsigned_integer' => '18446744073709551610', | ||
]); | ||
$this->assertPHPValue( | ||
'18446744073709551610', | ||
'SELECT unsigned_integer from bigint_conversion_test WHERE id = 7', | ||
); | ||
} | ||
|
||
private function assertPHPValue(mixed $expected, string $sql): void | ||
{ | ||
self::assertSame( | ||
$expected, | ||
$this->typeInstance->convertToPHPValue( | ||
$this->connection->fetchOne($sql), | ||
$this->connection->getDatabasePlatform(), | ||
), | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\BigIntType; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use const PHP_INT_MAX; | ||
use const PHP_INT_MIN; | ||
|
||
class BigIntTypeTest extends TestCase | ||
{ | ||
/** @var MockObject&AbstractPlatform */ | ||
private AbstractPlatform $platform; | ||
|
||
private BigIntType $type; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->platform = $this->createMock(AbstractPlatform::class); | ||
$this->type = new BigIntType(); | ||
} | ||
|
||
public function testShouldConvertPhpIntMinToInteger(): void | ||
{ | ||
self::assertSame( | ||
PHP_INT_MIN, | ||
$this->type->convertToPHPValue(PHP_INT_MIN, $this->platform), | ||
); | ||
} | ||
|
||
public function testShouldConvertPhpIntMaxToInteger(): void | ||
{ | ||
self::assertSame( | ||
PHP_INT_MAX, | ||
$this->type->convertToPHPValue(PHP_INT_MAX, $this->platform), | ||
); | ||
} | ||
|
||
public function testShouldConvertPhpIntMinAsStringToInteger(): void | ||
{ | ||
self::assertSame( | ||
PHP_INT_MIN, | ||
$this->type->convertToPHPValue( | ||
(string) PHP_INT_MIN, | ||
$this->platform, | ||
), | ||
); | ||
} | ||
|
||
public function testShouldConvertPhpIntMaxAsStringToInteger(): void | ||
{ | ||
self::assertSame( | ||
PHP_INT_MAX, | ||
$this->type->convertToPHPValue( | ||
(string) PHP_INT_MAX, | ||
$this->platform, | ||
), | ||
); | ||
} | ||
|
||
public function testShouldConvertZeroIntegerToInteger(): void | ||
{ | ||
self::assertSame(0, $this->type->convertToPHPValue(0, $this->platform)); | ||
} | ||
|
||
public function testShouldConvertZeroStringToInteger(): void | ||
{ | ||
self::assertSame( | ||
0, | ||
$this->type->convertToPHPValue('0', $this->platform), | ||
); | ||
} | ||
|
||
public function testShouldConvertSafeNegativeValueToInteger(): void | ||
{ | ||
self::assertSame( | ||
PHP_INT_MIN + 1, | ||
$this->type->convertToPHPValue(PHP_INT_MIN + 1, $this->platform), | ||
); | ||
} | ||
|
||
public function testShouldConvertSafePositiveValueToInteger(): void | ||
{ | ||
self::assertSame( | ||
PHP_INT_MAX - 1, | ||
$this->type->convertToPHPValue(PHP_INT_MAX - 1, $this->platform), | ||
); | ||
} | ||
|
||
public function testShouldConvertMaximumUnsignedIntegerValueToString(): void | ||
{ | ||
self::assertSame( | ||
'18446744073709551615', | ||
$this->type->convertToPHPValue( | ||
'18446744073709551615', | ||
$this->platform, | ||
), | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be worth mentioning that getting a string can only happen on PHP 32-bit architecture or for unsigned integers, as PHP 64-bit has an integer type that corresponds to the range of signed bigint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I had no idea on how to express this in the documentation.