tripal_db.api.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /*************************************************************************
  3. * Purpose: To retrieve a chado db object
  4. *
  5. * @params db.db_id
  6. * @return chado db object with all fields from the chado db table
  7. */
  8. function tripal_db_get_db_by_db_id ($db_id) {
  9. $previous_db = tripal_db_set_active('chado');
  10. $r = db_fetch_object(db_query(
  11. "SELECT * FROM db WHERE db_id=%d", $db_id
  12. ));
  13. tripal_db_set_active($previous_db);
  14. return $r;
  15. }
  16. /*************************************************************************
  17. * Purpose: To retrieve a chado db object
  18. *
  19. * @params db.name
  20. * @return chado db object with all fields from the chado db table
  21. */
  22. function tripal_db_get_db_by_name ($name) {
  23. $previous_db = tripal_db_set_active('chado');
  24. $r = db_fetch_object(db_query(
  25. "SELECT * FROM db WHERE name='%s'", $name
  26. ));
  27. tripal_db_set_active($previous_db);
  28. return $r;
  29. }
  30. /*************************************************************************
  31. * Purpose: To retrieve a chado db object
  32. *
  33. * @params where_options: array(
  34. * <column_name> => array(
  35. * 'type' => <type of column: INT/STRING>,
  36. * 'value' => <the vlaue you want to filter on>,
  37. * 'exact' => <if TRUE use =; if FALSE use ~>,
  38. * )
  39. * )
  40. * @return chado db object with all fields from the chado db table
  41. */
  42. function tripal_db_get_db ($where_options) {
  43. $previous_db = tripal_db_set_active('chado');
  44. $where= array();
  45. //generate the where clause from supplied options
  46. // the key is the column name
  47. foreach ($where_options as $key => $val_array) {
  48. if (preg_match('/INT/', $val_array['type'])) {
  49. $where[] = $key."=".$val_array['value'];
  50. } else {
  51. if ($val_array['exact']) { $operator='='; }
  52. else { $operator='~'; }
  53. $where[] = $key.$operator."'".$val_array['value']."'";
  54. }
  55. }
  56. $r = db_fetch_object(db_query(
  57. "SELECT * FROM db WHERE ".implode(' AND ',$where)
  58. ));
  59. tripal_db_set_active($previous_db);
  60. return $r;
  61. }
  62. /*************************************************************************
  63. * Purpose: Create an options array to be used in a form element
  64. * which provides a list of all chado dbs
  65. *
  66. * @return an array(db_id => name) for each db in the chado db table
  67. */
  68. function tripal_db_get_db_options() {
  69. $previous_db = tripal_db_set_active('chado');
  70. $result = db_query(
  71. "SELECT db_id, name FROM db"
  72. );
  73. tripal_db_set_active($previous_db);
  74. $options = array();
  75. while ( $r = db_fetch_object($result) ) {
  76. $options[$r->db_id] = $r->name;
  77. }
  78. return $options;
  79. }
  80. /*************************************************************************
  81. * Purpose: To retrieve a chado dbxref object
  82. *
  83. * @params where_options: array(
  84. * <column_name> => array(
  85. * 'type' => <type of column: INT/STRING>,
  86. * 'value' => <the vlaue you want to filter on>,
  87. * 'exact' => <if TRUE use =; if FALSE use ~>,
  88. * )
  89. * )
  90. * @return chado dbxref object with all fields from the chado dbxref table
  91. */
  92. function tripal_db_get_dbxref ($where_options) {
  93. $previous_db = tripal_db_set_active('chado');
  94. $where= array();
  95. //generate the where clause from supplied options
  96. // the key is the column name
  97. foreach ($where_options as $key => $val_array) {
  98. if (preg_match('/INT/', $val_array['type'])) {
  99. $where[] = $key."=".$val_array['value'];
  100. } else {
  101. if ($val_array['exact']) { $operator='='; }
  102. else { $operator='~'; }
  103. $where[] = $key.$operator."'".$val_array['value']."'";
  104. }
  105. }
  106. $r = db_fetch_object(db_query(
  107. "SELECT * FROM dbxref WHERE ".implode(' AND ',$where)
  108. ));
  109. tripal_db_set_active($previous_db);
  110. return $r;
  111. }
  112. /*************************************************************************
  113. * Purpose: To retrieve a chado dbxref object with a given accession
  114. *
  115. * @params dbxref.accession
  116. * @params dbxref.db_id
  117. * @return chado dbxref object with all fields from the chado dbxref table
  118. */
  119. function tripal_db_get_dbxref_by_accession ($accession, $db_id=0) {
  120. if (!empty($db_id)) {
  121. $previous_db = tripal_db_set_active('chado');
  122. $r = db_fetch_object(db_query(
  123. "SELECT * FROM dbxref WHERE accession='%s' AND db_id=%d",
  124. $accession, $db_id
  125. ));
  126. tripal_db_set_active($previous_db);
  127. } else {
  128. $previous_db = tripal_db_set_active('chado');
  129. $r = db_fetch_object(db_query(
  130. "SELECT * FROM dbxref WHERE accession='%s'",
  131. $accession
  132. ));
  133. tripal_db_set_active($previous_db);
  134. }
  135. return $r;
  136. }
  137. /****************************************************************************
  138. * @section Chado Table Descriptions
  139. ****************************************************************************/
  140. /****************************************************************************
  141. * Implements hook_chado_dbxref_schema()
  142. * Purpose: To add descriptions and foreign keys to default table description
  143. * Note: This array will be merged with the array from all other implementations
  144. *
  145. * @return
  146. * Array describing the dbxref table
  147. */
  148. function tripal_stock_chado_dbxref_schema() {
  149. $description = array();
  150. $description['foreign keys']['db'] = array(
  151. 'table' => 'db',
  152. 'columns' => array(
  153. 'cv_id' => 'db_id',
  154. ),
  155. );
  156. return $description;
  157. }