public static function getNativeType($creoleType) { if (self::$reverseMap === null) { self::$reverseMap = array_flip(self::$typeMap); } return @self::$reverseMap[$creoleType]; }
/** 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; }
private function processLengthScale($intTypmod, $strName) { // Define the return array $arrRetVal = array('length' => null, 'scale' => null); // Some datatypes don't have a Typmod if ($intTypmod == -1) { return $arrRetVal; } // if ($intTypmod == -1) // Numeric Datatype? if ($strName == PgSQLTypes::getNativeType(CreoleTypes::NUMERIC)) { $intLen = $intTypmod - 4 >> 16; $intPrec = $intTypmod - 4 & 0xffff; $intLen = sprintf("%ld", $intLen); if ($intPrec) { $intPrec = sprintf("%ld", $intPrec); } // if ($intPrec) $arrRetVal['length'] = $intLen; $arrRetVal['scale'] = $intPrec; } elseif ($strName == PgSQLTypes::getNativeType(CreoleTypes::TIME) || $strName == 'timetz' || $strName == PgSQLTypes::getNativeType(CreoleTypes::TIMESTAMP) || $strName == 'timestamptz' || $strName == 'interval' || $strName == 'bit') { $arrRetVal['length'] = sprintf("%ld", $intTypmod); } else { $arrRetVal['length'] = sprintf("%ld", $intTypmod - 4); } // else return $arrRetVal; }