forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.php
More file actions
197 lines (154 loc) · 6.25 KB
/
query.php
File metadata and controls
197 lines (154 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Test WP_Meta_Query, in wp-includes/meta.php
*
* @group meta
*/
class Tests_Meta_Query extends WP_UnitTestCase {
function test_default_relation() {
$query = new WP_Meta_Query( array( array( 'key' => 'abc' ) ) );
$this->assertEquals( 'AND', $query->relation );
}
function test_set_relation() {
$query = new WP_Meta_Query( array( array( 'key' => 'abc' ), 'relation' => 'AND' ) );
$this->assertEquals( 'AND', $query->relation );
$query = new WP_Meta_Query( array( array( 'key' => 'abc' ), 'relation' => 'OR' ) );
$this->assertEquals( 'OR', $query->relation );
}
/**
* Test all key only meta queries use the same INNER JOIN when using relation=OR
*
* @ticket 19729
*/
function test_single_inner_join_for_keys_only() {
global $wpdb;
$query = new WP_Meta_Query( array(
array( 'key' => 'abc' ),
array( 'key' => 'def' ),
'relation' => 'OR'
) );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID' );
$this->assertEquals( 1, substr_count( $sql['join'], 'INNER JOIN' ) );
// also check mixing key and key => value
$query = new WP_Meta_Query( array(
array( 'key' => 'abc' ),
array( 'key' => 'def' ),
array( 'key' => 'ghi', 'value' => 'abc' ),
'relation' => 'OR'
) );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID' );
$this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
}
/**
* Test the conversion between "WP_Query" style meta args (meta_value=x&meta_key=y)
* to a meta query array
*/
function test_parse_query_vars() {
$query = new WP_Meta_Query();
// just meta_value
$query->parse_query_vars( array( 'meta_key' => 'abc' ) );
$this->assertEquals( array( array( 'key' => 'abc' ) ), $query->queries );
// meta_key & meta_value
$query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_value' => 'def' ) );
$this->assertEquals( array( array( 'key' => 'abc', 'value' => 'def' ) ), $query->queries );
// meta_compare
$query->parse_query_vars( array( 'meta_key' => 'abc', 'meta_compare' => '=>' ) );
$this->assertEquals( array( array( 'key' => 'abc', 'compare' => '=>' ) ), $query->queries );
}
/**
* @ticket 22096
*/
function test_empty_value_sql() {
global $wpdb;
$query = new WP_Meta_Query();
$the_complex_query['meta_query'] = array(
array( 'key' => 'my_first_key', 'value' => 'my_amazing_value' ),
array( 'key' => 'my_second_key', 'compare' => 'NOT EXISTS' ),
array( 'key' => 'my_third_key', 'value' => array( ), 'compare' => 'IN' ),
);
$query->parse_query_vars( $the_complex_query );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
// We should have 2 joins - one for my_first_key and one for my_second_key
$this->assertEquals( 2, substr_count( $sql['join'], 'INNER JOIN' ) );
// The WHERE should check my_third_key against an unaliased table
$this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
}
/**
* @ticket 22967
*/
function test_null_value_sql() {
global $wpdb;
$query = new WP_Meta_Query( array(
array( 'key' => 'abc', 'value' => null, 'compare' => '=' )
) );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
$this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
}
/**
* @ticket 23033
*/
function test_get_cast_for_type() {
$query = new WP_Meta_Query();
$this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) );
$this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) );
$this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) );
$this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) );
$this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) );
$this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) );
$this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) );
$this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) );
$this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
$this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) );
$this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) );
$this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) );
$this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
$this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
$this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
}
function test_not_exists() {
global $wpdb;
$query = new WP_Meta_Query( array(
'relation' => 'OR',
array(
'key' => 'exclude',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'exclude',
'compare' => '!=',
'value' => '1'
),
) );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
$this->assertNotContains( "{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where'] );
$this->assertContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
}
function test_empty_compare() {
global $wpdb;
$query = new WP_Meta_Query( array(
'relation' => 'OR',
array(
'key' => 'exclude',
'compare' => ''
),
array(
'key' => 'exclude',
'compare' => '!=',
'value' => '1'
),
) );
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
$this->assertContains( "{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where'] );
$this->assertNotContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
}
}