tripal_core.api.inc 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604
  1. <?php
  2. require_once "tripal_core.schema_v1.2.api.inc";
  3. require_once "tripal_core.schema_v1.11.api.inc";
  4. /**
  5. * @file
  6. * The Tripal Core API
  7. *
  8. * This file provides the API needed for all other Tripal and Tripal dependent
  9. * modules.
  10. *
  11. * @defgroup tripal_api Tripal API
  12. * @{
  13. * Provides an application programming interface (API) for Tripal
  14. *
  15. * The Tripal API currently provides generic insert/update/select functions for all chado content as
  16. * well as some module specific functions that insert/update/delete/select specific chado content.
  17. *
  18. * This API is currently in its infancy and some necessary functions might be missing. If you find
  19. * a missing function that you think should be included go to the sourceforge feature request
  20. * page and request it's inclusion in the API. Such feature requests with a working function
  21. * definition will be given priority.
  22. * @}
  23. *
  24. * @defgroup tripal_chado_api Core Module Chado API
  25. * @{
  26. * Provides an application programming interface (API) to manage data withing the Chado database.
  27. * This includes functions for selecting, inserting, updating and deleting records
  28. * in Chado tables. The functions will ensure proper integrity contraints are met
  29. * for inserts and updates.
  30. *
  31. * Also, a set of functions is provided for creating template variables. First,
  32. * is the tripal_core_generate_chado_vars which is used to select one ore more
  33. * records from a table and return an array with foreign key relationships fully
  34. * populated. For example, if selecting a feature, the organism_id and type_id
  35. * would be present in the returned array as a nested array with their respective
  36. * foreign keys also nested. The only fields that are not included are text
  37. * fields (which may be very large) or many-to-many foreign key relationships.
  38. * However, these fields and relationships can be expanded using the
  39. * tripal_core_expand_chado_vars.
  40. *
  41. * When a row from a chado table is selected using these two functions, it provides
  42. * a way for users who want to cutomize Drupal template files to access all data
  43. * associate with a specific record.
  44. *
  45. * Finally, the property tables in Chado generally follow the same format. Therefore
  46. * there is a set of functions for inserting, updating and deleting properties for
  47. * any table. This provides quick lookup of properties (provided the CV term is
  48. * known).
  49. *
  50. * @}
  51. * @ingroup tripal_api
  52. *
  53. * @defgroup tripal_files_api Core Module Files API
  54. * @{
  55. * Provides an application programming interface (API) for managing files within
  56. * the Tripal data directory structure.
  57. *
  58. * @}
  59. * @ingroup tripal_api
  60. /**
  61. * Provides a generic routine for inserting into any Chado table
  62. *
  63. * Use this function to insert a record into any Chado table. The first
  64. * argument specifies the table for inserting and the second is an array
  65. * of values to be inserted. The array is mutli-dimensional such that
  66. * foreign key lookup values can be specified.
  67. *
  68. * @param $table
  69. * The name of the chado table for inserting
  70. * @param $values
  71. * An associative array containing the values for inserting.
  72. * @param $options
  73. * An array of options such as:
  74. * - statement_name: the name of the prepared statement to use. If the statement
  75. * has not yet been prepared it will be prepared automatically. On subsequent
  76. * calls with the same statement_name only an execute on the previously
  77. * prepared statement will occur.
  78. * - is_prepared: TRUE or FALSE. Whether or not the statement is prepared. By
  79. * default if the statement is not prepared it will be automatically.
  80. * However to avoid this check, which requires a database query you can
  81. * set this value to true and the check will not be performed.
  82. * - skip_validation: TRUE or FALSE. If TRUE will skip all the validation steps and
  83. * just try to insert as is. This is much faster but results in unhandled
  84. * non user-friendly errors if the insert fails.
  85. * - return_record: by default, the function will return the record but with
  86. * the primary keys added after insertion. To simply return TRUE on success
  87. * set this option to FALSE
  88. *
  89. * @return
  90. * On success this function returns the inserted record with the new primary keys
  91. * add to the returned array. On failure, it returns FALSE.
  92. *
  93. * Example usage:
  94. * @code
  95. * $values = array(
  96. * 'organism_id' => array(
  97. * 'genus' => 'Citrus',
  98. * 'species' => 'sinensis',
  99. * ),
  100. * 'name' => 'orange1.1g000034m.g',
  101. * 'uniquename' => 'orange1.1g000034m.g',
  102. * 'type_id' => array (
  103. * 'cv_id' => array (
  104. * 'name' => 'sequence',
  105. * ),
  106. * 'name' => 'gene',
  107. * 'is_obsolete' => 0
  108. * ),
  109. * );
  110. * $result = tripal_core_chado_insert('feature',$values);
  111. * @endcode
  112. * The above code inserts a record into the feature table. The $values array is
  113. * nested such that the organism is selected by way of the organism_id foreign
  114. * key constraint by specifying the genus and species. The cvterm is also
  115. * specified using its foreign key and the cv_id for the cvterm is nested as
  116. * well.
  117. *
  118. * @ingroup tripal_chado_api
  119. */
  120. function tripal_core_chado_insert($table, $values, $options = array()) {
  121. // set defaults for options. If we don't set defaults then
  122. // we get memory leaks when we try to access the elements
  123. if (!is_array($options)) {
  124. $options = array();
  125. }
  126. if (!array_key_exists('is_prepared', $options)) {
  127. $options['is_prepared'] = FALSE;
  128. }
  129. if (!array_key_exists('statement_name', $options)) {
  130. $options['statement_name'] = FALSE;
  131. }
  132. if (!array_key_exists('skip_validation', $options)) {
  133. $options['skip_validation'] = FALSE;
  134. }
  135. if (!array_key_exists('return_record', $options)) {
  136. $options['return_record'] = TRUE;
  137. }
  138. $insert_values = array();
  139. // Determine plan of action
  140. if ($options['statement_name']) {
  141. // we have a prepared statment (or want to create one) so set $prepared = TRUE
  142. $prepared = TRUE;
  143. // we need to get a persistent connection. If one exists this function
  144. // will not recreate it, but if not it will create one and store it in
  145. // a Drupal variable for reuse later.
  146. $connection = tripal_db_persistent_chado();
  147. // if we cannot get a connection the abandon the prepared statement
  148. if (!$connection) {
  149. $prepared = FALSE;
  150. unset($options['statement_name']);
  151. }
  152. }
  153. else {
  154. //print "NO STATEMENT (insert): $table\n";
  155. //debug_print_backtrace();
  156. }
  157. if (array_key_exists('skip_validation', $options)) {
  158. $validate = !$options['skip_validation'];
  159. }
  160. else {
  161. $validate = TRUE;
  162. }
  163. // get the table description
  164. $table_desc = tripal_core_get_chado_table_schema($table);
  165. if (empty($table_desc)) {
  166. watchdog('tripal_core', 'tripal_core_chado_insert: There is no table description for !table_name', array('!table_name' => $table), WATCHDOG_WARNING);
  167. }
  168. // iterate through the values array and create a new 'insert_values' array
  169. // that has all the values needed for insert with all foreign relationsihps
  170. // resolved.
  171. foreach ($values as $field => $value) {
  172. // make sure the field is in the table description. If not then return an error
  173. // message
  174. if (!array_key_exists($field, $table_desc['fields'])) {
  175. watchdog('tripal_core', "tripal_core_chado_insert: The field '%field' does not exist ".
  176. "for the table '%table'. Cannot perform insert. Values: %array",
  177. array('%field' => $field, '%table' => $table, '%array' => print_r($values, 1)), WATCHDOG_ERROR);
  178. return FALSE;
  179. }
  180. if (is_array($value)) {
  181. $foreign_options = array();
  182. if ($options['statement_name']) {
  183. // add the fk relationship info to the prepared statement name so that
  184. // we can prepare the selects run by the recrusive tripal_core_chado_get_foreign_key
  185. // function.
  186. $fk_sname = "fk_" . $table . "_" . $field;
  187. foreach ($value as $k => $v) {
  188. $fk_sname .= substr($k, 0, 2);
  189. }
  190. $foreign_options['statement_name'] = $fk_sname;
  191. }
  192. // select the value from the foreign key relationship for this value
  193. $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
  194. if (sizeof($results) > 1) {
  195. 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);
  196. }
  197. elseif (sizeof($results) < 1) {
  198. //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);
  199. }
  200. else {
  201. $insert_values[$field] = $results[0];
  202. }
  203. }
  204. else {
  205. $insert_values[$field] = $value;
  206. }
  207. }
  208. if ($validate) {
  209. // check for violation of any unique constraints
  210. $ukeys = array();
  211. if (array_key_exists('unique keys', $table_desc)) {
  212. $ukeys = $table_desc['unique keys'];
  213. }
  214. $ukselect_cols = array();
  215. $ukselect_vals = array();
  216. if ($ukeys) {
  217. foreach ($ukeys as $name => $fields) {
  218. foreach ($fields as $index => $field) {
  219. // build the arrays for performing a select that will check the contraint
  220. $ukselect_cols[] = $field;
  221. if (!array_key_exists($field, $insert_values)) {
  222. if (array_key_exists('default', $table_desc['fields'][$field])) {
  223. $ukselect_vals[$field] = $table_desc['fields'][$field]['default'];
  224. }
  225. }
  226. else {
  227. $ukselect_vals[$field] = $insert_values[$field];
  228. }
  229. }
  230. // now check the constraint
  231. $coptions = array();
  232. if ($options['statement_name']) {
  233. $coptions = array('statement_name' => 'uqsel_' . $table . '_' . $name);
  234. }
  235. if (tripal_core_chado_select($table, $ukselect_cols, $ukselect_vals, $coptions)) {
  236. watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " .
  237. print_r($values, 1), array(), 'WATCHDOG_ERROR');
  238. return FALSE;
  239. }
  240. }
  241. }
  242. // if trying to insert a field that is the primary key, make sure it also is unique
  243. if (array_key_exists('primary key', $table_desc)) {
  244. $pkey = $table_desc['primary key'][0];
  245. if (array_key_exists($pkey, $insert_values)) {
  246. $coptions = array('statement_name' => 'pqsel_' . $table . '_' . $pkey);
  247. if (tripal_core_chado_select($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
  248. watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
  249. return FALSE;
  250. }
  251. }
  252. }
  253. // make sure required fields have a value
  254. if (!is_array($table_desc['fields'])) {
  255. $table_desc['fields'] = array();
  256. watchdog('tripal_core', "tripal_core_chado_insert: %table missing fields: \n %schema",
  257. array('%table' => $table, '%schema' => print_r($table_desc, 1)), WATCHDOG_WARNING);
  258. }
  259. foreach ($table_desc['fields'] as $field => $def) {
  260. // a field is considered missing if it cannot be NULL and there is no default
  261. // value for it or it is of type 'serial'
  262. if (array_key_exists('NOT NULL', $def) and
  263. !array_key_exists($field, $insert_values) and
  264. !array_key_exists('default', $def) and
  265. strcmp($def['type'], serial) != 0) {
  266. watchdog('tripal_core', "tripal_core_chado_insert: Field $table.$field cannot be NULL: " .
  267. print_r($values, 1), array(), 'WATCHDOG_ERROR');
  268. return FALSE;
  269. }
  270. }
  271. } //end of validation
  272. // Now build the insert SQL statement
  273. $ifields = array(); // contains the names of the fields
  274. $ivalues = array(); // contains the values of the fields
  275. $itypes = array(); // contains %d/%s placeholders for the sql query
  276. $iplaceholders = array(); // contains $1/$2 placeholders for the prepare query
  277. $idatatypes = array(); // contains the data type of the fields (int, text, etc.)
  278. $i = 1;
  279. foreach ($insert_values as $field => $value) {
  280. $ifields[] = $field;
  281. $ivalues[] = $value;
  282. $iplaceholders[] = '$' . $i;
  283. $i++;
  284. if (strcmp($value, '__NULL__')==0) {
  285. $itypes[] = "NULL";
  286. $idatatypes[] = "NULL";
  287. }
  288. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'serial')==0 OR
  289. strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
  290. strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
  291. $itypes[] = "%d";
  292. $idatatypes[] = 'int';
  293. }
  294. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'boolean')==0) {
  295. $itypes[] = "%s";
  296. $idatatypes[] = 'bool';
  297. }
  298. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'float')==0) {
  299. $itypes[] = "%s";
  300. $idatatypes[] = 'numeric';
  301. }
  302. else {
  303. $itypes[] = "'%s'";
  304. $idatatypes[] = 'text';
  305. }
  306. }
  307. // create the SQL
  308. $sql = "INSERT INTO {$table} (" . implode(", ", $ifields) . ") VALUES (" . implode(", ", $itypes) . ")";
  309. // if this is a prepared statement then execute it
  310. if ($prepared) {
  311. // if this is the first time we've run this query
  312. // then we need to do the prepare, otherwise just execute
  313. if ($options['is_prepared'] != TRUE) {
  314. // prepare the statement
  315. $psql = "PREPARE " . $options['statement_name'] . " (" . implode(', ', $idatatypes) . ") AS INSERT INTO {$table} (" . implode(", ", $ifields) . ") VALUES (" . implode(", ", $iplaceholders) . ")";
  316. $status = tripal_core_chado_prepare($options['statement_name'], $psql, $idatatypes);
  317. if (!$status) {
  318. watchdog('tripal_core', "tripal_core_chado_insert: not able to prepare '%name' statement for: %sql", array('%name' => $options['statement_name'], '%sql' => $sql), WATCHDOG_ERROR);
  319. return FALSE;
  320. }
  321. }
  322. $sql = "EXECUTE " . $options['statement_name'] . "(" . implode(", ", $itypes) . ")";
  323. $result = tripal_core_chado_execute_prepared($options['statement_name'], $sql, $ivalues);
  324. }
  325. // if it's not a prepared statement then insert normally
  326. else {
  327. $result = chado_query($sql, $ivalues);
  328. }
  329. // if we have a result then add primary keys to return array
  330. if ($options['return_record'] == TRUE and $result) {
  331. if (array_key_exists('primary key', $table_desc) and is_array($table_desc['primary key'])) {
  332. foreach ($table_desc['primary key'] as $field) {
  333. $sql = '';
  334. $psql = "PREPARE currval_" . $table . "_" . $field . " AS SELECT CURRVAL('" . $table . "_" . $field . "_seq')";
  335. $is_prepared = tripal_core_chado_prepare("currval_" . $table . "_" . $field, $psql, array());
  336. $value = '';
  337. if ($is_prepared) {
  338. $value = db_result(chado_query("EXECUTE currval_". $table . "_" . $field));
  339. if (!$value) {
  340. watchdog('tripal_core', "tripal_core_chado_insert: not able to retrieve primary key after insert: %sql",
  341. array('%sql' => $psql), WATCHDOG_ERROR);
  342. return FALSE;
  343. }
  344. }
  345. else {
  346. $sql = "SELECT CURRVAL('" . $table . "_" . $field . "_seq')";
  347. $value = db_result(chado_query($sql));
  348. if (!$value) {
  349. watchdog('tripal_core', "tripal_core_chado_insert: not able to retrieve primary key after insert: %sql",
  350. array('%sql' => $sql), WATCHDOG_ERROR);
  351. return FALSE;
  352. }
  353. }
  354. $values[$field] = $value;
  355. }
  356. }
  357. return $values;
  358. }
  359. elseif ($options['return_record'] == FALSE and $result) {
  360. return TRUE;
  361. }
  362. else {
  363. watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert record into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
  364. return FALSE;
  365. }
  366. return FALSE;
  367. }
  368. /**
  369. * Provides a generic routine for updating into any Chado table
  370. *
  371. * Use this function to update a record in any Chado table. The first
  372. * argument specifies the table for inserting, the second is an array
  373. * of values to matched for locating the record for updating, and the third
  374. * argument give the values to update. The arrays are mutli-dimensional such
  375. * that foreign key lookup values can be specified.
  376. *
  377. * @param $table
  378. * The name of the chado table for inserting
  379. * @param $match
  380. * An associative array containing the values for locating a record to update.
  381. * @param $values
  382. * An associative array containing the values for updating.
  383. * @param $options
  384. * An array of options such as:
  385. * - statement_name: the name of the prepared statement to use. If the statement
  386. * has not yet been prepared it will be prepared automatically. On subsequent
  387. * calls with the same statement_name only an execute on the previously
  388. * prepared statement will occur.
  389. * - is_prepared: TRUE or FALSE. Whether or not the statement is prepared. By
  390. * default if the statement is not prepared it will be automatically.
  391. * However to avoid this check, which requires a database query you can
  392. * set this value to true and the check will not be performed.
  393. * - return_record: by default, the function will return the TRUE if the record
  394. * was succesfully updated. However, set this option to TRUE to return the
  395. * record that was updated. The returned record will have the fields provided
  396. * but the primary key (if available for the table) will be added to the record.
  397. * @return
  398. * On success this function returns TRUE. On failure, it returns FALSE.
  399. *
  400. * Example usage:
  401. * @code
  402. $umatch = array(
  403. 'organism_id' => array(
  404. 'genus' => 'Citrus',
  405. 'species' => 'sinensis',
  406. ),
  407. 'uniquename' => 'orange1.1g000034m.g7',
  408. 'type_id' => array (
  409. 'cv_id' => array (
  410. 'name' => 'sequence',
  411. ),
  412. 'name' => 'gene',
  413. 'is_obsolete' => 0
  414. ),
  415. );
  416. $uvalues = array(
  417. 'name' => 'orange1.1g000034m.g',
  418. 'type_id' => array (
  419. 'cv_id' => array (
  420. 'name' => 'sequence',
  421. ),
  422. 'name' => 'mRNA',
  423. 'is_obsolete' => 0
  424. ),
  425. );
  426. * $result = tripal_core_chado_update('feature',$umatch,$uvalues);
  427. * @endcode
  428. * The above code species that a feature with a given uniquename, organism_id,
  429. * and type_id (the unique constraint for the feature table) will be updated.
  430. * The organism_id is specified as a nested array that uses the organism_id
  431. * foreign key constraint to lookup the specified values to find the exact
  432. * organism_id. The same nested struture is also used for specifying the
  433. * values to update. The function will find the record that matches the
  434. * columns specified and update the record with the avlues in the $uvalues array.
  435. *
  436. * @ingroup tripal_chado_api
  437. */
  438. function tripal_core_chado_update($table, $match, $values, $options = NULL) {
  439. // set defaults for options. If we don't set defaults then
  440. // we get memory leaks when we try to access the elements
  441. if (!is_array($options)) {
  442. $options = array();
  443. }
  444. if (!array_key_exists('is_prepared', $options)) {
  445. $options['is_prepared'] = FALSE;
  446. }
  447. if (!array_key_exists('statement_name', $options)) {
  448. $options['statement_name'] = FALSE;
  449. }
  450. if (!array_key_exists('return_record', $options)) {
  451. $options['return_record'] = FALSE;
  452. }
  453. $update_values = array(); // contains the values to be updated
  454. $update_matches = array(); // contains the values for the where clause
  455. // Determine plan of action
  456. if ($options['statement_name']) {
  457. // we have a prepared statment (or want to create one) so set $prepared = TRUE
  458. $prepared = TRUE;
  459. // we need to get a persistent connection. If one exists this function
  460. // will not recreate it, but if not it will create one and store it in
  461. // a Drupal variable for reuse later.
  462. $connection = tripal_db_persistent_chado();
  463. // if we cannot get a connection the abandon the prepared statement
  464. if (!$connection ) {
  465. $prepared = FALSE;
  466. unset($options['statement_name']);
  467. }
  468. }
  469. else {
  470. //print "NO STATEMENT (update): $table\n";
  471. //debug_print_backtrace();
  472. }
  473. // get the table description
  474. $table_desc = tripal_core_get_chado_table_schema($table);
  475. // if the user wants us to return the record then we need to get the
  476. // unique primary key if one exists. That way we can add it to the
  477. // values that get returned at the end of the function
  478. $pkeys = array();
  479. if ($options['return_record'] == TRUE) {
  480. if (array_key_exists('primary key', $table_desc) and is_array($table_desc['primary key'])) {
  481. $columns = array();
  482. $stmt_suffix = '';
  483. foreach ($table_desc['primary key'] as $field) {
  484. $columns[] = $field;
  485. $stmt_suffix .= substr($field, 0, 2);
  486. }
  487. $options2 = array('statement_name' => 'sel_' . $table . '_' . $stmt_suffix);
  488. $results = tripal_core_chado_select($table, $columns, $match, $options2);
  489. if (count($results) > 0) {
  490. foreach ($results as $index => $pkey) {
  491. $pkeys[] = $pkey;
  492. }
  493. }
  494. }
  495. }
  496. // get the values needed for matching in the SQL statement
  497. foreach ($match as $field => $value) {
  498. if (is_array($value)) {
  499. $foreign_options = array();
  500. if ($options['statement_name']) {
  501. // add the fk relationship info to the prepared statement name so that
  502. // we can prepare the selects run by the recrusive tripal_core_chado_get_foreign_key
  503. // function.
  504. $fk_sname = "fk_" . $table . "_" . $field;
  505. foreach ($value as $k => $v) {
  506. $fk_sname .= substr($k, 0, 2);
  507. }
  508. $foreign_options['statement_name'] = $fk_sname;
  509. }
  510. $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
  511. if (sizeof($results) > 1) {
  512. 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);
  513. }
  514. elseif (sizeof($results) < 1) {
  515. //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);
  516. }
  517. else {
  518. $update_matches[$field] = $results[0];
  519. }
  520. }
  521. else {
  522. $update_matches[$field] = $value;
  523. }
  524. }
  525. // get the values used for updating
  526. foreach ($values as $field => $value) {
  527. if (is_array($value)) {
  528. $foreign_options = array();
  529. // select the value from the foreign key relationship for this value
  530. if ($options['statement_name']) {
  531. // add the fk relationship info to the prepared statement name so that
  532. // we can prepare the selects run by the recrusive tripal_core_chado_get_foreign_key
  533. // function.
  534. $fk_sname = "fk_" . $table . "_" . $field;
  535. foreach ($value as $k => $v) {
  536. $fk_sname .= substr($k, 0, 2);
  537. }
  538. $foreign_options['statement_name'] = $fk_sname;
  539. }
  540. $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
  541. if (sizeof($results) > 1) {
  542. 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);
  543. }
  544. elseif (sizeof($results) < 1) {
  545. //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);
  546. }
  547. else {
  548. $update_values[$field] = $results[0];
  549. }
  550. }
  551. else {
  552. $update_values[$field] = $value;
  553. }
  554. }
  555. // now build the SQL statement
  556. $sql = "UPDATE {$table} SET ";
  557. $psql = "UPDATE {$table} SET ";
  558. $uargs = array();
  559. $idatatypes = array();
  560. $pvalues = array();
  561. $ivalues = array();
  562. $i = 1;
  563. foreach ($update_values as $field => $value) {
  564. if (strcasecmp($table_desc['fields'][$field]['type'], 'serial')==0 OR
  565. strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
  566. strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
  567. if (strcmp($value, '__NULL__') == 0) {
  568. $sql .= " $field = %s, ";
  569. $ivalues[] = 'NULL';
  570. $pvalues[] = '%s';
  571. $uargs[] = 'NULL';
  572. }
  573. else {
  574. $sql .= " $field = %d, ";
  575. $ivalues[] = $value;
  576. $pvalues[] = '%d';
  577. $uargs[] = $value;
  578. }
  579. $idatatypes[] = 'int';
  580. }
  581. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'boolean')==0) {
  582. $sql .= " $field = %s, ";
  583. $pvalues[] = '%s';
  584. if (strcmp($value, '__NULL__')==0) {
  585. $ivalues[] = 'NULL';
  586. $uargs[] = 'NULL';
  587. }
  588. else {
  589. $ivalues[] = $value;
  590. $uargs[] = $value;
  591. }
  592. $idatatypes[] = 'bool';
  593. }
  594. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'float')==0) {
  595. $sql .= " $field = %s, ";
  596. $pvalues[] = '%s';
  597. if (strcmp($value, '__NULL__')==0) {
  598. $ivalues[] = 'NULL';
  599. $uargs[] = 'NULL';
  600. }
  601. else {
  602. $ivalues[] = $value;
  603. $uargs[] = $value;
  604. }
  605. $idatatypes[] = 'numeric';
  606. }
  607. else {
  608. if (strcmp($value, '__NULL__') == 0) {
  609. $sql .= " $field = %s, ";
  610. $ivalues[] = 'NULL';
  611. $uargs[] = 'NULL';
  612. $pvalues[] = '%s';
  613. }
  614. else {
  615. $sql .= " $field = '%s', ";
  616. $ivalues[] = $value;
  617. $uargs[] = $value;
  618. $pvalues[] = "'%s'";
  619. }
  620. $idatatypes[] = 'text';
  621. }
  622. $psql .= "$field = \$" . $i . ", ";
  623. $i++;
  624. }
  625. $sql = drupal_substr($sql, 0, -2); // get rid of the trailing comma & space
  626. $psql = drupal_substr($psql, 0, -2); // get rid of the trailing comma & space
  627. $sql .= " WHERE ";
  628. $psql .= " WHERE ";
  629. foreach ($update_matches as $field => $value) {
  630. if (strcasecmp($table_desc['fields'][$field]['type'], 'serial')==0 OR
  631. strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
  632. strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
  633. if (strcmp($value, '__NULL__')==0) {
  634. $sql .= " $field = %s AND ";
  635. $ivalues[] = 'NULL';
  636. $uargs[] = 'NULL';
  637. $pvalues[] = '%s';
  638. }
  639. else {
  640. $sql .= " $field = %d AND ";
  641. $ivalues[] = $value;
  642. $uargs[] = $value;
  643. $pvalues[] = '%s';
  644. }
  645. $idatatypes[] = 'int';
  646. }
  647. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'boolean')==0) {
  648. $sql .= " $field = %s AND ";
  649. $pvalues[] = '%s';
  650. if (strcmp($value, '__NULL__')==0) {
  651. $ivalues[] = 'NULL';
  652. $uargs[] = 'NULL';
  653. }
  654. else {
  655. $ivalues[] = $value;
  656. $uargs[] = $value;
  657. }
  658. $idatatypes[] = 'bool';
  659. }
  660. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'float')==0) {
  661. $sql .= " $field = %s AND ";
  662. $pvalues[] = '%s';
  663. if (strcmp($value, '__NULL__')==0) {
  664. $ivalues[] = 'NULL';
  665. $uargs[] = 'NULL';
  666. }
  667. else {
  668. $ivalues[] = $value;
  669. $uargs[] = $value;
  670. }
  671. $idatatypes[] = 'numeric';
  672. }
  673. else {
  674. if (strcmp($value, '__NULL__')==0) {
  675. $sql .= " $field = %s AND ";
  676. $ivalues[] = 'NULL';
  677. $uargs[] = 'NULL';
  678. $pvalues[] = '%s';
  679. }
  680. else {
  681. $sql .= " $field = '%s' AND ";
  682. $ivalues[] = $value;
  683. $uargs[] = $value;
  684. $pvalues[] = "'%s'";
  685. }
  686. $idatatypes[] = 'text';
  687. }
  688. $psql .= "$field = \$" . $i . " AND ";
  689. $i++;
  690. }
  691. $sql = drupal_substr($sql, 0, -4); // get rid of the trailing 'AND'
  692. $psql = drupal_substr($psql, 0, -4); // get rid of the trailing 'AND'
  693. // finish constructing the prepared SQL statement
  694. $psql = "PREPARE " . $options['statement_name'] . " (" . implode(', ', $idatatypes) . ") AS " . $psql;
  695. // finally perform the update. If successful, return the updated record
  696. if ($prepared) {
  697. // if this is the first time we've run this query
  698. // then we need to do the prepare, otherwise just execute
  699. if ($options['is_prepared'] != TRUE and !tripal_core_is_sql_prepared($options['statement_name'])) {
  700. $status = chado_query($psql);
  701. if (!$status) {
  702. watchdog('tripal_core', "tripal_core_chado_update: not able to prepare '%name' statement for: %sql",
  703. array('%name' => $options['statement_name'], '%sql' => $sql), WATCHDOG_ERROR);
  704. return FALSE;
  705. }
  706. }
  707. $sql = "EXECUTE " . $options['statement_name'] . "(" . implode(", ", $pvalues) . ")";
  708. $result = chado_query($sql, $ivalues);
  709. }
  710. // if it's not a prepared statement then insert normally
  711. else {
  712. $result = chado_query($sql, $uargs);
  713. }
  714. // if we have a result then add primary keys to return array
  715. if ($options['return_record'] == TRUE and $result) {
  716. // only if we have a single result do we want to add the primary keys to the values
  717. // array. If the update matched many records we can't add the pkeys
  718. if (count($pkeys) == 1) {
  719. foreach ($pkeys as $index => $pkey) {
  720. foreach ($pkey as $field => $fvalue) {
  721. $values[$field] = $fvalue;
  722. }
  723. }
  724. }
  725. return $values;
  726. }
  727. elseif ($options['return_record'] == FALSE and $result) {
  728. return TRUE;
  729. }
  730. else {
  731. watchdog('tripal_core', "Cannot update record in $table table. \nMatch:" . print_r($match, 1) . "\nValues: ". print_r($values, 1), array(), 'WATCHDOG_ERROR');
  732. return FALSE;
  733. }
  734. return FALSE;
  735. }
  736. /**
  737. * Provides a generic function for deleting a record(s) from any chado table
  738. *
  739. * Use this function to delete a record(s) in any Chado table. The first
  740. * argument specifies the table to delete from and the second is an array
  741. * of values to match for locating the record(s) to be deleted. The arrays
  742. * are mutli-dimensional such that foreign key lookup values can be specified.
  743. *
  744. * @param $table
  745. * The name of the chado table for inserting
  746. * @param $match
  747. * An associative array containing the values for locating a record to update.
  748. * @param $options
  749. * An array of options such as:
  750. * - statement_name: the name of the prepared statement to use. If the statement
  751. * has not yet been prepared it will be prepared automatically. On subsequent
  752. * calls with the same statement_name only an execute on the previously
  753. * prepared statement will occur.
  754. * - is_prepared: TRUE or FALSE. Whether or not the statement is prepared. By
  755. * default if the statement is not prepared it will be automatically.
  756. * However to avoid this check, which requires a database query you can
  757. * set this value to true and the check will not be performed.
  758. * @return
  759. * On success this function returns TRUE. On failure, it returns FALSE.
  760. *
  761. * Example usage:
  762. * @code
  763. $umatch = array(
  764. 'organism_id' => array(
  765. 'genus' => 'Citrus',
  766. 'species' => 'sinensis',
  767. ),
  768. 'uniquename' => 'orange1.1g000034m.g7',
  769. 'type_id' => array (
  770. 'cv_id' => array (
  771. 'name' => 'sequence',
  772. ),
  773. 'name' => 'gene',
  774. 'is_obsolete' => 0
  775. ),
  776. );
  777. $uvalues = array(
  778. 'name' => 'orange1.1g000034m.g',
  779. 'type_id' => array (
  780. 'cv_id' => array (
  781. 'name' => 'sequence',
  782. ),
  783. 'name' => 'mRNA',
  784. 'is_obsolete' => 0
  785. ),
  786. );
  787. * $result = tripal_core_chado_update('feature',$umatch,$uvalues);
  788. * @endcode
  789. * The above code species that a feature with a given uniquename, organism_id,
  790. * and type_id (the unique constraint for the feature table) will be deleted.
  791. * The organism_id is specified as a nested array that uses the organism_id
  792. * foreign key constraint to lookup the specified values to find the exact
  793. * organism_id. The same nested struture is also used for specifying the
  794. * values to update. The function will find all records that match the
  795. * columns specified and delete them.
  796. *
  797. * @ingroup tripal_chado_api
  798. */
  799. function tripal_core_chado_delete($table, $match, $options = NULL) {
  800. // set defaults for options. If we don't set defaults then
  801. // we get memory leaks when we try to access the elements
  802. if (!is_array($options)) {
  803. $options = array();
  804. }
  805. if (!array_key_exists('is_prepared', $options)) {
  806. $options['is_prepared'] = FALSE;
  807. }
  808. if (!array_key_exists('statement_name', $options)) {
  809. $options['statement_name'] = FALSE;
  810. }
  811. // Determine plan of action
  812. if ($options['statement_name']) {
  813. // we have a prepared statment (or want to create one) so set $prepared = TRUE
  814. $prepared = TRUE;
  815. // we need to get a persistent connection. If one exists this function
  816. // will not recreate it, but if not it will create one and store it in
  817. // a Drupal variable for reuse later.
  818. $connection = tripal_db_persistent_chado();
  819. // if we cannot get a connection the abandon the prepared statement
  820. if (!$connection ) {
  821. $prepared = FALSE;
  822. unset($options['statement_name']);
  823. }
  824. }
  825. else {
  826. //print "NO STATEMENT (update): $table\n";
  827. //debug_print_backtrace();
  828. }
  829. $delete_matches = array(); // contains the values for the where clause
  830. // get the table description
  831. $table_desc = tripal_core_get_chado_table_schema($table);
  832. $fields = $table_desc['fields'];
  833. // get the values needed for matching in the SQL statement
  834. foreach ($match as $field => $value) {
  835. if (is_array($value)) {
  836. // if the user has specified an array of values to delete rather than
  837. // FK relationships the keep those in our match
  838. if (array_values($value) === $value) {
  839. $delete_matches[$field] = $value;
  840. }
  841. else {
  842. $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value);
  843. if (sizeof($results) > 1) {
  844. 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);
  845. }
  846. elseif (sizeof($results) < 1) {
  847. //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);
  848. }
  849. else {
  850. $delete_matches[$field] = $results[0];
  851. }
  852. }
  853. }
  854. else {
  855. $delete_matches[$field] = $value;
  856. }
  857. }
  858. // now build the SQL statement
  859. $sql = "DELETE FROM {$table} WHERE ";
  860. $psql = $sql;
  861. $uargs = array();
  862. $idatatypes = array();
  863. $pvalues = array();
  864. $ivalues = array();
  865. $dargs = array();
  866. $void_prepared = 0;
  867. $i = 1;
  868. foreach ($delete_matches as $field => $value) {
  869. // if we have an array values then this is an "IN" clasue.
  870. // we cannot use prepared statements with these
  871. if (count($value) > 1) {
  872. $sql .= "$field IN (" . db_placeholders($value, 'varchar') . ") AND ";
  873. foreach ($value as $v) {
  874. $dargs[] = $v;
  875. }
  876. $void_prepared = 1;
  877. continue;
  878. }
  879. if (strcasecmp($table_desc['fields'][$field]['type'], 'serial') == 0 OR
  880. strcasecmp($table_desc['fields'][$field]['type'], 'int') == 0 OR
  881. strcasecmp($table_desc['fields'][$field]['type'], 'integer') == 0) {
  882. if (strcmp($value, '__NULL__') == 0) {
  883. $sql .= " $field = NULL AND ";
  884. $ivalues[] = 'NULL';
  885. $pvalues[] = '%s';
  886. $uargs[] = 'NULL';
  887. }
  888. else {
  889. $sql .= " $field = %d AND ";
  890. $ivalues[] = $value;
  891. $pvalues[] = '%d';
  892. $uargs[] = $value;
  893. }
  894. $idatatypes[] = 'int';
  895. }
  896. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'boolean')==0) {
  897. $sql .= " $field = %s AND ";
  898. $pvalues[] = '%s';
  899. if (strcmp($value, '__NULL__')==0) {
  900. $ivalues[] = 'NULL';
  901. $uargs[] = 'NULL';
  902. }
  903. else {
  904. $ivalues[] = $value;
  905. $uargs[] = $value;
  906. }
  907. $idatatypes[] = 'bool';
  908. }
  909. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'float')==0) {
  910. $sql .= " $field = %s AND ";
  911. $pvalues[] = '%s';
  912. if (strcmp($value, '__NULL__')==0) {
  913. $ivalues[] = 'NULL';
  914. $uargs[] = 'NULL';
  915. }
  916. else {
  917. $ivalues[] = $value;
  918. $uargs[] = $value;
  919. }
  920. $idatatypes[] = 'numeric';
  921. }
  922. else {
  923. if (strcmp($value, '__NULL__')==0) {
  924. $sql .= " $field = %s AND ";
  925. $ivalues[] = 'NULL';
  926. $uargs[] = 'NULL';
  927. $pvalues[] = '%s';
  928. }
  929. else {
  930. $sql .= " $field = '%s' AND ";
  931. $ivalues[] = $value;
  932. $uargs[] = $value;
  933. $pvalues[] = "'%s'";
  934. }
  935. $idatatypes[] = 'text';
  936. }
  937. array_push($dargs, $value);
  938. $psql .= "$field = \$" . $i . " AND ";
  939. $i++;
  940. }
  941. $sql = drupal_substr($sql, 0, -4); // get rid of the trailing 'AND'
  942. $psql = drupal_substr($psql, 0, -4); // get rid of the trailing 'AND'
  943. // finish constructing the prepared SQL statement
  944. $psql = "PREPARE " . $options['statement_name'] . " (" . implode(', ', $idatatypes) . ") AS " . $psql;
  945. // finally perform the update. If successful, return the updated record
  946. if ($prepared and !$void_prepared) {
  947. // if this is the first time we've run this query
  948. // then we need to do the prepare, otherwise just execute
  949. if ($options['is_prepared'] != TRUE and
  950. !tripal_core_is_sql_prepared($options['statement_name'])) {
  951. $status = chado_query($psql);
  952. if (!$status) {
  953. watchdog('tripal_core', "tripal_core_chado_delete: not able to prepare '%name' statement for: %sql", array('%name' => $options['statement_name'], '%sql' => $sql), WATCHDOG_ERROR);
  954. return FALSE;
  955. }
  956. }
  957. $sql = "EXECUTE " . $options['statement_name'] . "(" . implode(", ", $pvalues) . ")";
  958. $resource = chado_query($sql, $ivalues);
  959. }
  960. // if it's not a prepared statement then insert normally
  961. else {
  962. $resource = chado_query($sql, $uargs);
  963. }
  964. // finally perform the delete. If successful, return the updated record
  965. $result = chado_query($sql, $dargs);
  966. if ($result) {
  967. return TRUE;
  968. }
  969. else {
  970. watchdog('tripal_core', "Cannot delete record in $table table. Match:" . print_r($match, 1) . ". Values: ". print_r($values, 1), array(), 'WATCHDOG_ERROR');
  971. return FALSE;
  972. }
  973. return FALSE;
  974. }
  975. /**
  976. * Provides a generic routine for selecting data from a Chado table
  977. *
  978. * Use this function to perform a simple select from any Chado table.
  979. *
  980. * @param $table
  981. * The name of the chado table for inserting
  982. * @param $columns
  983. * An array of column names
  984. * @param $values
  985. * An associative array containing the values for filtering the results. In the
  986. * case where multiple values for the same time are to be selected an additional
  987. * entry for the field should appear for each value
  988. * @param $options
  989. * An associative array of additional options where the key is the option
  990. * and the value is the value of that option.
  991. *
  992. * Additional Options Include:
  993. * - has_record
  994. * Set this argument to 'TRUE' to have this function return a numeric
  995. * value for the number of recrods rather than the array of records. this
  996. * can be useful in 'if' statements to check the presence of particula records.
  997. * - return_sql
  998. * Set this to 'TRUE' to have this function return an array where the first
  999. * element is the sql that would have been run and the second is an array of
  1000. * arguments.
  1001. * - case_insensitive_columns
  1002. * An array of columns to do a case insensitive search on.
  1003. * - regex_columns
  1004. * An array of columns where the value passed in should be treated as a regular expression
  1005. * - order_by
  1006. * An associative array containing the column names of the table as keys
  1007. * and the type of sort (i.e. ASC, DESC) as the values. The results in the
  1008. * query will be sorted by the key values in the direction listed by the value
  1009. * - statement_name: the name of the prepared statement to use. If the statement
  1010. * has not yet been prepared it will be prepared automatically. On subsequent
  1011. * calls with the same statement_name only an execute on the previously
  1012. * prepared statement will occur.
  1013. * - is_prepared: TRUE or FALSE. Whether or not the statement is prepared. By
  1014. * default if the statement is not prepared it will be automatically.
  1015. * However to avoid this check, which requires a database query you can
  1016. * set this value to true and the check will not be performed.
  1017. * - is_duplicate: TRUE or FALSE. Checks the values submited to see if
  1018. * they violate any of the unique constraints. If so, the record
  1019. * is returned, if not, FALSE is returned.
  1020. *
  1021. *
  1022. * @return
  1023. * A database query result resource, FALSE if the query was not executed
  1024. * correctly, an empty array if no records were matched, or the number of records
  1025. * in the dataset if $has_record is set.
  1026. * If the option 'is_duplicate' is provided and the record is a duplicate it
  1027. * will return the duplicated record. If the 'has_record' option is provided
  1028. * a value of TRUE will be returned if a record exists and FALSE will bee
  1029. * returned if there are not records.
  1030. *
  1031. * Example usage:
  1032. * @code
  1033. * $columns = array('feature_id', 'name');
  1034. * $values = array(
  1035. * 'organism_id' => array(
  1036. * 'genus' => 'Citrus',
  1037. * 'species' => array('sinensis', 'clementina'),
  1038. * ),
  1039. * 'uniquename' => 'orange1.1g000034m.g',
  1040. * 'type_id' => array (
  1041. * 'cv_id' => array (
  1042. * 'name' => 'sequence',
  1043. * ),
  1044. * 'name' => 'gene',
  1045. * 'is_obsolete' => 0
  1046. * ),
  1047. * );
  1048. * $options = array(
  1049. * 'statement_name' => 'sel_feature_genus_species_cvname'
  1050. * 'order_by' => array(
  1051. * 'name' => 'ASC'
  1052. * ),
  1053. * );
  1054. * $result = tripal_core_chado_select('feature',$columns,$values,$options);
  1055. * @endcode
  1056. * The above code selects a record from the feature table using the three fields
  1057. * that uniquely identify a feature. The $columns array simply lists the columns
  1058. * to select. The $values array is nested such that the organism is identified by
  1059. * way of the organism_id foreign key constraint by specifying the genus and
  1060. * species. The cvterm is also specified using its foreign key and the cv_id
  1061. * for the cvterm is nested as well. In the example above, two different species
  1062. * are allowed to match
  1063. *
  1064. * @ingroup tripal_chado_api
  1065. */
  1066. function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
  1067. // set defaults for options. If we don't set defaults then
  1068. // we get memory leaks when we try to access the elements
  1069. if (!is_array($options)) {
  1070. $options = array();
  1071. }
  1072. if (!array_key_exists('case_insensitive_columns', $options)) {
  1073. $options['case_insensitive_columns'] = array();
  1074. }
  1075. if (!array_key_exists('regex_columns', $options)) {
  1076. $options['regex_columns'] = array();
  1077. }
  1078. if (!array_key_exists('order_by', $options)) {
  1079. $options['order_by'] = array();
  1080. }
  1081. if (!array_key_exists('is_prepared', $options)) {
  1082. $options['is_prepared'] = FALSE;
  1083. }
  1084. if (!array_key_exists('return_sql', $options)) {
  1085. $options['return_sql'] = FALSE;
  1086. }
  1087. if (!array_key_exists('has_record', $options)) {
  1088. $options['has_record'] = FALSE;
  1089. }
  1090. if (!array_key_exists('statement_name', $options)) {
  1091. $options['statement_name'] = FALSE;
  1092. }
  1093. if (!array_key_exists('is_duplicate', $options)) {
  1094. $options['is_duplicate'] = FALSE;
  1095. }
  1096. // if this is a prepared statement check to see if it has already been prepared
  1097. $prepared = FALSE;
  1098. if ($options['statement_name']) {
  1099. $prepared = TRUE;
  1100. // we need to get a persistent connection. If one exists this function
  1101. // will not recreate it, but if not it will create one and store it in
  1102. // a Drupal variable for reuse later.
  1103. $connection = tripal_db_persistent_chado();
  1104. // if we cannot get a connection the abandon the prepared statement
  1105. if (!$connection) {
  1106. $prepared = FALSE;
  1107. unset($options['statement_name']);
  1108. }
  1109. }
  1110. else {
  1111. //print "NO STATEMENT (select): $table\n";
  1112. //debug_print_backtrace();
  1113. }
  1114. // check that our columns and values arguments are proper arrays
  1115. if (!is_array($columns)) {
  1116. watchdog('tripal_core', 'the $columns argument for tripal_core_chado_select must be an array.');
  1117. return FALSE;
  1118. }
  1119. if (!is_array($values)) {
  1120. watchdog('tripal_core', 'the $values argument for tripal_core_chado_select must be an array.');
  1121. return FALSE;
  1122. }
  1123. // get the table description
  1124. $table_desc = tripal_core_get_chado_table_schema($table);
  1125. $select = '';
  1126. $from = '';
  1127. $where = '';
  1128. $args = array();
  1129. // if the 'use_unique' option is turned on then we want
  1130. // to remove all but unique keys
  1131. if ($options['is_duplicate'] and array_key_exists('unique keys', $table_desc)) {
  1132. $ukeys = $table_desc['unique keys'];
  1133. $has_results = 0;
  1134. // iterate through the unique constraints and reset the values and columns
  1135. // arrays to only include these fields
  1136. foreach ($ukeys as $cname => $fields) {
  1137. if ($has_results) {
  1138. continue;
  1139. }
  1140. $new_values = array();
  1141. $new_columns = array();
  1142. $new_options = array();
  1143. $uq_sname = "uq_" . $table . "_";
  1144. $has_pkey = 0;
  1145. // include the primary key in the results returned
  1146. if (array_key_exists('primary key', $table_desc)) {
  1147. $has_pkey = 1;
  1148. $pkeys = $table_desc['primary key'];
  1149. foreach ($pkeys as $index => $key) {
  1150. array_push($new_columns, $key);
  1151. }
  1152. }
  1153. // recreate the $values and $columns arrays
  1154. foreach ($fields as $field) {
  1155. if (array_key_exists($field, $values)) {
  1156. $new_values[$field] = $values[$field];
  1157. $uq_sname .= substr($field, 0, 2);
  1158. // if there is no primary key then use the unique contraint fields
  1159. if (!$has_pkey) {
  1160. array_push($new_columns, $field);
  1161. }
  1162. }
  1163. // if the field doesn't exist in the values array then
  1164. // substitute any default values
  1165. elseif (array_key_exists('default', $table_desc['fields'][$field])) {
  1166. $new_values[$field] = $table_desc['fields'][$field]['default'];
  1167. $uq_sname .= substr($field, 0, 2);
  1168. if (!$has_pkey) {
  1169. array_push($new_columns, $field);
  1170. }
  1171. }
  1172. // if there is no value (default or otherwise) check if this field is
  1173. // allowed to be null
  1174. elseif (!$table_desc['fields'][$field]['not null']) {
  1175. $new_values[$field] = NULL;
  1176. $uq_sname .= "n".substr($field, 0, 2);
  1177. if (!$has_pkey) {
  1178. array_push($new_columns, $field);
  1179. }
  1180. }
  1181. // if the array key doesn't exist in the values given by the caller
  1182. // and there is no default value then we cannot check if the record
  1183. // is a duplicate so return FALSE
  1184. else {
  1185. watchdog('tripal_core', "tripal_core_chado_select: There is no value for %field"
  1186. ." thus we cannot check if this record is unique",
  1187. array('%field' => $field), WATCHDOG_ERROR);
  1188. return FALSE;
  1189. }
  1190. }
  1191. $new_options['statement_name'] = $uq_sname;
  1192. $results = tripal_core_chado_select($table, $new_columns, $new_values, $new_options);
  1193. // if we have a duplicate record then return the results
  1194. if (count($results) > 0) {
  1195. $has_results = 1;
  1196. }
  1197. unset($new_columns);
  1198. unset($new_values);
  1199. unset($new_options);
  1200. }
  1201. if ($options['has_record'] and $has_results) {
  1202. return TRUE;
  1203. }
  1204. else {
  1205. return $results;
  1206. }
  1207. }
  1208. foreach ($values as $field => $value) {
  1209. // make sure the field is in the table description. If not then return an error
  1210. // message
  1211. if (!array_key_exists($field, $table_desc['fields'])) {
  1212. watchdog('tripal_core', "tripal_core_chado_select: The field '%field' does not exist ".
  1213. "for the table '%table'. Cannot perform query. Values: %array",
  1214. array('%field' => $field, '%table' => $table, '%array' => print_r($values, 1)), WATCHDOG_ERROR);
  1215. return array();
  1216. }
  1217. $select[] = $field;
  1218. if (is_array($value)) {
  1219. // if the user has specified multiple values for matching then this we
  1220. // want to catch that and save them in our $where array, otherwise
  1221. // we'll descend for a foreign key relationship
  1222. if (array_values($value) === $value) {
  1223. $where[$field] = $value;
  1224. }
  1225. else {
  1226. // select the value from the foreign key relationship for this value
  1227. $foreign_options = array(
  1228. 'regex_columns' => $options['regex_columns'],
  1229. 'case_insensitive_columns' => $options['case_insensitive_columns']
  1230. );
  1231. if (array_key_exists('statement_name', $options) and $options['statement_name']) {
  1232. // add the fk relationship info to the prepared statement name so that
  1233. // we can prepare the selects run by the recrusive tripal_core_chado_get_foreign_key
  1234. // function. we need the statement name to be unique so take the first two characters of each column
  1235. $fk_sname = "fk_" . $table . "_" . $field;
  1236. foreach ($value as $k => $v) {
  1237. $fk_sname .= substr($k, 0, 2);
  1238. }
  1239. $foreign_options['statement_name'] = $fk_sname;
  1240. }
  1241. $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
  1242. if (!$results or count($results)==0) {
  1243. // foreign key records are required
  1244. // thus if none matched then return FALSE and alert the admin through watchdog
  1245. /*watchdog('tripal_core',
  1246. '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',
  1247. array('!table' => $table,
  1248. '!columns' => '<pre>' . print_r($columns, TRUE) . '</pre>',
  1249. '!values' => '<pre>' . print_r($values, TRUE) . '</pre>',
  1250. '!field' => $field,
  1251. ),
  1252. WATCHDOG_WARNING);*/
  1253. return array();
  1254. }
  1255. else {
  1256. $where[$field] = $results;
  1257. }
  1258. }
  1259. }
  1260. else {
  1261. // need to catch a 0 and make int if integer field
  1262. // but we don't want to catch a NULL
  1263. if ($value === NULL) {
  1264. $where[$field] = NULL;
  1265. }
  1266. elseif ($table_desc['fields'][$field]['type'] == 'int') {
  1267. $where[$field][] = (int) $value;
  1268. }
  1269. else {
  1270. $where[$field][] = $value;
  1271. }
  1272. }
  1273. }
  1274. // now build the SQL and prepared SQL statements. We may not use
  1275. // the prepared statement if it wasn't requested in the options of if the
  1276. // argument in a where statement has multiple values.
  1277. if (empty($where)) {
  1278. // sometimes want to select everything
  1279. $sql = "SELECT " . implode(', ', $columns) . " ";
  1280. $sql .= "FROM {$table} ";
  1281. // we don't prepare a statement if there is no where clause
  1282. $prepared = FALSE;
  1283. }
  1284. else {
  1285. $sql = "SELECT " . implode(', ', $columns) . " ";
  1286. $sql .= "FROM {$table} ";
  1287. $sql .= "WHERE ";
  1288. $psql = $sql; // prepared SQL statement;
  1289. $i = 1;
  1290. $pvalues = array();
  1291. $itypes = array();
  1292. foreach ($where as $field => $value) {
  1293. // if we have multiple values returned then we need an 'IN' statement
  1294. // in our where statement
  1295. if (count($value) > 1) {
  1296. $sql .= "$field IN (" . db_placeholders($value, 'varchar') . ") AND ";
  1297. foreach ($value as $v) {
  1298. $args[] = $v;
  1299. // we can't do a prepared statement with an 'IN' statement in a
  1300. // where clause because we can't guarantee we'll always have the
  1301. // same number of elements.
  1302. $prepared = FALSE;
  1303. }
  1304. }
  1305. // if we have a null value then we need an IS NULL in our where statement
  1306. elseif ($value === NULL) {
  1307. $sql .= "$field IS NULL AND ";
  1308. $psql .= "$field IS NULL AND ";
  1309. // Need to remove one from the argument count b/c nulls don't add an argument
  1310. $i--;
  1311. }
  1312. // if we have a single value then we need an = in our where statement
  1313. else {
  1314. $operator = '=';
  1315. if (in_array($field, $options['regex_columns'])) {
  1316. $operator = '~*';
  1317. }
  1318. // get the types for the prepared statement. First check if the type
  1319. // is an integer
  1320. if (strcasecmp($table_desc['fields'][$field]['type'], 'serial')==0 OR
  1321. strcasecmp($table_desc['fields'][$field]['type'], 'int')==0 OR
  1322. strcasecmp($table_desc['fields'][$field]['type'], 'integer')==0) {
  1323. $sql .= "$field $operator %d AND ";
  1324. $psql .= "$field $operator \$" . $i . " AND ";
  1325. $args[] = $value[0];
  1326. // set the variables needed for the prepared statement
  1327. $idatatypes[] = 'int';
  1328. $itypes[] = '%d';
  1329. $pvalues[] = $value[0];
  1330. }
  1331. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'boolean')==0) {
  1332. $sql .= "$field $operator %s AND ";
  1333. $psql .= "$field $operator \$" . $i . " AND ";
  1334. $args[] = $value[0];
  1335. // set the variables needed for the prepared statement
  1336. $idatatypes[] = 'bool';
  1337. $itypes[] = '%d';
  1338. $pvalues[] = $value[0];
  1339. }
  1340. elseif (strcasecmp($table_desc['fields'][$field]['type'], 'float')==0) {
  1341. $sql .= "$field $operator %s AND ";
  1342. $psql .= "$field $operator \$" . $i . " AND ";
  1343. $args[] = $value[0];
  1344. // set the variables needed for the prepared statement
  1345. $idatatypes[] = 'numeric';
  1346. $itypes[] = '%f';
  1347. $pvalues[] = $value[0];
  1348. }
  1349. // else the type is a text
  1350. else {
  1351. if (in_array($field, $options['case_insensitive_columns'])) {
  1352. $sql .= "lower($field) $operator lower('%s') AND ";
  1353. $psql .= "lower($field) $operator lower('\$" . $i . "') AND ";
  1354. $args[] = $value;
  1355. }
  1356. else {
  1357. $sql .= "$field $operator '%s' AND ";
  1358. $psql .= "$field $operator \$" . $i . " AND ";
  1359. $args[] = $value[0];
  1360. }
  1361. // set the variables needed for the prepared statement
  1362. $idatatypes[] = 'text';
  1363. $itypes[] = "'%s'";
  1364. $pvalues[] = $value[0];
  1365. }
  1366. }
  1367. $i++;
  1368. } // end foreach item in where clause
  1369. $sql = drupal_substr($sql, 0, -4); // get rid of the trailing 'AND '
  1370. $psql = drupal_substr($psql, 0, -4); // get rid of the trailing 'AND '
  1371. } // end if(empty($where)){ } else {
  1372. // finally add any ordering of the results to the SQL statement
  1373. if (count($options['order_by']) > 0) {
  1374. $sql .= " ORDER BY ";
  1375. $psql .= " ORDER BY ";
  1376. foreach ($options['order_by'] as $field => $dir) {
  1377. $sql .= "$field $dir, ";
  1378. $psql .= "$field $dir, ";
  1379. }
  1380. $sql = drupal_substr($sql, 0, -2); // get rid of the trailing ', '
  1381. $psql = drupal_substr($psql, 0, -2); // get rid of the trailing ', '
  1382. }
  1383. // finish constructing the prepared SQL statement
  1384. if ($options['statement_name']) {
  1385. $psql = "PREPARE " . $options['statement_name'] . " (" . implode(', ', $idatatypes) . ") AS " . $psql;
  1386. }
  1387. // if the caller has requested the SQL rather than the results...
  1388. // which happens in the case of wanting to use the Drupal pager, then do so
  1389. if ($options['return_sql'] == TRUE) {
  1390. return array('sql' => $sql, 'args' => $args);
  1391. }
  1392. // prepare the statement
  1393. if ($prepared) {
  1394. // if this is the first time we've run this query
  1395. // then we need to do the prepare, otherwise just execute
  1396. if ($options['is_prepared'] != TRUE) {
  1397. $status = tripal_core_chado_prepare($options['statement_name'], $psql, $idatatypes);
  1398. if (!$status) {
  1399. return FALSE;
  1400. }
  1401. }
  1402. $sql = "EXECUTE " . $options['statement_name'] . "(" . implode(", ", $itypes) . ")";
  1403. // WARNING: This call creates a memory leak: if you remove the $pvalues it doesn't
  1404. // do this. Got to find out what's causing this.
  1405. $resource = tripal_core_chado_execute_prepared($options['statement_name'], $sql, $pvalues);
  1406. }
  1407. else {
  1408. $resource = chado_query($sql, $args);
  1409. }
  1410. // format results into an array
  1411. $results = array();
  1412. while ($r = db_fetch_object($resource)) {
  1413. $results[] = $r;
  1414. }
  1415. if ($options['has_record']) {
  1416. return count($results);
  1417. }
  1418. return $results;
  1419. }
  1420. /**
  1421. * Gets the value of a foreign key relationship
  1422. *
  1423. * This function is used by tripal_core_chado_select, tripal_core_chado_insert,
  1424. * and tripal_core_chado_update to iterate through the associate array of
  1425. * values that gets passed to each of those routines. The values array
  1426. * is nested where foreign key contraints are used to specify a value that. See
  1427. * documentation for any of those functions for further information.
  1428. *
  1429. * @param $table_desc
  1430. * A table description for the table with the foreign key relationship to be identified generated by
  1431. * hook_chado_<table name>_schema()
  1432. * @param $field
  1433. * The field in the table that is the foreign key.
  1434. * @param $values
  1435. * An associative array containing the values
  1436. * @param $options
  1437. * An associative array of additional options where the key is the option
  1438. * and the value is the value of that option. These options are passed on to tripal_core_chado_select.
  1439. *
  1440. * Additional Options Include:
  1441. * - case_insensitive_columns
  1442. * An array of columns to do a case insensitive search on.
  1443. * - regex_columns
  1444. * An array of columns where the value passed in should be treated as a regular expression
  1445. *
  1446. * @return
  1447. * A string containg the results of the foreign key lookup, or FALSE if failed.
  1448. *
  1449. * Example usage:
  1450. * @code
  1451. *
  1452. * $values = array(
  1453. * 'genus' => 'Citrus',
  1454. * 'species' => 'sinensis',
  1455. * );
  1456. * $value = tripal_core_chado_get_foreign_key('feature', 'organism_id',$values);
  1457. *
  1458. * @endcode
  1459. * The above code selects a record from the feature table using the three fields
  1460. * that uniquely identify a feature. The $columns array simply lists the columns
  1461. * to select. The $values array is nested such that the organism is identified by
  1462. * way of the organism_id foreign key constraint by specifying the genus and
  1463. * species. The cvterm is also specified using its foreign key and the cv_id
  1464. * for the cvterm is nested as well.
  1465. *
  1466. * @ingroup tripal_chado_api
  1467. */
  1468. function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $options = NULL) {
  1469. // set defaults for options. If we don't set defaults then
  1470. // we get memory leaks when we try to access the elements
  1471. if (!is_array($options)) {
  1472. $options = array();
  1473. }
  1474. if (!array_key_exists('case_insensitive_columns', $options)) {
  1475. $options['case_insensitive_columns'] = array();
  1476. }
  1477. if (!array_key_exists('regex_columns', $options)) {
  1478. $options['regex_columns'] = array();
  1479. }
  1480. // get the list of foreign keys for this table description and
  1481. // iterate through those until we find the one we're looking for
  1482. $fkeys = '';
  1483. if (array_key_exists('foreign keys', $table_desc)) {
  1484. $fkeys = $table_desc['foreign keys'];
  1485. }
  1486. if ($fkeys) {
  1487. foreach ($fkeys as $name => $def) {
  1488. if (is_array($def['table'])) {
  1489. //foreign key was described 2X
  1490. $message = "The foreign key " . $name . " was defined twice. Please check modules "
  1491. ."to determine if hook_chado_schema_<version>_" . $table_desc['table'] . "() was "
  1492. ."implemented and defined this foreign key when it wasn't supposed to. Modules "
  1493. ."this hook was implemented in: " . implode(', ',
  1494. module_implements("chado_" . $table_desc['table'] . "_schema")) . ".";
  1495. watchdog('tripal_core', $message);
  1496. drupal_set_message(check_plain($message), 'error');
  1497. continue;
  1498. }
  1499. $table = $def['table'];
  1500. $columns = $def['columns'];
  1501. // iterate through the columns of the foreign key relationship
  1502. foreach ($columns as $left => $right) {
  1503. // does the left column in the relationship match our field?
  1504. if (strcmp($field, $left) == 0) {
  1505. // the column name of the foreign key matches the field we want
  1506. // so this is the right relationship. Now we want to select
  1507. $select_cols = array($right);
  1508. $result = tripal_core_chado_select($table, $select_cols, $values, $options);
  1509. $fields = array();
  1510. if ($result and count($result) > 0) {
  1511. foreach ($result as $obj) {
  1512. $fields[] = $obj->$right;
  1513. }
  1514. return $fields;
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. else {
  1521. // TODO: what do we do if we get to this point and we have a fk
  1522. // relationship expected but we don't have any definition for one in the
  1523. // table schema??
  1524. $version = tripal_core_get_chado_version(TRUE);
  1525. $message = t("There is no foreign key relationship defined for " . $field . ".
  1526. To define a foreign key relationship, determine the table this foreign
  1527. key referrs to (<foreign table>) and then implement
  1528. hook_chado_chado_schema_v<version>_<foreign table>(). See
  1529. tripal_feature_chado_v1_2_schema_feature for an example. Chado version: $version");
  1530. watchdog('tripal_core', $message);
  1531. drupal_set_message(check_plain($message), 'error');
  1532. }
  1533. return array();
  1534. }
  1535. /**
  1536. * Generates an object containing the full details of a record(s) in chado.
  1537. *
  1538. * This differs from the objects returned by tripal_core_chado_select in so far as all foreign key
  1539. * relationships have been followed meaning you have more complete details. Thus this function
  1540. * should be used whenever you need a full variable and tripal_core_chado_select should be used if
  1541. * you only case about a few columns.
  1542. *
  1543. * @param $table
  1544. * The name of the base table to generate a variable for
  1545. * @param $values
  1546. * A select values array that selects the records you want from the base table
  1547. * (this has the same form as tripal_core_chado_select)
  1548. * @param $base_options
  1549. * An array containing options for the base table. For example, an
  1550. * option of 'order_by' may be used to sort results in the base table
  1551. * if more than one are returned. The options must be compatible with
  1552. * the options accepted by the tripal_core_chado_select() function.
  1553. * Additionally, These options are available for this function:
  1554. * -return_array:
  1555. * can be provided to force the function to always return an array. Default
  1556. * behavior is to return a single record if only one record exists or to return
  1557. * an array if multiple records exist.
  1558. * - include_fk:
  1559. * an array of FK relationships to fallow. By default, the
  1560. * tripal_core_chado_select function will follow all FK relationships but this
  1561. * may generate more queries then is desired. Provide an array specifying the
  1562. * fields to include. For example, if including the cvterm, cv and dbxref records
  1563. * for a property the following array would work:
  1564. * array('type_id' => array('cv_id' => 1, 'dbxref_id' => 1)).
  1565. * Typicall, to expadn a property, this function would also include the db record
  1566. * through the db_id foreign key with the dbxref and db tables, but becuase
  1567. * it is not included in the array it will not be included in the results.
  1568. * @return
  1569. * Either an object (if only one record was selected from the base table)
  1570. * or an array of objects (if more than one record was selected from the base table).
  1571. * If the option 'return_array' is provided the function always returns an array.
  1572. *
  1573. * Example Usage:
  1574. * @code
  1575. $values = array(
  1576. 'name' => 'Medtr4g030710'
  1577. );
  1578. $features = tripal_core_generate_chado_var('feature', $values);
  1579. * @endcode
  1580. * This will return an object if there is only one feature with the name Medtr4g030710 or it will
  1581. * return an array of feature objects if more than one feature has that name.
  1582. *
  1583. * Note to Module Designers: Fields can be excluded by default from these objects by implementing
  1584. * one of the following hooks:
  1585. * - hook_exclude_field_from_tablename_by_default (where tablename is the name of the table):
  1586. * This hook allows you to add fields to be excluded on a per table basis. Simply implement
  1587. * this hook to return an array of fields to be excluded. For example:
  1588. * @code
  1589. mymodule_exclude_field_from_feature_by_default() {
  1590. return array('residues' => TRUE);
  1591. }
  1592. * @endcode
  1593. * will ensure that feature.residues is ecluded from a feature object by default.
  1594. * - hook_exclude_type_by_default:
  1595. * This hook allows you to exclude fields from all tables that are of a given postgresql field
  1596. * type. Simply implement this hook to return an array of postgresql types mapped to criteria.
  1597. * Then all fields of that type where the criteria supplied returns TRUE will be excluded from
  1598. * any table. Tokens available in criteria are &gt;field_value&lt; and &gt;field_name&lt; . For example:
  1599. * @code
  1600. mymodule_exclude_type_by_default() {
  1601. return array('text' => 'length(&gt;field_value&lt; ) > 50');
  1602. }
  1603. * @endcode
  1604. * 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.
  1605. *
  1606. * @ingroup tripal_chado_api
  1607. */
  1608. function tripal_core_generate_chado_var($table, $values, $base_options = array()) {
  1609. $all = new stdClass();
  1610. $return_array = 0;
  1611. if (array_key_exists('return_array', $base_options)) {
  1612. $return_array = 1;
  1613. }
  1614. $include_fk = 0;
  1615. if (array_key_exists('include_fk', $base_options)) {
  1616. $include_fk = $base_options['include_fk'];
  1617. }
  1618. // get description for the current table----------------------------------------------------------
  1619. $table_desc = tripal_core_get_chado_table_schema($table);
  1620. if (!$table_desc or count($table_desc) == 0) {
  1621. watchdog('tripal_core', "tripal_core_generate_chado_var: The table '%table' has not been defined. ".
  1622. "and cannot be expanded. If this is a custom table, please add it using the Tripal ".
  1623. "custom table interface.", array('%table' => $table), WATCHDOG_ERROR);
  1624. if ($return_array) {
  1625. return array();
  1626. }
  1627. return FALSE;
  1628. }
  1629. $table_primary_key = $table_desc['primary key'][0];
  1630. $table_columns = array_keys($table_desc['fields']);
  1631. // Expandable fields without value needed for criteria--------------------------------------------
  1632. $all->expandable_fields = array();
  1633. if ($table_desc['referring_tables']) {
  1634. $all->expandable_tables = $table_desc['referring_tables'];
  1635. }
  1636. else {
  1637. $all->expandable_tables = array();
  1638. }
  1639. $all->expandable_nodes = array();
  1640. // Get fields to be removed by name.................................
  1641. $fields_to_remove = module_invoke_all('exclude_field_from_' . $table . '_by_default');
  1642. foreach ($fields_to_remove as $field_name => $criteria) {
  1643. //replace &gt;field_name&lt; with the current field name &
  1644. $criteria = preg_replace('/&gt;field_name&lt; /', addslashes($field_name), $criteria);
  1645. // if field_value needed we can't deal with this field yet
  1646. if (preg_match('/&gt;field_value&lt; /', $criteria)) {
  1647. break;
  1648. }
  1649. //if criteria then remove from query
  1650. // @coder-ignore: only module designers can populate $criteria -not security risk
  1651. $success = drupal_eval('<?php return ' . $criteria . '; ?>');
  1652. // watchdog('tripal_core',
  1653. // 'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for %table evaluated to %success',
  1654. // array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
  1655. // WATCHDOG_NOTICE
  1656. // );
  1657. if ($success) {
  1658. unset($table_columns[array_search($field_name, $table_columns)]);
  1659. unset($fields_to_remove[$field_name]);
  1660. $all->expandable_fields[] = $table . '.' . $field_name;
  1661. }
  1662. }
  1663. //Get fields to be removed by type................................
  1664. $types_to_remove = module_invoke_all('exclude_type_by_default');
  1665. $field_types = array();
  1666. foreach ($table_desc['fields'] as $field_name => $field_array) {
  1667. $field_types[$field_array['type']][] = $field_name;
  1668. }
  1669. foreach ($types_to_remove as $field_type => $criteria) {
  1670. // if there are fields of that type to remove
  1671. if (is_array($field_types[$field_type])) {
  1672. //replace &gt;field_name&lt; with the current field name &
  1673. $criteria = preg_replace('/&gt;field_name&lt; /', addslashes($field_name), $criteria);
  1674. foreach ($field_types[$field_type] as $field_name) {
  1675. // if field_value needed we can't deal with this field yet
  1676. if (preg_match('/&gt;field_value&lt; /', $criteria)) {
  1677. $fields_to_remove[$field_name] = $criteria;
  1678. continue;
  1679. }
  1680. // if field_value needed we can't deal with this field yet
  1681. if (preg_match('/&gt;field_value&lt; /', $criteria)) {
  1682. break;
  1683. }
  1684. //if criteria then remove from query
  1685. // @coder-ignore: only module designers can populate $criteria -not security risk
  1686. $success = drupal_eval('<?php return ' . $criteria . '; ?>');
  1687. // watchdog('tripal_core',
  1688. // 'Evaluating criteria (%criteria) for field %field of $type in tripal_core_generate_chado_var for %table evaluated to %success',
  1689. // array('%table'=>$table, '%criteria'=>$criteria, '%field'=>$field_name, '%type'=>$field_type, '%success'=>$success),
  1690. // WATCHDOG_NOTICE
  1691. // );
  1692. if ($success) {
  1693. unset($table_columns[array_search($field_name, $table_columns)]);
  1694. $all->expandable_fields[] = $table . '.' . $field_name;
  1695. }
  1696. } //end of foreach field of that type
  1697. }
  1698. } //end of foreach type to be removed
  1699. // get the values for the record in the current table---------------------------------------------
  1700. $results = tripal_core_chado_select($table, $table_columns, $values, $base_options);
  1701. if ($results) {
  1702. foreach ($results as $key => $object) {
  1703. // Add empty expandable_x arrays
  1704. $object->expandable_fields = $all->expandable_fields;
  1705. $object->expandable_tables = $all->expandable_tables;
  1706. $object->expandable_nodes = $all->expandable_nodes;
  1707. // add curent table
  1708. $object->tablename = $table;
  1709. // check if the current table maps to a node type-----------------------------------------------
  1710. // if this table is connected to a node there will be a chado_tablename table in drupal
  1711. if (db_table_exists('chado_' . $table)) {
  1712. // that has a foreign key to this one ($table_desc['primary key'][0]
  1713. // and to the node table (nid)
  1714. $sql = "SELECT %s, nid FROM {chado_%s} WHERE %s=%d";
  1715. $mapping = db_fetch_object(db_query($sql, $table_primary_key, $table,
  1716. $table_primary_key, $object->{$table_primary_key}));
  1717. if ($mapping->{$table_primary_key}) {
  1718. $object->nid = $mapping->nid;
  1719. $object->expandable_nodes[] = $table;
  1720. }
  1721. }
  1722. // remove any fields where criteria need to be evalulated---------------------------------------
  1723. foreach ($fields_to_remove as $field_name => $criteria) {
  1724. if (!isset($object->{$field_name})) {
  1725. break;
  1726. }
  1727. $criteria = preg_replace('/&gt;field_value&lt; /', addslashes($object->{$field_name}), $criteria);
  1728. //if criteria then remove from query
  1729. // @coder-ignore: only module designers can populate $criteria -not security risk
  1730. $success = drupal_eval('<?php return ' . $criteria . '; ?>');
  1731. // watchdog('tripal_core',
  1732. // 'Evaluating criteria (%criteria) for field %field in tripal_core_generate_chado_var for %table evaluated to %success',
  1733. // array('%table' => $table, '%criteria'=>$criteria, '%field' => $field_name, '%success'=>$success),
  1734. // WATCHDOG_NOTICE
  1735. // );
  1736. if ($success) {
  1737. unset($object->{$field_name});
  1738. $object->expandable_fields[] = $table . '.' . $field_name;
  1739. }
  1740. }
  1741. // recursively follow foreign key relationships nesting objects as we go------------------------
  1742. if ($table_desc['foreign keys']) {
  1743. foreach ($table_desc['foreign keys'] as $foreign_key_array) {
  1744. $foreign_table = $foreign_key_array['table'];
  1745. foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
  1746. // Note: Foreign key is the field in the current table whereas primary_key is the field in
  1747. // the table referenced by the foreign key
  1748. //Dont do anything if the foreign key is empty
  1749. if (empty($object->{$foreign_key})) {
  1750. continue;
  1751. }
  1752. if ($include_fk) {
  1753. // don't recurse if the callee has supplied an $fk_include list and this
  1754. // FK table is not in the list.
  1755. if (is_array($include_fk) and !array_key_exists($foreign_key, $include_fk)) {
  1756. continue;
  1757. }
  1758. // if we have the option but it is not an array then we don't recurse any furutehr
  1759. if (!is_array($include_fk)) {
  1760. continue;
  1761. }
  1762. }
  1763. // get the record from the foreign table
  1764. $foreign_values = array($primary_key => $object->{$foreign_key});
  1765. $options = array();
  1766. if (is_array($include_fk)) {
  1767. $options['include_fk'] = $include_fk[$foreign_key];
  1768. }
  1769. $foreign_object = tripal_core_generate_chado_var($foreign_table, $foreign_values, $options);
  1770. // add the foreign record to the current object in a nested manner
  1771. $object->{$foreign_key} = $foreign_object;
  1772. // Flatten expandable_x arrays so only in the bottom object
  1773. if (is_array($object->{$foreign_key}->expandable_fields)) {
  1774. $object->expandable_fields = array_merge(
  1775. $object->expandable_fields,
  1776. $object->{$foreign_key}->expandable_fields
  1777. );
  1778. unset($object->{$foreign_key}->expandable_fields);
  1779. }
  1780. if (is_array($object->{$foreign_key}->expandable_tables)) {
  1781. $object->expandable_tables = array_merge(
  1782. $object->expandable_tables,
  1783. $object->{$foreign_key}->expandable_tables
  1784. );
  1785. unset($object->{$foreign_key}->expandable_tables);
  1786. }
  1787. if (is_array($object->{$foreign_key}->expandable_nodes)) {
  1788. $object->expandable_nodes = array_merge(
  1789. $object->expandable_nodes,
  1790. $object->{$foreign_key}->expandable_nodes
  1791. );
  1792. unset($object->{$foreign_key}->expandable_nodes);
  1793. }
  1794. }
  1795. }
  1796. $results[$key] = $object;
  1797. }
  1798. }
  1799. }
  1800. // check only one result returned
  1801. if (!$return_array) {
  1802. if (sizeof($results) == 1) {
  1803. // add results to object
  1804. return $results[0];
  1805. }
  1806. elseif (!empty($results)) {
  1807. return $results;
  1808. }
  1809. else {
  1810. // no results returned
  1811. }
  1812. }
  1813. // the caller has requested results are always returned as
  1814. // an array
  1815. else {
  1816. if (!$results) {
  1817. return array();
  1818. }
  1819. else {
  1820. return $results;
  1821. }
  1822. }
  1823. }
  1824. /**
  1825. * Retrieves fields/tables/nodes that were excluded by default from a variable and adds them
  1826. *
  1827. * This function exists to allow tripal_core_generate_chado_var() to excldue some
  1828. * fields/tables/nodes from the default form of a variable without making it extremely difficult for
  1829. * the tripal admin to get at these variables if he/she wants them.
  1830. *
  1831. * @param $object
  1832. * This must be an object generated using tripal_core_generate_chado_var()
  1833. * @param $type
  1834. * Must be one of 'field', 'table', 'node'. Indicates what is being expanded.
  1835. * @param $to_expand
  1836. * The name of the field/table/node to be expanded
  1837. * @param $table_options
  1838. * An array containing options for the base table. For example, an
  1839. * option of 'order_by' may be used to sort results in the base table
  1840. * if more than one are returned. The options must be compatible with
  1841. * the options accepted by the tripal_core_chado_select() function.
  1842. * Additionally, The option 'return_array' can be provided to force
  1843. * the function to expand tables as an array. Default behavior is to expand
  1844. * a table as single record if only one record exists or to expand as an array if
  1845. * multiple records exist.
  1846. * @return
  1847. * A chado object supplemented with the field/table/node requested to be expanded.
  1848. * If the type is a table and it has already been expanded no changes is made to the
  1849. * returned object
  1850. *
  1851. * Example Usage:
  1852. * @code
  1853. // Get a chado object to be expanded
  1854. $values = array(
  1855. 'name' => 'Medtr4g030710'
  1856. );
  1857. $features = tripal_core_generate_chado_var('feature', $values);
  1858. // Expand the organism node
  1859. $feature = tripal_core_expand_chado_vars($feature, 'node', 'organism');
  1860. // Expand the feature.residues field
  1861. $feature = tripal_core_expand_chado_vars($feature, 'field', 'feature.residues');
  1862. // Expand the feature properties (featureprop table)
  1863. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureprop');
  1864. * @endcode
  1865. *
  1866. * @ingroup tripal_chado_api
  1867. */
  1868. function tripal_core_expand_chado_vars($object, $type, $to_expand, $table_options = array()) {
  1869. $base_table = $object->tablename;
  1870. // check to see if we are expanding an array of objects
  1871. if (is_array($object)) {
  1872. foreach ($object as $index => $o) {
  1873. $object[$index] = tripal_core_expand_chado_vars($o, $type, $to_expand);
  1874. }
  1875. return $object;
  1876. }
  1877. switch ($type) {
  1878. case "field": //--------------------------------------------------------------------------------
  1879. if (preg_match('/(\w+)\.(\w+)/', $to_expand, $matches)) {
  1880. $tablename = $matches[1];
  1881. $fieldname = $matches[2];
  1882. $table_desc = tripal_core_get_chado_table_schema($tablename);
  1883. $values = array();
  1884. foreach ($table_desc['primary key'] as $key) {
  1885. $values[$key] = $object->{$key};
  1886. }
  1887. if ($base_table == $tablename) {
  1888. //get the field
  1889. $results = tripal_core_chado_select($tablename, array($fieldname), $values);
  1890. $object->{$fieldname} = $results[0]->{$fieldname};
  1891. $object->expanded = $to_expand;
  1892. }
  1893. else {
  1894. //We need to recurse -the field is in a nested object
  1895. foreach ((array) $object as $field_name => $field_value) {
  1896. if (is_object($field_value)) {
  1897. $object->{$field_name} = tripal_core_expand_chado_vars(
  1898. $field_value,
  1899. 'field',
  1900. $to_expand
  1901. );
  1902. }
  1903. } //end of for each field in the current object
  1904. }
  1905. }
  1906. else {
  1907. watchdog('tripal_core', 'tripal_core_expand_chado_vars: Field (%field) not in the right format. ".
  1908. "It should be <tablename>.<fieldname>', WATCHDOG_ERROR);
  1909. }
  1910. break;
  1911. case "table": //--------------------------------------------------------------------------------
  1912. $foreign_table = $to_expand;
  1913. // don't expand the table it already is expanded
  1914. if (array_key_exists($foreign_table, $object)) {
  1915. return $object;
  1916. }
  1917. $foreign_table_desc = tripal_core_get_chado_table_schema($foreign_table);
  1918. // If it's connected to the base table
  1919. if ($foreign_table_desc['foreign keys'][$base_table]) {
  1920. foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
  1921. if (!$object->{$right}) {
  1922. continue;
  1923. }
  1924. if (is_array($values)) {
  1925. $values = array_merge($values, array($left => $object->{$right}) );
  1926. }
  1927. else {
  1928. $values = array($left => $object->{$right});
  1929. }
  1930. // if a prepared statement is provide then generate a new name
  1931. $new_options = $table_options;
  1932. if (array_key_exists('statement_name', $table_options)) {
  1933. $new_options['statement_name'] = "exp_" . $foreign_table . "_" . substr($left, 0, 2) . substr($right, 0, 2);
  1934. }
  1935. $foreign_object = tripal_core_generate_chado_var($foreign_table, array($left => $object->{$right}), $new_options);
  1936. if ($foreign_object) {
  1937. // in the case where the foreign key relationships exists more
  1938. // than once with the same table we want to alter the
  1939. // array structure
  1940. if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
  1941. if (!is_object($object->{$foreign_table})) {
  1942. $object->{$foreign_table} = new stdClass();
  1943. }
  1944. $object->{$foreign_table}->{$left} = $foreign_object;
  1945. $object->expanded = $to_expand;
  1946. }
  1947. else {
  1948. $object->{$foreign_table} = $foreign_object;
  1949. $object->expanded = $to_expand;
  1950. }
  1951. }
  1952. // if the object returned is NULL then handle that
  1953. else {
  1954. if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
  1955. $object->{$foreign_table}->{$left} = NULL;
  1956. }
  1957. else {
  1958. $object->{$foreign_table} = NULL;
  1959. }
  1960. }
  1961. }
  1962. }
  1963. else {
  1964. // We need to recurse -the table has a relationship to one of the nested objects
  1965. $did_expansion = 0;
  1966. foreach ((array) $object as $field_name => $field_value) {
  1967. // if we have a nested object ->expand the table in it
  1968. if (is_object($field_value)) {
  1969. $did_expansion = 1;
  1970. $object->{$field_name} = tripal_core_expand_chado_vars($field_value, 'table', $foreign_table);
  1971. }
  1972. }
  1973. // if we did not expand this table we should return a message that the foreign table
  1974. // could not be expanded
  1975. if (!$did_expansion) {
  1976. watchdog('tripal_core', 'tripal_core_expand_chado_vars: Could not expand table, %table. It is ',
  1977. 'not in a foreign key relationship with the base object nor with any other expanded table. ' .
  1978. 'Check the table definition to ensure that a proper foreign key relationship is present.',
  1979. array('%table' => $foreign_table), WATCHDOG_ERROR);
  1980. }
  1981. }
  1982. break;
  1983. case "node": //---------------------------------------------------------------------------------
  1984. //if the node to be expanded is for our base table, then just expand it
  1985. if ($object->tablename == $to_expand) {
  1986. $node = node_load($object->nid);
  1987. if ($node) {
  1988. $object->expanded = $to_expand;
  1989. $node->expandable_fields = $object->expandable_fields;
  1990. unset($object->expandable_fields);
  1991. $node->expandable_tables = $object->expandable_tables;
  1992. unset($object->expandable_tables);
  1993. $node->expandable_nodes = $object->expandable_nodes;
  1994. unset($object->expandable_nodes);
  1995. $node->{$base_table} = $object;
  1996. $object = $node;
  1997. }
  1998. else {
  1999. watchdog('tripal_core', 'tripal_core_expand_chado_vars: No node matches the nid (%nid) supplied.',
  2000. array('%nid' => $object->nid), WATCHDOG_ERROR);
  2001. } //end of if node
  2002. }
  2003. else {
  2004. //We need to recurse -the node to expand is one of the nested objects
  2005. foreach ((array) $object as $field_name => $field_value) {
  2006. if (is_object($field_value)) {
  2007. $object->{$field_name} = tripal_core_expand_chado_vars(
  2008. $field_value,
  2009. 'node',
  2010. $to_expand
  2011. );
  2012. }
  2013. } //end of for each field in the current object
  2014. }
  2015. break;
  2016. default:
  2017. watchdog('tripal_core', 'tripal_core_expand_chado_vars: Unrecognized type (%type). Should be one of "field", "table", "node".',
  2018. array('%type' => $type), WATCHDOG_ERROR);
  2019. return FALSE;
  2020. }
  2021. //move extended array downwards-------------------------------------------------------------------
  2022. if (!$object->expanded) {
  2023. //if there's no extended field then go hunting for it
  2024. foreach ( (array)$object as $field_name => $field_value) {
  2025. if (is_object($field_value)) {
  2026. if (isset($field_value->expanded)) {
  2027. $object->expanded = $field_value->expanded;
  2028. unset($field_value->expanded);
  2029. }
  2030. }
  2031. }
  2032. }
  2033. //try again becasue now we might have moved it down
  2034. if ($object->expanded) {
  2035. $expandable_name = 'expandable_' . $type . 's';
  2036. if ($object->{$expandable_name}) {
  2037. $key_to_remove = array_search($object->expanded, $object->{$expandable_name});
  2038. unset($object->{$expandable_name}[$key_to_remove]);
  2039. unset($object->expanded);
  2040. }
  2041. else {
  2042. // if there is an expandable array then we've reached the base object
  2043. // if we get here and don't have anything expanded then something went wrong
  2044. // watchdog(
  2045. // 'tripal_core',
  2046. // 'tripal_core_expand_chado_vars: Unable to expand the %type %to_expand',
  2047. // array('%type'=>$type, '%to_expand'=>$to_expand),
  2048. // WATCHDOG_ERROR
  2049. // );
  2050. } //end of it we've reached the base object
  2051. }
  2052. return $object;
  2053. }
  2054. /**
  2055. * Implements hook_exclude_type_by_default()
  2056. *
  2057. * This hooks allows fields of a specified type that match a specified criteria to be excluded by
  2058. * default from any table when tripal_core_generate_chado_var() is called. Keep in mind that if
  2059. * fields are excluded by default they can always be expanded at a later date using
  2060. * tripal_core_expand_chado_vars().
  2061. *
  2062. * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
  2063. * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
  2064. * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
  2065. * contain the following tokens:
  2066. * - &gt;field_name&lt;
  2067. * Replaced by the name of the field to be excluded
  2068. * - &gt;field_value&lt;
  2069. * Replaced by the value of the field in the current record
  2070. * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt; token then it will be
  2071. * evaluated before the query is executed and if the field is excluded it won't be included in the
  2072. * query.
  2073. *
  2074. * @return
  2075. * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  2076. *
  2077. * @ingroup tripal_chado_api
  2078. */
  2079. function tripal_core_exclude_type_by_default() {
  2080. return array('text' => 'strlen("&gt;field_value&lt; ") > 100');
  2081. }
  2082. /**
  2083. * Implements hook_exclude_field_from_<tablename>_by_default()
  2084. *
  2085. * This hooks allows fields from a specified table that match a specified criteria to be excluded by
  2086. * default from any table when tripal_core_generate_chado_var() is called. Keep in mind that if
  2087. * fields are excluded by default they can always be expanded at a later date using
  2088. * tripal_core_expand_chado_vars().
  2089. *
  2090. * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
  2091. * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
  2092. * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
  2093. * contain the following tokens:
  2094. * - &gt;field_name&lt;
  2095. * Replaced by the name of the field to be excluded
  2096. * - &gt;field_value&lt;
  2097. * Replaced by the value of the field in the current record
  2098. * Also keep in mind that if your criteria doesn't contain the &gt;field_value&lt; token then it will be
  2099. * evaluated before the query is executed and if the field is excluded it won't be included in the
  2100. * query.
  2101. *
  2102. * @return
  2103. * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
  2104. *
  2105. * @ingroup tripal_chado_api
  2106. */
  2107. function tripal_core_exclude_field_from_feature_by_default() {
  2108. return array();
  2109. }
  2110. /**
  2111. * Use this function instead of db_query() to avoid switching databases
  2112. * when making query to the chado database
  2113. *
  2114. * Will use a chado persistent connection if it already exists
  2115. *
  2116. * @param $sql
  2117. * The sql statement to execute
  2118. *
  2119. * @returns
  2120. * A database query result resource or FALSE if the query was not
  2121. * executed correctly
  2122. */
  2123. function chado_query($sql) {
  2124. global $persistent_chado;
  2125. $is_local = tripal_core_is_chado_local();
  2126. $args = func_get_args();
  2127. array_shift($args); // remove the $sql from the argument list
  2128. $sql = db_prefix_tables($sql);
  2129. if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
  2130. $args = $args[0];
  2131. }
  2132. // run the Drupal command to clean up the SQL
  2133. _db_query_callback($args, TRUE);
  2134. $sql = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $sql);
  2135. // add the chado schema to the table names if Chado is local to the Drupal database
  2136. if ($is_local) {
  2137. // TODO: this regular expression really needs to be removed as there are too many
  2138. // cases where it could break. Instead we need to surround tables with brackets
  2139. // like Drupal tables are and then find those and prefix those with chado.
  2140. $sql = preg_replace('/\n/', '', $sql); // remove carriage returns
  2141. // in the statement below we want to add 'chado.' to the beginning of each table
  2142. // we use the FROM keyword to look for tables, but FROM is also used in the
  2143. // 'substring' function of postgres. But since table names can't start with
  2144. // a number we exclude words numeric values. We also exclude tables that
  2145. // already have a schema prefix.
  2146. $sql = preg_replace('/FROM\s+([^0123456789\(][^\.]*?)(\s|$)/i', 'FROM chado.\1 ', $sql);
  2147. $sql = preg_replace('/INNER\s+JOIN\s+([^\.]*?)\s/i', 'INNER JOIN chado.\1 ', $sql);
  2148. }
  2149. //print "$sql\n";
  2150. // If the query is not a select then we still need to change the search_path
  2151. if (!preg_match('/^SELECT/i',$sql)) {
  2152. $change_path = TRUE;
  2153. }
  2154. // Execute the query on the chado database/schema
  2155. // Use the persistent chado connection if it already exists
  2156. if ($persistent_chado) {
  2157. $query = $sql;
  2158. // Duplicate the _db_query code in order to ensure that the drupal
  2159. // $active_db variable is not used in the pg_query command
  2160. // thus changed $active_db to $persistent_chado
  2161. // START COPY FROM _db_query in database.pgsql.inc
  2162. if (variable_get('dev_query', 0)) {
  2163. list($usec, $sec) = explode(' ', microtime());
  2164. $timer = (float) $usec + (float) $sec;
  2165. }
  2166. // if we're local we can just run the query
  2167. if ($is_local) {
  2168. if ($change_path) {
  2169. $previous_db = tripal_db_set_active('chado');
  2170. }
  2171. $last_result = pg_query($persistent_chado, $query);
  2172. if ($change_path) {
  2173. tripal_db_set_active($previous_db);
  2174. }
  2175. }
  2176. else {
  2177. $previous_db = tripal_db_set_active('chado');
  2178. $last_result = pg_query($persistent_chado, $query);
  2179. tripal_db_set_active($previous_db);
  2180. }
  2181. if (variable_get('dev_query', 0)) {
  2182. $bt = debug_backtrace();
  2183. $query = $bt[2]['function'] . "\n" . $query;
  2184. list($usec, $sec) = explode(' ', microtime());
  2185. $stop = (float) $usec + (float) $sec;
  2186. $diff = $stop - $timer;
  2187. $queries[] = array($query, $diff);
  2188. }
  2189. if ($last_result !== FALSE) {
  2190. return $last_result;
  2191. }
  2192. else {
  2193. // Indicate to drupal_error_handler that this is a database error.
  2194. ${DB_ERROR} = TRUE;
  2195. trigger_error(check_plain(pg_last_error($persistent_chado) . "\nquery: " . $query), E_USER_WARNING);
  2196. return FALSE;
  2197. }
  2198. // END COPY FROM _db_query in database.pgsql.inc
  2199. }
  2200. else {
  2201. // before running the query we want to prefix the table names with
  2202. // the chado schema. Previously use had to make changes to the
  2203. // search_path but that caused a lot of database calls and wasted
  2204. // resources during long jobs.
  2205. if ($is_local) {
  2206. if ($change_path) {
  2207. $previous_db = tripal_db_set_active('chado');
  2208. }
  2209. $results = _db_query($sql);
  2210. if ($change_path) {
  2211. tripal_db_set_active($previous_db);
  2212. }
  2213. }
  2214. else {
  2215. $previous_db = tripal_db_set_active('chado') ;
  2216. $results = _db_query($sql);
  2217. tripal_db_set_active($previous_db);
  2218. }
  2219. }
  2220. return $results;
  2221. }
  2222. /**
  2223. * Get chado id for a node. E.g, if you want to get 'analysis_id' from the
  2224. * 'analysis' table for a synced 'chado_analysis' node, use:
  2225. * $analysis_id = chado_get_id_for_node ('analysis', $node)
  2226. * Likewise,
  2227. * $organism_id = chado_get_id_for_node ('organism', $node)
  2228. * $feature_id = chado_get_id_for_node ('feature', $node)
  2229. */
  2230. function chado_get_id_for_node($table, $node) {
  2231. return db_result(db_query("SELECT %s_id FROM {chado_%s} WHERE nid = %d", $table, $table, $node->nid));
  2232. }
  2233. /**
  2234. * Get node id for a chado feature/organism/analysis. E.g, if you want to
  2235. * get the node id for an analysis, use:
  2236. * $nid = chado_get_node_id ('analysis', $analysis_id)
  2237. * Likewise,
  2238. * $nid = chado_get_node_id ('organism', $organism_id)
  2239. * $nid = chado_get_node_id ('feature', $feature_id)
  2240. */
  2241. function chado_get_node_id($table, $id) {
  2242. return db_result(db_query("SELECT nid FROM {chado_%s} WHERE %s_id = %d", $table, $table, $id));
  2243. }
  2244. /**
  2245. * Retrieve a property for a given base table record
  2246. *
  2247. * @param $basetable
  2248. * The base table for which the property should be retrieved. Thus to retrieve a property
  2249. * for a feature the basetable=feature and property is retrieved from featureprop
  2250. * @param $record_id
  2251. * The foriegn key field of the base table. This should be in integer.
  2252. * @param $property
  2253. * The cvterm name describing the type of properties to be retrieved
  2254. * @param $cv_name
  2255. * The name of the cv that the above cvterm is part of
  2256. *
  2257. * @return
  2258. * An array in the same format as that generated by the function
  2259. * tripal_core_generate_chado_var(). If only one record is returned it
  2260. * is a single object. If more than one record is returned then it is an array
  2261. * of objects
  2262. *
  2263. * @ingroup tripal_chado_api
  2264. */
  2265. function tripal_core_get_property($basetable, $record_id, $property, $cv_name) {
  2266. // get the foreign key for this property table
  2267. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  2268. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  2269. // construct the array of values to be selected
  2270. $values = array(
  2271. $fkcol => $record_id,
  2272. 'type_id' => array(
  2273. 'cv_id' => array(
  2274. 'name' => $cv_name,
  2275. ),
  2276. 'name' => $property,
  2277. 'is_obsolete' => 0
  2278. ),
  2279. );
  2280. $results = tripal_core_generate_chado_var($basetable . 'prop', $values);
  2281. $results = tripal_core_expand_chado_vars($results, 'field', $basetable . 'prop.value');
  2282. return $results;
  2283. }
  2284. /**
  2285. * Insert a property for a given base table. By default if the property already
  2286. * exists a new property is added with the next available rank. If
  2287. * $update_if_present argument is specified then the record will be updated if it
  2288. * exists rather than adding a new property.
  2289. *
  2290. * @param $basetable
  2291. * The base table for which the property should be inserted. Thus to insert a property
  2292. * for a feature the basetable=feature and property is inserted into featureprop
  2293. * @param $record_id
  2294. * The foriegn key field of the base table. This should be in integer.
  2295. * @param $property
  2296. * The cvterm name describing the type of properties to be inserted
  2297. * @param $cv_name
  2298. * The name of the cv that the above cvterm is part of
  2299. * @param $value
  2300. * The value of the property to be inserted (can be empty)
  2301. * @param $update_if_present
  2302. * A boolean indicating whether an existing record should be updated. If the
  2303. * property already exists and this value is not specified or is zero then
  2304. * a new property will be added with the next largest rank.
  2305. *
  2306. * @return
  2307. * Return True on Insert/Update and False otherwise
  2308. *
  2309. * @ingroup tripal_chado_api
  2310. */
  2311. function tripal_core_insert_property($basetable, $record_id, $property,
  2312. $cv_name, $value, $update_if_present = 0) {
  2313. // first see if the property already exists, if the user want's to update
  2314. // then we can do that, but otherwise we want to increment the rank and
  2315. // insert
  2316. $props = tripal_core_get_property($basetable, $record_id, $property, $cv_name);
  2317. if (!is_array($props)) {
  2318. $props = array($props);
  2319. }
  2320. $rank = 0;
  2321. if (count($props) > 0) {
  2322. if ($update_if_present) {
  2323. return tripal_core_update_property($basetable, $record_id, $property, $cv_name, $value);
  2324. }
  2325. else {
  2326. // iterate through the properties returned and check to see if the
  2327. // property with this value already exists if not, get the largest rank
  2328. // and insert the same property but with this new value
  2329. foreach ($props as $p) {
  2330. if ($p->rank > $rank) {
  2331. $rank = $p->rank;
  2332. }
  2333. if (strcmp($p->value, $value) == 0) {
  2334. return TRUE;
  2335. }
  2336. }
  2337. // now add 1 to the rank
  2338. $rank++;
  2339. }
  2340. }
  2341. else {
  2342. watchdog('tripal_core', "Cannot find property '!prop_name'.",
  2343. array('!prop_name' => $property), WATCHDOG_ERROR);
  2344. }
  2345. // get the foreign key for this property table
  2346. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  2347. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  2348. // construct the array of values to be inserted
  2349. $values = array(
  2350. $fkcol => $record_id,
  2351. 'type_id' => array(
  2352. 'cv_id' => array(
  2353. 'name' => $cv_name,
  2354. ),
  2355. 'name' => $property,
  2356. 'is_obsolete' => 0
  2357. ),
  2358. 'value' => $value,
  2359. 'rank' => $rank,
  2360. );
  2361. return tripal_core_chado_insert($basetable . 'prop', $values);
  2362. }
  2363. /**
  2364. * Update a property for a given base table record and property name. This
  2365. * function should be used only if one record of the property will be present.
  2366. * If the property name can have multiple entries (with increasing rank) then
  2367. * use the function named tripal_core_update_property_by_id
  2368. *
  2369. * @param $basetable
  2370. * The base table for which the property should be updated. The property table
  2371. * is constructed using a combination of the base table name and the suffix
  2372. * 'prop' (e.g. basetable = feature then property tabie is featureprop).
  2373. * @param $record_id
  2374. * The foreign key of the basetable to update a property for. This should be in integer.
  2375. * For example, if the basetable is 'feature' then the $record_id should be the feature_id
  2376. * @param $property
  2377. * The cvterm name of property to be updated
  2378. * @param $cv_name
  2379. * The name of the cv that the above cvterm is part of
  2380. * @param $value
  2381. * The value of the property to be inserted (can be empty)
  2382. * @param $insert_if_missing
  2383. * A boolean indicating whether a record should be inserted if one doesn't exist to update
  2384. *
  2385. * Note: The property to be updated is select via the unique combination of $record_id and
  2386. * $property and then it is updated with the supplied value
  2387. *
  2388. * @return
  2389. * Return True on Update/Insert and False otherwise
  2390. *
  2391. * @ingroup tripal_chado_api
  2392. */
  2393. function tripal_core_update_property($basetable, $record_id, $property,
  2394. $cv_name, $value, $insert_if_missing = 0) {
  2395. // first see if the property is missing (we can't update a missing property
  2396. $prop = tripal_core_get_property($basetable, $record_id, $property, $cv_name);
  2397. if (count($prop)==0) {
  2398. if ($insert_if_missing) {
  2399. return tripal_core_insert_property($basetable, $record_id, $property, $cv_name, $value);
  2400. }
  2401. else {
  2402. return FALSE;
  2403. }
  2404. }
  2405. // get the foreign key for this property table
  2406. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  2407. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  2408. // construct the array that will match the exact record to update
  2409. $match = array(
  2410. $fkcol => $record_id,
  2411. 'type_id' => array(
  2412. 'cv_id' => array(
  2413. 'name' => $cv_name,
  2414. ),
  2415. 'name' => $property,
  2416. ),
  2417. );
  2418. // construct the array of values to be updated
  2419. $values = array(
  2420. 'value' => $value,
  2421. );
  2422. return tripal_core_chado_update($basetable . 'prop', $match, $values);
  2423. }
  2424. /**
  2425. * Update a property for a given base table record. This function should be
  2426. * used if multiple records of the same property will be present. Also, use this
  2427. * function to change the property name of an existing property.
  2428. *
  2429. * @param $basetable
  2430. * The base table for which the property should be updated. The property table
  2431. * is constructed using a combination of the base table name and the suffix
  2432. * 'prop' (e.g. basetable = feature then property tabie is featureprop).
  2433. * @param $record_id
  2434. * The primary key of the base table. This should be in integer.
  2435. * For example, if the basetable is 'feature' then the $record_id should be the featureprop_id
  2436. * @param $property
  2437. * The cvterm name of property to be updated
  2438. * @param $cv_name
  2439. * The name of the cv that the above cvterm is part of
  2440. * @param $value
  2441. * The value of the property to be inserted (can be empty)
  2442. *
  2443. * @return
  2444. * Return True on Update/Insert and False otherwise
  2445. *
  2446. * @ingroup tripal_chado_api
  2447. */
  2448. function tripal_core_update_property_by_id($basetable, $record_id, $property,
  2449. $cv_name, $value) {
  2450. // get the primary key for this property table
  2451. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  2452. $pkcol = $table_desc['primary key'][0];
  2453. // construct the array that will match the exact record to update
  2454. $match = array(
  2455. $pkcol => $record_id,
  2456. );
  2457. // construct the array of values to be updated
  2458. $values = array(
  2459. 'type_id' => array(
  2460. 'cv_id' => array(
  2461. 'name' => $cv_name,
  2462. ),
  2463. 'name' => $property,
  2464. ),
  2465. 'value' => $value,
  2466. );
  2467. return tripal_core_chado_update($basetable . 'prop', $match, $values);
  2468. }
  2469. /**
  2470. * Deletes a property for a given base table record using the property name
  2471. *
  2472. * @param $basetable
  2473. * The base table for which the property should be deleted. Thus to deleted a property
  2474. * for a feature the basetable=feature and property is deleted from featureprop
  2475. * @param $record_id
  2476. * The primary key of the basetable to delete a property for. This should be in integer.
  2477. * @param $property
  2478. * The cvterm name describing the type of property to be deleted
  2479. * @param $cv_name
  2480. * The name of the cv that the above cvterm is part of
  2481. *
  2482. * Note: The property to be deleted is select via the unique combination of $record_id and $property
  2483. *
  2484. * @return
  2485. * Return True on Delete and False otherwise
  2486. *
  2487. * @ingroup tripal_chado_api
  2488. */
  2489. function tripal_core_delete_property($basetable, $record_id, $property, $cv_name) {
  2490. // get the foreign key for this property table
  2491. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  2492. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  2493. // construct the array that will match the exact record to update
  2494. $match = array(
  2495. $fkcol => $record_id,
  2496. 'type_id' => array(
  2497. 'cv_id' => array(
  2498. 'name' => $cv_name,
  2499. ),
  2500. 'name' => $property,
  2501. ),
  2502. );
  2503. return tripal_core_chado_delete($basetable . 'prop', $match);
  2504. }
  2505. /**
  2506. * Deletes a property using the property ID
  2507. *
  2508. * @param $basetable
  2509. * The base table for which the property should be deleted. Thus to deleted a property
  2510. * for a feature the basetable=feature and property is deleted from featureprop
  2511. * @param $record_id
  2512. * The primary key of the basetable to delete a property for. This should be in integer.
  2513. *
  2514. * @return
  2515. * Return True on Delete and False otherwise
  2516. *
  2517. * @ingroup tripal_chado_api
  2518. */
  2519. function tripal_core_delete_property_by_id($basetable, $record_id) {
  2520. // get the foreign key for this property table
  2521. $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
  2522. $pkcol = $table_desc['primary key'][0];
  2523. // construct the array that will match the exact record to update
  2524. $match = array(
  2525. $pkcol => $record_id,
  2526. );
  2527. return tripal_core_chado_delete($basetable . 'prop', $match);
  2528. }
  2529. /**
  2530. * This function is typically used in the '.install' file for a Tripal module
  2531. * Each module should call this function during installation to create
  2532. * the module data directory which is sites/default/files/tripal/[module_name]
  2533. * for default Drupal settings. This directory can then be used by the module
  2534. * for storing files.
  2535. *
  2536. * @param $module_name
  2537. * the name of the module being installed.
  2538. *
  2539. * @returns
  2540. * nothing
  2541. *
  2542. * @ingroup tripal_files_api
  2543. */
  2544. function tripal_create_moddir($module_name) {
  2545. // make the data directory for this module
  2546. $data_dir = file_directory_path() . "/tripal/$module_name";
  2547. if (!file_check_directory($data_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  2548. $message = "Cannot create directory $data_dir. This module may not ".
  2549. "behave correctly without this directory. Please create ".
  2550. "the directory manually or fix the problem and reinstall.";
  2551. drupal_set_message(check_plain(t($message)), 'error');
  2552. watchdog('tripal_core', $message, array(), WATCHDOG_ERROR);
  2553. }
  2554. }
  2555. /**
  2556. * Each Tripal module has a unique data directory which was creatd using the
  2557. * tripal_create_moddir function during installation. This function
  2558. * retrieves the directory path.
  2559. *
  2560. * @param $module_name
  2561. * The name of the module
  2562. *
  2563. * @returns
  2564. * The path within the Drupal installation where the data directory resides
  2565. * @ingroup tripal_files_api
  2566. */
  2567. function tripal_get_moddir($module_name) {
  2568. $data_dir = file_directory_path() . "/tripal/$module_name";
  2569. return $data_dir;
  2570. }
  2571. /**
  2572. * Set the Tripal Database
  2573. *
  2574. * The tripal_db_set_active function is used to prevent namespace collisions
  2575. * when chado and drupal are installed in the same database but in different
  2576. * schemas. It is also used for backwards compatibility with older versions
  2577. * of tripal or in cases where chado is located outside of the Drupal database.
  2578. *
  2579. * @ingroup tripal_chado_api
  2580. */
  2581. function tripal_db_set_active($dbname = 'default') {
  2582. global $db_url, $db_type, $active_db;
  2583. $chado_exists = 0;
  2584. // only postgres can support search paths. So if this is MysQL then
  2585. // just run the normal tripal_db_set_active function.
  2586. if (strcmp($db_type, 'pgsql')==0) {
  2587. // if the 'chado' database is in the $db_url variable then chado is
  2588. // not in the same Drupal database, so we don't need to set any
  2589. // search_path and can just change the database
  2590. if (is_array($db_url)) {
  2591. if (isset($db_url[$dbname])) {
  2592. return db_set_active($dbname);
  2593. }
  2594. }
  2595. // if this is the default database then set the search path and return
  2596. if (strcmp($dbname, 'default')==0) {
  2597. tripal_db_set_default_search_path();
  2598. return db_set_active($dbname);
  2599. }
  2600. // if the user requests a database other than the default
  2601. // then we need to try and set the chado search path. This
  2602. // only works if Chado is local to the Drpual database. If it
  2603. // fails then do nothing.
  2604. else {
  2605. if (tripal_db_set_chado_search_path($dbname)) {
  2606. // if the chado schema is local to Drupal then
  2607. // just return the active database.
  2608. return 'default';
  2609. }
  2610. else {
  2611. watchdog('tripal_core', "Cannot set 'search_path' variable for Postgres to %dbname",
  2612. array('%dbname' => $dbname), WATCHDOG_ERROR);
  2613. }
  2614. }
  2615. }
  2616. // a non postgres database
  2617. else {
  2618. return db_set_active($dbname);
  2619. }
  2620. }
  2621. /**
  2622. * Gets the current search_path for PostgreSQL
  2623. *
  2624. * @ingroup tripal_chado_api
  2625. */
  2626. function tripal_db_get_search_path() {
  2627. $path = db_fetch_object(db_query("show search_path"));
  2628. return $path->search_path;
  2629. }
  2630. /**
  2631. * Set the chado search_path for PostgreSQL
  2632. *
  2633. * Sets the database search_path for postgreSQL to the
  2634. * chado schema.
  2635. *
  2636. * @ingroup tripal_chado_api
  2637. */
  2638. function tripal_db_set_chado_search_path($dbname) {
  2639. // check to make sure the chado schema exists
  2640. $chado_exists = variable_get('chado_schema_exists', FALSE);
  2641. if (!$chado_exists) {
  2642. $chado_exists = tripal_core_chado_schema_exists();
  2643. }
  2644. // here we make the assumption that the default database schema is
  2645. // 'public'. This will most likely always be the case but if not,
  2646. // then this code will break
  2647. if ($chado_exists) {
  2648. db_query('set search_path to %s', "$dbname,public");
  2649. return TRUE;
  2650. }
  2651. else {
  2652. return FALSE;
  2653. }
  2654. }
  2655. /**
  2656. * Set the default search_path for PostgreSQL
  2657. *
  2658. * Sets the database search_path for postgreSQL to the
  2659. * default schema.
  2660. *
  2661. * @ingroup tripal_chado_api
  2662. */
  2663. function tripal_db_set_default_search_path() {
  2664. // we make the assumption that the default schema is 'public'.
  2665. $chado_exists = variable_get('chado_schema_exists', FALSE);
  2666. if ($chado_exists) {
  2667. db_query('set search_path to %s', 'public,chado');
  2668. }
  2669. else {
  2670. db_query('set search_path to %s', 'public');
  2671. }
  2672. }
  2673. /**
  2674. * Indicates if the SQL statement is prepapred
  2675. *
  2676. * @param $statement_name
  2677. * The name of the statement to check if it is prepared.
  2678. *
  2679. * @return
  2680. * TRUE if the statement is preapred, FALSE otherwise
  2681. */
  2682. function tripal_core_is_sql_prepared($statement_name) {
  2683. global $prepared_statements;
  2684. // check to see if the statement is prepared already
  2685. if (in_array($statement_name, $prepared_statements)) {
  2686. return TRUE;
  2687. }
  2688. // @coder-ignore: acting on postgres tables rather then drupal schema therefore, table prefixing does not apply
  2689. $sql = "SELECT name FROM pg_prepared_statements WHERE name = '%s'";
  2690. // do not use 'chado_query' here or it causes memory-leaks
  2691. $result = db_fetch_object(db_query($sql, $statement_name));
  2692. if ($result) {
  2693. return TRUE;
  2694. }
  2695. return FALSE;
  2696. }
  2697. /**
  2698. * Prepare a chado query
  2699. *
  2700. * @param $statement_name
  2701. * The name of the prepared statement
  2702. * @param $psql
  2703. * The SQL statement to be executed via chado_query.
  2704. * Should be of the form PREPARE <statement name> AS <SQL Statement to be prepared>
  2705. * @param $args
  2706. * An array of arguements required to execute the prepared statement. The keys of
  2707. * the array should correspond to the $\d in the prepare statement and the value should
  2708. * be the type of value needed (ie: text, int, etc.)
  2709. */
  2710. function tripal_core_chado_prepare($statement_name, $psql, $args) {
  2711. global $persistent_chado;
  2712. global $prepared_statements;
  2713. if (!$persistent_chado) {
  2714. watchdog('tripal_core', "chado_prepare: not able to prepare '%name' statement as no persistent connection is available", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_ERROR);
  2715. return FALSE;
  2716. }
  2717. // Check to see if this statement was already prepared
  2718. if (tripal_core_is_sql_prepared($statement_name)) {
  2719. // check that the arguments are the same
  2720. $prepared_args = $prepared_statements[$statement_name]['prepared_args'];
  2721. $prepared_sql = $prepared_statements[$statement_name]['prepared_sql'];
  2722. if ($prepared_args == $args) {
  2723. // This statement is already prepared
  2724. return TRUE;
  2725. }
  2726. else {
  2727. // Although a statement with this name is already prepared it is not the same!
  2728. watchdog('tripal_core', "chado_prepare: '%name' statement already prepared with different arguments! ".
  2729. "You want to prepare \n%sql\n with \n%values\n and the existing statement is \n%esql\n with \n%existing",
  2730. array('%name' => $statement_name, '%sql' => $psql, '%values' => print_r($args, TRUE), '%esql' => $prepared_sql,
  2731. '%existing' => print_r($prepared_args, TRUE)), WATCHDOG_ERROR);
  2732. return FALSE;
  2733. }
  2734. }
  2735. $status = chado_query($psql);
  2736. if (!$status) {
  2737. watchdog('tripal_core', "chado_prepare: not able to prepare '%name' statement for: %sql", array('%name' => $statement_name, '%sql' => $psql), WATCHDOG_ERROR);
  2738. return FALSE;
  2739. }
  2740. else {
  2741. $prepared_statements[$statement_name] = array();
  2742. $prepared_statements[$statement_name]['prepared_args'] = $args;
  2743. $prepared_statements[$statement_name]['prepared_sql'] = $psql;
  2744. return TRUE;
  2745. }
  2746. }
  2747. /**
  2748. * Execute a prepared statement with the supplied values
  2749. *
  2750. * @param $statement_name
  2751. * The name of the prepared statement
  2752. * @param $sql
  2753. * The SQL to execute using chado query.
  2754. * Should be of the form EXECUTE <statement_name> (<arg1>,<arg2>...<argn>)
  2755. * @param $values
  2756. * An array of values in the execute sql statement
  2757. */
  2758. function tripal_core_chado_execute_prepared($statement_name, $sql, $values) {
  2759. global $prepared_statements;
  2760. if (!tripal_core_is_sql_prepared($statement_name)) {
  2761. watchdog('tripal_core', "tripal_core_chado_execute_prepared: Cannot execute an unprepared statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
  2762. return FALSE;
  2763. }
  2764. // Before Executing, Ensure that all the values are supplied
  2765. $required_values = $prepared_statements[$statement_name]['prepared_args'];
  2766. if (!$required_values) {
  2767. watchdog('tripal_core', "tripal_core_chado_execute_prepared: missing prepare arguments for this statement: '%name'", array('%name' => $statement_name), WATCHDOG_ERROR);
  2768. return FALSE;
  2769. }
  2770. if (sizeof($required_values) == sizeof($values)) {
  2771. $error = FALSE;
  2772. foreach ($values as $k => $v) {
  2773. if (isset($required_values[$k])) {
  2774. switch ($required_values[$k]) {
  2775. case 'text':
  2776. $check = is_string($v);
  2777. if ($v != '' and !$check) {
  2778. watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
  2779. array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
  2780. return FALSE;
  2781. }
  2782. break;
  2783. case 'int':
  2784. $check = is_numeric($v);
  2785. if (!$check) {
  2786. watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
  2787. array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
  2788. return FALSE;
  2789. }
  2790. break;
  2791. case 'bool':
  2792. if ($v != 'TRUE' and $v != 'FALSE') {
  2793. watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
  2794. array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
  2795. return FALSE;
  2796. }
  2797. break;
  2798. case 'numeric':
  2799. $check = is_numeric($v);
  2800. if (!$check) {
  2801. watchdog('tripal_core', "chado_execute_prepared: wrong argument type supplied for '%name' statement, field %k. Expected %required but recieved '%value'",
  2802. array('%name' => $statement_name, '%k' => $k+1, '%required' => $required_values[$k], '%value' => print_r($v, TRUE)), WATCHDOG_ERROR);
  2803. return FALSE;
  2804. }
  2805. break;
  2806. default:
  2807. watchdog('tripal_core', "chado_execute_prepared: unsupported argument type (supplied for '%name' statement %type)",
  2808. array('%name' => $statement_name, '%type' => $required_values[$k]), WATCHDOG_WARNING);
  2809. break;
  2810. }
  2811. }
  2812. else {
  2813. watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. Expected %required but recieved %values",
  2814. array('%name' => $statement_name, '%required' => print_r($required_values, TRUE), '%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  2815. return FALSE;
  2816. }
  2817. }
  2818. // Since all values are supplied, execute
  2819. $resource = chado_query($sql, $values);
  2820. return $resource;
  2821. }
  2822. else {
  2823. watchdog('tripal_core', "chado_execute_prepared: wrong number of arguments supplied for '%name' statement. ' .
  2824. 'Expected %required but recieved %values. Statement: %statement.",
  2825. array('%name' => $statement_name, '%required' => print_r($required_values, TRUE),
  2826. '%values' => print_r($values, TRUE), '%statement' => $prepared_statements[$statement_name]['prepared_sql']), WATCHDOG_ERROR);
  2827. return FALSE;
  2828. }
  2829. }
  2830. /**
  2831. * Clears prepared statements to avoid conflicts
  2832. *
  2833. * If no statement_name_regex is supplied then it clears ALL prepared statements;
  2834. * Otherwise, it clears prepared statement names that match the regex provided
  2835. */
  2836. function tripal_core_chado_clear_prepared ($statement_name_regex = NULL) {
  2837. global $prepared_statements;
  2838. if ($statement_name_regex) {
  2839. $resource = chado_query("SELECT * FROM pg_catalog.pg_prepared_statements WHERE name~'%s'",$statement_name_regex);
  2840. while ($r = db_fetch_object($resource)) {
  2841. $k = array_search($r->name, $prepared_statements);
  2842. unset($prepared_statements[$k]);
  2843. chado_query('DEALLOCATE PREPARE %s',$r->name);
  2844. }
  2845. }
  2846. else {
  2847. $prepared_statements = array();
  2848. chado_query('DEALLOCATE PREPARE ALL');
  2849. }
  2850. }
  2851. /**
  2852. * Instantiate or Return a persistent chado connection. This should not be confused with
  2853. * PHP persistent connections. Here we use the drupal db_connect function to
  2854. *
  2855. * NOTE: cannot use $active_db since a new connection is created each time
  2856. * db_set_active() is called
  2857. *
  2858. * @return
  2859. * A postgresql connection object which can be used by pg_prepare, pg_execute, etc.
  2860. */
  2861. function tripal_db_persistent_chado() {
  2862. global $db_url;
  2863. global $persistent_chado;
  2864. // get connection if it already exists otherwise we need to set it
  2865. if ($persistent_chado) {
  2866. return $persistent_chado;
  2867. }
  2868. else {
  2869. if (is_array($db_url) && isset($db_url['chado'])) {
  2870. $connection = db_connect($db_url['chado']);
  2871. if (!$connection) {
  2872. watchdog('tripal_core', "Could not create persistant connection", array(), WATCHDOG_ERROR);
  2873. return FALSE;
  2874. }
  2875. $persistent_chado = $connection;
  2876. }
  2877. else {
  2878. if (is_array($db_url)) {
  2879. $connection = db_connect($db_url['default']);
  2880. }
  2881. else {
  2882. $connection = db_connect($db_url);
  2883. }
  2884. if (!$connection) {
  2885. $persistent_chado = NULL;
  2886. watchdog('tripal_core', "Could not create persistant connection", array(), WATCHDOG_ERROR);
  2887. return FALSE;
  2888. }
  2889. $persistent_chado = $connection;
  2890. }
  2891. return $connection;
  2892. }
  2893. return FALSE;
  2894. }
  2895. /**
  2896. * Release a persistent chado connection
  2897. */
  2898. function tripal_db_release_persistent_chado() {
  2899. $persistent_chado = NULL;
  2900. }
  2901. /**
  2902. * Start a transaction block. Ensures the use of a persistent chado connection
  2903. */
  2904. function tripal_db_start_transaction() {
  2905. $connection = tripal_db_persistent_chado();
  2906. if ($connection) {
  2907. chado_query("BEGIN");
  2908. return $connection;
  2909. }
  2910. return FALSE;
  2911. }
  2912. /**
  2913. * Set a savepoint to roll the current transaction back to if an error is encountered
  2914. */
  2915. function tripal_db_set_savepoint_transaction($savepoint, $release = FALSE) {
  2916. // Postgresql requires a savepoint of the same name to be unset before re-use
  2917. if ($release) {
  2918. chado_query("RELEASE SAVEPOINT %s", $savepoint);
  2919. }
  2920. chado_query("SAVEPOINT %s", $savepoint);
  2921. }
  2922. /**
  2923. * Commit changes made during the current transaction
  2924. */
  2925. function tripal_db_commit_transaction() {
  2926. chado_query("COMMIT");
  2927. }
  2928. /**
  2929. * Rollback changes.
  2930. *
  2931. * If $savepoint is NULL then rollback to the beginning of the transaction,
  2932. * Otherwise, rollback to the point at which the named $savepoint was created
  2933. *
  2934. * @param $savepoint
  2935. * The name of the saved point in the transaction to rollback to
  2936. */
  2937. function tripal_db_rollback_transaction($savepoint = NULL, $commit = TRUE) {
  2938. if ($savepoint) {
  2939. chado_query("ROLLBACK TO SAVEPOINT %s", $savepoint);
  2940. }
  2941. else {
  2942. chado_query("ROLLBACK");
  2943. }
  2944. if ($commit) {
  2945. tripal_db_commit_transaction();
  2946. }
  2947. }
  2948. /**
  2949. * Purpose: Get max rank for a given set of criteria
  2950. * This function was developed with the many property tables in chado in mind
  2951. *
  2952. * @param $tablename
  2953. * The name of the chado table you want to select the max rank from this table must contain a
  2954. * rank column of type integer
  2955. * @param $where_options
  2956. * where options should include the id and type for that table to correctly
  2957. * group a set of records together where the only difference are the value and rank
  2958. * @code
  2959. * array(
  2960. * <column_name> => array(
  2961. * 'type' => <type of column: INT/STRING>,
  2962. * 'value' => <the value you want to filter on>,
  2963. * 'exact' => <if TRUE use =; if FALSE use ~>,
  2964. * )
  2965. * )
  2966. * @endcode
  2967. * @return the maximum rank
  2968. *
  2969. * @ingroup tripal_chado_api
  2970. */
  2971. function tripal_get_max_chado_rank($tablename, $where_options) {
  2972. $where= array();
  2973. //generate the where clause from supplied options
  2974. // the key is the column name
  2975. foreach ($where_options as $key => $val_array) {
  2976. if (preg_match('/INT/', $val_array['type'])) {
  2977. $where[] = $key . "=" . $val_array['value'];
  2978. }
  2979. else {
  2980. if ($val_array['exact']) {
  2981. $operator='=';
  2982. }
  2983. else {
  2984. $operator='~';
  2985. }
  2986. $where[] = $key . $operator . "'" . $val_array['value'] . "'";
  2987. }
  2988. }
  2989. $previous_db = tripal_db_set_active('chado');
  2990. $result = db_fetch_object(db_query("SELECT max(rank) as max_rank, count(rank) as count FROM %s WHERE %s",
  2991. $tablename, implode(' AND ', $where)));
  2992. tripal_db_set_active($previous_db);
  2993. //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
  2994. if ($result->count > 0) {
  2995. return $result->max_rank;
  2996. }
  2997. else {
  2998. return -1;
  2999. }
  3000. }
  3001. /**
  3002. * Retrieves the schema in an array for the specified custom table.
  3003. *
  3004. * @param $table
  3005. * The name of the table to create.
  3006. *
  3007. * @return
  3008. * A Drupal-style Schema API array definition of the table. Returns
  3009. * FALSE on failure.
  3010. *
  3011. * @ingroup tripal_core_api
  3012. */
  3013. function tripal_get_chado_custom_schema($table) {
  3014. $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = '%s'";
  3015. $custom = db_fetch_object(db_query($sql, $table));
  3016. if (!$custom) {
  3017. return FALSE;
  3018. }
  3019. else {
  3020. return unserialize($custom->schema);
  3021. }
  3022. }
  3023. /**
  3024. * Check that the Chado schema exists
  3025. *
  3026. * @return
  3027. * TRUE/FALSE depending upon whether it exists
  3028. */
  3029. function tripal_core_chado_schema_exists() {
  3030. $exists = variable_get('chado_schema_exists', FALSE);
  3031. if (!$exists) {
  3032. // This is postgresql-specific code to check the existence of the chado schema
  3033. // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
  3034. $sql = "SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname = 'chado'";
  3035. if (db_fetch_object(db_query($sql))) {
  3036. variable_set('chado_schema_exists', TRUE);
  3037. return TRUE;
  3038. }
  3039. else {
  3040. return FALSE;
  3041. }
  3042. }
  3043. return TRUE;
  3044. }
  3045. /**
  3046. * Check that any given schema exists
  3047. *
  3048. * @param $schema
  3049. * The name of the schema to check the existence of
  3050. *
  3051. * @return
  3052. * TRUE/FALSE depending upon whether or not the schema exists
  3053. *
  3054. * @ingroup tripal_chado_api
  3055. */
  3056. function tripal_core_schema_exists($schema) {
  3057. // check that the chado schema now exists
  3058. $sql = "SELECT nspname
  3059. FROM pg_namespace
  3060. WHERE has_schema_privilege(nspname, 'USAGE') and nspname = '%s'
  3061. ORDER BY nspname";
  3062. $name = db_fetch_object(db_query($sql, $schema));
  3063. if (strcmp($name->nspname, $schema) != 0) {
  3064. return FALSE;
  3065. }
  3066. return TRUE;
  3067. }
  3068. /**
  3069. * Retrieves the list tables in the Chado schema. By default it only retursn
  3070. * the default Chado tables, but may also return custom tables added to the
  3071. * Chado schema as well.
  3072. *
  3073. * @param $include_custom
  3074. * Optional. Set as TRUE to include any custom tables created in the
  3075. * Chado schema. Custom tables are added to Chado using the
  3076. * tripal_core_chado_create_table() function.
  3077. *
  3078. * @returns
  3079. * An associative array where the key and value pairs are the Chado table names.
  3080. *
  3081. * @ingroup tripal_core_api
  3082. */
  3083. function tripal_core_get_chado_tables($include_custom = NULL) {
  3084. // first get the chado version that is installed
  3085. $v = tripal_core_get_chado_version();
  3086. $tables = array();
  3087. if ($v == '1.2') {
  3088. $tables_v1_2 = tripal_core_chado_get_v1_2_tables();
  3089. foreach ($tables_v1_2 as $table) {
  3090. $tables[$table] = $table;
  3091. }
  3092. }
  3093. if ($v == '1.11' or $v == '1.11 or older') {
  3094. $tables_v1_11 = tripal_core_chado_get_v1_11_tables();
  3095. foreach ($tables_v1_11 as $table) {
  3096. $tables[$table] = $table;
  3097. }
  3098. }
  3099. // now add in the custom tables too if requested
  3100. if ($include_custom) {
  3101. $sql = "SELECT table_name FROM {tripal_custom_tables}";
  3102. $resource = db_query($sql);
  3103. while ($r = db_fetch_object($resource)) {
  3104. $tables[$r->table_name] = $r->table_name;
  3105. }
  3106. }
  3107. asort($tables);
  3108. return $tables;
  3109. }
  3110. /**
  3111. * Sets a Drupal variable with the current version of Chado. This variable
  3112. * can then be queried later using the tripal_core_get_chado_Version
  3113. *
  3114. * @returns
  3115. * The version of Chado
  3116. *
  3117. * @ingroup tripal_core_api
  3118. */
  3119. function tripal_core_set_chado_version() {
  3120. global $db_url;
  3121. // check that Chado is installed if not return 'uninstalled as the version'
  3122. $chado_exists = tripal_core_chado_schema_exists();
  3123. if (!$chado_exists) {
  3124. // if it's not in the drupal database check to see if it's specified in the $db_url
  3125. // in the settings.php
  3126. if (!is_array($db_url) or !array_key_exists('chado', $db_url)) {
  3127. // if it's not in the drupal database or specified in the $db_url then
  3128. // return uninstalled as the version
  3129. return 'not installed';
  3130. }
  3131. }
  3132. // if the table doesn't exist then we don't know what version but we know
  3133. // it must be 1.11 or older.
  3134. $previous_db = tripal_db_set_active('chado');
  3135. $prop_exists = db_table_exists('chadoprop');
  3136. tripal_db_set_active($previous_db);
  3137. if (!$prop_exists) {
  3138. return "1.11 or older";
  3139. }
  3140. // we can't use the Tripal API to query this table
  3141. // because the Tripal API depends on this function to
  3142. // tell it the version. So, we need a typical SQL statement
  3143. $sql = "SELECT value "
  3144. ."FROM chadoprop CP "
  3145. ." INNER JOIN cvterm CVT on CVT.cvterm_id = CP.type_id "
  3146. ." INNER JOIN cv CV on CVT.cv_id = CV.cv_id "
  3147. ."WHERE CV.name = 'chado_properties' and CVT.name = 'version'";
  3148. $previous_db = tripal_db_set_active('chado');
  3149. $v = db_fetch_object(db_query($sql));
  3150. tripal_db_set_active($previous_db);
  3151. // if we don't have a version in the chadoprop table then it must be
  3152. // v1.11 or older
  3153. if (!$v->value) {
  3154. variable_set('chado_version', "1.11 or older");
  3155. return "1.11 or older";
  3156. }
  3157. variable_set('chado_version', $v->value);
  3158. return $v->value;
  3159. }
  3160. /**
  3161. * Returns the version number of the currently installed Chado instance.
  3162. * It can return the real or effective version.
  3163. *
  3164. * @param $exact
  3165. * Set this argument to 1 to retrieve the exact version that is installed.
  3166. * Otherwise, this function will set the version to the nearest 'tenth'.
  3167. * Chado versioning numbers in the hundreds represent changes to the
  3168. * software and not the schema. Changes in the tenth's represent changes
  3169. * in the schema.
  3170. *
  3171. * @param $warn_if_unsupported
  3172. * If the currently installed version of Chado is not supported by Tripal
  3173. * the generatea a Drupal warning.
  3174. *
  3175. * @returns
  3176. * The version of Chado
  3177. *
  3178. * @ingroup tripal_core_api
  3179. */
  3180. function tripal_core_get_chado_version($exact = FALSE, $warn_if_unsupported = FALSE) {
  3181. // first get the chado version that is installed
  3182. $exact_version = variable_get('chado_version', '');
  3183. if (!$exact_version) {
  3184. $exact_version = tripal_core_set_chado_version();
  3185. }
  3186. // Tripal only supports v1.11 or newer.. really this is the same as v1.1
  3187. // but at the time the v1.11 schema API was written we didn't know that so
  3188. // we'll return the version 1.11 so the schema API will work.
  3189. if (strcmp($exact_version, '1.11 or older') == 0) {
  3190. $exact_version = "1.11";
  3191. if ($warn_if_unsupported) {
  3192. drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.1. If you are certain this is v1.1
  3193. of if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.1 or later"),
  3194. 'warning');
  3195. }
  3196. }
  3197. // if not returing an exact version, return the version to the nearest 10th.
  3198. // return 1.2 for all versions of 1.2x
  3199. $effective_version = $exact_version;
  3200. if (preg_match('/^1\.2\d+$/', $effective_version)) {
  3201. $effective_version = "1.2";
  3202. }
  3203. if ($warn_if_unsupported and ($effective_version != 1.11 and $effective_version != 1.2 and $effective_version != 'not installed')) {
  3204. drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
  3205. }
  3206. // if the callee has requested the exact version then return it
  3207. if ($exact) {
  3208. return $exact_version;
  3209. }
  3210. return $effective_version;
  3211. }
  3212. /**
  3213. * Retrieves the chado tables Schema API array.
  3214. *
  3215. * @param $table
  3216. * The name of the table to retrieve. The function will use the appopriate
  3217. * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
  3218. *
  3219. * @returns
  3220. * A Drupal Schema API array defining the table.
  3221. *
  3222. * @ingroup tripal_core_api
  3223. */
  3224. function tripal_core_get_chado_table_schema($table) {
  3225. // first get the chado version that is installed
  3226. $v = tripal_core_get_chado_version();
  3227. // get the table array from the proper chado schema
  3228. $v = preg_replace("/\./", "_", $v); // reformat version for hook name
  3229. $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);
  3230. // if the table_arr is empty then maybe this is a custom table
  3231. if (!is_array($table_arr) or count($table_arr) == 0) {
  3232. $table_arr = tripal_get_chado_custom_schema($table);
  3233. }
  3234. return $table_arr;
  3235. }
  3236. /**
  3237. * This function will delete Drupal nodes for any sync'ed table (e.g.
  3238. * feature, organism, analysis, stock, library) if the chado record has been
  3239. * deleted or the entry in the chado_[table] table has been removed.
  3240. *
  3241. * @param $table
  3242. * The name of the table that corresonds to the node type we want to clean up.
  3243. * @param $job_id
  3244. * This should be the job id from the Tripal jobs system. This function
  3245. * will update the job status using the provided job ID.
  3246. *
  3247. * @ingroup tripal_core_api
  3248. */
  3249. function tripal_core_clean_orphaned_nodes($table, $job_id) {
  3250. $count = 0;
  3251. // build the SQL statments needed to check if nodes point to valid analyses
  3252. $dsql = "SELECT * FROM {node} WHERE type = 'chado_%s' order by nid";
  3253. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  3254. $csql = "SELECT * FROM {chado_%s} where nid = %d ";
  3255. $clsql= "SELECT * FROM {chado_%s}";
  3256. $lsql = "SELECT * FROM %s where %s_id = %d ";
  3257. // load into nodes array
  3258. print "Getting nodes\n";
  3259. $nodes = array();
  3260. $res = db_query($dsql, $table);
  3261. while ($node = db_fetch_object($res)) {
  3262. $nodes[$count] = $node;
  3263. $count++;
  3264. }
  3265. // load the chado_$table into an array
  3266. print "Getting chado_$table\n";
  3267. $cnodes = array();
  3268. $res = db_query($clsql, $table);
  3269. while ($node = db_fetch_object($res)) {
  3270. $cnodes[$count] = $node;
  3271. $count++;
  3272. }
  3273. $interval = intval($count * 0.01);
  3274. if ($interval < 1) {
  3275. $interval = 1;
  3276. }
  3277. // iterate through all of the chado_$table entries and remove those
  3278. // that don't have a node or don't have a $table record in chado.libary
  3279. print "Verifying all chado_$table Entries\n";
  3280. $deleted = 0;
  3281. foreach ($cnodes as $nid) {
  3282. // update the job status every 1% analyses
  3283. if ($job_id and $i % $interval == 0) {
  3284. tripal_job_set_progress($job_id, intval(($i / $count) * 100));
  3285. }
  3286. // see if the node exits, if not remove the entry from the chado_$table table
  3287. $node = db_fetch_object(db_query($nsql, $nid->nid));
  3288. if (!$node) {
  3289. $deleted++;
  3290. db_query("DELETE FROM {chado_%s} WHERE nid = %d", $table, $nid->nid);
  3291. $message = "chado_$table missing node.... DELETING: $nid->nid";
  3292. watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
  3293. }
  3294. // see if the record in chado exist, if not remove the entry from the chado_$table
  3295. $table_id = $table . "_id";
  3296. $record = db_fetch_object(chado_query($lsql, $table, $table, $nid->$table_id));
  3297. if (!$record) {
  3298. $deleted++;
  3299. db_query("DELETE FROM {chado_%s} WHERE %s_id = '%d'", $table, $table, $nid->$table_id);
  3300. $message = "chado_$table missing $table.... DELETING entry.";
  3301. watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
  3302. }
  3303. $i++;
  3304. }
  3305. print "\t$deleted chado_$table entries missing either a node or chado entry.\n";
  3306. // iterate through all of the nodes and delete those that don't
  3307. // have a corresponding entry in chado_$table
  3308. $deleted = 0;
  3309. foreach ($nodes as $node) {
  3310. // update the job status every 1% libraries
  3311. if ($job_id and $i % $interval == 0) {
  3312. tripal_job_set_progress($job_id, intval(($i / $count) * 100));
  3313. }
  3314. // check to see if the node has a corresponding entry
  3315. // in the chado_$table table. If not then delete the node.
  3316. $link = db_fetch_object(db_query($csql, $table, $node->nid));
  3317. if (!$link) {
  3318. if (node_access('delete', $node)) {
  3319. $deleted++;
  3320. $message = "Node missing in chado_$table table.... DELETING node $node->nid";
  3321. watchdog("tripal_core", $message, array(), WATCHDOG_WARNING);
  3322. node_delete($node->nid);
  3323. }
  3324. else {
  3325. $message = "Node missing in chado_$table table.... but cannot delete due to improper permissions (node $node->nid)";
  3326. watchdog("tripal_core", $message, array(), WATCHDOG_WARNING);
  3327. }
  3328. }
  3329. $i++;
  3330. }
  3331. print "\t$deleted nodes did not have corresponding chado_$table entries.\n";
  3332. return '';
  3333. }
  3334. /**
  3335. * Check whether chado is installed (either in the same or a different database)
  3336. *
  3337. * @return
  3338. * TRUE/FALSE depending upon whether chado is installed.
  3339. *
  3340. * @ingroup tripal_chado_api
  3341. */
  3342. function tripal_core_is_chado_installed() {
  3343. global $db_url, $db_type;
  3344. // first check if chado is in the db_url of the
  3345. // settings.php file
  3346. if (is_array($db_url)) {
  3347. if (isset($db_url['chado'])) {
  3348. return TRUE;
  3349. }
  3350. }
  3351. // check to make sure the chado schema exists
  3352. return tripal_core_chado_schema_exists();
  3353. }
  3354. /**
  3355. * Check whether chado is installed local to the Drupal database
  3356. * in its own Chado schema.
  3357. *
  3358. * @return
  3359. * TRUE/FALSE depending upon whether chado is local.
  3360. *
  3361. * @ingroup tripal_chado_api
  3362. */
  3363. function tripal_core_is_chado_local() {
  3364. global $db_url, $db_type;
  3365. $is_installed = tripal_core_is_chado_installed();
  3366. if ($is_installed) {
  3367. if (is_array($db_url)) {
  3368. if (isset($db_url['chado'])) {
  3369. return FALSE;
  3370. }
  3371. }
  3372. return TRUE;
  3373. }
  3374. return FALSE;
  3375. }