MediaWiki  master
MWPostgreSqlPlatform.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
6 use Wikimedia\Timestamp\ConvertibleTimestamp;
7 
8 class MWPostgreSqlPlatform extends PostgreSQLPlatform {
16  public function getDefaultValueDeclarationSQL( $column ) {
17  $type = $column['type'];
18  $default = $column['default'] ?? null;
19 
20  if ( $type instanceof TimestampType && $default ) {
21  if ( isset( $column['allowInfinite'] ) &&
22  $column['allowInfinite'] &&
23  $default === 'infinity'
24  ) {
25  $pgTimestamp = $default;
26  } else {
27  $timestamp = new ConvertibleTimestamp( $default );
28  $pgTimestamp = $timestamp->getTimestamp( TS_POSTGRES );
29  }
30 
31  return " DEFAULT '$pgTimestamp' ";
32  }
33 
34  return parent::getDefaultValueDeclarationSQL( $column );
35  }
36 
41  protected function _getCreateTableSQL( $name, $columns, array $options = [] ) {
42  // phpcs:enable
43  $tableSql = parent::_getCreateTableSQL( $name, $columns, $options );
44  foreach ( $columns as $column ) {
45  if ( $column['type'] instanceof EnumType && $column['fixed'] ) {
46  // PostgreSQL does support ENUM datatype but they need to be
47  // created severally with CREATE TYPE command for each column
48  // as it's not possible to feed the values directly in the
49  // column declaration as it could be done in MySQL.
50  $typeSql = $column['type']->makeEnumTypeSql( $column, $this );
51  array_unshift( $tableSql, $typeSql );
52  }
53  }
54 
55  return $tableSql;
56  }
57 
61  public function getBlobTypeDeclarationSQL( array $column ) {
62  // MySQL goes with varbinary for collation reasons, but postgres can't
63  // properly understand BYTEA type and works just fine with TEXT type
64  // FIXME: This should be fixed at some point (T257755)
65  return 'TEXT';
66  }
67 
71  public function getBinaryTypeDeclarationSQL( array $column ) {
72  // MySQL goes with varbinary for collation reasons, but postgres can't
73  // properly understand BYTEA type and works just fine with TEXT type
74  // FIXME: This should be fixed at some point (T257755)
75  return 'TEXT';
76  }
77 
81  public function getFloatDeclarationSQL( array $column ) {
82  return 'FLOAT';
83  }
84 
88  public function getDateTimeTzTypeDeclarationSQL( array $column ) {
89  return 'TIMESTAMPTZ';
90  }
91 }
Custom handling for ENUM datatype.
Definition: EnumType.php:17
getDefaultValueDeclarationSQL( $column)
Handles Postgres unique timestamp format .
_getCreateTableSQL( $name, $columns, array $options=[])
phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
Handling timestamp edge cases in mediawiki.