views_handler_join_chado_aggregator.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Handler to allow joins between records via a linking table
  4. *
  5. * Example Usage:
  6. * To join the analysisprop table to the analysis table,
  7. * Use the following code in the analysisprop hook_views_data:
  8. * @code
  9. $data['analysisprop']['table']['join']['analysis'] = array(
  10. 'left_field' => 'analysis_id',
  11. 'field' => 'analysis_id',
  12. 'handler' => 'views_handler_join_chado_aggregator'
  13. );
  14. * @endcode
  15. */
  16. class views_handler_join_chado_aggregator extends views_join {
  17. // PHP 4 doesn't call constructors of the base class automatically from a
  18. // constructor of a derived class. It is your responsibility to propagate
  19. // the call to constructors upstream where appropriate.
  20. function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT', $added = NULL) {
  21. parent::construct($table, $left_table, $left_field, $field, $extra, $type);
  22. // Determine the postgresql version
  23. $postgresql_version = pg_version();
  24. $this->postgresql_version = $postgresql_version['client'];
  25. // If version is 9.0+ then indicate
  26. // Needed to apply sorting for aggregated fields
  27. if (intval($postgresql_version['client']) >= 9) {
  28. $this->postgresql_9up = TRUE;
  29. }
  30. }
  31. /**
  32. * Creates SQL including aggregation query used in join
  33. */
  34. function join($table, &$query) {
  35. $output = array();
  36. // Create the table SQL (used in join) -------
  37. // query creating one-to-one table using array_agg
  38. $table_desc = module_invoke_all('chado_'.$this->definition['table'].'_schema');
  39. $select_fields[ $this->definition['table'] ] = $table_desc['fields'];
  40. // Add joins to tables with a foreign key in this table
  41. // (ie: add join to cvterm if this table has a type_id
  42. $joins = array();
  43. foreach($table_desc['foreign keys'] as $defn) {
  44. if ($defn['table'] != $this->left_table) {
  45. foreach( $defn['columns'] as $left => $right) {
  46. $left = $this->definition['table'] .'.'. $left;
  47. $right = $defn[table] .'.'. $right;
  48. $joins[] = "LEFT JOIN $defn[table] $defn[table] ON $left=$right";
  49. }
  50. // Fields to be selected from joined table
  51. $join_table = module_invoke_all('chado_'.$defn['table'].'_schema');
  52. $select_fields[ $defn['table'] ] = $join_table['fields'];
  53. }
  54. }
  55. // Determine Order BY's for aggregates
  56. $order_by = array();
  57. foreach ($this->sort as $s) {
  58. $order_by[] = $s['table'].'.'.$s['field'].' '.$s['order'];
  59. }
  60. // Fields to be selected
  61. foreach ($select_fields as $table => $table_fields) {
  62. foreach ($table_fields as $fname => $f) {
  63. $alias = '';
  64. if ($table != $this->definition['table']) {
  65. $alias = $table .'_';
  66. }
  67. if ($fname != $this->definition['field']) {
  68. // Add sort to aggregate field if postgreSQL 9.0+
  69. if ($this->postgresql_9up && !empty($order_by)) {
  70. $fields[] = 'array_agg('.$table.'.'.$fname.' ORDER BY '.implode(',',$order_by).') as '.$alias.$fname;
  71. } else {
  72. $fields[] = 'array_agg('.$table.'.'.$fname.') as '.$alias.$fname;
  73. }
  74. $composite_field_parts[] = "'".$alias.$fname."::' ||".$table.'.'.$fname;
  75. } else {
  76. $fields[] = $fname;
  77. $composite_field_parts[] = "'".$alias.$fname."::' ||".$table.'.'.$fname;
  78. }
  79. }
  80. }
  81. // composite field
  82. // (combines all other fields before aggregating)
  83. // Add sort to aggregate field if postgreSQL 9.0+
  84. if ($this->postgresql_9up && !empty($order_by)) {
  85. $composite_field = "array_agg('{'||".implode(" || ',' || ",$composite_field_parts)."||'}' ORDER BY ".implode(',',$order_by).") as all";
  86. } else {
  87. $composite_field = "array_agg('{'||".implode(" || ',' || ",$composite_field_parts)."||'}') as all";
  88. }
  89. $fields[] = $composite_field;
  90. // SQL to use in the join
  91. $sql = 'SELECT '.implode(', ',$fields)
  92. .' FROM '.$this->definition['table']
  93. .' '.implode(' ',$joins);
  94. if (!empty($this->filter)) {
  95. $sql .= ' WHERE '.implode(', ', $this->filter);
  96. }
  97. $sql .= ' GROUP BY '.$this->definition['field'];
  98. // Create the join (full SQL) ----------------
  99. $output[] = $this->create_single_join(
  100. $query,
  101. array(
  102. 'table' => $this->definition['table'],
  103. 'field' => $this->definition['field'],
  104. 'table_sql' => $sql,
  105. 'is_drupal' => FALSE,
  106. ),
  107. array(
  108. 'table' => $this->definition['left_table'],
  109. 'field' => $this->definition['left_field'],
  110. ),
  111. 'LEFT'
  112. );
  113. return implode("\n",$output);
  114. }
  115. /**
  116. * Creates SQL for a single join based on parameters
  117. * Join will be: <type> JOIN (<query creating one-to-one table using array_agg>) <table alias>
  118. * ON <qualified left field>=<qualified right field>
  119. */
  120. function create_single_join (&$query, $right_spec, $left_spec, $join_type) {
  121. if ($right_spec['table']) {
  122. $right = $query->get_table_info($right_spec['table']);
  123. if (!$right['alias']) { $right['alias'] = $right_spec['table']; }
  124. $right_field = "$right[alias].$right_spec[field]";
  125. if ($right_spec['is_drupal']) {
  126. $right_table = '{'.$right_spec['table'].'}';
  127. } else {
  128. $right_table = $right_spec['table'];
  129. }
  130. }
  131. if ($left_spec['table']) {
  132. $left = $query->get_table_info($left_spec['table']);
  133. if (!$left['alias']) { $left['alias'] = $left_spec['table']; }
  134. $left_field = "$left[alias].$left_spec[field]";
  135. } else {
  136. // This can be used if left_field is a formula or something. It should be used only *very* rarely.
  137. $left_field = $this->left_spec['field'];
  138. }
  139. $output = " $join_type JOIN ($right_spec[table_sql]) $right[alias] ON $left_field = $right_field";
  140. return $output;
  141. }
  142. }