PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB » Bug #5796

Add Schema support to _pgFieldFlags in pgsql driver

Details

Submitted2005-10-27 18:13 UTC
Fromskeemer+peardev at gmail dot com
Assignedaharvey
StatusClosed
PackageDB
PHP VersionIrrelevant
Roadmaps(Not assigned)

Comments

[2005-10-27 18:13 UTC] skeemer+peardev at gmail dot com

Description:
------------
This patch will make it so _pgFieldFlags will accurately
handle multiple schemas and the tablename form
"schema.table". This is related to:
Bug #4235 - closed, fix not in CSV like said
Bug #5405 - bogus, solution proposed does not fix problem,
tables with same name in multiple schemas will break it
Bug #305 - won't fix, possibly doesn't need implemented
across all DBMS's since they don't all use 'schema.table'

function _pgFieldFlags($resource, $num_field,
$table_name)
{
$field_name =3D @pg_fieldname($resource,
$num_field);

// <ADDED>
if( strpos($table_name, '.')=3D=3D=3Dfalse )
{
$names =3D explode('.',$table_name);
$schema_name =3D $names[0];
$table_name =3D $names[1];
} else {
$result =3D @pg_exec($this->connection, "SELECT
current_schema()
FROM category LIMIT 1");

//MISSING CHECK HERE, NOT SURE HOW TO HANDLE IT WITHIN PEAR

$row =3D @pg_fetch_row($result, 0);
$schema_name =3D $row[0];
}
// </ADDED>

// IN THE QUERIES I CHANGED
// tab.relname =3D typ.typnamespace
// TO tab.relname||tab.relnamespace =
// typ.typname||typ.typnamespace
// ADDED
// AND ns.nspname =3D '$schema_name'
// AND tab.relnamespace =3D ns.oid
$result =3D @pg_exec($this->connection, "SELECT
f.attnotnull, f.att=
hasdef
FROM pg_attribute f,
pg_class tab,
pg_type typ, pg_namespace ns
WHERE tab.relname||
tab.relnamespace =3D
typ.typname||typ.typnamespace
AND typ.typrelid =3D
f.attrelid
AND tab.relnamespace =3D
ns.oid
AND f.attname =3D
'$field_name'
AND tab.relname =3D
'$table_name'
AND ns.nspname =3D
'$schema_name'");
if (@pg_numrows($result) > 0) {
$row =3D @pg_fetch_row($result, 0);
$flags =3D ($row[0] =3D=3D 't') ? 'not_null ' :
'';

if ($row[1] =3D=3D 't') {
$result =3D @pg_exec($this->connection,
"SELECT a.adsrc
FROM pg_attribute f,
pg_class tab,
pg_type typ, pg_attrdef a
WHERE
tab.relname||tab.relnamespace =3D typ.typname||
typ.typnamespace
AND typ.typrelid =3D
f.attrelid
AND f.attrelid =3D
a.adrelid
=09 AND tab.relnamespace =3D
ns.oid
AND f.attname =3D
'$field_name'
AND tab.relname =3D
'$table_name'
AND ns.oid =3D
'$schema_name'
AND f.attnum =3D
a.adnum");
$row =3D @pg_fetch_row($result, 0);
$num =3D preg_replace("/'(.*)'::\w+/", "\
\1", $row[0]);
$flags .=3D 'default_' . rawurlencode($num)
. ' ';
}
} else {
$flags =3D '';
}
$result =3D @pg_exec($this->connection, "SELECT
i.indisunique,
i.indisprimary, i.indkey
FROM pg_attribute f,
pg_class tab,
pg_type typ, pg_index i, pg_namespace ns
WHERE tab.relname||
tab.relnamespace =3D
typ.typname||typ.typnamespace
AND typ.typrelid =3D
f.attrelid
AND f.attrelid =3D
i.indrelid
AND tab.relnamespace =3D
ns.oid
AND f.attname =3D
'$field_name'
AND tab.relname =3D
'$table_name'
AND ns.nspname =3D
'$schema_name'");
$count =3D @pg_numrows($result);

for ($i =3D 0; $i < $count ; $i++) {
$row =3D @pg_fetch_row($result, $i);
$keys =3D explode(' ', $row[2]);

if (in_array($num_field + 1, $keys)) {
$flags .=3D ($row[0] =3D=3D 't' && $row[1]
=3D=3D 'f') ?
'unique_key ' : '';
$flags .=3D ($row[1] =3D=3D 't') ?
'primary_key ' : '';
if (count($keys) > 1)
$flags .=3D 'multiple_key ';
}
}
return trim($flags);
}

[2005-10-27 23:20 UTC] skeemer+peardev at gmail dot com

sorry about the 3D, don't know where that came from.
if( strpos($table_name, '.')===false )
should have been a !==