Make get_meta_sql() a standalone function. See #15032

git-svn-id: http://svn.automattic.com/wordpress/trunk@16266 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-11-09 23:22:13 +00:00
parent 2f25f589db
commit db1d9369eb
4 changed files with 94 additions and 95 deletions

View File

@ -71,99 +71,6 @@ class WP_Object_Query {
$qv['meta_query'] = $meta_query;
}
/*
* Used internally to generate an SQL string for searching across multiple meta key = value pairs
*
* @access protected
* @since 3.1.0
*
* @param array $meta_query List of metadata queries. A single query is an associative array:
* - 'key' string The meta key
* - 'value' string|array The meta value
* - 'compare' (optional) string How to compare the key to the value.
* Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
* Default: '='
* - 'type' string (optional) The type of the value.
* Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
* Default: 'CHAR'
*
* @param string $meta_type
* @param string $primary_table
* @param string $primary_id_column
* @return array( $join_sql, $where_sql )
*/
function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) {
global $wpdb;
if ( ! $meta_table = _get_meta_table( $meta_type ) )
return false;
$meta_id_column = esc_sql( $meta_type . '_id' );
$clauses = array();
$join = '';
$where = '';
$i = 0;
foreach ( $meta_query as $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_value = isset( $q['value'] ) ? $q['value'] : '';
$meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
$meta_compare = '=';
if ( 'NUMERIC' == $meta_type )
$meta_type = 'SIGNED';
elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
$meta_type = 'CHAR';
if ( empty( $meta_key ) && empty( $meta_value ) )
continue;
$alias = $i ? 'mt' . $i : $meta_table;
$join .= "\nINNER JOIN $meta_table";
$join .= $i ? " AS $alias" : '';
$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
$i++;
if ( !empty( $meta_key ) )
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
if ( ! is_array( $meta_value ) )
$meta_value = preg_split( '/[,\s]+/', $meta_value );
} else {
$meta_value = trim( $meta_value );
}
if ( empty( $meta_value ) )
continue;
if ( 'IN' == substr( $meta_compare, -2) ) {
$meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );
$meta_compare_string = "($meta_field_types)";
unset( $meta_field_types );
} elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
$meta_value = array_slice( $meta_value, 0, 2 );
$meta_compare_string = '%s AND %s';
} elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
$meta_value = '%' . like_escape( $meta_value ) . '%';
$meta_compare_string = '%s';
} else {
$meta_compare_string = '%s';
}
$where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
unset( $meta_compare_string );
}
return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column );
}
/*
* Used internally to generate an SQL string for searching across multiple taxonomies
*

View File

@ -351,6 +351,98 @@ function update_meta_cache($meta_type, $object_ids) {
return $cache;
}
/*
* Given a meta query, generates SQL clauses to be appended to a main query
*
* @since 3.1.0
*
* @param array $meta_query List of metadata queries. A single query is an associative array:
* - 'key' string The meta key
* - 'value' string|array The meta value
* - 'compare' (optional) string How to compare the key to the value.
* Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
* Default: '='
* - 'type' string (optional) The type of the value.
* Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
* Default: 'CHAR'
*
* @param string $meta_type
* @param string $primary_table
* @param string $primary_id_column
* @return array( 'join' => $join_sql, 'where' => $where_sql )
*/
function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) {
global $wpdb;
if ( ! $meta_table = _get_meta_table( $meta_type ) )
return false;
$meta_id_column = esc_sql( $meta_type . '_id' );
$clauses = array();
$join = '';
$where = '';
$i = 0;
foreach ( $meta_query as $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_value = isset( $q['value'] ) ? $q['value'] : '';
$meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
$meta_compare = '=';
if ( 'NUMERIC' == $meta_type )
$meta_type = 'SIGNED';
elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
$meta_type = 'CHAR';
if ( empty( $meta_key ) && empty( $meta_value ) )
continue;
$alias = $i ? 'mt' . $i : $meta_table;
$join .= "\nINNER JOIN $meta_table";
$join .= $i ? " AS $alias" : '';
$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
$i++;
if ( !empty( $meta_key ) )
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
if ( ! is_array( $meta_value ) )
$meta_value = preg_split( '/[,\s]+/', $meta_value );
} else {
$meta_value = trim( $meta_value );
}
if ( empty( $meta_value ) )
continue;
if ( 'IN' == substr( $meta_compare, -2) ) {
$meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );
$meta_compare_string = "($meta_field_types)";
unset( $meta_field_types );
} elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
$meta_value = array_slice( $meta_value, 0, 2 );
$meta_compare_string = '%s AND %s';
} elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
$meta_value = '%' . like_escape( $meta_value ) . '%';
$meta_compare_string = '%s';
} else {
$meta_compare_string = '%s';
}
$where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
unset( $meta_compare_string );
}
return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column );
}
/**
* Retrieve the name of the metadata table for the specified object type.
*

View File

@ -2147,7 +2147,7 @@ class WP_Query extends WP_Object_Query {
}
if ( !empty( $q['meta_query'] ) ) {
$clauses = $this->get_meta_sql( $q['meta_query'], 'post', $wpdb->posts, 'ID' );
$clauses = get_meta_sql( $q['meta_query'], 'post', $wpdb->posts, 'ID' );
$join .= $clauses['join'];
$where .= $clauses['where'];
}

View File

@ -481,7 +481,7 @@ class WP_User_Query extends WP_Object_Query {
}
if ( !empty( $qv['meta_query'] ) ) {
$clauses = $this->get_meta_sql( $qv['meta_query'], 'user', $wpdb->users, 'ID' );
$clauses = get_meta_sql( $qv['meta_query'], 'user', $wpdb->users, 'ID' );
$this->query_from .= $clauses['join'];
$this->query_where .= $clauses['where'];
}