4
4
5
5
use Doctrine \DBAL \Exception as DBALException ;
6
6
use Doctrine \DBAL \Platforms \AbstractPlatform ;
7
+ use Doctrine \DBAL \Platforms \MariaDb1027Platform ;
8
+ use Doctrine \DBAL \Platforms \MariaDBPlatform ;
9
+ use Doctrine \DBAL \Platforms \MySQL57Platform ;
10
+ use Doctrine \DBAL \Platforms \MySQL80Platform ;
11
+ use Doctrine \DBAL \Platforms \MySQLPlatform ;
12
+ use Doctrine \DBAL \Platforms \PostgreSQL100Platform ;
13
+ use Doctrine \DBAL \Platforms \PostgreSQL94Platform ;
14
+ use Doctrine \DBAL \Platforms \PostgreSQLPlatform ;
15
+ use Doctrine \DBAL \Platforms \SqlitePlatform ;
16
+ use Doctrine \DBAL \Platforms \SQLServer2012Platform ;
17
+ use Doctrine \DBAL \Platforms \SQLServerPlatform ;
7
18
use Doctrine \DBAL \Types \PhpDateTimeMappingType ;
8
19
use Doctrine \DBAL \Types \Type ;
9
20
10
21
class TimestampType extends Type implements PhpDateTimeMappingType
11
22
{
12
23
/**
13
24
* {@inheritdoc}
25
+ *
26
+ * @throws DBALException
14
27
*/
15
- public function getSQLDeclaration (array $ fieldDeclaration , AbstractPlatform $ platform )
28
+ public function getSQLDeclaration (array $ column , AbstractPlatform $ platform ): string
16
29
{
17
- return match ($ name = $ platform ->getName ()) {
18
- 'mysql ' ,
19
- 'mysql2 ' => $ this ->getMySqlPlatformSQLDeclaration ($ fieldDeclaration ),
20
- 'postgresql ' ,
21
- 'pgsql ' ,
22
- 'postgres ' => $ this ->getPostgresPlatformSQLDeclaration ($ fieldDeclaration ),
23
- 'mssql ' => $ this ->getSqlServerPlatformSQLDeclaration ($ fieldDeclaration ),
24
- 'sqlite ' ,
25
- 'sqlite3 ' => $ this ->getSQLitePlatformSQLDeclaration ($ fieldDeclaration ),
26
- default => throw new DBALException ('Invalid platform: ' .$ name ),
30
+ return match (get_class ($ platform )) {
31
+ MySQLPlatform::class,
32
+ MySQL57Platform::class,
33
+ MySQL80Platform::class,
34
+ MariaDBPlatform::class,
35
+ MariaDb1027Platform::class => $ this ->getMySqlPlatformSQLDeclaration ($ column ),
36
+ PostgreSQLPlatform::class,
37
+ PostgreSQL94Platform::class,
38
+ PostgreSQL100Platform::class => $ this ->getPostgresPlatformSQLDeclaration ($ column ),
39
+ SQLServerPlatform::class,
40
+ SQLServer2012Platform::class => $ this ->getSqlServerPlatformSQLDeclaration ($ column ),
41
+ SqlitePlatform::class => 'DATETIME ' ,
42
+ default => throw new DBALException ('Invalid platform: ' .substr (strrchr (get_class ($ platform ), '\\' ), 1 )),
27
43
};
28
44
}
29
45
30
46
/**
31
47
* Get the SQL declaration for MySQL.
32
48
*
33
- * @param array $fieldDeclaration
49
+ * @param array $column
34
50
* @return string
35
51
*/
36
- protected function getMySqlPlatformSQLDeclaration (array $ fieldDeclaration )
52
+ protected function getMySqlPlatformSQLDeclaration (array $ column ): string
37
53
{
38
54
$ columnType = 'TIMESTAMP ' ;
39
55
40
- if ($ fieldDeclaration ['precision ' ]) {
41
- $ columnType = 'TIMESTAMP( ' .$ fieldDeclaration ['precision ' ].') ' ;
56
+ if ($ column ['precision ' ]) {
57
+ $ columnType = 'TIMESTAMP( ' .min (( int ) $ column ['precision ' ], 6 ) .') ' ;
42
58
}
43
59
44
- $ notNull = $ fieldDeclaration ['notnull ' ] ?? false ;
60
+ $ notNull = $ column ['notnull ' ] ?? false ;
45
61
46
62
if (! $ notNull ) {
47
63
return $ columnType .' NULL ' ;
@@ -53,36 +69,25 @@ protected function getMySqlPlatformSQLDeclaration(array $fieldDeclaration)
53
69
/**
54
70
* Get the SQL declaration for PostgreSQL.
55
71
*
56
- * @param array $fieldDeclaration
72
+ * @param array $column
57
73
* @return string
58
74
*/
59
- protected function getPostgresPlatformSQLDeclaration (array $ fieldDeclaration )
75
+ protected function getPostgresPlatformSQLDeclaration (array $ column ): string
60
76
{
61
- return 'TIMESTAMP( ' .( int ) $ fieldDeclaration ['precision ' ].') ' ;
77
+ return 'TIMESTAMP( ' .min (( int ) $ column ['precision ' ], 6 ) .') ' ;
62
78
}
63
79
64
80
/**
65
81
* Get the SQL declaration for SQL Server.
66
82
*
67
- * @param array $fieldDeclaration
68
- * @return string
69
- */
70
- protected function getSqlServerPlatformSQLDeclaration (array $ fieldDeclaration )
71
- {
72
- return $ fieldDeclaration ['precision ' ] ?? false
73
- ? 'DATETIME2( ' .$ fieldDeclaration ['precision ' ].') '
74
- : 'DATETIME ' ;
75
- }
76
-
77
- /**
78
- * Get the SQL declaration for SQLite.
79
- *
80
- * @param array $fieldDeclaration
83
+ * @param array $column
81
84
* @return string
82
85
*/
83
- protected function getSQLitePlatformSQLDeclaration (array $ fieldDeclaration )
86
+ protected function getSqlServerPlatformSQLDeclaration (array $ column ): string
84
87
{
85
- return 'DATETIME ' ;
88
+ return $ column ['precision ' ] ?? false
89
+ ? 'DATETIME2( ' .min ((int ) $ column ['precision ' ], 7 ).') '
90
+ : 'DATETIME ' ;
86
91
}
87
92
88
93
/**
0 commit comments