From 810047e1f184f8a4def372885591e4fbb6996b51 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 14 Dec 2020 09:41:00 -0600 Subject: [PATCH] formatting --- .../Database/DBAL/TimestampType.php | 82 +++++++++++-------- .../Database/DatabaseServiceProvider.php | 41 +++++----- 2 files changed, 67 insertions(+), 56 deletions(-) diff --git a/src/Illuminate/Database/DBAL/TimestampType.php b/src/Illuminate/Database/DBAL/TimestampType.php index dbc205f0ed09..0ab733cfe520 100644 --- a/src/Illuminate/Database/DBAL/TimestampType.php +++ b/src/Illuminate/Database/DBAL/TimestampType.php @@ -8,52 +8,42 @@ class TimestampType extends Type { - public function getName() - { - return 'timestamp'; - } - + /** + * {@inheritdoc} + */ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { $name = $platform->getName(); - // See https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html switch ($name) { - case 'mssql': - return $this->getMSSQLPlatformSQLDeclaration($fieldDeclaration); - case 'mysql': case 'mysql2': - return $this->getMySQLPlatformSQLDeclaration($fieldDeclaration); + return $this->getMySqlPlatformSQLDeclaration($fieldDeclaration); case 'postgresql': case 'pgsql': case 'postgres': - return $this->getPostgreSQLPlatformSQLDeclaration($fieldDeclaration); + return $this->getPostgresPlatformSQLDeclaration($fieldDeclaration); + + case 'mssql': + return $this->getSqlServerPlatformSQLDeclaration($fieldDeclaration); case 'sqlite': case 'sqlite3': - return $this->getSqlitePlatformSQLDeclaration($fieldDeclaration); + return $this->getSQLitePlatformSQLDeclaration($fieldDeclaration); default: throw new DBALException('Invalid platform: '.$name); } } - // https://docs.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15 - // timestamp in MSSQL is not a field for storing datetime data - protected function getMSSQLPlatformSQLDeclaration(array $fieldDeclaration) - { - $columnType = 'DATETIME'; - - if ($fieldDeclaration['precision']) { - $columnType = 'DATETIME2('.$fieldDeclaration['precision'].')'; - } - - return $columnType; - } - - protected function getMySQLPlatformSQLDeclaration(array $fieldDeclaration) + /** + * Get the SQL declaration for MySQL. + * + * @param array $fieldDeclaration + * @return string + */ + protected function getMySqlPlatformSQLDeclaration(array $fieldDeclaration) { $columnType = 'TIMESTAMP'; @@ -70,22 +60,46 @@ protected function getMySQLPlatformSQLDeclaration(array $fieldDeclaration) return $columnType; } - protected function getPostgreSQLPlatformSQLDeclaration(array $fieldDeclaration) + /** + * Get the SQL declaration for PostgreSQL. + * + * @param array $fieldDeclaration + * @return string + */ + protected function getPostgresPlatformSQLDeclaration(array $fieldDeclaration) { - $columnType = 'TIMESTAMP('.(int) $fieldDeclaration['precision'].')'; + return 'TIMESTAMP('.(int) $fieldDeclaration['precision'].')'; + } - return $columnType; + /** + * Get the SQL declaration for SQL Server. + * + * @param array $fieldDeclaration + * @return string + */ + protected function getSqlServerPlatformSQLDeclaration(array $fieldDeclaration) + { + return $fieldDeclaration['precision'] ?? false + ? 'DATETIME2('.$fieldDeclaration['precision'].')' + : 'DATETIME'; } /** - * Laravel creates timestamps as datetime in SQLite. + * Get the SQL declaration for SQLite. * - * SQLite does not store microseconds without custom hacks. + * @param array $fieldDeclaration + * @return string */ - protected function getSqlitePlatformSQLDeclaration(array $fieldDeclaration) + protected function getSQLitePlatformSQLDeclaration(array $fieldDeclaration) { - $columnType = 'DATETIME'; + return 'DATETIME'; + } - return $columnType; + /** + * {@inheritdoc} + */ + public function getName() + { + return 'timestamp'; } } diff --git a/src/Illuminate/Database/DatabaseServiceProvider.php b/src/Illuminate/Database/DatabaseServiceProvider.php index c16b00c85bc3..72f131d0db49 100755 --- a/src/Illuminate/Database/DatabaseServiceProvider.php +++ b/src/Illuminate/Database/DatabaseServiceProvider.php @@ -42,30 +42,9 @@ public function register() Model::clearBootedModels(); $this->registerConnectionServices(); - $this->registerEloquentFactory(); - $this->registerQueueableEntityResolver(); - - $this->registerDBALTypes(); - } - - /** - * Add any custom types to the DBAL library. - * - * @return void - */ - protected function registerDBALTypes() - { - if (! class_exists(Type::class)) { - return; - } - - $types = config('database.dbal.types', []); - - foreach ($types as $typeName => $typeClassDefinition) { - Type::addType($typeName, $typeClassDefinition); - } + $this->registerDoctrineTypes(); } /** @@ -129,4 +108,22 @@ protected function registerQueueableEntityResolver() return new QueueEntityResolver; }); } + + /** + * Register custom types with the Doctrine DBAL library. + * + * @return void + */ + protected function registerDoctrineTypes() + { + if (! class_exists(Type::class)) { + return; + } + + $types = $this->app['config']->get('database.dbal.types', []); + + foreach ($types as $name => $class) { + Type::addType($name, $class); + } + } }