root

Changeset 682

Show
Ignore:
Timestamp:
08/12/09 01:44:53 (1 year ago)
Author:
wbond
Message:

Added the ability to pass an array of all values as a single parameter to fDatabase::escape() instead of one value per parameter

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fDatabase.php

    r671 r682 Hide Line Numbers
    4747 * @link       http://flourishlib.com/fDatabase 
    4848 *  
    49  * @version    1.0.0b16 
     49 * @version    1.0.0b17 
     50 * @changes    1.0.0b17  Added the ability to pass an array of all values as a single parameter to ::escape() instead of one value per parameter [wb, 2009-08-11] 
    5051 * @changes    1.0.0b16  Fixed PostgreSQL and Oracle from trying to get auto-incrementing values on inserts when explicit values were given [wb, 2009-08-06] 
    5152 * @changes    1.0.0b15  Fixed a bug where auto-incremented values would not be detected when table names were quoted [wb, 2009-07-15] 
     
    815816     *  - an array - the escaped values are inserted into the SQL string separated by commas 
    816817     *  
     818     * If `$sql_or_type` is a SQL string, it is also possible to pass an array 
     819     * of all values as a single parameter instead of one value per parameter. 
     820     * An example would look like the following: 
     821     *  
     822     * {{{ 
     823     * #!php 
     824     * $db->escape( 
     825     *     "SELECT * FROM users WHERE status = %s AND authorization_level = %s", 
     826     *     array('Active', 'Admin') 
     827     * ); 
     828     * }}} 
     829     *  
    817830     * @param  string $sql_or_type  This can either be the data type to escape or an SQL string with a data type placeholder - see method description 
    818831     * @param  mixed  $value        The value to escape - both single values and arrays of values are supported, see method description for details 
     
    831844         
    832845        // Convert all objects into strings 
    833         $new_values = array(); 
    834         foreach ($values as $value) { 
    835             if (is_object($value) && is_callable(array($value, '__toString'))) { 
    836                 $value = $value->__toString(); 
    837             } elseif (is_object($value)) { 
    838                 $value = (string) $value;    
    839             } 
    840             $new_values[] = $value; 
    841         } 
    842         $values = $new_values; 
     846        $values = $this->scalarize($values); 
    843847         
    844848        $value = array_shift($values); 
     
    887891        if ($callback) { 
    888892            if (is_array($value)) { 
     893                // If the values were passed as a single array, this handles that 
     894                if (count($value) == 1 && is_array(current($value))) { 
     895                    $value = current($value); 
     896                } 
    889897                return array_map($callback, $value);         
    890898            } 
     
    914922        $pieces = preg_split('#(%[lbdfistp])\b#', $temp_sql, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); 
    915923        $sql = ''; 
     924         
     925        // If the values were passed as a single array, this handles that 
     926        if (count($values) == 0 && is_array($value)) { 
     927            $placeholders = 0; 
     928            foreach ($pieces as $piece) { 
     929                if (strlen($piece) == 2 && $piece[0] == '%') { 
     930                    $placeholders++; 
     931                }    
     932            } 
     933             
     934            if ($placeholders == count($value)) { 
     935                $values = $value; 
     936                $value  = array_shift($values);  
     937            } 
     938        } 
    916939         
    917940        $missing_values = -1; 
     
    21032126     
    21042127    /** 
     2128     * Turns an array possibly containing objects into an array of all strings 
     2129     *  
     2130     * @param  array $values  The array of values to scalarize 
     2131     * @return array  The scalarized values 
     2132     */ 
     2133    private function scalarize($values) 
     2134    { 
     2135        $new_values = array(); 
     2136        foreach ($values as $value) { 
     2137            if (is_object($value) && is_callable(array($value, '__toString'))) { 
     2138                $value = $value->__toString(); 
     2139            } elseif (is_object($value)) { 
     2140                $value = (string) $value;    
     2141            } elseif (is_array($value)) { 
     2142                $value = $this->scalarize($value);   
     2143            } 
     2144            $new_values[] = $value; 
     2145        } 
     2146        return $new_values;  
     2147    } 
     2148     
     2149     
     2150    /** 
    21052151     * Sets the number of rows affected by the query 
    21062152     *