Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 14, 2020
1 parent cff3705 commit 810047e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 56 deletions.
82 changes: 48 additions & 34 deletions src/Illuminate/Database/DBAL/TimestampType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';
}
}
41 changes: 19 additions & 22 deletions src/Illuminate/Database/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 810047e

Please sign in to comment.