upgrade to codeigniter 1.7.2 for f12
[www-register-wizard.git] / database / drivers / mysqli / mysqli_driver.php
index 07a07d3..488b074 100644 (file)
@@ -6,7 +6,7 @@
  *
  * @package            CodeIgniter
  * @author             ExpressionEngine Dev Team
- * @copyright  Copyright (c) 2008, EllisLab, Inc.
+ * @copyright  Copyright (c) 2008 - 2009, EllisLab, Inc.
  * @license            http://codeigniter.com/user_guide/license.html
  * @link               http://codeigniter.com
  * @since              Version 1.0
@@ -35,6 +35,10 @@ class CI_DB_mysqli_driver extends CI_DB {
        // The character used for escaping
        var $_escape_char = '`';
 
+       // clause and character used for LIKE escape sequences - not used in MySQL
+       var $_like_escape_str = '';
+       var $_like_escape_chr = '';
+
        /**
         * The syntax to count rows is slightly different across different
         * database engines, so this string appears in each driver and is
@@ -60,7 +64,15 @@ class CI_DB_mysqli_driver extends CI_DB {
         */     
        function db_connect()
        {
-               return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
+               if ($this->port != '')
+               {
+                       return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);                        
+               }
+               else
+               {
+                       return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
+               }
+
        }
 
        // --------------------------------------------------------------------
@@ -78,6 +90,25 @@ class CI_DB_mysqli_driver extends CI_DB {
        
        // --------------------------------------------------------------------
 
+       /**
+        * Reconnect
+        *
+        * Keep / reestablish the db connection if no queries have been
+        * sent for a length of time exceeding the server's idle timeout
+        *
+        * @access      public
+        * @return      void
+        */
+       function reconnect()
+       {
+               if (mysqli_ping($this->conn_id) === FALSE)
+               {
+                       $this->conn_id = FALSE;
+               }
+       }
+
+       // --------------------------------------------------------------------
+
        /**
         * Select the database
         *
@@ -249,22 +280,41 @@ class CI_DB_mysqli_driver extends CI_DB {
         *
         * @access      public
         * @param       string
+        * @param       bool    whether or not the string will be used in a LIKE condition
         * @return      string
         */
-       function escape_str($str)       
+       function escape_str($str, $like = FALSE)        
        {
+               if (is_array($str))
+               {
+                       foreach($str as $key => $val)
+                       {
+                               $str[$key] = $this->escape_str($val, $like);
+                       }
+               
+                       return $str;
+               }
+
                if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
                {
-                       return mysqli_real_escape_string($this->conn_id, $str);
+                       $str = mysqli_real_escape_string($this->conn_id, $str);
                }
                elseif (function_exists('mysql_escape_string'))
                {
-                       return mysql_escape_string($str);
+                       $str = mysql_escape_string($str);
                }
                else
                {
-                       return addslashes($str);
+                       $str = addslashes($str);
+               }
+               
+               // escape LIKE condition wildcards
+               if ($like === TRUE)
+               {
+                       $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
                }
+               
+               return $str;
        }
                
        // --------------------------------------------------------------------
@@ -308,15 +358,19 @@ class CI_DB_mysqli_driver extends CI_DB {
        function count_all($table = '')
        {
                if ($table == '')
-                       return '0';
-               
-               $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
-               
+               {
+                       return 0;
+               }
+
+               $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
+
                if ($query->num_rows() == 0)
-                       return '0';
+               {
+                       return 0;
+               }
 
                $row = $query->row();
-               return $row->numrows;
+               return (int) $row->numrows;
        }
 
        // --------------------------------------------------------------------
@@ -336,7 +390,7 @@ class CI_DB_mysqli_driver extends CI_DB {
                
                if ($prefix_limit !== FALSE AND $this->dbprefix != '')
                {
-                       $sql .= " LIKE '".$this->dbprefix."%'";
+                       $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
                }
                
                return $sql;
@@ -417,7 +471,18 @@ class CI_DB_mysqli_driver extends CI_DB {
                {
                        return $item;
                }
-       
+               
+               foreach ($this->_reserved_identifiers as $id)
+               {
+                       if (strpos($item, '.'.$id) !== FALSE)
+                       {
+                               $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);  
+                               
+                               // remove duplicates if the user already included the escape
+                               return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
+                       }               
+               }
+               
                if (strpos($item, '.') !== FALSE)
                {
                        $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;