3 namespace Wikimedia\Rdbms
;
5 class PostgresField
implements Field
{
6 private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname,
7 $has_default, $default;
10 * @param DatabasePostgres $db
11 * @param string $table
12 * @param string $field
13 * @return null|PostgresField
15 static function fromText( DatabasePostgres
$db, $table, $field ) {
18 attnotnull, attlen, conname AS conname,
21 COALESCE(condeferred, 'f') AS deferred,
22 COALESCE(condeferrable, 'f') AS deferrable,
23 CASE WHEN typname = 'int2' THEN 'smallint'
24 WHEN typname = 'int4' THEN 'integer'
25 WHEN typname = 'int8' THEN 'bigint'
26 WHEN typname = 'bpchar' THEN 'char'
27 ELSE typname END AS typname
29 JOIN pg_namespace n ON (n.oid = c.relnamespace)
30 JOIN pg_attribute a ON (a.attrelid = c.oid)
31 JOIN pg_type t ON (t.oid = a.atttypid)
32 LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
33 LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
40 $table = $db->remappedTableName( $table );
43 $db->addQuotes( $db->getCoreSchema() ),
44 $db->addQuotes( $table ),
45 $db->addQuotes( $field )
48 $row = $db->fetchObject( $res );
52 $n = new PostgresField
;
53 $n->type
= $row->typname
;
54 $n->nullable
= ( $row->attnotnull
== 'f' );
56 $n->tablename
= $table;
57 $n->max_length
= $row->attlen
;
58 $n->deferrable
= ( $row->deferrable
== 't' );
59 $n->deferred
= ( $row->deferred
== 't' );
60 $n->conname
= $row->conname
;
61 $n->has_default
= ( $row->atthasdef
=== 't' );
62 $n->default = $row->adsrc
;
71 function tableName() {
72 return $this->tablename
;
79 function isNullable() {
80 return $this->nullable
;
83 function maxLength() {
84 return $this->max_length
;
87 function is_deferrable() {
88 return $this->deferrable
;
91 function is_deferred() {
92 return $this->deferred
;
96 return $this->conname
;
103 function defaultValue() {
104 if ( $this->has_default
) {
105 return $this->default;