Skip to content

Commit 80b32d6

Browse files
authored
Separate column type constants (#359)
1 parent 2402d7d commit 80b32d6

14 files changed

+225
-221
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- Enh #354: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov)
1313
- Enh #356: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
1414
- Enh #355: Implement `ColumnFactory` class (@Tigrov)
15+
- Enh #359: Separate column type constants (@Tigrov)
16+
- Enh #359: Remove `Schema::TYPE_ARRAY` and `Schema::TYPE_STRUCTURED` constants (@Tigrov)
1517

1618
## 1.3.0 March 21, 2024
1719

src/Column.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* For example, the following code creates a column schema for an integer column:
1717
*
1818
* ```php
19-
* $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
19+
* $column = (new Column(ColumnType::INTEGER))->notNull()->defaultValue(0);
2020
* ```
2121
*
2222
* Provides a fluent interface, which means that the methods can be chained together to create a column schema with

src/Column/ArrayColumnSchema.php

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

77
use Traversable;
8+
use Yiisoft\Db\Constant\ColumnType;
89
use Yiisoft\Db\Constant\PhpType;
910
use Yiisoft\Db\Expression\ArrayExpression;
1011
use Yiisoft\Db\Expression\ExpressionInterface;
1112
use Yiisoft\Db\Pgsql\ArrayParser;
12-
use Yiisoft\Db\Pgsql\Schema;
1313
use Yiisoft\Db\Schema\Column\AbstractColumnSchema;
1414
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
15-
use Yiisoft\Db\Schema\SchemaInterface;
1615

1716
use function array_map;
1817
use function array_walk_recursive;
@@ -33,8 +32,11 @@ final class ArrayColumnSchema extends AbstractColumnSchema
3332
*/
3433
private int $dimension = 1;
3534

35+
/**
36+
* @psalm-param ColumnType::* $type
37+
*/
3638
public function __construct(
37-
string $type = Schema::TYPE_ARRAY,
39+
string $type = ColumnType::ARRAY,
3840
) {
3941
parent::__construct($type);
4042
}
@@ -112,13 +114,13 @@ public function phpTypecast(mixed $value): array|null
112114
return null;
113115
}
114116

