function views_plugin_query_default::query
Generate a query and a countquery from all of the information supplied to the object.
Parameters
bool $get_count: Provide a countquery if this is true, otherwise provide a normal query.
Return value
SelectQuery A SelectQuery object.
Overrides views_plugin_query::query
1 call to views_plugin_query_default::query()
- views_plugin_query_default::build in plugins/
views_plugin_query_default.inc - Builds the necessary info to execute the query.
File
-
plugins/
views_plugin_query_default.inc, line 1290
Class
- views_plugin_query_default
- Object used to create a SELECT query.
Code
public function query($get_count = FALSE) {
// Check query distinct value.
if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) {
if ($this->pure_distinct === FALSE) {
$base_field_alias = $this->add_field($this->base_table, $this->base_field);
$this->add_groupby($base_field_alias);
}
$distinct = TRUE;
}
// An optimized count query includes just the base field instead of all the
// fields. Determine of this query qualifies by checking for a groupby or
// distinct.
$fields_array = $this->fields;
if ($get_count && !$this->groupby) {
foreach ($fields_array as $field) {
if (!empty($field['distinct']) || !empty($field['function'])) {
$this->get_count_optimized = FALSE;
break;
}
}
}
else {
$this->get_count_optimized = FALSE;
}
if (!isset($this->get_count_optimized)) {
$this->get_count_optimized = TRUE;
}
$options = array();
$target = 'default';
$key = 'default';
// Detect an external database and set the.
if (isset($this->view->base_database)) {
$key = $this->view->base_database;
}
if (!empty($this->options['slave'])) {
$target = 'slave';
// Set the replica target if the replica option is set.
}
// Go ahead and build the query. db_select doesn't support to specify the
// key, so use getConnection directly.
$query = Database::getConnection($target, $key)->select($this->base_table, $this->base_table, $options)
->addTag('views')
->addTag('views_' . $this->view->name);
// Add the tags added to the view itself.
foreach ($this->tags as $tag) {
$query->addTag($tag);
}
if (!empty($distinct)) {
$query->distinct();
}
// Add all the tables to the query via joins. We assume all LEFT joins.
foreach ($this->table_queue as $table) {
if (is_object($table['join'])) {
$table['join']->build_join($query, $table, $this);
}
}
$this->has_aggregate = FALSE;
list($non_aggregates) = $this->compile_fields($fields_array, $query);
if (count($this->having)) {
$this->has_aggregate = TRUE;
}
elseif (!$this->has_aggregate && !empty($this->view->display_handler)) {
$this->has_aggregate = $this->view->display_handler
->get_option('group_by');
}
if ($this->has_aggregate && (!empty($this->groupby) || !empty($non_aggregates))) {
$groupby = array_unique(array_merge($this->groupby, $non_aggregates));
foreach ($groupby as $field) {
$query->groupBy($field);
}
if (!empty($this->having) && ($condition = $this->build_condition('having'))) {
$query->havingCondition($condition);
}
}
if (!$this->get_count_optimized) {
// We only add the orderby if we're not counting.
if ($this->orderby) {
foreach ($this->orderby as $order) {
if ($order['field'] == 'rand_') {
$query->orderRandom();
}
else {
$query->orderBy($order['field'], $order['direction']);
}
}
}
}
if (!empty($this->where) && ($condition = $this->build_condition('where'))) {
$query->condition($condition);
}
// Add a query comment.
if (!empty($this->options['query_comment'])) {
$query->comment($this->options['query_comment']);
}
// Add the query tags.
if (!empty($this->options['query_tags'])) {
foreach ($this->options['query_tags'] as $tag) {
$query->addTag($tag);
}
}
// Add all query substitutions as metadata.
$query->addMetaData('views_substitutions', module_invoke_all('views_query_substitutions', $this->view));
if (!$get_count) {
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);
$query->range($offset, $limit);
}
}
return $query;
}