From cb5b8479f0c75631a79f9d2815da7c58ed19f575 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 30 May 2016 04:35:16 +0000 Subject: [PATCH] In `WP_Meta_Query`, don't cast `meta_value` to `CHAR`. `CHAR` is redundant, since the `meta_value` column is `LONGTEXT`. Meanwhile, use of `CAST()` causes MySQL to ignore any index that the administrator may have added to the column. A number of automated tests were doing searches for `CAST` in the SQL strings generated by `WP_Meta_Query` (for reasons unrelated to the `CAST()` behavior). These tests have been updated to expect the new query format. Props ericlewis. Fixes #36625. git-svn-id: https://develop.svn.wordpress.org/trunk@37594 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-meta-query.php | 6 +++++- tests/phpunit/tests/meta/query.php | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-meta-query.php b/src/wp-includes/class-wp-meta-query.php index b1369a067d..ddafc6181b 100644 --- a/src/wp-includes/class-wp-meta-query.php +++ b/src/wp-includes/class-wp-meta-query.php @@ -632,7 +632,11 @@ class WP_Meta_Query { } if ( $where ) { - $sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}"; + if ( 'CHAR' === $meta_type ) { + $sql_chunks['where'][] = "$alias.meta_value {$meta_compare} {$where}"; + } else { + $sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}"; + } } } diff --git a/tests/phpunit/tests/meta/query.php b/tests/phpunit/tests/meta/query.php index d3b315ac13..a2a66e5358 100644 --- a/tests/phpunit/tests/meta/query.php +++ b/tests/phpunit/tests/meta/query.php @@ -493,7 +493,7 @@ class Tests_Meta_Query extends WP_UnitTestCase { ) ); $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); - $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) ); + $this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = ''" ) ); } /** @@ -601,7 +601,7 @@ class Tests_Meta_Query extends WP_UnitTestCase { $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); - $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) ); + $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = ''" ) ); } public function test_get_sql_convert_lowercase_compare_to_uppercase() { @@ -632,7 +632,7 @@ class Tests_Meta_Query extends WP_UnitTestCase { $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); - $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) IN" ) ); + $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value IN" ) ); } public function test_get_sql_empty_meta_compare_with_non_array_value() { @@ -647,7 +647,7 @@ class Tests_Meta_Query extends WP_UnitTestCase { $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); - $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) ); + $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value =" ) ); } public function test_get_sql_invalid_meta_compare() { @@ -663,7 +663,7 @@ class Tests_Meta_Query extends WP_UnitTestCase { $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); - $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) ); + $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value =" ) ); } /** @@ -760,7 +760,7 @@ class Tests_Meta_Query extends WP_UnitTestCase { $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this ); - $this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = 'bar'" ) ); + $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = 'bar'" ) ); } public function test_not_exists() {