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;
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
23 public static function fromText( DatabasePostgres $db, $table, $field ) {
24 $q = <<<SQL
25SELECT
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
36FROM pg_class c
37JOIN pg_namespace n ON (n.oid = c.relnamespace)
38JOIN pg_attribute a ON (a.attrelid = c.oid)
39JOIN pg_type t ON (t.oid = a.atttypid)
40LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
41LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
42WHERE relkind = 'r'
43AND nspname=%s
44AND relname=%s
45AND attname=%s;
46SQL;
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
115 public function defaultValue() {
116 if ( $this->has_default ) {
117 return $this->default;
118 } else {
119 return false;
120 }
121 }
122}
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:622
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