ChadoDatabaseConnection.inc

File

tripal_chado/includes/ChadoDatabaseConnection.inc
View source
  1. <?php
  2. /**
  3. * Overrides the DatabaseConnection_pgsql.
  4. *
  5. * The primary purpose of this class is to allow for prefixing of Chado tables.
  6. * By default the only way to support this is to add an array to the 'prefix'
  7. * key of the settings.php file. But this is problematic. For example, what
  8. * if there is a contact table in the Drupal database as well as one in Chado.
  9. * The default prefix replacement would always rewrite it to be the one in
  10. * Chado. This class is intended to be used when the Chado tables
  11. * are needed.
  12. *
  13. */
  14. class ChadoDatabaseConnection extends DatabaseConnection_pgsql {
  15. /**
  16. * A replacement constructor for DatabaseConnection_pgsql::__construct.
  17. *
  18. * The primary purpose for overiding the constructor is to dynamically add
  19. * a set of prefixes for replacing. This will allow Chado tables to be
  20. * prefixed with the 'chado.' schema prefix. The alternative to overridding
  21. * the DatabaseConnection_pgsql is to ask the end-user to add a prefix
  22. * entry for every Chado table and custom table they create. That's not
  23. * very manageable.
  24. */
  25. function __construct(array $connection_options = array()) {
  26. parent::__construct($connection_options);
  27. // Get the list of prefix search and replace that are set in the
  28. // settings.php file. We'll need those later.
  29. $psearch = $this->prefixSearch;
  30. $preplace = $this->prefixReplace;
  31. // Reset the prefix serach and replace
  32. $this->prefixSearch = array();
  33. $this->prefixReplace = array();
  34. $tables = chado_get_table_names(TRUE);
  35. foreach ($tables as $table) {
  36. $this->prefixSearch[] = '{' . $table . '}';
  37. $this->prefixReplace[] = 'chado.' . $table;
  38. }
  39. $this->prefixSearch = array_merge($this->prefixSearch, $psearch);
  40. $this->prefixReplace = array_merge($this->prefixReplace, $preplace);
  41. }
  42. public function prefixTables($sql) {
  43. $sql = str_replace($this->prefixSearch, $this->prefixReplace, $sql);
  44. return $sql;
  45. }
  46. }