Skip to content

Commit

Permalink
Update according changes in ColumnSchemaInterface (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 1, 2024
1 parent 72da3f3 commit 3e1cbfb
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 155 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Enh #359: Separate column type constants (@Tigrov)
- Enh #359: Remove `Schema::TYPE_ARRAY` and `Schema::TYPE_STRUCTURED` constants (@Tigrov)
- Enh #360: Realize `ColumnBuilder` class (@Tigrov)
- Enh #362: Update according changes in `ColumnSchemaInterface` (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
1 change: 0 additions & 1 deletion src/Column/ArrayColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function getColumn(): ColumnSchemaInterface
if ($this->column === null) {
$this->column = (new ColumnFactory())->fromDbType($this->getDbType() ?? '');
$this->column->enumValues($this->getEnumValues());
$this->column->precision($this->getPrecision());
$this->column->scale($this->getScale());
$this->column->size($this->getSize());
}
Expand Down
7 changes: 5 additions & 2 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
namespace Yiisoft\Db\Pgsql\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

use const PHP_INT_SIZE;

/**
* @psalm-type ColumnInfo = array{
* allow_null?: bool|string|null,
* auto_increment?: bool|string,
* check?: string|null,
* columns?: array<string, ColumnSchemaInterface>,
* comment?: string|null,
* computed?: bool|string,
Expand All @@ -24,13 +25,15 @@
* extra?: string|null,
* primary_key?: bool|string,
* name?: string,
* precision?: int|string|null,
* not_null?: bool|string|null,
* reference?: ForeignKeyConstraint|null,
* sequence_name?: string|null,
* scale?: int|string|null,
* schema?: string|null,
* size?: int|string|null,
* table?: string|null,
* type?: string,
* unique?: bool|string,
* }
*/
final class ColumnFactory extends AbstractColumnFactory
Expand Down
63 changes: 33 additions & 30 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@
* type_scheme: string|null,
* character_maximum_length: int,
* column_comment: string|null,
* modifier: int,
* is_nullable: bool,
* column_default: string|null,
* is_autoinc: bool,
* sequence_name: string|null,
* enum_values: string|null,
* numeric_precision: int|null,
* numeric_scale: int|null,
* size: string|null,
* is_pkey: bool|null,
* size: int|null,
* scale: int|null,
* contype: string|null,
* dimension: int
* }
* @psalm-type ConstraintArray = array<
Expand Down Expand Up @@ -312,7 +310,7 @@ protected function loadTableIndexes(string $tableName): array
INNER JOIN "pg_attribute" AS "ia"
ON "ia"."attrelid" = "i"."indexrelid" AND "ia"."attnum" <= cardinality("i"."indoption")
WHERE "tcns"."nspname" = :schemaName AND "tc"."relname" = :tableName
ORDER BY "ia"."attnum" ASC
ORDER BY "i"."indkey", "ia"."attnum" ASC
SQL;

