3 namespace Wikimedia\Rdbms
;
7 class PostgresField
implements Field
{
8 private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname,
9 $has_default, $default;
12 * @param DatabasePostgres $db
13 * @param string $table
14 * @param string $field
15 * @return null|PostgresField
17 static function fromText( DatabasePostgres
$db, $table, $field ) {
20 attnotnull, attlen, conname AS conname,
23 COALESCE(condeferred, 'f') AS deferred,
24 COALESCE(condeferrable, 'f') AS deferrable,
25 CASE WHEN typname = 'int2' THEN 'smallint'
26 WHEN typname = 'int4' THEN 'integer'
27 WHEN typname = 'int8' THEN 'bigint'
28 WHEN typname = 'bpchar' THEN 'char'
29 ELSE typname END AS typname
31 JOIN pg_namespace n ON (n.oid = c.relnamespace)
32 JOIN pg_attribute a ON (a.attrelid = c.oid)
33 JOIN pg_type t ON (t.oid = a.atttypid)
34 LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
35 LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
42 $table = $db->remappedTableName( $table );
45 $db->addQuotes( $db->getCoreSchema() ),
46 $db->addQuotes( $table ),
47 $db->addQuotes( $field )
50 $row = $db->fetchObject( $res );
54 $n = new PostgresField
;
55 $n->type
= $row->typname
;
56 $n->nullable
= ( $row->attnotnull
== 'f' );
58 $n->tablename
= $table;
59 $n->max_length
= $row->attlen
;
60 $n->deferrable
= ( $row->deferrable
== 't' );
61 $n->deferred
= ( $row->deferred
== 't' );
62 $n->conname
= $row->conname
;
63 $n->has_default
= ( $row->atthasdef
=== 't' );
64 $n->default = $row->adsrc
;
73 function tableName() {
74 return $this->tablename
;
81 function isNullable() {
82 return $this->nullable
;
85 function maxLength() {
86 return $this->max_length
;
89 function is_deferrable() {
90 return $this->deferrable
;
93 function is_deferred() {
94 return $this->deferred
;
98 return $this->conname
;
105 function defaultValue() {
106 if ( $this->has_default
) {
107 return $this->default;