Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate column type constants #279

Merged
merged 2 commits into from
Sep 13, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Enh #275: Refactor PHP type of `ColumnSchemaInterface` instances (@Tigrov)
- Enh #277: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
- Enh #276: Implement `ColumnFactory` class (@Tigrov)
- Enh #279: Separate column type constants (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
2 changes: 1 addition & 1 deletion src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* For example, the following code creates a column schema for an integer column:
*
* ```php
* $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
* $column = (new Column(ColumnType::INTEGER))->notNull()->defaultValue(0);
* ```
*
* Provides a fluent interface, which means that the methods can be chained together to create a column schema with
Expand Down
54 changes: 27 additions & 27 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Yiisoft\Db\Oracle\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\SchemaInterface;

use function preg_replace;
use function strtolower;
Expand All @@ -23,28 +23,28 @@ final class ColumnFactory extends AbstractColumnFactory
* @psalm-suppress MissingClassConstType
*/
private const TYPE_MAP = [
'char' => SchemaInterface::TYPE_CHAR,
'nchar' => SchemaInterface::TYPE_CHAR,
'varchar2' => SchemaInterface::TYPE_STRING,
'nvarchar2' => SchemaInterface::TYPE_STRING,
'clob' => SchemaInterface::TYPE_TEXT,
'nclob' => SchemaInterface::TYPE_TEXT,
'blob' => SchemaInterface::TYPE_BINARY,
'bfile' => SchemaInterface::TYPE_BINARY,
'long raw' => SchemaInterface::TYPE_BINARY,
'raw' => SchemaInterface::TYPE_BINARY,
'number' => SchemaInterface::TYPE_DECIMAL,
'binary_float' => SchemaInterface::TYPE_FLOAT, // 32 bit
'binary_double' => SchemaInterface::TYPE_DOUBLE, // 64 bit
'float' => SchemaInterface::TYPE_DOUBLE, // 126 bit
'date' => SchemaInterface::TYPE_DATE,
'interval day to second' => SchemaInterface::TYPE_TIME,
'timestamp' => SchemaInterface::TYPE_TIMESTAMP,
'timestamp with time zone' => SchemaInterface::TYPE_TIMESTAMP,
'timestamp with local time zone' => SchemaInterface::TYPE_TIMESTAMP,
'char' => ColumnType::CHAR,
'nchar' => ColumnType::CHAR,
'varchar2' => ColumnType::STRING,
'nvarchar2' => ColumnType::STRING,
'clob' => ColumnType::TEXT,
'nclob' => ColumnType::TEXT,
'blob' => ColumnType::BINARY,
'bfile' => ColumnType::BINARY,
'long raw' => ColumnType::BINARY,
'raw' => ColumnType::BINARY,
'number' => ColumnType::DECIMAL,
'binary_float' => ColumnType::FLOAT, // 32 bit
'binary_double' => ColumnType::DOUBLE, // 64 bit
'float' => ColumnType::DOUBLE, // 126 bit
'date' => ColumnType::DATE,
'interval day to second' => ColumnType::TIME,
'timestamp' => ColumnType::TIMESTAMP,
'timestamp with time zone' => ColumnType::TIMESTAMP,
'timestamp with local time zone' => ColumnType::TIMESTAMP,

/** Deprecated */
'long' => SchemaInterface::TYPE_TEXT,
'long' => ColumnType::TEXT,
];

protected function getType(string $dbType, array $info = []): string
Expand All @@ -55,24 +55,24 @@ protected function getType(string $dbType, array $info = []): string
$scale = isset($info['scale']) ? (int) $info['scale'] : null;

return match ($scale) {
null => SchemaInterface::TYPE_DOUBLE,
0 => SchemaInterface::TYPE_INTEGER,
default => SchemaInterface::TYPE_DECIMAL,
null => ColumnType::DOUBLE,
0 => ColumnType::INTEGER,
default => ColumnType::DECIMAL,
};
}

$dbType = preg_replace('/\([^)]+\)/', '', $dbType);

if ($dbType === 'interval day to second' && isset($info['precision']) && $info['precision'] > 0) {
return SchemaInterface::TYPE_STRING;
return ColumnType::STRING;
}

return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING;
return self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
}

