-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviews_pgsql_plugin_query_pgsql.inc
More file actions
148 lines (124 loc) · 4.37 KB
/
views_pgsql_plugin_query_pgsql.inc
File metadata and controls
148 lines (124 loc) · 4.37 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
<?php
// $Id$
class views_pgsql_plugin_query_pgsql extends views_plugin_query_default {
function init($base_table = 'node', $base_field = 'nid', $options) {
require_once drupal_get_path('module', 'views_pgsql') . '/views_pgsql.inc';
parent::init($base_table, $base_field, $options);
}
/**
* Returns the array of every field of the field-part of the query.
*
* This function differs from the parent one by not using GLOBALS['db_type'].
*/
function compile_fields($fields_array) {
$fields = $distinct = array();
$non_aggregates = array();
foreach ($fields_array as $field) {
$string = '';
if (!empty($field['table'])) {
$string .= $field['table'] . '.';
}
$string .= $field['field'];
// store for use with non-aggregates below
$fieldname = (!empty($field['alias']) ? $field['alias'] : $string);
if (!empty($field['distinct'])) {
$string = "DISTINCT($string)";
}
if (!empty($field['count'])) {
// Retained for compatibility
$field['function'] = 'count';
}
if (!empty($field['function'])) {
$info = $this->get_aggregation_info();
if (!empty($info[$field['function']]['method']) && function_exists($info[$field['function']]['method'])) {
$string = $info[$field['function']]['method']($field['function'], $string);
}
$this->has_aggregate = TRUE;
}
elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
// This line was changed
$string = "FIRST($string)";
}
elseif (empty($field['aggregate'])) {
$non_aggregates[] = $fieldname;
}
if ($field['alias']) {
$string .= " AS $field[alias]";
}
if (!empty($field['distinct']) && empty($field['function'])) {
$distinct[] = $string;
}
else {
$fields[] = $string;
}
if (!empty($get_count_optimized)) {
// We only want the first field in this case.
break;
}
}
return array(
$distinct,
$fields,
$non_aggregates,
);
}
/**
* Executes the query and fills the associated view object with according
* values.
*
* Values to set: $view->result, $view->total_rows, $view->execute_time,
* $view->current_page.
*
* The changes here is that it uses the views_pgsql_ functions instead of db_.
*/
function execute(&$view) {
$external = FALSE; // Whether this query will run against an external database.
$query = $this->final_query;
$count_query = $this->count_query;
$args = $this->query_args;
vpr($query);
$items = array();
if ($query) {
$replacements = $this->view->substitutions();
$query = str_replace(array_keys($replacements), $replacements, $query);
$count_query = 'SELECT COUNT(*) FROM (' . str_replace(array_keys($replacements), $replacements, $count_query) . ') count_alias';
if (is_array($args)) {
foreach ($args as $id => $arg) {
$args[$id] = str_replace(array_keys($replacements), $replacements, $arg);
}
}
// Detect an external database.
if (isset($view->base_database)) {
views_pgsql_set_active($view->base_database);
$external = TRUE;
}
$start = views_microtime();
if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
$this->pager->execute_count_query($count_query, $args);
}
// Let the pager modify the query to add limits.
$this->pager->pre_execute($query, $args);
if (!empty($this->limit) || !empty($this->offset)) {
// We can't have an offset without a limit, so provide a very large limit instead.
$limit = intval(!empty($this->limit) ? $this->limit : 999999);
$offset = intval(!empty($this->offset) ? $this->offset : 0);
$result = views_pgsql_query_range($query, $args, $offset, $limit);
}
else {
$result = views_pgsql_query($query, $args);
}
$view->result = array();
while ($item = views_pgsql_fetch_object($result)) {
$view->result[] = $item;
}
$this->pager->post_execute($view->result);
if ($this->pager->use_pager()) {
$view->total_rows = $this->pager->get_total_items();
}
// if ($external) {
// views_pgsql_set_active();
// }
}
$view->execute_time = views_microtime() - $start;
}
}