summaryrefslogtreecommitdiffstats
path: root/system/database/DB_query_builder.php
diff options
context:
space:
mode:
authorJamie Rumbelow <jamie@jamierumbelow.net>2012-04-26 14:27:35 +0200
committerJamie Rumbelow <jamie@jamierumbelow.net>2012-04-26 14:27:35 +0200
commitd6ce1e936d60487ad8ddd099a040fba7d8f88367 (patch)
tree31dcdda9a24e2ed12135659a4e38cb4f35b0af31 /system/database/DB_query_builder.php
parentbcee50ff3247dee71d83bf273e52bc10096cd48c (diff)
parent9e2d5d130eff40592b49337a8ba4d8c170934de1 (diff)
Merge branch 'develop' of git://github.com/EllisLab/CodeIgniter into develop
Conflicts: system/core/Loader.php system/database/DB_query_builder.php system/database/drivers/cubrid/cubrid_driver.php system/database/drivers/mssql/mssql_driver.php system/database/drivers/mysql/mysql_driver.php system/database/drivers/mysqli/mysqli_driver.php system/database/drivers/oci8/oci8_driver.php system/database/drivers/odbc/odbc_driver.php system/database/drivers/pdo/pdo_driver.php system/database/drivers/postgre/postgre_driver.php system/database/drivers/sqlite/sqlite_driver.php user_guide_src/source/changelog.rst user_guide_src/source/database/query_builder.rst
Diffstat (limited to 'system/database/DB_query_builder.php')
-rw-r--r--system/database/DB_query_builder.php202
1 files changed, 163 insertions, 39 deletions
diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php
index 8979dc15b..393a1cd75 100644
--- a/system/database/DB_query_builder.php
+++ b/system/database/DB_query_builder.php
@@ -2,7 +2,7 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
*
@@ -25,8 +25,6 @@
* @filesource
*/
-// ------------------------------------------------------------------------
-
/**
* Query Builder Class
*
@@ -38,7 +36,8 @@
* @author EllisLab Dev Team
* @link http://codeigniter.com/user_guide/database/
*/
-class CI_DB_query_builder extends CI_DB_driver {
+
+abstract class CI_DB_query_builder extends CI_DB_driver {
protected $return_delete_sql = FALSE;
protected $reset_delete_data = FALSE;
@@ -213,7 +212,9 @@ class CI_DB_query_builder extends CI_DB_driver {
}
$sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->protect_identifiers(trim($alias));
+
$this->qb_select[] = $sql;
+ $this->qb_no_escape[] = NULL;
if ($this->qb_caching === TRUE)
{
@@ -423,7 +424,7 @@ class CI_DB_query_builder extends CI_DB_driver {
foreach ($key as $k => $v)
{
- $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type;
+ $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
if (is_null($v) && ! $this->_has_operator($k))
{
@@ -538,7 +539,7 @@ class CI_DB_query_builder extends CI_DB_driver {
*
* @param string The field to search
* @param array The values searched on
- * @param boolean If the statement would be IN or NOT IN
+ * @param bool If the statement would be IN or NOT IN
* @param string
* @return object
*/
@@ -719,8 +720,9 @@ class CI_DB_query_builder extends CI_DB_driver {
public function group_start($not = '', $type = 'AND ')
{
$type = $this->_group_get_type($type);
+
$this->qb_where_group_started = TRUE;
- $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type;
+ $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
$this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
if ($this->qb_caching)
@@ -985,8 +987,8 @@ class CI_DB_query_builder extends CI_DB_driver {
/**
* Sets the LIMIT value
*
- * @param integer the limit value
- * @param integer the offset value
+ * @param int the limit value
+ * @param int the offset value
* @return object
*/
public function limit($value, $offset = NULL)
@@ -1006,7 +1008,7 @@ class CI_DB_query_builder extends CI_DB_driver {
/**
* Sets the OFFSET value
*
- * @param integer the offset value
+ * @param int the offset value
* @return object
*/
public function offset($offset)
@@ -1022,7 +1024,7 @@ class CI_DB_query_builder extends CI_DB_driver {
*
* @param mixed
* @param string
- * @param boolean
+ * @param bool
* @return object
*/
public function set($key, $value = '', $escape = TRUE)
@@ -1056,9 +1058,8 @@ class CI_DB_query_builder extends CI_DB_driver {
*
* Compiles a SELECT query string and returns the sql.
*
- * @access public
* @param string the table name to select from (optional)
- * @param boolean TRUE: resets QB values; FALSE: leave QB vaules alone
+ * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
* @return string
*/
public function get_compiled_select($table = '', $reset = TRUE)
@@ -1223,11 +1224,28 @@ class CI_DB_query_builder extends CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Insert_batch statement
+ *
+ * Generates a platform-specific insert string from the supplied data.
+ *
+ * @param string the table name
+ * @param array the insert keys
+ * @param array the insert values
+ * @return string
+ */
+ protected function _insert_batch($table, $keys, $values)
+ {
+ return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
*
* @param mixed
* @param string
- * @param boolean
+ * @param bool
* @return object
*/
public function set_insert_batch($key, $value = '', $escape = TRUE)
@@ -1239,11 +1257,12 @@ class CI_DB_query_builder extends CI_DB_driver {
$key = array($key => $value);
}
- $keys = array_keys(current($key));
+ $keys = array_keys($this->_object_to_array(current($key)));
sort($keys);
foreach ($key as $row)
{
+ $row = $this->_object_to_array($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
@@ -1284,9 +1303,8 @@ class CI_DB_query_builder extends CI_DB_driver {
*
* Compiles an insert query and returns the sql
*
- * @access public
* @param string the table to insert into
- * @param boolean TRUE: reset QB values; FALSE: leave QB values alone
+ * @param bool TRUE: reset QB values; FALSE: leave QB values alone
* @return string
*/
public function get_compiled_insert($table = '', $reset = TRUE)
@@ -1319,7 +1337,6 @@ class CI_DB_query_builder 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
@@ -1351,13 +1368,29 @@ class CI_DB_query_builder extends CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Insert statement
+ *
+ * Generates a platform-specific insert string from the supplied data
+ *
+ * @param string the table name
+ * @param array the insert keys
+ * @param array the insert values
+ * @return string
+ */
+ protected function _insert($table, $keys, $values)
+ {
+ return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* 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
*/
@@ -1425,13 +1458,29 @@ class CI_DB_query_builder extends CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Replace statement
+ *
+ * Generates a platform-specific replace string from the supplied data
+ *
+ * @param string the table name
+ * @param array the insert keys
+ * @param array the insert values
+ * @return string
+ */
+ protected function _replace($table, $keys, $values)
+ {
+ return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Get UPDATE query string
*
* Compiles an update query and returns the sql
*
- * @access public
* @param string the table to update
- * @param boolean TRUE: reset QB values; FALSE: leave QB values alone
+ * @param bool TRUE: reset QB values; FALSE: leave QB values alone
* @return string
*/
public function get_compiled_update($table = '', $reset = TRUE)
@@ -1500,13 +1549,47 @@ class CI_DB_query_builder extends CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Update statement
+ *
+ * Generates a platform-specific update string from the supplied data
+ *
+ * @param string the table name
+ * @param array the update data
+ * @param array the where clause
+ * @param array the orderby clause
+ * @param array the limit clause
+ * @param array the like clause
+ * @return string
+ */
+ protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
+ {
+ foreach ($values as $key => $val)
+ {
+ $valstr[] = $key.' = '.$val;
+ }
+
+ $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
+
+ if ( ! empty($like))
+ {
+ $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
+ }
+
+ return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
+ .$where
+ .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
+ .($limit ? ' LIMIT '.$limit : '');
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* 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
*/
@@ -1591,7 +1674,7 @@ class CI_DB_query_builder extends CI_DB_driver {
*
* @param array
* @param string
- * @param boolean
+ * @param bool
* @return object
*/
public function set_update_batch($key, $index = '', $escape = TRUE)
@@ -1699,13 +1782,30 @@ class CI_DB_query_builder extends CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Truncate statement
+ *
+ * Generates a platform-specific truncate string from the supplied data
+ *
+ * If the database does not support the truncate() command,
+ * then this method maps to 'DELETE FROM table'
+ *
+ * @param string the table name
+ * @return string
+ */
+ protected function _truncate($table)
+ {
+ return 'TRUNCATE '.$table;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* 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 QB values; FALSE: leave QB values alone
+ * @param bool TRUE: reset QB values; FALSE: leave QB values alone
* @return string
*/
public function get_compiled_delete($table = '', $reset = TRUE)
@@ -1726,7 +1826,7 @@ class CI_DB_query_builder extends CI_DB_driver {
* @param mixed the table(s) to delete from. String or array
* @param mixed the where clause
* @param mixed the limit clause
- * @param boolean
+ * @param bool
* @return object
*/
public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
@@ -1785,6 +1885,31 @@ class CI_DB_query_builder extends CI_DB_driver {
// --------------------------------------------------------------------
/**
+ * Delete statement
+ *
+ * Generates a platform-specific delete string from the supplied data
+ *
+ * @param string the table name
+ * @param array the where clause
+ * @param array the like clause
+ * @param string the limit clause
+ * @return string
+ */
+ protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
+ {
+ $conditions = array();
+
+ empty($where) OR $conditions[] = implode(' ', $where);
+ empty($like) OR $conditions[] = implode(' ', $like);
+
+ return 'DELETE FROM '.$table
+ .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
+ .($limit ? ' LIMIT '.$limit : '');
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* DB Prefix
*
* Prepends a database prefix if one exists in configuration
@@ -2069,7 +2194,6 @@ class CI_DB_query_builder extends CI_DB_driver {
*
* Empties the QB cache
*
- * @access public
* @return void
*/
public function flush_cache()
@@ -2121,7 +2245,7 @@ class CI_DB_query_builder extends CI_DB_driver {
// 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->qb_cache_from) > 0)
+ if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
{
$this->_track_aliases($this->qb_from);
}
@@ -2204,19 +2328,19 @@ class CI_DB_query_builder extends CI_DB_driver {
protected function _reset_write()
{
$this->_reset_run(array(
- 'qb_set' => array(),
- 'qb_from' => array(),
- 'qb_where' => array(),
- 'qb_like' => array(),
- 'qb_orderby' => array(),
- 'qb_keys' => array(),
- 'qb_limit' => FALSE,
- 'qb_order' => FALSE
- )
- );
+ 'qb_set' => array(),
+ 'qb_from' => array(),
+ 'qb_where' => array(),
+ 'qb_like' => array(),
+ 'qb_orderby' => array(),
+ 'qb_keys' => array(),
+ 'qb_limit' => FALSE,
+ 'qb_order' => FALSE
+ )
+ );
}
}
/* End of file DB_query_builder.php */
-/* Location: ./system/database/DB_query_builder.php */
+/* Location: ./system/database/DB_query_builder.php */ \ No newline at end of file