Skip to content

Commit

Permalink
Rename DB_result _data_seek() to data_seek() and make it publicly ava…
Browse files Browse the repository at this point in the history
…ilable

(as requested in bcit-ci#2050)
  • Loading branch information
narfbg committed Dec 4, 2012
1 parent c07c485 commit 69edc43
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 25 deletions.
8 changes: 4 additions & 4 deletions system/database/DB_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function custom_result_object($class_name)
return $this->custom_result_object[$class_name];
}

$this->_data_seek(0);
$this->data_seek(0);
$this->custom_result_object[$class_name] = array();

while ($row = $this->_fetch_object($class_name))
Expand Down Expand Up @@ -246,7 +246,7 @@ public function result_object()
return $this->result_object;
}

$this->_data_seek(0);
$this->data_seek(0);
while ($row = $this->_fetch_object())
{
$this->result_object[] = $row;
Expand Down Expand Up @@ -287,7 +287,7 @@ public function result_array()
return $this->result_array;
}

$this->_data_seek(0);
$this->data_seek(0);
while ($row = $this->_fetch_assoc())
{
$this->result_array[] = $row;
Expand Down Expand Up @@ -617,7 +617,7 @@ public function free_result()
* @param int $n
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
return FALSE;
}
Expand Down
2 changes: 1 addition & 1 deletion system/database/drivers/cubrid/cubrid_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function free_result()
* @param int $n
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
return cubrid_data_seek($this->result_id, $n);
}
Expand Down
2 changes: 1 addition & 1 deletion system/database/drivers/mssql/mssql_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function free_result()
* @param int $n
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
return mssql_data_seek($this->result_id, $n);
}
Expand Down
2 changes: 1 addition & 1 deletion system/database/drivers/mysql/mysql_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function free_result()
* @param int $n
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
return $this->num_rows
? @mysql_data_seek($this->result_id, $n)
Expand Down
2 changes: 1 addition & 1 deletion system/database/drivers/mysqli/mysqli_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function free_result()
* @param int $n
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
return $this->result_id->data_seek($n);
}
Expand Down
7 changes: 6 additions & 1 deletion system/database/drivers/oci8/oci8_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,13 @@ protected function _fetch_object($class_name = 'stdClass')
* @param int $n (ignored)
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
if ($n > 0)
{
return FALSE;
}

/* The PHP manual says that if OCI_NO_AUTO_COMMIT mode
* is used, and oci_rollback() and/or oci_commit() are
* not subsequently called - this will cause an unnecessary
Expand Down
2 changes: 1 addition & 1 deletion system/database/drivers/postgre/postgre_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function free_result()
* @param int $n
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
return pg_result_seek($this->result_id, $n);
}
Expand Down
2 changes: 1 addition & 1 deletion system/database/drivers/sqlite/sqlite_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function field_data()
* @param int $n
* @return bool
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
return sqlite_seek($this->result_id, $n);
}
Expand Down
4 changes: 2 additions & 2 deletions system/database/drivers/sqlite3/sqlite3_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ protected function _fetch_object($class_name = 'stdClass')
* @param int $n (ignored)
* @return array
*/
protected function _data_seek($n = 0)
public function data_seek($n = 0)
{
// Only resetting to the start of the result set is supported
return $this->result_id->reset();
return ($n > 0) ? FALSE : $this->result_id->reset();
}

}
Expand Down
8 changes: 5 additions & 3 deletions user_guide_src/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,14 @@ Release Date: Not Released
- Added **schema** configuration setting (defaults to *public*) for drivers that might need it (currently used by PostgreSQL and ODBC).
- Added subdrivers support (currently only used by PDO).
- Added an optional database name parameter to ``db_select()``.
- Added a constructor to the ``DB_result`` class and moved all driver-specific properties and logic out of the base ``DB_driver`` class to allow better abstraction.
- Removed ``protect_identifiers()`` and renamed internal method ``_protect_identifiers()`` to it instead - it was just an alias.
- Renamed internal method ``_escape_identifiers()`` to ``escape_identifiers()``.
- Updated ``escape_identifiers()`` to accept an array of fields as well as strings.
- MySQL and MySQLi drivers now require at least MySQL version 5.1.
- ``db_set_charset()`` now only requires one parameter (collation was only needed due to legacy support for MySQL versions prior to 5.1).
- Replaced the ``_error_message()`` and ``_error_number()`` methods with ``error()``, which returns an array containing the last database error code and message.
- Improved ``version()`` implementation so that drivers that have a native function to get the version number don't have to be defined in the core ``DB_driver`` class.
- Added ``unbuffered_row()`` method for getting a row without prefetching whole result (consume less memory).
- Added capability for packages to hold *database.php* config files.
- Added capability for packages to hold *config/database.php* config files.
- Added MySQL client compression support.
- Added encrypted connections support (for *mysql*, *sqlsrv* and PDO with *sqlsrv*).
- Removed :doc:`Loader Class <libraries/loader>` from Database error tracing to better find the likely culprit.
Expand All @@ -133,6 +131,10 @@ Release Date: Not Released
- Changed ``limit()`` to ignore NULL values instead of always casting to integer.
- Changed ``offset()`` to ignore empty values instead of always casting to integer.
- Methods ``insert_batch()`` and ``update_batch()`` now return an integer representing the number of rows affected by them.
- :doc:`Database Results <database/results>` changes include:
- Added a constructor to the ``DB_result`` class and moved all driver-specific properties and logic out of the base ``DB_driver`` class to allow better abstraction.
- Added method ``unbuffered_row()`` for fetching a row without prefetching the whole result (consume less memory).
- Renamed former method ``_data_seek()`` to ``data_seek()`` and made it public.
- Improved support for the MySQLi driver, including:
- OOP style of the PHP extension is now used, instead of the procedural aliases.
- Server version checking is now done via ``mysqli::$server_info`` instead of running an SQL query.
Expand Down
43 changes: 34 additions & 9 deletions user_guide_src/source/database/results.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ to instantiate the row with::
echo $row->reverse_name(); // or methods defined on the 'User' class

