forked from WordPress/WordPress-Coding-Standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectDatabaseQueryStandard.xml
More file actions
119 lines (110 loc) · 2.85 KB
/
DirectDatabaseQueryStandard.xml
File metadata and controls
119 lines (110 loc) · 2.85 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
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Direct Database Query"
>
<standard>
<![CDATA[
You should not make direct database queries, but instead use abstractions such as WP_Query.
]]>
</standard>
<code_comparison>
<code title="Valid: Using WP_Query to retrieve data.">
<![CDATA[
$my_query = new WP_Query(
[
'post_type' => 'foo',
]
);
]]>
</code>
<code title="Invalid: Using a direct query to retrieve data.">
<![CDATA[
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM x WHERE y = %s",
'foo',
),
);
]]>
</code>
</code_comparison>
<standard>
<![CDATA[
You should not make direct database queries without caching.
]]>
</standard>
<code_comparison>
<code title="Valid: The results of a direct database query are stored in the object cache.">
<![CDATA[
function foo() {
global $wpdb;
$cached = wp_cache_get( 'foo' );
if ( false !== $cached ) {
return $cached;
}
$results = $wpdb->query(
'SELECT x FROM foo'
);
wp_cache_set( 'foo', $results );
return $results;
}
]]>
</code>
<code title="Invalid: Direct database query is made without leveraging the object cache.">
<![CDATA[
function foo() {
global $wpdb;
return $wpdb->query( 'SELECT x FROM foo' );
}
]]>
</code>
</code_comparison>
<code_comparison>
<code title="Valid: The results of a direct database query are stored in the object cache.">
<![CDATA[
function foo() {
global $wpdb;
$cached = wp_cache_get( 'foo' );
if ( false !== $cached ) {
return $cached;
}
$results = $wpdb->query(
'SELECT x FROM foo'
);
wp_cache_set( 'foo', $results );
return $results;
}
]]>
</code>
<code title="Invalid: Incorrect implementation of caching with a direct database query.">
<![CDATA[
function foo() {
global $wpdb;
$results = $wpdb->query(
'SELECT * from foo'
);
wp_cache_set( 'foo', $results );
return $results;
}
]]>
</code>
</code_comparison>
<standard>
<![CDATA[
You should not make direct database queries to alter the database schema.
]]>
</standard>
<code_comparison>
<code title="Valid: Altering database schema using the dbDelta function.">
<![CDATA[
dbDelta( 'CREATE TABLE foo' );
]]>
</code>
<code title="Invalid: Altering database schema using a direct database query.">
<![CDATA[
$wpdb->query( 'CREATE TABLE foo' );
]]>
</code>
</code_comparison>
</documentation>