/** Load the columns for this table */ protected function initColumns() { include_once 'creole/metadata/ColumnInfo.php'; include_once 'creole/drivers/pgsql/PgSQLTypes.php'; // Get any default values for columns $result = pg_query($this->dblink, "SELECT d.adnum as num, d.adsrc as def from pg_attrdef d, pg_class c where d.adrelid=c.oid and c.relname='" . $this->name . "' order by d.adnum"); if (!$result) { throw new SQLException("Could not get defaults for columns in table: " . $this->name, pg_last_error($this->dblink)); } $defaults = array(); while ($row = pg_fetch_assoc($result)) { // [HL] for now I am going to not add default // values that are nextval(...) sequence values. // We need to resolve on a larger level whether these should // be returned. Maybe instead indicating that these columns are // sequences would be appropriate... if (!preg_match('/^nextval\\(/', $row['def'])) { $defaults[$row['num']] = $row['def']; } } // Get the columns, types, etc. // based on SQL from ADOdb $result = pg_query($this->dblink, "SELECT a.attname,\n t.typname,\n a.attlen,\n a.atttypmod,\n a.attnotnull,\n a.atthasdef,\n a.attnum,\n CAST(\n CASE WHEN t.typtype = 'd' THEN\n CASE WHEN t.typbasetype IN (21, 23, 20) THEN 0\n WHEN t.typbasetype IN (1700) THEN (t.typtypmod - 4) & 65535\n ELSE null END\n ELSE\n CASE WHEN a.atttypid IN (21, 23, 20) THEN 0\n WHEN a.atttypid IN (1700) THEN (a.atttypmod - 4) & 65535\n ELSE null END\n END\n AS int) AS numeric_scale\n FROM pg_class c,\n pg_attribute a,\n pg_type t\n WHERE relkind = 'r' AND\n c.relname='" . $this->name . "' AND\n a.attnum > 0 AND\n a.atttypid = t.oid AND\n a.attrelid = c.oid\n ORDER BY a.attnum"); if (!$result) { throw new SQLException("Could not list fields for table: " . $this->name, pg_last_error($this->dblink)); } while ($row = pg_fetch_assoc($result)) { $name = $row['attname']; $type = $row['typname']; $size = $row['attlen']; $scale = $row['numeric_scale']; if ($size <= 0) { // maxlen for varchar is 4 larger than actual max length $size = $row['atttypmod'] - 4; if ($size <= 0) { $size = null; } } $is_nullable = $row['attnotnull'] == 't' ? true : false; $default = $row['atthasdef'] == 't' && isset($defaults[$row['attnum']]) ? $defaults[$row['attnum']] : null; $this->columns[$name] = new ColumnInfo($this, $name, PgSQLTypes::getType($type), $type, $size, $scale, $is_nullable, $default); } $this->colsLoaded = true; }
/** Load the columns for this table */ protected function initColumns() { // Include dependencies include_once 'creole/metadata/ColumnInfo.php'; include_once 'creole/drivers/pgsql/PgSQLTypes.php'; // Get the columns, types, etc. // Based on code from pgAdmin3 (http://www.pgadmin.org/) $result = pg_query($this->conn->getResource(), sprintf("SELECT \r\n \t\t\t\t\t\t\t\tatt.attname,\r\n \t\t\t\t\t\t\t\tatt.atttypmod,\r\n \t\t\t\t\t\t\t\tatt.atthasdef,\r\n \t\t\t\t\t\t\t\tatt.attnotnull,\r\n \t\t\t\t\t\t\t\tdef.adsrc, \r\n \t\t\t\t\t\t\t\tCASE WHEN att.attndims > 0 THEN 1 ELSE 0 END AS isarray, \r\n \t\t\t\t\t\t\t\tCASE \r\n \t\t\t\t\t\t\t\t\tWHEN ty.typname = 'bpchar' \r\n \t\t\t\t\t\t\t\t\t\tTHEN 'char' \r\n \t\t\t\t\t\t\t\t\tWHEN ty.typname = '_bpchar' \r\n\t \t\t\t\t\t\t\t\t\tTHEN '_char' \r\n \t\t\t\t\t\t\t\t\tELSE \r\n\t \t\t\t\t\t\t\t\t\tty.typname \r\n \t\t\t\t\t\t\t\tEND AS typname,\r\n \t\t\t\t\t\t\t\tty.typtype\r\n\t\t\t\t\t\t\t\tFROM pg_attribute att\r\n\t\t\t\t\t\t\t\t\tJOIN pg_type ty ON ty.oid=att.atttypid\r\n\t\t\t\t\t\t\t\t\tLEFT OUTER JOIN pg_attrdef def ON adrelid=att.attrelid AND adnum=att.attnum\r\n\t\t\t\t\t\t\t\tWHERE att.attrelid = %d AND att.attnum > 0\r\n\t\t\t\t\t\t\t\t\tAND att.attisdropped IS FALSE\r\n\t\t\t\t\t\t\t\tORDER BY att.attnum", $this->oid)); if (!$result) { throw new SQLException("Could not list fields for table: " . $this->name, pg_last_error($this->conn->getResource())); } while ($row = pg_fetch_assoc($result)) { $size = null; $precision = null; $scale = null; // Check to ensure that this column isn't an array data type if ((int) $row['isarray'] === 1) { throw new SQLException(sprintf("Array datatypes are not currently supported [%s.%s]", $this->name, $row['attname'])); } // if (((int) $row['isarray']) === 1) $name = $row['attname']; // If they type is a domain, Process it if (strtolower($row['typtype']) == 'd') { $arrDomain = $this->processDomain($row['typname']); $type = $arrDomain['type']; $size = $arrDomain['length']; $precision = $size; $scale = $arrDomain['scale']; $boolHasDefault = strlen(trim($row['atthasdef'])) > 0 ? $row['atthasdef'] : $arrDomain['hasdefault']; $default = strlen(trim($row['adsrc'])) > 0 ? $row['adsrc'] : $arrDomain['default']; $is_nullable = strlen(trim($row['attnotnull'])) > 0 ? $row['attnotnull'] : $arrDomain['notnull']; $is_nullable = $is_nullable == 't' ? false : true; } else { $type = $row['typname']; $arrLengthPrecision = $this->processLengthScale($row['atttypmod'], $type); $size = $arrLengthPrecision['length']; $precision = $size; $scale = $arrLengthPrecision['scale']; $boolHasDefault = $row['atthasdef']; $default = $row['adsrc']; $is_nullable = $row['attnotnull'] == 't' ? false : true; } // else (strtolower ($row['typtype']) == 'd') $autoincrement = null; // if column has a default if ($boolHasDefault == 't' && strlen(trim($default)) > 0) { if (!preg_match('/^nextval\\(/', $default)) { $strDefault = preg_replace('/::[\\W\\D]*/', '', $default); $default = str_replace("'", '', $strDefault); } else { $autoincrement = true; $default = null; } // else } else { $default = null; } // else (($boolHasDefault == 't') && (strlen (trim ($default)) > 0)) $this->columns[$name] = new ColumnInfo($this, $name, PgSQLTypes::getType($type), $type, $size, $precision, $scale, $is_nullable, $default, $autoincrement); } $this->colsLoaded = true; }