summaryrefslogtreecommitdiffstats
path: root/system/database
diff options
context:
space:
mode:
Diffstat (limited to 'system/database')
-rwxr-xr-xsystem/database/DB.php9
-rw-r--r--system/database/DB_driver.php17
-rw-r--r--system/database/DB_query_builder.php43
-rw-r--r--system/database/DB_result.php16
-rw-r--r--system/database/DB_utility.php4
-rw-r--r--system/database/drivers/mysql/mysql_driver.php6
-rw-r--r--system/database/drivers/mysqli/mysqli_driver.php6
-rw-r--r--system/database/drivers/oci8/oci8_result.php16
-rw-r--r--system/database/drivers/pdo/pdo_driver.php6
-rw-r--r--system/database/drivers/postgre/postgre_driver.php41
-rw-r--r--system/database/drivers/sqlite3/sqlite3_result.php16
-rw-r--r--system/database/drivers/sqlsrv/sqlsrv_driver.php10
12 files changed, 109 insertions, 81 deletions
diff --git a/system/database/DB.php b/system/database/DB.php
index b28439b29..1fe44c0e5 100755
--- a/system/database/DB.php
+++ b/system/database/DB.php
@@ -118,6 +118,13 @@ function &DB($params = '', $query_builder_override = NULL)
{
$query_builder = $query_builder_override;
}
+ // Backwards compatibility work-around for keeping the
+ // $active_record config variable working. Should be
+ // removed in v3.1
+ elseif ( ! isset($query_builder) && isset($active_record))
+ {
+ $query_builder = $active_record;
+ }
require_once(BASEPATH.'database/DB_driver.php');
@@ -159,4 +166,4 @@ function &DB($params = '', $query_builder_override = NULL)
}
/* End of file DB.php */
-/* Location: ./system/database/DB.php */
+/* Location: ./system/database/DB.php */ \ No newline at end of file
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index a0812d4c7..d8a1c13f0 100644
--- a/system/database/DB_driver.php
+++ b/system/database/DB_driver.php
@@ -1404,23 +1404,6 @@ abstract class CI_DB_driver {
{
}
- // --------------------------------------------------------------------
-
- /**
- * Destructor
- *
- * Closes the database connection, if needed.
- *
- * @return void
- */
- public function __destruct()
- {
- if ( ! $this->pconnect)
- {
- $this->close();
- }
- }
-
}
/* End of file DB_driver.php */
diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php
index d0af66de1..cee4354e9 100644
--- a/system/database/DB_query_builder.php
+++ b/system/database/DB_query_builder.php
@@ -211,8 +211,8 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
$alias = $this->_create_alias_from_table(trim($select));
}
- $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->protect_identifiers(trim($alias));
-
+ $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
+
$this->qb_select[] = $sql;
$this->qb_no_escape[] = NULL;
@@ -256,7 +256,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
*/
public function distinct($val = TRUE)
{
- $this->qb_distinct = (is_bool($val)) ? $val : TRUE;
+ $this->qb_distinct = is_bool($val) ? $val : TRUE;
return $this;
}
@@ -272,7 +272,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
*/
public function from($from)
{
- foreach ((array)$from as $val)
+ foreach ((array) $from as $val)
{
if (strpos($val, ',') !== FALSE)
{
@@ -1111,6 +1111,8 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
return $result;
}
+ // --------------------------------------------------------------------
+
/**
* "Count All Results" query
*
@@ -1139,6 +1141,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
$row = $result->row();
return (int) $row->numrows;
}
+
// --------------------------------------------------------------------
/**
@@ -1401,16 +1404,13 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
}
- if ($table == '')
+ if ($table != '')
{
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
+ $this->qb_from[0] = $table;
}
- else
+ elseif ( ! isset($this->qb_from[0]))
{
- $this->qb_from[0] = $table;
+ return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
}
return TRUE;
@@ -1600,16 +1600,13 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
}
- if ($table == '')
+ if ($table != '')
{
- if ( ! isset($this->qb_from[0]))
- {
- return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
- }
+ $this->qb_from[0] = $table;
}
- else
+ elseif ( ! isset($this->qb_from[0]))
{
- $this->qb_from[0] = $table;
+ return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
}
return TRUE;
@@ -1696,15 +1693,11 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
{
$index_set = TRUE;
}
- else
- {
- $not[] = $k.'-'.$v;
- }
$clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
}
- if ($index_set == FALSE)
+ if ($index_set === FALSE)
{
return $this->display_error('db_batch_missing_index');
}
@@ -2102,7 +2095,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
* @param object
* @return array
*/
- public function _object_to_array($object)
+ protected function _object_to_array($object)
{
if ( ! is_object($object))
{
@@ -2132,7 +2125,7 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
* @param object
* @return array
*/
- public function _object_to_array_batch($object)
+ protected function _object_to_array_batch($object)
{
if ( ! is_object($object))
{
diff --git a/system/database/DB_result.php b/system/database/DB_result.php
index 690734b08..334e08c72 100644
--- a/system/database/DB_result.php
+++ b/system/database/DB_result.php
@@ -242,7 +242,7 @@ class CI_DB_result {
$result = $this->custom_result_object($type);
if (count($result) === 0)
{
- return $result;
+ return NULL;
}
if ($n != $this->current_row && isset($result[$n]))
@@ -253,6 +253,8 @@ class CI_DB_result {
return $result[$this->current_row];
}
+ // --------------------------------------------------------------------
+
/**
* Returns a single result row - object version
*
@@ -263,7 +265,7 @@ class CI_DB_result {
$result = $this->result_object();
if (count($result) === 0)
{
- return $result;
+ return NULL;
}
if ($n != $this->current_row && isset($result[$n]))
@@ -286,7 +288,7 @@ class CI_DB_result {
$result = $this->result_array();
if (count($result) === 0)
{
- return $result;
+ return NULL;
}
if ($n != $this->current_row && isset($result[$n]))
@@ -307,7 +309,7 @@ class CI_DB_result {
public function first_row($type = 'object')
{
$result = $this->result($type);
- return (count($result) === 0) ? $result : $result[0];
+ return (count($result) === 0) ? NULL : $result[0];
}
// --------------------------------------------------------------------
@@ -320,7 +322,7 @@ class CI_DB_result {
public function last_row($type = 'object')
{
$result = $this->result($type);
- return (count($result) === 0) ? $result : $result[count($result) - 1];
+ return (count($result) === 0) ? NULL : $result[count($result) - 1];
}
// --------------------------------------------------------------------
@@ -335,7 +337,7 @@ class CI_DB_result {
$result = $this->result($type);
if (count($result) === 0)
{
- return $result;
+ return NULL;
}
if (isset($result[$this->current_row + 1]))
@@ -358,7 +360,7 @@ class CI_DB_result {
$result = $this->result($type);
if (count($result) === 0)
{
- return $result;
+ return NULL;
}
if (isset($result[$this->current_row - 1]))
diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php
index 587dfdc01..cb97ff448 100644
--- a/system/database/DB_utility.php
+++ b/system/database/DB_utility.php
@@ -212,7 +212,7 @@ abstract class CI_DB_utility extends CI_DB_forge {
$out = rtrim($out).$newline;
// Next blast through the result array and build out the rows
- foreach ($query->result_array() as $row)
+ while ($row = $query->unbuffered_row('array'))
{
foreach ($row as $item)
{
@@ -258,7 +258,7 @@ abstract class CI_DB_utility extends CI_DB_forge {
// Generate the result
$xml = '<'.$root.'>'.$newline;
- foreach ($query->result_array() as $row)
+ while ($row = $query->unbuffered_row())
{
$xml .= $tab.'<'.$element.'>'.$newline;
foreach ($row as $key => $val)
diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php
index 161f99541..d801a9aaf 100644
--- a/system/database/drivers/mysql/mysql_driver.php
+++ b/system/database/drivers/mysql/mysql_driver.php
@@ -47,7 +47,7 @@ class CI_DB_mysql_driver extends CI_DB {
// clause and character used for LIKE escape sequences - not used in MySQL
protected $_like_escape_str = '';
- protected $_like_escape_chr = '';
+ protected $_like_escape_chr = '\\';
/**
* The syntax to count rows is slightly different across different
@@ -291,7 +291,9 @@ class CI_DB_mysql_driver extends CI_DB {
// escape LIKE condition wildcards
if ($like === TRUE)
{
- return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
+ return str_replace(array($this->_like_escape_chr, '%', '_'),
+ array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
+ $str);
}
return $str;
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php
index 9261883f5..61761e0c6 100644
--- a/system/database/drivers/mysqli/mysqli_driver.php
+++ b/system/database/drivers/mysqli/mysqli_driver.php
@@ -47,7 +47,7 @@ class CI_DB_mysqli_driver extends CI_DB {
// clause and character used for LIKE escape sequences - not used in MySQL
protected $_like_escape_str = '';
- protected $_like_escape_chr = '';
+ protected $_like_escape_chr = '\\';
/**
* The syntax to count rows is slightly different across different
@@ -291,7 +291,9 @@ class CI_DB_mysqli_driver extends CI_DB {
// escape LIKE condition wildcards
if ($like === TRUE)
{
- return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
+ return str_replace(array($this->_like_escape_chr, '%', '_'),
+ array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
+ $str);
}
return $str;
diff --git a/system/database/drivers/oci8/oci8_result.php b/system/database/drivers/oci8/oci8_result.php
index 7b05e0a43..6fb6c81f1 100644
--- a/system/database/drivers/oci8/oci8_result.php
+++ b/system/database/drivers/oci8/oci8_result.php
@@ -419,7 +419,7 @@ class CI_DB_oci8_result extends CI_DB_result {
OR $n < $this->current_row)
{
// No such row exists
- return array();
+ return NULL;
}
// Get the next row index that would actually need to be fetched
@@ -460,7 +460,7 @@ class CI_DB_oci8_result extends CI_DB_result {
$this->num_rows = 0;
}
- return array();
+ return NULL;
}
$this->current_row = $n;
@@ -507,7 +507,7 @@ class CI_DB_oci8_result extends CI_DB_result {
return (object) $row;
}
- return array();
+ return NULL;
}
// --------------------------------------------------------------------
@@ -539,19 +539,19 @@ class CI_DB_oci8_result extends CI_DB_result {
}
else
{
- return array();
+ return NULL;
}
}
elseif ( ! class_exists($class_name)) // No such class exists
{
- return array();
+ return NULL;
}
$row = $this->row_array($n);
- // An array would mean that the row doesn't exist
- if (is_array($row))
+ // A non-array would mean that the row doesn't exist
+ if ( ! is_array($row))
{
- return $row;
+ return NULL;
}
// Convert to the desired class and return
diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php
index e38c1145c..4784fc65b 100644
--- a/system/database/drivers/pdo/pdo_driver.php
+++ b/system/database/drivers/pdo/pdo_driver.php
@@ -79,13 +79,13 @@ class CI_DB_pdo_driver extends CI_DB {
// clause and character used for LIKE escape sequences
// this one depends on the driver being used
- if ($this->pdodriver == 'mysql')
+ if ($this->pdodriver === 'mysql')
{
$this->_escape_char = '`';
$this->_like_escape_str = '';
- $this->_like_escape_chr = '';
+ $this->_like_escape_chr = '\\';
}
- elseif ($this->pdodriver == 'odbc')
+ elseif ($this->pdodriver === 'odbc')
{
$this->_like_escape_str = " {escape '%s'} ";
}
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index 0ddfd0abe..30689cc70 100644
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -539,6 +539,47 @@ class CI_DB_postgre_driver extends CI_DB {
// --------------------------------------------------------------------
/**
+ * Update_Batch statement
+ *
+ * Generates a platform-specific batch update string from the supplied data
+ *
+ * @param string the table name
+ * @param array the update data
+ * @param array the where clause
+ * @return string
+ */
+ protected function _update_batch($table, $values, $index, $where = NULL)
+ {
+ $ids = array();
+ foreach ($values as $key => $val)
+ {
+ $ids[] = $val[$index];
+
+ foreach (array_keys($val) as $field)
+ {
+ if ($field != $index)
+ {
+ $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
+ }
+ }
+ }
+
+ $cases = '';
+ foreach ($final as $k => $v)
+ {
+ $cases .= $k.' = (CASE '.$k."\n"
+ .implode("\n", $v)."\n"
+ .'ELSE '.$k.' END), ';
+ }
+
+ return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
+ .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
+ .$index.' IN('.implode(',', $ids).')';
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Delete statement
*
* Generates a platform-specific delete string from the supplied data
diff --git a/system/database/drivers/sqlite3/sqlite3_result.php b/system/database/drivers/sqlite3/sqlite3_result.php
index d83d6b2cd..946b36557 100644
--- a/system/database/drivers/sqlite3/sqlite3_result.php
+++ b/system/database/drivers/sqlite3/sqlite3_result.php
@@ -386,7 +386,7 @@ class CI_DB_sqlite3_result extends CI_DB_result {
OR count($this->result_array) > 0 OR $n < $this->current_row)
{
// No such row exists
- return array();
+ return NULL;
}
// Get the next row index that would actually need to be fetched
@@ -427,7 +427,7 @@ class CI_DB_sqlite3_result extends CI_DB_result {
$this->num_rows = 0;
}
- return array();
+ return NULL;
}
$this->current_row = $n;
@@ -469,7 +469,7 @@ class CI_DB_sqlite3_result extends CI_DB_result {
return (object) $row;
}
- return array();
+ return NULL;
}
// --------------------------------------------------------------------
@@ -501,19 +501,19 @@ class CI_DB_sqlite3_result extends CI_DB_result {
}
else
{
- return array();
+ return NULL;
}
}
elseif ( ! class_exists($class_name)) // No such class exists
{
- return array();
+ return NULL;
}
$row = $this->row_array($n);
- // An array would mean that the row doesn't exist
- if (is_array($row))
+ // A non-array would mean that the row doesn't exist
+ if ( ! is_array($row))
{
- return $row;
+ return NULL;
}
// Convert to the desired class and return
diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php
index 3e9fa7b1a..5a24f5532 100644
--- a/system/database/drivers/sqlsrv/sqlsrv_driver.php
+++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php
@@ -132,11 +132,9 @@ class CI_DB_sqlsrv_driver extends CI_DB {
*/
protected function _execute($sql)
{
- return sqlsrv_query($this->conn_id,
- $sql,
- NULL,
- array('Scrollable'=> SQLSRV_CURSOR_STATIC, 'SendStreamParamsAtExec' => TRUE)
- );
+ return (is_write_type($sql) && stripos($sql, 'INSERT') === FALSE)
+ ? sqlsrv_query($this->conn_id, $sql)
+ : sqlsrv_query($this->conn_id, $sql, NULL, array('Scrollable' => SQLSRV_CURSOR_STATIC));
}
// --------------------------------------------------------------------
@@ -237,7 +235,7 @@ class CI_DB_sqlsrv_driver extends CI_DB {
*/
public function affected_rows()
{
- return @sqlrv_rows_affected($this->conn_id);
+ return sqlrv_rows_affected($this->result_id);
}
// --------------------------------------------------------------------