1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657 |
- <?php
- class views_plugin_query_default extends views_plugin_query {
-
- var $table_queue = array();
-
- var $tables = array();
-
- var $relationships = array();
-
- var $where = array();
-
- var $having = array();
-
- var $group_operator = 'AND';
-
- var $orderby = array();
-
- var $groupby = array();
-
- var $fields = array();
-
- var $header = array();
-
- var $distinct = FALSE;
- var $has_aggregate = FALSE;
-
- var $get_count_optimized = NULL;
-
- var $pager = NULL;
-
- var $field_aliases = array();
-
- var $tags = array();
-
- var $no_distinct;
-
- public $pure_distinct = FALSE;
-
- function init($base_table = 'node', $base_field = 'nid', $options) {
- parent::init($base_table, $base_field, $options);
- $this->base_table = $base_table;
- $this->base_field = $base_field;
- $this->relationships[$base_table] = array(
- 'link' => NULL,
- 'table' => $base_table,
- 'alias' => $base_table,
- 'base' => $base_table
- );
-
- $this->table_queue[$base_table] = array(
- 'alias' => $base_table,
- 'table' => $base_table,
- 'relationship' => $base_table,
- 'join' => NULL,
- );
-
- $this->tables[$base_table][$base_table] = array(
- 'count' => 1,
- 'alias' => $base_table,
- );
- $this->count_field = array(
- 'table' => $base_table,
- 'field' => $base_field,
- 'alias' => $base_field,
- 'count' => TRUE,
- );
- }
-
-
-
- function set_distinct($value = TRUE, $pure_distinct = FALSE) {
- if (!(isset($this->no_distinct) && $value)) {
- $this->distinct = $value;
- $this->pure_distinct = $pure_distinct;
- }
- }
-
- 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,
- );
- }
-
- function set_header($header) {
- $this->header = $header;
- }
- function option_definition() {
- $options = parent::option_definition();
- $options['disable_sql_rewrite'] = array(
- 'default' => FALSE,
- 'translatable' => FALSE,
- 'bool' => TRUE,
- );
- $options['distinct'] = array(
- 'default' => FALSE,
- 'bool' => TRUE,
- );
- $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(),
- );
- return $options;
- }
-
- function options_form(&$form, &$form_state) {
- parent::options_form($form, $form_state);
- $form['disable_sql_rewrite'] = array(
- '#title' => t('Disable SQL rewriting'),
- '#description' => t('Disabling SQL rewriting will disable node_access checks as well as other modules that implement hook_query_alter().'),
- '#type' => 'checkbox',
- '#default_value' => !empty($this->options['disable_sql_rewrite']),
- '#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>',
- );
- $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']),
- );
- $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'),
- );
- }
-
- 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);
- }
-
-
-
- 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;
- }
-
- $alias_base = $alias;
- $count = 1;
- while (!empty($this->relationships[$alias])) {
- $alias = $alias_base . '_' . $count++;
- }
-
- if ($link_point && isset($this->relationships[$link_point])) {
- $join = $this->adjust_join($join, $link_point);
- }
-
-
- $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,
- );
- $this->tables[$this->base_table][$alias] = array(
- 'count' => 1,
- 'alias' => $alias,
- );
- return $alias;
- }
-
- 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);
- }
-
- function queue_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
-
- 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;
- }
- }
-
-
- if (isset($this->table_queue[$alias])) {
- return $alias;
- }
- $alias = $this->mark_table($table, $relationship, $alias);
-
- if (!isset($alias)) {
- $alias = $this->tables[$relationship][$table]['alias'] . $this->tables[$relationship][$table]['count'];
- }
-
-
- if ($table != $alias) {
- $this->mark_table($alias, $this->base_table, $alias);
- }
-
- 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;
- }
- function mark_table($table, $relationship, $alias) {
-
- if (empty($this->tables[$relationship][$table])) {
- if (!isset($alias)) {
- $alias = '';
- if ($relationship != $this->base_table) {
-
-
- $alias = $relationship . '__';
- }
- $alias .= $table;
- }
- $this->tables[$relationship][$table] = array(
- 'count' => 1,
- 'alias' => $alias,
- );
- }
- else {
- $this->tables[$relationship][$table]['count']++;
- }
- return $alias;
- }
-
- function ensure_table($table, $relationship = NULL, $join = NULL) {
-
- if (empty($relationship)) {
- $relationship = $this->base_table;
- }
-
-
-
-
- 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 (!isset($join)) {
- $join = $this->get_join_data($table, $this->relationships[$relationship]['base']);
- }
-
- if (empty($join)) {
- return;
- }
-
-
-
- $join = $this->adjust_join($join, $relationship);
- if ($this->ensure_path($table, $relationship, $join)) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- foreach ($this->table_queue as $queued_table) {
-
-
-
- if ($queued_table['relationship'] == $relationship && $queued_table['join'] == $join) {
- return $queued_table['alias'];
- }
- }
- return $this->queue_table($table, $relationship, $join);
- }
- }
-
- function ensure_path($table, $relationship = NULL, $join = NULL, $traced = array(), $add = array()) {
- if (!isset($relationship)) {
- $relationship = $this->base_table;
- }
- if (!array_key_exists($relationship, $this->relationships)) {
- return FALSE;
- }
-
- if (!isset($join)) {
- $join = $this->get_join_data($table, $this->relationships[$relationship]['base']);
- }
-
- if (empty($join)) {
- return FALSE;
- }
-
- if (isset($this->tables[$relationship][$table]) ||
- ($join && $join->left_table == $relationship) ||
- ($join && $join->left_table == $this->relationships[$relationship]['table'])) {
-
- foreach (array_reverse($add) as $table => $path_join) {
- $this->queue_table($table, $relationship, $this->adjust_join($path_join, $relationship));
- }
- return TRUE;
- }
-
- if (isset($traced[$join->left_table])) {
-
- return FALSE;
- }
-
- $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;
- }
-
- $traced[$join->left_table] = TRUE;
- return $this->ensure_path($join->left_table, $relationship, $left_join, $traced, $add);
- }
-
- function adjust_join($join, $relationship) {
- if (!empty($join->adjusted)) {
- return $join;
- }
- if (empty($relationship) || empty($this->relationships[$relationship])) {
- return $join;
- }
-
- if ($relationship != $this->base_table) {
-
-
-
- $join = clone $join;
-
- 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);
- }
-
- if ($join->left_table == $this->relationships[$relationship]['table']) {
- $join->left_table = $relationship;
- }
-
- elseif (isset($this->tables[$relationship][$join->left_table]['alias'])) {
- $join->left_table = $this->tables[$relationship][$join->left_table]['alias'];
- }
-
- elseif (isset($this->table_queue[$relationship]['alias'])) {
- $join->left_table = $this->table_queue[$relationship]['alias'];
- }
- }
- $join->adjusted = TRUE;
- return $join;
- }
-
- function get_join_data($table, $base_table) {
-
-
- if (!empty($this->table_queue[$table])) {
- $table = $this->table_queue[$table]['table'];
- }
- return views_get_table_join($table, $base_table);
- }
-
- function get_table_info($table) {
- if (!empty($this->table_queue[$table])) {
- return $this->table_queue[$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];
- }
- }
- }
-
- function add_field($table, $field, $alias = '', $params = array()) {
-
- 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;
- }
-
- $alias = $alias ? $alias : $field;
-
-
-
- $alias = strtolower(substr($alias, 0, 60));
-
- $field_info = array(
- 'field' => $field,
- 'table' => $table,
- 'alias' => $alias,
- ) + $params;
-
-
-
- $base = $alias;
- $counter = 0;
- while (!empty($this->fields[$alias]) && $this->fields[$alias] != $field_info) {
- $field_info['alias'] = $alias = $base . '_' . ++$counter;
- }
- if (empty($this->fields[$alias])) {
- $this->fields[$alias] = $field_info;
- }
-
- $this->field_aliases[$table][$field] = $alias;
- return $alias;
- }
-
- function clear_fields() {
- $this->fields = array();
- }
-
- function add_where($group, $field, $value = NULL, $operator = NULL) {
-
-
- if (empty($group)) {
- $group = 0;
- }
-
- if (!isset($this->where[$group])) {
- $this->set_where_group('AND', $group);
- }
- $this->where[$group]['conditions'][] = array(
- 'field' => $field,
- 'value' => $value,
- 'operator' => $operator,
- );
- }
-
- function add_where_expression($group, $snippet, $args = array()) {
-
-
- if (empty($group)) {
- $group = 0;
- }
-
- if (!isset($this->where[$group])) {
- $this->set_where_group('AND', $group);
- }
- $this->where[$group]['conditions'][] = array(
- 'field' => $snippet,
- 'value' => $args,
- 'operator' => 'formula',
- );
- }
-
- function add_having($group, $field, $value = NULL, $operator = NULL) {
-
-
- if (empty($group)) {
- $group = 0;
- }
-
- if (!isset($this->having[$group])) {
- $this->set_where_group('AND', $group, 'having');
- }
-
- $this->having[$group]['conditions'][] = array(
- 'field' => $field,
- 'value' => $value,
- 'operator' => $operator,
- );
- }
-
- function add_having_expression($group, $snippet, $args = array()) {
-
-
- if (empty($group)) {
- $group = 0;
- }
-
- if (!isset($this->having[$group])) {
- $this->set_where_group('AND', $group, 'having');
- }
-
- $this->having[$group]['conditions'][] = array(
- 'field' => $snippet,
- 'value' => $args,
- 'operator' => 'formula',
- );
- }
-
- function add_orderby($table, $field = NULL, $order = 'ASC', $alias = '', $params = array()) {
-
-
- if ($table && $table != 'rand') {
- $this->ensure_table($table);
- }
-
-
- if (!$alias && $table) {
- $as = $table . '_' . $field;
- }
- else {
- $as = $alias;
- }
- if ($field) {
- $as = $this->add_field($table, $field, $as, $params);
- }
- $this->orderby[] = array(
- 'field' => $as,
- 'direction' => strtoupper($order)
- );
-
- }
-
- function add_groupby($clause) {
-
- if (!in_array($clause, $this->groupby)) {
- $this->groupby[] = $clause;
- }
- }
-
- function get_field_alias($table_alias, $field) {
- return isset($this->field_aliases[$table_alias][$field]) ? $this->field_aliases[$table_alias][$field] : FALSE;
- }
-
- function add_tag($tag) {
- $this->tags[] = $tag;
- }
-
- function placeholder($base = 'views') {
- static $placeholders = array();
- if (!isset($placeholders[$base])) {
- $placeholders[$base] = 0;
- return ':' . $base;
- }
- else {
- return ':' . $base . ++$placeholders[$base];
- }
- }
-
- function build_condition($where = 'where') {
- $has_condition = FALSE;
- $has_arguments = FALSE;
- $has_filter = FALSE;
- $main_group = db_and();
- $filter_group = $this->group_operator == 'OR' ? db_or() : db_and();
- foreach ($this->$where as $group => $info) {
- if (!empty($info['conditions'])) {
- $sub_group = $info['type'] == 'OR' ? db_or() : db_and();
- foreach ($info['conditions'] as $key => $clause) {
-
-
-
- if (is_object($clause['value']) && $clause['value'] instanceof SelectQuery) {
- $clause['value'] = clone $clause['value'];
- }
- if ($clause['operator'] == 'formula') {
- $has_condition = TRUE;
- $sub_group->where($clause['field'], $clause['value']);
- }
- else {
- $has_condition = TRUE;
- $sub_group->condition($clause['field'], $clause['value'], $clause['operator']);
- }
- }
-
- if ($group != 0) {
- $has_filter = TRUE;
- $filter_group->condition($sub_group);
- }
- else {
- $has_arguments = TRUE;
- $main_group->condition($sub_group);
- }
- }
- }
- if ($has_filter) {
- $main_group->condition($filter_group);
- }
- if (!$has_arguments && $has_condition) {
- return $filter_group;
- }
- if ($has_arguments && $has_condition) {
- return $main_group;
- }
- }
-
- function compile_fields($fields_array, $query) {
- $non_aggregates = array();
- foreach ($fields_array as $field) {
- $string = '';
- if (!empty($field['table'])) {
- $string .= $field['table'] . '.';
- }
- $string .= $field['field'];
- $fieldname = (!empty($field['alias']) ? $field['alias'] : $string);
- if (!empty($field['distinct'])) {
- throw new Exception("Column-level distinct is not supported anymore.");
- }
- if (!empty($field['count'])) {
-
- $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);
- $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
- $query->addExpression($string, $fieldname, $placeholders);
- }
- $this->has_aggregate = TRUE;
- }
-
- elseif (empty($field['table'])) {
- $non_aggregates[] = $fieldname;
- $placeholders = !empty($field['placeholders']) ? $field['placeholders'] : array();
- $query->addExpression($string, $fieldname, $placeholders);
- }
- elseif ($this->distinct && !in_array($fieldname, $this->groupby)) {
-
-
- $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
- }
- elseif (empty($field['aggregate'])) {
- $non_aggregates[] = $fieldname;
- $query->addField(!empty($field['table']) ? $field['table'] : $this->base_table, $field['field'], $fieldname);
- }
-
- if (!empty($field['distinct']) && empty($field['function'])) {
- $distinct[] = $string;
- }
- else {
- $fields[] = $string;
- }
- if ($this->get_count_optimized) {
-
- break;
- }
- }
- return array(
- $non_aggregates,
- );
- }
-
- function query($get_count = FALSE) {
-
- 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;
- }
-
- $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';
-
- if (isset($this->view->base_database)) {
- $key = $this->view->base_database;
- }
-
- if (!empty($this->options['slave'])) {
- $target = 'slave';
- }
-
-
- $query = Database::getConnection($target, $key)
- ->select($this->base_table, $this->base_table, $options)
- ->addTag('views')
- ->addTag('views_' . $this->view->name);
-
- foreach ($this->tags as $tag) {
- $query->addTag($tag);
- }
- if (!empty($distinct)) {
- $query->distinct();
- }
- $joins = $where = $having = $orderby = $groupby = '';
- $fields = $distinct = array();
-
- foreach ($this->table_queue as $table) {
- if (is_object($table['join'])) {
- $table['join']->build_join($query, $table, $this);
- }
- }
- $this->has_aggregate = FALSE;
- $non_aggregates = array();
- list($non_aggregates) = $this->compile_fields($fields_array, $query);
- if (count($this->having)) {
- $this->has_aggregate = TRUE;
- }
- 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) {
-
- 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);
- }
-
- if (!empty($this->options['query_comment'])) {
- $query->comment($this->options['query_comment']);
- }
-
- if (!empty($this->options['query_tags'])) {
- foreach ($this->options['query_tags'] as $tag) {
- $query->addTag($tag);
- }
- }
-
- $query->addMetaData('views_substitutions', module_invoke_all('views_query_substitutions', $this));
- return $query;
- }
-
- function get_where_args() {
- $args = array();
- foreach ($this->where as $group => $where) {
- $args = array_merge($args, $where['args']);
- }
- foreach ($this->having as $group => $having) {
- $args = array_merge($args, $having['args']);
- }
- return $args;
- }
-
- function alter(&$view) {
- foreach (module_implements('views_query_alter') as $module) {
- $function = $module . '_views_query_alter';
- $function($view, $this);
- }
- }
-
- function build(&$view) {
-
- if (!empty($this->options['distinct'])) {
- $this->set_distinct(TRUE, !empty($this->options['pure_distinct']));
- }
-
- $this->view = $view;
- $view->init_pager();
-
- $this->pager->query();
- $view->build_info['query'] = $this->query();
- $view->build_info['count_query'] = $this->query(TRUE);
- }
-
- function execute(&$view) {
- $external = FALSE;
- $query = $view->build_info['query'];
- $count_query = $view->build_info['count_query'];
- $query->addMetaData('view', $view);
- $count_query->addMetaData('view', $view);
- if (empty($this->options['disable_sql_rewrite'])) {
- $base_table_data = views_fetch_data($this->base_table);
- if (isset($base_table_data['table']['base']['access query tag'])) {
- $access_tag = $base_table_data['table']['base']['access query tag'];
- $query->addTag($access_tag);
- $count_query->addTag($access_tag);
- }
- }
- $items = array();
- if ($query) {
- $additional_arguments = module_invoke_all('views_query_substitutions', $view);
-
-
-
-
-
-
-
- $count_query->preExecute();
-
- $count_query = $count_query->countQuery();
-
-
-
- if (!empty($additional_arguments)) {
-
-
- }
- $start = microtime(TRUE);
- try {
- if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
- $this->pager->execute_count_query($count_query);
- }
-
- $this->pager->pre_execute($query);
- if (!empty($this->limit) || !empty($this->offset)) {
-
- $limit = intval(!empty($this->limit) ? $this->limit : 999999);
- $offset = intval(!empty($this->offset) ? $this->offset : 0);
- $query->range($offset, $limit);
- }
- $result = $query->execute();
- $view->result = array();
- foreach ($result as $item) {
- $view->result[] = $item;
- }
- $this->pager->post_execute($view->result);
- if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
- $view->total_rows = $this->pager->get_total_items();
- }
- }
- catch (Exception $e) {
- $view->result = array();
- if (!empty($view->live_preview)) {
- drupal_set_message($e->getMessage(), 'error');
- }
- else {
- vpr('Exception in @human_name[@view_name]: @message', array('@human_name' => $view->human_name, '@view_name' => $view->name, '@message' => $e->getMessage()));
- }
- }
- }
- else {
- $start = microtime(TRUE);
- }
- $view->execute_time = microtime(TRUE) - $start;
- }
- function add_signature(&$view) {
- $view->query->add_field(NULL, "'" . $view->name . ':' . $view->current_display . "'", 'view_name');
- }
- function get_aggregation_info() {
-
-
- return array(
- 'group' => array(
- 'title' => t('Group results together'),
- 'is aggregate' => FALSE,
- ),
- 'count' => array(
- 'title' => t('Count'),
- 'method' => 'views_query_default_aggregation_method_simple',
- 'handler' => array(
- 'argument' => 'views_handler_argument_group_by_numeric',
- 'field' => 'views_handler_field_numeric',
- 'filter' => 'views_handler_filter_group_by_numeric',
- 'sort' => 'views_handler_sort_group_by_numeric',
- ),
- ),
- 'count_distinct' => array(
- 'title' => t('Count DISTINCT'),
- 'method' => 'views_query_default_aggregation_method_distinct',
- 'handler' => array(
- 'argument' => 'views_handler_argument_group_by_numeric',
- 'field' => 'views_handler_field_numeric',
- 'filter' => 'views_handler_filter_group_by_numeric',
- 'sort' => 'views_handler_sort_group_by_numeric',
- ),
- ),
- 'sum' => array(
- 'title' => t('Sum'),
- 'method' => 'views_query_default_aggregation_method_simple',
- 'handler' => array(
- 'argument' => 'views_handler_argument_group_by_numeric',
- 'filter' => 'views_handler_filter_group_by_numeric',
- 'sort' => 'views_handler_sort_group_by_numeric',
- ),
- ),
- 'avg' => array(
- 'title' => t('Average'),
- 'method' => 'views_query_default_aggregation_method_simple',
- 'handler' => array(
- 'argument' => 'views_handler_argument_group_by_numeric',
- 'filter' => 'views_handler_filter_group_by_numeric',
- 'sort' => 'views_handler_sort_group_by_numeric',
- ),
- ),
- 'min' => array(
- 'title' => t('Minimum'),
- 'method' => 'views_query_default_aggregation_method_simple',
- 'handler' => array(
- 'argument' => 'views_handler_argument_group_by_numeric',
- 'filter' => 'views_handler_filter_group_by_numeric',
- 'sort' => 'views_handler_sort_group_by_numeric',
- ),
- ),
- 'max' => array(
- 'title' => t('Maximum'),
- 'method' => 'views_query_default_aggregation_method_simple',
- 'handler' => array(
- 'argument' => 'views_handler_argument_group_by_numeric',
- 'filter' => 'views_handler_filter_group_by_numeric',
- 'sort' => 'views_handler_sort_group_by_numeric',
- ),
- ),
- 'stddev_pop' => array(
- 'title' => t('Standard deviation'),
- 'method' => 'views_query_default_aggregation_method_simple',
- 'handler' => array(
- 'argument' => 'views_handler_argument_group_by_numeric',
- 'filter' => 'views_handler_filter_group_by_numeric',
- 'sort' => 'views_handler_sort_group_by_numeric',
- ),
- )
- );
- }
-
- function get_result_entities($results, $relationship = NULL) {
- $base_table = $this->base_table;
- $base_table_alias = $base_table;
- if (!empty($relationship)) {
- foreach ($this->view->relationship as $current) {
- if ($current->alias == $relationship) {
- $base_table = $current->definition['base'];
- $base_table_alias = $relationship;
- break;
- }
- }
- }
- $table_data = views_fetch_data($base_table);
-
- if (!isset($table_data['table']['entity type'])) {
- return FALSE;
- }
- $entity_type = $table_data['table']['entity type'];
- $info = entity_get_info($entity_type);
- $id_alias = $this->get_field_alias($base_table_alias, $info['entity keys']['id']);
-
- $ids = array();
- foreach ($results as $key => $result) {
- if (isset($result->$id_alias)) {
- $ids[$key] = $result->$id_alias;
- }
- }
- $entities = entity_load($entity_type, $ids);
-
- $result = array();
- foreach ($ids as $key => $id) {
- $result[$key] = isset($entities[$id]) ? $entities[$id] : FALSE;
- }
- return array($entity_type, $result);
- }
- }
- function views_query_default_aggregation_method_simple($group_type, $field) {
- return strtoupper($group_type) . '(' . $field . ')';
- }
- function views_query_default_aggregation_method_distinct($group_type, $field) {
- $group_type = str_replace('_distinct', '', $group_type);
- return strtoupper($group_type) . '(DISTINCT ' . $field . ')';
- }
- function views_element_validate_tags($element, &$form_state) {
- $values = array_map('trim', explode(',', $element['#value']));
- foreach ($values as $value) {
- if (preg_match("/[^a-z_]/", $value)) {
- form_error($element, t('The query tags may only contain lower-case alphabetical characters and underscores.'));
- return;
- }
- }
- }
|