Make WordPress Core

Ticket #19019: wpdb-condense.2.diff

File wpdb-condense.2.diff, 7.6 KB (added by wonderboymusic, 13 years ago)

This is a much better version of this

  • wp-includes/load.php

     
    354354        global $wpdb, $table_prefix;
    355355        if ( !empty( $wpdb->error ) )
    356356                dead_db();
    357 
     357        /**
     358         * List all columns in WP tables that should be formatted as %d
     359         * These columns will be matched against the master list of tables in $wpdb->tables
     360         * - so "parent" won't indiscrimately be cast to INT in a user-defined table where its type is VARCHAR or TEXT, etc
     361         * 
     362         */
    358363        $wpdb->field_types = array( 'post_author' => '%d', 'post_parent' => '%d', 'menu_order' => '%d', 'term_id' => '%d', 'term_group' => '%d', 'term_taxonomy_id' => '%d',
    359                 'parent' => '%d', 'count' => '%d','object_id' => '%d', 'term_order' => '%d', 'ID' => '%d', 'commment_ID' => '%d', 'comment_post_ID' => '%d', 'comment_parent' => '%d',
     364                'parent' => '%d', 'count' => '%d','object_id' => '%d', 'term_order' => '%d', 'ID' => '%d', 'comment_ID' => '%d', 'comment_post_ID' => '%d', 'comment_parent' => '%d',
    360365                'user_id' => '%d', 'link_id' => '%d', 'link_owner' => '%d', 'link_rating' => '%d', 'option_id' => '%d', 'blog_id' => '%d', 'meta_id' => '%d', 'post_id' => '%d',
    361366                'user_status' => '%d', 'umeta_id' => '%d', 'comment_karma' => '%d', 'comment_count' => '%d',
    362367                // multisite:
  • wp-includes/wp-db.php

     
    11861186        }
    11871187
    11881188        /**
     1189         * Helper function for resolving form field formats
     1190         *
     1191         * @access protected
     1192         * @since 3.5.0
     1193         * @see wpdb::prepare()
     1194         * @see wpdb::$field_types
     1195         * @see wp_set_wpdb_vars()
     1196         *
     1197         * @param string $table table name
     1198         * @param string $field the column to format
     1199         * @param array|string $format Optional. An array of formats, the first item will be returned.
     1200         *              If string, that format will be used.
     1201         *              A format is one of '%d', '%f', '%s' (integer, float, string).
     1202         *              If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
     1203         *              $format is passed by-reference so that array_shift() will move the cursor to the next item in $format on subsequent calls
     1204         *             
     1205         * @return string format of value.
     1206         */
     1207        protected function field_format( $table, $field, &$format = null ) {
     1208                if ( ! empty( $format ) ) {
     1209                        if ( is_string( $format ) )
     1210                                $form = $format;
     1211                        elseif ( is_array( $format ) )
     1212                                $form = array_shift( $format );
     1213                } elseif ( in_array( $table, array_keys( $this->tables ) ) && isset( $this->field_types[$field] ) ) {
     1214                        $form = $this->field_types[$field];
     1215                } else {
     1216                        $form = '%s';
     1217                }
     1218                       
     1219                return $form;
     1220        }
     1221       
     1222        /**
    11891223         * Helper function for insert and replace.
    11901224         *
    11911225         * Runs an insert or replace query based on $type argument.
     
    11981232         *
    11991233         * @param string $table table name
    12001234         * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
    1201          * @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.
     1235         * @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.
    12021236         *      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.
    12031237         * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
    12041238         * @return int|false The number of rows affected, or false on error.
     
    12061240        function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
    12071241                if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
    12081242                        return false;
    1209                 $formats = $format = (array) $format;
    12101243                $fields = array_keys( $data );
    12111244                $formatted_fields = array();
    1212                 foreach ( $fields as $field ) {
    1213                         if ( !empty( $format ) )
    1214                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1215                         elseif ( isset( $this->field_types[$field] ) )
    1216                                 $form = $this->field_types[$field];
    1217                         else
    1218                                 $form = '%s';
    1219                         $formatted_fields[] = $form;
    1220                 }
     1245                foreach ( $fields as $field )
     1246                        $formatted_fields[] = $this->field_format( $table, $field, $format );
     1247
    12211248                $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
    12221249                return $this->query( $this->prepare( $sql, $data ) );
    12231250        }
    12241251
    12251252        /**
     1253         * Helper function for format of WHERE clauses
     1254         *
     1255         * @access protected
     1256         * @since 3.5.0
     1257         * @see wpdb::prepare()
     1258         * @see wpdb::$field_types
     1259         * @see wp_set_wpdb_vars()
     1260         *
     1261         * @param string $table table name
     1262         * @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".
     1263         * @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.
     1264         * @return array of formatted field => $value
     1265         */
     1266        protected function where_clauses( $table, $where, $where_format = null ) {
     1267                $wheres = array();
     1268                foreach ( (array) array_keys( $where ) as $field ) {
     1269                        $form = $this->field_format( $table, $field, $where_format );
     1270                        $wheres[] = "`$field` = {$form}";
     1271                }
     1272               
     1273                return $wheres;
     1274        }
     1275       
     1276        /**
    12261277         * Update a row in the table
    12271278         *
    12281279         * <code>
     
    12471298                if ( ! is_array( $data ) || ! is_array( $where ) )
    12481299                        return false;
    12491300
    1250                 $formats = $format = (array) $format;
    1251                 $bits = $wheres = array();
     1301                $bits = array();
    12521302                foreach ( (array) array_keys( $data ) as $field ) {
    1253                         if ( !empty( $format ) )
    1254                                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1255                         elseif ( isset($this->field_types[$field]) )
    1256                                 $form = $this->field_types[$field];
    1257                         else
    1258                                 $form = '%s';
     1303                        $form = $this->field_format( $table, $field, $format );
    12591304                        $bits[] = "`$field` = {$form}";
    12601305                }
    12611306
    1262                 $where_formats = $where_format = (array) $where_format;
    1263                 foreach ( (array) array_keys( $where ) as $field ) {
    1264                         if ( !empty( $where_format ) )
    1265                                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    1266                         elseif ( isset( $this->field_types[$field] ) )
    1267                                 $form = $this->field_types[$field];
    1268                         else
    1269                                 $form = '%s';
    1270                         $wheres[] = "`$field` = {$form}";
    1271                 }
     1307                $wheres = $this->where_clauses( $table, $where, $where_format );
    12721308
    12731309                $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
    12741310                return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
     
    12961332                if ( ! is_array( $where ) )
    12971333                        return false;
    12981334
    1299                 $bits = $wheres = array();
     1335                $wheres = $this->where_clauses( $table, $where, $where_format );
    13001336
    1301                 $where_formats = $where_format = (array) $where_format;
    1302 
    1303                 foreach ( array_keys( $where ) as $field ) {
    1304                         if ( !empty( $where_format ) ) {
    1305                                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    1306                         } elseif ( isset( $this->field_types[ $field ] ) ) {
    1307                                 $form = $this->field_types[ $field ];
    1308                         } else {
    1309                                 $form = '%s';
    1310                         }
    1311 
    1312                         $wheres[] = "$field = $form";
    1313                 }
    1314 
    13151337                $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
    13161338                return $this->query( $this->prepare( $sql, $where ) );
    13171339        }