Révision 5d12d676
Ajouté par Assos Assos il y a environ 6 ans
drupal7/sites/all/modules/views/plugins/views_plugin_query_default.inc | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Defines the default query object.
|
|
5 |
* Definition of views_plugin_query_default.
|
|
6 | 6 |
*/ |
7 | 7 |
|
8 | 8 |
/** |
... | ... | |
15 | 15 |
/** |
16 | 16 |
* A list of tables in the order they should be added, keyed by alias. |
17 | 17 |
*/ |
18 |
var $table_queue = array();
|
|
18 |
public $table_queue = array();
|
|
19 | 19 |
|
20 | 20 |
/** |
21 |
* Holds an array of tables and counts added so that we can create aliases |
|
21 |
* Holds an array of tables and counts added so that we can create aliases.
|
|
22 | 22 |
*/ |
23 |
var $tables = array();
|
|
23 |
public $tables = array();
|
|
24 | 24 |
|
25 | 25 |
/** |
26 | 26 |
* Holds an array of relationships, which are aliases of the primary |
27 | 27 |
* table that represent different ways to join the same table in. |
28 | 28 |
*/ |
29 |
var $relationships = array();
|
|
29 |
public $relationships = array();
|
|
30 | 30 |
|
31 | 31 |
/** |
32 | 32 |
* An array of sections of the WHERE query. Each section is in itself |
33 | 33 |
* an array of pieces and a flag as to whether or not it should be AND |
34 | 34 |
* or OR. |
35 | 35 |
*/ |
36 |
var $where = array(); |
|
36 |
public $where = array(); |
|
37 |
|
|
37 | 38 |
/** |
38 | 39 |
* An array of sections of the HAVING query. Each section is in itself |
39 | 40 |
* an array of pieces and a flag as to whether or not it should be AND |
40 | 41 |
* or OR. |
41 | 42 |
*/ |
42 |
var $having = array(); |
|
43 |
public $having = array(); |
|
44 |
|
|
43 | 45 |
/** |
44 | 46 |
* The default operator to use when connecting the WHERE groups. May be |
45 | 47 |
* AND or OR. |
46 | 48 |
*/ |
47 |
var $group_operator = 'AND';
|
|
49 |
public $group_operator = 'AND';
|
|
48 | 50 |
|
49 | 51 |
/** |
50 | 52 |
* A simple array of order by clauses. |
51 | 53 |
*/ |
52 |
var $orderby = array();
|
|
54 |
public $orderby = array();
|
|
53 | 55 |
|
54 | 56 |
/** |
55 | 57 |
* A simple array of group by clauses. |
56 | 58 |
*/ |
57 |
var $groupby = array(); |
|
58 |
|
|
59 |
public $groupby = array(); |
|
59 | 60 |
|
60 | 61 |
/** |
61 | 62 |
* An array of fields. |
62 | 63 |
*/ |
63 |
var $fields = array(); |
|
64 |
|
|
64 |
public $fields = array(); |
|
65 | 65 |
|
66 | 66 |
/** |
67 | 67 |
* The table header to use for tablesort. This matters because tablesort |
68 | 68 |
* needs to modify the query and needs the header. |
69 | 69 |
*/ |
70 |
var $header = array();
|
|
70 |
public $header = array();
|
|
71 | 71 |
|
72 | 72 |
/** |
73 | 73 |
* A flag as to whether or not to make the primary field distinct. |
74 | 74 |
*/ |
75 |
var $distinct = FALSE;
|
|
75 |
public $distinct = FALSE;
|
|
76 | 76 |
|
77 |
var $has_aggregate = FALSE; |
|
77 |
/** |
|
78 |
* |
|
79 |
*/ |
|
80 |
public $has_aggregate = FALSE; |
|
78 | 81 |
|
79 | 82 |
/** |
80 | 83 |
* Should this query be optimized for counts, for example no sorts. |
81 | 84 |
*/ |
82 |
var $get_count_optimized = NULL;
|
|
85 |
public $get_count_optimized = NULL;
|
|
83 | 86 |
|
84 | 87 |
/** |
85 | 88 |
* The current used pager plugin. |
86 | 89 |
* |
87 | 90 |
* @var views_plugin_pager |
88 | 91 |
*/ |
89 |
var $pager = NULL;
|
|
92 |
public $pager = NULL;
|
|
90 | 93 |
|
91 |
/**
|
|
94 |
/** |
|
92 | 95 |
* An array mapping table aliases and field names to field aliases. |
93 | 96 |
*/ |
94 |
var $field_aliases = array();
|
|
97 |
public $field_aliases = array();
|
|
95 | 98 |
|
96 |
/**
|
|
99 |
/** |
|
97 | 100 |
* Query tags which will be passed over to the dbtng query object. |
98 | 101 |
*/ |
99 |
var $tags = array();
|
|
102 |
public $tags = array();
|
|
100 | 103 |
|
101 | 104 |
/** |
102 | 105 |
* Is the view marked as not distinct. |
103 | 106 |
* |
104 | 107 |
* @var bool |
105 | 108 |
*/ |
106 |
var $no_distinct;
|
|
109 |
public $no_distinct;
|
|
107 | 110 |
|
108 | 111 |
/** |
109 | 112 |
* Defines the distinct type. |
... | ... | |
115 | 118 |
public $pure_distinct = FALSE; |
116 | 119 |
|
117 | 120 |
/** |
118 |
* Constructor; Create the basic query object and fill with default values.
|
|
121 |
* {@inheritdoc}
|
|
119 | 122 |
*/ |
120 |
function init($base_table = 'node', $base_field = 'nid', $options) { |
|
123 |
public function init($base_table = 'node', $base_field = 'nid', $options) {
|
|
121 | 124 |
parent::init($base_table, $base_field, $options); |
122 |
$this->base_table = $base_table; // Predefine these above, for clarity. |
|
125 |
$this->base_table = $base_table; |
|
126 |
// Predefine these above, for clarity. |
|
123 | 127 |
$this->base_field = $base_field; |
124 | 128 |
$this->relationships[$base_table] = array( |
125 | 129 |
'link' => NULL, |
126 | 130 |
'table' => $base_table, |
127 | 131 |
'alias' => $base_table, |
128 |
'base' => $base_table |
|
132 |
'base' => $base_table,
|
|
129 | 133 |
); |
130 | 134 |
|
131 |
// init the table queue with our primary table.
|
|
135 |
// Unit the table queue with our primary table.
|
|
132 | 136 |
$this->table_queue[$base_table] = array( |
133 | 137 |
'alias' => $base_table, |
134 | 138 |
'table' => $base_table, |
... | ... | |
136 | 140 |
'join' => NULL, |
137 | 141 |
); |
138 | 142 |
|
139 |
// init the tables with our primary table
|
|
143 |
// Init the tables with our primary table.
|
|
140 | 144 |
$this->tables[$base_table][$base_table] = array( |
141 | 145 |
'count' => 1, |
142 | 146 |
'alias' => $base_table, |
143 | 147 |
); |
144 | 148 |
|
145 |
/** |
|
146 |
* -- we no longer want the base field to appear automatically. |
|
147 |
if ($base_field) { |
|
148 |
$this->fields[$base_field] = array( |
|
149 |
'table' => $base_table, |
|
150 |
'field' => $base_field, |
|
151 |
'alias' => $base_field, |
|
152 |
); |
|
153 |
} |
|
154 |
*/ |
|
149 |
// We no longer want the base field to appear automatically. |
|
150 |
// if ($base_field) { |
|
151 |
// $this->fields[$base_field] = array( |
|
152 |
// 'table' => $base_table, |
|
153 |
// 'field' => $base_field, |
|
154 |
// 'alias' => $base_field, |
|
155 |
// ); |
|
156 |
// } |
|
155 | 157 |
|
156 | 158 |
$this->count_field = array( |
157 | 159 |
'table' => $base_table, |
... | ... | |
161 | 163 |
); |
162 | 164 |
} |
163 | 165 |
|
164 |
// ---------------------------------------------------------------- |
|
165 |
// Utility methods to set flags and data. |
|
166 |
/** |
|
167 |
* Utility methods to set flags and data. |
|
168 |
*/ |
|
166 | 169 |
|
167 | 170 |
/** |
168 | 171 |
* Set the view to be distinct. |
... | ... | |
175 | 178 |
* @param bool $pure_distinct |
176 | 179 |
* Should only the sql keyword be added. |
177 | 180 |
*/ |
178 |
function set_distinct($value = TRUE, $pure_distinct = FALSE) { |
|
181 |
public function set_distinct($value = TRUE, $pure_distinct = FALSE) {
|
|
179 | 182 |
if (!(isset($this->no_distinct) && $value)) { |
180 | 183 |
$this->distinct = $value; |
181 | 184 |
$this->pure_distinct = $pure_distinct; |
... | ... | |
185 | 188 |
/** |
186 | 189 |
* Set what field the query will count() on for paging. |
187 | 190 |
*/ |
188 |
function set_count_field($table, $field, $alias = NULL) { |
|
191 |
public function set_count_field($table, $field, $alias = NULL) {
|
|
189 | 192 |
if (empty($alias)) { |
190 | 193 |
$alias = $table . '_' . $field; |
191 | 194 |
} |
... | ... | |
198 | 201 |
} |
199 | 202 |
|
200 | 203 |
/** |
201 |
* Set the table header; used for click-sorting because it's needed |
|
202 |
* info to modify the ORDER BY clause. |
|
204 |
* Set the table header. |
|
205 |
* |
|
206 |
* Used for click-sorting because it's needed info to modify the ORDER BY |
|
207 |
* clause. |
|
203 | 208 |
*/ |
204 |
function set_header($header) { |
|
209 |
public function set_header($header) {
|
|
205 | 210 |
$this->header = $header; |
206 | 211 |
} |
207 | 212 |
|
208 |
function option_definition() { |
|
213 |
/** |
|
214 |
* {@inheritdoc} |
|
215 |
*/ |
|
216 |
public function option_definition() { |
|
209 | 217 |
$options = parent::option_definition(); |
210 | 218 |
$options['disable_sql_rewrite'] = array( |
211 | 219 |
'default' => FALSE, |
... | ... | |
237 | 245 |
/** |
238 | 246 |
* Add settings for the ui. |
239 | 247 |
*/ |
240 |
function options_form(&$form, &$form_state) { |
|
248 |
public function options_form(&$form, &$form_state) {
|
|
241 | 249 |
parent::options_form($form, $form_state); |
242 | 250 |
|
243 | 251 |
$form['disable_sql_rewrite'] = array( |
... | ... | |
245 | 253 |
'#description' => t('Disabling SQL rewriting will disable node_access checks as well as other modules that implement hook_query_alter().'), |
246 | 254 |
'#type' => 'checkbox', |
247 | 255 |
'#default_value' => !empty($this->options['disable_sql_rewrite']), |
248 |
'#suffix' => '<div class="messages warning sql-rewrite-warning js-hide">' . t('WARNING: Disabling SQL rewriting means that node access security is disabled. This may allow users to see data they should not be able to see if your view is misconfigured. Please use this option only if you understand and accept this security risk.') . '</div>', |
|
256 |
'#suffix' => '<div class="messages warning sql-rewrite-warning js-hide">' |
|
257 |
. t('WARNING: Disabling SQL rewriting means that node access security is disabled. This may allow users to see data they should not be able to see if your view is misconfigured. Please use this option only if you understand and accept this security risk.') |
|
258 |
. '</div>', |
|
249 | 259 |
); |
250 | 260 |
$form['distinct'] = array( |
251 | 261 |
'#type' => 'checkbox', |
... | ... | |
284 | 294 |
/** |
285 | 295 |
* Special submit handling. |
286 | 296 |
*/ |
287 |
function options_submit(&$form, &$form_state) { |
|
297 |
public function options_submit(&$form, &$form_state) {
|
|
288 | 298 |
$element = array('#parents' => array('query', 'options', 'query_tags')); |
289 | 299 |
$value = explode(',', drupal_array_get_nested_value($form_state['values'], $element['#parents'])); |
290 | 300 |
$value = array_filter(array_map('trim', $value)); |
291 | 301 |
form_set_value($element, $value, $form_state); |
292 | 302 |
} |
293 | 303 |
|
294 |
// ---------------------------------------------------------------- |
|
295 |
// Table/join adding |
|
304 |
/** |
|
305 |
* Table/join adding. |
|
306 |
*/ |
|
296 | 307 |
|
297 | 308 |
/** |
298 |
* A relationship is an alternative endpoint to a series of table |
|
299 |
* joins. Relationships must be aliases of the primary table and |
|
300 |
* they must join either to the primary table or to a pre-existing |
|
301 |
* relationship. |
|
309 |
* A relationship is an alternative endpoint to a series of table joins. |
|
302 | 310 |
* |
303 |
* An example of a relationship would be a nodereference table. |
|
304 |
* If you have a nodereference named 'book_parent' which links to a |
|
305 |
* parent node, you could set up a relationship 'node_book_parent' |
|
306 |
* to 'node'. Then, anything that links to 'node' can link to |
|
307 |
* 'node_book_parent' instead, thus allowing all properties of |
|
308 |
* both nodes to be available in the query. |
|
311 |
* Relationships must be aliases of the primary table and they must join |
|
312 |
* either to the primary table or to a pre-existing relationship. |
|
309 | 313 |
* |
310 |
* @param $alias |
|
311 |
* What this relationship will be called, and is also the alias |
|
312 |
* for the table. |
|
314 |
* An example of a relationship would be a nodereference table. If you have a |
|
315 |
* nodereference named 'book_parent' which links to a parent node, you could |
|
316 |
* set up a relationship 'node_book_parent' to 'node'. Then, anything that |
|
317 |
* links to 'node' can link to 'node_book_parent' instead, thus allowing all |
|
318 |
* properties of both nodes to be available in the query. |
|
319 |
* |
|
320 |
* @param string $alias |
|
321 |
* What this relationship will be called, and is also the alias for the |
|
322 |
* table. |
|
313 | 323 |
* @param views_join $join |
314 | 324 |
* A views_join object (or derived object) to join the alias in. |
315 |
* @param $base |
|
316 |
* The name of the 'base' table this relationship represents; this |
|
317 |
* tells the join search which path to attempt to use when finding
|
|
318 |
* the path to this relationship.
|
|
319 |
* @param $link_point |
|
320 |
* If this relationship links to something other than the primary |
|
321 |
* table, specify that table here. For example, a 'track' node
|
|
322 |
* might have a relationship to an 'album' node, which might
|
|
323 |
* have a relationship to an 'artist' node.
|
|
325 |
* @param string $base
|
|
326 |
* The name of the 'base' table this relationship represents; this tells the
|
|
327 |
* join search which path to attempt to use when finding the path to this
|
|
328 |
* relationship. |
|
329 |
* @param string $link_point
|
|
330 |
* If this relationship links to something other than the primary table,
|
|
331 |
* specify that table here. For example, a 'track' node might have a
|
|
332 |
* relationship to an 'album' node, which might have a relationship to an
|
|
333 |
* 'artist' node. |
|
324 | 334 |
*/ |
325 |
function add_relationship($alias, $join, $base, $link_point = NULL) { |
|
335 |
public function add_relationship($alias, $join, $base, $link_point = NULL) {
|
|
326 | 336 |
if (empty($link_point)) { |
327 | 337 |
$link_point = $this->base_table; |
328 | 338 |
} |
... | ... | |
342 | 352 |
$join = $this->adjust_join($join, $link_point); |
343 | 353 |
} |
344 | 354 |
|
345 |
// Add the table directly to the queue to avoid accidentally marking |
|
346 |
// it. |
|
355 |
// Add the table directly to the queue to avoid accidentally marking it. |
|
347 | 356 |
$this->table_queue[$alias] = array( |
348 | 357 |
'table' => $join->table, |
349 | 358 |
'num' => 1, |
... | ... | |
369 | 378 |
/** |
370 | 379 |
* Add a table to the query, ensuring the path exists. |
371 | 380 |
* |
372 |
* This function will test to ensure that the path back to the primary |
|
373 |
* table is valid and exists; if you do not wish for this testing to
|
|
374 |
* occur, use $query->queue_table() instead.
|
|
381 |
* This function will test to ensure that the path back to the primary table
|
|
382 |
* is valid and exists; if you do not wish for this testing to occur, use
|
|
383 |
* $query->queue_table() instead. |
|
375 | 384 |
* |
376 |
* @param $table |
|
385 |
* @param string $table
|
|
377 | 386 |
* The name of the table to add. It needs to exist in the global table |
378 | 387 |
* array. |
379 |
* @param $relationship |
|
380 |
* An alias of a table; if this is set, the path back to this table will |
|
381 |
* be tested prior to adding the table, making sure that all intermediary
|
|
382 |
* tables exist and are properly aliased. If set to NULL the path to |
|
383 |
* the primary table will be ensured. If the path cannot be made, the
|
|
384 |
* table will NOT be added.
|
|
388 |
* @param string $relationship
|
|
389 |
* An alias of a table; if this is set, the path back to this table will be
|
|
390 |
* tested prior to adding the table, making sure that all intermediary |
|
391 |
* tables exist and are properly aliased. If set to NULL the path to the
|
|
392 |
* primary table will be ensured. If the path cannot be made, the table
|
|
393 |
* will NOT be added. |
|
385 | 394 |
* @param views_join $join |
386 |
* In some join configurations this table may actually join back through |
|
387 |
* a different method; this is most likely to be used when tracing
|
|
388 |
* a hierarchy path. (node->parent->parent2->parent3). This parameter
|
|
389 |
* will specify how this table joins if it is not the default.
|
|
390 |
* @param $alias |
|
395 |
* In some join configurations this table may actually join back through a
|
|
396 |
* different method; this is most likely to be used when tracing a
|
|
397 |
* hierarchy path. (node->parent->parent2->parent3). This parameter will
|
|
398 |
* specify how this table joins if it is not the default. |
|
399 |
* @param string $alias
|
|
391 | 400 |
* A specific alias to use, rather than the default alias. |
392 | 401 |
* |
393 |
* @return $alias
|
|
402 |
* @return string
|
|
394 | 403 |
* The alias of the table; this alias can be used to access information |
395 | 404 |
* about the table and should always be used to refer to the table when |
396 | 405 |
* adding parts to the query. Or FALSE if the table was not able to be |
397 | 406 |
* added. |
398 | 407 |
*/ |
399 |
function add_table($table, $relationship = NULL, $join = NULL, $alias = NULL) { |
|
408 |
public function add_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
|
|
400 | 409 |
if (!$this->ensure_path($table, $relationship, $join)) { |
401 | 410 |
return FALSE; |
402 | 411 |
} |
... | ... | |
415 | 424 |
* ensure_table() should be used instead of this one, unless you are |
416 | 425 |
* absolutely sure this is what you want. |
417 | 426 |
* |
418 |
* @param $table |
|
427 |
* @param string $table
|
|
419 | 428 |
* The name of the table to add. It needs to exist in the global table |
420 | 429 |
* array. |
421 |
* @param $relationship |
|
430 |
* @param string $relationship
|
|
422 | 431 |
* The primary table alias this table is related to. If not set, the |
423 | 432 |
* primary table will be used. |
424 | 433 |
* @param views_join $join |
425 |
* In some join configurations this table may actually join back through |
|
426 |
* a different method; this is most likely to be used when tracing
|
|
427 |
* a hierarchy path. (node->parent->parent2->parent3). This parameter
|
|
428 |
* will specify how this table joins if it is not the default.
|
|
429 |
* @param $alias |
|
434 |
* In some join configurations this table may actually join back through a
|
|
435 |
* different method; this is most likely to be used when tracing a
|
|
436 |
* hierarchy path. (node->parent->parent2->parent3). This parameter will
|
|
437 |
* specify how this table joins if it is not the default. |
|
438 |
* @param string $alias
|
|
430 | 439 |
* A specific alias to use, rather than the default alias. |
431 | 440 |
* |
432 |
* @return $alias
|
|
441 |
* @return string
|
|
433 | 442 |
* The alias of the table; this alias can be used to access information |
434 | 443 |
* about the table and should always be used to refer to the table when |
435 | 444 |
* adding parts to the query. Or FALSE if the table was not able to be |
436 | 445 |
* added. |
437 | 446 |
*/ |
438 |
function queue_table($table, $relationship = NULL, $join = NULL, $alias = NULL) { |
|
447 |
public function queue_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
|
|
439 | 448 |
// If the alias is set, make sure it doesn't already exist. |
440 | 449 |
if (isset($this->table_queue[$alias])) { |
441 | 450 |
return $alias; |
... | ... | |
458 | 467 |
} |
459 | 468 |
} |
460 | 469 |
|
461 |
// Check this again to make sure we don't blow up existing aliases for already
|
|
462 |
// adjusted joins. |
|
470 |
// Check this again to make sure we don't blow up existing aliases for |
|
471 |
// already adjusted joins.
|
|
463 | 472 |
if (isset($this->table_queue[$alias])) { |
464 | 473 |
return $alias; |
465 | 474 |
} |
... | ... | |
471 | 480 |
$alias = $this->tables[$relationship][$table]['alias'] . $this->tables[$relationship][$table]['count']; |
472 | 481 |
} |
473 | 482 |
|
474 |
// If this is a relationship based table, add a marker with |
|
475 |
// the relationship as a primary table for the alias.
|
|
483 |
// If this is a relationship based table, add a marker with the
|
|
484 |
// relationship as a primary table for the alias. |
|
476 | 485 |
if ($table != $alias) { |
477 | 486 |
$this->mark_table($alias, $this->base_table, $alias); |
478 | 487 |
} |
... | ... | |
498 | 507 |
return $alias; |
499 | 508 |
} |
500 | 509 |
|
501 |
function mark_table($table, $relationship, $alias) { |
|
510 |
/** |
|
511 |
* |
|
512 |
*/ |
|
513 |
public function mark_table($table, $relationship, $alias) { |
|
502 | 514 |
// Mark that this table has been added. |
503 | 515 |
if (empty($this->tables[$relationship][$table])) { |
504 | 516 |
if (!isset($alias)) { |
505 | 517 |
$alias = ''; |
506 | 518 |
if ($relationship != $this->base_table) { |
507 |
// double underscore will help prevent accidental name
|
|
508 |
// space collisions.
|
|
519 |
// Double underscore will help prevent accidental name space
|
|
520 |
// collisions. |
|
509 | 521 |
$alias = $relationship . '__'; |
510 | 522 |
} |
511 | 523 |
$alias .= $table; |
... | ... | |
523 | 535 |
} |
524 | 536 |
|
525 | 537 |
/** |
526 |
* Ensure a table exists in the queue; if it already exists it won't |
|
527 |
* do anything, but if it doesn't it will add the table queue. It will ensure
|
|
528 |
* a path leads back to the relationship table.
|
|
538 |
* Ensure a table exists in the queue; if it already exists it won't do
|
|
539 |
* anything, but if it doesn't it will add the table queue. It will ensure a
|
|
540 |
* path leads back to the relationship table. |
|
529 | 541 |
* |
530 |
* @param $table |
|
542 |
* @param string $table
|
|
531 | 543 |
* The unaliased name of the table to ensure. |
532 |
* @param $relationship |
|
544 |
* @param string $relationship
|
|
533 | 545 |
* The relationship to ensure the table links to. Each relationship will |
534 |
* get a unique instance of the table being added. If not specified, |
|
535 |
* will be the primary table.
|
|
546 |
* get a unique instance of the table being added. If not specified, will
|
|
547 |
* be the primary table. |
|
536 | 548 |
* @param views_join $join |
537 | 549 |
* A views_join object (or derived object) to join the alias in. |
538 | 550 |
* |
539 |
* @return |
|
551 |
* @return string
|
|
540 | 552 |
* The alias used to refer to this specific table, or NULL if the table |
541 | 553 |
* cannot be ensured. |
542 | 554 |
*/ |
543 |
function ensure_table($table, $relationship = NULL, $join = NULL) { |
|
544 |
// ensure a relationship
|
|
555 |
public function ensure_table($table, $relationship = NULL, $join = NULL) {
|
|
556 |
// Ensure a relationship.
|
|
545 | 557 |
if (empty($relationship)) { |
546 | 558 |
$relationship = $this->base_table; |
547 | 559 |
} |
548 | 560 |
|
549 | 561 |
// If the relationship is the primary table, this actually be a relationship |
550 |
// link back from an alias. We store all aliases along with the primary table
|
|
551 |
// to detect this state, because eventually it'll hit a table we already
|
|
552 |
// have and that's when we want to stop. |
|
562 |
// link back from an alias. We store all aliases along with the primary |
|
563 |
// table to detect this state, because eventually it'll hit a table we
|
|
564 |
// already have and that's when we want to stop.
|
|
553 | 565 |
if ($relationship == $this->base_table && !empty($this->tables[$relationship][$table])) { |
554 | 566 |
return $this->tables[$relationship][$table]['alias']; |
555 | 567 |
} |
... | ... | |
578 | 590 |
$join = $this->adjust_join($join, $relationship); |
579 | 591 |
|
580 | 592 |
if ($this->ensure_path($table, $relationship, $join)) { |
581 |
// Attempt to eliminate redundant joins. If this table's |
|
582 |
// relationship and join exactly matches an existing table's
|
|
583 |
// relationship and join, we do not have to join to it again;
|
|
584 |
// just return the existing table's alias. See
|
|
585 |
// http://groups.drupal.org/node/11288 for details.
|
|
593 |
// Attempt to eliminate redundant joins. If this table's relationship
|
|
594 |
// and join exactly matches an existing table's relationship and join, we
|
|
595 |
// do not have to join to it again; just return the existing table's
|
|
596 |
// alias.
|
|
597 |
// @see http://groups.drupal.org/node/11288
|
|
586 | 598 |
// |
587 |
// This can be done safely here but not lower down in |
|
588 |
// queue_table(), because queue_table() is also used by |
|
589 |
// add_table() which requires the ability to intentionally add |
|
590 |
// the same table with the same join multiple times. For |
|
591 |
// example, a view that filters on 3 taxonomy terms using AND |
|
592 |
// needs to join taxonomy_term_data 3 times with the same join. |
|
593 |
|
|
599 |
// This can be done safely here but not lower down in queue_table(), |
|
600 |
// because queue_table() is also used by add_table() which requires the |
|
601 |
// ability to intentionally add the same table with the same join |
|
602 |
// multiple times. For example, a view that filters on 3 taxonomy terms |
|
603 |
// using AND needs to join taxonomy_term_data 3 times with the same join. |
|
594 | 604 |
// scan through the table queue to see if a matching join and |
595 | 605 |
// relationship exists. If so, use it instead of this join. |
596 |
|
|
597 |
// TODO: Scanning through $this->table_queue results in an |
|
598 |
// O(N^2) algorithm, and this code runs every time the view is |
|
599 |
// instantiated (Views 2 does not currently cache queries). |
|
600 |
// There are a couple possible "improvements" but we should do |
|
601 |
// some performance testing before picking one. |
|
606 |
// @todo Scanning through $this->table_queue results in an O(N^2) |
|
607 |
// algorithm, and this code runs every time the view is instantiated |
|
608 |
// (Views 2 does not currently cache queries). There are a couple |
|
609 |
// possible "improvements" but we should do some performance testing |
|
610 |
// before picking one. |
|
602 | 611 |
foreach ($this->table_queue as $queued_table) { |
603 |
// In PHP 4 and 5, the == operation returns TRUE for two objects |
|
604 |
// if they are instances of the same class and have the same
|
|
605 |
// attributes and values.
|
|
612 |
// In PHP 4 and 5, the == operation returns TRUE for two objects if
|
|
613 |
// they are instances of the same class and have the same attributes
|
|
614 |
// and values. |
|
606 | 615 |
if ($queued_table['relationship'] == $relationship && $queued_table['join'] == $join) { |
607 | 616 |
return $queued_table['alias']; |
608 | 617 |
} |
... | ... | |
614 | 623 |
|
615 | 624 |
/** |
616 | 625 |
* Make sure that the specified table can be properly linked to the primary |
617 |
* table in the JOINs. This function uses recursion. If the tables |
|
618 |
* needed to complete the path back to the primary table are not in the
|
|
619 |
* query they will be added, but additional copies will NOT be added
|
|
620 |
* if the table is already there.
|
|
626 |
* table in the JOINs. This function uses recursion. If the tables needed
|
|
627 |
* to complete the path back to the primary table are not in the query they
|
|
628 |
* will be added, but additional copies will NOT be added if the table is
|
|
629 |
* already there. |
|
621 | 630 |
*/ |
622 |
function ensure_path($table, $relationship = NULL, $join = NULL, $traced = array(), $add = array()) { |
|
631 |
public function ensure_path($table, $relationship = NULL, $join = NULL, $traced = array(), $add = array()) {
|
|
623 | 632 |
if (!isset($relationship)) { |
624 | 633 |
$relationship = $this->base_table; |
625 | 634 |
} |
... | ... | |
652 | 661 |
|
653 | 662 |
// Have we been this way? |
654 | 663 |
if (isset($traced[$join->left_table])) { |
655 |
// we looped. Broked.
|
|
664 |
// We looped. Broked.
|
|
656 | 665 |
return FALSE; |
657 | 666 |
} |
658 | 667 |
|
... | ... | |
668 | 677 |
} |
669 | 678 |
|
670 | 679 |
/** |
671 |
* Fix a join to adhere to the proper relationship; the left table can vary |
|
672 |
* based upon what relationship items are joined in on. |
|
680 |
* Fix a join to adhere to the proper relationship. |
|
681 |
* |
|
682 |
* The left table can vary based upon what relationship items are joined in |
|
683 |
* on. |
|
673 | 684 |
*/ |
674 |
function adjust_join($join, $relationship) { |
|
685 |
public function adjust_join($join, $relationship) {
|
|
675 | 686 |
if (!empty($join->adjusted)) { |
676 | 687 |
return $join; |
677 | 688 |
} |
... | ... | |
683 | 694 |
// Adjusts the left table for our relationship. |
684 | 695 |
if ($relationship != $this->base_table) { |
685 | 696 |
// If we're linking to the primary table, the relationship to use will |
686 |
// be the prior relationship. Unless it's a direct link. |
|
687 |
|
|
688 |
// Safety! Don't modify an original here. |
|
697 |
// be the prior relationship. Unless it's a direct link. Safety! Don't |
|
698 |
// modify an original here. |
|
689 | 699 |
$join = clone $join; |
690 | 700 |
|
691 | 701 |
// Do we need to try to ensure a path? |
... | ... | |
695 | 705 |
$this->ensure_table($join->left_table, $relationship); |
696 | 706 |
} |
697 | 707 |
|
698 |
// First, if this is our link point/anchor table, just use the relationship |
|
708 |
// First, if this is our link point/anchor table, just use the |
|
709 |
// relationship. |
|
699 | 710 |
if ($join->left_table == $this->relationships[$relationship]['table']) { |
700 | 711 |
$join->left_table = $relationship; |
701 | 712 |
} |
... | ... | |
716 | 727 |
/** |
717 | 728 |
* Retrieve join data from the larger join data cache. |
718 | 729 |
* |
719 |
* @param $table |
|
730 |
* @param string $table
|
|
720 | 731 |
* The table to get the join information for. |
721 |
* @param $base_table |
|
732 |
* @param string $base_table
|
|
722 | 733 |
* The path we're following to get this join. |
723 | 734 |
* |
724 | 735 |
* @return views_join |
725 | 736 |
* A views_join object or child object, if one exists. |
726 | 737 |
*/ |
727 |
function get_join_data($table, $base_table) { |
|
738 |
public function get_join_data($table, $base_table) {
|
|
728 | 739 |
// Check to see if we're linking to a known alias. If so, get the real |
729 | 740 |
// table's data instead. |
730 | 741 |
if (!empty($this->table_queue[$table])) { |
... | ... | |
739 | 750 |
* If you need the alias of a table with a particular relationship, use |
740 | 751 |
* ensure_table(). |
741 | 752 |
*/ |
742 |
function get_table_info($table) { |
|
753 |
public function get_table_info($table) {
|
|
743 | 754 |
if (!empty($this->table_queue[$table])) { |
744 | 755 |
return $this->table_queue[$table]; |
745 | 756 |
} |
... | ... | |
755 | 766 |
|
756 | 767 |
/** |
757 | 768 |
* Add a field to the query table, possibly with an alias. This will |
758 |
* automatically call ensure_table to make sure the required table |
|
759 |
* exists, *unless* $table is unset.
|
|
769 |
* automatically call ensure_table to make sure the required table exists,
|
|
770 |
* *unless* $table is unset. |
|
760 | 771 |
* |
761 |
* @param $table |
|
762 |
* The table this field is attached to. If NULL, it is assumed this will |
|
763 |
* be a formula; otherwise, ensure_table is used to make sure the |
|
764 |
* table exists. |
|
765 |
* @param $field |
|
772 |
* @param string $table |
|
773 |
* The table this field is attached to. If NULL, it is assumed this will be |
|
774 |
* a formula; otherwise, ensure_table is used to make sure the table exists. |
|
775 |
* @param string $field |
|
766 | 776 |
* The name of the field to add. This may be a real field or a formula. |
767 |
* @param $alias |
|
777 |
* @param string $alias
|
|
768 | 778 |
* The alias to create. If not specified, the alias will be $table_$field |
769 | 779 |
* unless $table is NULL. When adding formulae, it is recommended that an |
770 | 780 |
* alias be used. |
771 |
* @param $params |
|
781 |
* @param array $params
|
|
772 | 782 |
* An array of parameters additional to the field that will control items |
773 | 783 |
* such as aggregation functions and DISTINCT. |
774 | 784 |
* |
775 |
* @return $name
|
|
776 |
* The name that this field can be referred to as. Usually this is the alias.
|
|
785 |
* @return string
|
|
786 |
* The name that this field can be referred to as, usually the alias.
|
|
777 | 787 |
*/ |
778 |
function add_field($table, $field, $alias = '', $params = array()) { |
|
788 |
public function add_field($table, $field, $alias = '', $params = array()) {
|
|
779 | 789 |
// We check for this specifically because it gets a special alias. |
780 | 790 |
if ($table == $this->base_table && $field == $this->base_field && empty($alias)) { |
781 | 791 |
$alias = $this->base_field; |
... | ... | |
789 | 799 |
$alias = $table . '_' . $field; |
790 | 800 |
} |
791 | 801 |
|
792 |
// Make sure an alias is assigned |
|
802 |
// Make sure an alias is assigned.
|
|
793 | 803 |
$alias = $alias ? $alias : $field; |
794 | 804 |
|
795 |
// PostgreSQL truncates aliases to 63 characters: http://drupal.org/node/571548
|
|
796 |
|
|
805 |
// PostgreSQL truncates aliases to 63 characters.
|
|
806 |
// @see http://drupal.org/node/571548 |
|
797 | 807 |
// We limit the length of the original alias up to 60 characters |
798 |
// to get a unique alias later if its have duplicates |
|
808 |
// to get a unique alias later if its have duplicates.
|
|
799 | 809 |
$alias = strtolower(substr($alias, 0, 60)); |
800 | 810 |
|
801 | 811 |
// Create a field info array. |
... | ... | |
805 | 815 |
'alias' => $alias, |
806 | 816 |
) + $params; |
807 | 817 |
|
808 |
// Test to see if the field is actually the same or not. Due to |
|
809 |
// differing parameters changing the aggregation function, we need
|
|
810 |
// to do some automatic alias collision detection:
|
|
818 |
// Test to see if the field is actually the same or not. Due to differing
|
|
819 |
// parameters changing the aggregation function, we need to do some
|
|
820 |
// automatic alias collision detection.
|
|
811 | 821 |
$base = $alias; |
812 | 822 |
$counter = 0; |
813 | 823 |
while (!empty($this->fields[$alias]) && $this->fields[$alias] != $field_info) { |
... | ... | |
825 | 835 |
} |
826 | 836 |
|
827 | 837 |
/** |
828 |
* Remove all fields that may've been added; primarily used for summary |
|
829 |
* mode where we're changing the query because we didn't get data we needed. |
|
838 |
* Remove all fields that may've been added. |
|
839 |
* |
|
840 |
* Primarily used for summary mode where we're changing the query because we |
|
841 |
* didn't get data we needed. |
|
830 | 842 |
*/ |
831 |
function clear_fields() { |
|
843 |
public function clear_fields() {
|
|
832 | 844 |
$this->fields = array(); |
833 | 845 |
} |
834 | 846 |
|
835 | 847 |
/** |
836 |
* Add a simple WHERE clause to the query. The caller is responsible for |
|
837 |
* ensuring that all fields are fully qualified (TABLE.FIELD) and that |
|
838 |
* the table already exists in the query. |
|
848 |
* Add a simple WHERE clause to the query. |
|
839 | 849 |
* |
840 |
* @param $group |
|
850 |
* The caller is responsible for ensuring that all fields are fully qualified |
|
851 |
* (TABLE.FIELD) and that the table already exists in the query. |
|
852 |
* |
|
853 |
* @param string $group |
|
841 | 854 |
* The WHERE group to add these to; groups are used to create AND/OR |
842 |
* sections. Groups cannot be nested. Use 0 as the default group. |
|
843 |
* If the group does not yet exist it will be created as an AND group.
|
|
844 |
* @param $field |
|
855 |
* sections. Groups cannot be nested. Use 0 as the default group. If the
|
|
856 |
* group does not yet exist it will be created as an AND group. |
|
857 |
* @param string $field
|
|
845 | 858 |
* The name of the field to check. |
846 |
* @param $value |
|
847 |
* The value to test the field against. In most cases, this is a scalar. For more |
|
848 |
* complex options, it is an array. The meaning of each element in the array is |
|
849 |
* dependent on the $operator. |
|
850 |
* @param $operator |
|
851 |
* The comparison operator, such as =, <, or >=. It also accepts more complex |
|
852 |
* options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is an array |
|
853 |
* = otherwise. If $field is a string you have to use 'formula' here. |
|
859 |
* @param string $value |
|
860 |
* The value to test the field against. In most cases, this is a scalar. For |
|
861 |
* more complex options, it is an array. The meaning of each element in the |
|
862 |
* array is dependent on the $operator. |
|
863 |
* @param string $operator |
|
864 |
* The comparison operator, such as =, <, or >=. It also accepts more |
|
865 |
* complex options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is |
|
866 |
* an array = otherwise. If $field is a string you have to use 'formula' |
|
867 |
* here. |
|
854 | 868 |
* |
855 | 869 |
* The $field, $value and $operator arguments can also be passed in with a |
856 | 870 |
* single DatabaseCondition object, like this: |
871 |
* |
|
857 | 872 |
* @code |
858 | 873 |
* $this->query->add_where( |
859 | 874 |
* $this->options['group'], |
... | ... | |
866 | 881 |
* @see QueryConditionInterface::condition() |
867 | 882 |
* @see DatabaseCondition |
868 | 883 |
*/ |
869 |
function add_where($group, $field, $value = NULL, $operator = NULL) { |
|
870 |
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all |
|
871 |
// the default group.
|
|
884 |
public function add_where($group, $field, $value = NULL, $operator = NULL) {
|
|
885 |
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all the
|
|
886 |
// default group. |
|
872 | 887 |
if (empty($group)) { |
873 | 888 |
$group = 0; |
874 | 889 |
} |
... | ... | |
889 | 904 |
* Add a complex WHERE clause to the query. |
890 | 905 |
* |
891 | 906 |
* The caller is responsible for ensuring that all fields are fully qualified |
892 |
* (TABLE.FIELD) and that the table already exists in the query. |
|
893 |
* Internally the dbtng method "where" is used.
|
|
907 |
* (TABLE.FIELD) and that the table already exists in the query. Internally
|
|
908 |
* the dbtng method "where" is used. |
|
894 | 909 |
* |
895 |
* @param $group |
|
910 |
* @param string $group
|
|
896 | 911 |
* The WHERE group to add these to; groups are used to create AND/OR |
897 |
* sections. Groups cannot be nested. Use 0 as the default group. |
|
898 |
* If the group does not yet exist it will be created as an AND group.
|
|
899 |
* @param $snippet |
|
900 |
* The snippet to check. This can be either a column or |
|
901 |
* a complex expression like "UPPER(table.field) = 'value'"
|
|
902 |
* @param $args |
|
912 |
* sections. Groups cannot be nested. Use 0 as the default group. If the
|
|
913 |
* group does not yet exist it will be created as an AND group. |
|
914 |
* @param string $snippet
|
|
915 |
* The snippet to check. This can be either a column or a complex expression
|
|
916 |
* like "UPPER(table.field) = 'value'".
|
|
917 |
* @param array $args
|
|
903 | 918 |
* An associative array of arguments. |
904 | 919 |
* |
905 | 920 |
* @see QueryConditionInterface::where() |
906 | 921 |
*/ |
907 |
function add_where_expression($group, $snippet, $args = array()) { |
|
922 |
public function add_where_expression($group, $snippet, $args = array()) {
|
|
908 | 923 |
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all |
909 | 924 |
// the default group. |
910 | 925 |
if (empty($group)) { |
... | ... | |
927 | 942 |
* Add a simple HAVING clause to the query. |
928 | 943 |
* |
929 | 944 |
* The caller is responsible for ensuring that all fields are fully qualified |
930 |
* (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist in the query.
|
|
931 |
* Internally the dbtng method "havingCondition" is used. |
|
945 |
* (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist |
|
946 |
* in the query. Internally the dbtng method "havingCondition" is used.
|
|
932 | 947 |
* |
933 |
* @param $group |
|
948 |
* @param string $group
|
|
934 | 949 |
* The HAVING group to add these to; groups are used to create AND/OR |
935 |
* sections. Groups cannot be nested. Use 0 as the default group. |
|
936 |
* If the group does not yet exist it will be created as an AND group.
|
|
937 |
* @param $field |
|
950 |
* sections. Groups cannot be nested. Use 0 as the default group. If the
|
|
951 |
* group does not yet exist it will be created as an AND group. |
|
952 |
* @param string $field
|
|
938 | 953 |
* The name of the field to check. |
939 |
* @param $value |
|
940 |
* The value to test the field against. In most cases, this is a scalar. For more |
|
941 |
* complex options, it is an array. The meaning of each element in the array is |
|
942 |
* dependent on the $operator. |
|
943 |
* @param $operator |
|
944 |
* The comparison operator, such as =, <, or >=. It also accepts more complex |
|
945 |
* options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is an array |
|
946 |
* = otherwise. If $field is a string you have to use 'formula' here. |
|
954 |
* @param string $value |
|
955 |
* The value to test the field against. In most cases, this is a scalar. For |
|
956 |
* more complex options, it is an array. The meaning of each element in the |
|
957 |
* array is dependent on the $operator. |
|
958 |
* @param string $operator |
|
959 |
* The comparison operator, such as =, <, or >=. It also accepts more |
|
960 |
* complex options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is |
|
961 |
* an array = otherwise. If $field is a string you have to use 'formula' |
|
962 |
* here. |
|
947 | 963 |
* |
948 | 964 |
* @see SelectQueryInterface::havingCondition() |
949 | 965 |
*/ |
950 |
function add_having($group, $field, $value = NULL, $operator = NULL) { |
|
951 |
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all |
|
952 |
// the default group.
|
|
966 |
public function add_having($group, $field, $value = NULL, $operator = NULL) {
|
|
967 |
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all the
|
|
968 |
// default group. |
|
953 | 969 |
if (empty($group)) { |
954 | 970 |
$group = 0; |
955 | 971 |
} |
... | ... | |
969 | 985 |
|
970 | 986 |
/** |
971 | 987 |
* Add a complex HAVING clause to the query. |
988 |
* |
|
972 | 989 |
* The caller is responsible for ensuring that all fields are fully qualified |
973 |
* (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist in the query.
|
|
974 |
* Internally the dbtng method "having" is used. |
|
990 |
* (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist |
|
991 |
* in the query. Internally the dbtng method "having" is used.
|
|
975 | 992 |
* |
976 |
* @param $group |
|
993 |
* @param string $group
|
|
977 | 994 |
* The HAVING group to add these to; groups are used to create AND/OR |
978 |
* sections. Groups cannot be nested. Use 0 as the default group. |
|
979 |
* If the group does not yet exist it will be created as an AND group.
|
|
980 |
* @param $snippet |
|
981 |
* The snippet to check. This can be either a column or |
|
982 |
* a complex expression like "COUNT(table.field) > 3"
|
|
983 |
* @param $args |
|
995 |
* sections. Groups cannot be nested. Use 0 as the default group. If the
|
|
996 |
* group does not yet exist it will be created as an AND group. |
|
997 |
* @param string $snippet
|
|
998 |
* The snippet to check. This can be either a column or a complex
|
|
999 |
* expression like "COUNT(table.field) > 3" |
|
1000 |
* @param array $args
|
|
984 | 1001 |
* An associative array of arguments. |
985 | 1002 |
* |
986 | 1003 |
* @see QueryConditionInterface::having() |
987 | 1004 |
*/ |
988 |
function add_having_expression($group, $snippet, $args = array()) { |
|
1005 |
public function add_having_expression($group, $snippet, $args = array()) {
|
|
989 | 1006 |
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all |
990 | 1007 |
// the default group. |
991 | 1008 |
if (empty($group)) { |
... | ... | |
1008 | 1025 |
/** |
1009 | 1026 |
* Add an ORDER BY clause to the query. |
1010 | 1027 |
* |
1011 |
* @param $table |
|
1012 |
* The table this field is part of. If a formula, enter NULL. |
|
1013 |
* If you want to orderby random use "rand" as table and nothing else.
|
|
1014 |
* @param $field |
|
1015 |
* The field or formula to sort on. If already a field, enter NULL |
|
1016 |
* and put in the alias.
|
|
1017 |
* @param $order |
|
1028 |
* @param string $table
|
|
1029 |
* The table this field is part of. If a formula, enter NULL. If you want to
|
|
1030 |
* orderby random use "rand" as table and nothing else. |
|
1031 |
* @param string $field
|
|
1032 |
* The field or formula to sort on. If already a field, enter NULL and put
|
|
1033 |
* in the alias. |
|
1034 |
* @param string $order
|
|
1018 | 1035 |
* Either ASC or DESC. |
1019 |
* @param $alias |
|
1020 |
* The alias to add the field as. In SQL, all fields in the order by |
|
1021 |
* must also be in the SELECT portion. If an $alias isn't specified
|
|
1022 |
* one will be generated for from the $field; however, if the
|
|
1023 |
* $field is a formula, this alias will likely fail.
|
|
1024 |
* @param $params |
|
1036 |
* @param string $alias
|
|
1037 |
* The alias to add the field as. In SQL, all fields in the order by must
|
|
1038 |
* also be in the SELECT portion. If an $alias isn't specified one will be
|
|
1039 |
* generated for from the $field; however, if the $field is a formula, this
|
|
1040 |
* alias will likely fail. |
|
1041 |
* @param string $params
|
|
1025 | 1042 |
* Any params that should be passed through to the add_field. |
1026 | 1043 |
*/ |
1027 |
function add_orderby($table, $field = NULL, $order = 'ASC', $alias = '', $params = array()) { |
|
1044 |
public function add_orderby($table, $field = NULL, $order = 'ASC', $alias = '', $params = array()) {
|
|
1028 | 1045 |
// Only ensure the table if it's not the special random key. |
1029 |
// @todo: Maybe it would make sense to just add a add_orderby_rand or something similar. |
|
1046 |
// @todo Maybe it would make sense to just add a add_orderby_rand or |
|
1047 |
// something similar. |
|
1030 | 1048 |
if ($table && $table != 'rand') { |
1031 | 1049 |
$this->ensure_table($table); |
1032 | 1050 |
} |
1033 | 1051 |
|
1034 |
// Only fill out this aliasing if there is a table; |
|
1035 |
// otherwise we assume it is a formula.
|
|
1052 |
// Only fill out this aliasing if there is a table; otherwise we assume it
|
|
1053 |
// is a formula. |
|
1036 | 1054 |
if (!$alias && $table) { |
1037 | 1055 |
$as = $table . '_' . $field; |
1038 | 1056 |
} |
... | ... | |
1046 | 1064 |
|
1047 | 1065 |
$this->orderby[] = array( |
1048 | 1066 |
'field' => $as, |
1049 |
'direction' => strtoupper($order) |
|
1067 |
'direction' => strtoupper($order),
|
|
1050 | 1068 |
); |
1051 |
|
|
1052 |
/** |
|
1053 |
* -- removing, this should be taken care of by field adding now. |
|
1054 |
* -- leaving commented because I am unsure. |
|
1055 |
// If grouping, all items in the order by must also be in the |
|
1056 |
// group by clause. Check $table to ensure that this is not a |
|
1057 |
// formula. |
|
1058 |
if ($this->groupby && $table) { |
|
1059 |
$this->add_groupby($as); |
|
1060 |
} |
|
1061 |
*/ |
|
1062 | 1069 |
} |
1063 | 1070 |
|
1064 | 1071 |
/** |
1065 |
* Add a simple GROUP BY clause to the query. The caller is responsible |
|
1066 |
* for ensuring that the fields are fully qualified and the table is properly |
|
1067 |
* added. |
|
1072 |
* Add a simple GROUP BY clause to the query. |
|
1073 |
* |
|
1074 |
* The caller is responsible for ensuring that the fields are fully qualified |
|
1075 |
* and the table is properly added. |
|
1068 | 1076 |
*/ |
1069 |
function add_groupby($clause) { |
|
1077 |
public function add_groupby($clause) {
|
|
1070 | 1078 |
// Only add it if it's not already in there. |
1071 | 1079 |
if (!in_array($clause, $this->groupby)) { |
1072 | 1080 |
$this->groupby[] = $clause; |
... | ... | |
1078 | 1086 |
* |
1079 | 1087 |
* @see views_plugin_query_default::add_field() |
1080 | 1088 |
*/ |
1081 |
function get_field_alias($table_alias, $field) { |
|
1089 |
public function get_field_alias($table_alias, $field) {
|
|
1082 | 1090 |
return isset($this->field_aliases[$table_alias][$field]) ? $this->field_aliases[$table_alias][$field] : FALSE; |
1083 | 1091 |
} |
1084 | 1092 |
|
... | ... | |
1087 | 1095 |
* |
1088 | 1096 |
* @see SelectQuery::addTag() |
1089 | 1097 |
*/ |
1090 |
function add_tag($tag) { |
|
1098 |
public function add_tag($tag) {
|
|
1091 | 1099 |
$this->tags[] = $tag; |
1092 | 1100 |
} |
1093 | 1101 |
|
1094 | 1102 |
/** |
1095 | 1103 |
* Generates a unique placeholder used in the db query. |
1096 | 1104 |
*/ |
1097 |
function placeholder($base = 'views') { |
|
1105 |
public function placeholder($base = 'views') {
|
|
1098 | 1106 |
static $placeholders = array(); |
1099 | 1107 |
if (!isset($placeholders[$base])) { |
1100 | 1108 |
$placeholders[$base] = 0; |
... | ... | |
1113 | 1121 |
* There is other code in filters which makes sure that the group IDs are |
1114 | 1122 |
* higher than zero. |
1115 | 1123 |
* |
1116 |
* @param $where |
|
1117 |
* 'where' or 'having'. |
|
1124 |
* @param string $where
|
|
1125 |
* Either 'where' or 'having'.
|
|
1118 | 1126 |
*/ |
1119 |
function build_condition($where = 'where') { |
|
1127 |
public function build_condition($where = 'where') {
|
|
1120 | 1128 |
$has_condition = FALSE; |
1121 | 1129 |
$has_arguments = FALSE; |
1122 | 1130 |
$has_filter = FALSE; |
... | ... | |
1130 | 1138 |
$sub_group = $info['type'] == 'OR' ? db_or() : db_and(); |
1131 | 1139 |
foreach ($info['conditions'] as $key => $clause) { |
1132 | 1140 |
// DBTNG doesn't support to add the same subquery twice to the main |
1133 |
// query and the count query, so clone the subquery to have two instances |
|
1134 |
// of the same object. - http://drupal.org/node/1112854 |
|
1141 |
// query and the count query, so clone the subquery to have two |
|
1142 |
// instances of the same object. |
|
1143 |
// @see http://drupal.org/node/1112854 |
|
1135 | 1144 |
if (is_object($clause['value']) && $clause['value'] instanceof SelectQuery) { |
1136 | 1145 |
$clause['value'] = clone $clause['value']; |
1137 | 1146 |
} |
... | ... | |
1172 | 1181 |
/** |
1173 | 1182 |
* Build fields array. |
1174 | 1183 |
*/ |
1175 |
function compile_fields($fields_array, $query) { |
|
1184 |
public function compile_fields($fields_array, $query) {
|
|
1176 | 1185 |
$non_aggregates = array(); |
1177 | 1186 |
foreach ($fields_array as $field) { |
1178 | 1187 |
$string = ''; |
... | ... | |
1187 | 1196 |
} |
1188 | 1197 |
|
1189 | 1198 |
if (!empty($field['count'])) { |
1190 |
// Retained for compatibility |
|
1199 |
// Retained for compatibility.
|
|
1191 | 1200 |
$field['function'] = 'count'; |
1192 | 1201 |
// It seems there's no way to abstract the table+column reference |
1193 | 1202 |
// without adding a field, aliasing, and then using the alias. |
... | ... | |
1205 | 1214 |
} |
1206 | 1215 |
// This is a formula, using no tables. |
1207 | 1216 |
elseif (empty($field['table'])) { |
1208 |
$non_aggregates[] = $fieldname; |
|
1217 |
if (!in_array($fieldname, $non_aggregates)) { |
|
1218 |
$non_aggregates[] = $fieldname; |
|
1219 |
} |
|
1209 | 1220 |
$placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array(); |
1210 | 1221 |
$query->addExpression($string, $fieldname, $placeholders); |
1211 | 1222 |
} |
... | ... | |
1213 | 1224 |
elseif ($this->distinct && !in_array($fieldname, $this->groupby)) { |
1214 | 1225 |
// d7cx: This code was there, apparently needed for PostgreSQL |
1215 | 1226 |
// $string = db_driver() == 'pgsql' ? "FIRST($string)" : $string; |
1227 |
if (!in_array($string, $non_aggregates)) { |
|
1228 |
$non_aggregates[] = $string; |
|
1229 |
} |
|
1216 | 1230 |
$query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname); |
1217 | 1231 |
} |
1218 | 1232 |
elseif (empty($field['aggregate'])) { |
1219 |
$non_aggregates[] = $fieldname; |
|
1233 |
if (!in_array($string, $non_aggregates)) { |
|
1234 |
$non_aggregates[] = $string; |
|
1235 |
} |
|
1220 | 1236 |
$query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname); |
1221 | 1237 |
} |
1222 | 1238 |
|
1223 |
// @TODO Remove this old code.
|
|
1239 |
// @todo Remove this old code.
|
|
1224 | 1240 |
if (!empty($field['distinct']) && empty($field['function'])) { |
1225 | 1241 |
$distinct[] = $string; |
1226 | 1242 |
} |
... | ... | |
1242 | 1258 |
* Generate a query and a countquery from all of the information supplied |
1243 | 1259 |
* to the object. |
1244 | 1260 |
* |
1245 |
* @param $get_count |
|
1261 |
* @param bool $get_count
|
|
1246 | 1262 |
* Provide a countquery if this is true, otherwise provide a normal query. |
1247 | 1263 |
* |
1248 | 1264 |
* @return SelectQuery |
1249 | 1265 |
* A SelectQuery object. |
1250 | 1266 |
*/ |
1251 |
function query($get_count = FALSE) { |
|
1267 |
public function query($get_count = FALSE) {
|
|
1252 | 1268 |
// Check query distinct value. |
1253 | 1269 |
if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) { |
1254 |
if ($this->pure_distinct === FALSE){ |
|
1270 |
if ($this->pure_distinct === FALSE) {
|
|
1255 | 1271 |
$base_field_alias = $this->add_field($this->base_table, $this->base_field); |
1256 | 1272 |
$this->add_groupby($base_field_alias); |
1257 | 1273 |
} |
1258 | 1274 |
$distinct = TRUE; |
1259 | 1275 |
} |
1260 | 1276 |
|
1261 |
/** |
|
1262 |
* An optimized count query includes just the base field instead of all the fields. |
|
1263 |
* Determine of this query qualifies by checking for a groupby or distinct. |
|
1264 |
*/ |
|
1277 |
// An optimized count query includes just the base field instead of all the |
|
1278 |
// fields. Determine of this query qualifies by checking for a groupby or |
|
1279 |
// distinct. |
|
1265 | 1280 |
$fields_array = $this->fields; |
1266 | 1281 |
if ($get_count && !$this->groupby) { |
1267 | 1282 |
foreach ($fields_array as $field) { |
... | ... | |
1281 | 1296 |
$options = array(); |
1282 | 1297 |
$target = 'default'; |
1283 | 1298 |
$key = 'default'; |
1284 |
// Detect an external database and set the |
|
1299 |
// Detect an external database and set the.
|
|
1285 | 1300 |
if (isset($this->view->base_database)) { |
1286 | 1301 |
$key = $this->view->base_database; |
1287 | 1302 |
} |
1288 | 1303 |
|
1289 |
// Set the slave target if the slave option is set |
|
1304 |
// Set the slave target if the slave option is set.
|
|
1290 | 1305 |
if (!empty($this->options['slave'])) { |
1291 | 1306 |
$target = 'slave'; |
1292 | 1307 |
} |
1293 | 1308 |
|
1294 |
// Go ahead and build the query. |
|
1295 |
// db_select doesn't support to specify the key, so use getConnection directly.
|
|
1309 |
// Go ahead and build the query. db_select doesn't support to specify the
|
|
1310 |
// key, so use getConnection directly. |
|
1296 | 1311 |
$query = Database::getConnection($target, $key) |
1297 | 1312 |
->select($this->base_table, $this->base_table, $options) |
1298 | 1313 |
->addTag('views') |
... | ... | |
1340 | 1355 |
} |
1341 | 1356 |
|
1342 | 1357 |
if (!$this->get_count_optimized) { |
1343 |
// we only add the orderby if we're not counting.
|
|
1358 |
// We only add the orderby if we're not counting.
|
|
1344 | 1359 |
if ($this->orderby) { |
1345 | 1360 |
foreach ($this->orderby as $order) { |
1346 | 1361 |
if ($order['field'] == 'rand_') { |
... | ... | |
1374 | 1389 |
|
1375 | 1390 |
if (!$get_count) { |
1376 | 1391 |
if (!empty($this->limit) || !empty($this->offset)) { |
1377 |
// We can't have an offset without a limit, so provide a very large limit
|
|
1378 |
// instead. |
|
1392 |
// We can't have an offset without a limit, so provide a very large |
|
1393 |
// limit instead.
|
|
1379 | 1394 |
$limit = intval(!empty($this->limit) ? $this->limit : 999999); |
1380 | 1395 |
$offset = intval(!empty($this->offset) ? $this->offset : 0); |
1381 | 1396 |
$query->range($offset, $limit); |
... | ... | |
1388 | 1403 |
/** |
1389 | 1404 |
* Get the arguments attached to the WHERE and HAVING clauses of this query. |
1390 | 1405 |
*/ |
1391 |
function get_where_args() { |
|
1406 |
public function get_where_args() {
|
|
1392 | 1407 |
$args = array(); |
1393 | 1408 |
foreach ($this->where as $group => $where) { |
1394 | 1409 |
$args = array_merge($args, $where['args']); |
... | ... | |
1402 | 1417 |
/** |
1403 | 1418 |
* Let modules modify the query just prior to finalizing it. |
1404 | 1419 |
*/ |
1405 |
function alter(&$view) { |
|
1420 |
public function alter(&$view) {
|
|
1406 | 1421 |
foreach (module_implements('views_query_alter') as $module) { |
1407 | 1422 |
$function = $module . '_views_query_alter'; |
1408 | 1423 |
$function($view, $this); |
... | ... | |
1412 | 1427 |
/** |
1413 | 1428 |
* Builds the necessary info to execute the query. |
1414 | 1429 |
*/ |
1415 |
function build(&$view) { |
|
1430 |
public function build(&$view) {
|
|
1416 | 1431 |
// Make the query distinct if the option was set. |
1417 | 1432 |
if (!empty($this->options['distinct'])) { |
1418 | 1433 |
$this->set_distinct(TRUE, !empty($this->options['pure_distinct'])); |
... | ... | |
1437 | 1452 |
* Values to set: $view->result, $view->total_rows, $view->execute_time, |
1438 | 1453 |
* $view->current_page. |
1439 | 1454 |
*/ |
1440 |
function execute(&$view) { |
|
1441 |
$external = FALSE; // Whether this query will run against an external database. |
|
1455 |
public function execute(&$view) { |
|
1456 |
// Whether this query will run against an external database. |
|
1457 |
$external = FALSE; |
|
1442 | 1458 |
$query = $view->build_info['query']; |
1443 | 1459 |
$count_query = $view->build_info['count_query']; |
1444 | 1460 |
|
... | ... | |
1462 | 1478 |
// If not, then hook_query_node_access_alter() may munge the count by |
1463 | 1479 |
// adding a distinct against an empty query string |
1464 | 1480 |
// (e.g. COUNT DISTINCT(1) ...) and no pager will return. |
1465 |
// See pager.inc > PagerDefault::execute()
|
|
1466 |
// http://api.drupal.org/api/drupal/includes--pager.inc/function/PagerDefault::execute/7 |
|
1467 |
// See http://drupal.org/node/1046170.
|
|
1481 |
// @see pager.inc > PagerDefault::execute()
|
|
1482 |
// @see http://api.drupal.org/api/drupal/includes--pager.inc/function/PagerDefault::execute/7
|
|
1483 |
// @see http://drupal.org/node/1046170.
|
|
1468 | 1484 |
$count_query->preExecute(); |
1469 | 1485 |
|
1470 | 1486 |
// Build the count query. |
... | ... | |
1480 | 1496 |
|
1481 | 1497 |
$start = microtime(TRUE); |
1482 | 1498 |
|
1483 |
|
|
1484 | 1499 |
try { |
1485 | 1500 |
if ($this->pager->use_count_query() || !empty($view->get_total_rows)) { |
1486 | 1501 |
$this->pager->execute_count_query($count_query); |
... | ... | |
1518 | 1533 |
$view->execute_time = microtime(TRUE) - $start; |
1519 | 1534 |
} |
1520 | 1535 |
|
1521 |
function add_signature(&$view) { |
|
1536 |
/** |
|
1537 |
* |
|
1538 |
*/ |
|
1539 |
public function add_signature(&$view) { |
|
1522 | 1540 |
$view->query->add_field(NULL, "'" . $view->name . ':' . $view->current_display . "'", 'view_name'); |
1523 | 1541 |
} |
1524 | 1542 |
|
1525 |
function get_aggregation_info() { |
|
1526 |
// @todo -- need a way to get database specific and customized aggregation |
|
1543 |
/** |
|
1544 |
* |
|
1545 |
*/ |
|
1546 |
public function get_aggregation_info() { |
|
1547 |
// @todo Need a way to get database specific and customized aggregation |
|
1527 | 1548 |
// functions into here. |
1528 | 1549 |
return array( |
1529 | 1550 |
'group' => array( |
... | ... | |
1594 | 1615 |
'filter' => 'views_handler_filter_group_by_numeric', |
1595 | 1616 |
'sort' => 'views_handler_sort_group_by_numeric', |
1596 | 1617 |
), |
1597 |
) |
|
1618 |
),
|
|
1598 | 1619 |
) + views_fetch_plugin_data('query_aggregate'); |
1599 | 1620 |
} |
1600 | 1621 |
|
1601 | 1622 |
/** |
1602 | 1623 |
* Returns the according entity objects for the given query results. |
1603 |
* |
|
1604 | 1624 |
*/ |
1605 |
function get_result_entities($results, $relationship = NULL) { |
|
1625 |
public function get_result_entities($results, $relationship = NULL) {
|
|
1606 | 1626 |
$base_table = $this->base_table; |
1607 | 1627 |
$base_table_alias = $base_table; |
1608 | 1628 |
|
... | ... | |
1664 | 1684 |
|
1665 | 1685 |
return array($entity_type, $result); |
1666 | 1686 |
} |
1687 |
|
|
1667 | 1688 |
} |
1668 | 1689 |
|
1690 |
/** |
|
1691 |
* |
|
1692 |
*/ |
|
1669 | 1693 |
function views_query_default_aggregation_method_simple($group_type, $field) { |
1670 | 1694 |
return strtoupper($group_type) . '(' . $field . ')'; |
1671 | 1695 |
} |
1672 | 1696 |
|
1697 |
/** |
|
1698 |
* |
|
1699 |
*/ |
|
1673 | 1700 |
function views_query_default_aggregation_method_distinct($group_type, $field) { |
1674 | 1701 |
$group_type = str_replace('_distinct', '', $group_type); |
1675 | 1702 |
return strtoupper($group_type) . '(DISTINCT ' . $field . ')'; |
Formats disponibles : Unified diff
Weekly update of contrib modules