tripal_core.api.inc 48 KB

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