5
5
namespace Yiisoft \Db \Oracle \Column ;
6
6
7
7
use Yiisoft \Db \Constant \ColumnType ;
8
+ use Yiisoft \Db \Expression \Expression ;
8
9
use Yiisoft \Db \Schema \Column \AbstractColumnFactory ;
9
10
use Yiisoft \Db \Schema \Column \ColumnInterface ;
10
11
12
+ use function date_create_immutable ;
13
+ use function preg_match ;
11
14
use function rtrim ;
12
15
use function strcasecmp ;
13
16
14
17
final class ColumnFactory extends AbstractColumnFactory
15
18
{
19
+ private const DATETIME_REGEX = "/^(?:TIMESTAMP|DATE|INTERVAL|to_timestamp(?:_tz)?\(|to_date\(|to_dsinterval\()\s*'(?:\d )?([^']+)/ " ;
20
+
16
21
/**
17
22
* The mapping from physical column types (keys) to abstract column types (values).
18
23
*
@@ -39,9 +44,9 @@ final class ColumnFactory extends AbstractColumnFactory
39
44
'binary_double ' => ColumnType::DOUBLE , // 64 bit
40
45
'float ' => ColumnType::DOUBLE , // 126 bit
41
46
'date ' => ColumnType::DATE ,
42
- 'timestamp ' => ColumnType::TIMESTAMP ,
43
- 'timestamp with time zone ' => ColumnType::TIMESTAMP ,
44
- 'timestamp with local time zone ' => ColumnType::TIMESTAMP ,
47
+ 'timestamp ' => ColumnType::DATETIME ,
48
+ 'timestamp with time zone ' => ColumnType::DATETIMETZ ,
49
+ 'timestamp with local time zone ' => ColumnType::DATETIME ,
45
50
'interval day to second ' => ColumnType::STRING ,
46
51
'interval year to month ' => ColumnType::STRING ,
47
52
'json ' => ColumnType::JSON ,
@@ -91,13 +96,29 @@ protected function getColumnClass(string $type, array $info = []): string
91
96
return match ($ type ) {
92
97
ColumnType::BINARY => BinaryColumn::class,
93
98
ColumnType::BOOLEAN => BooleanColumn::class,
99
+ ColumnType::DATETIME => DateTimeColumn::class,
100
+ ColumnType::DATETIMETZ => DateTimeColumn::class,
101
+ ColumnType::TIME => DateTimeColumn::class,
102
+ ColumnType::TIMETZ => DateTimeColumn::class,
103
+ ColumnType::DATE => DateTimeColumn::class,
94
104
ColumnType::JSON => JsonColumn::class,
95
105
default => parent ::getColumnClass ($ type , $ info ),
96
106
};
97
107
}
98
108
99
109
protected function normalizeNotNullDefaultValue (string $ defaultValue , ColumnInterface $ column ): mixed
100
110
{
101
- return parent ::normalizeNotNullDefaultValue (rtrim ($ defaultValue ), $ column );
111
+ $ value = parent ::normalizeNotNullDefaultValue (rtrim ($ defaultValue ), $ column );
112
+
113
+ if ($ column instanceof DateTimeColumn
114
+ && $ value instanceof Expression
115
+ && preg_match (self ::DATETIME_REGEX , (string ) $ value , $ matches ) === 1
116
+ ) {
117
+ return date_create_immutable ($ matches [1 ]) !== false
118
+ ? $ column ->phpTypecast ($ matches [1 ])
119
+ : new Expression ($ matches [1 ]);
120
+ }
121
+
122
+ return $ value ;
102
123
}
103
124
}
0 commit comments