| 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 |
|
| 11 |
private $max_length; |
| 12 |
private bool $deferred; |
| 13 |
private bool $deferrable; |
| 14 |
private ?string $conname; |
| 15 |
private bool $has_default; |
| 16 |
|
| 17 |
private $default; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public static function fromText( DatabasePostgres $db, $table, $field ) { |
| 26 |
$q = <<<SQL |
| 27 |
SELECT |
| 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 |
| 38 |
FROM pg_class c |
| 39 |
JOIN pg_namespace n ON (n.oid = c.relnamespace) |
| 40 |
JOIN pg_attribute a ON (a.attrelid = c.oid) |
| 41 |
JOIN pg_type t ON (t.oid = a.atttypid) |
| 42 |
LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f') |
| 43 |
LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum |
| 44 |
WHERE relkind = 'r' |
| 45 |
AND nspname=%s |
| 46 |
AND relname=%s |
| 47 |
AND attname=%s; |
| 48 |
SQL; |
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function defaultValue() { |
| 118 |
if ( $this->has_default ) { |
| 119 |
return $this->default; |
| 120 |
} else { |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
} |