Changeset 664

Show
Ignore:
Timestamp:
10/05/2007 10:33:08 AM (14 months ago)
Author:
zombor
Message:

Fix for Ticket #143, add Query Binding support

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/Database.php

    r658 r664  
    147147         * @access  public 
    148148         * @param   string 
     149         * @param   array 
    149150         * @param   boolean 
    150151         * @return  mixed 
    151152         */ 
    152         public function query($sql = '', $object = FALSE) 
     153        public function query($sql = '', $binds = FALSE, $object = FALSE) 
    153154        { 
    154155                if ($sql == '') return FALSE; 
     
    157158                $object = (bool) ($object == FALSE) ? $this->config['object'] : $object; 
    158159 
     160                // Compile binds if needed 
     161                if ($binds !== FALSE) 
     162                { 
     163                        $sql = $this->compile_binds($sql, $binds); 
     164                } 
     165                 
    159166                $this->last_query = $sql; 
    160167                return $this->driver->query($sql, $object); 
     
    724731        } 
    725732         
    726  
     733        /** 
     734         * Compile Bindings 
     735         * 
     736         * @access      public 
     737         * @param       string  the sql statement 
     738         * @param       array   an array of bind data 
     739         * @return      string           
     740         */      
     741        public function compile_binds($sql, $binds) 
     742        {        
     743                if (strpos($sql, '?') === FALSE) 
     744                { 
     745                        return $sql; 
     746                } 
     747                 
     748                $binds = (array) $binds; 
     749                 
     750                foreach ($binds as $val) 
     751                { 
     752                        $val = $this->driver->escape($val); 
     753                                         
     754                        // Just in case the replacement string contains the bind 
     755                        // character we'll temporarily replace it with a marker 
     756                        $val = str_replace('?', '{%bind_marker%}', $val); 
     757                        $sql = preg_replace("#".preg_quote('?', '#')."#", str_replace('$', '\$', $val), $sql, 1); 
     758                } 
     759 
     760                return str_replace('{%bind_marker%}', '?', $sql);                
     761        } 
    727762} // End Database Class