Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- New #355, #368, #370, #399: Implement `ColumnFactory` class (@Tigrov)
- Enh #359: Separate column type constants (@Tigrov)
- Enh #359: Remove `Schema::TYPE_ARRAY` and `Schema::TYPE_STRUCTURED` constants (@Tigrov)
- New #360: Realize `ColumnBuilder` class (@Tigrov)
- New #360, #460: Realize `ColumnBuilder` class (@Tigrov, @vjik)
- Enh #362: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #364, #372: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #365, #427: Refactor `Dsn` class (@Tigrov)
Expand Down Expand Up @@ -56,6 +56,7 @@
- Enh #444: Improve `ArrayExpressionBuilder` and `JsonExpressionBuilder` classes (@Tigrov)
- Chg #447: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov)
- Bug #456: Fix typecasting bit columns' values with big size (@Tigrov)
- Chg #460: Throw exception on "unsigned" column usage (@vjik)

## 1.3.0 March 21, 2024

Expand Down
6 changes: 4 additions & 2 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
return new IntegerColumn(ColumnType::INTEGER, size: $size);
}

public static function bigint(?int $size = null): IntegerColumn
public static function bigint(?int $size = null, bool $unsigned = false): BigIntColumn|IntegerColumn
{
return new IntegerColumn(ColumnType::BIGINT, size: $size);
return PHP_INT_SIZE === 4 || $unsigned

Check warning on line 42 in src/Column/ColumnBuilder.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": @@ @@ } public static function bigint(?int $size = null, bool $unsigned = false): BigIntColumn|IntegerColumn { - return PHP_INT_SIZE === 4 || $unsigned ? new BigIntColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned) : new IntegerColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned); + return PHP_INT_SIZE === 5 || $unsigned ? new BigIntColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned) : new IntegerColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned); } public static function binary(?int $size = null): BinaryColumn {

Check warning on line 42 in src/Column/ColumnBuilder.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": @@ @@ } public static function bigint(?int $size = null, bool $unsigned = false): BigIntColumn|IntegerColumn { - return PHP_INT_SIZE === 4 || $unsigned ? new BigIntColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned) : new IntegerColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned); + return PHP_INT_SIZE === 3 || $unsigned ? new BigIntColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned) : new IntegerColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned); } public static function binary(?int $size = null): BinaryColumn {
? new BigIntColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned)
: new IntegerColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned);
}

public static function binary(?int $size = null): BinaryColumn
Expand Down
5 changes: 5 additions & 0 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Pgsql\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder;
use Yiisoft\Db\Schema\Column\AbstractArrayColumn;
use Yiisoft\Db\Schema\Column\CollatableColumnInterface;
Expand Down Expand Up @@ -40,6 +41,10 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder

public function build(ColumnInterface $column): string
{
if ($column->isUnsigned()) {
throw new NotSupportedException('The "unsigned" attribute is not supported by PostgreSQL.');
}
Comment on lines +44 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just ignore this. Or as an option we can add CHECK (column >= 0)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsigned columns often usage for big values. Exception will alert the user that this is not available.


return $this->buildType($column)
. $this->buildNotNull($column)
. $this->buildPrimaryKey($column)
Expand Down
6 changes: 6 additions & 0 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
return $column;
}

public function fromPseudoType(string $pseudoType, array $info = []): ColumnInterface
{
// PostgreSQL doesn't support unsigned types
return parent::fromPseudoType($pseudoType, $info)->unsigned(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that this is correct. There are ways how to get unsigned type (with check)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsigned in most cases (almost always?) is added for store big values, not for non-negative values constraint.

}

protected function columnDefinitionParser(): ColumnDefinitionParser
{
return new ColumnDefinitionParser();
Expand Down Expand Up @@ -133,7 +139,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 142 in src/Column/ColumnFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "UnwrapSubstr": @@ @@ /** @var string $value */ $value = preg_replace("/::[^:']+\$/", '$1', $defaultValue); if (str_starts_with($value, "B'") && $value[-1] === "'") { - return $column->phpTypecast(substr($value, 2, -1)); + return $column->phpTypecast($value); } $value = parent::normalizeNotNullDefaultValue($value, $column); if ($value instanceof Expression) {

Check warning on line 142 in src/Column/ColumnFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": @@ @@ /** @var string $value */ $value = preg_replace("/::[^:']+\$/", '$1', $defaultValue); if (str_starts_with($value, "B'") && $value[-1] === "'") { - return $column->phpTypecast(substr($value, 2, -1)); + return $column->phpTypecast(substr($value, 1, -1)); } $value = parent::normalizeNotNullDefaultValue($value, $column); if ($value instanceof Expression) {
}

$value = parent::normalizeNotNullDefaultValue($value, $column);
Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public static function pseudoTypes(): array
{
$result = parent::pseudoTypes();
$result['pk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true);
$result['upk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true, unsigned: true);
$result['upk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true, unsigned: false);
$result['bigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true);
$result['ubigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true, unsigned: true);
$result['ubigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true, unsigned: false);

return $result;
}
Expand Down
7 changes: 6 additions & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ public static function buildColumnDefinition(): array
{
$values = parent::buildColumnDefinition();

// PostgreSQL does not support unsigned types
unset(
$values['bigint(15) unsigned'],
$values['unsigned()'],
);

$values[PseudoType::PK][0] = 'serial PRIMARY KEY';
$values[PseudoType::UPK][0] = 'serial PRIMARY KEY';
$values[PseudoType::BIGPK][0] = 'bigserial PRIMARY KEY';
Expand Down Expand Up @@ -443,7 +449,6 @@ public static function buildColumnDefinition(): array
$values['structured()'][0] = 'jsonb';
$values['json()'][0] = 'jsonb';
$values['json(100)'][0] = 'jsonb';
$values['unsigned()'][0] = 'integer';
$values['scale(2)'][0] = 'numeric(10,2)';
$values['integer(8)->scale(2)'][0] = 'integer';
$values["collation('collation_name')"] = [
Expand Down
Loading