summaryrefslogtreecommitdiffstats
path: root/system/database/DB_active_rec.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@server-speed.net>2011-01-28 22:51:06 +0100
committerFlorian Pritz <bluewind@server-speed.net>2011-01-28 22:58:52 +0100
commitce2b69675075444c9e40b72bcdd42ab7edbbe633 (patch)
tree2932f13b0db14fe53dc0622d888318db638a017f /system/database/DB_active_rec.php
parentb6b8a6587c399bfd89e13e92ce04ee8486688e6e (diff)
update to CI 2.0
Signed-off-by: Florian Pritz <bluewind@server-speed.net>
Diffstat (limited to 'system/database/DB_active_rec.php')
-rwxr-xr-x[-rw-r--r--]system/database/DB_active_rec.php703
1 files changed, 478 insertions, 225 deletions
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php
index bf4d5117e..ce9d1c1af 100644..100755
--- a/system/database/DB_active_rec.php
+++ b/system/database/DB_active_rec.php
@@ -2,11 +2,11 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 4.3.2 or newer
+ * An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
@@ -36,17 +36,18 @@ class CI_DB_active_record extends CI_DB_driver {
var $ar_like = array();
var $ar_groupby = array();
var $ar_having = array();
+ var $ar_keys = array();
var $ar_limit = FALSE;
var $ar_offset = FALSE;
var $ar_order = FALSE;
var $ar_orderby = array();
- var $ar_set = array();
+ var $ar_set = array();
var $ar_wherein = array();
var $ar_aliased_tables = array();
var $ar_store_array = array();
-
+
// Active Record Caching variables
- var $ar_caching = FALSE;
+ var $ar_caching = FALSE;
var $ar_cache_exists = array();
var $ar_cache_select = array();
var $ar_cache_from = array();
@@ -56,7 +57,7 @@ class CI_DB_active_record extends CI_DB_driver {
var $ar_cache_groupby = array();
var $ar_cache_having = array();
var $ar_cache_orderby = array();
- var $ar_cache_set = array();
+ var $ar_cache_set = array();
// --------------------------------------------------------------------
@@ -72,12 +73,12 @@ class CI_DB_active_record extends CI_DB_driver {
*/
function select($select = '*', $escape = NULL)
{
- // Set the global value if this was sepecified
+ // Set the global value if this was sepecified
if (is_bool($escape))
{
$this->_protect_identifiers = $escape;
}
-
+
if (is_string($select))
{
$select = explode(',', $select);
@@ -117,7 +118,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $this->_max_min_avg_sum($select, $alias, 'MAX');
}
-
+
// --------------------------------------------------------------------
/**
@@ -178,7 +179,7 @@ class CI_DB_active_record extends CI_DB_driver {
* select_min()
* select_avg()
* select_sum()
- *
+ *
* @access public
* @param string the field
* @param string an alias
@@ -190,29 +191,29 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->display_error('db_invalid_query');
}
-
+
$type = strtoupper($type);
-
+
if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
{
show_error('Invalid function type: '.$type);
}
-
+
if ($alias == '')
{
$alias = $this->_create_alias_from_table(trim($select));
}
-
+
$sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
$this->ar_select[] = $sql;
-
+
if ($this->ar_caching === TRUE)
{
$this->ar_cache_select[] = $sql;
$this->ar_cache_exists[] = 'select';
}
-
+
return $this;
}
@@ -231,7 +232,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return end(explode('.', $item));
}
-
+
return $item;
}
@@ -251,7 +252,7 @@ class CI_DB_active_record extends CI_DB_driver {
$this->ar_distinct = (is_bool($val)) ? $val : TRUE;
return $this;
}
-
+
// --------------------------------------------------------------------
/**
@@ -275,12 +276,12 @@ class CI_DB_active_record extends CI_DB_driver {
$this->_track_aliases($v);
$this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
-
+
if ($this->ar_caching === TRUE)
{
$this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
$this->ar_cache_exists[] = 'from';
- }
+ }
}
}
@@ -289,11 +290,11 @@ class CI_DB_active_record extends CI_DB_driver {
$val = trim($val);
// Extract any aliases that might exist. We use this information
- // in the _protect_identifiers to know whether to add a table prefix
+ // in the _protect_identifiers to know whether to add a table prefix
$this->_track_aliases($val);
-
+
$this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
-
+
if ($this->ar_caching === TRUE)
{
$this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
@@ -319,7 +320,7 @@ class CI_DB_active_record extends CI_DB_driver {
* @return object
*/
function join($table, $cond, $type = '')
- {
+ {
if ($type != '')
{
$type = strtoupper(trim($type));
@@ -335,7 +336,7 @@ class CI_DB_active_record extends CI_DB_driver {
}
// Extract any aliases that might exist. We use this information
- // in the _protect_identifiers to know whether to add a table prefix
+ // in the _protect_identifiers to know whether to add a table prefix
$this->_track_aliases($table);
// Strip apart the condition and protect the identifiers
@@ -343,10 +344,10 @@ class CI_DB_active_record extends CI_DB_driver {
{
$match[1] = $this->_protect_identifiers($match[1]);
$match[3] = $this->_protect_identifiers($match[3]);
-
- $cond = $match[1].$match[2].$match[3];
+
+ $cond = $match[1].$match[2].$match[3];
}
-
+
// Assemble the JOIN statement
$join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
@@ -377,7 +378,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $this->_where($key, $value, 'AND ', $escape);
}
-
+
// --------------------------------------------------------------------
/**
@@ -399,18 +400,6 @@ class CI_DB_active_record extends CI_DB_driver {
// --------------------------------------------------------------------
/**
- * orwhere() is an alias of or_where()
- * this function is here for backwards compatibility, as
- * orwhere() has been deprecated
- */
- function orwhere($key, $value = NULL, $escape = TRUE)
- {
- return $this->or_where($key, $value, $escape);
- }
-
- // --------------------------------------------------------------------
-
- /**
* Where
*
* Called by where() or orwhere()
@@ -427,7 +416,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
$key = array($key => $value);
}
-
+
// If the escape value was not set will will base it on the global setting
if ( ! is_bool($escape))
{
@@ -443,13 +432,13 @@ class CI_DB_active_record extends CI_DB_driver {
// value appears not to have been set, assign the test to IS NULL
$k .= ' IS NULL';
}
-
+
if ( ! is_null($v))
{
if ($escape === TRUE)
{
$k = $this->_protect_identifiers($k, FALSE, $escape);
-
+
$v = ' '.$this->escape($v);
}
@@ -460,19 +449,19 @@ class CI_DB_active_record extends CI_DB_driver {
}
else
{
- $k = $this->_protect_identifiers($k, FALSE, $escape);
+ $k = $this->_protect_identifiers($k, FALSE, $escape);
}
$this->ar_where[] = $prefix.$k.$v;
-
+
if ($this->ar_caching === TRUE)
{
$this->ar_cache_where[] = $prefix.$k.$v;
$this->ar_cache_exists[] = 'where';
}
-
+
}
-
+
return $this;
}
@@ -493,7 +482,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $this->_where_in($key, $values);
}
-
+
// --------------------------------------------------------------------
/**
@@ -529,7 +518,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $this->_where_in($key, $values, TRUE);
}
-
+
// --------------------------------------------------------------------
/**
@@ -559,7 +548,7 @@ class CI_DB_active_record extends CI_DB_driver {
* @param string The field to search
* @param array The values searched on
* @param boolean If the statement would be IN or NOT IN
- * @param string
+ * @param string
* @return object
*/
function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
@@ -568,12 +557,12 @@ class CI_DB_active_record extends CI_DB_driver {
{
return;
}
-
+
if ( ! is_array($values))
{
$values = array($values);
}
-
+
$not = ($not) ? ' NOT' : '';
foreach ($values as $value)
@@ -582,7 +571,7 @@ class CI_DB_active_record extends CI_DB_driver {
}
$prefix = (count($this->ar_where) == 0) ? '' : $type;
-
+
$where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
$this->ar_where[] = $where_in;
@@ -596,7 +585,7 @@ class CI_DB_active_record extends CI_DB_driver {
$this->ar_wherein = array();
return $this;
}
-
+
// --------------------------------------------------------------------
/**
@@ -632,7 +621,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $this->_like($field, $match, 'AND ', $side, 'NOT');
}
-
+
// --------------------------------------------------------------------
/**
@@ -668,19 +657,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $this->_like($field, $match, 'OR ', $side, 'NOT');
}
-
- // --------------------------------------------------------------------
- /**
- * orlike() is an alias of or_like()
- * this function is here for backwards compatibility, as
- * orlike() has been deprecated
- */
- function orlike($field, $match = '', $side = 'both')
- {
- return $this->or_like($field, $match, $side);
- }
-
// --------------------------------------------------------------------
/**
@@ -700,7 +677,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
$field = array($field => $match);
}
-
+
foreach ($field as $k => $v)
{
$k = $this->_protect_identifiers($k);
@@ -721,24 +698,24 @@ class CI_DB_active_record extends CI_DB_driver {
{
$like_statement = $prefix." $k $not LIKE '%{$v}%'";
}
-
+
// some platforms require an escape sequence definition for LIKE wildcards
if ($this->_like_escape_str != '')
{
- $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_char);
+ $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
}
-
+
$this->ar_like[] = $like_statement;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_like[] = $like_statement;
$this->ar_cache_exists[] = 'like';
}
-
+
}
return $this;
}
-
+
// --------------------------------------------------------------------
/**
@@ -754,15 +731,15 @@ class CI_DB_active_record extends CI_DB_driver {
{
$by = explode(',', $by);
}
-
+
foreach ($by as $val)
{
$val = trim($val);
-
+
if ($val != '')
{
$this->ar_groupby[] = $this->_protect_identifiers($val);
-
+
if ($this->ar_caching === TRUE)
{
$this->ar_cache_groupby[] = $this->_protect_identifiers($val);
@@ -776,18 +753,6 @@ class CI_DB_active_record extends CI_DB_driver {
// --------------------------------------------------------------------
/**
- * groupby() is an alias of group_by()
- * this function is here for backwards compatibility, as
- * groupby() has been deprecated
- */
- function groupby($by)
- {
- return $this->group_by($by);
- }
-
- // --------------------------------------------------------------------
-
- /**
* Sets the HAVING value
*
* Separates multiple calls with AND
@@ -805,18 +770,6 @@ class CI_DB_active_record extends CI_DB_driver {
// --------------------------------------------------------------------
/**
- * orhaving() is an alias of or_having()
- * this function is here for backwards compatibility, as
- * orhaving() has been deprecated
- */
-
- function orhaving($key, $value = '', $escape = TRUE)
- {
- return $this->or_having($key, $value, $escape);
- }
- // --------------------------------------------------------------------
-
- /**
* Sets the OR HAVING value
*
* Separates multiple calls with OR
@@ -830,7 +783,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $this->_having($key, $value, 'OR ', $escape);
}
-
+
// --------------------------------------------------------------------
/**
@@ -849,7 +802,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
$key = array($key => $value);
}
-
+
foreach ($key as $k => $v)
{
$prefix = (count($this->ar_having) == 0) ? '' : $type;
@@ -868,7 +821,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
$v = ' '.$this->escape_str($v);
}
-
+
$this->ar_having[] = $prefix.$k.$v;
if ($this->ar_caching === TRUE)
{
@@ -876,10 +829,10 @@ class CI_DB_active_record extends CI_DB_driver {
$this->ar_cache_exists[] = 'having';
}
}
-
+
return $this;
}
-
+
// --------------------------------------------------------------------
/**
@@ -901,8 +854,8 @@ class CI_DB_active_record extends CI_DB_driver {
{
$direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
}
-
-
+
+
if (strpos($orderby, ',') !== FALSE)
{
$temp = array();
@@ -913,19 +866,19 @@ class CI_DB_active_record extends CI_DB_driver {
{
$part = $this->_protect_identifiers(trim($part));
}
-
+
$temp[] = $part;
}
-
- $orderby = implode(', ', $temp);
+
+ $orderby = implode(', ', $temp);
}
else if ($direction != $this->_random_keyword)
{
$orderby = $this->_protect_identifiers($orderby);
}
-
+
$orderby_statement = $orderby.$direction;
-
+
$this->ar_orderby[] = $orderby_statement;
if ($this->ar_caching === TRUE)
{
@@ -935,19 +888,7 @@ class CI_DB_active_record extends CI_DB_driver {
return $this;
}
-
- // --------------------------------------------------------------------
- /**
- * orderby() is an alias of order_by()
- * this function is here for backwards compatibility, as
- * orderby() has been deprecated
- */
- function orderby($orderby, $direction = '')
- {
- return $this->order_by($orderby, $direction);
- }
-
// --------------------------------------------------------------------
/**
@@ -966,10 +907,10 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->ar_offset = $offset;
}
-
+
return $this;
}
-
+
// --------------------------------------------------------------------
/**
@@ -984,7 +925,7 @@ class CI_DB_active_record extends CI_DB_driver {
$this->ar_offset = $offset;
return $this;
}
-
+
// --------------------------------------------------------------------
/**
@@ -999,11 +940,11 @@ class CI_DB_active_record extends CI_DB_driver {
function set($key, $value = '', $escape = TRUE)
{
$key = $this->_object_to_array($key);
-
+
if ( ! is_array($key))
{
$key = array($key => $value);
- }
+ }
foreach ($key as $k => $v)
{
@@ -1016,10 +957,10 @@ class CI_DB_active_record extends CI_DB_driver {
$this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
}
}
-
+
return $this;
}
-
+
// --------------------------------------------------------------------
/**
@@ -1041,12 +982,12 @@ class CI_DB_active_record extends CI_DB_driver {
$this->_track_aliases($table);
$this->from($table);
}
-
+
if ( ! is_null($limit))
{
$this->limit($limit, $offset);
}
-
+
$sql = $this->_compile_select();
$result = $this->query($sql);
@@ -1057,7 +998,7 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* "Count All Results" query
*
- * Generates a platform-specific query string that counts all records
+ * Generates a platform-specific query string that counts all records
* returned by an Active Record query.
*
* @access public
@@ -1071,12 +1012,12 @@ class CI_DB_active_record extends CI_DB_driver {
$this->_track_aliases($table);
$this->from($table);
}
-
+
$sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
$query = $this->query($sql);
$this->_reset_select();
-
+
if ($query->num_rows() == 0)
{
return '0';
@@ -1110,12 +1051,12 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->where($where);
}
-
+
if ( ! is_null($limit))
{
$this->limit($limit, $offset);
}
-
+
$sql = $this->_compile_select();
$result = $this->query($sql);
@@ -1126,15 +1067,123 @@ class CI_DB_active_record extends CI_DB_driver {
// --------------------------------------------------------------------
/**
- * getwhere() is an alias of get_where()
- * this function is here for backwards compatibility, as
- * getwhere() has been deprecated
+ * Insert_Batch
+ *
+ * Compiles batch insert strings and runs the queries
+ *
+ * @access public
+ * @param string the table to retrieve the results from
+ * @param array an associative array of insert values
+ * @return object
*/
- function getwhere($table = '', $where = null, $limit = null, $offset = null)
+ function insert_batch($table = '', $set = NULL)
{
- return $this->get_where($table, $where, $limit, $offset);
+ if ( ! is_null($set))
+ {
+ $this->set_insert_batch($set);
+ }
+
+ if (count($this->ar_set) == 0)
+ {
+ if ($this->db_debug)
+ {
+ //No valid data array. Folds in cases where keys and values did not match up
+ return $this->display_error('db_must_use_set');
+ }
+ return FALSE;
+ }
+
+ if ($table == '')
+ {
+ if ( ! isset($this->ar_from[0]))
+ {
+ if ($this->db_debug)
+ {
+ return $this->display_error('db_must_set_table');
+ }
+ return FALSE;
+ }
+
+ $table = $this->ar_from[0];
+ }
+
+ // Batch this baby
+ for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
+ {
+
+ $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
+
+ //echo $sql;
+
+ $this->query($sql);
+ }
+
+ $this->_reset_write();
+
+
+ return TRUE;
}
-
+
+ // --------------------------------------------------------------------
+
+ /**
+ * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
+ *
+ * @access public
+ * @param mixed
+ * @param string
+ * @param boolean
+ * @return object
+ */
+
+ function set_insert_batch($key, $value = '', $escape = TRUE)
+ {
+ $key = $this->_object_to_array_batch($key);
+
+ if ( ! is_array($key))
+ {
+ $key = array($key => $value);
+ }
+
+ $keys = array_keys(current($key));
+ sort($keys);
+
+ foreach ($key as $row)
+ {
+ if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
+ {
+ // batch function above returns an error on an empty array
+ $this->ar_set[] = array();
+ return;
+ }
+
+ ksort($row); // puts $row in the same order as our keys
+
+ if ($escape === FALSE)
+ {
+ $this->ar_set[] = '('.implode(',', $row).')';
+ }
+ else
+ {
+ $clean = array();
+
+ foreach($row as $value)
+ {
+ $clean[] = $this->escape($value);
+ }
+
+ $this->ar_set[] = '('.implode(',', $clean).')';
+ }
+ }
+
+ foreach ($keys as $k)
+ {
+ $this->ar_keys[] = $this->_protect_identifiers($k);
+ }
+
+ return $this;
+ }
+
// --------------------------------------------------------------------
/**
@@ -1148,12 +1197,12 @@ class CI_DB_active_record extends CI_DB_driver {
* @return object
*/
function insert($table = '', $set = NULL)
- {
+ {
if ( ! is_null($set))
{
$this->set($set);
}
-
+
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
@@ -1173,16 +1222,52 @@ class CI_DB_active_record extends CI_DB_driver {
}
return FALSE;
}
-
+
$table = $this->ar_from[0];
}
$sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
-
+
$this->_reset_write();
- return $this->query($sql);
+ return $this->query($sql);
}
-
+
+ function replace($table = '', $set = NULL)
+ {
+ if ( ! is_null($set))
+ {
+ $this->set($set);
+ }
+
+ if (count($this->ar_set) == 0)
+ {
+ if ($this->db_debug)
+ {
+ return $this->display_error('db_must_use_set');
+ }
+ return FALSE;
+ }
+
+ if ($table == '')
+ {
+ if ( ! isset($this->ar_from[0]))
+ {
+ if ($this->db_debug)
+ {
+ return $this->display_error('db_must_set_table');
+ }
+ return FALSE;
+ }
+
+ $table = $this->ar_from[0];
+ }
+
+ $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
+
+ $this->_reset_write();
+ return $this->query($sql);
+ }
+
// --------------------------------------------------------------------
/**
@@ -1205,7 +1290,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->set($set);
}
-
+
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
@@ -1225,10 +1310,10 @@ class CI_DB_active_record extends CI_DB_driver {
}
return FALSE;
}
-
+
$table = $this->ar_from[0];
}
-
+
if ($where != NULL)
{
$this->where($where);
@@ -1238,13 +1323,140 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->limit($limit);
}
-
+
$sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
-
+
$this->_reset_write();
return $this->query($sql);
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Update_Batch
+ *
+ * Compiles an update string and runs the query
+ *
+ * @access public
+ * @param string the table to retrieve the results from
+ * @param array an associative array of update values
+ * @param string the where key
+ * @return object
+ */
+ function update_batch($table = '', $set = NULL, $index = NULL)
+ {
+ // Combine any cached components with the current statements
+ $this->_merge_cache();
+
+ if (is_null($index))
+ {
+ if ($this->db_debug)
+ {
+ return $this->display_error('db_myst_use_index');
+ }
+
+ return FALSE;
+ }
+
+ if ( ! is_null($set))
+ {
+ $this->set_update_batch($set, $index);
+ }
+
+ if (count($this->ar_set) == 0)
+ {
+ if ($this->db_debug)
+ {
+ return $this->display_error('db_must_use_set');
+ }
+
+ return FALSE;
+ }
+
+ if ($table == '')
+ {
+ if ( ! isset($this->ar_from[0]))
+ {
+ if ($this->db_debug)
+ {
+ return $this->display_error('db_must_set_table');
+ }
+ return FALSE;
+ }
+
+ $table = $this->ar_from[0];
+ }
+
+ // Batch this baby
+ for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
+ {
+ $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
+
+ $this->query($sql);
+ }
+
+ $this->_reset_write();
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
+ *
+ * @access public
+ * @param array
+ * @param string
+ * @param boolean
+ * @return object
+ */
+
+ function set_update_batch($key, $index = '', $escape = TRUE)
+ {
+ $key = $this->_object_to_array_batch($key);
+
+ if ( ! is_array($key))
+ {
+ // @todo error
+ }
+
+ foreach ($key as $k => $v)
+ {
+ $index_set = FALSE;
+ $clean = array();
+
+ foreach($v as $k2 => $v2)
+ {
+ if ($k2 == $index)
+ {
+ $index_set = TRUE;
+ }
+ else
+ {
+ $not[] = $k.'-'.$v;
+ }
+
+ if ($escape === FALSE)
+ {
+ $clean[$this->_protect_identifiers($k2)] = $v2;
+ }
+ else
+ {
+ $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
+ }
+ }
+
+ if ($index_set == FALSE)
+ {
+ return $this->display_error('db_batch_missing_index');
+ }
+
+ $this->ar_set[] = $clean;
+ }
+
+ return $this;
+ }
+
// --------------------------------------------------------------------
/**
@@ -1279,7 +1491,7 @@ class CI_DB_active_record extends CI_DB_driver {
$sql = $this->_delete($table);
$this->_reset_write();
-
+
return $this->query($sql);
}
@@ -1319,10 +1531,10 @@ class CI_DB_active_record extends CI_DB_driver {
$sql = $this->_truncate($table);
$this->_reset_write();
-
+
return $this->query($sql);
}
-
+
// --------------------------------------------------------------------
/**
@@ -1388,7 +1600,7 @@ class CI_DB_active_record extends CI_DB_driver {
}
return FALSE;
- }
+ }
$sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
@@ -1396,7 +1608,7 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->_reset_write();
}
-
+
return $this->query($sql);
}
@@ -1431,7 +1643,7 @@ class CI_DB_active_record extends CI_DB_driver {
* @access private
* @param string The table to inspect
* @return string
- */
+ */
function _track_aliases($table)
{
if (is_array($table))
@@ -1442,23 +1654,23 @@ class CI_DB_active_record extends CI_DB_driver {
}
return;
}
-
+
// Does the string contain a comma? If so, we need to separate
// the string into discreet statements
if (strpos($table, ',') !== FALSE)
{
return $this->_track_aliases(explode(',', $table));
}
-
+
// if a table alias is used we can recognize it by a space
if (strpos($table, " ") !== FALSE)
{
// if the alias is written with the AS keyword, remove it
$table = preg_replace('/ AS /i', ' ', $table);
-
+
// Grab the alias
$table = trim(strrchr($table, " "));
-
+
// Store the alias, if it doesn't already exist
if ( ! in_array($table, $this->ar_aliased_tables))
{
@@ -1484,7 +1696,7 @@ class CI_DB_active_record extends CI_DB_driver {
$this->_merge_cache();
// ----------------------------------------------------------------
-
+
// Write the "select" portion of the query
if ($select_override !== FALSE)
@@ -1494,13 +1706,13 @@ class CI_DB_active_record extends CI_DB_driver {
else
{
$sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
-
+
if (count($this->ar_select) == 0)
{
- $sql .= '*';
+ $sql .= '*';
}
else
- {
+ {
// Cycle through the "select" portion of the query and prep each column name.
// The reason we protect identifiers here rather then in the select() function
// is because until the user calls the from() function we don't know if there are aliases
@@ -1508,13 +1720,13 @@ class CI_DB_active_record extends CI_DB_driver {
{
$this->ar_select[$key] = $this->_protect_identifiers($val);
}
-
+
$sql .= implode(', ', $this->ar_select);
}
}
// ----------------------------------------------------------------
-
+
// Write the "FROM" portion of the query
if (count($this->ar_from) > 0)
@@ -1525,7 +1737,7 @@ class CI_DB_active_record extends CI_DB_driver {
}
// ----------------------------------------------------------------
-
+
// Write the "JOIN" portion of the query
if (count($this->ar_join) > 0)
@@ -1536,7 +1748,7 @@ class CI_DB_active_record extends CI_DB_driver {
}
// ----------------------------------------------------------------
-
+
// Write the "WHERE" portion of the query
if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
@@ -1549,9 +1761,9 @@ class CI_DB_active_record extends CI_DB_driver {
$sql .= implode("\n", $this->ar_where);
// ----------------------------------------------------------------
-
+
// Write the "LIKE" portion of the query
-
+
if (count($this->ar_like) > 0)
{
if (count($this->ar_where) > 0)
@@ -1563,20 +1775,20 @@ class CI_DB_active_record extends CI_DB_driver {
}
// ----------------------------------------------------------------
-
+
// Write the "GROUP BY" portion of the query
-
+
if (count($this->ar_groupby) > 0)
{
$sql .= "\nGROUP BY ";
-
+
$sql .= implode(', ', $this->ar_groupby);
}
// ----------------------------------------------------------------
-
+
// Write the "HAVING" portion of the query
-
+
if (count($this->ar_having) > 0)
{
$sql .= "\nHAVING ";
@@ -1584,24 +1796,24 @@ class CI_DB_active_record extends CI_DB_driver {
}
// ----------------------------------------------------------------
-
+
// Write the "ORDER BY" portion of the query
if (count($this->ar_orderby) > 0)
{
$sql .= "\nORDER BY ";
$sql .= implode(', ', $this->ar_orderby);
-
+
if ($this->ar_order !== FALSE)
{
$sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
- }
+ }
}
// ----------------------------------------------------------------
-
+
// Write the "LIMIT" portion of the query
-
+
if (is_numeric($this->ar_limit))
{
$sql .= "\n";
@@ -1628,20 +1840,60 @@ class CI_DB_active_record extends CI_DB_driver {
{
return $object;
}
-
+
$array = array();
foreach (get_object_vars($object) as $key => $val)
{
// There are some built in keys we need to ignore for this conversion
- if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
+ if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
{
$array[$key] = $val;
}
}
-
+
+ return $array;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Object to Array
+ *
+ * Takes an object as input and converts the class variables to array key/vals
+ *
+ * @access public
+ * @param object
+ * @return array
+ */
+ function _object_to_array_batch($object)
+ {
+ if ( ! is_object($object))
+ {
+ return $object;
+ }
+
+ $array = array();
+ $out = get_object_vars($object);
+ $fields = array_keys($out);
+
+ foreach ($fields as $val)
+ {
+ // There are some built in keys we need to ignore for this conversion
+ if ($val != '_parent_name')
+ {
+
+ $i = 0;
+ foreach ($out[$val] as $data)
+ {
+ $array[$i][$val] = $data;
+ $i++;
+ }
+ }
+ }
+
return $array;
}
-
+
// --------------------------------------------------------------------
/**
@@ -1651,7 +1903,7 @@ class CI_DB_active_record extends CI_DB_driver {
*
* @access public
* @return void
- */
+ */
function start_cache()
{
$this->ar_caching = TRUE;
@@ -1666,7 +1918,7 @@ class CI_DB_active_record extends CI_DB_driver {
*
* @access public
* @return void
- */
+ */
function stop_cache()
{
$this->ar_caching = FALSE;
@@ -1681,23 +1933,23 @@ class CI_DB_active_record extends CI_DB_driver {
*
* @access public
* @return void
- */
+ */
function flush_cache()
- {
+ {
$this->_reset_run(
array(
- 'ar_cache_select' => array(),
- 'ar_cache_from' => array(),
+ 'ar_cache_select' => array(),
+ 'ar_cache_from' => array(),
'ar_cache_join' => array(),
- 'ar_cache_where' => array(),
- 'ar_cache_like' => array(),
- 'ar_cache_groupby' => array(),
- 'ar_cache_having' => array(),
- 'ar_cache_orderby' => array(),
+ 'ar_cache_where' => array(),
+ 'ar_cache_like' => array(),
+ 'ar_cache_groupby' => array(),
+ 'ar_cache_having' => array(),
+ 'ar_cache_orderby' => array(),
'ar_cache_set' => array(),
'ar_cache_exists' => array()
)
- );
+ );
}
// --------------------------------------------------------------------
@@ -1705,7 +1957,7 @@ class CI_DB_active_record extends CI_DB_driver {
/**
* Merge Cache
*
- * When called, this function merges any cached AR arrays with
+ * When called, this function merges any cached AR arrays with
* locally called ones.
*
* @access private
@@ -1770,50 +2022,51 @@ class CI_DB_active_record extends CI_DB_driver {
function _reset_select()
{
$ar_reset_items = array(
- 'ar_select' => array(),
- 'ar_from' => array(),
- 'ar_join' => array(),
- 'ar_where' => array(),
- 'ar_like' => array(),
- 'ar_groupby' => array(),
- 'ar_having' => array(),
- 'ar_orderby' => array(),
- 'ar_wherein' => array(),
+ 'ar_select' => array(),
+ 'ar_from' => array(),
+ 'ar_join' => array(),
+ 'ar_where' => array(),
+ 'ar_like' => array(),
+ 'ar_groupby' => array(),
+ 'ar_having' => array(),
+ 'ar_orderby' => array(),
+ 'ar_wherein' => array(),
'ar_aliased_tables' => array(),
- 'ar_distinct' => FALSE,
- 'ar_limit' => FALSE,
- 'ar_offset' => FALSE,
+ 'ar_distinct' => FALSE,
+ 'ar_limit' => FALSE,
+ 'ar_offset' => FALSE,
'ar_order' => FALSE,
);
-
+
$this->_reset_run($ar_reset_items);
}
-
+
// --------------------------------------------------------------------
/**
* Resets the active record "write" values.
*
- * Called by the insert() update() and delete() functions
+ * Called by the insert() update() insert_batch() update_batch() and delete() functions
*
* @access private
* @return void
*/
function _reset_write()
- {
+ {
$ar_reset_items = array(
- 'ar_set' => array(),
- 'ar_from' => array(),
- 'ar_where' => array(),
+ 'ar_set' => array(),
+ 'ar_from' => array(),
+ 'ar_where' => array(),
'ar_like' => array(),
- 'ar_orderby' => array(),
- 'ar_limit' => FALSE,
+ 'ar_orderby' => array(),
+ 'ar_keys' => array(),
+ 'ar_limit' => FALSE,
'ar_order' => FALSE
);
$this->_reset_run($ar_reset_items);
}
-
+
}
/* End of file DB_active_rec.php */