MediaWiki REL1_39
EnumType.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use Doctrine\DBAL\Platforms\AbstractPlatform;
6use Doctrine\DBAL\Types\Type;
7
17class EnumType extends Type {
18 public const ENUM = 'mwenum';
19
28 public function getSQLDeclaration( array $column, AbstractPlatform $platform ) {
29 // SQLite does not support ENUM type
30 if ( $platform->getName() == 'sqlite' ) {
31 return 'TEXT';
32 }
33
34 // PostgreSQL does support but needs custom handling.
35 // This just returns a string name that references the
36 // actual ENUM which will be created by CREATE TYPE command
37 // If 'fixed' option is not passed, this field will use TEXT
38 if ( $platform->getName() == 'postgresql' ) {
39 if ( !$column['fixed'] ) {
40 return 'TEXT';
41 }
42
43 return strtoupper( $column['name'] . '_enum' );
44 }
45
46 if ( $platform->getName() == 'mysql' ) {
47 $enumValues = $this->formatValues( $column['enum_values'] );
48 return "ENUM( $enumValues )";
49 }
50 }
51
62 public function makeEnumTypeSql( $column, $platform ) {
63 if ( $platform->getName() !== 'postgresql' ) {
64 throw new \InvalidArgumentException(
65 __METHOD__ . ' can only be called on Postgres platform'
66 );
67 }
68
69 $enumName = strtoupper( $column['name'] . '_enum' );
70 $enumValues = $this->formatValues( $column['enum_values'] );
71 $typeSql = "\n\nCREATE TYPE $enumName AS ENUM( $enumValues )";
72
73 return $typeSql;
74 }
75
82 public function formatValues( $values ) {
83 $values = implode( "','", $values );
84 $enumValues = "'" . $values . "'";
85
86 return $enumValues;
87 }
88
89 public function getName() {
90 return self::ENUM;
91 }
92}
Custom handling for ENUM datatype.
Definition EnumType.php:17
makeEnumTypeSql( $column, $platform)
Gets the sql portion to create ENUM for Postgres table column.
Definition EnumType.php:62
getSQLDeclaration(array $column, AbstractPlatform $platform)
Gets the SQL declaration snippet for an ENUM column.
Definition EnumType.php:28
formatValues( $values)
Get the imploded values suitable for pushing directly into ENUM();.
Definition EnumType.php:82