ChadoPrefixExtender.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * ChadoPrefixExtender
  4. *
  5. * A query extender that for select queries. By extending the
  6. * SelectQueryExtender class, we can make sure that chado tables
  7. *
  8. * @see https://www.drupal.org/docs/7/api/database-api/dynamic-queries/extenders
  9. */
  10. class ChadoPrefixExtender extends SelectQueryExtender {
  11. /**
  12. * A static cache for Chado tables.
  13. *
  14. * @var array
  15. */
  16. protected static $chado_tables = [];
  17. /**
  18. * A replacement for db_select when querying Chado.
  19. *
  20. * Use this function instead of db_select when querying Chado tables.
  21. *
  22. * @param string|\SelectQuery $table
  23. * The base table for this query. May be a
  24. * string or another SelectQuery object. If a query object is passed, it
  25. * will be used as a subselect.
  26. * @param string $alias
  27. * The alias for the base table of this query.
  28. * @param array $options
  29. * An array of options to control how the query
  30. * operates.
  31. *
  32. * @return \SelectQuery
  33. * A new SelectQuery object for this connection.
  34. *
  35. * @ingroup tripal_chado_query_api
  36. */
  37. public static function select($table, $alias = NULL, array $options = []) {
  38. // Since the table could also be a SelectQuery object, we should verify that
  39. // it is a string first.
  40. if (is_string($table)) {
  41. $table = static::getTable($table);
  42. }
  43. // If the alias is null, determine a safe alias. db_select fails to generate
  44. // a safe alias when the table name is prefixed with "public.".
  45. if (is_null($alias)) {
  46. $alias = static::makeAlias($table);
  47. }
  48. // Create a select query
  49. $query = db_select($table, $alias, $options);
  50. return $query->extend('ChadoPrefixExtender');
  51. }
  52. /**
  53. * Overwrites the join to prefix table names.
  54. *
  55. * @param string $table
  56. * Table to join.
  57. * @param string $alias
  58. * Alias for joined table.
  59. * @param string $condition
  60. * Operation for joining.
  61. * @param array $arguments
  62. * Additional arguments.
  63. *
  64. * @return $this
  65. * The current object.
  66. *
  67. * @ingroup tripal_chado_query_api
  68. */
  69. public function join($table, $alias = NULL, $condition = NULL, $arguments = []) {
  70. $table = static::getTable($table);
  71. if (is_null($alias)) {
  72. $alias = static::makeAlias($table);
  73. }
  74. $this->query->join($table, $alias, $condition, $arguments);
  75. return $this;
  76. }
  77. /**
  78. * Checks if a table is a chado table.
  79. *
  80. * @param string $table
  81. *
  82. * @return bool
  83. */
  84. public static function isChadoTable($table) {
  85. if (empty(static::$chado_tables)) {
  86. static::$chado_tables = chado_get_table_names(TRUE);
  87. }
  88. return in_array($table, static::$chado_tables);
  89. }
  90. /**
  91. * If the table name has a schema name as a prefix, replace it with the
  92. * correct schema name.
  93. *
  94. * @param string $table
  95. * The table name.
  96. *
  97. * @return string
  98. * The table with the correct prefix.
  99. *
  100. * @see chado_replace_schema_prefix()
  101. */
  102. public static function getTable($table) {
  103. // No schema was provided.
  104. if (strpos($table, '.') === FALSE) {
  105. // If this is a chado table, add the chado prefix. Otherwise, add the
  106. // public prefix.
  107. if (static::isChadoTable($table)) {
  108. $table = "chado.{$table}";
  109. }
  110. else {
  111. $table = "public.{$table}";
  112. }
  113. }
  114. // Now that the schema has been set, we can replace it with the correct
  115. // name. Note that schema names can be altered by developers so we need to
  116. // to run the following function to obtain the final name.
  117. $table = chado_replace_schema_prefix($table);
  118. return $table;
  119. }
  120. /**
  121. * Create a safe alias.
  122. *
  123. * @param string $table Table name.
  124. *
  125. * @return string
  126. * The safe alias.
  127. */
  128. public static function makeAlias($table) {
  129. return str_replace('.', '_', $table);
  130. }
  131. }