$resolvedName = $this->resolveTableName($tableName);
Expand Down Expand Up @@ -623,7 +621,6 @@ protected function findColumns(TableSchemaInterface $table): bool
(SELECT nspname FROM pg_namespace WHERE oid = COALESCE(td.typnamespace, tb.typnamespace, t.typnamespace)) AS type_scheme,
a.attlen AS character_maximum_length,
pg_catalog.col_description(c.oid, a.attnum) AS column_comment,
information_schema._pg_truetypmod(a, t) AS modifier,
NOT (a.attnotnull OR t.typnotnull) AS is_nullable,
COALESCE(t.typdefault, pg_get_expr(ad.adbin, ad.adrelid)) AS column_default,
COALESCE(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval', false) $orIdentity AS is_autoinc,
Expand All @@ -639,19 +636,25 @@ protected function findColumns(TableSchemaInterface $table): bool
',')
ELSE NULL
END AS enum_values,
information_schema._pg_numeric_precision(
COALESCE(td.oid, tb.oid, a.atttypid),
information_schema._pg_truetypmod(a, t)
) AS numeric_precision,
COALESCE(
information_schema._pg_char_max_length(
COALESCE(td.oid, tb.oid, a.atttypid),
a.atttypmod
),
information_schema._pg_datetime_precision(
COALESCE(td.oid, tb.oid, a.atttypid),
a.atttypmod
),
CASE a.atttypmod
WHEN -1 THEN null
ELSE ((a.atttypmod - 4) >> 16) & 65535
END
) AS size,
information_schema._pg_numeric_scale(
COALESCE(td.oid, tb.oid, a.atttypid),
information_schema._pg_truetypmod(a, t)
) AS numeric_scale,
information_schema._pg_char_max_length(
COALESCE(td.oid, tb.oid, a.atttypid),
information_schema._pg_truetypmod(a, t)
) AS size,
ct.oid IS NOT NULL AS is_pkey,
a.atttypmod
) AS scale,
ct.contype,
COALESCE(NULLIF(a.attndims, 0), NULLIF(t.typndims, 0), (t.typcategory='A')::int) AS dimension
FROM
pg_class c
Expand All @@ -663,11 +666,11 @@ protected function findColumns(TableSchemaInterface $table): bool
LEFT JOIN pg_type td ON t.typndims > 0 AND t.typbasetype > 0 AND tb.typelem = td.oid
LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
LEFT JOIN pg_rewrite rw ON c.relkind = 'v' AND rw.ev_class = c.oid AND rw.rulename = '_RETURN'
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p' AND a.attnum = ANY (ct.conkey)
OR rw.ev_action IS NOT NULL AND ct.contype = 'p'
AND (ARRAY(
LEFT JOIN pg_constraint ct ON (ct.contype = 'p' OR ct.contype = 'u' AND cardinality(ct.conkey) = 1)
AND (ct.conrelid = c.oid AND a.attnum = ANY (ct.conkey)
OR rw.ev_action IS NOT NULL AND (ARRAY(
SELECT regexp_matches(rw.ev_action, '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
))[a.attnum:a.attnum] <@ (ct.conrelid::text || ct.conkey::text[])
))[a.attnum:a.attnum] <@ (ct.conrelid::text || ct.conkey::text[]))
WHERE
a.attnum > 0 AND t.typname != '' AND NOT a.attisdropped
AND c.relname = :tableName
Expand Down Expand Up @@ -738,18 +741,19 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
->fromDbType($dbType, ['dimension' => $info['dimension']]);
}

/** @psalm-suppress DeprecatedMethod */
$column->name($info['column_name']);
$column->dbType($dbType);
$column->allowNull($info['is_nullable']);
$column->notNull(!$info['is_nullable']);
$column->autoIncrement($info['is_autoinc']);
$column->comment($info['column_comment']);
$column->enumValues($info['enum_values'] !== null
? explode(',', str_replace(["''"], ["'"], $info['enum_values']))
: null);
$column->primaryKey((bool) $info['is_pkey']);
$column->precision($info['numeric_precision']);
$column->scale($info['numeric_scale']);
$column->size($info['size'] === null ? null : (int) $info['size']);
$column->primaryKey($info['contype'] === 'p');
$column->unique($info['contype'] === 'u');
$column->scale($info['scale']);
$column->size($info['size']);

/**
* pg_get_serial_sequence() doesn't track DEFAULT value change.
Expand All @@ -771,9 +775,8 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
$arrayColumn = $column->getColumn();
$arrayColumn->dbType($dbType);
$arrayColumn->enumValues($column->getEnumValues());
$arrayColumn->precision($info['numeric_precision']);
$arrayColumn->scale($info['numeric_scale']);
$arrayColumn->size($info['size'] === null ? null : (int) $info['size']);
$arrayColumn->scale($info['scale']);
$arrayColumn->size($info['size']);
}

$column->defaultValue($this->normalizeDefaultValue($defaultValue, $column));
Expand Down
2 changes: 1 addition & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function buildCondition(): array
$buildCondition = parent::buildCondition();

$priceColumns = [
'value' => ColumnSchemaBuilder::numeric(name: 'value', precision: 10, scale: 2),
'value' => ColumnSchemaBuilder::numeric(name: 'value', size: 10, scale: 2),
'currency_code' => ColumnSchemaBuilder::char(name: 'currency_code', size: 3),
];

Expand Down
Loading

0 comments on commit 3e1cbfb

Please sign in to comment.