* @package Mammut\DB\Adapter\PostgreSQL */ class Result extends \Mammut\DB\Result { private $result; private $rownum = 0; private $fieldcount = 0; private $fields; public function __construct($result) { $this->result = $result; $this->fieldcount = pg_num_fields($result); for($i = 0; $i < $this->fieldcount; $i++) { $type = strtolower(pg_field_type($result, $i)); $pgtype = $type; $name = pg_field_name($result, $i); switch($type) { case 'bool': $type = ColumnInfo::TYPE_BOOLEAN; break; case 'int2': $type = ColumnInfo::TYPE_SHORT; break; case 'int4': $type = ColumnInfo::TYPE_INT; break; case 'int8': $type = ColumnInfo::TYPE_LONG; break; case 'real': case 'float4': $type = ColumnInfo::TYPE_FLOAT; break; case 'float8': $type = ColumnInfo::TYPE_DOUBLE; break; case 'bpchar': $type = ColumnInfo::TYPE_CHAR; break; case 'varchar': $type = ColumnInfo::TYPE_VCHAR; break; case 'date': $type = ColumnInfo::TYPE_DATE; break; case 'timestamp': $type = ColumnInfo::TYPE_DATETIME; break; case 'timestamp': $type = ColumnInfo::TYPE_DATETIME; break; case 'bytea': $type = ColumnInfo::TYPE_BLOB; break; } $this->fields[$i]['type'] = $type; $this->fields[$i]['pgtype'] = $pgtype; $this->fields[$i]['name'] = $name; $this->fields[$name]['type'] = $type; $this->fields[$name]['pgtype'] = $pgtype; $this->fields[$name]['id'] = $i; } } public function getFields() { throw new ImplementationException('not implemented'); } public function getFieldTypes() { throw new ImplementationException('not implemented'); } public function position() { throw new ImplementationException('not implemented'); } public function seek() { throw new ImplementationException('not implemented'); } public function fetchRow() { $result = pg_fetch_row($this->result); if($result) { $this->rownum++; for($i = 0; $i < $this->fieldcount; $i++) $result[$i] = PostGre::convertDBData($this->fields[$i]['pgtype'], $result[$i]); } return $result; } public function fetchArray() { if(!is_resource($this->result)) throw new SQLException('no result set'); $result = pg_fetch_assoc($this->result); if($result) { $this->rownum++; foreach($result as $key=>$value) $result[$key] = PostGre::convertDBData($this->fields[$key]['pgtype'], $result[$key]); } return $result; } public function fetchObject($class = false, $param = array()) { if(!is_resource($this->result)) throw new SQLException('no result set'); if($class) { if(empty($param)) $result = pg_fetch_object($this->result, NULL, $class); else $result = pg_fetch_object($this->result, NULL, $class, $param); } else { $result = pg_fetch_object($this->result); if($result) $this->rownum++; } if($result) { $this->rownum++; for($i = 0; $i < $this->fieldcount; $i++) { $name = $this->fields[$i]['name']; $result->$name = PostGre::convertDBData($this->fields[$i]['pgtype'], $result->$name); } } return $result; } public function getRowCount() { return pg_num_rows($this->result); } public function getRowNumber() { return $this->rownum; } public function getColumns() { $result = array(); for($i = 0; $i < $this->fieldcount; $i++) $result[$i] = $this->fields[$i]; return $result; } public function getColumnInfo($column) { if(is_int($column)) { $col = $this->fields[$column]; $name = $col['name']; $type = $col['type']; } else { $col = $this->fields[$column]; $name = $column; $type = $col['type']; } $info = new ColumnInfo($name, $type); return $info; } public function getColumnCount() { return pg_numfields($this->result); } public function close() { pg_free_result($this->result); $this->result = NULL; } }