MediaWiki master
PostgresField.php
Go to the documentation of this file.
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;
11 private $max_length;
12 private bool $deferred;
13 private bool $deferrable;
14 private ?string $conname;
15 private bool $has_default;
17 private $default;
18
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( $table ),
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
117 public function defaultValue() {
118 if ( $this->has_default ) {
119 return $this->default;
120 } else {
121 return false;
122 }
123 }
124}
Postgres database abstraction layer.
getCoreSchemas()
Return schema names for temporary tables and core application tables.
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
query( $sql, $fname=__METHOD__, $flags=0)
Run an SQL query statement and return the result.
Definition Database.php:626
tableName()
Name of table this field belongs to.
isNullable()
Whether this field can store NULL values.
static fromText(DatabasePostgres $db, $table, $field)
Base for all database-specific classes representing information about database fields.
Definition Field.php:9