views_plugin_query.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * @return SelectQuery
  39. * A SelectQuery object.
  40. */
  41. function query($get_count = FALSE) { }
  42. /**
  43. * Let modules modify the query just prior to finalizing it.
  44. *
  45. * @param view $view
  46. * The view which is executed.
  47. */
  48. function alter(&$view) { }
  49. /**
  50. * Builds the necessary info to execute the query.
  51. *
  52. * @param view $view
  53. * The view which is executed.
  54. */
  55. function build(&$view) { }
  56. /**
  57. * Executes the query and fills the associated view object with according
  58. * values.
  59. *
  60. * Values to set: $view->result, $view->total_rows, $view->execute_time,
  61. * $view->pager['current_page'].
  62. *
  63. * $view->result should contain an array of objects. The array must use a
  64. * numeric index starting at 0.
  65. *
  66. * @param view $view
  67. * The view which is executed.
  68. */
  69. function execute(&$view) { }
  70. /**
  71. * Add a signature to the query, if such a thing is feasible.
  72. *
  73. * This signature is something that can be used when perusing query logs to
  74. * discern where particular queries might be coming from.
  75. *
  76. * @param view $view
  77. * The view which is executed.
  78. */
  79. function add_signature(&$view) { }
  80. /**
  81. * Get aggregation info for group by queries.
  82. *
  83. * If NULL, aggregation is not allowed.
  84. */
  85. function get_aggregation_info() { }
  86. /**
  87. * Add settings for the ui.
  88. */
  89. function options_form(&$form, &$form_state) {
  90. parent::options_form($form, $form_state);
  91. }
  92. function options_validate(&$form, &$form_state) { }
  93. function options_submit(&$form, &$form_state) { }
  94. function summary_title() {
  95. return t('Settings');
  96. }
  97. /**
  98. * Set a LIMIT on the query, specifying a maximum number of results.
  99. */
  100. function set_limit($limit) {
  101. $this->limit = $limit;
  102. }
  103. /**
  104. * Set an OFFSET on the query, specifying a number of results to skip
  105. */
  106. function set_offset($offset) {
  107. $this->offset = $offset;
  108. }
  109. /**
  110. * Render the pager, if necessary.
  111. */
  112. function render_pager($exposed_input) {
  113. if (!empty($this->pager) && $this->pager->use_pager()) {
  114. return $this->pager->render($exposed_input);
  115. }
  116. return '';
  117. }
  118. /**
  119. * Create a new grouping for the WHERE or HAVING clause.
  120. *
  121. * @param $type
  122. * Either 'AND' or 'OR'. All items within this group will be added
  123. * to the WHERE clause with this logical operator.
  124. * @param $group
  125. * An ID to use for this group. If unspecified, an ID will be generated.
  126. * @param $where
  127. * 'where' or 'having'.
  128. *
  129. * @return $group
  130. * The group ID generated.
  131. */
  132. function set_where_group($type = 'AND', $group = NULL, $where = 'where') {
  133. // Set an alias.
  134. $groups = &$this->$where;
  135. if (!isset($group)) {
  136. $group = empty($groups) ? 1 : max(array_keys($groups)) + 1;
  137. }
  138. // Create an empty group
  139. if (empty($groups[$group])) {
  140. $groups[$group] = array('conditions' => array(), 'args' => array());
  141. }
  142. $groups[$group]['type'] = strtoupper($type);
  143. return $group;
  144. }
  145. /**
  146. * Control how all WHERE and HAVING groups are put together.
  147. *
  148. * @param $type
  149. * Either 'AND' or 'OR'
  150. */
  151. function set_group_operator($type = 'AND') {
  152. $this->group_operator = strtoupper($type);
  153. }
  154. /**
  155. * Returns the according entity objects for the given query results.
  156. */
  157. function get_result_entities($results, $relationship = NULL) {
  158. return FALSE;
  159. }
  160. }
  161. /**
  162. * @}
  163. */