root

Changeset 630

Show
Ignore:
Timestamp:
07/07/09 08:01:06 (1 year ago)
Author:
wbond
Message:

Updated fDatabase::escape() to accept arrays of values for insertion into SQL strings

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fDatabase.php

    r603 r630 Hide Line Numbers
    4747 * @link       http://flourishlib.com/fDatabase 
    4848 *  
    49  * @version    1.0.0b12 
     49 * @version    1.0.0b13 
     50 * @changes    1.0.0b13  Updated ::escape() to accept arrays of values for insertion into full SQL strings [wb, 2009-07-06] 
    5051 * @changes    1.0.0b12  Updates to ::unescape() to improve performance [wb, 2009-06-15] 
    5152 * @changes    1.0.0b11  Changed replacement values in preg_replace() calls to be properly escaped [wb, 2009-06-11] 
     
    799800     *  - `%p` for a timestamp 
    800801     *  
     802     * Depending on what `$sql_or_type` and `$value` are, the output will be 
     803     * slightly different. If `$sql_or_type` is a data type or a single 
     804     * placeholder and `$value` is: 
     805     *  
     806     *  - a scalar value - an escaped SQL string is returned 
     807     *  - an array - an array of escaped SQL strings is returned 
     808     *  
     809     * If `$sql_or_type` is a SQL string and `$value` is: 
     810     *  
     811     *  - a scalar value - the escaped value is inserted into the SQL string 
     812     *  - an array - the escaped values are inserted into the SQL string separated by commas 
     813     *  
    801814     * @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 
    802      * @param  mixed  $value        The value to escape - you should pass a single value or an array of values if a data type is specified, or a value for each placeholder 
     815     * @param  mixed  $value        The value to escape - both single values and arrays of values are supported, see method description for details 
    803816     * @param  mixed  ... 
    804817     * @return mixed  The escaped value/SQL or an array of the escaped values 
     
    904917            switch ($piece) { 
    905918                case '%l': 
    906                     $sql .= $this->escapeBlob($value)
     919                    $callback = $this->escapeBlob
    907920                    break; 
    908921                case '%b': 
    909                     $sql .= $this->escapeBoolean($value)
     922                    $callback = $this->escapeBoolean
    910923                    break; 
    911924                case '%d': 
    912                     $sql .= $this->escapeDate($value)
     925                    $callback = $this->escapeDate
    913926                    break; 
    914927                case '%f': 
    915                     $sql .= $this->escapeFloat($value)
     928                    $callback = $this->escapeFloat
    916929                    break; 
    917930                case '%i': 
    918                     $sql .= $this->escapeInteger($value)
     931                    $callback = $this->escapeInteger
    919932                    break; 
    920933                case '%s': 
    921                     $sql .= $this->escapeString($value)
     934                    $callback = $this->escapeString
    922935                    break; 
    923936                case '%t': 
    924                     $sql .= $this->escapeTime($value)
     937                    $callback = $this->escapeTime
    925938                    break; 
    926939                case '%p': 
    927                     $sql .= $this->escapeTimestamp($value)
     940                    $callback = $this->escapeTimestamp
    928941                    break; 
    929942                default: 
    930943                    $sql .= $piece; 
    931944                    continue 2;  
    932             }        
     945            } 
     946             
     947            if (is_array($value)) { 
     948                $sql .= join(', ', array_map($callback, $value));        
     949            } else { 
     950                $sql .= call_user_func($callback, $value); 
     951            } 
     952                     
    933953            if (sizeof($values)) { 
    934954                $value = array_shift($values);