Reduce `select *` usage

This commit is contained in:
Mike Jolley 2017-02-13 12:58:42 +00:00
parent cfa3c34ada
commit d080e135ee
12 changed files with 36 additions and 34 deletions

View File

@ -252,7 +252,7 @@ class WC_Admin_Attributes {
$edit = absint( $_GET['edit'] );
$attribute_to_edit = $wpdb->get_row( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_id = '$edit'" );
$attribute_to_edit = $wpdb->get_row( "SELECT attribute_type, attribute_label, attribute_name, attribute_orderby, attribute_public FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_id = '$edit'" );
?>
<div class="wrap woocommerce">

View File

@ -246,15 +246,17 @@ class WC_Admin_Dashboard {
*/
public function recent_reviews() {
global $wpdb;
$comments = $wpdb->get_results( "SELECT *, SUBSTRING(comment_content,1,100) AS comment_excerpt
FROM $wpdb->comments
LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHERE comment_approved = '1'
AND comment_type = ''
AND post_password = ''
AND post_type = 'product'
ORDER BY comment_date_gmt DESC
LIMIT 8" );
$comments = $wpdb->get_results( "
SELECT posts.ID, posts.post_title, comments.comment_author, comments.comment_ID, SUBSTRING(comments.comment_content,1,100) AS comment_excerpt
FROM $wpdb->comments comments
LEFT JOIN $wpdb->posts posts ON (comments.comment_post_ID = posts.ID)
WHERE comments.comment_approved = '1'
AND comments.comment_type = ''
AND posts.post_password = ''
AND posts.post_type = 'product'
ORDER BY comments.comment_date_gmt DESC
LIMIT 5
" );
if ( $comments ) {
echo '<ul>';

View File

@ -143,14 +143,14 @@ class WC_Admin_Duplicate_Product {
return false;
}
$post = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE ID=$id" );
$post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) );
if ( isset( $post->post_type ) && 'revision' === $post->post_type ) {
$id = $post->post_parent;
$post = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE ID=$id" );
$post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) );
}
return $post[0];
return $post;
}
}

View File

@ -668,7 +668,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
// Verify provided line item ID is associated with order.
if ( 'update' === $action ) {
$result = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
$wpdb->prepare( "SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
absint( $posted['id'] ),
absint( $order->get_id() )
) );

View File

@ -661,7 +661,7 @@ class WC_Tax {
$rate = $key_or_rate;
} else {
$key = $key_or_rate;
$rate = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $key ) );
$rate = $wpdb->get_row( $wpdb->prepare( "SELECT tax_rate_country, tax_rate_state, tax_rate_name, tax_rate_priority FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $key ) );
}
$code_string = '';

View File

@ -255,7 +255,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
public function read_items( $order, $type ) {
global $wpdb;
$get_items_sql = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s ORDER BY order_item_id;", $order->get_id(), $type );
$get_items_sql = $wpdb->prepare( "SELECT order_item_type, order_item_id, order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s ORDER BY order_item_id;", $order->get_id(), $type );
$items = $wpdb->get_results( $get_items_sql );
if ( ! empty( $items ) ) {

View File

@ -101,7 +101,7 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
$item->set_defaults();
$data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item->get_id() ) );
$data = $wpdb->get_row( $wpdb->prepare( "SELECT order_id, order_item_name, order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item->get_id() ) );
if ( ! $data ) {
throw new Exception( __( 'Invalid order item.', 'woocommerce' ) );

View File

@ -123,7 +123,7 @@ class WC_Payment_Token_Data_Store extends WC_Data_Store_WP implements WC_Payment
*/
public function read( &$token ) {
global $wpdb;
if ( $data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d LIMIT 1;", $token->get_id() ) ) ) {
if ( $data = $wpdb->get_row( $wpdb->prepare( "SELECT token, user_id, gateway_id, is_default FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d LIMIT 1;", $token->get_id() ) ) ) {
$token->set_props( array(
'token' => $data->token,
'user_id' => $data->user_id,

View File

@ -67,7 +67,7 @@ class WC_Shipping_Zone_Data_Store extends WC_Data_Store_WP implements WC_Shippin
$zone->read_meta_data();
$zone->set_object_read( true );
do_action( 'woocommerce_shipping_zone_loaded', $zone );
} elseif ( $zone_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1;", $zone->get_id() ) ) ) {
} elseif ( $zone_data = $wpdb->get_row( $wpdb->prepare( "SELECT zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1;", $zone->get_id() ) ) ) {
$zone->set_zone_name( $zone_data->zone_name );
$zone->set_zone_order( $zone_data->zone_order );
$this->read_zone_locations( $zone );
@ -176,7 +176,7 @@ class WC_Shipping_Zone_Data_Store extends WC_Data_Store_WP implements WC_Shippin
*/
public function get_method( $instance_id ) {
global $wpdb;
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d LIMIT 1;", $instance_id ) );
return $wpdb->get_row( $wpdb->prepare( "SELECT zone_id, method_id, instance_id, method_order, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d LIMIT 1;", $instance_id ) );
}
/**
@ -254,7 +254,7 @@ class WC_Shipping_Zone_Data_Store extends WC_Data_Store_WP implements WC_Shippin
*/
private function read_zone_locations( &$zone ) {
global $wpdb;
if ( $locations = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id = %d;", $zone->get_id() ) ) ) {
if ( $locations = $wpdb->get_results( $wpdb->prepare( "SELECT location_code, location_type FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id = %d;", $zone->get_id() ) ) ) {
foreach ( $locations as $location ) {
$zone->add_location( $location->location_code, $location->location_type );
}

View File

@ -273,12 +273,12 @@ if ( ! class_exists( 'WP_Background_Process' ) ) {
$key = $this->identifier . '_batch_%';
$query = $wpdb->get_row( $wpdb->prepare( "
SELECT *
FROM {$table}
WHERE {$column} LIKE %s
ORDER BY {$key_column} ASC
LIMIT 1
", $key ) );
SELECT {$value_column}, {$column}
FROM {$table}
WHERE {$column} LIKE %s
ORDER BY {$key_column} ASC
LIMIT 1
", $key ) );
$batch = new stdClass();
$batch->key = $query->$column;

View File

@ -260,7 +260,7 @@ function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new
if ( get_option( 'db_version' ) < 34370 ) {
if ( 'product_cat' === $taxonomy || taxonomy_is_product_attribute( $taxonomy ) ) {
$old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_termmeta WHERE woocommerce_term_id = %d;", $old_term_id ) );
$old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->prefix}woocommerce_termmeta WHERE woocommerce_term_id = %d;", $old_term_id ) );
// Copy across to split term
if ( $old_meta_data ) {

View File

@ -18,7 +18,7 @@ function wc_update_200_file_paths() {
global $wpdb;
// Upgrade old style files paths to support multiple file paths
$existing_file_paths = $wpdb->get_results( "SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_file_path' AND meta_value != '';" );
$existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id, post_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_path' AND meta_value != '';" );
if ( $existing_file_paths ) {
@ -192,7 +192,7 @@ function wc_update_200_line_items() {
// Now its time for the massive update to line items - move them to the new DB tables
// Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_items' WHERE meta_key = '_order_items_old'
$order_item_rows = $wpdb->get_results( "
SELECT * FROM {$wpdb->postmeta}
SELECT meta_value, post_id FROM {$wpdb->postmeta}
WHERE meta_key = '_order_items'
" );
@ -268,7 +268,7 @@ function wc_update_200_line_items() {
// Do the same kind of update for order_taxes - move to lines
// Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_taxes' WHERE meta_key = '_order_taxes_old'
$order_tax_rows = $wpdb->get_results( "
SELECT * FROM {$wpdb->postmeta}
SELECT meta_value, post_id FROM {$wpdb->postmeta}
WHERE meta_key = '_order_taxes'
" );
@ -397,7 +397,7 @@ function wc_update_210_file_paths() {
global $wpdb;
// Upgrade file paths to support multiple file paths + names etc
$existing_file_paths = $wpdb->get_results( "SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_file_paths' AND meta_value != '';" );
$existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_paths' AND meta_value != '';" );
if ( $existing_file_paths ) {
@ -556,7 +556,7 @@ function wc_update_220_variations() {
function wc_update_220_attributes() {
global $wpdb;
// Update taxonomy names with correct sanitized names
$attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies" );
$attribute_taxonomies = $wpdb->get_results( "SELECT attribute_name, attribute_id FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies" );
foreach ( $attribute_taxonomies as $attribute_taxonomy ) {
$sanitized_attribute_name = wc_sanitize_taxonomy_name( $attribute_taxonomy->attribute_name );
@ -886,7 +886,7 @@ function wc_update_260_zone_methods() {
* Migrate the old data out of woocommerce_shipping_zone_shipping_methods into the new table and port over any known options (used by table rates and flat rate boxes).
*/
if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_shipping_zone_shipping_methods';" ) ) {
$old_methods = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" );
$old_methods = $wpdb->get_results( "SELECT zone_id, shipping_method_type, shipping_method_order, shipping_method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" );
if ( $old_methods ) {
$max_new_id = $wpdb->get_var( "SELECT MAX(instance_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" );