Make WordPress Core


Ignore:
Timestamp:
02/27/2010 03:45:09 PM (14 years ago)
Author:
nacin
Message:

Add wpdb::replace() for replace queries. props AaronCampbell fixes #10864

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r13426 r13454  
    11441144     *
    11451145     * @param string $table table name
     1146     * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     1147     * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
     1148     *  A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
     1149     * @return int|false The number of rows inserted, or false on error.
     1150     */
     1151    function insert( $table, $data, $format = null ) {
     1152        return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
     1153    }
     1154
     1155    /**
     1156     * Replace a row into a table.
     1157     *
     1158     * <code>
     1159     * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
     1160     * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
     1161     * </code>
     1162     *
     1163     * @since 3.0.0
     1164     * @see wpdb::prepare()
     1165     * @see wpdb::$field_types
     1166     * @see wp_set_wpdb_vars()
     1167     *
     1168     * @param string $table table name
     1169     * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
     1170     * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
     1171     *  A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
     1172     * @return int|false The number of rows affected, or false on error.
     1173     */
     1174    function replace( $table, $data, $format = null ) {
     1175        return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
     1176    }
     1177
     1178    /**
     1179     * Helper function for insert and replace.
     1180     *
     1181     * Runs an insert or replace query based on $type argument.
     1182     *
     1183     * @access private
     1184     * @since 3.0.0
     1185     * @see wpdb::prepare()
     1186     * @see wpdb::$field_types
     1187     * @see wp_set_wpdb_vars()
     1188     *
     1189     * @param string $table table name
    11461190     * @param array $data Data to insert (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    11471191     * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
    11481192     *  A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    1149      * @return int|false The number of rows inserted, or false on error.
    1150      */
    1151     function insert( $table, $data, $format = null ) {
     1193     * @return int|false The number of rows affected, or false on error.
     1194     */
     1195    function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
     1196        if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
     1197            return false;
    11521198        $formats = $format = (array) $format;
    11531199        $fields = array_keys( $data );
     
    11621208            $formatted_fields[] = $form;
    11631209        }
    1164         $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
     1210        $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
    11651211        return $this->query( $this->prepare( $sql, $data ) );
    11661212    }
    1167 
    11681213
    11691214    /**
Note: See TracChangeset for help on using the changeset viewer.