Преглед на файлове

factor out table prefix replacement. create SelectQueryExtender

bradfordcondon преди 6 години
родител
ревизия
896c0d8a39

+ 2 - 1
tests/tripal_chado/api/ChadoQueryTest.php

@@ -79,11 +79,12 @@ class ChadoQueryTest extends TripalTestCase {
 
     $id = $analysis_record->analysis_id;
 
-    $query = chado_db_select('analysis', 't');
+    $query = chado_db_select('{analysis}', 't');
     $analysis = $query->fields('t')
       ->condition('analysis_id', $id)
       ->execute()
       ->fetchObject();
+
     $querytwo = db_select('chado.analysis', 't');
 
     $traditional_analysis = $querytwo

Файловите разлики са ограничени, защото са твърде много
+ 229 - 216
tripal_chado/api/tripal_chado.query.api.inc


+ 0 - 53
tripal_chado/includes/ChadoDatabaseConnection.inc

@@ -1,53 +0,0 @@
-<?php
-
-/**
- * Overrides the DatabaseConnection_pgsql.
- *
- * The primary purpose of this class is to allow for prefixing of Chado tables.
- * By default the only way to support this is to add an array to the 'prefix'
- * key of the settings.php file.  But this is problematic. For example, what
- * if there is a contact table in the Drupal database as well as one in Chado.
- * 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 SelectQueryExtender {
-  /**
-   * A replacement constructor for DatabaseConnection_pgsql::__construct.
-   *
-   * The primary purpose for overiding the constructor is to dynamically add
-   * a set of prefixes for replacing. This will allow Chado tables to be
-   * prefixed with the 'chado.' schema prefix.  The alternative to overridding
-   * the DatabaseConnection_pgsql is to ask the end-user to add a prefix
-   * entry for every Chado table and custom table they create.  That's not
-   * very manageable.
-   */
-  public function __construct() {
-
-  }
-
-  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;
-  }
-}

+ 34 - 0
tripal_chado/includes/ChadoPrefixExtender.inc

@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * SelectQueryExtender to use chado_replace_table_prefix().
+ *
+ *   Note that extenders can only be called on an existing db_select().
+ * Therefore the table name in the initial select must be prefixed prior to
+ * initialization. Read more about extenders:
+ * https://www.drupal.org/docs/7/api/database-api/dynamic-queries/extenders.
+ */
+class ChadoPrefixExtender extends SelectQueryExtender {
+
+  /**
+   * Overwrites the join to prefix table names.
+   *
+   * @param string $table
+   *   Table to join.
+   * @param null $alias
+   *   Alias for joined table.
+   * @param null $condition
+   *   Operation for joining.
+   * @param array $arguments
+   *   Additional arguments.
+   *
+   * @return $this
+   */
+  public function join($table, $alias = NULL, $condition = NULL, $arguments = []) {
+
+    $table = chado_replace_table_prefix($table);
+    $this->query->join($table, $alias, $condition, $arguments);
+    return $this;
+  }
+
+}

+ 1 - 1
tripal_chado/tripal_chado.module

@@ -68,7 +68,7 @@ require_once "includes/TripalFields/ChadoField.inc";
 require_once "includes/TripalFields/ChadoFieldWidget.inc";
 require_once "includes/TripalFields/ChadoFieldFormatter.inc";
 
-require_once "includes/ChadoDatabaseConnection.inc";
+require_once "includes/ChadoPrefixExtender.inc";
 
 tripal_chado_set_globals();
 

Някои файлове не бяха показани, защото твърде много файлове са промени