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