tripal_core_chado.api.inc 132 KB

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