summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2016-02-04 14:45:10 +0100
committerAndrey Andreev <narf@devilix.net>2016-02-04 14:45:10 +0100
commit105a48bbfc97be5902dbe80f90f8936fe174c9cf (patch)
tree2dbc336de70f4f8ddd5903cece03e9f302466aaa
parent71d64cbcfde625606ffb55867e0bb547123ef22f (diff)
Add batch_size param to insert_batch(), update_batch()
This should resolve #42
-rw-r--r--system/database/DB_query_builder.php12
-rw-r--r--user_guide_src/source/changelog.rst5
-rw-r--r--user_guide_src/source/database/query_builder.rst18
3 files changed, 25 insertions, 10 deletions
diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php
index be7582815..4922dd623 100644
--- a/system/database/DB_query_builder.php
+++ b/system/database/DB_query_builder.php
@@ -1458,7 +1458,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
* @param bool $escape Whether to escape values and identifiers
* @return int Number of rows inserted or FALSE on failure
*/
- public function insert_batch($table, $set = NULL, $escape = NULL)
+ public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 100)
{
if ($set === NULL)
{
@@ -1489,9 +1489,9 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
// Batch this baby
$affected_rows = 0;
- for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
+ for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
{
- $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
+ $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size)));
$affected_rows += $this->affected_rows();
}
@@ -1865,7 +1865,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
* @param string the where key
* @return int number of rows affected or FALSE on failure
*/
- public function update_batch($table, $set = NULL, $index = NULL)
+ public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
{
// Combine any cached components with the current statements
$this->_merge_cache();
@@ -1904,9 +1904,9 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
// Batch this baby
$affected_rows = 0;
- for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
+ for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
{
- $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index)));
+ $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, $batch_size), $this->protect_identifiers($index)));
$affected_rows += $this->affected_rows();
$this->qb_where = array();
}
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 1a06bd5ae..7e72c230f 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -7,6 +7,11 @@ Version 3.0.5
Release Date: Not Released
+- :doc:`Query Builder <database/query_builder>`
+
+ - Added a ``$batch_size`` parameter to the ``insert_batch()`` method (defaults to 100).
+ - Added a ``$batch_size`` parameter to the ``update_batch()`` method (defaults to 100).
+
Bug fixes for 3.0.5
-------------------
diff --git a/user_guide_src/source/database/query_builder.rst b/user_guide_src/source/database/query_builder.rst
index 5d9ae4592..3135f76da 100644
--- a/user_guide_src/source/database/query_builder.rst
+++ b/user_guide_src/source/database/query_builder.rst
@@ -1433,15 +1433,20 @@ Class Reference
Compiles and executes an INSERT statement.
- .. php:method:: insert_batch([$table = ''[, $set = NULL[, $escape = NULL]]])
+ .. php:method:: insert_batch($table[, $set = NULL[, $escape = NULL[, $batch_size = 100]]])
:param string $table: Table name
:param array $set: Data to insert
:param bool $escape: Whether to escape values and identifiers
+ :param int $batch_size: Count of rows to insert at once
:returns: Number of rows inserted or FALSE on failure
:rtype: mixed
- Compiles and executes batch INSERT statements.
+ Compiles and executes batch ``INSERT`` statements.
+
+ .. note:: When more than ``$batch_size`` rows are provided, multiple
+ ``INSERT`` queries will be executed, each trying to insert
+ up to ``$batch_size`` rows.
.. php:method:: set_insert_batch($key[, $value = ''[, $escape = NULL]])
@@ -1464,15 +1469,20 @@ Class Reference
Compiles and executes an UPDATE statement.
- .. php:method:: update_batch([$table = ''[, $set = NULL[, $value = NULL]]])
+ .. php:method:: update_batch($table[, $set = NULL[, $value = NULL[, $batch_size = 100]]])
:param string $table: Table name
:param array $set: Field name, or an associative array of field/value pairs
:param string $value: Field value, if $set is a single field
+ :param int $batch_size: Count of conditions to group in a single query
:returns: Number of rows updated or FALSE on failure
:rtype: mixed
- Compiles and executes batch UPDATE statements.
+ Compiles and executes batch ``UPDATE`` statements.
+
+ .. note:: When more than ``$batch_size`` field/value pairs are provided,
+ multiple queries will be executed, each handling up to
+ ``$batch_size`` field/value pairs.
.. php:method:: set_update_batch($key[, $value = ''[, $escape = NULL]])