tripal_core.api.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. require_once "chado_tables.schema.inc";
  3. function tripal_core_chado_insert_test(){
  4. $values = array(
  5. 'organism_id' => array(
  6. 'genus' => 'Citrus',
  7. 'species' => 'sinensis',
  8. ),
  9. 'name' => 'orange1.1g000034m.g',
  10. 'uniquename' => 'orange1.1g000034m.g',
  11. 'type_id' => array (
  12. 'cv_id' => array (
  13. 'name' => 'sequence',
  14. ),
  15. 'name' => 'gene',
  16. 'is_obsolete' => 0
  17. ),
  18. );
  19. $result = tripal_core_chado_insert('feature',$values);
  20. return "<pre>$result</pre>";
  21. }
  22. /************************************************************************
  23. *
  24. */
  25. function tripal_core_chado_insert($table,$values){
  26. $chado = tripal_core_get_chado_schema();
  27. $insert_values = array();
  28. // get the table description
  29. $table_desc = $chado[$table];
  30. // iterate through the values array and create a new 'insert_values' array
  31. // that has all the values needed for insert with all foreign relationsihps
  32. // resolved.
  33. foreach($values as $field => $value){
  34. if(is_array($value)){
  35. // select the value from the foreign key relationship for this value
  36. $insert_values[$field] = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
  37. }
  38. else {
  39. $insert_values[$field] = $value;
  40. }
  41. }
  42. // check for violation of any unique constraints
  43. return print_r($insert_values,1) . " " .print_r($values,1) . " " . print_r($table_desc,1) ;
  44. }
  45. /************************************************************************
  46. *
  47. */
  48. function tripal_core_chado_get_foreign_key($table_desc,$field,$values){
  49. // get the list of foreign keys for this table description and
  50. // iterate through those until we find the one we're looking for
  51. $fkeys = $table_desc['foreign keys'];
  52. if($fkeys){
  53. foreach($fkeys as $name => $def){
  54. $table = $def['table'];
  55. $columns = $def['columns'];
  56. // iterate through the columns of the foreign key relationship
  57. foreach($columns as $left => $right){
  58. // does the left column in the relationship match our field?
  59. if(strcmp($field,$left)==0){
  60. // the column name of the foreign key matches the field we want
  61. // so this is the right relationship. Now we want to select
  62. $select_cols = array($right);
  63. $result = tripal_core_chado_select($table,$select_cols,$values);
  64. return $result->$right;
  65. }
  66. }
  67. }
  68. }
  69. else {
  70. // TODO: what do we do if we get to this point and we have a fk
  71. // relationship expected but we don't have any definition for one in the
  72. // table schema??
  73. }
  74. }
  75. /************************************************************************
  76. *
  77. */
  78. function tripal_core_chado_select($table,$columns,$values){
  79. // get the table description
  80. $chado = tripal_core_get_chado_schema();
  81. $table_desc = $chado[$table];
  82. $select = '';
  83. $from = '';
  84. $where = '';
  85. $args = array();
  86. foreach($values as $field => $value){
  87. $select[] = $field;
  88. if(is_array($value)){
  89. // select the value from the foreign key relationship for this value
  90. $where[$field] = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
  91. }
  92. else {
  93. $where[$field] = $value;
  94. }
  95. }
  96. // now build the SQL select statement
  97. $sql = "SELECT " . implode(',',$columns) . " ";
  98. $sql .= "FROM {$table} ";
  99. $sql .= "WHERE ";
  100. foreach($where as $field => $value){
  101. $sql .= "$field = '%s' AND ";
  102. $args[] = $value;
  103. }
  104. $sql .= " 1=1"; // just add a 1=1 so we don't have trim off the last 'AND'
  105. return db_fetch_object(db_query($sql,$args));
  106. }