row_array()
============
===========

Identical to the above row() function, except it returns an array.
Example::
Expand Down Expand Up @@ -138,12 +138,12 @@ parameter:
.. note:: all the functions above will load the whole result into memory (prefetching) use unbuffered_row() for processing large result sets.

unbuffered_row($type)
=====================
unbuffered_row()
================

This function returns a single result row without prefetching the whole result in memory as row() does.
If your query has more than one row, it returns the current row and moves the internal data pointer ahead.
The result is returned as $type could be 'object' (default) or 'array' that will return an associative array.
This method returns a single result row without prefetching the whole
result in memory as ``row()`` does. If your query has more than one row,
it returns the current row and moves the internal data pointer ahead.

::

Expand All @@ -156,12 +156,19 @@ The result is returned as $type could be 'object' (default) or 'array' that will
echo $row->body;
}

You can optionally pass 'object' (default) or 'array' in order to specify
the returned value's type::

$query->unbuffered_row(); // object
$query->unbuffered_row('object'); // object
$query->unbuffered_row('array'); // associative array

***********************
Result Helper Functions
***********************

$query->num_rows()
===================
==================

The number of rows returned by the query. Note: In this example, $query
is the variable that the query result object is assigned to::
Expand All @@ -177,7 +184,7 @@ is the variable that the query result object is assigned to::
resulting array in order to achieve the same functionality.

$query->num_fields()
=====================
====================

The number of FIELDS (columns) returned by the query. Make sure to call
the function using your query result object::
Expand All @@ -187,7 +194,7 @@ the function using your query result object::
echo $query->num_fields();

$query->free_result()
======================
=====================

It frees the memory associated with the result and deletes the result
resource ID. Normally PHP frees its memory automatically at the end of
Expand All @@ -209,3 +216,21 @@ Example::
$row = $query2->row();
echo $row->name;
$query2->free_result(); // The $query2 result object will no longer be available

data_seek()
===========

This method sets the internal pointer for the next result row to be
fetched. It is only useful in combination with ``unbuffered_row()``.

It accepts a positive integer value, which defaults to 0 and returns
TRUE on success or FALSE on failure.

::

$query = $this->db->query('SELECT `field_name` FROM `table_name`');
$query->data_seek(5); // Skip the first 5 rows
$row = $query->unbuffered_row();

.. note:: Not all database drivers support this feature and will return FALSE.
Most notably - you won't be able to use it with PDO.

0 comments on commit 69edc43

Please sign in to comment.