Tripal v1.0 (6.x-1.0)
tripal_db.api.inc
Go to the documentation of this file.
00001 <?php
00002 
00044 function tripal_db_get_db($select_values) {
00045 
00046   $columns = array(
00047     'db_id',
00048     'name',
00049     'description',
00050     'urlprefix',
00051     'url'
00052   );
00053   $results = tripal_core_chado_select('db', $columns, $select_values);
00054   if (sizeof($results) == 1) {
00055     return $results[0];
00056   }
00057   elseif (empty($results)) {
00058     watchdog('tripal_cdb',
00059       'tripal_db_get_db: No db matches criteria values:%values',
00060       array('%values' => print_r($select_values, TRUE)),
00061       WATCHDOG_WARNING
00062     );
00063     return FALSE;
00064   }
00065   else {
00066     watchdog('tripal_db',
00067       'tripal_db_get_db: 2+ dbs match criteria values:%values',
00068       array('%values' => print_r($select_values, TRUE)),
00069       WATCHDOG_WARNING
00070     );
00071   }
00072 
00073 }
00074 
00085 function tripal_db_get_db_by_db_id($db_id) {
00086 
00087   $r = db_fetch_object(chado_query(
00088     "SELECT * FROM {db} WHERE db_id=%d", $db_id
00089   ));
00090 
00091   return $r;
00092 }
00093 
00104 function tripal_db_get_db_by_name($name) {
00105 
00106   $r = db_fetch_object(chado_query(
00107     "SELECT * FROM {db} WHERE name='%s'", $name
00108   ));
00109 
00110   return $r;
00111 }
00112 
00113 // Purpose: To retrieve a chado db object
00114 //
00115 // @params where_options: array(
00116 //                          <column_name> => array(
00117 //                            'type' => <type of column: INT/**STRING>,
00118 //                            'value' => <the vlaue you want to filter on>,
00119 //                            'exact' => <if TRUE use =; if FALSE use ~>,
00120 //                          )
00121 //        )
00122 // @return chado db object with all fields from the chado db table
00123 //
00124 //function tripal_db_get_db ($where_options) {
00125 
00135 function tripal_db_get_db_options() {
00136 
00137   $result = chado_query(
00138     "SELECT db_id, name FROM {db}"
00139   );
00140 
00141   $options = array();
00142   while ( $r = db_fetch_object($result) ) {
00143     $options[$r->db_id] = $r->name;
00144   }
00145 
00146   return $options;
00147 
00148 }
00149 
00150 // Purpose: To retrieve a chado dbxref object
00151 //
00152 // @param where_options: array(
00153 //                          <column_name> => array(
00154 //                            'type' => <type of column: INT/**STRING>,
00155 //                            'value' => <the vlaue you want to filter on>,
00156 //                            'exact' => <if TRUE use =; if FALSE use ~>,
00157 //                          )
00158 //        )
00159 // @return chado dbxref object with all fields from the chado dbxref table
00160 //
00161 //function tripal_db_get_dbxref ($where_options) {
00162 
00203 function tripal_db_get_dbxref($select_values) {
00204 
00205   $columns = array(
00206     'dbxref_id',
00207     'db_id',
00208     'accession',
00209     'description',
00210     'version'
00211   );
00212   $results = tripal_core_chado_select('dbxref', $columns, $select_values);
00213   if (sizeof($results) == 1) {
00214     $dbxref = tripal_db_add_db_to_object(array('db_id' => $results[0]->db_id), $results[0], array());
00215     unset($dbxref->db_id);
00216 
00217     return $dbxref;
00218   }
00219   elseif (empty($results)) {
00220     watchdog('tripal_db',
00221       'tripal_db_get_dbxref: No dbxref matches criteria values:%values',
00222       array('%values' => print_r($select_values, TRUE)),
00223       WATCHDOG_WARNING
00224     );
00225     return FALSE;
00226   }
00227   else {
00228     watchdog('tripal_db',
00229       'tripal_db_get_dbxref: 2+ dbxrefs match criteria values:%values',
00230       array('%values' => print_r($select_values, TRUE)),
00231       WATCHDOG_WARNING
00232     );
00233   }
00234 
00235 }
00236 
00249 function tripal_db_get_dbxref_by_accession($accession, $db_id=0) {
00250 
00251   if (!empty($db_id)) {
00252     $r = db_fetch_object(chado_query(
00253       "SELECT * FROM {dbxref} WHERE accession='%s' AND db_id=%d",
00254       $accession, $db_id
00255     ));
00256   }
00257   else {
00258     $r = db_fetch_object(chado_query(
00259       "SELECT * FROM {dbxref} WHERE accession='%s'",
00260       $accession
00261     ));
00262   }
00263 
00264   return $r;
00265 }
00266 
00291 function tripal_db_add_db($dbname, $description = '', $url = '', 
00292   $urlprefix = '', $update = 0) {
00293 
00294   // build the values array for inserting/updating
00295   $ins_values = array(
00296     'name' => $dbname,
00297     'description' => $description,
00298     'url' => $url,
00299     'urlprefix' => $urlprefix
00300   );
00301   
00302   // get the database record if it already exists
00303   $sel_values = array('name' => $dbname);
00304   $sel_options = array('statement_name' => 'sel_db_na');
00305   $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
00306   
00307   // if it does not exists then add it
00308   if (count($result) == 0) {    
00309     $ins_options = array('statement_name' => 'ins_db_nadeurur');   
00310     $success = tripal_core_chado_insert('db', $ins_values, $ins_options); 
00311     if (!$success) {
00312       watchdog('tripal_db', "Cannot create db '$dbname'.", NULL, WATCHDOG_WARNING);
00313       return 0;
00314     }
00315     $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
00316   }
00317   // if it exists and update is enabled the do the update
00318   elseif ($update) {
00319     $upd_options = array('statement_name' => 'upd_db_nadeurur');
00320     $success = tripal_core_chado_update('db', $sel_values, $ins_values, $upd_options);
00321     if (!$success) {
00322       watchdog('tripal_db', "Cannot update db '$dbname'.", NULL, WATCHDOG_WARNING);
00323       return 0;
00324     }
00325     $result = tripal_core_chado_select('db', array('*'), $sel_values, $sel_options);
00326   }
00327   
00328   // return the database object
00329   return $result[0];
00330 
00331 }
00336 function tripal_db_add_dbxref($db_id, $accession, $version = '', $description = '') {
00337 
00338   $ins_values = array(
00339     'db_id'       => $db_id,
00340     'accession'   => $accession,
00341     'version'     => $version,
00342     'description' => $description
00343   );
00344     
00345   // check to see if the dbxref exists
00346   $sel_values = array(
00347     'db_id'     => $db_id,
00348     'accession' => $accession,
00349   );
00350   $sel_options = array('statement_name' => 'sel_dbxref_dbacve', 'is_duplicate' => 1);
00351   $result = tripal_core_chado_select('dbxref', array('*'), $sel_values, $sel_options);
00352   
00353   // if it doesn't already exist then add it
00354   if (!$result) {
00355     $ins_options = array('statement_name' => 'ins_dbxref_dbacvede');
00356     $success = tripal_core_chado_insert('dbxref', $ins_values, $ins_options);
00357     if (!$success) {
00358       watchdog('tripal_cv', "Failed to insert the dbxref record $accession", NULL, WATCHDOG_WARNING);
00359       return 0;
00360     }
00361     $result = tripal_core_chado_select('dbxref', array('*'), $sel_values, $sel_options);
00362   }
00363   
00364   return $result[0];
00365 }
 All Classes Files Functions Variables