Added unbuffered_row() function (issue #1351) #1372

Merged
merged 6 commits into from May 21, 2012

2 participants

@juanitomint

For dealing with large result sets.
Added function and documentation.

@narfbg narfbg commented on an outdated diff May 19, 2012
system/database/DB_result.php
@@ -371,6 +371,24 @@ public function previous_row($type = 'object')
// --------------------------------------------------------------------
/**
+ * Returns an unbuffered row and move pointer to next row
+ *
+ * @return object
@narfbg
narfbg added a note May 19, 2012

This should say 'mixed' instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg narfbg commented on an outdated diff May 19, 2012
system/database/DB_result.php
@@ -371,6 +371,24 @@ public function previous_row($type = 'object')
// --------------------------------------------------------------------
/**
+ * Returns an unbuffered row and move pointer to next row
+ *
+ * @return object
+ */
+ public function unbuffered_row($type = 'object')
+ {
+ if ($type == 'object')
+ {
+ return $this->_fetch_object();
+ } else
@narfbg
narfbg added a note May 19, 2012

else needs to be on the next line, or you could just make the whole thing like this:

return ($type !== 'array') ? $this->_fetch_object() : $this->_fetch_assoc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg narfbg and 1 other commented on an outdated diff May 19, 2012
user_guide_src/source/database/results.rst
@@ -136,6 +136,26 @@ parameter:
| **$row = $query->next_row('array')**
| **$row = $query->previous_row('array')**
+.. note:: all the functions above will load the whole result into memory (prefetching) use unbuffered_row() for processing large result sets.
+
+unbuffered_row($type)
+=====
+
+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.
+
+
+
+ $query = $this->db->query("YOUR QUERY");
+
+ while ($row=$query->unbuffered_rows())
@narfbg
narfbg added a note May 19, 2012

Add spaces around the assignment operator and the method name needs to be fixed as well. :)

ok, got it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg

And a changelog entry should be added as well, under the Database section.

@juanitomint juanitomint commented on the diff May 19, 2012
user_guide_src/source/database/results.rst
@@ -136,6 +136,26 @@ parameter:
| **$row = $query->next_row('array')**
| **$row = $query->previous_row('array')**
+.. note:: all the functions above will load the whole result into memory (prefetching) use unbuffered_row() for processing large result sets.
+
+unbuffered_row($type)
+=====
+
+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.
+
+
+
+ $query = $this->db->query("YOUR QUERY");
+
+ while ($row = $query->unbuffered_row())

Added space around: ready!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@juanitomint juanitomint commented on the diff May 19, 2012
system/database/DB_result.php
@@ -371,6 +371,19 @@ public function previous_row($type = 'object')
// --------------------------------------------------------------------
/**
+ * Returns an unbuffered row and move pointer to next row
+ *
+ * @return mixed either a result object or array
+ */
+ public function unbuffered_row($type = 'object')
+ {
+ return ($type !== 'array') ? $this->_fetch_object() : $this->_fetch_assoc();

changed to this more compact form of conditional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@narfbg narfbg merged commit 1d79efe into bcit-ci:develop May 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment