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