]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/libs/rdbms/field/PostgresField.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / libs / rdbms / field / PostgresField.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 class PostgresField implements Field {
6         private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname,
7                 $has_default, $default;
8
9         /**
10          * @param DatabasePostgres $db
11          * @param string $table
12          * @param string $field
13          * @return null|PostgresField
14          */
15         static function fromText( DatabasePostgres $db, $table, $field ) {
16                 $q = <<<SQL
17 SELECT
18  attnotnull, attlen, conname AS conname,
19  atthasdef,
20  adsrc,
21  COALESCE(condeferred, 'f') AS deferred,
22  COALESCE(condeferrable, 'f') AS deferrable,
23  CASE WHEN typname = 'int2' THEN 'smallint'
24   WHEN typname = 'int4' THEN 'integer'
25   WHEN typname = 'int8' THEN 'bigint'
26   WHEN typname = 'bpchar' THEN 'char'
27  ELSE typname END AS typname
28 FROM pg_class c
29 JOIN pg_namespace n ON (n.oid = c.relnamespace)
30 JOIN pg_attribute a ON (a.attrelid = c.oid)
31 JOIN pg_type t ON (t.oid = a.atttypid)
32 LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
33 LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
34 WHERE relkind = 'r'
35 AND nspname=%s
36 AND relname=%s
37 AND attname=%s;
38 SQL;
39
40                 $table = $db->remappedTableName( $table );
41                 $res = $db->query(
42                         sprintf( $q,
43                                 $db->addQuotes( $db->getCoreSchema() ),
44                                 $db->addQuotes( $table ),
45                                 $db->addQuotes( $field )
46                         )
47                 );
48                 $row = $db->fetchObject( $res );
49                 if ( !$row ) {
50                         return null;
51                 }
52                 $n = new PostgresField;
53                 $n->type = $row->typname;
54                 $n->nullable = ( $row->attnotnull == 'f' );
55                 $n->name = $field;
56                 $n->tablename = $table;
57                 $n->max_length = $row->attlen;
58                 $n->deferrable = ( $row->deferrable == 't' );
59                 $n->deferred = ( $row->deferred == 't' );
60                 $n->conname = $row->conname;
61                 $n->has_default = ( $row->atthasdef === 't' );
62                 $n->default = $row->adsrc;
63
64                 return $n;
65         }
66
67         function name() {
68                 return $this->name;
69         }
70
71         function tableName() {
72                 return $this->tablename;
73         }
74
75         function type() {
76                 return $this->type;
77         }
78
79         function isNullable() {
80                 return $this->nullable;
81         }
82
83         function maxLength() {
84                 return $this->max_length;
85         }
86
87         function is_deferrable() {
88                 return $this->deferrable;
89         }
90
91         function is_deferred() {
92                 return $this->deferred;
93         }
94
95         function conname() {
96                 return $this->conname;
97         }
98
99         /**
100          * @since 1.19
101          * @return bool|mixed
102          */
103         function defaultValue() {
104                 if ( $this->has_default ) {
105                         return $this->default;
106                 } else {
107                         return false;
108                 }
109         }
110 }