Skip to content

Commit e3025a4

Browse files
authored
Consider platform integer size in ColumnBuilder::bigint() + Throw exception on "unsigned" column usage (#460)
1 parent a38f6c1 commit e3025a4

6 files changed

Lines changed: 25 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- New #355, #368, #370, #399: Implement `ColumnFactory` class (@Tigrov)
1616
- Enh #359: Separate column type constants (@Tigrov)
1717
- Enh #359: Remove `Schema::TYPE_ARRAY` and `Schema::TYPE_STRUCTURED` constants (@Tigrov)
18-
- New #360: Realize `ColumnBuilder` class (@Tigrov)
18+
- New #360, #460: Realize `ColumnBuilder` class (@Tigrov, @vjik)
1919
- Enh #362: Update according changes in `ColumnSchemaInterface` (@Tigrov)
2020
- New #364, #372: Add `ColumnDefinitionBuilder` class (@Tigrov)
2121
- Enh #365, #427: Refactor `Dsn` class (@Tigrov)
@@ -56,6 +56,7 @@
5656
- Enh #444: Improve `ArrayExpressionBuilder` and `JsonExpressionBuilder` classes (@Tigrov)
5757
- Chg #447: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov)
5858
- Bug #456: Fix typecasting bit columns' values with big size (@Tigrov)
59+
- Chg #460: Throw exception on "unsigned" column usage (@vjik)
5960

6061
## 1.3.0 March 21, 2024
6162

src/Column/ColumnBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ public static function integer(?int $size = null): IntegerColumn
3737
return new IntegerColumn(ColumnType::INTEGER, size: $size);
3838
}
3939

40-
public static function bigint(?int $size = null): IntegerColumn
40+
public static function bigint(?int $size = null, bool $unsigned = false): BigIntColumn|IntegerColumn
4141
{
42-
return new IntegerColumn(ColumnType::BIGINT, size: $size);
42+
return PHP_INT_SIZE === 4 || $unsigned
43+
? new BigIntColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned)
44+
: new IntegerColumn(ColumnType::BIGINT, size: $size, unsigned: $unsigned);
4345
}
4446

4547
public static function binary(?int $size = null): BinaryColumn

src/Column/ColumnDefinitionBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Pgsql\Column;
66

77
use Yiisoft\Db\Constant\ColumnType;
8+
use Yiisoft\Db\Exception\NotSupportedException;
89
use Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder;
910
use Yiisoft\Db\Schema\Column\AbstractArrayColumn;
1011
use Yiisoft\Db\Schema\Column\CollatableColumnInterface;
@@ -40,6 +41,10 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
4041

4142
public function build(ColumnInterface $column): string
4243
{
44+
if ($column->isUnsigned()) {
45+
throw new NotSupportedException('The "unsigned" attribute is not supported by PostgreSQL.');
46+
}
47+
4348
return $this->buildType($column)
4449
. $this->buildNotNull($column)
4550
. $this->buildPrimaryKey($column)

src/Column/ColumnFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public function fromType(string $type, array $info = []): ColumnInterface
104104
return $column;
105105
}
106106

107+
public function fromPseudoType(string $pseudoType, array $info = []): ColumnInterface
108+
{
109+
// PostgreSQL doesn't support unsigned types
110+
return parent::fromPseudoType($pseudoType, $info)->unsigned(false);
111+
}
112+
107113
protected function columnDefinitionParser(): ColumnDefinitionParser
108114
{
109115
return new ColumnDefinitionParser();

tests/Provider/ColumnFactoryProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public static function pseudoTypes(): array
105105
{
106106
$result = parent::pseudoTypes();
107107
$result['pk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true);
108-
$result['upk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true, unsigned: true);
108+
$result['upk'][1] = new IntegerColumn(primaryKey: true, autoIncrement: true, unsigned: false);
109109
$result['bigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true);
110-
$result['ubigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true, unsigned: true);
110+
$result['ubigpk'][1] = new IntegerColumn(ColumnType::BIGINT, primaryKey: true, autoIncrement: true, unsigned: false);
111111

112112
return $result;
113113
}

tests/Provider/QueryBuilderProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ public static function buildColumnDefinition(): array
396396
{
397397
$values = parent::buildColumnDefinition();
398398

399+
// PostgreSQL does not support unsigned types
400+
unset(
401+
$values['bigint(15) unsigned'],
402+
$values['unsigned()'],
403+
);
404+
399405
$values[PseudoType::PK][0] = 'serial PRIMARY KEY';
400406
$values[PseudoType::UPK][0] = 'serial PRIMARY KEY';
401407
$values[PseudoType::BIGPK][0] = 'bigserial PRIMARY KEY';
@@ -443,7 +449,6 @@ public static function buildColumnDefinition(): array
443449
$values['structured()'][0] = 'jsonb';
444450
$values['json()'][0] = 'jsonb';
445451
$values['json(100)'][0] = 'jsonb';
446-
$values['unsigned()'][0] = 'integer';
447452
$values['scale(2)'][0] = 'numeric(10,2)';
448453
$values['integer(8)->scale(2)'][0] = 'integer';
449454
$values["collation('collation_name')"] = [

0 commit comments

Comments
 (0)