123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * ChadoPrefixExtender
- *
- * A query extender that for select queries. By extending the
- * SelectQueryExtender class, we can make sure that chado tables
- *
- * @see https://www.drupal.org/docs/7/api/database-api/dynamic-queries/extenders
- */
- class ChadoPrefixExtender extends SelectQueryExtender {
- /**
- * A static cache for Chado tables.
- *
- * @var array
- */
- protected static $chado_tables = [];
- /**
- * A replacement for db_select when querying Chado.
- *
- * Use this function instead of db_select when querying Chado tables.
- *
- * @param string|\SelectQuery $table
- * The base table for this query. May be a
- * string or another SelectQuery object. If a query object is passed, it
- * will be used as a subselect.
- * @param string $alias
- * The alias for the base table of this query.
- * @param array $options
- * An array of options to control how the query
- * operates.
- *
- * @return \SelectQuery
- * A new SelectQuery object for this connection.
- *
- * @ingroup tripal_chado_query_api
- */
- public static function select($table, $alias = NULL, array $options = []) {
- // Since the table could also be a SelectQuery object, we should verify that
- // it is a string first.
- if (is_string($table)) {
- $table = static::getTable($table);
- }
- // If the alias is null, determine a safe alias. db_select fails to generate
- // a safe alias when the table name is prefixed with "public.".
- if (is_null($alias)) {
- $alias = static::makeAlias($table);
- }
- // Create a select query
- $query = db_select($table, $alias, $options);
- return $query->extend('ChadoPrefixExtender');
- }
- /**
- * Overwrites the join to prefix table names.
- *
- * @param string $table
- * Table to join.
- * @param string $alias
- * Alias for joined table.
- * @param string $condition
- * Operation for joining.
- * @param array $arguments
- * Additional arguments.
- *
- * @return $this
- * The current object.
- *
- * @ingroup tripal_chado_query_api
- */
- 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;
- }
- /**
- * Checks if a table is a chado table.
- *
- * @param string $table
- *
- * @return bool
- */
- public static function isChadoTable($table) {
- if (empty(static::$chado_tables)) {
- static::$chado_tables = chado_get_table_names(TRUE);
- }
- return in_array($table, static::$chado_tables);
- }
- /**
- * If the table name has a schema name as a prefix, replace it with the
- * correct schema name.
- *
- * @param string $table
- * The table name.
- *
- * @return string
- * The table with the correct prefix.
- *
- * @see chado_replace_schema_prefix()
- */
- public static function getTable($table) {
- // No schema was provided.
- if (strpos($table, '.') === FALSE) {
- // If this is a chado table, add the chado prefix. Otherwise, add the
- // public prefix.
- if (static::isChadoTable($table)) {
- $table = "chado.{$table}";
- }
- else {
- $table = "public.{$table}";
- }
- }
- // Now that the schema has been set, we can replace it with the correct
- // name. Note that schema names can be altered by developers so we need to
- // to run the following function to obtain the final name.
- $table = chado_replace_schema_prefix($table);
- return $table;
- }
- /**
- * Create a safe alias.
- *
- * @param string $table Table name.
- *
- * @return string
- * The safe alias.
- */
- public static function makeAlias($table) {
- return str_replace('.', '_', $table);
- }
- }
|