115-
if ($this->getType() === SchemaInterface::TYPE_STRING) {
117+
if ($this->getType() === ColumnType::STRING) {
116118
return $value;
117119
}
118120

119121
$column = $this->getColumn();
120122

121-
if ($this->dimension === 1 && $column->getType() !== SchemaInterface::TYPE_JSON) {
123+
if ($this->dimension === 1 && $column->getType() !== ColumnType::JSON) {
122124
return array_map($column->phpTypecast(...), $value);
123125
}
124126

src/Column/ColumnFactory.php

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace Yiisoft\Db\Pgsql\Column;
66

7-
use Yiisoft\Db\Pgsql\Schema;
7+
use Yiisoft\Db\Constant\ColumnType;
88
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
99
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
10-
use Yiisoft\Db\Schema\SchemaInterface;
1110

1211
use const PHP_INT_SIZE;
1312

@@ -46,72 +45,73 @@ final class ColumnFactory extends AbstractColumnFactory
4645
* @psalm-suppress MissingClassConstType
4746
*/
4847
private const TYPE_MAP = [
49-
'bool' => SchemaInterface::TYPE_BOOLEAN,
50-
'boolean' => SchemaInterface::TYPE_BOOLEAN,
51-
'bit' => SchemaInterface::TYPE_BIT,
52-
'bit varying' => SchemaInterface::TYPE_BIT,
53-
'varbit' => SchemaInterface::TYPE_BIT,
54-
'smallint' => SchemaInterface::TYPE_SMALLINT,
55-
'int2' => SchemaInterface::TYPE_SMALLINT,
56-
'smallserial' => SchemaInterface::TYPE_SMALLINT,
57-
'serial2' => SchemaInterface::TYPE_SMALLINT,
58-
'int4' => SchemaInterface::TYPE_INTEGER,
59-
'int' => SchemaInterface::TYPE_INTEGER,
60-
'integer' => SchemaInterface::TYPE_INTEGER,
61-
'serial' => SchemaInterface::TYPE_INTEGER,
62-
'serial4' => SchemaInterface::TYPE_INTEGER,
63-
'bigint' => SchemaInterface::TYPE_BIGINT,
64-
'int8' => SchemaInterface::TYPE_BIGINT,
65-
'bigserial' => SchemaInterface::TYPE_BIGINT,
66-
'serial8' => SchemaInterface::TYPE_BIGINT,
67-
'oid' => SchemaInterface::TYPE_BIGINT, // shouldn't be used. it's pg internal!
68-
'pg_lsn' => SchemaInterface::TYPE_BIGINT,
69-
'real' => SchemaInterface::TYPE_FLOAT,
70-
'float4' => SchemaInterface::TYPE_FLOAT,
71-
'float8' => SchemaInterface::TYPE_DOUBLE,
72-
'double precision' => SchemaInterface::TYPE_DOUBLE,
73-
'decimal' => SchemaInterface::TYPE_DECIMAL,
74-
'numeric' => SchemaInterface::TYPE_DECIMAL,
75-
'money' => SchemaInterface::TYPE_MONEY,
76-
'char' => SchemaInterface::TYPE_CHAR,
77-
'character' => SchemaInterface::TYPE_CHAR,
78-
'bpchar' => SchemaInterface::TYPE_CHAR,
79-
'character varying' => SchemaInterface::TYPE_STRING,
80-
'varchar' => SchemaInterface::TYPE_STRING,
81-
'text' => SchemaInterface::TYPE_TEXT,
82-
'bytea' => SchemaInterface::TYPE_BINARY,
83-
'date' => SchemaInterface::TYPE_DATE,
84-
'time' => SchemaInterface::TYPE_TIME,
85-
'time without time zone' => SchemaInterface::TYPE_TIME,
86-
'time with time zone' => SchemaInterface::TYPE_TIME,
87-
'timetz' => SchemaInterface::TYPE_TIME,
88-
'timestamp' => SchemaInterface::TYPE_TIMESTAMP,
89-
'timestamp without time zone' => SchemaInterface::TYPE_TIMESTAMP,
90-
'timestamp with time zone' => SchemaInterface::TYPE_TIMESTAMP,
91-
'timestamptz' => SchemaInterface::TYPE_TIMESTAMP,
92-
'abstime' => SchemaInterface::TYPE_TIMESTAMP,
93-
'interval' => SchemaInterface::TYPE_STRING,
94-
'box' => SchemaInterface::TYPE_STRING,
95-
'circle' => SchemaInterface::TYPE_STRING,
96-
'point' => SchemaInterface::TYPE_STRING,
97-
'line' => SchemaInterface::TYPE_STRING,
98-
'lseg' => SchemaInterface::TYPE_STRING,
99-
'polygon' => SchemaInterface::TYPE_STRING,
100-
'path' => SchemaInterface::TYPE_STRING,
101-
'cidr' => SchemaInterface::TYPE_STRING,
102-
'inet' => SchemaInterface::TYPE_STRING,
103-
'macaddr' => SchemaInterface::TYPE_STRING,
104-
'tsquery' => SchemaInterface::TYPE_STRING,
105-
'tsvector' => SchemaInterface::TYPE_STRING,
106-
'txid_snapshot' => SchemaInterface::TYPE_STRING,
107-
'unknown' => SchemaInterface::TYPE_STRING,
108-
'uuid' => SchemaInterface::TYPE_STRING,
109-
'xml' => SchemaInterface::TYPE_STRING,
110-
'json' => SchemaInterface::TYPE_JSON,
111-
'jsonb' => SchemaInterface::TYPE_JSON,
48+
'bool' => ColumnType::BOOLEAN,
49+
'boolean' => ColumnType::BOOLEAN,
50+
'bit' => ColumnType::BIT,
51+
'bit varying' => ColumnType::BIT,
52+
'varbit' => ColumnType::BIT,
53+
'smallint' => ColumnType::SMALLINT,
54+
'int2' => ColumnType::SMALLINT,
55+
'smallserial' => ColumnType::SMALLINT,
56+
'serial2' => ColumnType::SMALLINT,
57+
'int4' => ColumnType::INTEGER,
58+
'int' => ColumnType::INTEGER,
59+
'integer' => ColumnType::INTEGER,
60+
'serial' => ColumnType::INTEGER,
61+
'serial4' => ColumnType::INTEGER,
62+
'bigint' => ColumnType::BIGINT,
63+
'int8' => ColumnType::BIGINT,
64+
'bigserial' => ColumnType::BIGINT,
65+
'serial8' => ColumnType::BIGINT,
66+
'oid' => ColumnType::BIGINT, // shouldn't be used. it's pg internal!
67+
'pg_lsn' => ColumnType::BIGINT,
68+
'real' => ColumnType::FLOAT,
69+
'float4' => ColumnType::FLOAT,
70+
'float8' => ColumnType::DOUBLE,
71+
'double precision' => ColumnType::DOUBLE,
72+
'decimal' => ColumnType::DECIMAL,
73+
'numeric' => ColumnType::DECIMAL,
74+
'money' => ColumnType::MONEY,
75+
'char' => ColumnType::CHAR,
76+
'character' => ColumnType::CHAR,
77+
'bpchar' => ColumnType::CHAR,
78+
'character varying' => ColumnType::STRING,
79+
'varchar' => ColumnType::STRING,
80+
'text' => ColumnType::TEXT,
81+
'bytea' => ColumnType::BINARY,
82+
'date' => ColumnType::DATE,
83+
'time' => ColumnType::TIME,
84+
'time without time zone' => ColumnType::TIME,
85+
'time with time zone' => ColumnType::TIME,
86+
'timetz' => ColumnType::TIME,
87+
'timestamp' => ColumnType::TIMESTAMP,
88+
'timestamp without time zone' => ColumnType::TIMESTAMP,
89+
'timestamp with time zone' => ColumnType::TIMESTAMP,
90+
'timestamptz' => ColumnType::TIMESTAMP,
91+
'abstime' => ColumnType::TIMESTAMP,
92+
'interval' => ColumnType::STRING,
93+
'box' => ColumnType::STRING,
94+
'circle' => ColumnType::STRING,
95+
'point' => ColumnType::STRING,
96+
'line' => ColumnType::STRING,
97+
'lseg' => ColumnType::STRING,
98+
'polygon' => ColumnType::STRING,
99+
'path' => ColumnType::STRING,
100+
'cidr' => ColumnType::STRING,
101+
'inet' => ColumnType::STRING,
102+
'macaddr' => ColumnType::STRING,
103+
'tsquery' => ColumnType::STRING,
104+
'tsvector' => ColumnType::STRING,
105+
'txid_snapshot' => ColumnType::STRING,
106+
'unknown' => ColumnType::STRING,
107+
'uuid' => ColumnType::STRING,
108+
'xml' => ColumnType::STRING,
109+
'json' => ColumnType::JSON,
110+
'jsonb' => ColumnType::JSON,
112111
];
113112

114113
/**
114+
* @psalm-param ColumnType::* $type
115115
* @psalm-param ColumnInfo $info
116116
* @psalm-suppress MoreSpecificImplementedParamType
117117
*/
@@ -125,17 +125,18 @@ public function fromType(string $type, array $info = []): ColumnSchemaInterface
125125
->dimension($dimension)
126126
->column($this->fromType($type, $info));
127127
} else {
128+
/** @psalm-suppress ArgumentTypeCoercion */
128129
$column = match ($type) {
129-
SchemaInterface::TYPE_BOOLEAN => new BooleanColumnSchema($type),
130-
SchemaInterface::TYPE_BIT => new BitColumnSchema($type),
131-
SchemaInterface::TYPE_TINYINT => new IntegerColumnSchema($type),
132-
SchemaInterface::TYPE_SMALLINT => new IntegerColumnSchema($type),
133-
SchemaInterface::TYPE_INTEGER => new IntegerColumnSchema($type),
134-
SchemaInterface::TYPE_BIGINT => PHP_INT_SIZE !== 8
130+
ColumnType::BOOLEAN => new BooleanColumnSchema($type),
131+
ColumnType::BIT => new BitColumnSchema($type),
132+
ColumnType::TINYINT => new IntegerColumnSchema($type),
133+
ColumnType::SMALLINT => new IntegerColumnSchema($type),
134+
ColumnType::INTEGER => new IntegerColumnSchema($type),
135+
ColumnType::BIGINT => PHP_INT_SIZE !== 8
135136
? new BigIntColumnSchema($type)
136137
: new IntegerColumnSchema($type),
137-
SchemaInterface::TYPE_BINARY => new BinaryColumnSchema($type),
138-
Schema::TYPE_STRUCTURED => (new StructuredColumnSchema($type))->columns($info['columns'] ?? []),
138+
ColumnType::BINARY => new BinaryColumnSchema($type),
139+
ColumnType::STRUCTURED => (new StructuredColumnSchema($type))->columns($info['columns'] ?? []),
139140
default => parent::fromType($type, $info),
140141
};
141142
}
@@ -145,6 +146,6 @@ public function fromType(string $type, array $info = []): ColumnSchemaInterface
145146

146147
protected function getType(string $dbType, array $info = []): string
147148
{
148-
return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING;
149+
return self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
149150
}
150151
}

