-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Column; | ||
|
||
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; | ||
use Yiisoft\Db\Schema\SchemaInterface; | ||
|
||
class ColumnFactory extends \Yiisoft\Db\Schema\Column\ColumnFactory | ||
{ | ||
/** | ||
* The mapping from physical column types (keys) to abstract column types (values). | ||
* | ||
* @link https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/Data-Types.html | ||
* | ||
* @var string[] | ||
*/ | ||
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, | ||
|
||
/** Deprecated */ | ||
'long' => SchemaInterface::TYPE_TEXT, | ||
]; | ||
|
||
protected function getType(string $dbType, array $info = []): string | ||
{ | ||
$dbType = strtolower($dbType); | ||
|
||
if ($dbType === 'number') { | ||
$scale = isset($info['scale']) ? (int) $info['scale'] : null; | ||
|
||
return match ($scale) { | ||
null => SchemaInterface::TYPE_DOUBLE, | ||
0 => SchemaInterface::TYPE_INTEGER, | ||
default => SchemaInterface::TYPE_DECIMAL, | ||
}; | ||
} | ||
|
||
$dbType = preg_replace('/\([^)]+\)/', '', $dbType); | ||
|
||
if ($dbType === 'interval day to second' && isset($info['precision']) && $info['precision'] > 0) { | ||
return SchemaInterface::TYPE_STRING; | ||
} | ||
|
||
return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; | ||
} | ||
|
||
public function fromType(string $type, array $info = []): ColumnSchemaInterface | ||
{ | ||
if ($type === SchemaInterface::TYPE_BINARY) { | ||
return (new BinaryColumnSchema($type))->load($info); | ||
} | ||
|
||
return parent::fromType($type, $info); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Tests; | ||
|
||
use Yiisoft\Db\Oracle\Tests\Support\TestTrait; | ||
use Yiisoft\Db\Tests\AbstractColumnFactoryTest; | ||
|
||
/** | ||
* @group oracle | ||
*/ | ||
final class ColumnFactoryTest extends AbstractColumnFactoryTest | ||
{ | ||
use TestTrait; | ||
|
||
/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::dbTypes */ | ||
public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void | ||
{ | ||
parent::testFromDbType($dbType, $expectedType, $expectedInstanceOf); | ||
} | ||
|
||
/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::definitions */ | ||
public function testFromDefinition(string $definition, string $expectedType, string $expectedInstanceOf, array $expectedInfo = []): void | ||
{ | ||
parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedInfo); | ||
} | ||
|
||
/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::types */ | ||
public function testFromType(string $type, string $expectedType, string $expectedInstanceOf): void | ||
{ | ||
parent::testFromType($type, $expectedType, $expectedInstanceOf); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Oracle\Tests\Provider; | ||
|
||
use Yiisoft\Db\Oracle\Column\BinaryColumnSchema; | ||
use Yiisoft\Db\Schema\Column\DoubleColumnSchema; | ||
use Yiisoft\Db\Schema\Column\StringColumnSchema; | ||
|
||
final class ColumnFactoryProvider extends \Yiisoft\Db\Tests\Provider\ColumnFactoryProvider | ||
{ | ||
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], | ||
]; | ||
} | ||
|
||
public static function definitions(): array | ||
{ | ||
$definitions = parent::definitions(); | ||
|
||
$definitions['text'][0] = 'clob'; | ||
$definitions['text NOT NULL'][0] = 'clob NOT NULL'; | ||
$definitions['decimal(10,2)'][0] = 'number(10,2)'; | ||
|
||
unset($definitions['bigint UNSIGNED']); | ||
|
||
return $definitions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters