-
-
Notifications
You must be signed in to change notification settings - Fork 17
Fix big bit column typecasting #456
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1544250
Fix big bit column typecasting
Tigrov 407e32f
Add line to CHANGELOG.md [skip ci]
Tigrov 7ec445d
Move code to `ColumnClassName` class
Tigrov 217c452
Move code to `BitColumnInternal` class
Tigrov 6c80d1f
Remove use ...;
Tigrov 9870d5b
Merge branch 'refs/heads/master' into fix-bigbit
Tigrov bac8ebd
Apply PHP CS Fixer and Rector changes (CI)
Tigrov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Pgsql\Column; | ||
|
|
||
| use Yiisoft\Db\Constant\ColumnType; | ||
| use Yiisoft\Db\Expression\ExpressionInterface; | ||
| use Yiisoft\Db\Schema\Column\AbstractColumn; | ||
|
|
||
| final class BigBitColumn extends AbstractColumn | ||
| { | ||
| protected const DEFAULT_TYPE = ColumnType::BIT; | ||
|
|
||
| public function dbTypecast(mixed $value): string|ExpressionInterface|null | ||
| { | ||
| return BitColumnInternal::dbTypecast($value, $this->getSize()); | ||
| } | ||
|
|
||
| public function phpTypecast(mixed $value): ?string | ||
| { | ||
| /** @var string|null $value */ | ||
| return $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
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,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Pgsql\Column; | ||
|
|
||
| use Yiisoft\Db\Expression\ExpressionInterface; | ||
|
|
||
| use function decbin; | ||
| use function gettype; | ||
| use function str_pad; | ||
|
|
||
| use const PHP_INT_SIZE; | ||
| use const STR_PAD_LEFT; | ||
|
|
||
| /** | ||
| * Helper utilities for `bit` and `varbit` column handling. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class BitColumnInternal | ||
| { | ||
| final private function __construct() {} | ||
|
|
||
| /** @psalm-return class-string<BitColumn|BigBitColumn> */ | ||
| public static function className(?int $size): string | ||
| { | ||
| return !empty($size) && ($size > 63 || PHP_INT_SIZE !== 8 && $size > 31) | ||
| ? BigBitColumn::class | ||
| : BitColumn::class; | ||
| } | ||
|
|
||
| /** @psalm-suppress RedundantCast */ | ||
| public static function dbTypecast(mixed $value, ?int $size): string|ExpressionInterface|null | ||
| { | ||
| return match (gettype($value)) { | ||
| 'integer', 'double' => self::addZero(decbin((int) $value), $size), | ||
| 'NULL' => null, | ||
| 'boolean' => self::addZero($value ? '1' : '0', $size), | ||
| 'string' => $value === '' ? null : $value, | ||
| default => $value instanceof ExpressionInterface ? $value : (string) $value, | ||
| }; | ||
| } | ||
|
|
||
| private static function addZero(string $value, ?int $size): string | ||
| { | ||
| return str_pad($value, (int) $size, '0', STR_PAD_LEFT); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -142,7 +142,7 @@ | |
| { | ||
| return match ($type) { | ||
| ColumnType::BOOLEAN => BooleanColumn::class, | ||
| ColumnType::BIT => BitColumn::class, | ||
| ColumnType::BIT => BitColumnInternal::className($info['size'] ?? null), | ||
| ColumnType::TINYINT => IntegerColumn::class, | ||
| ColumnType::SMALLINT => IntegerColumn::class, | ||
| ColumnType::INTEGER => IntegerColumn::class, | ||
|
|
@@ -162,7 +162,7 @@ | |
| $value = preg_replace("/::[^:']+$/", '$1', $defaultValue); | ||
|
|
||
| if (str_starts_with($value, "B'") && $value[-1] === "'") { | ||
| return $column->phpTypecast(substr($value, 2, -1)); | ||
|
Check warning on line 165 in src/Column/ColumnFactory.php
|
||
| } | ||
|
|
||
| $value = parent::normalizeNotNullDefaultValue($value, $column); | ||
|
|
||
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
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
Oops, something went wrong.
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.