diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-11-24 19:20:36 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-11-24 19:20:36 -0800 |
commit | 9b6663f87a7e679ffba691cf516191fc840cf978 (patch) | |
tree | 20cf9f3aaf93b4ba69d282dcf10d259db4a752de /system/libraries/Database_Mysql_Result.php | |
parent | 82ee5f9d338017c69331b2907f37a468ced8c66e (diff) |
Update to Kohana r4684 which is now Kohana 2.4 and has substantial
changes.
Diffstat (limited to 'system/libraries/Database_Mysql_Result.php')
-rw-r--r-- | system/libraries/Database_Mysql_Result.php | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/system/libraries/Database_Mysql_Result.php b/system/libraries/Database_Mysql_Result.php new file mode 100644 index 00000000..020360d1 --- /dev/null +++ b/system/libraries/Database_Mysql_Result.php @@ -0,0 +1,176 @@ +<?php defined('SYSPATH') or die('No direct script access.'); +/** + * MySQL database result. + * + * $Id: Database_Mysql_Result.php 4679 2009-11-10 01:45:52Z isaiah $ + * + * @package Kohana + * @author Kohana Team + * @copyright (c) 2008-2009 Kohana Team + * @license http://kohanaphp.com/license + */ +class Database_Mysql_Result_Core extends Database_Result { + + protected $internal_row = 0; + + public function __construct($result, $sql, $link, $return_objects) + { + if (is_resource($result)) + { + // True to return objects, false for arrays + $this->return_objects = $return_objects; + + $this->total_rows = mysql_num_rows($result); + } + elseif (is_bool($result)) + { + if ($result == FALSE) + { + throw new Database_Exception('#:errno: :error [ :query ]', + array(':error' => mysql_error($link), + ':query' => $sql, + ':errno' => mysql_errno($link))); + + } + else + { + // It's a DELETE, INSERT, REPLACE, or UPDATE query + $this->insert_id = mysql_insert_id($link); + $this->total_rows = mysql_affected_rows($link); + } + } + + // Store the result locally + $this->result = $result; + + $this->sql = $sql; + } + + public function __destruct() + { + if (is_resource($this->result)) + { + mysql_free_result($this->result); + } + } + + public function as_array($return = FALSE) + { + // Return arrays rather than objects + $this->return_objects = FALSE; + + if ( ! $return ) + { + // Return this result object + return $this; + } + + // Return a nested array of all results + $array = array(); + + if ($this->total_rows > 0) + { + // Seek to the beginning of the result + mysql_data_seek($this->result, 0); + + while ($row = mysql_fetch_assoc($this->result)) + { + // Add each row to the array + $array[] = $row; + } + + $this->internal_row = $this->total_rows; + } + + return $array; + } + + public function as_object($class = NULL, $return = FALSE) + { + // Return objects of type $class (or stdClass if none given) + $this->return_objects = ($class !== NULL) ? $class : TRUE; + + if ( ! $return ) + { + // Return this result object + return $this; + } + + // Return a nested array of all results + $array = array(); + + if ($this->total_rows > 0) + { + // Seek to the beginning of the result + mysql_data_seek($this->result, 0); + + if (is_string($this->return_objects)) + { + while ($row = mysql_fetch_object($this->result, $this->return_objects)) + { + // Add each row to the array + $array[] = $row; + } + } + else + { + while ($row = mysql_fetch_object($this->result)) + { + // Add each row to the array + $array[] = $row; + } + } + + $this->internal_row = $this->total_rows; + } + + return $array; + } + + /** + * SeekableIterator: seek + */ + public function seek($offset) + { + if ($this->offsetExists($offset) AND mysql_data_seek($this->result, $offset)) + { + // Set the current row to the offset + $this->current_row = $this->internal_row = $offset; + + return TRUE; + } + else + { + return FALSE; + } + } + + /** + * Iterator: current + */ + public function current() + { + if ($this->current_row !== $this->internal_row AND ! $this->seek($this->current_row)) + return NULL; + + ++$this->internal_row; + + if ($this->return_objects) + { + if (is_string($this->return_objects)) + { + return mysql_fetch_object($this->result, $this->return_objects); + } + else + { + return mysql_fetch_object($this->result); + } + } + else + { + // Return an array of the row + return mysql_fetch_assoc($this->result); + } + } + +} // End Database_MySQL_Result
\ No newline at end of file |