Make WordPress Core


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

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

File:
1 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/*
Note: See TracChangeset for help on using the changeset viewer.