-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathColumnFactory.php
86 lines (71 loc) · 2.59 KB
/
ColumnFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Oracle\Column;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use function preg_replace;
use function strtolower;
final class ColumnFactory extends AbstractColumnFactory
{
/**
* 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[]
*
* @psalm-suppress MissingClassConstType
*/
private const TYPE_MAP = [
'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' => ColumnType::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 => ColumnType::DOUBLE,
0 => ColumnType::INTEGER,
default => ColumnType::DECIMAL,
};
}
$dbType = preg_replace('/\([^)]+\)/', '', $dbType);
if ($dbType === 'interval day to second' && isset($info['size']) && $info['size'] > 0) {
return ColumnType::STRING;
}
return self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
}
public function fromType(string $type, array $info = []): ColumnSchemaInterface
{
if ($type === ColumnType::BINARY) {
return (new BinaryColumnSchema($type))->load($info);
}
return parent::fromType($type, $info);
}
protected function isDbType(string $dbType): bool
{
return isset(self::TYPE_MAP[$dbType]);
}
}