public function fromType(string $type, array $info = []): ColumnSchemaInterface
{
if ($type === SchemaInterface::TYPE_BINARY) {
if ($type === ColumnType::BINARY) {
return (new BinaryColumnSchema($type))->load($info);
}

Expand Down
48 changes: 25 additions & 23 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Db\Oracle;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
Expand All @@ -17,29 +19,29 @@ final class QueryBuilder extends AbstractQueryBuilder
* @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values).
*/
protected array $typeMap = [
SchemaInterface::TYPE_PK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY',
SchemaInterface::TYPE_UPK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY',
SchemaInterface::TYPE_BIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY',
SchemaInterface::TYPE_UBIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY',
SchemaInterface::TYPE_CHAR => 'CHAR(1)',
SchemaInterface::TYPE_STRING => 'VARCHAR2(255)',
SchemaInterface::TYPE_TEXT => 'CLOB',
SchemaInterface::TYPE_TINYINT => 'NUMBER(3)',
SchemaInterface::TYPE_SMALLINT => 'NUMBER(5)',
SchemaInterface::TYPE_INTEGER => 'NUMBER(10)',
SchemaInterface::TYPE_BIGINT => 'NUMBER(20)',
SchemaInterface::TYPE_FLOAT => 'BINARY_FLOAT',
SchemaInterface::TYPE_DOUBLE => 'BINARY_DOUBLE',
SchemaInterface::TYPE_DECIMAL => 'NUMBER(10,0)',
SchemaInterface::TYPE_DATETIME => 'TIMESTAMP(0)',
SchemaInterface::TYPE_TIMESTAMP => 'TIMESTAMP(0)',
SchemaInterface::TYPE_TIME => 'INTERVAL DAY(0) TO SECOND(0)',
SchemaInterface::TYPE_DATE => 'DATE',
SchemaInterface::TYPE_BINARY => 'BLOB',
SchemaInterface::TYPE_BOOLEAN => 'NUMBER(1)',
SchemaInterface::TYPE_MONEY => 'NUMBER(19,4)',
SchemaInterface::TYPE_UUID => 'RAW(16)',
SchemaInterface::TYPE_UUID_PK => 'RAW(16) DEFAULT SYS_GUID() PRIMARY KEY',
PseudoType::PK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY',
PseudoType::UPK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY',
PseudoType::BIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY',
PseudoType::UBIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY',
ColumnType::CHAR => 'CHAR(1)',
ColumnType::STRING => 'VARCHAR2(255)',
ColumnType::TEXT => 'CLOB',
ColumnType::TINYINT => 'NUMBER(3)',
ColumnType::SMALLINT => 'NUMBER(5)',
ColumnType::INTEGER => 'NUMBER(10)',
ColumnType::BIGINT => 'NUMBER(20)',
ColumnType::FLOAT => 'BINARY_FLOAT',
ColumnType::DOUBLE => 'BINARY_DOUBLE',
ColumnType::DECIMAL => 'NUMBER(10,0)',
ColumnType::DATETIME => 'TIMESTAMP(0)',
ColumnType::TIMESTAMP => 'TIMESTAMP(0)',
ColumnType::TIME => 'INTERVAL DAY(0) TO SECOND(0)',
ColumnType::DATE => 'DATE',
ColumnType::BINARY => 'BLOB',
ColumnType::BOOLEAN => 'NUMBER(1)',
ColumnType::MONEY => 'NUMBER(19,4)',
ColumnType::UUID => 'RAW(16)',
PseudoType::UUID_PK => 'RAW(16) DEFAULT SYS_GUID() PRIMARY KEY',
];

public function __construct(QuoterInterface $quoter, SchemaInterface $schema)
Expand Down
3 changes: 2 additions & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Throwable;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
Expand Down Expand Up @@ -467,7 +468,7 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn
return null;
}

if ($column->getType() === self::TYPE_TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') {
if ($column->getType() === ColumnType::TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') {
return new Expression($defaultValue);
}

Expand Down
11 changes: 6 additions & 5 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use ReflectionException;
use Throwable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidCallException;
Expand All @@ -16,7 +18,6 @@
use Yiisoft\Db\Oracle\Driver;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Common\CommonCommandTest;
use Yiisoft\Db\Tests\Support\Assert;
use Yiisoft\Db\Tests\Support\DbHelper;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function testCLOBStringInsertion(): void
$command->dropTable('longstring')->execute();
}

$command->createTable('longstring', ['message' => SchemaInterface::TYPE_TEXT])->execute();
$command->createTable('longstring', ['message' => ColumnType::TEXT])->execute();
$longData = str_pad('-', 4001, '-=', STR_PAD_LEFT);
$command->insert('longstring', ['message' => $longData])->execute();

Expand Down Expand Up @@ -130,7 +131,7 @@ public function testCreateTable(): void

$command->createTable(
'{{testCreateTable}}',
['id' => SchemaInterface::TYPE_PK, 'bar' => SchemaInterface::TYPE_INTEGER]
['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER]
)->execute();
$command->setSql(
<<<SQL
Expand Down Expand Up @@ -182,8 +183,8 @@ public function testCreateView(): void
$command->createTable(
'{{testCreateViewTable}}',
[
'[[id]]' => SchemaInterface::TYPE_PK,
'[[bar]]' => SchemaInterface::TYPE_INTEGER,
'[[id]]' => PseudoType::PK,
'[[bar]]' => ColumnType::INTEGER,
],
)->execute();
$command->setSql(
Expand Down
41 changes: 21 additions & 20 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Oracle\Tests\Provider;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Oracle\Column\BinaryColumnSchema;
use Yiisoft\Db\Schema\Column\DoubleColumnSchema;
use Yiisoft\Db\Schema\Column\StringColumnSchema;
Expand All @@ -14,26 +15,26 @@ public static function dbTypes(): array
{
return [
// db type, expected abstract type, expected instance of
['char', 'char', StringColumnSchema::class],
['nchar', 'char', StringColumnSchema::class],
['varchar2', 'string', StringColumnSchema::class],
['nvarchar2', 'string', StringColumnSchema::class],
['clob', 'text', StringColumnSchema::class],
['nclob', 'text', StringColumnSchema::class],
['long', 'text', StringColumnSchema::class],
['blob', 'binary', BinaryColumnSchema::class],
['bfile', 'binary', BinaryColumnSchema::class],
['long raw', 'binary', BinaryColumnSchema::class],
['raw', 'binary', BinaryColumnSchema::class],
['number', 'double', DoubleColumnSchema::class],
['binary_float', 'float', DoubleColumnSchema::class],
['binary_double', 'double', DoubleColumnSchema::class],
['float', 'double', DoubleColumnSchema::class],
['date', 'date', StringColumnSchema::class],
['interval day(0) to second', 'time', StringColumnSchema::class],
['timestamp', 'timestamp', StringColumnSchema::class],
['timestamp with time zone', 'timestamp', StringColumnSchema::class],
['timestamp with local time zone', 'timestamp', StringColumnSchema::class],
['char', ColumnType::CHAR, StringColumnSchema::class],
['nchar', ColumnType::CHAR, StringColumnSchema::class],
['varchar2', ColumnType::STRING, StringColumnSchema::class],
['nvarchar2', ColumnType::STRING, StringColumnSchema::class],
['clob', ColumnType::TEXT, StringColumnSchema::class],
['nclob', ColumnType::TEXT, StringColumnSchema::class],
['long', ColumnType::TEXT, StringColumnSchema::class],
['blob', ColumnType::BINARY, BinaryColumnSchema::class],
['bfile', ColumnType::BINARY, BinaryColumnSchema::class],
['long raw', ColumnType::BINARY, BinaryColumnSchema::class],
['raw', ColumnType::BINARY, BinaryColumnSchema::class],
['number', ColumnType::DOUBLE, DoubleColumnSchema::class],
['binary_float', ColumnType::FLOAT, DoubleColumnSchema::class],
['binary_double', ColumnType::DOUBLE, DoubleColumnSchema::class],
['float', ColumnType::DOUBLE, DoubleColumnSchema::class],
['date', ColumnType::DATE, StringColumnSchema::class],
['interval day(0) to second', ColumnType::TIME, StringColumnSchema::class],
['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class],
['timestamp with time zone', ColumnType::TIMESTAMP, StringColumnSchema::class],
['timestamp with local time zone', ColumnType::TIMESTAMP, StringColumnSchema::class],
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/ColumnSchemaBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Db\Oracle\Tests\Provider;

use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Constant\ColumnType;

final class ColumnSchemaBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider
{
Expand All @@ -19,7 +19,7 @@ public static function types(): array

return [
...$types,
['integer UNSIGNED', SchemaInterface::TYPE_INTEGER, null, [['unsigned']]],
['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]],
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Oracle\Tests;

use Throwable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand All @@ -13,7 +14,6 @@
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest;

/**
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testAlterColumn(): void
<<<SQL
ALTER TABLE "customer" MODIFY "email" VARCHAR2(255)
SQL,
$qb->alterColumn('customer', 'email', SchemaInterface::TYPE_STRING),
$qb->alterColumn('customer', 'email', ColumnType::STRING),
);

$db->close();
Expand Down