getTableDef() mostly working in postgres

This commit is contained in:
Brenda Wallace 2010-01-10 05:21:23 +00:00
parent e765a9657b
commit 870c83c17d

View File

@ -97,7 +97,12 @@ class Schema
public function getTableDef($name) public function getTableDef($name)
{ {
if(common_config('db','type') == 'pgsql') {
$res = $this->conn->query("select column_default as default, is_nullable as Null, udt_name as Type, column_name AS Field from INFORMATION_SCHEMA.COLUMNS where table_name = '$name'");
}
else {
$res = $this->conn->query('DESCRIBE ' . $name); $res = $this->conn->query('DESCRIBE ' . $name);
}
if (PEAR::isError($res)) { if (PEAR::isError($res)) {
throw new Exception($res->getMessage()); throw new Exception($res->getMessage());
@ -111,12 +116,16 @@ class Schema
$row = array(); $row = array();
while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
//lower case the keys, because the php postgres driver is case insentive for column names
foreach($row as $k=>$v) {
$row[strtolower($k)] = $row[$k];
}
$cd = new ColumnDef(); $cd = new ColumnDef();
$cd->name = $row['Field']; $cd->name = $row['field'];
$packed = $row['Type']; $packed = $row['type'];
if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) { if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) {
$cd->type = $match[1]; $cd->type = $match[1];
@ -125,9 +134,9 @@ class Schema
$cd->type = $packed; $cd->type = $packed;
} }
$cd->nullable = ($row['Null'] == 'YES') ? true : false; $cd->nullable = ($row['null'] == 'YES') ? true : false;
$cd->key = $row['Key']; $cd->key = $row['Key'];
$cd->default = $row['Default']; $cd->default = $row['default'];
$cd->extra = $row['Extra']; $cd->extra = $row['Extra'];
$td->columns[] = $cd; $td->columns[] = $cd;