Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1431029
PostgresField.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
PostgresField.php
View Options
<?php
namespace
Wikimedia\Rdbms
;
class
PostgresField
implements
Field
{
private
string
$name
;
private
string
$tablename
;
private
string
$type
;
private
bool
$nullable
;
/** @var int */
private
$max_length
;
private
bool
$deferred
;
private
bool
$deferrable
;
private
?
string
$conname
;
private
bool
$has_default
;
/** @var mixed */
private
$default
;
/**
* @param DatabasePostgres $db
* @param string $table
* @param string $field
* @return null|PostgresField
*/
public
static
function
fromText
(
DatabasePostgres
$db
,
$table
,
$field
)
{
$q
=
<<<SQL
SELECT
attnotnull, attlen, conname AS conname,
atthasdef,
pg_get_expr(adbin, adrelid) AS adsrc,
COALESCE(condeferred, FALSE) AS deferred,
COALESCE(condeferrable, FALSE) AS deferrable,
CASE WHEN typname = 'int2' THEN 'smallint'
WHEN typname = 'int4' THEN 'integer'
WHEN typname = 'int8' THEN 'bigint'
WHEN typname = 'bpchar' THEN 'char'
ELSE typname END AS typname
FROM pg_class c
JOIN pg_namespace n ON (n.oid = c.relnamespace)
JOIN pg_attribute a ON (a.attrelid = c.oid)
JOIN pg_type t ON (t.oid = a.atttypid)
LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
WHERE relkind = 'r'
AND nspname=%s
AND relname=%s
AND attname=%s;
SQL;
foreach
(
$db
->
getCoreSchemas
()
as
$schema
)
{
$res
=
$db
->
query
(
sprintf
(
$q
,
$db
->
addQuotes
(
$schema
),
$db
->
addQuotes
(
$db
->
tableName
(
$table
,
'raw'
)
),
$db
->
addQuotes
(
$field
)
),
__METHOD__
);
$row
=
$res
->
fetchObject
();
if
(
!
$row
)
{
continue
;
}
$n
=
new
PostgresField
;
$n
->
type
=
$row
->
typname
;
$n
->
nullable
=
!
$row
->
attnotnull
;
$n
->
name
=
$field
;
$n
->
tablename
=
$table
;
$n
->
max_length
=
$row
->
attlen
;
$n
->
deferrable
=
(
bool
)
$row
->
deferrable
;
$n
->
deferred
=
(
bool
)
$row
->
deferred
;
$n
->
conname
=
$row
->
conname
;
$n
->
has_default
=
(
bool
)
$row
->
atthasdef
;
$n
->
default
=
$row
->
adsrc
;
return
$n
;
}
return
null
;
}
/** @inheritDoc */
public
function
name
()
{
return
$this
->
name
;
}
/** @inheritDoc */
public
function
tableName
()
{
return
$this
->
tablename
;
}
/** @inheritDoc */
public
function
type
()
{
return
$this
->
type
;
}
/** @inheritDoc */
public
function
isNullable
()
{
return
$this
->
nullable
;
}
/**
* @return int
*/
public
function
maxLength
()
{
return
$this
->
max_length
;
}
/**
* @return bool
*/
public
function
is_deferrable
()
{
return
$this
->
deferrable
;
}
/**
* @return bool
*/
public
function
is_deferred
()
{
return
$this
->
deferred
;
}
/**
* @return string|null
*/
public
function
conname
()
{
return
$this
->
conname
;
}
/**
* @since 1.19
* @return mixed|false
*/
public
function
defaultValue
()
{
if
(
$this
->
has_default
)
{
return
$this
->
default
;
}
else
{
return
false
;
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 19:58 (3 h, 3 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
2f/8b/abda14474c502f0e606ada4a64ae
Default Alt Text
PostgresField.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment