From 0c147b365a8bb2e584d4f957d4d0761f02bebe56 Mon Sep 17 00:00:00 2001 From: Kyle Farris Date: Fri, 26 Aug 2011 02:29:31 -0400 Subject: Added get_compiled_select(), get_compiled_insert(), get_compiled_update(), get_compiled_delete(), and reset_query() methods. to the Active Record class. --- system/database/DB_active_rec.php | 257 +++++++++++++++++++++++++++++++++----- 1 file changed, 223 insertions(+), 34 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 37d162bc1..8c801cd62 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -28,6 +28,9 @@ */ class CI_DB_active_record extends CI_DB_driver { + private $return_delete_sql = FALSE; + private $reset_delete_data = FALSE; + var $ar_select = array(); var $ar_distinct = FALSE; var $ar_from = array(); @@ -196,7 +199,7 @@ class CI_DB_active_record extends CI_DB_driver { $alias = $this->_create_alias_from_table(trim($select)); } - $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias)); + $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias; $this->ar_select[] = $sql; @@ -660,12 +663,8 @@ class CI_DB_active_record extends CI_DB_driver { $prefix = (count($this->ar_like) == 0) ? '' : $type; $v = $this->escape_like_str($v); - - if ($side == 'none') - { - $like_statement = $prefix." $k $not LIKE '{$v}'"; - } - elseif ($side == 'before') + + if ($side == 'before') { $like_statement = $prefix." $k $not LIKE '%{$v}'"; } @@ -874,11 +873,11 @@ class CI_DB_active_record extends CI_DB_driver { */ public function limit($value, $offset = '') { - $this->ar_limit = (int) $value; + $this->ar_limit = $value; if ($offset != '') { - $this->ar_offset = (int) $offset; + $this->ar_offset = $offset; } return $this; @@ -931,6 +930,38 @@ class CI_DB_active_record extends CI_DB_driver { return $this; } + + + // -------------------------------------------------------------------- + + /** + * Get SELECT query string + * + * Compiles a SELECT query string and returns the sql. + * + * @access public + * @param string the table name to select from (optional) + * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone + * @return string + */ + public function get_compiled_select($table = '', $reset = TRUE) + { + if ($table != '') + { + $this->_track_aliases($table); + $this->from($table); + } + + $select = $this->_compile_select(); + + if ($reset === TRUE) + { + $this->_reset_select(); + } + + return $select; + } + // -------------------------------------------------------------------- @@ -1148,6 +1179,36 @@ class CI_DB_active_record extends CI_DB_driver { return $this; } + + // -------------------------------------------------------------------- + + /** + * Get INSERT query string + * + * Compiles an insert query and returns the sql + * + * @access public + * @param string the table to insert into + * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @return string + */ + public function get_compiled_insert($table = '', $reset = TRUE) + { + if ($this->_validate_insert($table) === FALSE) + { + return FALSE; + } + + $sql = $this->_insert($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); + + if ($reset === TRUE) + { + $this->_reset_write(); + } + + return $sql; + } + // -------------------------------------------------------------------- @@ -1156,17 +1217,45 @@ class CI_DB_active_record extends CI_DB_driver { * * Compiles an insert string and runs the query * + * @access public * @param string the table to insert data into * @param array an associative array of insert values * @return object */ - function insert($table = '', $set = NULL) + public function insert($table = '', $set = NULL) { if ( ! is_null($set)) { $this->set($set); } + + if ($this->_validate_insert($table) === FALSE) + { + return FALSE; + } + + $sql = $this->_insert($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); + $this->_reset_write(); + return $this->query($sql); + } + + + // -------------------------------------------------------------------- + + /** + * Validate Insert + * + * This method is used by both insert() and get_compiled_insert() to + * validate that the there data is actually being set and that table + * has been chosen to be inserted into. + * + * @access public + * @param string the table to insert data into + * @return string + */ + protected function _validate_insert($table = '') + { if (count($this->ar_set) == 0) { if ($this->db_debug) @@ -1186,14 +1275,13 @@ 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); + else + { + $this->ar_from[0] = $table; + } + + return TRUE; } // -------------------------------------------------------------------- @@ -1242,7 +1330,41 @@ class CI_DB_active_record extends CI_DB_driver { $this->_reset_write(); return $this->query($sql); } + + + // -------------------------------------------------------------------- + /** + * Get UPDATE query string + * + * Compiles an update query and returns the sql + * + * @access public + * @param string the table to update + * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @return string + */ + public function get_compiled_update($table = '', $reset = TRUE) + { + // Combine any cached components with the current statements + $this->_merge_cache(); + + if ($this->_validate_update($table) === FALSE) + { + return FALSE; + } + + $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); + + if ($reset === TRUE) + { + $this->_reset_write(); + } + + return $sql; + } + + // -------------------------------------------------------------------- /** @@ -1265,6 +1387,43 @@ class CI_DB_active_record extends CI_DB_driver { $this->set($set); } + if ($this->_validate_update($table) === FALSE) + { + return FALSE; + } + + if ($where != NULL) + { + $this->where($where); + } + + if ($limit != NULL) + { + $this->limit($limit); + } + + $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); + + $this->_reset_write(); + return $this->query($sql); + } + + + // -------------------------------------------------------------------- + + /** + * Validate Update + * + * This method is used by both update() and get_compiled_update() to + * validate that data is actually being set and that a table has been + * chosen to be update. + * + * @access public + * @param string the table to update data on + * @return string + */ + protected function _validate_update($table = '') + { if (count($this->ar_set) == 0) { if ($this->db_debug) @@ -1284,24 +1443,11 @@ class CI_DB_active_record extends CI_DB_driver { } return FALSE; } - - $table = $this->ar_from[0]; } - - if ($where != NULL) - { - $this->where($where); - } - - if ($limit != NULL) + else { - $this->limit($limit); + $this->ar_from[0] = $table; } - - $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); } @@ -1326,7 +1472,7 @@ class CI_DB_active_record extends CI_DB_driver { { if ($this->db_debug) { - return $this->display_error('db_must_use_index'); + return $this->display_error('db_myst_use_index'); } return FALSE; @@ -1503,7 +1649,27 @@ class CI_DB_active_record extends CI_DB_driver { return $this->query($sql); } + + // -------------------------------------------------------------------- + /** + * Get DELETE query string + * + * Compiles a delete query string and returns the sql + * + * @access public + * @param string the table to delete from + * @param boolean TRUE: reset AR values; FALSE: leave AR values alone + * @return string + */ + public function get_compiled_delete($table = '', $reset = TRUE) + { + $this->return_delete_sql = TRUE; + $sql = $this->delete($table, '', NULL, $reset); + $this->return_delete_sql = FALSE; + return $sql; + } + // -------------------------------------------------------------------- /** @@ -1576,9 +1742,15 @@ class CI_DB_active_record extends CI_DB_driver { { $this->_reset_write(); } + + if ($this->return_delete_sql === true) + { + return $sql; + } return $this->query($sql); } + // -------------------------------------------------------------------- @@ -1659,6 +1831,7 @@ class CI_DB_active_record extends CI_DB_driver { } } } + // -------------------------------------------------------------------- @@ -1965,6 +2138,22 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_no_escape = $this->ar_cache_no_escape; } + + // -------------------------------------------------------------------- + + /** + * Reset Active Record values. + * + * Publicly-visible method to reset the AR values. + * + * @access public + * @return void + */ + public function reset_query() + { + $this->_reset_select(); + $this->_reset_write(); + } // -------------------------------------------------------------------- @@ -2042,4 +2231,4 @@ class CI_DB_active_record extends CI_DB_driver { } /* End of file DB_active_rec.php */ -/* Location: ./system/database/DB_active_rec.php */ \ No newline at end of file +/* Location: ./system/database/DB_active_rec.php */ -- cgit v1.2.3-24-g4f1b From 0a3176b56ad069643c400302b1baf9a2c90267ad Mon Sep 17 00:00:00 2001 From: kylefarris Date: Fri, 26 Aug 2011 02:37:52 -0400 Subject: Small formatting fix. --- system/database/DB_active_rec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 8c801cd62..c36e20348 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -947,7 +947,7 @@ class CI_DB_active_record extends CI_DB_driver { public function get_compiled_select($table = '', $reset = TRUE) { if ($table != '') - { + { $this->_track_aliases($table); $this->from($table); } -- cgit v1.2.3-24-g4f1b From 7611601875b619d8201633cf16a790b356182039 Mon Sep 17 00:00:00 2001 From: Kyle Farris Date: Wed, 31 Aug 2011 11:17:48 -0400 Subject: Fixed some items based on code comments by gaker. --- system/database/DB_active_rec.php | 96 ++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 47 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index c36e20348..46202224b 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -28,42 +28,42 @@ */ class CI_DB_active_record extends CI_DB_driver { - private $return_delete_sql = FALSE; - private $reset_delete_data = FALSE; + protected $return_delete_sql = FALSE; + protected $reset_delete_data = FALSE; - var $ar_select = array(); - var $ar_distinct = FALSE; - var $ar_from = array(); - var $ar_join = array(); - var $ar_where = array(); - 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_wherein = array(); - var $ar_aliased_tables = array(); - var $ar_store_array = array(); + protected $ar_select = array(); + protected $ar_distinct = FALSE; + protected $ar_from = array(); + protected $ar_join = array(); + protected $ar_where = array(); + protected $ar_like = array(); + protected $ar_groupby = array(); + protected $ar_having = array(); + protected $ar_keys = array(); + protected $ar_limit = FALSE; + protected $ar_offset = FALSE; + protected $ar_order = FALSE; + protected $ar_orderby = array(); + protected $ar_set = array(); + protected $ar_wherein = array(); + protected $ar_aliased_tables = array(); + protected $ar_store_array = array(); // Active Record Caching variables - var $ar_caching = FALSE; - var $ar_cache_exists = array(); - var $ar_cache_select = array(); - var $ar_cache_from = array(); - var $ar_cache_join = array(); - var $ar_cache_where = array(); - var $ar_cache_like = array(); - var $ar_cache_groupby = array(); - var $ar_cache_having = array(); - var $ar_cache_orderby = array(); - var $ar_cache_set = array(); + protected $ar_caching = FALSE; + protected $ar_cache_exists = array(); + protected $ar_cache_select = array(); + protected $ar_cache_from = array(); + protected $ar_cache_join = array(); + protected $ar_cache_where = array(); + protected $ar_cache_like = array(); + protected $ar_cache_groupby = array(); + protected $ar_cache_having = array(); + protected $ar_cache_orderby = array(); + protected $ar_cache_set = array(); - var $ar_no_escape = array(); - var $ar_cache_no_escape = array(); + protected $ar_no_escape = array(); + protected $ar_cache_no_escape = array(); // -------------------------------------------------------------------- @@ -873,11 +873,11 @@ class CI_DB_active_record extends CI_DB_driver { */ public function limit($value, $offset = '') { - $this->ar_limit = $value; + $this->ar_limit = (int) $value; if ($offset != '') { - $this->ar_offset = $offset; + $this->ar_offset = (int) $offset; } return $this; @@ -931,7 +931,6 @@ class CI_DB_active_record extends CI_DB_driver { return $this; } - // -------------------------------------------------------------------- /** @@ -962,7 +961,6 @@ class CI_DB_active_record extends CI_DB_driver { return $select; } - // -------------------------------------------------------------------- /** @@ -1199,7 +1197,13 @@ class CI_DB_active_record extends CI_DB_driver { return FALSE; } - $sql = $this->_insert($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); + $sql = $this->_insert( + $this->_protect_identifiers( + $this->ar_from[0], TRUE, NULL, FALSE + ), + array_keys($this->ar_set), + array_values($this->ar_set) + ); if ($reset === TRUE) { @@ -1208,7 +1212,6 @@ class CI_DB_active_record extends CI_DB_driver { return $sql; } - // -------------------------------------------------------------------- @@ -1234,13 +1237,18 @@ class CI_DB_active_record extends CI_DB_driver { return FALSE; } - $sql = $this->_insert($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); + $sql = $this->_insert( + $this->_protect_identifiers( + $this->ar_from[0], TRUE, NULL, FALSE + ), + array_keys($this->ar_set), + array_values($this->ar_set) + ); $this->_reset_write(); return $this->query($sql); } - // -------------------------------------------------------------------- /** @@ -1331,7 +1339,6 @@ class CI_DB_active_record extends CI_DB_driver { return $this->query($sql); } - // -------------------------------------------------------------------- /** @@ -1364,7 +1371,6 @@ class CI_DB_active_record extends CI_DB_driver { return $sql; } - // -------------------------------------------------------------------- /** @@ -1407,7 +1413,6 @@ class CI_DB_active_record extends CI_DB_driver { $this->_reset_write(); return $this->query($sql); } - // -------------------------------------------------------------------- @@ -1449,8 +1454,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_from[0] = $table; } } - - + // -------------------------------------------------------------------- /** @@ -1751,7 +1755,6 @@ class CI_DB_active_record extends CI_DB_driver { return $this->query($sql); } - // -------------------------------------------------------------------- /** @@ -1832,7 +1835,6 @@ class CI_DB_active_record extends CI_DB_driver { } } - // -------------------------------------------------------------------- /** -- cgit v1.2.3-24-g4f1b From 2de2fa022253597c8f5c807218be3aa05fac340e Mon Sep 17 00:00:00 2001 From: Kyle Farris Date: Wed, 31 Aug 2011 11:52:20 -0400 Subject: Merged changes with development. --- system/database/DB_active_rec.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 46202224b..076cc7ce4 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -199,7 +199,7 @@ class CI_DB_active_record extends CI_DB_driver { $alias = $this->_create_alias_from_table(trim($select)); } - $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias; + $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias)); $this->ar_select[] = $sql; @@ -664,7 +664,11 @@ class CI_DB_active_record extends CI_DB_driver { $v = $this->escape_like_str($v); - if ($side == 'before') + if ($side == 'none') + { + $like_statement = $prefix." $k $not LIKE '{$v}'"; + } + elseif ($side == 'before') { $like_statement = $prefix." $k $not LIKE '%{$v}'"; } @@ -1476,7 +1480,7 @@ class CI_DB_active_record extends CI_DB_driver { { if ($this->db_debug) { - return $this->display_error('db_myst_use_index'); + return $this->display_error('db_must_use_index'); } return FALSE; @@ -2233,4 +2237,4 @@ class CI_DB_active_record extends CI_DB_driver { } /* End of file DB_active_rec.php */ -/* Location: ./system/database/DB_active_rec.php */ +/* Location: ./system/database/DB_active_rec.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 81ef70f32951de5f7b4196c1db9ce969133362dd Mon Sep 17 00:00:00 2001 From: Kyle Farris Date: Wed, 31 Aug 2011 11:59:12 -0400 Subject: One more formatting fix. --- system/database/DB_active_rec.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 076cc7ce4..59cd1972c 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -664,11 +664,11 @@ class CI_DB_active_record extends CI_DB_driver { $v = $this->escape_like_str($v); - if ($side == 'none') - { - $like_statement = $prefix." $k $not LIKE '{$v}'"; - } - elseif ($side == 'before') + if ($side == 'none') + { + $like_statement = $prefix." $k $not LIKE '{$v}'"; + } + elseif ($side == 'before') { $like_statement = $prefix." $k $not LIKE '%{$v}'"; } -- cgit v1.2.3-24-g4f1b From 8b4d83b23b3b93e8042b01d9117f496206b309c0 Mon Sep 17 00:00:00 2001 From: Juan José González Date: Tue, 27 Sep 2011 17:21:14 -0500 Subject: Fixing issue 465: select_max is adding prefix to table aliases when is not necessary --- system/database/DB_active_rec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 7162e2ac5..83518232e 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -196,7 +196,7 @@ class CI_DB_active_record extends CI_DB_driver { $alias = $this->_create_alias_from_table(trim($select)); } - $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias)); + $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias)); $this->ar_select[] = $sql; -- cgit v1.2.3-24-g4f1b From f4a4bd8fac188ebc9cda822ffc811c218fd92b45 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Thu, 20 Oct 2011 12:18:42 -0500 Subject: adding new license file (OSL 3.0) and updating readme to ReST added notice of license to all source files. OSL to all except the few files we ship inside of the application folder, those are AFL. Updated license in user guide. incrementing next dev version to 3.0 due to licensing change --- system/database/DB_active_rec.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index f99d13ec8..43920772a 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -4,10 +4,22 @@ * * An open source application development framework for PHP 5.1.6 or newer * + * NOTICE OF LICENSE + * + * Licensed under the Open Software License version 3.0 + * + * This source file is subject to the Open Software License (OSL 3.0) that is + * bundled with this package in the files license.txt / license.rst. It is + * also available through the world wide web at this URL: + * http://opensource.org/licenses/OSL-3.0 + * If you did not receive a copy of the license and are unable to obtain it + * through the world wide web, please send an email to + * licensing@ellislab.com so we can send you a copy immediately. + * * @package CodeIgniter - * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. - * @license http://codeigniter.com/user_guide/license.html + * @author EllisLab Dev Team + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) + * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 * @filesource @@ -23,7 +35,7 @@ * @package CodeIgniter * @subpackage Drivers * @category Database - * @author ExpressionEngine Dev Team + * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/database/ */ class CI_DB_active_record extends CI_DB_driver { -- cgit v1.2.3-24-g4f1b From c4caf5a181344e48fc6740f1383dd302f5b604d2 Mon Sep 17 00:00:00 2001 From: Mancy Date: Tue, 20 Dec 2011 11:47:51 +0300 Subject: taking care of LIKE when used in UPDATE statement #798 --- system/database/DB_active_rec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 43920772a..7a608c4f0 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -1424,7 +1424,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->limit($limit); } - $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); + $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, , $this->ar_like); $this->_reset_write(); return $this->query($sql); -- cgit v1.2.3-24-g4f1b From 0d91fd2956812e07905d8047ef95a0e556edbb1a Mon Sep 17 00:00:00 2001 From: Mancy Date: Tue, 20 Dec 2011 13:13:14 +0300 Subject: #798: update changelog and typo fix --- system/database/DB_active_rec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 7a608c4f0..41950e7d8 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -1424,7 +1424,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->limit($limit); } - $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, , $this->ar_like); + $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, $this->ar_like); $this->_reset_write(); return $this->query($sql); -- cgit v1.2.3-24-g4f1b From 2c685fb03a4d4b5a96789b839147191ce8cffacc Mon Sep 17 00:00:00 2001 From: pporlan Date: Thu, 22 Dec 2011 12:15:25 +0100 Subject: Adding $escape parameter to the order_by function, this enables ordering by custom fields --- system/database/DB_active_rec.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 41950e7d8..412febfcc 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -830,9 +830,10 @@ class CI_DB_active_record extends CI_DB_driver { * * @param string * @param string direction: asc or desc + * @param bool enable field name escaping * @return object */ - public function order_by($orderby, $direction = '') + public function order_by($orderby, $direction = '', $escape = TRUE) { if (strtolower($direction) == 'random') { @@ -845,7 +846,7 @@ class CI_DB_active_record extends CI_DB_driver { } - if (strpos($orderby, ',') !== FALSE) + if ((strpos($orderby, ',') !== FALSE) && ($escape === TRUE)) { $temp = array(); foreach (explode(',', $orderby) as $part) @@ -863,7 +864,10 @@ class CI_DB_active_record extends CI_DB_driver { } else if ($direction != $this->_random_keyword) { - $orderby = $this->_protect_identifiers($orderby); + if ($escape === TRUE) + { + $orderby = $this->_protect_identifiers($orderby); + } } $orderby_statement = $orderby.$direction; -- cgit v1.2.3-24-g4f1b From 03abee3df4534028c795e3c3da91034a3d3ee0f4 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Sun, 25 Dec 2011 00:31:29 -0600 Subject: Fixing soft tabs in a few files. --- system/database/DB_active_rec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 412febfcc..530b44e09 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -75,7 +75,7 @@ class CI_DB_active_record extends CI_DB_driver { protected $ar_cache_set = array(); protected $ar_no_escape = array(); - protected $ar_cache_no_escape = array(); + protected $ar_cache_no_escape = array(); // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 7219c07e9e6be839cbf9931a17d587ef4b2895b1 Mon Sep 17 00:00:00 2001 From: WanWizard Date: Wed, 28 Dec 2011 14:09:05 +0100 Subject: added query grouping to Active Record this is a feature that has been lacking for a very long time. lots of people complained about it over the years, but it never got added so you'd have to resort to handcrafted queries when you needed this feature. This is a port of code from DataMapper, in use since CI 1.6. --- system/database/DB_active_rec.php | 258 +++++++++++++++++++++++++++----------- 1 file changed, 186 insertions(+), 72 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 530b44e09..b7f43945c 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -5,9 +5,9 @@ * An open source application development framework for PHP 5.1.6 or newer * * NOTICE OF LICENSE - * + * * Licensed under the Open Software License version 3.0 - * + * * This source file is subject to the Open Software License (OSL 3.0) that is * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: @@ -40,42 +40,44 @@ */ class CI_DB_active_record extends CI_DB_driver { - protected $return_delete_sql = FALSE; - protected $reset_delete_data = FALSE; - - protected $ar_select = array(); - protected $ar_distinct = FALSE; - protected $ar_from = array(); - protected $ar_join = array(); - protected $ar_where = array(); - protected $ar_like = array(); - protected $ar_groupby = array(); - protected $ar_having = array(); - protected $ar_keys = array(); - protected $ar_limit = FALSE; - protected $ar_offset = FALSE; - protected $ar_order = FALSE; - protected $ar_orderby = array(); - protected $ar_set = array(); - protected $ar_wherein = array(); - protected $ar_aliased_tables = array(); - protected $ar_store_array = array(); + protected $return_delete_sql = FALSE; + protected $reset_delete_data = FALSE; + + protected $ar_select = array(); + protected $ar_distinct = FALSE; + protected $ar_from = array(); + protected $ar_join = array(); + protected $ar_where = array(); + protected $ar_like = array(); + protected $ar_groupby = array(); + protected $ar_having = array(); + protected $ar_keys = array(); + protected $ar_limit = FALSE; + protected $ar_offset = FALSE; + protected $ar_order = FALSE; + protected $ar_orderby = array(); + protected $ar_set = array(); + protected $ar_wherein = array(); + protected $ar_aliased_tables = array(); + protected $ar_store_array = array(); + protected $ar_where_group_started = FALSE; + protected $ar_where_group_count = 0; // Active Record Caching variables - protected $ar_caching = FALSE; - protected $ar_cache_exists = array(); - protected $ar_cache_select = array(); - protected $ar_cache_from = array(); - protected $ar_cache_join = array(); - protected $ar_cache_where = array(); - protected $ar_cache_like = array(); - protected $ar_cache_groupby = array(); - protected $ar_cache_having = array(); - protected $ar_cache_orderby = array(); - protected $ar_cache_set = array(); - - protected $ar_no_escape = array(); - protected $ar_cache_no_escape = array(); + protected $ar_caching = FALSE; + protected $ar_cache_exists = array(); + protected $ar_cache_select = array(); + protected $ar_cache_from = array(); + protected $ar_cache_join = array(); + protected $ar_cache_where = array(); + protected $ar_cache_like = array(); + protected $ar_cache_groupby = array(); + protected $ar_cache_having = array(); + protected $ar_cache_orderby = array(); + protected $ar_cache_set = array(); + + protected $ar_no_escape = array(); + protected $ar_cache_no_escape = array(); // -------------------------------------------------------------------- @@ -412,6 +414,8 @@ class CI_DB_active_record extends CI_DB_driver { */ protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) { + $type = $this->_group_get_type($type); + if ( ! is_array($key)) { $key = array($key => $value); @@ -441,7 +445,7 @@ class CI_DB_active_record extends CI_DB_driver { $v = ' '.$this->escape($v); } - + if ( ! $this->_has_operator($k)) { $k .= ' = '; @@ -553,6 +557,8 @@ class CI_DB_active_record extends CI_DB_driver { return; } + $type = $this->_group_get_type($type); + if ( ! is_array($values)) { $values = array($values); @@ -663,6 +669,8 @@ class CI_DB_active_record extends CI_DB_driver { */ protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') { + $type = $this->_group_get_type($type); + if ( ! is_array($field)) { $field = array($field => $match); @@ -712,6 +720,112 @@ class CI_DB_active_record extends CI_DB_driver { // -------------------------------------------------------------------- + /** + * Starts a query group. + * + * @param string (Internal use only) + * @param string (Internal use only) + * @return object + */ + public function group_start($not = '', $type = 'AND ') + { + $type = $this->_group_get_type($type); + + $this->ar_where_group_started = TRUE; + + $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type; + $value = $prefix . $not . str_repeat(' ', ++$this->ar_where_group_count) . ' ('; + + $this->ar_where[] = $value; + if ($this->ar_caching) + { + $this->ar_cache_where[] = $value; + } + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Starts a query group, but ORs the group + * + * @return object + */ + public function or_group_start() + { + return $this->group_start('', 'OR '); + } + + // -------------------------------------------------------------------- + + /** + * Starts a query group, but NOTs the group + * + * @return object + */ + public function not_group_start() + { + return $this->group_start('NOT ', 'AND '); + } + + // -------------------------------------------------------------------- + + /** + * Starts a query group, but OR NOTs the group + * + * @return object + */ + public function or_not_group_start() + { + return $this->group_start('NOT ', 'OR '); + } + + // -------------------------------------------------------------------- + + /** + * Ends a query group + * + * @return object + */ + public function group_end() + { + $value = str_repeat(' ', $this->ar_where_group_count--) . ')'; + + $this->ar_where[] = $value; + if ($this->ar_caching) + { + $this->ar_cache_where[] = $value; + } + + $this->ar_where_group_started = FALSE; + + return $this; + } + + // -------------------------------------------------------------------- + + /** + * Group_get_type + * + * Called by group_start(), _like(), _where() and _where_in() + * + * @param string + * @return string + */ + protected function _group_get_type($type) + { + if ($this->ar_where_group_started) + { + $type = ''; + $this->ar_where_group_started = FALSE; + } + + return $type; + } + + // -------------------------------------------------------------------- + /** * GROUP BY * @@ -950,7 +1064,7 @@ class CI_DB_active_record extends CI_DB_driver { return $this; } - + // -------------------------------------------------------------------- /** @@ -963,24 +1077,24 @@ class CI_DB_active_record extends CI_DB_driver { * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone * @return string */ - public function get_compiled_select($table = '', $reset = TRUE) + public function get_compiled_select($table = '', $reset = TRUE) { if ($table != '') { $this->_track_aliases($table); $this->from($table); } - + $select = $this->_compile_select(); - + if ($reset === TRUE) { $this->_reset_select(); } - + return $select; } - + // -------------------------------------------------------------------- /** @@ -1197,7 +1311,7 @@ class CI_DB_active_record extends CI_DB_driver { return $this; } - + // -------------------------------------------------------------------- /** @@ -1211,25 +1325,25 @@ class CI_DB_active_record extends CI_DB_driver { * @return string */ public function get_compiled_insert($table = '', $reset = TRUE) - { + { if ($this->_validate_insert($table) === FALSE) { return FALSE; } - + $sql = $this->_insert( $this->_protect_identifiers( $this->ar_from[0], TRUE, NULL, FALSE ), - array_keys($this->ar_set), + array_keys($this->ar_set), array_values($this->ar_set) ); - + if ($reset === TRUE) { $this->_reset_write(); } - + return $sql; } @@ -1251,24 +1365,24 @@ class CI_DB_active_record extends CI_DB_driver { { $this->set($set); } - + if ($this->_validate_insert($table) === FALSE) { return FALSE; } - + $sql = $this->_insert( $this->_protect_identifiers( $this->ar_from[0], TRUE, NULL, FALSE - ), - array_keys($this->ar_set), + ), + array_keys($this->ar_set), array_values($this->ar_set) ); $this->_reset_write(); return $this->query($sql); } - + // -------------------------------------------------------------------- /** @@ -1282,7 +1396,7 @@ class CI_DB_active_record extends CI_DB_driver { * @param string the table to insert data into * @return string */ - protected function _validate_insert($table = '') + protected function _validate_insert($table = '') { if (count($this->ar_set) == 0) { @@ -1308,7 +1422,7 @@ class CI_DB_active_record extends CI_DB_driver { { $this->ar_from[0] = $table; } - + return TRUE; } @@ -1358,7 +1472,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->_reset_write(); return $this->query($sql); } - + // -------------------------------------------------------------------- /** @@ -1375,22 +1489,22 @@ class CI_DB_active_record extends CI_DB_driver { { // Combine any cached components with the current statements $this->_merge_cache(); - + if ($this->_validate_update($table) === FALSE) { return FALSE; } - + $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); - + if ($reset === TRUE) { $this->_reset_write(); } - + return $sql; } - + // -------------------------------------------------------------------- /** @@ -1433,7 +1547,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->_reset_write(); return $this->query($sql); } - + // -------------------------------------------------------------------- /** @@ -1474,7 +1588,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_from[0] = $table; } } - + // -------------------------------------------------------------------- /** @@ -1673,7 +1787,7 @@ class CI_DB_active_record extends CI_DB_driver { return $this->query($sql); } - + // -------------------------------------------------------------------- /** @@ -1693,7 +1807,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->return_delete_sql = FALSE; return $sql; } - + // -------------------------------------------------------------------- /** @@ -1766,7 +1880,7 @@ class CI_DB_active_record extends CI_DB_driver { { $this->_reset_write(); } - + if ($this->return_delete_sql === true) { return $sql; @@ -1774,7 +1888,7 @@ class CI_DB_active_record extends CI_DB_driver { return $this->query($sql); } - + // -------------------------------------------------------------------- /** @@ -1854,7 +1968,7 @@ class CI_DB_active_record extends CI_DB_driver { } } } - + // -------------------------------------------------------------------- /** @@ -2160,12 +2274,12 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_no_escape = $this->ar_cache_no_escape; } - + // -------------------------------------------------------------------- /** * Reset Active Record values. - * + * * Publicly-visible method to reset the AR values. * * @access public @@ -2253,4 +2367,4 @@ class CI_DB_active_record extends CI_DB_driver { } /* End of file DB_active_rec.php */ -/* Location: ./system/database/DB_active_rec.php */ \ No newline at end of file +/* Location: ./system/database/DB_active_rec.php */ -- cgit v1.2.3-24-g4f1b From 0defe5d33ee2633f377a109519ca818becc60f64 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Sun, 1 Jan 2012 18:46:41 -0600 Subject: Updating copyright date to 2012 --- system/database/DB_active_rec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index b7f43945c..486b4d775 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -18,7 +18,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 24276a3a204ddf5947c66bd74f183d8058c1171e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 8 Jan 2012 02:44:38 +0200 Subject: Improve database classes --- system/database/DB_active_rec.php | 404 ++++++++++++-------------------------- 1 file changed, 126 insertions(+), 278 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 486b4d775..30a17d11a 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -1,4 +1,4 @@ -_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias)); - $this->ar_select[] = $sql; if ($this->ar_caching === TRUE) @@ -280,30 +278,27 @@ class CI_DB_active_record extends CI_DB_driver { { $v = trim($v); $this->_track_aliases($v); - - $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); + $this->ar_from[] = $v = $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_from[] = $v; $this->ar_cache_exists[] = 'from'; } } - } else { $val = trim($val); - // Extract any aliases that might exist. We use this information + // Extract any aliases that might exist. We use this information // 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); + $this->ar_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE); if ($this->ar_caching === TRUE) { - $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE); + $this->ar_cache_from[] = $val; $this->ar_cache_exists[] = 'from'; } } @@ -340,23 +335,19 @@ class CI_DB_active_record extends CI_DB_driver { } } - // Extract any aliases that might exist. We use this information + // Extract any aliases that might exist. We use this information // in the _protect_identifiers to know whether to add a table prefix $this->_track_aliases($table); // Strip apart the condition and protect the identifiers if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match)) { - $match[1] = $this->_protect_identifiers($match[1]); - $match[3] = $this->_protect_identifiers($match[3]); - - $cond = $match[1].$match[2].$match[3]; + $cond = $this->_protect_identifiers($match[1]).$match[2].$this->_protect_identifiers($match[3]); } // Assemble the JOIN statement - $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; + $this->ar_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; - $this->ar_join[] = $join; if ($this->ar_caching === TRUE) { $this->ar_cache_join[] = $join; @@ -429,7 +420,7 @@ class CI_DB_active_record extends CI_DB_driver { foreach ($key as $k => $v) { - $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type; + $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; if (is_null($v) && ! $this->_has_operator($k)) { @@ -442,7 +433,6 @@ class CI_DB_active_record extends CI_DB_driver { if ($escape === TRUE) { $k = $this->_protect_identifiers($k, FALSE, $escape); - $v = ' '.$this->escape($v); } @@ -457,7 +447,6 @@ class CI_DB_active_record extends CI_DB_driver { } $this->ar_where[] = $prefix.$k.$v; - if ($this->ar_caching === TRUE) { $this->ar_cache_where[] = $prefix.$k.$v; @@ -571,11 +560,9 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_wherein[] = $this->escape($value); } - $prefix = (count($this->ar_where) == 0) ? '' : $type; - - $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") "; + $prefix = (count($this->ar_where) === 0) ? '' : $type; + $this->ar_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->ar_wherein).') '; - $this->ar_where[] = $where_in; if ($this->ar_caching === TRUE) { $this->ar_cache_where[] = $where_in; @@ -679,20 +666,18 @@ class CI_DB_active_record extends CI_DB_driver { foreach ($field as $k => $v) { $k = $this->_protect_identifiers($k); - - $prefix = (count($this->ar_like) == 0) ? '' : $type; - + $prefix = (count($this->ar_like) === 0) ? '' : $type; $v = $this->escape_like_str($v); - if ($side == 'none') + if ($side === 'none') { $like_statement = $prefix." $k $not LIKE '{$v}'"; } - elseif ($side == 'before') + elseif ($side === 'before') { $like_statement = $prefix." $k $not LIKE '%{$v}'"; } - elseif ($side == 'after') + elseif ($side === 'after') { $like_statement = $prefix." $k $not LIKE '{$v}%'"; } @@ -715,6 +700,7 @@ class CI_DB_active_record extends CI_DB_driver { } } + return $this; } @@ -730,13 +716,10 @@ class CI_DB_active_record extends CI_DB_driver { public function group_start($not = '', $type = 'AND ') { $type = $this->_group_get_type($type); - $this->ar_where_group_started = TRUE; + $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; + $this->ar_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->ar_where_group_count).' ('; - $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type; - $value = $prefix . $not . str_repeat(' ', ++$this->ar_where_group_count) . ' ('; - - $this->ar_where[] = $value; if ($this->ar_caching) { $this->ar_cache_where[] = $value; @@ -790,16 +773,14 @@ class CI_DB_active_record extends CI_DB_driver { */ public function group_end() { - $value = str_repeat(' ', $this->ar_where_group_count--) . ')'; + $this->ar_where_group_started = FALSE; + $this->ar_where[] = $value = str_repeat(' ', $this->ar_where_group_count--) . ')'; - $this->ar_where[] = $value; if ($this->ar_caching) { $this->ar_cache_where[] = $value; } - $this->ar_where_group_started = FALSE; - return $this; } @@ -845,15 +826,16 @@ class CI_DB_active_record extends CI_DB_driver { if ($val != '') { - $this->ar_groupby[] = $this->_protect_identifiers($val); + $this->ar_groupby[] = $val = $this->_protect_identifiers($val); if ($this->ar_caching === TRUE) { - $this->ar_cache_groupby[] = $this->_protect_identifiers($val); + $this->ar_cache_groupby[] = $val; $this->ar_cache_exists[] = 'groupby'; } } } + return $this; } @@ -909,7 +891,7 @@ class CI_DB_active_record extends CI_DB_driver { foreach ($key as $k => $v) { - $prefix = (count($this->ar_having) == 0) ? '' : $type; + $prefix = (count($this->ar_having) === 0) ? '' : $type; if ($escape === TRUE) { @@ -949,7 +931,7 @@ class CI_DB_active_record extends CI_DB_driver { */ public function order_by($orderby, $direction = '', $escape = TRUE) { - if (strtolower($direction) == 'random') + if (strtolower($direction) === 'random') { $orderby = ''; // Random results want or don't need a field name $direction = $this->_random_keyword; @@ -960,7 +942,7 @@ class CI_DB_active_record extends CI_DB_driver { } - if ((strpos($orderby, ',') !== FALSE) && ($escape === TRUE)) + if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE) { $temp = array(); foreach (explode(',', $orderby) as $part) @@ -976,7 +958,7 @@ class CI_DB_active_record extends CI_DB_driver { $orderby = implode(', ', $temp); } - else if ($direction != $this->_random_keyword) + elseif ($direction != $this->_random_keyword) { if ($escape === TRUE) { @@ -984,9 +966,8 @@ class CI_DB_active_record extends CI_DB_driver { } } - $orderby_statement = $orderby.$direction; + $this->ar_orderby[] = $orderby_statement = $orderby.$direction; - $this->ar_orderby[] = $orderby_statement; if ($this->ar_caching === TRUE) { $this->ar_cache_orderby[] = $orderby_statement; @@ -1121,9 +1102,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->limit($limit, $offset); } - $sql = $this->_compile_select(); - - $result = $this->query($sql); + $result = $this->query($this->_compile_select()); $this->_reset_select(); return $result; } @@ -1145,12 +1124,10 @@ class CI_DB_active_record extends CI_DB_driver { $this->from($table); } - $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows')); - - $query = $this->query($sql); + $result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows'))); $this->_reset_select(); - if ($query->num_rows() == 0) + if ($query->num_rows() === 0) { return 0; } @@ -1188,9 +1165,7 @@ class CI_DB_active_record extends CI_DB_driver { $this->limit($limit, $offset); } - $sql = $this->_compile_select(); - - $result = $this->query($sql); + $result = $this->query($this->_compile_select()); $this->_reset_select(); return $result; } @@ -1213,11 +1188,11 @@ class CI_DB_active_record extends CI_DB_driver { $this->set_insert_batch($set); } - if (count($this->ar_set) == 0) + 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 + // 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; @@ -1227,30 +1202,19 @@ class CI_DB_active_record extends CI_DB_driver { { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; } $table = $this->ar_from[0]; } // Batch this baby - for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) + for ($i = 0, $total = count($this->ar_set); $i < $total; $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->query($this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100))); } $this->_reset_write(); - - return TRUE; } @@ -1294,7 +1258,6 @@ class CI_DB_active_record extends CI_DB_driver { else { $clean = array(); - foreach ($row as $value) { $clean[] = $this->escape($value); @@ -1398,24 +1361,16 @@ class CI_DB_active_record extends CI_DB_driver { */ protected function _validate_insert($table = '') { - if (count($this->ar_set) == 0) + if (count($this->ar_set) === 0) { - if ($this->db_debug) - { - return $this->display_error('db_must_use_set'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; } if ($table == '') { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; } } else @@ -1444,31 +1399,22 @@ class CI_DB_active_record extends CI_DB_driver { $this->set($set); } - if (count($this->ar_set) == 0) + if (count($this->ar_set) === 0) { - if ($this->db_debug) - { - return $this->display_error('db_must_use_set'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; } if ($table == '') { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : 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); } @@ -1543,7 +1489,6 @@ class CI_DB_active_record extends CI_DB_driver { } $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, $this->ar_like); - $this->_reset_write(); return $this->query($sql); } @@ -1559,34 +1504,28 @@ class CI_DB_active_record extends CI_DB_driver { * * @access public * @param string the table to update data on - * @return string + * @return bool */ protected function _validate_update($table = '') { if (count($this->ar_set) == 0) { - if ($this->db_debug) - { - return $this->display_error('db_must_use_set'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; } if ($table == '') { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; } } else { $this->ar_from[0] = $table; } + + return TRUE; } // -------------------------------------------------------------------- @@ -1599,7 +1538,7 @@ class CI_DB_active_record extends CI_DB_driver { * @param string the table to retrieve the results from * @param array an associative array of update values * @param string the where key - * @return object + * @return bool */ public function update_batch($table = '', $set = NULL, $index = NULL) { @@ -1608,12 +1547,7 @@ class CI_DB_active_record extends CI_DB_driver { if (is_null($index)) { - if ($this->db_debug) - { - return $this->display_error('db_must_use_index'); - } - - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE; } if ( ! is_null($set)) @@ -1621,39 +1555,29 @@ class CI_DB_active_record extends CI_DB_driver { $this->set_update_batch($set, $index); } - if (count($this->ar_set) == 0) + if (count($this->ar_set) === 0) { - if ($this->db_debug) - { - return $this->display_error('db_must_use_set'); - } - - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; } if ($table == '') { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; } $table = $this->ar_from[0]; } // Batch this baby - for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) + for ($i = 0, $total = count($this->ar_set); $i < $total; $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->query($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->_reset_write(); + return TRUE; } // -------------------------------------------------------------------- @@ -1679,7 +1603,6 @@ class CI_DB_active_record extends CI_DB_driver { { $index_set = FALSE; $clean = array(); - foreach ($v as $k2 => $v2) { if ($k2 == $index) @@ -1691,14 +1614,7 @@ class CI_DB_active_record extends CI_DB_driver { $not[] = $k.'-'.$v; } - if ($escape === FALSE) - { - $clean[$this->_protect_identifiers($k2)] = $v2; - } - else - { - $clean[$this->_protect_identifiers($k2)] = $this->escape($v2); - } + $clean[$this->_protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2); } if ($index_set == FALSE) @@ -1728,11 +1644,7 @@ class CI_DB_active_record extends CI_DB_driver { { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; } $table = $this->ar_from[0]; @@ -1743,9 +1655,7 @@ class CI_DB_active_record extends CI_DB_driver { } $sql = $this->_delete($table); - $this->_reset_write(); - return $this->query($sql); } @@ -1767,11 +1677,7 @@ class CI_DB_active_record extends CI_DB_driver { { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; } $table = $this->ar_from[0]; @@ -1782,9 +1688,7 @@ class CI_DB_active_record extends CI_DB_driver { } $sql = $this->_truncate($table); - $this->_reset_write(); - return $this->query($sql); } @@ -1830,11 +1734,7 @@ class CI_DB_active_record extends CI_DB_driver { { if ( ! isset($this->ar_from[0])) { - if ($this->db_debug) - { - return $this->display_error('db_must_set_table'); - } - return FALSE; + return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; } $table = $this->ar_from[0]; @@ -1864,29 +1764,18 @@ class CI_DB_active_record extends CI_DB_driver { $this->limit($limit); } - if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0) + if (count($this->ar_where) === 0 && count($this->ar_wherein) === 0 && count($this->ar_like) === 0) { - if ($this->db_debug) - { - return $this->display_error('db_del_must_use_where'); - } - - return FALSE; + return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE; } $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit); - if ($reset_data) { $this->_reset_write(); } - if ($this->return_delete_sql === true) - { - return $sql; - } - - return $this->query($sql); + return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql); } // -------------------------------------------------------------------- @@ -1953,13 +1842,13 @@ class CI_DB_active_record extends CI_DB_driver { } // if a table alias is used we can recognize it by a space - if (strpos($table, " ") !== FALSE) + 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, " ")); + $table = trim(strrchr($table, ' ')); // Store the alias, if it doesn't already exist if ( ! in_array($table, $this->ar_aliased_tables)) @@ -1984,10 +1873,7 @@ class CI_DB_active_record extends CI_DB_driver { // Combine any cached components with the current statements $this->_merge_cache(); - // ---------------------------------------------------------------- - // Write the "select" portion of the query - if ($select_override !== FALSE) { $sql = $select_override; @@ -1996,7 +1882,7 @@ class CI_DB_active_record extends CI_DB_driver { { $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; - if (count($this->ar_select) == 0) + if (count($this->ar_select) === 0) { $sql .= '*'; } @@ -2015,32 +1901,19 @@ class CI_DB_active_record extends CI_DB_driver { } } - // ---------------------------------------------------------------- - // Write the "FROM" portion of the query - if (count($this->ar_from) > 0) { - $sql .= "\nFROM "; - - $sql .= $this->_from_tables($this->ar_from); + $sql .= "\nFROM ".$this->_from_tables($this->ar_from); } - // ---------------------------------------------------------------- - // Write the "JOIN" portion of the query - if (count($this->ar_join) > 0) { - $sql .= "\n"; - - $sql .= implode("\n", $this->ar_join); + $sql .= "\n".implode("\n", $this->ar_join); } - // ---------------------------------------------------------------- - // Write the "WHERE" portion of the query - if (count($this->ar_where) > 0 OR count($this->ar_like) > 0) { $sql .= "\nWHERE "; @@ -2048,10 +1921,7 @@ 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) @@ -2062,50 +1932,32 @@ class CI_DB_active_record extends CI_DB_driver { $sql .= implode("\n", $this->ar_like); } - // ---------------------------------------------------------------- - // Write the "GROUP BY" portion of the query - if (count($this->ar_groupby) > 0) { - $sql .= "\nGROUP BY "; - - $sql .= implode(', ', $this->ar_groupby); + $sql .= "\nGROUP BY ".implode(', ', $this->ar_groupby); } - // ---------------------------------------------------------------- - // Write the "HAVING" portion of the query - if (count($this->ar_having) > 0) { - $sql .= "\nHAVING "; - $sql .= implode("\n", $this->ar_having); + $sql .= "\nHAVING ".implode("\n", $this->ar_having); } - // ---------------------------------------------------------------- - // Write the "ORDER BY" portion of the query - if (count($this->ar_orderby) > 0) { - $sql .= "\nORDER BY "; - $sql .= implode(', ', $this->ar_orderby); - + $sql .= "\nORDER BY ".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"; - $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset); + return $this->_limit($sql."\n", $this->ar_limit, $this->ar_offset); } return $sql; @@ -2165,14 +2017,12 @@ class CI_DB_active_record extends CI_DB_driver { foreach ($fields as $val) { // There are some built in keys we need to ignore for this conversion - if ($val != '_parent_name') + if ($val !== '_parent_name') { - $i = 0; foreach ($out[$val] as $data) { - $array[$i][$val] = $data; - $i++; + $array[$i++][$val] = $data; } } } @@ -2247,7 +2097,7 @@ class CI_DB_active_record extends CI_DB_driver { */ protected function _merge_cache() { - if (count($this->ar_cache_exists) == 0) + if (count($this->ar_cache_exists) === 0) { return; } @@ -2257,7 +2107,7 @@ class CI_DB_active_record extends CI_DB_driver { $ar_variable = 'ar_'.$val; $ar_cache_var = 'ar_cache_'.$val; - if (count($this->$ar_cache_var) == 0) + if (count($this->$ar_cache_var) === 0) { continue; } @@ -2282,7 +2132,6 @@ class CI_DB_active_record extends CI_DB_driver { * * Publicly-visible method to reset the AR values. * - * @access public * @return void */ public function reset_query() @@ -2319,25 +2168,24 @@ class CI_DB_active_record extends CI_DB_driver { */ protected 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_aliased_tables' => array(), - 'ar_no_escape' => array(), - 'ar_distinct' => FALSE, - 'ar_limit' => FALSE, - 'ar_offset' => FALSE, - 'ar_order' => FALSE, - ); - - $this->_reset_run($ar_reset_items); + $this->_reset_run(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_no_escape' => array(), + 'ar_distinct' => FALSE, + 'ar_limit' => FALSE, + 'ar_offset' => FALSE, + 'ar_order' => FALSE + ) + ); } // -------------------------------------------------------------------- @@ -2351,19 +2199,19 @@ class CI_DB_active_record extends CI_DB_driver { */ protected function _reset_write() { - $ar_reset_items = array( - 'ar_set' => array(), - 'ar_from' => array(), - 'ar_where' => array(), - 'ar_like' => array(), - 'ar_orderby' => array(), - 'ar_keys' => array(), - 'ar_limit' => FALSE, - 'ar_order' => FALSE - ); - - $this->_reset_run($ar_reset_items); + $this->_reset_run(array( + 'ar_set' => array(), + 'ar_from' => array(), + 'ar_where' => array(), + 'ar_like' => array(), + 'ar_orderby' => array(), + 'ar_keys' => array(), + 'ar_limit' => FALSE, + 'ar_order' => FALSE + ) + ); } + } /* End of file DB_active_rec.php */ -- cgit v1.2.3-24-g4f1b From 0f7decce7cb64566e63bd910557006f041514d89 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 8 Jan 2012 04:40:29 +0200 Subject: Swap two vars for readability --- system/database/DB_active_rec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 30a17d11a..71762a4de 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -278,7 +278,7 @@ class CI_DB_active_record extends CI_DB_driver { { $v = trim($v); $this->_track_aliases($v); - $this->ar_from[] = $v = $this->_protect_identifiers($v, TRUE, NULL, FALSE); + $v = $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); if ($this->ar_caching === TRUE) { -- cgit v1.2.3-24-g4f1b From 1d160e78619574bf7fcb3c9b5d6a4bc9f93f2db6 Mon Sep 17 00:00:00 2001 From: Purwandi Date: Mon, 9 Jan 2012 16:33:28 +0700 Subject: Fix error undefined variable query on count_all_results db active record --- system/database/DB_active_rec.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 71762a4de..424735157 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -1127,15 +1127,14 @@ class CI_DB_active_record extends CI_DB_driver { $result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows'))); $this->_reset_select(); - if ($query->num_rows() === 0) + if ($result->num_rows() === 0) { return 0; } - $row = $query->row(); + $row = $result->row(); return (int) $row->numrows; } - // -------------------------------------------------------------------- /** -- cgit v1.2.3-24-g4f1b From 7efad20597ef7e06f8cf837a9f40918d2d3f2727 Mon Sep 17 00:00:00 2001 From: Jamie Rumbelow Date: Sun, 19 Feb 2012 12:37:00 +0000 Subject: Renaming Active Record to Query Builder across the system --- system/database/DB_active_rec.php | 2217 ------------------------------------- 1 file changed, 2217 deletions(-) delete mode 100644 system/database/DB_active_rec.php (limited to 'system/database/DB_active_rec.php') diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php deleted file mode 100644 index 424735157..000000000 --- a/system/database/DB_active_rec.php +++ /dev/null @@ -1,2217 +0,0 @@ -ar_select[] = $val; - $this->ar_no_escape[] = $escape; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_select[] = $val; - $this->ar_cache_exists[] = 'select'; - $this->ar_cache_no_escape[] = $escape; - } - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Select Max - * - * Generates a SELECT MAX(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_max($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'MAX'); - } - - // -------------------------------------------------------------------- - - /** - * Select Min - * - * Generates a SELECT MIN(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_min($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'MIN'); - } - - // -------------------------------------------------------------------- - - /** - * Select Average - * - * Generates a SELECT AVG(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_avg($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'AVG'); - } - - // -------------------------------------------------------------------- - - /** - * Select Sum - * - * Generates a SELECT SUM(field) portion of a query - * - * @param string the field - * @param string an alias - * @return object - */ - public function select_sum($select = '', $alias = '') - { - return $this->_max_min_avg_sum($select, $alias, 'SUM'); - } - - // -------------------------------------------------------------------- - - /** - * Processing Function for the four functions above: - * - * select_max() - * select_min() - * select_avg() - * select_sum() - * - * @param string the field - * @param string an alias - * @return object - */ - protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') - { - if ( ! is_string($select) OR $select == '') - { - $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 = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias)); - $this->ar_select[] = $sql; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_select[] = $sql; - $this->ar_cache_exists[] = 'select'; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Determines the alias name based on the table - * - * @param string - * @return string - */ - protected function _create_alias_from_table($item) - { - if (strpos($item, '.') !== FALSE) - { - return end(explode('.', $item)); - } - - return $item; - } - - // -------------------------------------------------------------------- - - /** - * DISTINCT - * - * Sets a flag which tells the query string compiler to add DISTINCT - * - * @param bool - * @return object - */ - public function distinct($val = TRUE) - { - $this->ar_distinct = (is_bool($val)) ? $val : TRUE; - return $this; - } - - // -------------------------------------------------------------------- - - /** - * From - * - * Generates the FROM portion of the query - * - * @param mixed can be a string or array - * @return object - */ - public function from($from) - { - foreach ((array)$from as $val) - { - if (strpos($val, ',') !== FALSE) - { - foreach (explode(',', $val) as $v) - { - $v = trim($v); - $this->_track_aliases($v); - $v = $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_from[] = $v; - $this->ar_cache_exists[] = 'from'; - } - } - } - else - { - $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 - $this->_track_aliases($val); - $this->ar_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE); - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_from[] = $val; - $this->ar_cache_exists[] = 'from'; - } - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Join - * - * Generates the JOIN portion of the query - * - * @param string - * @param string the join condition - * @param string the type of join - * @return object - */ - public function join($table, $cond, $type = '') - { - if ($type != '') - { - $type = strtoupper(trim($type)); - - if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'))) - { - $type = ''; - } - else - { - $type .= ' '; - } - } - - // Extract any aliases that might exist. We use this information - // in the _protect_identifiers to know whether to add a table prefix - $this->_track_aliases($table); - - // Strip apart the condition and protect the identifiers - if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match)) - { - $cond = $this->_protect_identifiers($match[1]).$match[2].$this->_protect_identifiers($match[3]); - } - - // Assemble the JOIN statement - $this->ar_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_join[] = $join; - $this->ar_cache_exists[] = 'join'; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Where - * - * Generates the WHERE portion of the query. Separates - * multiple calls with AND - * - * @param mixed - * @param mixed - * @return object - */ - public function where($key, $value = NULL, $escape = TRUE) - { - return $this->_where($key, $value, 'AND ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * OR Where - * - * Generates the WHERE portion of the query. Separates - * multiple calls with OR - * - * @param mixed - * @param mixed - * @return object - */ - public function or_where($key, $value = NULL, $escape = TRUE) - { - return $this->_where($key, $value, 'OR ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * Where - * - * Called by where() or or_where() - * - * @param mixed - * @param mixed - * @param string - * @return object - */ - protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) - { - $type = $this->_group_get_type($type); - - if ( ! is_array($key)) - { - $key = array($key => $value); - } - - // If the escape value was not set will will base it on the global setting - if ( ! is_bool($escape)) - { - $escape = $this->_protect_identifiers; - } - - foreach ($key as $k => $v) - { - $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; - - if (is_null($v) && ! $this->_has_operator($k)) - { - // 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); - } - - if ( ! $this->_has_operator($k)) - { - $k .= ' = '; - } - } - else - { - $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; - } - - // -------------------------------------------------------------------- - - /** - * Where_in - * - * Generates a WHERE field IN ('item', 'item') SQL query joined with - * AND if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function where_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values); - } - - // -------------------------------------------------------------------- - - /** - * Where_in_or - * - * Generates a WHERE field IN ('item', 'item') SQL query joined with - * OR if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function or_where_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values, FALSE, 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Where_not_in - * - * Generates a WHERE field NOT IN ('item', 'item') SQL query joined - * with AND if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function where_not_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values, TRUE); - } - - // -------------------------------------------------------------------- - - /** - * Where_not_in_or - * - * Generates a WHERE field NOT IN ('item', 'item') SQL query joined - * with OR if appropriate - * - * @param string The field to search - * @param array The values searched on - * @return object - */ - public function or_where_not_in($key = NULL, $values = NULL) - { - return $this->_where_in($key, $values, TRUE, 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Where_in - * - * Called by where_in, where_in_or, where_not_in, where_not_in_or - * - * @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 - * @return object - */ - protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ') - { - if ($key === NULL OR $values === NULL) - { - return; - } - - $type = $this->_group_get_type($type); - - if ( ! is_array($values)) - { - $values = array($values); - } - - $not = ($not) ? ' NOT' : ''; - - foreach ($values as $value) - { - $this->ar_wherein[] = $this->escape($value); - } - - $prefix = (count($this->ar_where) === 0) ? '' : $type; - $this->ar_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->ar_wherein).') '; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_where[] = $where_in; - $this->ar_cache_exists[] = 'where'; - } - - // reset the array for multiple calls - $this->ar_wherein = array(); - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Like - * - * Generates a %LIKE% portion of the query. Separates - * multiple calls with AND - * - * @param mixed - * @param mixed - * @return object - */ - public function like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'AND ', $side); - } - - // -------------------------------------------------------------------- - - /** - * Not Like - * - * Generates a NOT LIKE portion of the query. Separates - * multiple calls with AND - * - * @param mixed - * @param mixed - * @return object - */ - public function not_like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'AND ', $side, 'NOT'); - } - - // -------------------------------------------------------------------- - - /** - * OR Like - * - * Generates a %LIKE% portion of the query. Separates - * multiple calls with OR - * - * @param mixed - * @param mixed - * @return object - */ - public function or_like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'OR ', $side); - } - - // -------------------------------------------------------------------- - - /** - * OR Not Like - * - * Generates a NOT LIKE portion of the query. Separates - * multiple calls with OR - * - * @param mixed - * @param mixed - * @return object - */ - public function or_not_like($field, $match = '', $side = 'both') - { - return $this->_like($field, $match, 'OR ', $side, 'NOT'); - } - - // -------------------------------------------------------------------- - - /** - * Like - * - * Called by like() or orlike() - * - * @param mixed - * @param mixed - * @param string - * @return object - */ - protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') - { - $type = $this->_group_get_type($type); - - if ( ! is_array($field)) - { - $field = array($field => $match); - } - - foreach ($field as $k => $v) - { - $k = $this->_protect_identifiers($k); - $prefix = (count($this->ar_like) === 0) ? '' : $type; - $v = $this->escape_like_str($v); - - if ($side === 'none') - { - $like_statement = $prefix." $k $not LIKE '{$v}'"; - } - elseif ($side === 'before') - { - $like_statement = $prefix." $k $not LIKE '%{$v}'"; - } - elseif ($side === 'after') - { - $like_statement = $prefix." $k $not LIKE '{$v}%'"; - } - else - { - $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_chr); - } - - $this->ar_like[] = $like_statement; - if ($this->ar_caching === TRUE) - { - $this->ar_cache_like[] = $like_statement; - $this->ar_cache_exists[] = 'like'; - } - - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group. - * - * @param string (Internal use only) - * @param string (Internal use only) - * @return object - */ - public function group_start($not = '', $type = 'AND ') - { - $type = $this->_group_get_type($type); - $this->ar_where_group_started = TRUE; - $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type; - $this->ar_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->ar_where_group_count).' ('; - - if ($this->ar_caching) - { - $this->ar_cache_where[] = $value; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group, but ORs the group - * - * @return object - */ - public function or_group_start() - { - return $this->group_start('', 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group, but NOTs the group - * - * @return object - */ - public function not_group_start() - { - return $this->group_start('NOT ', 'AND '); - } - - // -------------------------------------------------------------------- - - /** - * Starts a query group, but OR NOTs the group - * - * @return object - */ - public function or_not_group_start() - { - return $this->group_start('NOT ', 'OR '); - } - - // -------------------------------------------------------------------- - - /** - * Ends a query group - * - * @return object - */ - public function group_end() - { - $this->ar_where_group_started = FALSE; - $this->ar_where[] = $value = str_repeat(' ', $this->ar_where_group_count--) . ')'; - - if ($this->ar_caching) - { - $this->ar_cache_where[] = $value; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Group_get_type - * - * Called by group_start(), _like(), _where() and _where_in() - * - * @param string - * @return string - */ - protected function _group_get_type($type) - { - if ($this->ar_where_group_started) - { - $type = ''; - $this->ar_where_group_started = FALSE; - } - - return $type; - } - - // -------------------------------------------------------------------- - - /** - * GROUP BY - * - * @param string - * @return object - */ - public function group_by($by) - { - if (is_string($by)) - { - $by = explode(',', $by); - } - - foreach ($by as $val) - { - $val = trim($val); - - if ($val != '') - { - $this->ar_groupby[] = $val = $this->_protect_identifiers($val); - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_groupby[] = $val; - $this->ar_cache_exists[] = 'groupby'; - } - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the HAVING value - * - * Separates multiple calls with AND - * - * @param string - * @param string - * @return object - */ - public function having($key, $value = '', $escape = TRUE) - { - return $this->_having($key, $value, 'AND ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * Sets the OR HAVING value - * - * Separates multiple calls with OR - * - * @param string - * @param string - * @return object - */ - public function or_having($key, $value = '', $escape = TRUE) - { - return $this->_having($key, $value, 'OR ', $escape); - } - - // -------------------------------------------------------------------- - - /** - * Sets the HAVING values - * - * Called by having() or or_having() - * - * @param string - * @param string - * @return object - */ - protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE) - { - if ( ! is_array($key)) - { - $key = array($key => $value); - } - - foreach ($key as $k => $v) - { - $prefix = (count($this->ar_having) === 0) ? '' : $type; - - if ($escape === TRUE) - { - $k = $this->_protect_identifiers($k); - } - - if ( ! $this->_has_operator($k)) - { - $k .= ' = '; - } - - if ($v != '') - { - $v = ' '.$this->escape($v); - } - - $this->ar_having[] = $prefix.$k.$v; - if ($this->ar_caching === TRUE) - { - $this->ar_cache_having[] = $prefix.$k.$v; - $this->ar_cache_exists[] = 'having'; - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the ORDER BY value - * - * @param string - * @param string direction: asc or desc - * @param bool enable field name escaping - * @return object - */ - public function order_by($orderby, $direction = '', $escape = TRUE) - { - if (strtolower($direction) === 'random') - { - $orderby = ''; // Random results want or don't need a field name - $direction = $this->_random_keyword; - } - elseif (trim($direction) != '') - { - $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC'; - } - - - if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE) - { - $temp = array(); - foreach (explode(',', $orderby) as $part) - { - $part = trim($part); - if ( ! in_array($part, $this->ar_aliased_tables)) - { - $part = $this->_protect_identifiers(trim($part)); - } - - $temp[] = $part; - } - - $orderby = implode(', ', $temp); - } - elseif ($direction != $this->_random_keyword) - { - if ($escape === TRUE) - { - $orderby = $this->_protect_identifiers($orderby); - } - } - - $this->ar_orderby[] = $orderby_statement = $orderby.$direction; - - if ($this->ar_caching === TRUE) - { - $this->ar_cache_orderby[] = $orderby_statement; - $this->ar_cache_exists[] = 'orderby'; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the LIMIT value - * - * @param integer the limit value - * @param integer the offset value - * @return object - */ - public function limit($value, $offset = NULL) - { - $this->ar_limit = (int) $value; - - if ( ! is_null($offset)) - { - $this->ar_offset = (int) $offset; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Sets the OFFSET value - * - * @param integer the offset value - * @return object - */ - public function offset($offset) - { - $this->ar_offset = (int) $offset; - return $this; - } - - // -------------------------------------------------------------------- - - /** - * The "set" function. Allows key/value pairs to be set for inserting or updating - * - * @param mixed - * @param string - * @param boolean - * @return object - */ - public 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) - { - if ($escape === FALSE) - { - $this->ar_set[$this->_protect_identifiers($k)] = $v; - } - else - { - $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); - } - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Get SELECT query string - * - * Compiles a SELECT query string and returns the sql. - * - * @access public - * @param string the table name to select from (optional) - * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone - * @return string - */ - public function get_compiled_select($table = '', $reset = TRUE) - { - if ($table != '') - { - $this->_track_aliases($table); - $this->from($table); - } - - $select = $this->_compile_select(); - - if ($reset === TRUE) - { - $this->_reset_select(); - } - - return $select; - } - - // -------------------------------------------------------------------- - - /** - * Get - * - * Compiles the select statement based on the other functions called - * and runs the query - * - * @param string the table - * @param string the limit clause - * @param string the offset clause - * @return object - */ - public function get($table = '', $limit = null, $offset = null) - { - if ($table != '') - { - $this->_track_aliases($table); - $this->from($table); - } - - if ( ! is_null($limit)) - { - $this->limit($limit, $offset); - } - - $result = $this->query($this->_compile_select()); - $this->_reset_select(); - return $result; - } - - /** - * "Count All Results" query - * - * Generates a platform-specific query string that counts all records - * returned by an Active Record query. - * - * @param string - * @return string - */ - public function count_all_results($table = '') - { - if ($table != '') - { - $this->_track_aliases($table); - $this->from($table); - } - - $result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows'))); - $this->_reset_select(); - - if ($result->num_rows() === 0) - { - return 0; - } - - $row = $result->row(); - return (int) $row->numrows; - } - // -------------------------------------------------------------------- - - /** - * Get_Where - * - * Allows the where clause, limit and offset to be added directly - * - * @param string the where clause - * @param string the limit clause - * @param string the offset clause - * @return object - */ - public function get_where($table = '', $where = null, $limit = null, $offset = null) - { - if ($table != '') - { - $this->from($table); - } - - if ( ! is_null($where)) - { - $this->where($where); - } - - if ( ! is_null($limit)) - { - $this->limit($limit, $offset); - } - - $result = $this->query($this->_compile_select()); - $this->_reset_select(); - return $result; - } - - // -------------------------------------------------------------------- - - /** - * Insert_Batch - * - * Compiles batch insert strings and runs the queries - * - * @param string the table to retrieve the results from - * @param array an associative array of insert values - * @return object - */ - public function insert_batch($table = '', $set = NULL) - { - 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])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - - // Batch this baby - for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100) - { - $this->query($this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100))); - } - - $this->_reset_write(); - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts - * - * @param mixed - * @param string - * @param boolean - * @return object - */ - public 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; - } - - // -------------------------------------------------------------------- - - /** - * Get INSERT query string - * - * Compiles an insert query and returns the sql - * - * @access public - * @param string the table to insert into - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone - * @return string - */ - public function get_compiled_insert($table = '', $reset = TRUE) - { - if ($this->_validate_insert($table) === FALSE) - { - return FALSE; - } - - $sql = $this->_insert( - $this->_protect_identifiers( - $this->ar_from[0], TRUE, NULL, FALSE - ), - array_keys($this->ar_set), - array_values($this->ar_set) - ); - - if ($reset === TRUE) - { - $this->_reset_write(); - } - - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Insert - * - * Compiles an insert string and runs the query - * - * @access public - * @param string the table to insert data into - * @param array an associative array of insert values - * @return object - */ - public function insert($table = '', $set = NULL) - { - if ( ! is_null($set)) - { - $this->set($set); - } - - if ($this->_validate_insert($table) === FALSE) - { - return FALSE; - } - - $sql = $this->_insert( - $this->_protect_identifiers( - $this->ar_from[0], TRUE, NULL, FALSE - ), - array_keys($this->ar_set), - array_values($this->ar_set) - ); - - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Validate Insert - * - * This method is used by both insert() and get_compiled_insert() to - * validate that the there data is actually being set and that table - * has been chosen to be inserted into. - * - * @access public - * @param string the table to insert data into - * @return string - */ - protected function _validate_insert($table = '') - { - if (count($this->ar_set) === 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - } - else - { - $this->ar_from[0] = $table; - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Replace - * - * Compiles an replace into string and runs the query - * - * @param string the table to replace data into - * @param array an associative array of insert values - * @return object - */ - public function replace($table = '', $set = NULL) - { - if ( ! is_null($set)) - { - $this->set($set); - } - - if (count($this->ar_set) === 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : 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); - } - - // -------------------------------------------------------------------- - - /** - * Get UPDATE query string - * - * Compiles an update query and returns the sql - * - * @access public - * @param string the table to update - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone - * @return string - */ - public function get_compiled_update($table = '', $reset = TRUE) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if ($this->_validate_update($table) === FALSE) - { - return FALSE; - } - - $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); - - if ($reset === TRUE) - { - $this->_reset_write(); - } - - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Update - * - * Compiles an update string and runs the query - * - * @param string the table to retrieve the results from - * @param array an associative array of update values - * @param mixed the where clause - * @return object - */ - public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if ( ! is_null($set)) - { - $this->set($set); - } - - if ($this->_validate_update($table) === FALSE) - { - return FALSE; - } - - if ($where != NULL) - { - $this->where($where); - } - - if ($limit != NULL) - { - $this->limit($limit); - } - - $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, $this->ar_like); - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Validate Update - * - * This method is used by both update() and get_compiled_update() to - * validate that data is actually being set and that a table has been - * chosen to be update. - * - * @access public - * @param string the table to update data on - * @return bool - */ - protected function _validate_update($table = '') - { - if (count($this->ar_set) == 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - } - else - { - $this->ar_from[0] = $table; - } - - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Update_Batch - * - * Compiles an update string and runs the query - * - * @param string the table to retrieve the results from - * @param array an associative array of update values - * @param string the where key - * @return bool - */ - public function update_batch($table = '', $set = NULL, $index = NULL) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if (is_null($index)) - { - return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE; - } - - if ( ! is_null($set)) - { - $this->set_update_batch($set, $index); - } - - if (count($this->ar_set) === 0) - { - return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; - } - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - - // Batch this baby - for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100) - { - $this->query($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->_reset_write(); - return TRUE; - } - - // -------------------------------------------------------------------- - - /** - * The "set_update_batch" function. Allows key/value pairs to be set for batch updating - * - * @param array - * @param string - * @param boolean - * @return object - */ - public 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; - } - - $clean[$this->_protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2); - } - - if ($index_set == FALSE) - { - return $this->display_error('db_batch_missing_index'); - } - - $this->ar_set[] = $clean; - } - - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Empty Table - * - * Compiles a delete string and runs "DELETE FROM table" - * - * @param string the table to empty - * @return object - */ - public function empty_table($table = '') - { - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - else - { - $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); - } - - $sql = $this->_delete($table); - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Truncate - * - * Compiles a truncate string and runs the query - * If the database does not support the truncate() command - * This function maps to "DELETE FROM table" - * - * @param string the table to truncate - * @return object - */ - public function truncate($table = '') - { - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - else - { - $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); - } - - $sql = $this->_truncate($table); - $this->_reset_write(); - return $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * Get DELETE query string - * - * Compiles a delete query string and returns the sql - * - * @access public - * @param string the table to delete from - * @param boolean TRUE: reset AR values; FALSE: leave AR values alone - * @return string - */ - public function get_compiled_delete($table = '', $reset = TRUE) - { - $this->return_delete_sql = TRUE; - $sql = $this->delete($table, '', NULL, $reset); - $this->return_delete_sql = FALSE; - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Delete - * - * Compiles a delete string and runs the query - * - * @param mixed the table(s) to delete from. String or array - * @param mixed the where clause - * @param mixed the limit clause - * @param boolean - * @return object - */ - public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - if ($table == '') - { - if ( ! isset($this->ar_from[0])) - { - return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; - } - - $table = $this->ar_from[0]; - } - elseif (is_array($table)) - { - foreach ($table as $single_table) - { - $this->delete($single_table, $where, $limit, FALSE); - } - - $this->_reset_write(); - return; - } - else - { - $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); - } - - if ($where != '') - { - $this->where($where); - } - - if ($limit != NULL) - { - $this->limit($limit); - } - - if (count($this->ar_where) === 0 && count($this->ar_wherein) === 0 && count($this->ar_like) === 0) - { - return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE; - } - - $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit); - if ($reset_data) - { - $this->_reset_write(); - } - - return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql); - } - - // -------------------------------------------------------------------- - - /** - * DB Prefix - * - * Prepends a database prefix if one exists in configuration - * - * @param string the table - * @return string - */ - public function dbprefix($table = '') - { - if ($table == '') - { - $this->display_error('db_table_name_required'); - } - - return $this->dbprefix.$table; - } - - // -------------------------------------------------------------------- - - /** - * Set DB Prefix - * - * Set's the DB Prefix to something new without needing to reconnect - * - * @param string the prefix - * @return string - */ - public function set_dbprefix($prefix = '') - { - return $this->dbprefix = $prefix; - } - - // -------------------------------------------------------------------- - - /** - * Track Aliases - * - * Used to track SQL statements written with aliased tables. - * - * @param string The table to inspect - * @return string - */ - protected function _track_aliases($table) - { - if (is_array($table)) - { - foreach ($table as $t) - { - $this->_track_aliases($t); - } - 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)) - { - $this->ar_aliased_tables[] = $table; - } - } - } - - // -------------------------------------------------------------------- - - /** - * Compile the SELECT statement - * - * Generates a query string based on which functions were used. - * Should not be called directly. The get() function calls it. - * - * @return string - */ - protected function _compile_select($select_override = FALSE) - { - // Combine any cached components with the current statements - $this->_merge_cache(); - - // Write the "select" portion of the query - if ($select_override !== FALSE) - { - $sql = $select_override; - } - else - { - $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; - - if (count($this->ar_select) === 0) - { - $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 - foreach ($this->ar_select as $key => $val) - { - $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL; - $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape); - } - - $sql .= implode(', ', $this->ar_select); - } - } - - // Write the "FROM" portion of the query - if (count($this->ar_from) > 0) - { - $sql .= "\nFROM ".$this->_from_tables($this->ar_from); - } - - // Write the "JOIN" portion of the query - if (count($this->ar_join) > 0) - { - $sql .= "\n".implode("\n", $this->ar_join); - } - - // Write the "WHERE" portion of the query - if (count($this->ar_where) > 0 OR count($this->ar_like) > 0) - { - $sql .= "\nWHERE "; - } - - $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) - { - $sql .= "\nAND "; - } - - $sql .= implode("\n", $this->ar_like); - } - - // Write the "GROUP BY" portion of the query - if (count($this->ar_groupby) > 0) - { - $sql .= "\nGROUP BY ".implode(', ', $this->ar_groupby); - } - - // Write the "HAVING" portion of the query - if (count($this->ar_having) > 0) - { - $sql .= "\nHAVING ".implode("\n", $this->ar_having); - } - - // Write the "ORDER BY" portion of the query - if (count($this->ar_orderby) > 0) - { - $sql .= "\nORDER BY ".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)) - { - return $this->_limit($sql."\n", $this->ar_limit, $this->ar_offset); - } - - return $sql; - } - - // -------------------------------------------------------------------- - - /** - * Object to Array - * - * Takes an object as input and converts the class variables to array key/vals - * - * @param object - * @return array - */ - public function _object_to_array($object) - { - if ( ! is_object($object)) - { - 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') - { - $array[$key] = $val; - } - } - - return $array; - } - - // -------------------------------------------------------------------- - - /** - * Object to Array - * - * Takes an object as input and converts the class variables to array key/vals - * - * @param object - * @return array - */ - public 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; - } - } - } - - return $array; - } - - // -------------------------------------------------------------------- - - /** - * Start Cache - * - * Starts AR caching - * - * @return void - */ - public function start_cache() - { - $this->ar_caching = TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Stop Cache - * - * Stops AR caching - * - * @return void - */ - public function stop_cache() - { - $this->ar_caching = FALSE; - } - - // -------------------------------------------------------------------- - - /** - * Flush Cache - * - * Empties the AR cache - * - * @access public - * @return void - */ - public function flush_cache() - { - $this->_reset_run(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_set' => array(), - 'ar_cache_exists' => array(), - 'ar_cache_no_escape' => array() - )); - } - - // -------------------------------------------------------------------- - - /** - * Merge Cache - * - * When called, this function merges any cached AR arrays with - * locally called ones. - * - * @return void - */ - protected function _merge_cache() - { - if (count($this->ar_cache_exists) === 0) - { - return; - } - - foreach ($this->ar_cache_exists as $val) - { - $ar_variable = 'ar_'.$val; - $ar_cache_var = 'ar_cache_'.$val; - - if (count($this->$ar_cache_var) === 0) - { - continue; - } - - $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable)); - } - - // If we are "protecting identifiers" we need to examine the "from" - // portion of the query to determine if there are any aliases - if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0) - { - $this->_track_aliases($this->ar_from); - } - - $this->ar_no_escape = $this->ar_cache_no_escape; - } - - // -------------------------------------------------------------------- - - /** - * Reset Active Record values. - * - * Publicly-visible method to reset the AR values. - * - * @return void - */ - public function reset_query() - { - $this->_reset_select(); - $this->_reset_write(); - } - - // -------------------------------------------------------------------- - - /** - * Resets the active record values. Called by the get() function - * - * @param array An array of fields to reset - * @return void - */ - protected function _reset_run($ar_reset_items) - { - foreach ($ar_reset_items as $item => $default_value) - { - if ( ! in_array($item, $this->ar_store_array)) - { - $this->$item = $default_value; - } - } - } - - // -------------------------------------------------------------------- - - /** - * Resets the active record values. Called by the get() function - * - * @return void - */ - protected function _reset_select() - { - $this->_reset_run(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_no_escape' => array(), - 'ar_distinct' => FALSE, - 'ar_limit' => FALSE, - 'ar_offset' => FALSE, - 'ar_order' => FALSE - ) - ); - } - - // -------------------------------------------------------------------- - - /** - * Resets the active record "write" values. - * - * Called by the insert() update() insert_batch() update_batch() and delete() functions - * - * @return void - */ - protected function _reset_write() - { - $this->_reset_run(array( - 'ar_set' => array(), - 'ar_from' => array(), - 'ar_where' => array(), - 'ar_like' => array(), - 'ar_orderby' => array(), - 'ar_keys' => array(), - 'ar_limit' => FALSE, - 'ar_order' => FALSE - ) - ); - } - -} - -/* End of file DB_active_rec.php */ -/* Location: ./system/database/DB_active_rec.php */ -- cgit v1.2.3-24-g4f1b