55namespace Yiisoft \Db \Oracle \Column ;
66
77use Yiisoft \Db \Constant \ColumnType ;
8+ use Yiisoft \Db \Expression \Expression ;
89use Yiisoft \Db \Schema \Column \AbstractColumnFactory ;
910use Yiisoft \Db \Schema \Column \ColumnInterface ;
1011
12+ use function date_create_immutable ;
13+ use function preg_match ;
1114use function rtrim ;
1215use function strcasecmp ;
1316
1417final class ColumnFactory extends AbstractColumnFactory
1518{
19+ private const DATETIME_REGEX = "/^(?:TIMESTAMP|DATE|INTERVAL|to_timestamp(?:_tz)?\(|to_date\(|to_dsinterval\()\s*'(?:\d )?([^']+)/ " ;
20+
1621 /**
1722 * The mapping from physical column types (keys) to abstract column types (values).
1823 *
@@ -39,9 +44,9 @@ final class ColumnFactory extends AbstractColumnFactory
3944 'binary_double ' => ColumnType::DOUBLE , // 64 bit
4045 'float ' => ColumnType::DOUBLE , // 126 bit
4146 '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 ,
4550 'interval day to second ' => ColumnType::STRING ,
4651 'interval year to month ' => ColumnType::STRING ,
4752 'json ' => ColumnType::JSON ,
@@ -91,13 +96,29 @@ protected function getColumnClass(string $type, array $info = []): string
9196 return match ($ type ) {
9297 ColumnType::BINARY => BinaryColumn::class,
9398 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,
94104 ColumnType::JSON => JsonColumn::class,
95105 default => parent ::getColumnClass ($ type , $ info ),
96106 };
97107 }
98108
99109 protected function normalizeNotNullDefaultValue (string $ defaultValue , ColumnInterface $ column ): mixed
100110 {
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 ;
102123 }
103124}
0 commit comments