tripal_db.api.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*************************************************************************
  3. * Purpose: To retrieve a chado db object
  4. *
  5. * @params where_options: array(
  6. * <column_name> => array(
  7. * 'type' => <type of column: INT/STRING>,
  8. * 'value' => <the vlaue you want to filter on>,
  9. * 'exact' => <if TRUE use =; if FALSE use ~>,
  10. * )
  11. * )
  12. * @return chado db object with all fields from the chado db table
  13. */
  14. function tripal_db_get_db ($where_options) {
  15. $previous_db = tripal_db_set_active('chado');
  16. $where= array();
  17. //generate the where clause from supplied options
  18. // the key is the column name
  19. foreach ($where_options as $key => $val_array) {
  20. if (preg_match('/INT/', $val_array['type'])) {
  21. $where[] = $key."=".$val_array['value'];
  22. } else {
  23. if ($val_array['exact']) { $operator='='; }
  24. else { $operator='~'; }
  25. $where[] = $key.$operator."'".$val_array['value']."'";
  26. }
  27. }
  28. $r = db_fetch_object(db_query(
  29. "SELECT * FROM db WHERE ".implode(' AND ',$where)
  30. ));
  31. tripal_db_set_active($previous_db);
  32. return $r;
  33. }
  34. /*************************************************************************
  35. * Purpose: Create an options array to be used in a form element
  36. * which provides a list of all chado dbs
  37. *
  38. * @return an array(db_id => name) for each db in the chado db table
  39. */
  40. function tripal_db_get_db_options() {
  41. $previous_db = tripal_db_set_active('chado');
  42. $result = db_query(
  43. "SELECT db_id, name FROM db"
  44. );
  45. db_set_active($previous_db);
  46. $options = array();
  47. while ( $r = db_fetch_object($result) ) {
  48. $options[$r->db_id] = $r->name;
  49. }
  50. return $options;
  51. }
  52. /*************************************************************************
  53. * Purpose: To retrieve a chado dbxref object
  54. *
  55. * @params where_options: array(
  56. * <column_name> => array(
  57. * 'type' => <type of column: INT/STRING>,
  58. * 'value' => <the vlaue you want to filter on>,
  59. * 'exact' => <if TRUE use =; if FALSE use ~>,
  60. * )
  61. * )
  62. * @return chado dbxref object with all fields from the chado dbxref table
  63. */
  64. function tripal_db_get_dbxref ($where_options) {
  65. $previous_db = tripal_db_set_active('chado');
  66. $where= array();
  67. //generate the where clause from supplied options
  68. // the key is the column name
  69. foreach ($where_options as $key => $val_array) {
  70. if (preg_match('/INT/', $val_array['type'])) {
  71. $where[] = $key."=".$val_array['value'];
  72. } else {
  73. if ($val_array['exact']) { $operator='='; }
  74. else { $operator='~'; }
  75. $where[] = $key.$operator."'".$val_array['value']."'";
  76. }
  77. }
  78. $r = db_fetch_object(db_query(
  79. "SELECT * FROM dbxref WHERE ".implode(' AND ',$where)
  80. ));
  81. tripal_db_set_active($previous_db);
  82. return $r;
  83. }