views_handler_join_chado_aggregator.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. module_load_include('inc', 'views', 'includes/base');
  3. module_load_include('inc', 'views', 'includes/handlers');
  4. /**
  5. * @file
  6. * Handler to allow joins between records via a linking table
  7. *
  8. * Example Usage:
  9. * To join the analysisprop table to the analysis table,
  10. * Use the following code in the analysisprop hook_views_data:
  11. * @code
  12. $data['analysisprop']['table']['join']['analysis'] = array(
  13. 'left_field' => 'analysis_id',
  14. 'field' => 'analysis_id',
  15. 'handler' => 'views_handler_join_chado_aggregator',
  16. 'pre-aggregated' => TRUE | FALSE //whether the table is already aggregated (contains arrays)
  17. 'table_aggregated' => CURRENT | LEFT //the table which has many records for each record in the other
  18. );
  19. * @endcode
  20. */
  21. class views_handler_join_chado_aggregator extends views_join {
  22. // PHP 4 doesn't call constructors of the base class automatically from a
  23. // constructor of a derived class. It is your responsibility to propagate
  24. // the call to constructors upstream where appropriate.
  25. function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT', $added = NULL) {
  26. parent::construct($table, $left_table, $left_field, $field, $extra, $type);
  27. // Determine the postgresql version
  28. $postgresql_version = pg_version();
  29. $this->postgresql_version = $postgresql_version['client'];
  30. // If version is 9.0+ then indicate
  31. // Needed to apply sorting for aggregated fields
  32. if (intval($postgresql_version['client']) >= 9) {
  33. $this->postgresql_9up = TRUE;
  34. }
  35. }
  36. /**
  37. * Creates SQL including aggregation query used in join
  38. */
  39. function join($table, &$query) {
  40. $opt = array(
  41. 'table' => $this->definition['table'],
  42. 'field' => $this->definition['field'],
  43. 'left_table' => $this->definition['left_table'],
  44. 'left_field' => $this->definition['left_field'],
  45. 'table_aggregated' => $this->definition['table_aggregated'],
  46. 'sort' => $this->sort,
  47. 'filter' => $this->filter,
  48. 'additional_joins' => $this->additional_joins,
  49. 'postgresql_9up' => $this->postgresql_9up,
  50. );
  51. $output = $this->aggregate_join($query, $opt);
  52. return implode("\n", $output);
  53. }
  54. function aggregate_join(&$query, $opt) {
  55. // Create the table SQL (used in join) -------
  56. // query creating one-to-one table using array_agg
  57. // Only aggregate each field if it the join table hadn't been pre-aggregated
  58. // Example where it might be pre-aggregated: Materialized view
  59. if (!$this->definition['pre-aggregated']) {
  60. $sql = $this->get_aggregate_sql_for_table_field($opt);
  61. // Create the join (full SQL) ----------------
  62. $output[] = $this->create_single_join(
  63. $query,
  64. array(
  65. 'table' => $opt['table'],
  66. 'field' => $opt['field'],
  67. 'table_sql' => $sql,
  68. 'is_drupal' => FALSE,
  69. ),
  70. array(
  71. 'table' => $opt['left_table'],
  72. 'field' => $opt['left_field'],
  73. ),
  74. 'LEFT'
  75. );
  76. // Otherwise the table has been pre-aggregated
  77. // Then only need to do a regular join with any in where
  78. }
  79. else {
  80. // Create the join
  81. $current_table_spec = array(
  82. 'table' => $opt['table'],
  83. 'field' => $opt['field'],
  84. 'is_drupal' => FALSE,
  85. );
  86. $left_table_spec = array(
  87. 'table' => $opt['left_table'],
  88. 'field' => $opt['left_field'],
  89. );
  90. switch ($opt['table_aggregated']) {
  91. default:
  92. case 'CURRENT':
  93. $current_table_spec['pre-aggregated'] = TRUE;
  94. break;
  95. case 'LEFT':
  96. $left_table_spec['pre-aggregated'] = TRUE;
  97. break;
  98. }
  99. $output[] = $this->create_single_join(
  100. $query,
  101. $current_table_spec,
  102. $left_table_spec,
  103. 'LEFT'
  104. );
  105. }
  106. return $output;
  107. }
  108. /**
  109. * Create the SQL needed to aggregate a table
  110. */
  111. function get_aggregate_sql_for_table_field($opt) {
  112. // Determine Order BY's for aggregates
  113. $order_by = array();
  114. if (!is_array($opt['sort'])) {
  115. $opt['sort'] = array();
  116. }
  117. foreach ($opt['sort'] as $s) {
  118. $order_by[] = $s['table'] . '.' . $s['field'] . ' ' . $s['order'];
  119. }
  120. // get table description (if defined via schema api)
  121. $table_desc = tripal_core_get_chado_table_schema($opt['table']);
  122. $select_fields[ $opt['table'] ] = $table_desc['fields'];
  123. $joins = array();
  124. if (!empty($table_desc)) {
  125. // Add joins to tables with a foreign key in this table
  126. // (ie: add join to cvterm if this table has a type_id
  127. foreach ($table_desc['foreign keys'] as $defn) {
  128. if ($defn['table'] != $opt['left_table']) {
  129. foreach ( $defn['columns'] as $left => $right) {
  130. $left = $opt['table'] . '.' . $left;
  131. $right = $defn['table'] . '.' . $right;
  132. $joins[] = "LEFT JOIN $defn[table] $defn[table] ON $left=$right";
  133. }
  134. // Fields to be selected from joined table
  135. $join_table = tripal_core_get_chado_table_schema($defn['table']);
  136. $select_fields[ $defn['table'] ] = $join_table['fields'];
  137. }
  138. }
  139. // Fields to be selected
  140. foreach ($select_fields as $table => $table_fields) {
  141. foreach ($table_fields as $fname => $f) {
  142. $alias = '';
  143. if ($table != $opt['table']) {
  144. $alias = $table . '_';
  145. }
  146. if ($fname != $opt['field']) {
  147. // Add sort to aggregate field if postgreSQL 9.0+
  148. if ($opt['postgresql_9up'] && !empty($order_by)) {
  149. $fields[] = 'array_agg(' . $table . '.' . $fname . ' ORDER BY ' . implode(',', $order_by) . ') as ' . $alias . $fname;
  150. }
  151. else {
  152. $fields[] = 'array_agg(' . $table . '.' . $fname . ') as '. $alias . $fname;
  153. }
  154. $composite_field_parts[] = "'" . $alias . $fname . "::' || COALESCE(CAST(" . $table . '.' . $fname . " as text), '')";
  155. }
  156. else {
  157. $fields[] = $fname;
  158. $composite_field_parts[] = "'" . $alias . $fname . "::' || COALESCE(CAST(" . $table . '.' . $fname . " as text), '')";
  159. }
  160. }
  161. }
  162. // There is no definition in schema api
  163. // then use postgresql select
  164. }
  165. else {
  166. // No known foreign key relationships
  167. // so don't add to $joins
  168. // Fields to be selected
  169. $sql = "SELECT
  170. attname as column,
  171. format_type(atttypid, atttypmod) as datatype
  172. FROM pg_attribute, pg_type
  173. WHERE typname='nd_genotype_experiment'
  174. AND attrelid=typrelid
  175. AND attname NOT IN ('cmin','cmax','ctid','oid','tableoid','xmin','xmax')";
  176. $resource = chado_query($sql);
  177. while ($r = db_fetch_object($resource)) {
  178. $table = $opt['table'];
  179. $alias = ''; //no alias needed if table is current table (only option if no schema api definition)
  180. $fname = $r->column;
  181. if ($fname != $opt['field']) {
  182. // Add sort to aggregate field if postgreSQL 9.0+
  183. if ($opt['postgresql_9up'] && !empty($order_by)) {
  184. $fields[] = 'array_agg(' . $table . '.' . $fname . ' ORDER BY ' . implode(',', $order_by) . ') as ' . $alias . $fname;
  185. }
  186. else {
  187. $fields[] = 'array_agg(' . $table . '.' . $fname . ') as ' . $alias . $fname;
  188. }
  189. $composite_field_parts[] = "'" . $alias . $fname . "::' || COALESCE(CAST(" . $table . '.' . $fname . " as text), '')";
  190. }
  191. else {
  192. $fields[] = $fname;
  193. $composite_field_parts[] = "'" . $alias . $fname . "::' || COALESCE(CAST(" . $table . '.' . $fname . " as text), '')";
  194. }
  195. }
  196. }
  197. // Add in any additional joins specified by handlers
  198. if (!empty($opt['additional_joins'])) {
  199. foreach ($opt['additional_joins'] as $join_defn) {
  200. // Add the join SQL in
  201. $table = ($join_defn['table_alias']) ? $join_defn['table_alias'] : $join_defn['table'];
  202. $left = $join_defn['left_table'] . '.' . $join_defn['left_field'];
  203. $right = $table . '.' . $join_defn['field'];
  204. $joins[] = "LEFT JOIN $join_defn[table] $table ON $left=$right";
  205. // Add to the fields and composite field
  206. $join_table_desc = tripal_core_get_chado_table_schema($join_defn['table']);
  207. $alias = $table . '_';
  208. foreach ($join_table_desc['fields'] as $fname => $fdesc) {
  209. // Add sort to aggregate field if postgreSQL 9.0+
  210. if ($opt['postgresql_9up'] && !empty($order_by)) {
  211. $fields[] = 'array_agg(' . $table . '.' . $fname . ' ORDER BY ' . implode(',', $order_by) . ') as ' . $alias . $fname;
  212. }
  213. else {
  214. $fields[] = 'array_agg(' . $table . '.' . $fname . ') as ' . $alias . $fname;
  215. }
  216. $composite_field_parts[] = "'" . $alias . $fname . "::' || COALESCE(CAST(" . $table . '.' . $fname . " as text), '')";
  217. }
  218. }
  219. }
  220. // composite field
  221. // (combines all other fields before aggregating)
  222. // Add sort to aggregate field if postgreSQL 9.0+
  223. if ($opt['postgresql_9up'] && !empty($order_by)) {
  224. $composite_field = "array_agg('{'||" . implode(" || ',' || ", $composite_field_parts) . "||'}' ORDER BY " . implode(',', $order_by) . ") as all";
  225. }
  226. else {
  227. $composite_field = "array_agg('{'||" . implode(" || ',' || ", $composite_field_parts) . "||'}') as all";
  228. }
  229. $fields[] = $composite_field;
  230. // SQL to use in the join
  231. $sql = 'SELECT ' . implode(', ', $fields)
  232. .' FROM ' . $opt['table']
  233. .' ' . implode(' ', $joins);
  234. if (!empty($opt['filter'])) {
  235. $sql .= ' WHERE ' . implode(' AND ', $opt['filter']);
  236. }
  237. $sql .= ' GROUP BY ' . $opt['field'];
  238. return $sql;
  239. }
  240. /**
  241. * Creates SQL for a single join based on parameters
  242. * Join will be: <type> JOIN (<query creating one-to-one table using array_agg>) <table alias>
  243. * ON <qualified left field>=<qualified right field>
  244. */
  245. function create_single_join(&$query, $right_spec, $left_spec, $join_type) {
  246. if ($right_spec['table']) {
  247. $right = $query->get_table_info($right_spec['table']);
  248. if (!$right['alias']) {
  249. $right['alias'] = $right_spec['table'];
  250. }
  251. $right_field = "$right[alias].$right_spec[field]";
  252. // Add any() around field if already aggregated
  253. if ($right_spec['pre-aggregated']) {
  254. $right_field = "any(" . $right_field . ")";
  255. }
  256. // Add drupal { } around table
  257. if ($right_spec['is_drupal']) {
  258. $right_table = '{' . $right_spec['table'] . '}';
  259. }
  260. else {
  261. $right_table = $right_spec['table'];
  262. }
  263. }
  264. if ($left_spec['table']) {
  265. $left = $query->get_table_info($left_spec['table']);
  266. if (!$left['alias']) {
  267. $left['alias'] = $left_spec['table'];
  268. }
  269. $left_field = "$left[alias].$left_spec[field]";
  270. }
  271. else {
  272. // This can be used if left_field is a formula or something. It should be used only *very* rarely.
  273. $left_field = $this->left_spec['field'];
  274. }
  275. // Add any() around field if already aggregated
  276. if ($left_spec['pre-aggregated']) {
  277. $left_field = "any(" . $left_field . ")";
  278. }
  279. // Concatenate parts together to form join sql
  280. if (!empty($right_spec['table_sql'])) {
  281. $output = " $join_type JOIN ($right_spec[table_sql]) $right[alias] ON $left_field = $right_field";
  282. }
  283. else {
  284. $output = " $join_type JOIN $right_spec[table] $right[alias] ON $left_field = $right_field";
  285. }
  286. return $output;
  287. }
  288. }