src/Column/StructuredColumnSchema.php

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

77
use Traversable;
8+
use Yiisoft\Db\Constant\ColumnType;
89
use Yiisoft\Db\Constant\PhpType;
910
use Yiisoft\Db\Expression\ExpressionInterface;
10-
use Yiisoft\Db\Pgsql\Schema;
1111
use Yiisoft\Db\Pgsql\StructuredExpression;
1212
use Yiisoft\Db\Pgsql\StructuredParser;
1313
use Yiisoft\Db\Schema\Column\AbstractColumnSchema;
@@ -26,8 +26,11 @@ final class StructuredColumnSchema extends AbstractColumnSchema implements Struc
2626
*/
2727
private array $columns = [];
2828

29+
/**
30+
* @psalm-param ColumnType::* $type
31+
*/
2932
public function __construct(
30-
string $type = Schema::TYPE_STRUCTURED,
33+
string $type = ColumnType::STRUCTURED,
3134
) {
3235
parent::__construct($type);
3336
}

src/QueryBuilder.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yiisoft\Db\Pgsql;
66

7+
use Yiisoft\Db\Constant\ColumnType;
8+
use Yiisoft\Db\Constant\PseudoType;
79
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
810
use Yiisoft\Db\Schema\QuoterInterface;
911
use Yiisoft\Db\Schema\SchemaInterface;
@@ -19,30 +21,30 @@ final class QueryBuilder extends AbstractQueryBuilder
1921
* @psalm-var string[]
2022
*/
2123
protected array $typeMap = [
22-
SchemaInterface::TYPE_PK => 'serial NOT NULL PRIMARY KEY',
23-
SchemaInterface::TYPE_UPK => 'serial NOT NULL PRIMARY KEY',
24-
SchemaInterface::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY',
25-
SchemaInterface::TYPE_UBIGPK => 'bigserial NOT NULL PRIMARY KEY',
26-
SchemaInterface::TYPE_CHAR => 'char(1)',
27-
SchemaInterface::TYPE_STRING => 'varchar(255)',
28-
SchemaInterface::TYPE_TEXT => 'text',
29-
SchemaInterface::TYPE_TINYINT => 'smallint',
30-
SchemaInterface::TYPE_SMALLINT => 'smallint',
31-
SchemaInterface::TYPE_INTEGER => 'integer',
32-
SchemaInterface::TYPE_BIGINT => 'bigint',
33-
SchemaInterface::TYPE_FLOAT => 'double precision',
34-
SchemaInterface::TYPE_DOUBLE => 'double precision',
35-
SchemaInterface::TYPE_DECIMAL => 'numeric(10,0)',
36-
SchemaInterface::TYPE_DATETIME => 'timestamp(0)',
37-
SchemaInterface::TYPE_TIMESTAMP => 'timestamp(0)',
38-
SchemaInterface::TYPE_TIME => 'time(0)',
39-
SchemaInterface::TYPE_DATE => 'date',
40-
SchemaInterface::TYPE_BINARY => 'bytea',
41-
SchemaInterface::TYPE_BOOLEAN => 'boolean',
42-
SchemaInterface::TYPE_MONEY => 'numeric(19,4)',
43-
SchemaInterface::TYPE_JSON => 'jsonb',
44-
SchemaInterface::TYPE_UUID => 'uuid',
45-
SchemaInterface::TYPE_UUID_PK => 'uuid PRIMARY KEY',
24+
PseudoType::PK => 'serial NOT NULL PRIMARY KEY',
25+
PseudoType::UPK => 'serial NOT NULL PRIMARY KEY',
26+
PseudoType::BIGPK => 'bigserial NOT NULL PRIMARY KEY',
27+
PseudoType::UBIGPK => 'bigserial NOT NULL PRIMARY KEY',
28+
ColumnType::CHAR => 'char(1)',
29+
ColumnType::STRING => 'varchar(255)',
30+
ColumnType::TEXT => 'text',
31+
ColumnType::TINYINT => 'smallint',
32+
ColumnType::SMALLINT => 'smallint',
33+
ColumnType::INTEGER => 'integer',
34+
ColumnType::BIGINT => 'bigint',
35+
ColumnType::FLOAT => 'double precision',
36+
ColumnType::DOUBLE => 'double precision',
37+
ColumnType::DECIMAL => 'numeric(10,0)',
38+
ColumnType::DATETIME => 'timestamp(0)',
39+
ColumnType::TIMESTAMP => 'timestamp(0)',
40+
ColumnType::TIME => 'time(0)',
41+
ColumnType::DATE => 'date',
42+
ColumnType::BINARY => 'bytea',
43+
ColumnType::BOOLEAN => 'boolean',
44+
ColumnType::MONEY => 'numeric(19,4)',
45+
ColumnType::JSON => 'jsonb',
46+
ColumnType::UUID => 'uuid',
47+
PseudoType::UUID_PK => 'uuid PRIMARY KEY',
4648
];
4749

