Browse Source

Begin extender implemntation

Abdullah Almsaeed 6 years ago
parent
commit
d8ae22d151
1 changed files with 25 additions and 27 deletions
  1. 25 27
      tripal_chado/includes/ChadoDatabaseConnection.inc

+ 25 - 27
tripal_chado/includes/ChadoDatabaseConnection.inc

@@ -10,10 +10,8 @@
  * The default prefix replacement would always rewrite it to be the one in
  * Chado.  This class is intended to be used when the Chado tables
  * are needed.
- *
  */
-class ChadoDatabaseConnection extends DatabaseConnection_pgsql {
-
+class ChadoDatabaseConnection extends SelectQueryExtender {
   /**
    * A replacement constructor for DatabaseConnection_pgsql::__construct.
    *
@@ -24,32 +22,32 @@ class ChadoDatabaseConnection extends DatabaseConnection_pgsql {
    * entry for every Chado table and custom table they create.  That's not
    * very manageable.
    */
-  function __construct(array $connection_options = array()) {
-    parent::__construct($connection_options);
-
-
-    // Get the list of prefix search and replace that are set in the
-    // settings.php file. We'll need those later.
-    $psearch = $this->prefixSearch;
-    $preplace = $this->prefixReplace;
-
-    // Reset the prefix serach and replace
-    $this->prefixSearch = array();
-    $this->prefixReplace = array();
-
-    $tables = chado_get_table_names(TRUE);
-    foreach ($tables as $table) {
-      $this->prefixSearch[] = '{' . $table . '}';
-      $this->prefixReplace[] = 'chado.' . $table;
-    }
-    $this->prefixSearch = array_merge($this->prefixSearch, $psearch);
-    $this->prefixReplace = array_merge($this->prefixReplace, $preplace);
+  public function __construct() {
+
   }
 
-  public function prefixTables($sql) {
-    $sql = str_replace($this->prefixSearch, $this->prefixReplace, $sql);
-    return $sql;
+  public function select($table, $alias = NULL, array $options = []) {
+    $table = $this->prefixTables($table);
+
+    $this->query->select($table, $alias, $options);
+
+    return $this;
   }
 
+  public function join($table, $alias = NULL, $condition = NULL, $arguments = []) {
+    $table = $this->prefixTables($table);
 
-}
+    $this->query->join($table, $alias, $condition, $arguments);
+
+    return $this;
+  }
+
+  public function prefixTables($sql) {
+    $chado_schema_name = chado_get_schema_name('chado');
+    $drupal_schema_name = chado_get_schema_name('drupal');
+    $sql = preg_replace('/\{(.*?)\}/', $chado_schema_name.'.$1', $sql);
+    $sql = preg_replace('/\[(\w+)\]/', $drupal_schema_name.'.$1', $sql);
+
+    return $sql;
+  }
+}