-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Cast BIGINT
values to int if possible
#6177
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
Merged
Merged
Changes from all commits
Commits
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
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,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Tests\TestUtil; | ||
use Doctrine\DBAL\Types\Types; | ||
use Generator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Constraint\IsIdentical; | ||
use PHPUnit\Framework\Constraint\LogicalOr; | ||
|
||
use const PHP_INT_MAX; | ||
use const PHP_INT_MIN; | ||
use const PHP_INT_SIZE; | ||
|
||
class BigIntTypeTest extends FunctionalTestCase | ||
{ | ||
#[DataProvider('provideBigIntLiterals')] | ||
public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedValue): void | ||
{ | ||
$table = new Table('bigint_type_test'); | ||
$table->addColumn('id', Types::SMALLINT, ['notnull' => true]); | ||
$table->addColumn('my_integer', Types::BIGINT, ['notnull' => false]); | ||
$table->setPrimaryKey(['id']); | ||
$this->dropAndCreateTable($table); | ||
|
||
$this->connection->executeStatement(<<<SQL | ||
INSERT INTO bigint_type_test (id, my_integer) | ||
VALUES (42, $sqlLiteral) | ||
SQL); | ||
|
||
self::assertSame( | ||
$expectedValue, | ||
$this->connection->convertToPHPValue( | ||
$this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'), | ||
Types::BIGINT, | ||
), | ||
); | ||
} | ||
|
||
/** @return Generator<string, array{string, int|string|null}> */ | ||
public static function provideBigIntLiterals(): Generator | ||
{ | ||
yield 'zero' => ['0', 0]; | ||
yield 'null' => ['null', null]; | ||
yield 'positive number' => ['42', 42]; | ||
yield 'negative number' => ['-42', -42]; | ||
|
||
if (PHP_INT_SIZE < 8) { | ||
// The following tests only work on 64bit systems. | ||
return; | ||
} | ||
|
||
yield 'large positive number' => ['9223372036854775806', PHP_INT_MAX - 1]; | ||
yield 'large negative number' => ['-9223372036854775807', PHP_INT_MIN + 1]; | ||
} | ||
|
||
#[DataProvider('provideBigIntEdgeLiterals')] | ||
public function testSelectBigIntEdge(int $value): void | ||
{ | ||
$table = new Table('bigint_type_test'); | ||
$table->addColumn('id', Types::SMALLINT, ['notnull' => true]); | ||
$table->addColumn('my_integer', Types::BIGINT, ['notnull' => false]); | ||
$table->setPrimaryKey(['id']); | ||
$this->dropAndCreateTable($table); | ||
|
||
$this->connection->executeStatement(<<<SQL | ||
INSERT INTO bigint_type_test (id, my_integer) | ||
VALUES (42, $value) | ||
SQL); | ||
|
||
self::assertThat( | ||
$this->connection->convertToPHPValue( | ||
$this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'), | ||
Types::BIGINT, | ||
), | ||
LogicalOr::fromConstraints(new IsIdentical($value), new IsIdentical((string) $value)), | ||
); | ||
} | ||
|
||
/** @return Generator<string, array{int}> */ | ||
public static function provideBigIntEdgeLiterals(): Generator | ||
{ | ||
yield 'max int' => [PHP_INT_MAX]; | ||
yield 'min int' => [PHP_INT_MIN]; | ||
} | ||
|
||
public function testUnsignedBigIntOnMySQL(): void | ||
{ | ||
if (! TestUtil::isDriverOneOf('mysqli', 'pdo_mysql')) { | ||
self::markTestSkipped('This test only works on MySQL/MariaDB.'); | ||
} | ||
|
||
$table = new Table('bigint_type_test'); | ||
$table->addColumn('id', Types::SMALLINT, ['notnull' => true]); | ||
$table->addColumn('my_integer', Types::BIGINT, ['notnull' => false, 'unsigned' => true]); | ||
$table->setPrimaryKey(['id']); | ||
$this->dropAndCreateTable($table); | ||
|
||
// Insert (2 ** 64) - 1 | ||
$this->connection->executeStatement(<<<'SQL' | ||
INSERT INTO bigint_type_test (id, my_integer) | ||
VALUES (42, 0xFFFFFFFFFFFFFFFF) | ||
SQL); | ||
|
||
self::assertSame( | ||
'18446744073709551615', | ||
$this->connection->convertToPHPValue( | ||
$this->connection->fetchOne('SELECT my_integer from bigint_type_test WHERE id = 42'), | ||
Types::BIGINT, | ||
), | ||
); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.