Browse Source

Added query filter for the relationship field

Stephen Ficklin 8 years ago
parent
commit
b8bc4ab2ee

+ 3 - 1
tripal_chado/includes/ChadoDatabaseConnection.inc

@@ -47,7 +47,9 @@ class ChadoDatabaseConnection extends DatabaseConnection_pgsql {
   }
 
   public function prefixTables($sql) {
-    return str_replace($this->prefixSearch, $this->prefixReplace, $sql);
+    $sql = str_replace($this->prefixSearch, $this->prefixReplace, $sql);
+    print_r($sql);
+    return $sql;
   }
 
 

+ 11 - 4
tripal_chado/includes/TripalFields/ChadoField.inc

@@ -53,10 +53,17 @@ class ChadoField extends TripalField {
    * Entities can be filtered using the fields.  This function should be
    * implemented if the field  supports filtering.  To provide filtering,
    * the $query object should be updated to including any joins and
-   * conditions necessary. When giving alias to joined tables be sure to
-   * use aliases that are unique to avoid conflicts with other fields.  When
-   * joining with the base table its alias is 'base'.  This function should
-   * never set the fields that should be returned nor ordering or group by.
+   * conditions necessary. The following rules should be followed when
+   * implementing this function:
+   * - Try to implement a filter for every element of the 'value' array
+   *   returned by the load() function.
+   * - However, avoid making filteres for non-indexed database columns.
+   * - When giving alias to joined tables be sure to use aliases that are
+   *   unique to avoid conflicts with other fields.
+   * - When joining with the base table its alias is 'base'.
+   * - This function should never set the fields that should be returned
+   *   nor ordering or group by.
+   * - You may join to materialized views if need be to help speed queries.
    *
    * @param $condition
    *   The field specific condition as set in the TripalFieldQuery object.

+ 56 - 0
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc

@@ -299,7 +299,63 @@ class sbo__relationship extends ChadoField {
    * @see ChadoField::query()
    */
   public function query($condition, &$query) {
+    $alias = $this->field['field_name'];
+    $chado_table = $this->instance['settings']['chado_table'];
+    $base_table = $this->instance['settings']['base_table'];
+    $bschema = chado_get_schema($base_table);
+    $bpkey = $bschema['primary key'][0];
+
+    // Filter by the name of the subject or object.
+    if ($condition['column'] == 'relationship.clause_subject.schema:name' or
+        $condition['column'] == 'relationship.clause_subject.name') {
+      $query->join($chado_table, $alias, "base.$bpkey = $alias.object_id");
+      $query->join($base_table, 'base2', "base2.$bpkey = $alias.subject_id");
+      $query->condition("base2.name", $condition['value']);
+    }
+    if ($condition['column'] == 'relationship.clause_predicate.schema:name' or
+        $condition['column'] == 'relationship.clause_predicate.name') {
+      $query->join($chado_table, $alias, "base.$bpkey = $alias.subject_id");
+      $query->join($base_table, 'base2', "base2.$bpkey = $alias.object_id");
+      $query->condition("base2.name", $condition['value']);
+    }
+
+    // Filter by unique name of the subject or object.
+    if ($condition['column'] == 'relationship.clause_subject.data:0842' or
+        $condition['column'] == 'relationship.clause_subject.identifier') {
+      $query->join($chado_table, $alias, "base.$bpkey = $alias.object_id");
+      $query->join($base_table, 'base2', "base2.$bpkey = $alias.subject_id");
+      $query->condition("base2.uniquename", $condition['value']);
+    }
+    if ($condition['column'] == 'relationship.clause_predicate.data:0842' or
+        $condition['column'] == 'relationship.clause_predicate.identifier') {
+      $query->join($chado_table, $alias, "base.$bpkey = $alias.subject_id");
+      $query->join($base_table, 'base2', "base2.$bpkey = $alias.object_id");
+      $query->condition("base2.uniquename", $condition['value']);
+    }
 
+    // Filter by the type of the subject or object
+    if ($condition['column'] == 'relationship.clause_subject.rdfs:type' or
+        $condition['column'] == 'relationship.clause_subject.type') {
+      $query->join($chado_table, $alias, "base.$bpkey = $alias.object_id");
+      $query->join($base_table, 'base2', "base2.$bpkey = $alias.subject_id");
+      $query->join('cvterm', 'SubjectCVT', "SubjectCVT.cvterm_id = base2.type_id");
+      $query->condition("SubjectCVT.name", $condition['value']);
+    }
+    if ($condition['column'] == 'relationship.clause_predicate.rdfs:type' or
+        $condition['column'] == 'relationship.clause_predicate.type') {
+      $query->join($chado_table, $alias, "base.$bpkey = $alias.subject_id");
+      $query->join($base_table, 'base2', "base2.$bpkey = $alias.object_id");
+      $query->join('cvterm', 'ObjectCVT', "ObjectCVT.cvterm_id = base2.type_id");
+      $query->condition("ObjectCVT.name", $condition['value']);
+    }
+
+    // Filter by relationship type
+    if ($condition['column'] == 'relationship.relationship_type') {
+      // This filter commented out because it's way to slow...
+//       $query->join($chado_table, $alias, "base.$bpkey = $alias.subject_id OR base.$bpkey = $alias.object_id");
+//       $query->join('cvterm', 'RelTypeCVT', "RelTypeCVT.cvterm_id = $alias.type_id");
+//       $query->condition("RelTypeCVT.name", $condition['value']);
+    }
   }
 
   /**