123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- /**
- * Database result wrapper. See [Results](/database/results) for usage and examples.
- *
- * @package Kohana/Database
- * @category Query/Result
- * @author Kohana Team
- * @copyright (c) Kohana Team
- * @license https://koseven.ga/LICENSE.md
- */
- abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIterator, ArrayAccess {
- // Executed SQL for this result
- protected $_query;
- // Raw result resource
- protected $_result;
- // Total number of rows and current row
- protected $_total_rows = 0;
- protected $_current_row = 0;
- // Return rows as an object or associative array
- protected $_as_object;
- // Parameters for __construct when using object results
- protected $_object_params = NULL;
- /**
- * Sets the total number of rows and stores the result locally.
- *
- * @param mixed $result query result
- * @param string $sql SQL query
- * @param mixed $as_object
- * @param array $params
- * @return void
- */
- public function __construct($result, $sql, $as_object = FALSE, array $params = NULL)
- {
- // Store the result locally
- $this->_result = $result;
- // Store the SQL locally
- $this->_query = $sql;
- if (is_object($as_object))
- {
- // Get the object class name
- $as_object = get_class($as_object);
- }
- // Results as objects or associative arrays
- $this->_as_object = $as_object;
- if ($params)
- {
- // Object constructor params
- $this->_object_params = $params;
- }
- }
- /**
- * Result destruction cleans up all open result sets.
- *
- * @return void
- */
- abstract public function __destruct();
- /**
- * Get a cached database result from the current result iterator.
- *
- * $cachable = serialize($result->cached());
- *
- * @return Database_Result_Cached
- * @since 3.0.5
- */
- public function cached()
- {
- return new Database_Result_Cached($this->as_array(), $this->_query, $this->_as_object);
- }
- /**
- * Return all of the rows in the result as an array.
- *
- * // Indexed array of all rows
- * $rows = $result->as_array();
- *
- * // Associative array of rows by "id"
- * $rows = $result->as_array('id');
- *
- * // Associative array of rows, "id" => "name"
- * $rows = $result->as_array('id', 'name');
- *
- * @param string|null $key column for associative keys
- * @param string $value column for values
- * @return array
- */
- public function as_array($key = NULL, $value = NULL)
- {
- $results = [];
- if ($key === NULL AND $value === NULL)
- {
- // Indexed rows
- foreach ($this as $row)
- {
- $results[] = $row;
- }
- }
- elseif ($key === NULL)
- {
- // Indexed columns
- if ($this->_as_object)
- {
- foreach ($this as $row)
- {
- $results[] = $row->$value;
- }
- }
- else
- {
- foreach ($this as $row)
- {
- $results[] = $row[$value];
- }
- }
- }
- elseif ($value === NULL)
- {
- // Associative rows
- if ($this->_as_object)
- {
- foreach ($this as $row)
- {
- $results[$row->$key] = $row;
- }
- }
- else
- {
- foreach ($this as $row)
- {
- $results[$row[$key]] = $row;
- }
- }
- }
- else
- {
- // Associative columns
- if ($this->_as_object)
- {
- foreach ($this as $row)
- {
- $results[$row->$key] = $row->$value;
- }
- }
- else
- {
- foreach ($this as $row)
- {
- $results[$row[$key]] = $row[$value];
- }
- }
- }
- $this->rewind();
- return $results;
- }
- /**
- * Return the named column from the current row.
- *
- * // Get the "id" value
- * $id = $result->get('id');
- *
- * @param string $name column to get
- * @param mixed $default default value if the column does not exist
- * @return mixed
- */
- public function get($name, $default = NULL)
- {
- $row = $this->current();
- if ($this->_as_object)
- {
- if (isset($row->$name))
- return $row->$name;
- }
- else
- {
- if (isset($row[$name]))
- return $row[$name];
- }
- return $default;
- }
- /**
- * Implements [Countable::count], returns the total number of rows.
- *
- * echo count($result);
- *
- * @return integer
- */
- #[\ReturnTypeWillChange]
- public function count()
- {
- return $this->_total_rows;
- }
- /**
- * Implements [ArrayAccess::offsetExists], determines if row exists.
- *
- * if (isset($result[10]))
- * {
- * // Row 10 exists
- * }
- *
- * @param int $offset
- * @return boolean
- */
- #[\ReturnTypeWillChange]
- public function offsetExists($offset)
- {
- return ($offset >= 0 AND $offset < $this->_total_rows);
- }
- /**
- * Implements [ArrayAccess::offsetGet], gets a given row.
- *
- * $row = $result[10];
- *
- * @param int $offset
- * @return mixed
- */
- #[\ReturnTypeWillChange]
- public function offsetGet($offset)
- {
- if ( ! $this->seek($offset))
- return NULL;
- return $this->current();
- }
- /**
- * Implements [ArrayAccess::offsetSet], throws an error.
- *
- * [!!] You cannot modify a database result.
- *
- * @param int $offset
- * @param mixed $value
- * @return void
- * @throws Kohana_Exception
- */
- #[\ReturnTypeWillChange]
- final public function offsetSet($offset, $value)
- {
- throw new Kohana_Exception('Database results are read-only');
- }
- /**
- * Implements [ArrayAccess::offsetUnset], throws an error.
- *
- * [!!] You cannot modify a database result.
- *
- * @param int $offset
- * @return void
- * @throws Kohana_Exception
- */
- #[\ReturnTypeWillChange]
- final public function offsetUnset($offset)
- {
- throw new Kohana_Exception('Database results are read-only');
- }
- /**
- * Implements [Iterator::key], returns the current row number.
- *
- * echo key($result);
- *
- * @return integer
- */
- #[\ReturnTypeWillChange]
- public function key()
- {
- return $this->_current_row;
- }
- /**
- * Implements [Iterator::next], moves to the next row.
- *
- * next($result);
- *
- * @return $this
- */
- #[\ReturnTypeWillChange]
- public function next()
- {
- ++$this->_current_row;
- return $this;
- }
- /**
- * Implements [Iterator::prev], moves to the previous row.
- *
- * prev($result);
- *
- * @return $this
- */
- public function prev()
- {
- --$this->_current_row;
- return $this;
- }
- /**
- * Implements [Iterator::rewind], sets the current row to zero.
- *
- * rewind($result);
- *
- * @return $this
- */
- #[\ReturnTypeWillChange]
- public function rewind()
- {
- $this->_current_row = 0;
- return $this;
- }
- /**
- * Implements [Iterator::valid], checks if the current row exists.
- *
- * [!!] This method is only used internally.
- *
- * @return boolean
- */
- #[\ReturnTypeWillChange]
- public function valid()
- {
- return $this->offsetExists($this->_current_row);
- }
- } // End Database_Result
|