MediaWiki REL1_30
PostgresField.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5class PostgresField implements Field {
8
15 static function fromText( DatabasePostgres $db, $table, $field ) {
16 $q = <<<SQL
17SELECT
18 attnotnull, attlen, conname AS conname,
19 atthasdef,
20 adsrc,
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
28FROM pg_class c
29JOIN pg_namespace n ON (n.oid = c.relnamespace)
30JOIN pg_attribute a ON (a.attrelid = c.oid)
31JOIN pg_type t ON (t.oid = a.atttypid)
32LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
33LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
34WHERE relkind = 'r'
35AND nspname=%s
36AND relname=%s
37AND attname=%s;
38SQL;
39
40 $table = $db->remappedTableName( $table );
41 $res = $db->query(
42 sprintf( $q,
43 $db->addQuotes( $db->getCoreSchema() ),
44 $db->addQuotes( $table ),
45 $db->addQuotes( $field )
46 )
47 );
48 $row = $db->fetchObject( $res );
49 if ( !$row ) {
50 return null;
51 }
52 $n = new PostgresField;
53 $n->type = $row->typname;
54 $n->nullable = ( $row->attnotnull == 'f' );
55 $n->name = $field;
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;
63
64 return $n;
65 }
66
67 function name() {
68 return $this->name;
69 }
70
71 function tableName() {
72 return $this->tablename;
73 }
74
75 function type() {
76 return $this->type;
77 }
78
79 function isNullable() {
80 return $this->nullable;
81 }
82
83 function maxLength() {
84 return $this->max_length;
85 }
86
87 function is_deferrable() {
88 return $this->deferrable;
89 }
90
91 function is_deferred() {
92 return $this->deferred;
93 }
94
95 function conname() {
96 return $this->conname;
97 }
98
103 function defaultValue() {
104 if ( $this->has_default ) {
105 return $this->default;
106 } else {
107 return false;
108 }
109 }
110}
type show c for details The hypothetical commands show w and show c should show the appropriate parts of the General Public License Of the commands you use may be called something other than show w and show c
Definition COPYING.txt:322
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to and or sell copies of the and to permit persons to whom the Software is furnished to do subject to the following WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR ARISING FROM
fetchObject( $res)
Fetch the next row from the given result object, in object form.
addQuotes( $s)
Adds quotes and backslashes.
getCoreSchema()
Return schema name for core application tables.
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition Database.php:888
tableName()
Name of table this field belongs to.
isNullable()
Whether this field can store NULL values.
static fromText(DatabasePostgres $db, $table, $field)
$res
Definition database.txt:21
Base for all database-specific classes representing information about database fields.
Definition Field.php:9