tripal_db.api.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * @defgroup tripal_db_api External Database Module API
  4. * @ingroup tripal_api
  5. * @ingroup tripal_db
  6. * Provides an application programming interface (API) to manage databases
  7. */
  8. /**
  9. * Purpose: To retrieve a chado database object
  10. *
  11. * @param $select_values
  12. * An array meant to uniquely select a given database
  13. *
  14. * @return
  15. * Chado database object
  16. *
  17. * The database is selected using tripal_core_chado select and as such the
  18. * $select_values array parameter meant to uniquely identify the database to be
  19. * returned follows the same form as when using tripal_core_chado_select directly.
  20. *
  21. * Example Usage:
  22. * @code
  23. $select_values = array(
  24. 'name' => 'SOFP'
  25. );
  26. $db_object = tripal_db_get_db($select_values);
  27. * @endcode
  28. * The above code selects the SOFP db and returns the following object:
  29. * @code
  30. $db_object = stdClass Object (
  31. [db_id] => 49
  32. [name] => SOFP
  33. [description] =>
  34. [urlprefix] =>
  35. [url] =>
  36. );
  37. * @endcode
  38. *
  39. * @ingroup tripal_db_api
  40. */
  41. function tripal_db_get_db ($select_values) {
  42. /**
  43. $columns = array(
  44. 'db_id',
  45. 'name',
  46. 'description',
  47. 'urlprefix',
  48. 'url'
  49. );
  50. $results = tripal_core_chado_select('db', $columns, $select_values);
  51. if (sizeof($results) == 1) {
  52. return $results[0];
  53. } elseif (empty($results)) {
  54. watchdog('tripal_cdb',
  55. 'tripal_db_get_db: No db matches criteria values:%values',
  56. array('%values' => print_r($select_values, TRUE)),
  57. WATCHDOG_WARNING
  58. );
  59. return FALSE;
  60. } else {
  61. watchdog('tripal_db',
  62. 'tripal_db_get_db: 2+ dbs match criteria values:%values',
  63. array('%values' => print_r($select_values, TRUE)),
  64. WATCHDOG_WARNING
  65. );
  66. }
  67. */
  68. }
  69. /**
  70. * Purpose: To retrieve a chado db object
  71. *
  72. * @param $db_id
  73. * db.db_id
  74. * @return
  75. * Chado db object with all fields from the chado db table
  76. *
  77. * @ingroup tripal_db_api
  78. */
  79. function tripal_db_get_db_by_db_id ($db_id) {
  80. $previous_db = tripal_db_set_active('chado');
  81. $r = db_fetch_object(db_query(
  82. "SELECT * FROM db WHERE db_id=%d", $db_id
  83. ));
  84. tripal_db_set_active($previous_db);
  85. return $r;
  86. }
  87. /**
  88. * Purpose: To retrieve a chado db object
  89. *
  90. * @param $name
  91. * db.name
  92. * @return
  93. * chado db object with all fields from the chado db table
  94. *
  95. * @ingroup tripal_db_api
  96. */
  97. function tripal_db_get_db_by_name ($name) {
  98. $previous_db = tripal_db_set_active('chado');
  99. $r = db_fetch_object(db_query(
  100. "SELECT * FROM db WHERE name='%s'", $name
  101. ));
  102. tripal_db_set_active($previous_db);
  103. return $r;
  104. }
  105. // Purpose: To retrieve a chado db object
  106. //
  107. // @params where_options: array(
  108. // <column_name> => array(
  109. // 'type' => <type of column: INT/**STRING>,
  110. // 'value' => <the vlaue you want to filter on>,
  111. // 'exact' => <if TRUE use =; if FALSE use ~>,
  112. // )
  113. // )
  114. // @return chado db object with all fields from the chado db table
  115. //
  116. //function tripal_db_get_db ($where_options) {
  117. /**
  118. * Purpose: Create an options array to be used in a form element
  119. * which provides a list of all chado dbs
  120. *
  121. * @return
  122. * An array(db_id => name) for each db in the chado db table
  123. *
  124. * @ingroup tripal_db_api
  125. */
  126. function tripal_db_get_db_options() {
  127. $previous_db = tripal_db_set_active('chado');
  128. $result = db_query(
  129. "SELECT db_id, name FROM db"
  130. );
  131. tripal_db_set_active($previous_db);
  132. $options = array();
  133. while ( $r = db_fetch_object($result) ) {
  134. $options[$r->db_id] = $r->name;
  135. }
  136. return $options;
  137. }
  138. // Purpose: To retrieve a chado dbxref object
  139. //
  140. // @param where_options: array(
  141. // <column_name> => array(
  142. // 'type' => <type of column: INT/**STRING>,
  143. // 'value' => <the vlaue you want to filter on>,
  144. // 'exact' => <if TRUE use =; if FALSE use ~>,
  145. // )
  146. // )
  147. // @return chado dbxref object with all fields from the chado dbxref table
  148. //
  149. //function tripal_db_get_dbxref ($where_options) {
  150. /**
  151. * Purpose: To retrieve a chado database reference object
  152. *
  153. * @param $select_values
  154. * An array meant to uniquely select a given database reference
  155. *
  156. * @return
  157. * Chado database reference object
  158. *
  159. * The database reference is selected using tripal_core_chado select and as such the
  160. * $select_values array parameter meant to uniquely identify the database reference to be
  161. * returned follows the same form as when using tripal_core_chado_select directly.
  162. *
  163. * Example Usage:
  164. * @code
  165. $select_values = array(
  166. 'accession' => 'synonym',
  167. 'db_id' => array(
  168. 'name' => 'SOFP'
  169. )
  170. );
  171. $dbxref_object = tripal_db_get_dbxref($select_values);
  172. * @endcode
  173. * The above code selects the synonym database reference and returns the following object:
  174. * @code
  175. $dbxref_object = stdClass Object (
  176. [dbxref_id] => 2581
  177. [accession] => synonym
  178. [description] =>
  179. [version] =>
  180. [db_db_id] => 49
  181. [db_name] => SOFP
  182. [db_description] =>
  183. [db_urlprefix] =>
  184. [db_url] =>
  185. );
  186. * @endcode
  187. *
  188. * @ingroup tripal_db_api
  189. */
  190. function tripal_db_get_dbxref ($select_values) {
  191. /**
  192. $columns = array(
  193. 'dbxref_id',
  194. 'db_id',
  195. 'accession',
  196. 'description',
  197. 'version'
  198. );
  199. $results = tripal_core_chado_select('dbxref', $columns, $select_values);
  200. if (sizeof($results) == 1) {
  201. $dbxref = tripal_db_add_db_to_object(array('db_id' => $results[0]->db_id), $results[0], array());
  202. unset($dbxref->db_id);
  203. return $dbxref;
  204. } elseif (empty($results)) {
  205. watchdog('tripal_db',
  206. 'tripal_db_get_dbxref: No dbxref matches criteria values:%values',
  207. array('%values' => print_r($select_values, TRUE)),
  208. WATCHDOG_WARNING
  209. );
  210. return FALSE;
  211. } else {
  212. watchdog('tripal_db',
  213. 'tripal_db_get_dbxref: 2+ dbxrefs match criteria values:%values',
  214. array('%values' => print_r($select_values, TRUE)),
  215. WATCHDOG_WARNING
  216. );
  217. }
  218. */
  219. }
  220. /**
  221. * Purpose: To retrieve a chado dbxref object with a given accession
  222. *
  223. * @param $accession
  224. * dbxref.accession
  225. * @param $db_id
  226. * dbxref.db_id
  227. * @return
  228. * chado dbxref object with all fields from the chado dbxref table
  229. *
  230. * @ingroup tripal_db_api
  231. */
  232. function tripal_db_get_dbxref_by_accession ($accession, $db_id=0) {
  233. if (!empty($db_id)) {
  234. $previous_db = tripal_db_set_active('chado');
  235. $r = db_fetch_object(db_query(
  236. "SELECT * FROM dbxref WHERE accession='%s' AND db_id=%d",
  237. $accession, $db_id
  238. ));
  239. tripal_db_set_active($previous_db);
  240. } else {
  241. $previous_db = tripal_db_set_active('chado');
  242. $r = db_fetch_object(db_query(
  243. "SELECT * FROM dbxref WHERE accession='%s'",
  244. $accession
  245. ));
  246. tripal_db_set_active($previous_db);
  247. }
  248. return $r;
  249. }
  250. /**
  251. * Implements hook_chado_dbxref_schema()
  252. * Purpose: To add descriptions and foreign keys to default table description
  253. * Note: This array will be merged with the array from all other implementations
  254. *
  255. * @return
  256. * Array describing the dbxref table
  257. *
  258. * @ingroup tripal_schema_api
  259. */
  260. function tripal_db_chado_dbxref_schema() {
  261. $description = array();
  262. $description['foreign keys']['db'] = array(
  263. 'table' => 'db',
  264. 'columns' => array(
  265. 'db_id' => 'db_id',
  266. ),
  267. );
  268. return $description;
  269. }