Newer
Older
<?php
/**
* @file
Damien McKenna
committed
* Definition of views_plugin_query_default.
*/
/**
* Object used to create a SELECT query.
*
* @ingroup views_query_plugins
*/
class views_plugin_query_default extends views_plugin_query {
/**
* A list of tables in the order they should be added, keyed by alias.
*/
Damien McKenna
committed
public $table_queue = array();
/**
Damien McKenna
committed
* Holds an array of tables and counts added so that we can create aliases.
*/
Damien McKenna
committed
public $tables = array();
/**
* Holds an array of relationships, which are aliases of the primary
* table that represent different ways to join the same table in.
*/
Damien McKenna
committed
public $relationships = array();
/**
* An array of sections of the WHERE query. Each section is in itself
* an array of pieces and a flag as to whether or not it should be AND
* or OR.
*/
Damien McKenna
committed
public $where = array();
/**
* An array of sections of the HAVING query. Each section is in itself
* an array of pieces and a flag as to whether or not it should be AND
* or OR.
*/
Damien McKenna
committed
public $having = array();
/**
* The default operator to use when connecting the WHERE groups. May be
* AND or OR.
*/
Damien McKenna
committed
public $group_operator = 'AND';
/**
* A simple array of order by clauses.
*/
Damien McKenna
committed
public $orderby = array();
/**
* A simple array of group by clauses.
*/
Damien McKenna
committed
public $groupby = array();
Herman van Rink
committed
/**
* An array of fields.
*/
Damien McKenna
committed
public $fields = array();
Herman van Rink
committed
/**
* The table header to use for tablesort. This matters because tablesort
* needs to modify the query and needs the header.
*/
Damien McKenna
committed
public $header = array();
/**
* A flag as to whether or not to make the primary field distinct.
*/
Damien McKenna
committed
public $distinct = FALSE;
Damien McKenna
committed
/**
*
*/
public $has_aggregate = FALSE;
Daniel Wehner
committed
/**
* Should this query be optimized for counts, for example no sorts.
*/
Damien McKenna
committed
public $get_count_optimized = NULL;
Daniel Wehner
committed
/**
* The current used pager plugin.
*
* @var views_plugin_pager
*/
Damien McKenna
committed
public $pager = NULL;
Damien McKenna
committed
/**
Daniel Wehner
committed
* An array mapping table aliases and field names to field aliases.
*/
Damien McKenna
committed
public $field_aliases = array();
Daniel Wehner
committed
Damien McKenna
committed
/**
* Query tags which will be passed over to the dbtng query object.
*/
Damien McKenna
committed
public $tags = array();
Daniel Wehner
committed
/**
* Is the view marked as not distinct.
*
* @var bool
*/
Damien McKenna
committed
public $no_distinct;
Daniel Wehner
committed
/**
* Defines the distinct type.
* - FALSE if it's distinct by base field.
* - TRUE if it just adds the sql distinct keyword.
*
* @var bool
*/
public $pure_distinct = FALSE;
/**
Damien McKenna
committed
* {@inheritdoc}
*/
Damien McKenna
committed
public function init($base_table = 'node', $base_field = 'nid', $options) {
Daniel Wehner
committed
parent::init($base_table, $base_field, $options);
Damien McKenna
committed
$this->base_table = $base_table;
// Predefine these above, for clarity.
$this->base_field = $base_field;
$this->relationships[$base_table] = array(
'link' => NULL,
'table' => $base_table,
'alias' => $base_table,
Damien McKenna
committed
'base' => $base_table,
);
Damien McKenna
committed
// Unit the table queue with our primary table.
$this->table_queue[$base_table] = array(
'alias' => $base_table,
'table' => $base_table,
'relationship' => $base_table,
'join' => NULL,
);
Damien McKenna
committed
// Init the tables with our primary table.
$this->tables[$base_table][$base_table] = array(
'count' => 1,
'alias' => $base_table,
);
Damien McKenna
committed
// We no longer want the base field to appear automatically.
// if ($base_field) {
// $this->fields[$base_field] = array(
// 'table' => $base_table,
// 'field' => $base_field,
// 'alias' => $base_field,
// );
// }
$this->count_field = array(
'table' => $base_table,
'field' => $base_field,
'alias' => $base_field,
'count' => TRUE,
);
}
Damien McKenna
committed
/**
* Utility methods to set flags and data.
*/
/**
Daniel Wehner
committed
* Set the view to be distinct.
*
* There are either distinct per base field or distinct in the pure sql way,
* based on $pure_distinct.
*
* @param bool $value
* Should the view by distincted.
* @param bool $pure_distinct
* Should only the sql keyword be added.
*/
Damien McKenna
committed
public function set_distinct($value = TRUE, $pure_distinct = FALSE) {
if (!(isset($this->no_distinct) && $value)) {
Daniel Wehner
committed
$this->pure_distinct = $pure_distinct;
}
}
/**
* Set what field the query will count() on for paging.
*/
Damien McKenna
committed
public function set_count_field($table, $field, $alias = NULL) {
if (empty($alias)) {
$alias = $table . '_' . $field;
}
$this->count_field = array(
'table' => $table,
'field' => $field,
'alias' => $alias,
'count' => TRUE,
);
}
/**
Damien McKenna
committed
* Set the table header.
*
* Used for click-sorting because it's needed info to modify the ORDER BY
* clause.
*/
Damien McKenna
committed
public function set_header($header) {
$this->header = $header;
}
Damien McKenna
committed
/**
* {@inheritdoc}
*/
public function option_definition() {
Daniel Wehner
committed
$options = parent::option_definition();
$options['disable_sql_rewrite'] = array(
'default' => FALSE,
'translatable' => FALSE,
'bool' => TRUE,
);
$options['distinct'] = array(
'default' => FALSE,
'bool' => TRUE,
);
Daniel Wehner
committed
$options['pure_distinct'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['slave'] = array(
'default' => FALSE,
'bool' => TRUE,
);
$options['query_comment'] = array(
'default' => '',
);
$options['query_tags'] = array(
'default' => array(),
);
Daniel Wehner
committed
return $options;
}
/**
* Add settings for the ui.
*/
Damien McKenna
committed
public function options_form(&$form, &$form_state) {
Daniel Wehner
committed
parent::options_form($form, $form_state);
Daniel Wehner
committed
$form['disable_sql_rewrite'] = array(
'#title' => t('Disable SQL rewriting'),
Daniel Wehner
committed
'#description' => t('Disabling SQL rewriting will disable node_access checks as well as other modules that implement hook_query_alter().'),
Daniel Wehner
committed
'#type' => 'checkbox',
'#default_value' => !empty($this->options['disable_sql_rewrite']),
Damien McKenna
committed
'#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>',
Daniel Wehner
committed
);
$form['distinct'] = array(
'#type' => 'checkbox',
'#title' => t('Distinct'),
'#description' => t('This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work. Note that this can slow queries down, so use it with caution.'),
'#default_value' => !empty($this->options['distinct']),
);
Daniel Wehner
committed
$form['pure_distinct'] = array(
'#type' => 'checkbox',
'#title' => t('Pure Distinct'),
'#description' => t('This will prevent views from adding the base column to the distinct field. If this is not selected and the base column is a primary key, then a non-pure distinct will not function properly because the primary key is always unique.'),
'#default_value' => !empty($this->options['pure_distinct']),
'#dependency' => array('edit-query-options-distinct' => '1'),
);
$form['slave'] = array(
'#type' => 'checkbox',
'#title' => t('Use Slave Server'),
'#description' => t('This will make the query attempt to connect to a slave server if available. If no slave server is defined or available, it will fall back to the default server.'),
'#default_value' => !empty($this->options['slave']),
);
$form['query_comment'] = array(
'#type' => 'textfield',
'#title' => t('Query Comment'),
'#description' => t('If set, this comment will be embedded in the query and passed to the SQL server. This can be helpful for logging or debugging.'),
'#default_value' => $this->options['query_comment'],
);
$form['query_tags'] = array(
'#type' => 'textfield',
'#title' => t('Query Tags'),
'#description' => t('If set, these tags will be appended to the query and can be used to identify the query in a module. This can be helpful for altering queries.'),
'#default_value' => implode(', ', $this->options['query_tags']),
'#element_validate' => array('views_element_validate_tags'),
);
}
/**
* Special submit handling.
*/
Damien McKenna
committed
public function options_submit(&$form, &$form_state) {
$element = array('#parents' => array('query', 'options', 'query_tags'));
$value = explode(',', drupal_array_get_nested_value($form_state['values'], $element['#parents']));
$value = array_filter(array_map('trim', $value));
form_set_value($element, $value, $form_state);
Daniel Wehner
committed
}
Damien McKenna
committed
/**
* Table/join adding.
*/
/**
Damien McKenna
committed
* A relationship is an alternative endpoint to a series of table joins.
*
* Relationships must be aliases of the primary table and they must join
* either to the primary table or to a pre-existing relationship.
*
Damien McKenna
committed
* An example of a relationship would be a nodereference table. If you have a
* nodereference named 'book_parent' which links to a parent node, you could
* set up a relationship 'node_book_parent' to 'node'. Then, anything that
* links to 'node' can link to 'node_book_parent' instead, thus allowing all
* properties of both nodes to be available in the query.
*
Damien McKenna
committed
* @param string $alias
* What this relationship will be called, and is also the alias for the
* table.
* @param views_join $join
* A views_join object (or derived object) to join the alias in.
Damien McKenna
committed
* @param string $base
* The name of the 'base' table this relationship represents; this tells the
* join search which path to attempt to use when finding the path to this
* relationship.
* @param string $link_point
* If this relationship links to something other than the primary table,
* specify that table here. For example, a 'track' node might have a
* relationship to an 'album' node, which might have a relationship to an
* 'artist' node.
*/
Damien McKenna
committed
public function add_relationship($alias, $join, $base, $link_point = NULL) {
if (empty($link_point)) {
$link_point = $this->base_table;
}
elseif (!array_key_exists($link_point, $this->relationships)) {
return FALSE;
}
// Make sure $alias isn't already used; if it, start adding stuff.
$alias_base = $alias;
$count = 1;
while (!empty($this->relationships[$alias])) {
$alias = $alias_base . '_' . $count++;
}
// Make sure this join is adjusted for our relationship.
if ($link_point && isset($this->relationships[$link_point])) {
$join = $this->adjust_join($join, $link_point);
}
Damien McKenna
committed
// Add the table directly to the queue to avoid accidentally marking it.
$this->table_queue[$alias] = array(
'table' => $join->table,
'num' => 1,
'alias' => $alias,
'join' => $join,
'relationship' => $link_point,
);
$this->relationships[$alias] = array(
'link' => $link_point,
'table' => $join->table,
'base' => $base,
);
Earl Miles
committed
$this->tables[$this->base_table][$alias] = array(
'count' => 1,
'alias' => $alias,
);
return $alias;
}
/**
* Add a table to the query, ensuring the path exists.
*
Damien McKenna
committed
* This function will test to ensure that the path back to the primary table
* is valid and exists; if you do not wish for this testing to occur, use
* $query->queue_table() instead.
*
Damien McKenna
committed
* @param string $table
* The name of the table to add. It needs to exist in the global table
* array.
Damien McKenna
committed
* @param string $relationship
* An alias of a table; if this is set, the path back to this table will be
* tested prior to adding the table, making sure that all intermediary
* tables exist and are properly aliased. If set to NULL the path to the
* primary table will be ensured. If the path cannot be made, the table
* will NOT be added.
* @param views_join $join
Damien McKenna
committed
* In some join configurations this table may actually join back through a
* different method; this is most likely to be used when tracing a
* hierarchy path. (node->parent->parent2->parent3). This parameter will
* specify how this table joins if it is not the default.
* @param string $alias
* A specific alias to use, rather than the default alias.
*
Damien McKenna
committed
* @return string
* The alias of the table; this alias can be used to access information
* about the table and should always be used to refer to the table when
* adding parts to the query. Or FALSE if the table was not able to be
* added.
*/
Damien McKenna
committed
public function add_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
if (!$this->ensure_path($table, $relationship, $join)) {
return FALSE;
}
if ($join && $relationship) {
$join = $this->adjust_join($join, $relationship);
}
return $this->queue_table($table, $relationship, $join, $alias);
}
/**
* Add a table to the query without ensuring the path.
*
* This is a pretty internal function to Views and add_table() or
* ensure_table() should be used instead of this one, unless you are
* absolutely sure this is what you want.
*
Damien McKenna
committed
* @param string $table
* The name of the table to add. It needs to exist in the global table
* array.
Damien McKenna
committed
* @param string $relationship
* The primary table alias this table is related to. If not set, the
* primary table will be used.
* @param views_join $join
Damien McKenna
committed
* In some join configurations this table may actually join back through a
* different method; this is most likely to be used when tracing a
* hierarchy path. (node->parent->parent2->parent3). This parameter will
* specify how this table joins if it is not the default.
* @param string $alias
* A specific alias to use, rather than the default alias.
*
Damien McKenna
committed
* @return string
* The alias of the table; this alias can be used to access information
* about the table and should always be used to refer to the table when
* adding parts to the query. Or FALSE if the table was not able to be
* added.
*/
Damien McKenna
committed
public function queue_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
// If the alias is set, make sure it doesn't already exist.
if (isset($this->table_queue[$alias])) {
return $alias;
}
if (empty($relationship)) {
$relationship = $this->base_table;
}
if (!array_key_exists($relationship, $this->relationships)) {
return FALSE;
}
if (!$alias && $join && $relationship && !empty($join->adjusted) && $table != $join->table) {
if ($relationship == $this->base_table) {
$alias = $table;
}
else {
$alias = $relationship . '_' . $table;
}
}
Damien McKenna
committed
// Check this again to make sure we don't blow up existing aliases for
// already adjusted joins.
if (isset($this->table_queue[$alias])) {
return $alias;
}
$alias = $this->mark_table($table, $relationship, $alias);
// If no alias is specified, give it the default.
if (!isset($alias)) {
$alias = $this->tables[$relationship][$table]['alias'] . $this->tables[$relationship][$table]['count'];
}
Damien McKenna
committed
// If this is a relationship based table, add a marker with the
// relationship as a primary table for the alias.
if ($table != $alias) {
$this->mark_table($alias, $this->base_table, $alias);
}
// If no join is specified, pull it from the table data.
if (!isset($join)) {
$join = $this->get_join_data($table, $this->relationships[$relationship]['base']);
if (empty($join)) {
return FALSE;
}
$join = $this->adjust_join($join, $relationship);
}
$this->table_queue[$alias] = array(
'table' => $table,
'num' => $this->tables[$relationship][$table]['count'],
'alias' => $alias,
'join' => $join,
'relationship' => $relationship,
);
return $alias;
}
Damien McKenna
committed
/**
*
*/
public function mark_table($table, $relationship, $alias) {
// Mark that this table has been added.
if (empty($this->tables[$relationship][$table])) {
if (!isset($alias)) {
$alias = '';
if ($relationship != $this->base_table) {
Damien McKenna
committed
// Double underscore will help prevent accidental name space
// collisions.
$alias = $relationship . '__';
}
$alias .= $table;
}
$this->tables[$relationship][$table] = array(
'count' => 1,
'alias' => $alias,
);
}
else {
$this->tables[$relationship][$table]['count']++;
}
return $alias;
}
/**
Damien McKenna
committed
* Ensure a table exists in the queue; if it already exists it won't do
* anything, but if it doesn't it will add the table queue. It will ensure a
* path leads back to the relationship table.
*
Damien McKenna
committed
* @param string $table
* The unaliased name of the table to ensure.
Damien McKenna
committed
* @param string $relationship
* The relationship to ensure the table links to. Each relationship will
Damien McKenna
committed
* get a unique instance of the table being added. If not specified, will
* be the primary table.
* @param views_join $join
* A views_join object (or derived object) to join the alias in.
*
Damien McKenna
committed
* @return string
* The alias used to refer to this specific table, or NULL if the table
* cannot be ensured.
*/
Damien McKenna
committed
public function ensure_table($table, $relationship = NULL, $join = NULL) {
// Ensure a relationship.
if (empty($relationship)) {
$relationship = $this->base_table;
}
// If the relationship is the primary table, this actually be a relationship
Damien McKenna
committed
// link back from an alias. We store all aliases along with the primary
// table to detect this state, because eventually it'll hit a table we
// already have and that's when we want to stop.
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
if ($relationship == $this->base_table && !empty($this->tables[$relationship][$table])) {
return $this->tables[$relationship][$table]['alias'];
}
if (!array_key_exists($relationship, $this->relationships)) {
return FALSE;
}
if ($table == $this->relationships[$relationship]['base']) {
return $relationship;
}
// If we do not have join info, fetch it.
if (!isset($join)) {
$join = $this->get_join_data($table, $this->relationships[$relationship]['base']);
}
// If it can't be fetched, this won't work.
if (empty($join)) {
return;
}
// Adjust this join for the relationship, which will ensure that the 'base'
// table it links to is correct. Tables adjoined to a relationship
// join to a link point, not the base table.
$join = $this->adjust_join($join, $relationship);
if ($this->ensure_path($table, $relationship, $join)) {
Damien McKenna
committed
// Attempt to eliminate redundant joins. If this table's relationship
// and join exactly matches an existing table's relationship and join, we
// do not have to join to it again; just return the existing table's
// alias.
// @see http://groups.drupal.org/node/11288
//
Damien McKenna
committed
// This can be done safely here but not lower down in queue_table(),
// because queue_table() is also used by add_table() which requires the
// ability to intentionally add the same table with the same join
// multiple times. For example, a view that filters on 3 taxonomy terms
// using AND needs to join taxonomy_term_data 3 times with the same join.
// scan through the table queue to see if a matching join and
// relationship exists. If so, use it instead of this join.
Damien McKenna
committed
// @todo Scanning through $this->table_queue results in an O(N^2)
// algorithm, and this code runs every time the view is instantiated
// (Views 2 does not currently cache queries). There are a couple
// possible "improvements" but we should do some performance testing
// before picking one.
foreach ($this->table_queue as $queued_table) {
Damien McKenna
committed
// In PHP 4 and 5, the == operation returns TRUE for two objects if
// they are instances of the same class and have the same attributes
// and values.
if ($queued_table['relationship'] == $relationship && $queued_table['join'] == $join) {
return $queued_table['alias'];
}
}
return $this->queue_table($table, $relationship, $join);
}
}
/**
* Make sure that the specified table can be properly linked to the primary
Damien McKenna
committed
* table in the JOINs. This function uses recursion. If the tables needed
* to complete the path back to the primary table are not in the query they
* will be added, but additional copies will NOT be added if the table is
* already there.
*/
Damien McKenna
committed
public function ensure_path($table, $relationship = NULL, $join = NULL, $traced = array(), $add = array()) {
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
if (!isset($relationship)) {
$relationship = $this->base_table;
}
if (!array_key_exists($relationship, $this->relationships)) {
return FALSE;
}
// If we do not have join info, fetch it.
if (!isset($join)) {
$join = $this->get_join_data($table, $this->relationships[$relationship]['base']);
}
// If it can't be fetched, this won't work.
if (empty($join)) {
return FALSE;
}
// Does a table along this path exist?
if (isset($this->tables[$relationship][$table]) ||
($join && $join->left_table == $relationship) ||
($join && $join->left_table == $this->relationships[$relationship]['table'])) {
// Make sure that we're linking to the correct table for our relationship.
foreach (array_reverse($add) as $table => $path_join) {
$this->queue_table($table, $relationship, $this->adjust_join($path_join, $relationship));
}
return TRUE;
}
// Have we been this way?
if (isset($traced[$join->left_table])) {
Damien McKenna
committed
// We looped. Broked.
return FALSE;
}
// Do we have to add this table?
$left_join = $this->get_join_data($join->left_table, $this->relationships[$relationship]['base']);
if (!isset($this->tables[$relationship][$join->left_table])) {
$add[$join->left_table] = $left_join;
}
// Keep looking.
$traced[$join->left_table] = TRUE;
return $this->ensure_path($join->left_table, $relationship, $left_join, $traced, $add);
}
/**
Damien McKenna
committed
* Fix a join to adhere to the proper relationship.
*
* The left table can vary based upon what relationship items are joined in
* on.
*/
Damien McKenna
committed
public function adjust_join($join, $relationship) {
if (!empty($join->adjusted)) {
return $join;
}
if (empty($relationship) || empty($this->relationships[$relationship])) {
return $join;
}
// Adjusts the left table for our relationship.
if ($relationship != $this->base_table) {
// If we're linking to the primary table, the relationship to use will
Damien McKenna
committed
// be the prior relationship. Unless it's a direct link. Safety! Don't
// modify an original here.
$join = clone $join;
// Do we need to try to ensure a path?
if ($join->left_table != $this->relationships[$relationship]['table'] &&
$join->left_table != $this->relationships[$relationship]['base'] &&
!isset($this->tables[$relationship][$join->left_table]['alias'])) {
$this->ensure_table($join->left_table, $relationship);
}
Damien McKenna
committed
// First, if this is our link point/anchor table, just use the
// relationship.
if ($join->left_table == $this->relationships[$relationship]['table']) {
$join->left_table = $relationship;
}
// then, try the base alias.
elseif (isset($this->tables[$relationship][$join->left_table]['alias'])) {
$join->left_table = $this->tables[$relationship][$join->left_table]['alias'];
}
// But if we're already looking at an alias, use that instead.
elseif (isset($this->table_queue[$relationship]['alias'])) {
$join->left_table = $this->table_queue[$relationship]['alias'];
}
}
$join->adjusted = TRUE;
return $join;
}
/**
* Retrieve join data from the larger join data cache.
*
Damien McKenna
committed
* @param string $table
* The table to get the join information for.
Damien McKenna
committed
* @param string $base_table
* The path we're following to get this join.
*
* @return views_join
* A views_join object or child object, if one exists.
*/
Damien McKenna
committed
public function get_join_data($table, $base_table) {
// Check to see if we're linking to a known alias. If so, get the real
// table's data instead.
if (!empty($this->table_queue[$table])) {
$table = $this->table_queue[$table]['table'];
}
return views_get_table_join($table, $base_table);
}
/**
* Get the information associated with a table.
*
* If you need the alias of a table with a particular relationship, use
* ensure_table().
*/
Damien McKenna
committed
public function get_table_info($table) {
if (!empty($this->table_queue[$table])) {
return $this->table_queue[$table];
}
// In rare cases we might *only* have aliased versions of the table.
if (!empty($this->tables[$this->base_table][$table])) {
$alias = $this->tables[$this->base_table][$table]['alias'];
if (!empty($this->table_queue[$alias])) {
return $this->table_queue[$alias];
}
}
}
/**
* Add a field to the query table, possibly with an alias. This will
Damien McKenna
committed
* automatically call ensure_table to make sure the required table exists,
* *unless* $table is unset.
*
Damien McKenna
committed
* @param string $table
* The table this field is attached to. If NULL, it is assumed this will be
* a formula; otherwise, ensure_table is used to make sure the table exists.
* @param string $field
* The name of the field to add. This may be a real field or a formula.
Damien McKenna
committed
* @param string $alias
* The alias to create. If not specified, the alias will be $table_$field
* unless $table is NULL. When adding formulae, it is recommended that an
* alias be used.
Damien McKenna
committed
* @param array $params
Daniel Wehner
committed
* An array of parameters additional to the field that will control items
* such as aggregation functions and DISTINCT.
*
Damien McKenna
committed
* @return string
* The name that this field can be referred to as, usually the alias.
*/
Damien McKenna
committed
public function add_field($table, $field, $alias = '', $params = array()) {
// We check for this specifically because it gets a special alias.
if ($table == $this->base_table && $field == $this->base_field && empty($alias)) {
$alias = $this->base_field;
}
if ($table && empty($this->table_queue[$table])) {
$this->ensure_table($table);
}
if (!$alias && $table) {
$alias = $table . '_' . $field;
}
Damien McKenna
committed
// Make sure an alias is assigned.
Daniel Wehner
committed
$alias = $alias ? $alias : $field;
Damien McKenna
committed
// PostgreSQL truncates aliases to 63 characters.
// @see http://drupal.org/node/571548
Earl Miles
committed
// We limit the length of the original alias up to 60 characters
Damien McKenna
committed
// to get a unique alias later if its have duplicates.
Earl Miles
committed
$alias = strtolower(substr($alias, 0, 60));
Daniel Wehner
committed
// Create a field info array.
$field_info = array(
'field' => $field,
'table' => $table,
'alias' => $alias,
) + $params;
Damien McKenna
committed
// Test to see if the field is actually the same or not. Due to differing
// parameters changing the aggregation function, we need to do some
// automatic alias collision detection.
Daniel Wehner
committed
$base = $alias;
$counter = 0;
while (!empty($this->fields[$alias]) && $this->fields[$alias] != $field_info) {
$field_info['alias'] = $alias = $base . '_' . ++$counter;
}
Daniel Wehner
committed
if (empty($this->fields[$alias])) {
$this->fields[$alias] = $field_info;
}
Daniel Wehner
committed
// Keep track of all aliases used.
$this->field_aliases[$table][$field] = $alias;
Daniel Wehner
committed
return $alias;
}
/**
Damien McKenna
committed
* Remove all fields that may've been added.
*
* Primarily used for summary mode where we're changing the query because we
* didn't get data we needed.
*/
Damien McKenna
committed
public function clear_fields() {
$this->fields = array();
}
/**
Damien McKenna
committed
* Add a simple WHERE clause to the query.
*
Damien McKenna
committed
* The caller is responsible for ensuring that all fields are fully qualified
* (TABLE.FIELD) and that the table already exists in the query.
*
* @param string $group
* The WHERE group to add these to; groups are used to create AND/OR
Damien McKenna
committed
* sections. Groups cannot be nested. Use 0 as the default group. If the
* group does not yet exist it will be created as an AND group.
* @param string $field
* The name of the field to check.
Damien McKenna
committed
* @param string $value
* The value to test the field against. In most cases, this is a scalar. For
* more complex options, it is an array. The meaning of each element in the
* array is dependent on the $operator.
* @param string $operator
* The comparison operator, such as =, <, or >=. It also accepts more
* complex options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is
* an array = otherwise. If $field is a string you have to use 'formula'
* here.
Daniel Wehner
committed
*
Claes Gyllensvärd
committed
* The $field, $value and $operator arguments can also be passed in with a
* single DatabaseCondition object, like this:
Damien McKenna
committed
*
Claes Gyllensvärd
committed
* @code
* $this->query->add_where(
* $this->options['group'],
* db_or()
* ->condition($field, $value, 'NOT IN')
* ->condition($field, $value, 'IS NULL')
* );
* @endcode
*
Daniel Wehner
committed
* @see QueryConditionInterface::condition()
Claes Gyllensvärd
committed
* @see DatabaseCondition
*/
Damien McKenna
committed
public function add_where($group, $field, $value = NULL, $operator = NULL) {
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all the
// default group.
if (empty($group)) {
$group = 0;
}
// Check for a group.
if (!isset($this->where[$group])) {
$this->set_where_group('AND', $group);
}
$this->where[$group]['conditions'][] = array(
'field' => $field,
'value' => $value,
'operator' => $operator,
);
}
/**
Daniel Wehner
committed
* Add a complex WHERE clause to the query.
*
* The caller is responsible for ensuring that all fields are fully qualified
Damien McKenna
committed
* (TABLE.FIELD) and that the table already exists in the query. Internally
* the dbtng method "where" is used.
*
Damien McKenna
committed
* @param string $group
Daniel Wehner
committed
* The WHERE group to add these to; groups are used to create AND/OR
Damien McKenna
committed
* sections. Groups cannot be nested. Use 0 as the default group. If the
* group does not yet exist it will be created as an AND group.
* @param string $snippet
* The snippet to check. This can be either a column or a complex expression
* like "UPPER(table.field) = 'value'".
* @param array $args
Daniel Wehner
committed
* An associative array of arguments.
*
* @see QueryConditionInterface::where()
*/
Damien McKenna
committed
public function add_where_expression($group, $snippet, $args = array()) {
Daniel Wehner
committed
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all
// the default group.
if (empty($group)) {
$group = 0;
}
// Check for a group.
if (!isset($this->where[$group])) {
$this->set_where_group('AND', $group);
}
$this->where[$group]['conditions'][] = array(
'field' => $snippet,
'value' => $args,
'operator' => 'formula',
);
}
/**
* Add a simple HAVING clause to the query.
*
Daniel Wehner
committed
* The caller is responsible for ensuring that all fields are fully qualified
Damien McKenna
committed
* (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist
* in the query. Internally the dbtng method "havingCondition" is used.
*
Damien McKenna
committed
* @param string $group
* The HAVING group to add these to; groups are used to create AND/OR
Damien McKenna
committed
* sections. Groups cannot be nested. Use 0 as the default group. If the
* group does not yet exist it will be created as an AND group.
* @param string $field
* The name of the field to check.
Damien McKenna
committed
* @param string $value
* The value to test the field against. In most cases, this is a scalar. For
* more complex options, it is an array. The meaning of each element in the
* array is dependent on the $operator.
* @param string $operator
* The comparison operator, such as =, <, or >=. It also accepts more
* complex options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is
* an array = otherwise. If $field is a string you have to use 'formula'
* here.
Daniel Wehner
committed
*
* @see SelectQueryInterface::havingCondition()
*/
Damien McKenna
committed
public function add_having($group, $field, $value = NULL, $operator = NULL) {
// Ensure all variants of 0 are actually 0. Thus '', 0 and NULL are all the
// default group.
if (empty($group)) {
$group = 0;
}
// Check for a group.
if (!isset($this->having[$group])) {
$this->set_where_group('AND', $group, 'having');
}
// Add the clause and the args.
$this->having[$group]['conditions'][] = array(
'field' => $field,
'value' => $value,
'operator' => $operator,
);
}
Daniel Wehner
committed
/**
* Add a complex HAVING clause to the query.
Damien McKenna
committed
*
Daniel Wehner
committed
* The caller is responsible for ensuring that all fields are fully qualified
Damien McKenna
committed
* (TABLE.FIELD) and that the table and an appropriate GROUP BY already exist
* in the query. Internally the dbtng method "having" is used.
Daniel Wehner
committed
*
Damien McKenna
committed
* @param string $group
Daniel Wehner
committed
* The HAVING group to add these to; groups are used to create AND/OR
Damien McKenna
committed
* sections. Groups cannot be nested. Use 0 as the default group. If the
* group does not yet exist it will be created as an AND group.
* @param string $snippet
* The snippet to check. This can be either a column or a complex
* expression like "COUNT(table.field) > 3"
* @param array $args