tripal_core.api.inc 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Core API
  5. *
  6. * This file provides the API needed for all other Tripal and Tripal dependent
  7. * modules.
  8. */
  9. /**
  10. * @defgroup tripal_api Tripal API
  11. * @{
  12. * Provides an application programming interface (API) for the tripal package
  13. *
  14. * The Tripal API currently provides generic insert/update/select functions for all chado content as
  15. * well as some module specific functions that insert/update/delete/select specific chado content.
  16. *
  17. * This API is currently in it's infancy so some necessary functions might be missing. If you come
  18. * across a missing function that you think should be included go to the sourceforge feature request * page and request it's inclusion in the API. Such feature requests with a working function * definition will be given priority.
  19. * @}
  20. */
  21. /**
  22. * @defgroup tripal_schema_api Tripal Schema API
  23. * @{
  24. * Provides an application programming interface (API) used to describe chado tables to
  25. * drupal/tripal
  26. * @}
  27. * @ingroup tripal_api
  28. */
  29. require_once "tripal_core.schema.api.inc";
  30. // just a temporary function for debugging
  31. function tripal_core_chado_insert_test(){
  32. $ivalues = array(
  33. 'organism_id' => array(
  34. 'genus' => 'Lens',
  35. 'species' => 'culinaris',
  36. ),
  37. 'name' => 'orange1.1g000034m.g',
  38. 'uniquename' => 'orange1.1g000034m.g9',
  39. 'type_id' => array (
  40. 'cv_id' => array (
  41. 'name' => 'sequence',
  42. ),
  43. 'name' => 'gene',
  44. 'is_obsolete' => 0
  45. ),
  46. );
  47. $umatch = array(
  48. 'organism_id' => array(
  49. 'genus' => 'Lens',
  50. 'species' => 'culinaris',
  51. ),
  52. 'uniquename' => 'orange1.1g000034m.g9',
  53. 'type_id' => array (
  54. 'cv_id' => array (
  55. 'name' => 'sequence',
  56. ),
  57. 'name' => 'gene',
  58. 'is_obsolete' => 0
  59. ),
  60. );
  61. $uvalues = array(
  62. 'name' => 'orange1.1g000034m.g',
  63. 'type_id' => array (
  64. 'cv_id' => array (
  65. 'name' => 'sequence',
  66. ),
  67. 'name' => 'mRNA',
  68. 'is_obsolete' => 0
  69. ),
  70. );
  71. $select_multiple = array(
  72. 'dbxref_id' => array(
  73. 'db_id' => 2,
  74. )
  75. );
  76. //$result = tripal_core_chado_insert('feature',$ivalues);
  77. //return "$result->feature_id";
  78. $result = tripal_core_chado_update('feature',$umatch,$uvalues);
  79. //$result = tripal_core_chado_select('feature',array('type_id', 'uniquename'),$select_multiple);
  80. return $result;
  81. }
  82. /**
  83. * Provides a generic routine for inserting into any Chado table
  84. *
  85. * Use this function to insert a record into any Chado table. The first
  86. * argument specifies the table for inserting and the second is an array
  87. * of values to be inserted. The array is mutli-dimensional such that
  88. * foreign key lookup values can be specified.
  89. *
  90. * @param $table
  91. * The name of the chado table for inserting
  92. * @param $values
  93. * An associative array containing the values for inserting.
  94. *
  95. * @return
  96. * On success this function returns TRUE. On failure, it returns FALSE.
  97. *
  98. * Example usage:
  99. * @code
  100. * $values = array(
  101. * 'organism_id' => array(
  102. * 'genus' => 'Citrus',
  103. * 'species' => 'sinensis',
  104. * ),
  105. * 'name' => 'orange1.1g000034m.g',
  106. * 'uniquename' => 'orange1.1g000034m.g',
  107. * 'type_id' => array (
  108. * 'cv_id' => array (
  109. * 'name' => 'sequence',
  110. * ),
  111. * 'name' => 'gene',
  112. * 'is_obsolete' => 0
  113. * ),
  114. * );
  115. * $result = tripal_core_chado_insert('feature',$values);
  116. * @endcode
  117. * The above code inserts a record into the feature table. The $values array is
  118. * nested such that the organism is selected by way of the organism_id foreign
  119. * key constraint by specifying the genus and species. The cvterm is also
  120. * specified using its foreign key and the cv_id for the cvterm is nested as
  121. * well.
  122. *
  123. * @ingroup tripal_api
  124. */
  125. function tripal_core_chado_insert($table,$values){
  126. $insert_values = array();
  127. // get the table description
  128. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  129. // iterate through the values array and create a new 'insert_values' array
  130. // that has all the values needed for insert with all foreign relationsihps
  131. // resolved.
  132. foreach($values as $field => $value){
  133. if(is_array($value)){
  134. // select the value from the foreign key relationship for this value
  135. $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
  136. if (sizeof($results) > 1) {
  137. watchdog('tripal_core', 'tripal_core_chado_insert: Too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  138. } elseif (sizeof($results) < 1) {
  139. watchdog('tripal_core', 'tripal_core_chado_insert: no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  140. } else {
  141. $insert_values[$field] = $results[0];
  142. }
  143. }
  144. else {
  145. $insert_values[$field] = $value;
  146. }
  147. }
  148. // check for violation of any unique constraints
  149. $ukeys = $table_desc['unique keys'];
  150. $ukselect_cols = array();
  151. $ukselect_vals = array();
  152. if ($ukeys) {
  153. foreach($ukeys as $name => $fields){
  154. foreach($fields as $index => $field){
  155. // build the arrays for performing a select that will check the contraint
  156. array_push($ukselect_cols,$field);
  157. $ukselect_vals[$field] = $insert_values[$field];
  158. }
  159. // now check the constraint
  160. if(tripal_core_chado_select($table,$ukselect_cols,$ukselect_vals)){
  161. watchdog('tripal_core',"tripal_core_chado_insert: Cannot insert duplicate record into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
  162. return false;
  163. }
  164. }
  165. }
  166. // if trying to insert a field that is the primary key, make sure it also is unique
  167. $pkey = $table_desc['primary key'][0];
  168. if($insert_values[$pkey]){
  169. if(tripal_core_chado_select($table,array($pkey),array($pkey => $insert_values[$pkey]))){
  170. watchdog('tripal_core',"tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
  171. return false;
  172. }
  173. }
  174. // make sure required fields have a value
  175. $fields = $table_desc['fields'];
  176. foreach($fields as $field => $def){
  177. // a field is considered missing if it cannot be null and there is no default
  178. // value for it or it is of type 'serial'
  179. if($def['not null'] == 1 and !array_key_exists($field,$insert_values) and !isset($def['default']) and strcmp($def['type'],serial)!=0){
  180. watchdog('tripal_core',"tripal_core_chado_insert: Field $table.$field cannot be null: " . print_r($values,1),array(),'WATCHDOG_ERROR');
  181. return false;
  182. }
  183. }
  184. // Now build the insert SQL statement
  185. $ifields = array();
  186. $ivalues = array();
  187. $itypes = array();
  188. foreach ($insert_values as $field => $value){
  189. array_push($ifields,$field);
  190. array_push($ivalues,$value);
  191. if(strcmp($fields[$field]['type'],'serial')==0 or
  192. strcmp($fields[$field]['type'],'int')==0){
  193. array_push($itypes,"%d");
  194. } else {
  195. array_push($itypes,"'%s'");
  196. }
  197. }
  198. $sql = "INSERT INTO {$table} (" . implode(", ",$ifields) . ") VALUES (". implode(", ",$itypes) .")";
  199. // finally perform the insert.
  200. $previous_db = tripal_db_set_active('chado'); // use chado database
  201. $result = db_query($sql,$ivalues);
  202. tripal_db_set_active($previous_db); // now use drupal database
  203. if($result){
  204. // add primary keys to values before return
  205. $primary_key = array();
  206. foreach ($table_desc['primary key'] as $field) {
  207. $value = db_last_insert_id($table, $field);
  208. $values[$field] = $value;
  209. }
  210. return $values;
  211. }
  212. else {
  213. watchdog('tripal_core',"tripal_core_chado_insert: Cannot insert record into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
  214. return false;
  215. }
  216. return false;
  217. }
  218. /**
  219. *
  220. */
  221. function tripal_core_chado_delete($table,$match){
  222. $delete_matches = array(); // contains the values for the where clause
  223. // get the table description
  224. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  225. // get the values needed for matching in the SQL statement
  226. foreach ($match as $field => $value){
  227. if(is_array($value)){
  228. $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
  229. if (sizeof($results) > 1) {
  230. watchdog('tripal_core', 'tripal_core_chado_delete: When trying to find record to delete, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  231. } elseif (sizeof($results) < 1) {
  232. watchdog('tripal_core', 'tripal_core_chado_delete: When trying to find record to delete, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  233. } else {
  234. $delete_matches[$field] = $results[0];
  235. }
  236. }
  237. else {
  238. $delete_matches[$field] = $value;
  239. }
  240. }
  241. // now build the SQL statement
  242. $sql = "DELETE FROM {$table} WHERE ";
  243. $dargs = array();
  244. foreach($delete_matches as $field => $value){
  245. if(strcmp($fields[$field]['type'],'serial')==0 or
  246. strcmp($fields[$field]['type'],'int')==0){
  247. $sql .= " $field = %d AND ";
  248. } else {
  249. $sql .= " $field = '%s' AND ";
  250. }
  251. array_push($dargs,$value);
  252. }
  253. $sql = substr($sql,0,-4); // get rid of the trailing 'AND'
  254. // finally perform the delete. If successful, return the updated record
  255. $previous_db = tripal_db_set_active('chado'); // use chado database
  256. $result = db_query($sql,$dargs);
  257. tripal_db_set_active($previous_db); // now use drupal database
  258. if($result){
  259. return true;
  260. }
  261. else {
  262. watchdog('tripal_core',"Cannot delete record in $table table. Match:" . print_r($match,1) . ". Values: ". print_r($values,1),array(),'WATCHDOG_ERROR');
  263. return false;
  264. }
  265. return false;
  266. }
  267. /**
  268. * Provides a generic routine for updating into any Chado table
  269. *
  270. * Use this function to update a record in any Chado table. The first
  271. * argument specifies the table for inserting, the second is an array
  272. * of values to matched for locating the record for updating, and the third
  273. * argument give the values to update. The arrays are mutli-dimensional such
  274. * that foreign key lookup values can be specified.
  275. *
  276. * @param $table
  277. * The name of the chado table for inserting
  278. * @param $match
  279. * An associative array containing the values for locating a record to update.
  280. * @param $values
  281. * An associative array containing the values for updating.
  282. *
  283. * @return
  284. * On success this function returns TRUE. On failure, it returns FALSE.
  285. *
  286. * Example usage:
  287. * @code
  288. $umatch = array(
  289. 'organism_id' => array(
  290. 'genus' => 'Citrus',
  291. 'species' => 'sinensis',
  292. ),
  293. 'uniquename' => 'orange1.1g000034m.g7',
  294. 'type_id' => array (
  295. 'cv_id' => array (
  296. 'name' => 'sequence',
  297. ),
  298. 'name' => 'gene',
  299. 'is_obsolete' => 0
  300. ),
  301. );
  302. $uvalues = array(
  303. 'name' => 'orange1.1g000034m.g',
  304. 'type_id' => array (
  305. 'cv_id' => array (
  306. 'name' => 'sequence',
  307. ),
  308. 'name' => 'mRNA',
  309. 'is_obsolete' => 0
  310. ),
  311. );
  312. * $result = tripal_core_chado_update('feature',$umatch,$uvalues);
  313. * @endcode
  314. * The above code species that a feature with a given uniquename, organism_id,
  315. * and type_id (the unique constraint for the feature table) will be updated.
  316. * The organism_id is specified as a nested array that uses the organism_id
  317. * foreign key constraint to lookup the specified values to find the exact
  318. * organism_id. The same nested struture is also used for specifying the
  319. * values to update. The function will find the record that matches the
  320. * columns specified and update the record with the avlues in the $uvalues array.
  321. *
  322. * @ingroup tripal_api
  323. */
  324. function tripal_core_chado_update($table,$match,$values){
  325. $update_values = array(); // contains the values to be updated
  326. $update_matches = array(); // contains the values for the where clause
  327. // get the table description
  328. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  329. // get the values needed for matching in the SQL statement
  330. foreach ($match as $field => $value){
  331. if(is_array($value)){
  332. $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
  333. if (sizeof($results) > 1) {
  334. watchdog('tripal_core', 'tripal_core_chado_update: When trying to find record to update, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  335. } elseif (sizeof($results) < 1) {
  336. watchdog('tripal_core', 'tripal_core_chado_update: When trying to find record to update, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  337. } else {
  338. $update_matches[$field] = $results[0];
  339. }
  340. }
  341. else {
  342. $update_matches[$field] = $value;
  343. }
  344. }
  345. // get the values used for updating
  346. foreach ($values as $field => $value){
  347. if(is_array($value)){
  348. $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
  349. if (sizeof($results) > 1) {
  350. watchdog('tripal_core', 'tripal_core_chado_update: When trying to find update values, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  351. } elseif (sizeof($results) < 1) {
  352. watchdog('tripal_core', 'tripal_core_chado_update: When trying to find update values, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
  353. } else {
  354. $update_values[$field] = $results[0];
  355. }
  356. }
  357. else {
  358. $update_values[$field] = $value;
  359. }
  360. }
  361. // now build the SQL statement
  362. $sql = "UPDATE {$table} SET ";
  363. $fields = $table_desc['fields'];
  364. $uargs = array();
  365. foreach($update_values as $field => $value){
  366. if(strcmp($fields[$field]['type'],'serial')==0 or
  367. strcmp($fields[$field]['type'],'int')==0){
  368. $sql .= " $field = %d, ";
  369. } else {
  370. $sql .= " $field = '%s', ";
  371. }
  372. array_push($uargs,$value);
  373. }
  374. $sql = substr($sql,0,-2); // get rid of the trailing comma & space
  375. $sql .= " WHERE ";
  376. foreach($update_matches as $field => $value){
  377. if(strcmp($fields[$field]['type'],'serial')==0 or
  378. strcmp($fields[$field]['type'],'int')==0){
  379. $sql .= " $field = %d AND ";
  380. } else {
  381. $sql .= " $field = '%s' AND ";
  382. }
  383. array_push($uargs,$value);
  384. }
  385. $sql = substr($sql,0,-4); // get rid of the trailing 'AND'
  386. // finally perform the update. If successful, return the updated record
  387. $previous_db = tripal_db_set_active('chado'); // use chado database
  388. $result = db_query($sql,$uargs);
  389. tripal_db_set_active($previous_db); // now use drupal database
  390. if($result){
  391. return true;
  392. }
  393. else {
  394. watchdog('tripal_core',"Cannot update record in $table table. Match:" . print_r($match,1) . ". Values: ". print_r($values,1),array(),'WATCHDOG_ERROR');
  395. return false;
  396. }
  397. return false;
  398. }
  399. /**
  400. * Provides a generic routine for selecting data from a Chado table
  401. *
  402. * Use this function to perform a simple select from any Chado table.
  403. *
  404. * @param $table
  405. * The name of the chado table for inserting
  406. * @param $columns
  407. * An array of column names
  408. * @param $values
  409. * An associative array containing the values for filtering the results. In the
  410. * case where multiple values for the same time are to be selected an additional
  411. * entry for the field should appear for each value
  412. * @param $options
  413. * An associative array of additional options where the key is the option
  414. * and the value is the value of that option.
  415. *
  416. * Additional Options Include:
  417. * - has_record
  418. * Set this argument to 'true' to have this function return a numeric
  419. * value for the number of recrods rather than the array of records. this
  420. * can be useful in 'if' statements to check the presence of particula records.
  421. * - return_sql
  422. * Set this to 'true' to have this function return an array where the first element is the sql
  423. * that would have been run and the second is an array of arguments.
  424. * - case_insensitive_columns
  425. * An array of columns to do a case insensitive search on.
  426. * - regex_columns
  427. * An array of columns where the value passed in should be treated as a regular expression
  428. *
  429. * @return
  430. * A database query result resource, FALSE if the query was not executed
  431. * correctly, or the number of records in the dataset if $has_record is set.
  432. *
  433. * Example usage:
  434. * @code
  435. * $columns = array('feature_id','name');
  436. * $values = array(
  437. * 'organism_id' => array(
  438. * 'genus' => 'Citrus',
  439. * 'species' => array('sinensis','clementina'),
  440. * ),
  441. * 'uniquename' => 'orange1.1g000034m.g',
  442. * 'type_id' => array (
  443. * 'cv_id' => array (
  444. * 'name' => 'sequence',
  445. * ),
  446. * 'name' => 'gene',
  447. * 'is_obsolete' => 0
  448. * ),
  449. * );
  450. * $result = tripal_core_chado_select('feature',$columns,$values);
  451. * @endcode
  452. * The above code selects a record from the feature table using the three fields
  453. * that uniquely identify a feature. The $columns array simply lists the columns
  454. * to select. The $values array is nested such that the organism is identified by
  455. * way of the organism_id foreign key constraint by specifying the genus and
  456. * species. The cvterm is also specified using its foreign key and the cv_id
  457. * for the cvterm is nested as well. In the example above, two different species
  458. * are allowed to match
  459. *
  460. * @ingroup tripal_api
  461. */
  462. function tripal_core_chado_select($table,$columns,$values,$options = null){
  463. if (!is_array($options)) { $options = array(); }
  464. if (!$options['case_insensitive_columns']) { $options['case_insensitive_columns'] = array(); }
  465. if (!$options['regex_columns']) { $options['regex_columns'] = array(); }
  466. if (!is_array($columns)){
  467. watchdog('tripal_feature', 'the $columns argument for tripal_core_chado_select must be an array.');
  468. return false;
  469. }
  470. if (!is_array($values)){
  471. watchdog('tripal_feature', 'the $values argument for tripal_core_chado_select must be an array.');
  472. return false;
  473. }
  474. // get the table description
  475. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  476. $select = '';
  477. $from = '';
  478. $where = '';
  479. $args = array();
  480. foreach($values as $field => $value){
  481. $select[] = $field;
  482. if(is_array($value)){
  483. // if the user has specified multiple values for matching then this we
  484. // want to catch that and save them in our $where array, otherwise
  485. // we'll descend for a foreign key relationship
  486. if(array_values($value) === $value){
  487. $where[$field] = $value;
  488. } else {
  489. // select the value from the foreign key relationship for this value
  490. $foreign_options = array(
  491. 'regex_columns' => $options['regex_columns'],
  492. 'case_insensitive_columns' => $options['case_insensitive_columns']
  493. );
  494. $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value, $foreign_options);
  495. if (sizeof($results) < 1) {
  496. // foreign key records are required
  497. // thus if none matched then return false and alert the admin through watchdog
  498. watchdog('tripal_core',
  499. 'tripal_core_chado_select: no record in the table referenced by the foreign key (!field) exists. tripal_core_chado_select table=!table, columns=!columns, values=!values',
  500. array('!table' => $table,
  501. '!columns' => '<pre>' . print_r($columns, TRUE) . '</pre>',
  502. '!values' => '<pre>' . print_r($values, TRUE) . '</pre>',
  503. '!field' => $field,
  504. ),
  505. WATCHDOG_WARNING);
  506. return false;
  507. } else {
  508. $where[$field] = $results;
  509. }
  510. }
  511. }
  512. else {
  513. //need to catch a 0 and make int if integer field
  514. if ($table_desc['fields'][$field]['type'] == 'int') {
  515. $where[$field][] = (int) $value;
  516. } else {
  517. $where[$field][] = $value;
  518. }
  519. }
  520. }
  521. // now build the SQL select statement
  522. if (empty($where)) {
  523. // sometimes want to select everything
  524. $sql = "SELECT " . implode(',',$columns) . " ";
  525. $sql .= "FROM {$table} ";
  526. } else {
  527. $sql = "SELECT " . implode(',',$columns) . " ";
  528. $sql .= "FROM {$table} ";
  529. $sql .= "WHERE ";
  530. foreach($where as $field => $value){
  531. if (count($value) > 1) {
  532. $sql .= "$field IN (".db_placeholders($value,'varchar').") AND ";
  533. foreach ($value as $v) { $args[] = $v; }
  534. } else {
  535. $operator = '=';
  536. if (in_array($field, $options['regex_columns'])) {
  537. $operator = '~*';
  538. }
  539. if (in_array($field, $options['case_insensitive_columns'])) {
  540. $sql .= "lower($field) $operator lower('%s') AND ";
  541. $args[] = $value[0];
  542. } else {
  543. $sql .= "$field $operator '%s' AND ";
  544. $args[] = $value[0];
  545. }
  546. }
  547. }
  548. $sql = substr($sql,0,-4); // get rid of the trailing 'AND'
  549. }
  550. // if the caller has requested the SQL rather than the results...
  551. // which happens in the case of wanting to use the Drupal pager, then do so
  552. if($options['return_sql']){
  553. return array('sql'=> $sql, 'args' => $args);
  554. }
  555. $previous_db = tripal_db_set_active('chado'); // use chado database
  556. $resource = db_query($sql,$args);
  557. tripal_db_set_active($previous_db); // now use drupal database
  558. $results = array();
  559. while ($r = db_fetch_object($resource)) {
  560. $results[] = $r;
  561. }
  562. if(!$options['has_record']){
  563. return $results;
  564. } else{
  565. return count($results);
  566. }
  567. }
  568. /**
  569. * Gets the value of a foreign key relationship
  570. *
  571. * This function is used by tripal_core_chado_select, tripal_core_chado_insert,
  572. * and tripal_core_chado_update to iterate through the associate array of
  573. * values that gets passed to each of those routines. The values array
  574. * is nested where foreign key contraints are used to specify a value that. See
  575. * documentation for any of those functions for further information.
  576. *
  577. * @param $table_desc
  578. * A table description for the table with the foreign key relationship to be identified generated by
  579. * hook_chado_<table name>_schema()
  580. * @param $field
  581. * The field in the table that is the foreign key.
  582. * @param $values
  583. * An associative array containing the values
  584. *
  585. * @return
  586. * A string containg the results of the foreign key lookup, or FALSE if failed.
  587. *
  588. * Example usage:
  589. * @code
  590. *
  591. * $values = array(
  592. * 'genus' => 'Citrus',
  593. * 'species' => 'sinensis',
  594. * );
  595. * $value = tripal_core_chado_get_foreign_key('feature','organism_id',$values);
  596. *
  597. * @endcode
  598. * The above code selects a record from the feature table using the three fields
  599. * that uniquely identify a feature. The $columns array simply lists the columns
  600. * to select. The $values array is nested such that the organism is identified by
  601. * way of the organism_id foreign key constraint by specifying the genus and
  602. * species. The cvterm is also specified using its foreign key and the cv_id
  603. * for the cvterm is nested as well.
  604. *
  605. * @ingroup tripal_api
  606. */
  607. function tripal_core_chado_get_foreign_key($table_desc,$field,$values, $options = null){
  608. if (!is_array($options)) { $options = array(); }
  609. if (!$options['case_insensitive_columns']) { $options['case_insensitive_columns'] = array(); }
  610. if (!$options['regex_columns']) { $options['regex_columns'] = array(); }
  611. // get the list of foreign keys for this table description and
  612. // iterate through those until we find the one we're looking for
  613. $fkeys = $table_desc['foreign keys'];
  614. if($fkeys){
  615. foreach($fkeys as $name => $def){
  616. if (is_array($def['table'])) {
  617. //foreign key was described 2X
  618. $message = "The foreign key ".$name." was defined twice. Please check modules to determine if hook_chado_".$table_desc['table']."_schema() was implemented and defined this foreign key when it wasn't supposed to. Modules this hook was implemented in: ".implode(', ', module_implements("chado_".$table_desc['table']."_schema")).".";
  619. watchdog('tripal_core', $message);
  620. drupal_set_message($message,'error');
  621. continue;
  622. }
  623. $table = $def['table'];
  624. $columns = $def['columns'];
  625. // iterate through the columns of the foreign key relationship
  626. foreach($columns as $left => $right){
  627. // does the left column in the relationship match our field?
  628. if(strcmp($field,$left)==0){
  629. // the column name of the foreign key matches the field we want
  630. // so this is the right relationship. Now we want to select
  631. $select_cols = array($right);
  632. $result = tripal_core_chado_select($table,$select_cols,$values, $options);
  633. $fields = array();
  634. foreach ($result as $obj) {
  635. $fields[] = $obj->$right;
  636. }
  637. return $fields;
  638. }
  639. }
  640. }
  641. }
  642. else {
  643. // TODO: what do we do if we get to this point and we have a fk
  644. // relationship expected but we don't have any definition for one in the
  645. // table schema??
  646. $message = "There is no foreign key relationship defined for ".$field.". To define a foreign key relationship, determine the table this foreign key referrs to (<foreign table>) and then implement hook_chado_<foreign table>_schema(). See tripal_feature_chado_feature_schema for an example.";
  647. watchdog('tripal_core', $message);
  648. drupal_set_message($message,'error');
  649. }
  650. return false;
  651. }
  652. /**
  653. * Generates an object containing the full details of a record(s) in chado.
  654. *
  655. * @todo Add expand_tables arrays
  656. *
  657. * This differs from the objects returned by tripal_core_chado_select in so far as all foreign key
  658. * relationships have been followed meaning you have more complete details. Thus this function
  659. * should be used whenever you need a full variable and tripal_core_chado_select should be used if
  660. * you only case about a few columns.
  661. *
  662. * @param $table
  663. * The name of the base table to generate a variable for
  664. * @param $values
  665. * A select values array that selects the records you want from the base table
  666. * (this has the same form as tripal_core_chado_select)
  667. * @return
  668. * Either an object (if only one record was selected from the base table)
  669. * or an array of objects (if more than one record was selected from the base table).
  670. *
  671. * Example Usage:
  672. * @code
  673. $values = array(
  674. 'name' => 'Medtr4g030710'
  675. );
  676. $features = tripal_core_generate_chado_var('feature', $values);
  677. * @endcode
  678. * This will return an object if there is only one feature with the name Medtr4g030710 or it will
  679. * return an array of feature objects if more than one feature has that name.
  680. *
  681. * Note to Module Designers: Fields can be excluded by default from these objects by implementing
  682. * one of the following hooks:
  683. * - hook_exclude_field_from_tablename_by_default (where tablename is the name of the table):
  684. * This hook allows you to add fields to be excluded on a per table basis. Simply implement
  685. * this hook to return an array of fields to be excluded. For example:
  686. * @code
  687. mymodule_exclude_field_from_feature_by_default() {
  688. return array('residues' => TRUE);
  689. }
  690. * @endcode
  691. * will ensure that feature.residues is ecluded from a feature object by default.
  692. * - hook_exclude_type_by_default:
  693. * This hook allows you to exclude fields from all tables that are of a given postgresql field
  694. * type. Simply implement this hook to return an array of postgresql types mapped to criteria.
  695. * Then all fields of that type where the criteria supplied returns TRUE will be excluded from
  696. * any table. Tokens available in criteria are &gt;field_value&lt; and &gt;field_name&lt; . For example:
  697. * @code
  698. mymodule_exclude_type_by_default() {
  699. return array('text' => 'length(&gt;field_value&lt; ) > 50');
  700. }
  701. * @endcode
  702. * will exclude all text fields with a length > 50. Thus if $feature.residues is longer than 50 * it will be excluded, otherwise it will be added.
  703. *
  704. * @ingroup tripal_api
  705. */
  706. function tripal_core_generate_chado_var($table, $values) {
  707. // get description for the current table----------------------------------------------------------
  708. $table_desc = module_invoke_all('chado_'.$table.'_schema');
  709. $table_primary_key = $table_desc['primary key'][0];
  710. $table_columns = array_keys($table_desc['fields']);
  711. // Expandable fields without value needed for criteria--------------------------------------------
  712. $all->expandable_fields = array();
  713. if ($table_desc['referring_tables']) {
  714. $all->expandable_tables = $table_desc['referring_tables'];
  715. } else {
  716. $all->expandable_tables = array();
  717. }
  718. $all->expandable_nodes = array();
  719. // Get fields to be removed by name.................................
  720. $fields_to_remove = module_invoke_all('exclude_field_from_'.$table.'_by_default');
  721. foreach ($fields_to_remove as $field_name => $criteria) {
  722. //replace &gt;field_name&lt; with the current field name &
  723. $criteria = preg_replace('/&gt;field_name&lt; /', $field_name, $criteria);
  724. // if field_value needed we can't deal with this field yet
  725. if (preg_match('/&gt;field_value&lt; /', $criteria)) { break; }
  726. //if criteria then remove from query
  727. $success = drupal_eval('<?php return '.$criteria.'; ?>');
  728. // watchdog('tripal_core',
  729. // 'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for %table evaluated to %success',
  730. // array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
  731. // WATCHDOG_NOTICE
  732. // );
  733. if ($success) {
  734. unset($table_columns[array_search($field_name, $table_columns)]);
  735. unset($fields_to_remove[$field_name]);
  736. $all->expandable_fields[] = $table . '.' . $field_name;
  737. }
  738. }
  739. //Get fields to be removed by type................................
  740. $types_to_remove = module_invoke_all('exclude_type_by_default');
  741. $field_types = array();
  742. foreach ($table_desc['fields'] as $field_name => $field_array) {
  743. $field_types[$field_array['type']][] = $field_name;
  744. }
  745. foreach ($types_to_remove as $field_type => $criteria) {
  746. // if there are fields of that type to remove
  747. if (is_array($field_types[$field_type])) {
  748. //replace &gt;field_name&lt; with the current field name &
  749. $criteria = preg_replace('/&gt;field_name&lt; /', $field_name, $criteria);
  750. foreach ($field_types[$field_type] as $field_name) {
  751. // if field_value needed we can't deal with this field yet
  752. if (preg_match('/&gt;field_value&lt; /', $criteria)) {
  753. $fields_to_remove[$field_name] = $criteria;
  754. continue;
  755. }
  756. // if field_value needed we can't deal with this field yet
  757. if (preg_match('/&gt;field_value&lt; /', $criteria)) { break; }
  758. //if criteria then remove from query
  759. $success = drupal_eval('<?php return '.$criteria.'; ?>');
  760. // watchdog('tripal_core',
  761. // 'Evaluating criteria (%criteria) for field %field of $type in tripal_core_generate_chado_var for %table evaluated to %success',
  762. // array('%table'=>$table, '%criteria'=>$criteria, '%field'=>$field_name, '%type'=>$field_type, '%success'=>$success),
  763. // WATCHDOG_NOTICE
  764. // );
  765. if ($success) {
  766. unset($table_columns[array_search($field_name, $table_columns)]);
  767. $all->expandable_fields[] = $table . '.' . $field_name;
  768. }
  769. } //end of foreach field of that type
  770. }
  771. } //end of foreach type to be removed
  772. // get the values for the record in the current table---------------------------------------------
  773. $results = tripal_core_chado_select($table, $table_columns, $values);
  774. foreach ($results as $key => $object) {
  775. // Add empty expandable_x arrays
  776. $object->expandable_fields = $all->expandable_fields;
  777. $object->expandable_tables = $all->expandable_tables;
  778. $object->expandable_nodes = $all->expandable_nodes;
  779. // add curent table
  780. $object->tablename = $table;
  781. // check if the current table maps to a node type-----------------------------------------------
  782. // if this table is connected to a node there will be a chado_tablename table in drupal
  783. if (db_table_exists('chado_'.$table)) {
  784. // that has a foreign key to this one ($table_desc['primary key'][0]
  785. // and to the node table (nid)
  786. $sql = "SELECT %s, nid FROM chado_%s WHERE %s=%d";
  787. $mapping = db_fetch_object(db_query(
  788. $sql,
  789. $table_primary_key,
  790. $table,
  791. $table_primary_key,
  792. $object->{$table_primary_key}
  793. ));
  794. if ($mapping->{$table_primary_key}) {
  795. $object->nid = $mapping->nid;
  796. $object->expandable_nodes[] = $table;
  797. }
  798. }
  799. // remove any fields where criteria need to be evalulated---------------------------------------
  800. foreach ($fields_to_remove as $field_name => $criteria) {
  801. if (!isset($object->{$field_name})) { break; }
  802. $criteria = preg_replace('/&gt;field_value&lt; /', $object->{$field_name}, $criteria);
  803. //if criteria then remove from query
  804. $success = drupal_eval('<?php return '.$criteria.'; ?>');
  805. // watchdog('tripal_core',
  806. // 'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for %table evaluated to %success',
  807. // array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
  808. // WATCHDOG_NOTICE
  809. // );
  810. if ($success) {
  811. unset($object->{$field_name});
  812. $object->expandable_fields[] = $table . '.' . $field_name;
  813. }
  814. }
  815. // recursively follow foreign key relationships nesting objects as we go------------------------
  816. foreach ($table_desc['foreign keys'] as $foreign_key_array) {
  817. $foreign_table = $foreign_key_array['table'];
  818. foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
  819. // Note: Foreign key is the field in the current table whereas primary_key is the field in
  820. // the table referenced by the foreign key
  821. //Dont do anything if the foreign key is empty
  822. if (empty($object->{$foreign_key})) {
  823. break;
  824. }
  825. // get the record from the foreign table
  826. $foreign_values = array($primary_key => $object->{$foreign_key});
  827. $foreign_object = tripal_core_generate_chado_var($foreign_table, $foreign_values);
  828. // add the foreign record to the current object in a nested manner
  829. $object->{$foreign_key} = $foreign_object;
  830. // Flatten expandable_x arrays so only in the bottom object
  831. if (is_array($object->{$foreign_key}->expandable_fields)) {
  832. $object->expandable_fields = array_merge(
  833. $object->expandable_fields,
  834. $object->{$foreign_key}->expandable_fields
  835. );
  836. unset($object->{$foreign_key}->expandable_fields);
  837. }
  838. if (is_array($object->{$foreign_key}->expandable_tables)) {
  839. $object->expandable_tables = array_merge(
  840. $object->expandable_tables,
  841. $object->{$foreign_key}->expandable_tables
  842. );
  843. unset($object->{$foreign_key}->expandable_tables);
  844. }
  845. if (is_array($object->{$foreign_key}->expandable_nodes)) {
  846. $object->expandable_nodes = array_merge(
  847. $object->expandable_nodes,
  848. $object->{$foreign_key}->expandable_nodes
  849. );
  850. unset($object->{$foreign_key}->expandable_nodes);
  851. }
  852. }
  853. }
  854. $results[$key] = $object;
  855. }
  856. // check only one result returned
  857. if (sizeof($results) == 1) {
  858. // add results to object
  859. return $results[0];
  860. } elseif (!empty($results)) {
  861. return $results;
  862. } else {
  863. // no results returned
  864. }
  865. }
  866. /**
  867. * Retrieves fields/tables/nodes that were excluded by default from a variable and adds them
  868. *
  869. * This function exists to allow tripal_core_generate_chado_var() to excldue some
  870. * fields/tables/nodes from the default form of a variable without making it extremely difficult for
  871. * the tripal admin to get at these variables if he/she wants them.
  872. *
  873. * @param $object
  874. * This must be an object generated using tripal_core_generate_chado_var()
  875. * @param $type
  876. * Must be one of 'field', 'table', 'node'. Indicates what is being expanded.
  877. * @param $to_expand
  878. * The name of the field/table/node to be expanded
  879. *
  880. * @return
  881. * A chado object supplemented with the field/table/node requested to be expanded
  882. *
  883. * Example Usage:
  884. * @code
  885. // Get a chado object to be expanded
  886. $values = array(
  887. 'name' => 'Medtr4g030710'
  888. );
  889. $features = tripal_core_generate_chado_var('feature', $values);
  890. // Expand the organism node
  891. $feature = tripal_core_expand_chado_vars($feature, 'node', 'organism');
  892. // Expand the feature.residues field
  893. $feature = tripal_core_expand_chado_vars($feature, 'field', 'feature.residues');
  894. // Expand the feature properties (featureprop table)
  895. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureprop');
  896. * @endcode
  897. *
  898. * @ingroup tripal_api
  899. */
  900. function tripal_core_expand_chado_vars ($object, $type, $to_expand) {
  901. $base_table = $object->tablename;
  902. switch ($type) {
  903. case "field": //--------------------------------------------------------------------------------
  904. if (preg_match('/(\w+)\.(\w+)/', $to_expand, $matches)) {
  905. $tablename = $matches[1];
  906. $fieldname = $matches[2];
  907. $table_desc = module_invoke_all('chado_'.$tablename.'_schema');
  908. $values = array();
  909. foreach($table_desc['primary key'] as $key) {
  910. $values[$key] = $object->{$key};
  911. }
  912. if ($base_table == $tablename) {
  913. //get the field
  914. $results = tripal_core_chado_select(
  915. $tablename,
  916. array($fieldname),
  917. $values
  918. );
  919. $object->{$fieldname} = $results[0]->{$fieldname};
  920. $object->expanded = $to_expand;
  921. } else {
  922. //We need to recurse -the field is in a nested object
  923. foreach ((array) $object as $field_name => $field_value) {
  924. if (is_object($field_value)) {
  925. $object->{$field_name} = tripal_core_expand_chado_vars(
  926. $field_value,
  927. 'field',
  928. $to_expand
  929. );
  930. }
  931. } //end of for each field in the current object
  932. }
  933. } else {
  934. watchdog(
  935. 'tripal_core',
  936. 'tripal_core_expand_chado_vars: Field (%field) not in the right format. It should be <tablename>.<fieldname>',
  937. WATCHDOG_ERROR
  938. );
  939. }
  940. break;
  941. case "table": //--------------------------------------------------------------------------------
  942. $foreign_table = $to_expand;
  943. $foreign_table_desc = module_invoke_all('chado_'.$foreign_table.'_schema');
  944. // If it's connected to the base table
  945. if ($foreign_table_desc['foreign keys'][$base_table]) {
  946. foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
  947. if (!$object->{$right}) { break; }
  948. $foreign_object = tripal_core_generate_chado_var(
  949. $foreign_table,
  950. array($left => $object->{$right})
  951. );
  952. if ($foreign_object) {
  953. $object->{$foreign_table} = $foreign_object;
  954. $object->expanded = $to_expand;
  955. }
  956. }
  957. } else {
  958. //We need to recurse -the table has a relationship to one of the nested objects
  959. foreach ((array) $object as $field_name => $field_value) {
  960. if (is_object($field_value)) {
  961. $object->{$field_name} = tripal_core_expand_chado_vars(
  962. $field_value,
  963. 'table',
  964. $foreign_table
  965. );
  966. }
  967. }
  968. }
  969. break;
  970. case "node": //---------------------------------------------------------------------------------
  971. //if the node to be expanded is for our base table, then just expand it
  972. if ($object->tablename == $to_expand) {
  973. $node = node_load($object->nid);
  974. if ($node) {
  975. $object->expanded = $to_expand;
  976. $node->expandable_fields = $object->expandable_fields;
  977. unset($object->expandable_fields);
  978. $node->expandable_tables = $object->expandable_tables;
  979. unset($object->expandable_tables);
  980. $node->expandable_nodes = $object->expandable_nodes;
  981. unset($object->expandable_nodes);
  982. $node->{$base_table} = $object;
  983. $object = $node;
  984. } else {
  985. watchdog(
  986. 'tripal_core',
  987. 'tripal_core_expand_chado_vars: No node matches the nid (%nid) supplied.',
  988. array('%nid'=>$object->nid),
  989. WATCHDOG_ERROR
  990. );
  991. } //end of if node
  992. } else {
  993. //We need to recurse -the node to expand is one of the nested objects
  994. foreach ((array) $object as $field_name => $field_value) {
  995. if (is_object($field_value)) {
  996. $object->{$field_name} = tripal_core_expand_chado_vars(
  997. $field_value,
  998. 'node',
  999. $to_expand
  1000. );
  1001. }
  1002. } //end of for each field in the current object
  1003. }
  1004. break;
  1005. default:
  1006. watchdog('tripal_core',
  1007. 'tripal_core_expand_chado_vars: Unrecognized type (%type). Should be one of "field", "table", "node".',
  1008. array('%type'=>$type),
  1009. WATCHDOG_ERROR
  1010. );
  1011. return FALSE;
  1012. }
  1013. //move extended array downwards-------------------------------------------------------------------
  1014. if (!$object->expanded) {
  1015. //if there's no extended field then go hunting for it
  1016. foreach ( (array)$object as $field_name => $field_value) {
  1017. if (is_object($field_value)) {
  1018. if (isset($field_value->expanded)) {
  1019. $object->expanded = $field_value->expanded;
  1020. unset($field_value->expanded);
  1021. }
  1022. }
  1023. }
  1024. }
  1025. //try again becasue now we might have moved it down
  1026. if ($object->expanded) {
  1027. $expandable_name = 'expandable_'.$type.'s';
  1028. if ($object->{$expandable_name}) {
  1029. $key_to_remove = array_search($object->expanded, $object->{$expandable_name});
  1030. unset($object->{$expandable_name}[$key_to_remove]);
  1031. unset($object->expanded);
  1032. } else {
  1033. // if there is an expandable array then we've reached the base object
  1034. // if we get here and don't have anything expanded then something went wrong
  1035. // watchdog(
  1036. // 'tripal_core',
  1037. // 'tripal_core_expand_chado_vars: Unable to expand the %type %to_expand',
  1038. // array('%type'=>$type, '%to_expand'=>$to_expand),
  1039. // WATCHDOG_ERROR
  1040. // );
  1041. } //end of it we've reached the base object
  1042. }
  1043. return $object;
  1044. }
  1045. /**
  1046. * Implements hook_exclude_type_by_default()
  1047. *
  1048. * This hooks allows fields of a specified type that match a specified criteria to be excluded by
  1049. * default from any table when tripal_core_generate_chado_var() is called. Keep in mind that if
  1050. * fields are excluded by default they can always be expanded at a later date using
  1051. * tripal_core_expand_chado_vars().
  1052. *
  1053. * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
  1054. * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
  1055. * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
  1056. * contain the following tokens:
  1057. * - &gt;field_name&lt;
  1058. * Replaced by the name of the field to be excluded
  1059. * - &gt;field_value&lt;
  1060. * Replaced by the value of the field in the current record
  1061. * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt; token then it will be
  1062. * evaluated before the query is executed and if the field is excluded it won't be included in the
  1063. * query.
  1064. *
  1065. * @return
  1066. * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  1067. *
  1068. * @ingroup tripal_api
  1069. */
  1070. function tripal_core_exclude_type_by_default() {
  1071. return array('text' => "strlen('&gt;field_value&lt; ') > 100");
  1072. }
  1073. /**
  1074. * Implements hook_exclude_field_from_<tablename>_by_default()
  1075. *
  1076. * This hooks allows fields from a specified table that match a specified criteria to be excluded by
  1077. * default from any table when tripal_core_generate_chado_var() is called. Keep in mind that if
  1078. * fields are excluded by default they can always be expanded at a later date using
  1079. * tripal_core_expand_chado_vars().
  1080. *
  1081. * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
  1082. * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
  1083. * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
  1084. * contain the following tokens:
  1085. * - &gt;field_name&lt;
  1086. * Replaced by the name of the field to be excluded
  1087. * - &gt;field_value&lt;
  1088. * Replaced by the value of the field in the current record
  1089. * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt; token then it will be
  1090. * evaluated before the query is executed and if the field is excluded it won't be included in the
  1091. * query.
  1092. *
  1093. * @return
  1094. * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  1095. *
  1096. * @ingroup tripal_api
  1097. */
  1098. function tripal_core_exclude_field_from_feature_by_default() {
  1099. return array();
  1100. }
  1101. /************************************************************************
  1102. * Use this function instead of db_query() to avoid switching databases
  1103. * when making query to the chado database
  1104. */
  1105. function chado_query($sql) {
  1106. $args = func_get_args();
  1107. array_shift($args);
  1108. $sql = db_prefix_tables($sql);
  1109. if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
  1110. $args = $args[0];
  1111. }
  1112. _db_query_callback($args, TRUE);
  1113. $sql = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $sql);
  1114. $previous_db = db_set_active('chado');
  1115. $results = _db_query($sql);
  1116. db_set_active($previous_db);
  1117. return $results;
  1118. }
  1119. /************************************************************************
  1120. * Get chado id for a node. E.g, if you want to get 'analysis_id' from the
  1121. * 'analysis' table for a synced 'chado_analysis' node, use:
  1122. * $analysis_id = chado_get_id_for_node ('analysis', $node)
  1123. * Likewise,
  1124. * $organism_id = chado_get_id_for_node ('organism', $node)
  1125. * $feature_id = chado_get_id_for_node ('feature', $node)
  1126. */
  1127. function chado_get_id_for_node ($table, $node) {
  1128. return db_result(db_query("SELECT $table"."_id FROM {chado_".$table."} WHERE nid = $node->nid"));
  1129. }
  1130. /************************************************************************
  1131. * Get node id for a chado feature/organism/analysis. E.g, if you want to
  1132. * get the node id for an analysis, use:
  1133. * $nid = chado_get_node_id ('analysis', $analysis_id)
  1134. * Likewise,
  1135. * $nid = chado_get_node_id ('organism', $organism_id)
  1136. * $nid = chado_get_node_id ('feature', $feature_id)
  1137. */
  1138. function chado_get_node_id ($table, $id) {
  1139. return db_result(db_query("SELECT nid FROM {chado_".$table."} WHERE $table"."_id = $id"));
  1140. }