This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
views_plugin_query_slave.inc
87 lines (80 loc) · 2.75 KB
/
views_plugin_query_slave.inc
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
<?php
// $Id$
/**
* @file views_plugin_query_slave.inc
* Defines the default query object which builds SQL to execute using the slave
* database API within Pressflow.
*/
/**
* Extension of views_plugin_query_default to query a slave db if it exists. The
* slave database is only queried against if there is caching in place on the
* view.
*/
class views_plugin_query_slave extends views_plugin_query_default{
/**
* Whether or not this view is safe to be run against the Slave database.
*
* @var boolean
*/
protected $slave_safe = FALSE;
/**
* Builds the necessary info to execute the query.
*
* The only reason this is here is to set up $slave_safe. In this example, we'll
* assume that if this display can be cached, it can also run it's queries
* against the slave database. If caching is enabled, we use Pressflow's
* db_query_slave() and db_query_range_slave() in lieu of db_query() &
* db_query_range(), respectively. Feel free to use your own method for
* determining which views are 'slave_safe'.
*/
function build(&$view) {
// Load up the cache settings.
$cache_settings = $view->display_handler->get_option('cache');
// If caching is anything but 'none', we assume this display is 'slave_safe'.
$this->slave_safe = $cache_settings['type'] != 'none';
// Here's just a little informational message to show us it's working. You'll
// probably want to delete this. ;)
if ($this->slave_safe) {
$t_args = array(
'%display' => $view->current_display,
'%view' => $view->name,
);
drupal_set_message(t('The %display display of the %view view is querying the slave database. Yay!', $t_args));
}
return parent::build($view);
}
/**
* Wrapper method for db_query().
*
* @param $query
* A string containing an SQL query.
* @param $args
* An array containing the query arguments.
* @return
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
function db_query($query, $args = array()) {
$fnc = $this->slave_safe ? 'db_query_slave' : 'db_query';
return $fnc($query, $args);
}
/**
* Wrapper method for db_query_range().
*
* @param $query
* A string containing an SQL query.
* @param $from
* The first result row to return.
* @param $count
* The maximum number of result rows to return.
* @param $args
* An array containing the query arguments.
* @return
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
function db_query_range($query, $from, $count, $args = array()) {
$fnc = $this->slave_safe ? 'db_query_range_slave' : 'db_query_range';
return $fnc($query, $from, $count, $args);
}
}