Skip to content

Commit

Permalink
Improve and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 19, 2025
1 parent 752bbcf commit bbb52b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
55 changes: 32 additions & 23 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,39 @@ protected function buildType(ColumnInterface $column): string

protected function getDbType(ColumnInterface $column): string
{
$dbType = $column->getDbType();

/** @psalm-suppress DocblockTypeContradiction */
return $column->getDbType() ?? match ($column->getType()) {
ColumnType::BOOLEAN => 'boolean',
ColumnType::BIT => 'varbit',
ColumnType::TINYINT => $column->isAutoIncrement() ? 'smallserial' : 'smallint',
ColumnType::SMALLINT => $column->isAutoIncrement() ? 'smallserial' : 'smallint',
ColumnType::INTEGER => $column->isAutoIncrement() ? 'serial' : 'integer',
ColumnType::BIGINT => $column->isAutoIncrement() ? 'bigserial' : 'bigint',
ColumnType::FLOAT => 'real',
ColumnType::DOUBLE => 'double precision',
ColumnType::DECIMAL => 'numeric',
ColumnType::MONEY => 'money',
ColumnType::CHAR => 'char',
ColumnType::STRING => 'varchar(' . ($column->getSize() ?? 255) . ')',
ColumnType::TEXT => 'text',
ColumnType::BINARY => 'bytea',
ColumnType::UUID => 'uuid',
ColumnType::DATETIME => 'timestamp',
ColumnType::TIMESTAMP => 'timestamp',
ColumnType::DATE => 'date',
ColumnType::TIME => 'time',
ColumnType::STRUCTURED => 'jsonb',
ColumnType::JSON => 'jsonb',
default => 'varchar',
return match ($dbType) {
default => $dbType,
null => match ($column->getType()) {
ColumnType::BOOLEAN => 'boolean',
ColumnType::BIT => 'varbit',
ColumnType::TINYINT => $column->isAutoIncrement() ? 'smallserial' : 'smallint',
ColumnType::SMALLINT => $column->isAutoIncrement() ? 'smallserial' : 'smallint',
ColumnType::INTEGER => $column->isAutoIncrement() ? 'serial' : 'integer',
ColumnType::BIGINT => $column->isAutoIncrement() ? 'bigserial' : 'bigint',
ColumnType::FLOAT => 'real',
ColumnType::DOUBLE => 'double precision',
ColumnType::DECIMAL => 'numeric',
ColumnType::MONEY => 'money',
ColumnType::CHAR => 'char',
ColumnType::STRING => 'varchar(' . ($column->getSize() ?? 255) . ')',
ColumnType::TEXT => 'text',
ColumnType::BINARY => 'bytea',
ColumnType::UUID => 'uuid',
ColumnType::DATETIME => 'timestamp',
ColumnType::TIMESTAMP => 'timestamp',
ColumnType::DATE => 'date',
ColumnType::TIME => 'time',
ColumnType::STRUCTURED => 'jsonb',
ColumnType::JSON => 'jsonb',
default => 'varchar',
},
'timestamp without time zone' => 'timestamp',
'timestamp with time zone' => 'timestamptz',
'time without time zone' => 'time',
'time with time zone' => 'timetz',
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/Column/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Pgsql\Column;

use function preg_match;
use function preg_replace;
use function strlen;
use function strtolower;
use function substr;
Expand All @@ -26,7 +27,7 @@ public function parse(string $definition): array
{
preg_match(self::TYPE_PATTERN, $definition, $matches);

$type = strtolower($matches[3] ?? $matches[1]);
$type = strtolower($matches[3] ?? preg_replace('/\s*\(\d+\)/', '', $matches[1]));
$info = ['type' => $type];

$typeDetails = $matches[4] ?? $matches[2] ?? '';
Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/ColumnDefinitionParserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public static function parse(): array
['character varying(126)', ['type' => 'character varying', 'size' => 126]],
['bit varying(8)', ['type' => 'bit varying', 'size' => 8]],
['timestamp without time zone', ['type' => 'timestamp without time zone']],
['timestamp(3) with time zone', ['type' => 'timestamp(3) with time zone', 'size' => 3]],
['timestamp(3) with time zone', ['type' => 'timestamp with time zone', 'size' => 3]],
['time without time zone', ['type' => 'time without time zone']],
['time (3) with time zone', ['type' => 'time (3) with time zone', 'size' => 3]],
['time (3) with time zone', ['type' => 'time with time zone', 'size' => 3]],
];
}
}
9 changes: 8 additions & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,14 @@ public static function buildColumnDefinition(): array
$values['uuidPrimaryKey()'][0] = "uuid PRIMARY KEY DEFAULT $uuidExpression";
}

return $values;
return [
...$values,
['character varying(255)', 'character varying(255)'],
['timestamp(5)', 'timestamp (5) without time zone'],
['timestamptz', 'timestamp with time zone'],
['time(3)', 'time(3) without time zone'],
['timetz(0)', 'time(0) with time zone'],
];
}

public static function prepareParam(): array
Expand Down

0 comments on commit bbb52b9

Please # to comment.