123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- class views_handler_join_chado_aggregator extends views_join {
-
-
-
- function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT', $added = NULL) {
- parent::construct($table, $left_table, $left_field, $field, $extra, $type);
-
- $postgresql_version = pg_version();
- $this->postgresql_version = $postgresql_version['client'];
-
-
- if (intval($postgresql_version['client']) >= 9) {
- $this->postgresql_9up = TRUE;
- }
- }
-
- function join($table, &$query) {
- $output = array();
-
-
-
-
- if (!$this->definition['pre-aggregated']) {
-
- $order_by = array();
- if (!is_array($this->sort)) {
- $this->sort = array();
- }
- foreach ($this->sort as $s) {
- $order_by[] = $s['table'] . '.' . $s['field'] . ' ' . $s['order'];
- }
-
- $table_desc = module_invoke_all('chado_' . $this->definition['table'] . '_schema');
- $select_fields[ $this->definition['table'] ] = $table_desc['fields'];
- if (!empty($table_desc)) {
-
-
- $joins = array();
- foreach ($table_desc['foreign keys'] as $defn) {
- if ($defn['table'] != $this->left_table) {
- foreach ( $defn['columns'] as $left => $right) {
- $left = $this->definition['table'] . '.' . $left;
- $right = $defn['table'] . '.' . $right;
- $joins[] = "LEFT JOIN $defn[table] $defn[table] ON $left=$right";
- }
-
- $join_table = module_invoke_all('chado_' . $defn['table'] . '_schema');
- $select_fields[ $defn['table'] ] = $join_table['fields'];
- }
- }
-
- foreach ($select_fields as $table => $table_fields) {
- foreach ($table_fields as $fname => $f) {
- $alias = '';
- if ($table != $this->definition['table']) {
- $alias = $table . '_';
- }
- if ($fname != $this->definition['field']) {
-
- if ($this->postgresql_9up && !empty($order_by)) {
- $fields[] = 'array_agg(' . $table . '.' . $fname . ' ORDER BY ' . implode(',', $order_by) . ') as ' . $alias . $fname;
- }
- else {
- $fields[] = 'array_agg(' . $table . '.' . $fname . ') as '. $alias . $fname;
- }
- $composite_field_parts[] = "'" . $alias . $fname . "::' ||" . $table . '.' . $fname;
- }
- else {
- $fields[] = $fname;
- $composite_field_parts[] = "'" . $alias . $fname . "::' ||" . $table . '.' . $fname;
- }
- }
- }
-
-
- }
- else {
-
- $joins = array();
-
- $sql = "SELECT
- attname as column,
- format_type(atttypid, atttypmod) as datatype
- FROM pg_attribute, pg_type
- WHERE typname='nd_genotype_experiment'
- AND attrelid=typrelid
- AND attname NOT IN ('cmin','cmax','ctid','oid','tableoid','xmin','xmax')";
- $previous_db = tripal_db_set_active('chado');
- $resource = db_query($sql);
- tripal_db_set_active($previous_db);
- while ($r = db_fetch_object($resource)) {
- $table = $this->definition['table'];
- $alias = '';
- $fname = $r->column;
- if ($fname != $this->definition['field']) {
-
- if ($this->postgresql_9up && !empty($order_by)) {
- $fields[] = 'array_agg(' . $table . '.' . $fname . ' ORDER BY ' . implode(',', $order_by) . ') as ' . $alias . $fname;
- }
- else {
- $fields[] = 'array_agg(' . $table . '.' . $fname . ') as ' . $alias . $fname;
- }
- $composite_field_parts[] = "'" . $alias . $fname . "::' ||" . $table . '.' . $fname;
- }
- else {
- $fields[] = $fname;
- $composite_field_parts[] = "'" . $alias . $fname . "::' ||" . $table . '.' . $fname;
- }
- }
- }
-
-
-
- if ($this->postgresql_9up && !empty($order_by)) {
- $composite_field = "array_agg('{'||" . implode(" || ',' || ", $composite_field_parts) . "||'}' ORDER BY " . implode(',', $order_by) . ") as all";
- }
- else {
- $composite_field = "array_agg('{'||" . implode(" || ',' || ", $composite_field_parts) . "||'}') as all";
- }
- $fields[] = $composite_field;
-
- $sql = 'SELECT ' . implode(', ', $fields)
- .' FROM ' . $this->definition['table']
- .' ' . implode(' ', $joins);
- if (!empty($this->filter)) {
- $sql .= ' WHERE ' . implode(', ', $this->filter);
- }
- $sql .= ' GROUP BY ' . $this->definition['field'];
-
- $output[] = $this->create_single_join(
- $query,
- array(
- 'table' => $this->definition['table'],
- 'field' => $this->definition['field'],
- 'table_sql' => $sql,
- 'is_drupal' => FALSE,
- ),
- array(
- 'table' => $this->definition['left_table'],
- 'field' => $this->definition['left_field'],
- ),
- 'LEFT'
- );
-
-
- }
- else {
-
- $current_table_spec = array(
- 'table' => $this->definition['table'],
- 'field' => $this->definition['field'],
- 'is_drupal' => FALSE,
- );
- $left_table_spec = array(
- 'table' => $this->definition['left_table'],
- 'field' => $this->definition['left_field'],
- );
- switch ($this->definition['table_aggregated']) {
- default:
- case 'CURRENT':
- $current_table_spec['pre-aggregated'] = TRUE;
- break;
- case 'LEFT':
- $left_table_spec['pre-aggregated'] = TRUE;
- break;
- }
- $output[] = $this->create_single_join(
- $query,
- $current_table_spec,
- $left_table_spec,
- 'LEFT'
- );
- }
- return implode("\n", $output);
- }
-
- function create_single_join(&$query, $right_spec, $left_spec, $join_type) {
- if ($right_spec['table']) {
- $right = $query->get_table_info($right_spec['table']);
- if (!$right['alias']) {
- $right['alias'] = $right_spec['table'];
- }
- $right_field = "$right[alias].$right_spec[field]";
-
- if ($right_spec['pre-aggregated']) {
- $right_field = "any(" . $right_field . ")";
- }
-
- if ($right_spec['is_drupal']) {
- $right_table = '{' . $right_spec['table'] . '}';
- }
- else {
- $right_table = $right_spec['table'];
- }
- }
- if ($left_spec['table']) {
- $left = $query->get_table_info($left_spec['table']);
- if (!$left['alias']) {
- $left['alias'] = $left_spec['table'];
- }
- $left_field = "$left[alias].$left_spec[field]";
- }
- else {
-
- $left_field = $this->left_spec['field'];
- }
-
- if ($left_spec['pre-aggregated']) {
- $left_field = "any(" . $left_field . ")";
- }
-
- if (!empty($right_spec['table_sql'])) {
- $output = " $join_type JOIN ($right_spec[table_sql]) $right[alias] ON $left_field = $right_field";
- }
- else {
- $output = " $join_type JOIN $right_spec[table] $right[alias] ON $left_field = $right_field";
- }
- return $output;
- }
- }
|