WordPress.org

Make WordPress Core

Changeset 15580


Ignore:
Timestamp:
09/06/2010 11:04:50 AM (10 years ago)
Author:
scribu
Message:

Use multiple JOINs instead of CASE in _wp_meta_sql(). See #14572

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r15579 r15580  
    42444244 *
    42454245 * @param array $queries An array of queries
    4246  * @param string $meta_id_column The column that holds the object id
    4247  * @return string
    4248  */
    4249 function _wp_meta_sql( $queries, $meta_id_column ) {
     4246 * @param string $primary_table
     4247 * @param string $primary_id_column
     4248 * @param string $meta_table
     4249 * @param string $meta_id_column
     4250 * @return array( $join_sql, $where_sql )
     4251 */
     4252function _wp_meta_sql( $queries, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
    42504253    global $wpdb;
    42514254
     
    42634266            continue;
    42644267
    4265         $clause = $wpdb->prepare( "WHEN %s THEN meta_value ", $meta_key );
    4266 
    42674268        if ( empty( $meta_value ) ) {
    4268             $clauses[] = $clause . "IS NOT NULL";
     4269            $clauses[ $meta_key ] = "";
    42694270        } elseif ( 'like' == $meta_compare ) {
    4270             $clauses[] = $clause . $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' );
     4271            $clauses[ $meta_key ] = $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' );
    42714272        } else {
    4272             $clauses[] = $clause . $wpdb->prepare( "$meta_compare %s", $meta_value );
     4273            $clauses[ $meta_key ] = $wpdb->prepare( "$meta_compare %s", $meta_value );
    42734274        }
    42744275    }
    42754276
    42764277    if ( empty( $clauses ) )
    4277         return '';
    4278 
    4279     return "
    4280         AND CASE meta_key
    4281         " . implode( "\n", $clauses ) . "
    4282         END
    4283         GROUP BY $meta_id_column
    4284         HAVING COUNT(*) = " . count( $clauses );
    4285 }
    4286 
     4278        return array('', '');
     4279
     4280    $join = '';
     4281    $where = '';
     4282
     4283    $i = 0;
     4284    foreach ( $clauses as $meta_key => $value_query ) {
     4285        $alias = $i ? 'mt' . $i : $meta_table;
     4286
     4287        $join .= "\nINNER JOIN $meta_table";
     4288        $join .= $i ? " AS $alias" : '';
     4289        $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
     4290
     4291        $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
     4292        if ( !empty( $value_query ) )
     4293            $where .= " AND $alias.meta_value $value_query";
     4294           
     4295        $i++;
     4296    }
     4297
     4298    return array( $join, $where );
     4299}
    42874300
    42884301/*
  • trunk/wp-includes/user.php

    r15579 r15580  
    464464        $meta_queries[] = wp_array_slice_assoc( $qv, array( 'meta_key', 'meta_value', 'meta_compare' ) );
    465465
    466         $meta_query_sql = _wp_meta_sql( $meta_queries, 'user_id' );
    467 
    468         if ( !empty( $meta_query_sql ) ) {
    469             $this->query_from .= " INNER JOIN $wpdb->usermeta ON ($wpdb->users.ID = $wpdb->usermeta.user_id)";
    470             $this->query_where .= $meta_query_sql;
    471         }
     466        list( $meta_join, $meta_where ) = _wp_meta_sql( $meta_queries, $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' );
     467        $this->query_from .= $meta_join;
     468        $this->query_where .= $meta_where;
    472469
    473470        if ( !empty($qv['include']) ) {
Note: See TracChangeset for help on using the changeset viewer.