ChadoSchema.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Provides an application programming interface (API) for describing Chado tables.
  4. *
  5. * The functions provided in this documentation should not be called as is, but if you need
  6. * the Drupal-style array definition for any table, use the following function
  7. * call:
  8. *
  9. * $table_desc = chado_get_schema($table)
  10. *
  11. * where the variable $table contains the name of the table you want to
  12. * retireve. The chado_get_schema function determines the appropriate version of
  13. * Chado and uses the Drupal hook infrastructure to call the appropriate
  14. * hook function to retrieve the table schema.
  15. */
  16. class ChadoSchema {
  17. /**
  18. * @var string
  19. * The current version for this site. E.g. "1.3".
  20. */
  21. protected $version = '';
  22. /**
  23. * @var string
  24. * The name of the schema chado was installed in.
  25. */
  26. protected $schema_name = 'chado';
  27. /**
  28. * The ChadoSchema constructor.
  29. *
  30. * @param string $version
  31. * The current version for this site. E.g. "1.3". If a version is not provided, the
  32. * version of the current database will be looked up.
  33. */
  34. public function __construct($version = NULL, $schema_name = NULL) {
  35. // Set the version of the schema.
  36. if ($version === NULL) {
  37. $this->version = chado_get_version(TRUE);
  38. }
  39. else {
  40. $this->version = $version;
  41. }
  42. // Set the name of the schema.
  43. if ($schema_name === NULL) {
  44. $this->schema_name = chado_get_schema_name('chado');
  45. }
  46. else {
  47. $this->schema_name = $schema_name;
  48. }
  49. }
  50. /**
  51. * Returns the version number of the Chado this object references.
  52. *
  53. * @returns
  54. * The version of Chado
  55. */
  56. public function getVersion() {
  57. return $this->version;
  58. }
  59. /**
  60. * Retrieve the name of the PostgreSQL schema housing Chado.
  61. *
  62. * @return
  63. * The name of the schema.
  64. */
  65. public function getSchemaName() {
  66. return $this->schema_name;
  67. }
  68. /**
  69. * Retrieves the list of tables in the Chado schema. By default it only returns
  70. * the default Chado tables, but can return custom tables added to the
  71. * Chado schema if requested.
  72. *
  73. * @param $include_custom
  74. * Optional. Set as TRUE to include any custom tables created in the
  75. * Chado schema. Custom tables are added to Chado using the
  76. * tripal_chado_chado_create_table() function.
  77. *
  78. * @returns
  79. * An associative array where the key and value pairs are the Chado table names.
  80. */
  81. public function getTableNames($include_custom = FALSE) {
  82. $tables = array();
  83. if ($this->version == '1.3') {
  84. $tables_v1_3 = tripal_chado_chado_get_v1_3_tables();
  85. foreach ($tables_v1_3 as $table) {
  86. $tables[$table] = $table;
  87. }
  88. }
  89. if ($this->version == '1.2') {
  90. $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
  91. foreach ($tables_v1_2 as $table) {
  92. $tables[$table] = $table;
  93. }
  94. }
  95. if ($this->version == '1.11' or $v == '1.11 or older') {
  96. $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
  97. foreach ($tables_v1_11 as $table) {
  98. $tables[$table] = $table;
  99. }
  100. }
  101. // now add in the custom tables too if requested
  102. if ($include_custom) {
  103. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  104. $resource = db_query($sql);
  105. foreach ($resource as $r) {
  106. $tables[$r->table_name] = $r->table_name;
  107. }
  108. }
  109. asort($tables);
  110. return $tables;
  111. }
  112. /**
  113. * Check that any given Chado table exists.
  114. *
  115. * This function is necessary because Drupal's db_table_exists() function will
  116. * not look in any other schema but the one where Drupal is installed
  117. *
  118. * @param $table
  119. * The name of the chado table whose existence should be checked.
  120. *
  121. * @return
  122. * TRUE if the table exists in the chado schema and FALSE if it does not.
  123. */
  124. public function checkTableExists($table) {
  125. return chado_table_exists($table);
  126. }
  127. }