Make WordPress Core

Ticket #19019: wpdb-condense.4.diff

File wpdb-condense.4.diff, 10.7 KB (added by atimmer, 12 years ago)
  • src/wp-includes/wp-db.php

     
    12431243
    12441244                return $return_val;
    12451245        }
     1246       
     1247        /**
     1248         * Helper function for format of name value pairs joined by =
     1249         *
     1250         * @access protected
     1251         * @since 3.5.0
     1252         * @see wpdb::prepare()
     1253         * @see wpdb::$field_types
     1254         * @see wp_set_wpdb_vars()
     1255         *
     1256         * @param array $data A named array of clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $data columns and $data values should be "raw".
     1257         * @param array|string $formats Optional. An array of formats to be mapped to each of the values in $where.
     1258         *      If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string).
     1259         *      If omitted, all values in $where will be treated as strings.
     1260         * @param boolean $values If set to true, only returns an array of formats
     1261         * @return array of formatted field => $value
     1262         */
     1263        protected function format_clauses( $data, $formats = null, $values = false ) {
     1264                $wheres = array();
     1265                foreach ( (array) array_keys( $data ) as $field ) {
     1266                        $format = $formats;
     1267                       
     1268                        if ( isset( $this->field_types[$field] ) )
     1269                                $format = $this->field_types[$field];                                   
     1270                       
     1271                        if ( is_array( $formats ) )
     1272                                $format = empty( $formats ) ? '%s' : array_shift( $formats );
    12461273
     1274                        if ( empty( $format ) )
     1275                                $format = '%s';
     1276                       
     1277                        $wheres[] = $values ? $format : "`$field` = {$format}";
     1278                }
     1279
     1280                return $wheres;
     1281        }       
     1282       
    12471283        /**
    12481284         * Insert a row into a table.
    12491285         *
     
    12591295         *
    12601296         * @param string $table table name
    12611297         * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1262          * @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.
     1298         * @param array|string $formats 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.
    12631299         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    12641300         * @return int|false The number of rows inserted, or false on error.
    12651301         */
    1266         function insert( $table, $data, $format = null ) {
    1267                 return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
     1302        function insert( $table, $data, $formats = null ) {
     1303                return $this->_insert_replace_helper( $table, $data, $formats, 'INSERT' );
    12681304        }
    12691305
    12701306        /**
     
    12821318         *
    12831319         * @param string $table table name
    12841320         * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1285          * @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.
     1321         * @param array|string $formats 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.
    12861322         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    12871323         * @return int|false The number of rows affected, or false on error.
    12881324         */
    1289         function replace( $table, $data, $format = null ) {
    1290                 return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
     1325        function replace( $table, $data, $formats = null ) {
     1326                return $this->_insert_replace_helper( $table, $data, $formats, 'REPLACE' );
    12911327        }
    12921328
    12931329        /**
     
    13031339         *
    13041340         * @param string $table table name
    13051341         * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1306          * @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.
     1342         * @param array|string $formats 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.
    13071343         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    13081344         * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
    13091345         * @return int|false The number of rows affected, or false on error.
    13101346         */
    1311         function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
     1347        function _insert_replace_helper( $table, $data, $formats = null, $type = 'INSERT' ) {
    13121348                if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
    13131349                        return false;
    13141350                $this->insert_id = 0;
    1315                 $formats = $format = (array) $format;
    1316                 $fields = array_keys( $data );
    1317                 $formatted_fields = array();
    1318                 foreach ( $fields as $field ) {
    1319                         if ( !empty( $format ) )
    1320                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1321                         elseif ( isset( $this->field_types[$field] ) )
    1322                                 $form = $this->field_types[$field];
    1323                         else
    1324                                 $form = '%s';
    1325                         $formatted_fields[] = $form;
    1326                 }
    1327                 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
     1351               
     1352                $fields = implode( '`,`', array_keys( $data ) );
     1353                $formatted_fields = implode( ",", $this->format_clauses( $data, $formats, true ) );
     1354
     1355                $sql = "{$type} INTO `$table` (`$fields`) VALUES ($formatted_fields)";
    13281356                return $this->query( $this->prepare( $sql, $data ) );
    13291357        }
    13301358
     
    13441372         * @param string $table table name
    13451373         * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    13461374         * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
    1347          * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.
     1375         * @param array|string $formats Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.
    13481376         *      A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
    1349          * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings.
     1377         * @param array|string $where_formats Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings.
    13501378         * @return int|false The number of rows updated, or false on error.
    13511379         */
    1352         function update( $table, $data, $where, $format = null, $where_format = null ) {
     1380        function update( $table, $data, $where, $formats = null, $where_formats = null ) {
    13531381                if ( ! is_array( $data ) || ! is_array( $where ) )
    13541382                        return false;
    13551383
    1356                 $formats = $format = (array) $format;
    1357                 $bits = $wheres = array();
    1358                 foreach ( (array) array_keys( $data ) as $field ) {
    1359                         if ( !empty( $format ) )
    1360                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1361                         elseif ( isset($this->field_types[$field]) )
    1362                                 $form = $this->field_types[$field];
    1363                         else
    1364                                 $form = '%s';
    1365                         $bits[] = "`$field` = {$form}";
    1366                 }
     1384                $bits = implode( ', ', $this->format_clauses( $data, $formats ) );
     1385                $wheres = implode( ' AND ', $this->format_clauses( $where, $where_formats ) );
    13671386
    1368                 $where_formats = $where_format = (array) $where_format;
    1369                 foreach ( (array) array_keys( $where ) as $field ) {
    1370                         if ( !empty( $where_format ) )
    1371                                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    1372                         elseif ( isset( $this->field_types[$field] ) )
    1373                                 $form = $this->field_types[$field];
    1374                         else
    1375                                 $form = '%s';
    1376                         $wheres[] = "`$field` = {$form}";
    1377                 }
    1378 
    1379                 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
     1387                $sql = "UPDATE `$table` SET $bits WHERE $wheres";
    13801388                return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    13811389        }
    13821390
     
    13951403         *
    13961404         * @param string $table table name
    13971405         * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
    1398          * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
     1406         * @param array|string $where_formats Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
    13991407         * @return int|false The number of rows updated, or false on error.
    14001408         */
    1401         function delete( $table, $where, $where_format = null ) {
     1409        function delete( $table, $where, $where_formats = null ) {
    14021410                if ( ! is_array( $where ) )
    14031411                        return false;
    14041412
    1405                 $bits = $wheres = array();
     1413                $wheres = implode( ' AND ', $this->format_clauses( $where, $where_formats ) );
    14061414
    1407                 $where_formats = $where_format = (array) $where_format;
    1408 
    1409                 foreach ( array_keys( $where ) as $field ) {
    1410                         if ( !empty( $where_format ) ) {
    1411                                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    1412                         } elseif ( isset( $this->field_types[ $field ] ) ) {
    1413                                 $form = $this->field_types[ $field ];
    1414                         } else {
    1415                                 $form = '%s';
    1416                         }
    1417 
    1418                         $wheres[] = "$field = $form";
    1419                 }
    1420 
    1421                 $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
     1415                $sql = "DELETE FROM `$table` WHERE $wheres";
    14221416                return $this->query( $this->prepare( $sql, $where ) );
    14231417        }
    14241418
    1425 
    14261419        /**
    14271420         * Retrieve one variable from the database.
    14281421         *