123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- class ChadoPrefixExtender extends SelectQueryExtender {
-
- protected static $chado_tables = [];
-
- public static function select($table, $alias = NULL, array $options = []) {
-
-
- if (is_string($table)) {
- $table = static::getTable($table);
- }
-
-
- if (is_null($alias)) {
- $alias = static::makeAlias($table);
- }
-
- $query = db_select($table, $alias, $options);
- return $query->extend('ChadoPrefixExtender');
- }
-
- public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = []) {
- $table = static::getTable($table);
- if (is_null($alias)) {
- $alias = static::makeAlias($table);
- }
- $this->query->addJoin($type, $table, $alias, $condition, $arguments);
- return $this;
- }
-
- public function join($table, $alias = NULL, $condition = NULL, $arguments = []) {
- $table = static::getTable($table);
- if (is_null($alias)) {
- $alias = static::makeAlias($table);
- }
- $this->query->join($table, $alias, $condition, $arguments);
- return $this;
- }
-
- public function innerJoin($table, $alias = NULL, $condition = NULL, $arguments = []) {
- $table = static::getTable($table);
- if (is_null($alias)) {
- $alias = static::makeAlias($table);
- }
- $this->query->innerJoin($table, $alias, $condition, $arguments);
- return $this;
- }
- public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments = []) {
- $table = static::getTable($table);
- if (is_null($alias)) {
- $alias = static::makeAlias($table);
- }
- $this->query->leftJoin($table, $alias, $condition, $arguments);
- return $this;
- }
-
- public function rightJoin($table, $alias = NULL, $condition = NULL, $arguments = []) {
- $table = static::getTable($table);
- if (is_null($alias)) {
- $alias = static::makeAlias($table);
- }
- $this->query->rightJoin($table, $alias, $condition, $arguments);
- return $this;
- }
-
- public static function isChadoTable($table) {
- if (empty(static::$chado_tables)) {
- static::$chado_tables = chado_get_table_names(TRUE);
- }
- return in_array(strtolower($table), static::$chado_tables);
- }
-
- public static function getTable($table) {
- $chado_schema_name = chado_get_schema_name('chado');
- $drupal_schema_name = chado_get_schema_name('drupal');
-
- if (strpos($table, '.') === FALSE) {
-
-
- if (static::isChadoTable($table)) {
- $table = $chado_schema_name . ".{$table}";
- }
- else {
- $table = $drupal_schema_name . ".{$table}";
- }
- }
-
-
-
- $table = static::getRealSchema($table);
- return $table;
- }
-
- public static function getRealSchema($table) {
- if (strpos($table, 'public.') === 0) {
- $replace = chado_get_schema_name('drupal') . '.';
- return str_replace('public.', $replace, $table);
- }
- if (strpos($table, 'chado.') === 0) {
- $replace = chado_get_schema_name('chado') . '.';
- return str_replace('chado.', $replace, $table);
- }
- return $table;
- }
-
- public static function makeAlias($table) {
- return str_replace('.', '_', $table);
- }
- }
|