views_plugin_query.inc

Defines the base query class, which is the underlying layer in a View.

File

plugins/views_plugin_query.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Defines the base query class, which is the underlying layer in a View.
  5. */
  6. /**
  7. * @defgroup views_query_plugins Views query plugins
  8. * @{
  9. * A Views query plugin builds SQL to execute using the Drupal database API.
  10. *
  11. * @see hook_views_plugins()
  12. */
  13. /**
  14. * Object used to create a SELECT query.
  15. */
  16. class views_plugin_query extends views_plugin {
  17. /**
  18. * A pager plugin that should be provided by the display.
  19. *
  20. * @var views_plugin_pager
  21. */
  22. var $pager = NULL;
  23. /**
  24. * Constructor; Create the basic query object and fill with default values.
  25. */
  26. function init($base_table, $base_field, $options) {
  27. $this->base_table = $base_table;
  28. $this->base_field = $base_field;
  29. $this->unpack_options($this->options, $options);
  30. }
  31. /**
  32. * Generate a query and a countquery from all of the information supplied
  33. * to the object.
  34. *
  35. * @param $get_count
  36. * Provide a countquery if this is true, otherwise provide a normal query.
  37. */
  38. function query($get_count = FALSE) { }
  39. /**
  40. * Let modules modify the query just prior to finalizing it.
  41. *
  42. * @param view $view
  43. * The view which is executed.
  44. */
  45. function alter(&$view) { }
  46. /**
  47. * Builds the necessary info to execute the query.
  48. *
  49. * @param view $view
  50. * The view which is executed.
  51. */
  52. function build(&$view) { }
  53. /**
  54. * Executes the query and fills the associated view object with according
  55. * values.
  56. *
  57. * Values to set: $view->result, $view->total_rows, $view->execute_time,
  58. * $view->pager['current_page'].
  59. *
  60. * $view->result should contain an array of objects. The array must use a
  61. * numeric index starting at 0.
  62. *
  63. * @param view $view
  64. * The view which is executed.
  65. */
  66. function execute(&$view) { }
  67. /**
  68. * Add a signature to the query, if such a thing is feasible.
  69. *
  70. * This signature is something that can be used when perusing query logs to
  71. * discern where particular queries might be coming from.
  72. *
  73. * @param view $view
  74. * The view which is executed.
  75. */
  76. function add_signature(&$view) { }
  77. /**
  78. * Get aggregation info for group by queries.
  79. *
  80. * If NULL, aggregation is not allowed.
  81. */
  82. function get_aggregation_info() { }
  83. /**
  84. * Add settings for the ui.
  85. */
  86. function options_form(&$form, &$form_state) {
  87. parent::options_form($form, $form_state);
  88. }
  89. function options_validate(&$form, &$form_state) { }
  90. function options_submit(&$form, &$form_state) { }
  91. function summary_title() {
  92. return t('Settings');
  93. }
  94. /**
  95. * Set a LIMIT on the query, specifying a maximum number of results.
  96. */
  97. function set_limit($limit) {
  98. $this->limit = $limit;
  99. }
  100. /**
  101. * Set an OFFSET on the query, specifying a number of results to skip
  102. */
  103. function set_offset($offset) {
  104. $this->offset = $offset;
  105. }
  106. /**
  107. * Render the pager, if necessary.
  108. */
  109. function render_pager($exposed_input) {
  110. if (!empty($this->pager) && $this->pager->use_pager()) {
  111. return $this->pager->render($exposed_input);
  112. }
  113. return '';
  114. }
  115. /**
  116. * Create a new grouping for the WHERE or HAVING clause.
  117. *
  118. * @param $type
  119. * Either 'AND' or 'OR'. All items within this group will be added
  120. * to the WHERE clause with this logical operator.
  121. * @param $group
  122. * An ID to use for this group. If unspecified, an ID will be generated.
  123. * @param $where
  124. * 'where' or 'having'.
  125. *
  126. * @return $group
  127. * The group ID generated.
  128. */
  129. function set_where_group($type = 'AND', $group = NULL, $where = 'where') {
  130. // Set an alias.
  131. $groups = &$this->$where;
  132. if (!isset($group)) {
  133. $group = empty($groups) ? 1 : max(array_keys($groups)) + 1;
  134. }
  135. // Create an empty group
  136. if (empty($groups[$group])) {
  137. $groups[$group] = array('conditions' => array(), 'args' => array());
  138. }
  139. $groups[$group]['type'] = strtoupper($type);
  140. return $group;
  141. }
  142. /**
  143. * Control how all WHERE and HAVING groups are put together.
  144. *
  145. * @param $type
  146. * Either 'AND' or 'OR'
  147. */
  148. function set_group_operator($type = 'AND') {
  149. $this->group_operator = strtoupper($type);
  150. }
  151. /**
  152. * Returns the according entity objects for the given query results.
  153. */
  154. function get_result_entities($results, $relationship = NULL) {
  155. return FALSE;
  156. }
  157. }
  158. /**
  159. * @}
  160. */