Menu

#88 PG_SQL will not give accurate sizes

7.4pre1
open
nobody
DB_SQL (15)
5
2002-05-29
2002-05-29
Anonymous
No

I have found that the libraray will not give accurate
results when the php functions are used to determine
the size of varchar fields. This makes for some
problems. For example on a form it is possible to have
a -1 maxlength on a text input.

I have developed a workaround that seems to address all
the variable types except to text and blobs.

Below is the code I found. I have commented out the old
code that was there and added the new. This is a bit
slower then the php functions but it works right. The
query was initally found in the phpPgAdmin program.

function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$row = array();

$this->connect();
// $id = pg_exec($this->Link_ID, "select * from
$table");
// if ($id < 0) {
// $this->Error = pg_ErrorMessage($id);
// $this->Errno = 1;
// $this->halt("Metadata query failed.");
// }
// $count = pg_NumFields($id);
// $id = pg_exec($this->Link_ID, "select * from
$table");

// for ($i=0; $i<$count; $i++) {
// $res[$i]["table"] = $table;
// $res[$i]["name"] = pg_FieldName ($id, $i);
// $res[$i]["type"] = pg_FieldType ($id, $i);
// $res[$i]["len"] = pg_FieldSize ($id, $i);
// $res[$i]["flags"] = "";
// }

// Additional code to get rid of the -1 errors.
// include ("array_dump.inc");
$query = sprintf (" select a.attnum,a.attname as field,
t.typname as type, case when a.attlen='-1' then
a.atttypmod else a.attlen end as length, a.attnotnull
as notnul from pg_class c, pg_attribute a, pg_type t
where c.relname='%s' and a.attnum > 0 and a.attrelid =
c.oid and a.atttypid = t.oid order by a.attnum;
",$table);
$res_fields = @pg_exec($this->Link_ID, $query);

for ($i=0; $row= @pg_fetch_array ($res_fields,$i); $i++) {
$res[$i]["table"] = $table;
$res[$i]["name"] = $row["field"];
$res[$i]["len"] = $row["length"];
$res[$i]["type"] = $row["type"];
$res[$i]["flags"] = "";
// array_dump ($row);
}

pg_FreeResult($res_fields);
// pg_FreeResult($id);
return $res;
}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.