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