* @package Mammut\DB\Adapter\MySQL */ class Result extends \Mammut\DB\Result { protected $result; protected $db; protected $colCount = -1; protected $columns; protected $rowCount = -1; protected $row = 0; public function getColumnCount() { return $this->colCount; } public function getColumns() { $i = 0; $cols = array(); for($i = 0; $i < $this->colCount; $i++) $cols[] = $this->columns[$i]['name']; return $cols; } public function getColumnInfo($column) { if(is_int($column)) { $col = $this->columns[$column]; $name = $col['name']; $type = $col['type']; } else { $col = $this->columns[$column]; $name = $column; $type = $col['type']; } $info = new ColumnInfo($name, $type); return $info; } public function getRowNumber() { return $this->row; } public function getRowCount() { return $this->rowCount; } /** * converts values based from a ColumnInfo type constant * * @param mixed $input * the value returned from the query * @param string $type * the MFDBColumnInfo type * @return mixed the converted value */ public static function convertResultValue($input, $type) { switch($type) { case ColumnInfo::TYPE_BOOLEAN: return ($input == 1); case ColumnInfo::TYPE_DATE: return is_null($input) ? NULL : new \Mammut\Date\Date($input); case ColumnInfo::TYPE_DATETIME: return is_null($input) ? NULL : new \DateTime($input); } return $input; } /** * converts values based on the type number from the result info * * @param mixed $input * the value returned from the query * @param integer $type * the mysql type value * @return mixed the converted value */ public static function convertDBResultValue($input, $type) { switch($type) { case MySQLUtil::MYSQL_TYPE_DATE: return is_null($input) ? NULL : new \Mammut\Date\Date($input); case MySQLUtil::MYSQL_TYPE_DATETIME: return is_null($input) ? NULL : new \DateTime($input); case MySQLUtil::MYSQL_TYPE_TIME: return is_null($input) ? NULL : new \Mammut\Date\Time($input); } return $input; } public function __construct(\mysqli &$db,\mysqli_result &$result) { $this->db = &$db; $this->result = &$result; $fields = $result->fetch_fields(); $i = 0; foreach($fields as $field) { $name = $field->name; $type = $field->type; $size = $field->length; switch($type) { case MYSQLI_TYPE_TINY: $type = ($size == 1) ? ColumnInfo::TYPE_BOOLEAN : ColumnInfo::TYPE_TINY; break; case MYSQLI_TYPE_SHORT: $type = ColumnInfo::TYPE_SHORT; break; case MYSQLI_TYPE_LONG: $type = ColumnInfo::TYPE_INT; break; case MYSQLI_TYPE_LONGLONG: $type = ColumnInfo::TYPE_LONG; break; case MYSQLI_TYPE_FLOAT: $type = ColumnInfo::TYPE_FLOAT; break; case MYSQLI_TYPE_DOUBLE: $type = ColumnInfo::TYPE_DOUBLE; break; case MYSQLI_TYPE_DATE: $type = ColumnInfo::TYPE_DATE; break; case MYSQLI_TYPE_DATETIME: $type = ColumnInfo::TYPE_DATETIME; break; case MYSQLI_TYPE_BLOB: $type = ($field->flags & MYSQLI_BLOB_FLAG) ? ColumnInfo::TYPE_BLOB : ColumnInfo::TYPE_TEXT; break; case MYSQLI_TYPE_VAR_STRING: $type = ColumnInfo::TYPE_VCHAR; break; case MYSQLI_TYPE_STRING: $type = ColumnInfo::TYPE_CHAR; break; } $this->columns[$i]['type'] = $type; $this->columns[$i]['name'] = $name; $this->columns[$name]['type'] = $type; $this->columns[$name]['id'] = $i; $i++; } $this->colCount = $i; $this->rowCount = $this->result->num_rows; } public function fetchRow() { $this->row++; $result = $this->result->fetch_row(); if($result) { for($i = 0; $i < $this->colCount; $i++) $result[$i] = self::convertResultValue($result[$i], $this->columns[$i]['type']); } return $result; } public function fetchArray() { $this->row++; $result = $this->result->fetch_assoc(); if($result) { for($i = 0; $i < $this->colCount; $i++) { $name = $this->columns[$i]['name']; $result[$name] = self::convertResultValue($result[$name], $this->columns[$name]['type']); } } return $result; } public function fetchObject($class = false, $param = array()) { $this->row++; if($class != false) $result = (count($param) == 0 ? $this->result->fetch_object($class) : $this->result->fetch_object($class, $param)); else $result = $this->result->fetch_object(); if($result) { for($i = 0; $i < $this->colCount; $i++) { $name = $this->columns[$i]['name']; $result->$name = self::convertResultValue($result->$name, $this->columns[$i]['type']); } } return $result; } public function close() { $this->result->free(); } }