4850
public function __construct(QuoterInterface $quoter, SchemaInterface $schema)

src/Schema.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use JsonException;
88
use Throwable;
9+
use Yiisoft\Db\Constant\ColumnType;
910
use Yiisoft\Db\Constraint\CheckConstraint;
1011
use Yiisoft\Db\Constraint\Constraint;
1112
use Yiisoft\Db\Constraint\DefaultValueConstraint;
@@ -31,6 +32,7 @@
3132
use function array_unique;
3233
use function array_values;
3334
use function explode;
35+
use function in_array;
3436
use function is_string;
3537
use function preg_match;
3638
use function preg_replace;
@@ -92,15 +94,6 @@
9294
*/
9395
final class Schema extends AbstractPdoSchema
9496
{
95-
/**
96-
* Define the abstract column type as `array`.
97-
*/
98-
public const TYPE_ARRAY = 'array';
99-
/**
100-
* Define the abstract column type as `structured`.
101-
*/
102-
public const TYPE_STRUCTURED = 'structured';
103-
10497
/**
10598
* @var string|null The default schema used for the current session.
10699
*/
@@ -745,7 +738,7 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
745738
}
746739

747740
$column = $this->getColumnFactory()
748-
->fromType(self::TYPE_STRUCTURED, ['dimension' => $info['dimension'], 'columns' => $columns]);
741+
->fromType(ColumnType::STRUCTURED, ['dimension' => $info['dimension'], 'columns' => $columns]);
749742
} else {
750743
$column = $this->getColumnFactory()
751744
->fromDbType($dbType, ['dimension' => $info['dimension']]);
@@ -823,12 +816,12 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn
823816
return null;
824817
}
825818

826-
if ($column->getType() === self::TYPE_BOOLEAN && in_array($defaultValue, ['true', 'false'], true)) {
819+
if ($column->getType() === ColumnType::BOOLEAN && in_array($defaultValue, ['true', 'false'], true)) {
827820
return $defaultValue === 'true';
828821
}
829822

830823
if (
831-
in_array($column->getType(), [self::TYPE_TIMESTAMP, self::TYPE_DATE, self::TYPE_TIME], true)
824+
in_array($column->getType(), [ColumnType::TIMESTAMP, ColumnType::DATE, ColumnType::TIME], true)
832825
&& in_array(strtoupper($defaultValue), ['NOW()', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'], true)
833826
) {
834827
return new Expression($defaultValue);
@@ -837,7 +830,7 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn
837830
$value = preg_replace("/^B?['(](.*?)[)'](?:::[^:]+)?$/s", '$1', $defaultValue);
838831
$value = str_replace("''", "'", $value);
839832

840-
if ($column->getType() === self::TYPE_BINARY && str_starts_with($value, '\\x')) {
833+
if ($column->getType() === ColumnType::BINARY && str_starts_with($value, '\\x')) {
841834
return hex2bin(substr($value, 2));
842835
}
843836

0 commit comments

Comments
 (0)