12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870 |
- <?php
- require_once "tripal_core.schema_v1.2.api.inc";
- require_once "tripal_core.schema_v1.11.api.inc";
-
- function tripal_core_chado_insert($table, $values, $options = array()) {
- if (!is_array($values)) {
- watchdog('tripal_core', 'Cannot pass non array as values for inserting.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
- if (count($values)==0) {
- watchdog('tripal_core', 'Cannot pass an empty array as values for inserting.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('skip_validation', $options)) {
- $options['skip_validation'] = FALSE;
- }
- if (!array_key_exists('return_record', $options)) {
- $options['return_record'] = TRUE;
- }
- $insert_values = array();
- if (array_key_exists('skip_validation', $options)) {
- $validate = !$options['skip_validation'];
- }
- else {
- $validate = TRUE;
- }
-
- $table_desc = tripal_core_get_chado_table_schema($table);
- if (empty($table_desc)) {
- watchdog('tripal_core', 'tripal_core_chado_insert: There is no table description for !table_name', array('!table_name' => $table), WATCHDOG_WARNING);
- }
-
-
-
- foreach ($values as $field => $value) {
-
-
- if (!array_key_exists($field, $table_desc['fields'])) {
- watchdog('tripal_core', "tripal_core_chado_insert: The field '%field' does not exist " .
- "for the table '%table'. Cannot perform insert. Values: %array",
- array('%field' => $field, '%table' => $table, '%array' => print_r($values, 1)), WATCHDOG_ERROR);
- return FALSE;
- }
- if (is_array($value)) {
-
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
- if (sizeof($results) > 1) {
- 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);
- }
- elseif (sizeof($results) < 1) {
-
- }
- else {
- $insert_values[$field] = $results[0];
- }
- }
- else {
- $insert_values[$field] = $value;
- }
- }
- if ($validate) {
-
- $ukeys = array();
- if (array_key_exists('unique keys', $table_desc)) {
- $ukeys = $table_desc['unique keys'];
- }
- $ukselect_cols = array();
- $ukselect_vals = array();
- if ($ukeys) {
- foreach ($ukeys as $name => $fields) {
- foreach ($fields as $index => $field) {
-
- $ukselect_cols[] = $field;
- if (!array_key_exists($field, $insert_values)) {
- if (array_key_exists('default', $table_desc['fields'][$field])) {
- $ukselect_vals[$field] = $table_desc['fields'][$field]['default'];
- }
- }
- else {
- $ukselect_vals[$field] = $insert_values[$field];
- }
- }
-
- if (tripal_core_chado_select($table, $ukselect_cols, $ukselect_vals)) {
- watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " .
- print_r($values, 1), array(), 'WATCHDOG_ERROR');
- return FALSE;
- }
- }
- }
-
- if (array_key_exists('primary key', $table_desc)) {
- $pkey = $table_desc['primary key'][0];
- if (array_key_exists($pkey, $insert_values)) {
- $coptions = array('statement_name' => 'pqsel_' . $table . '_' . $pkey);
- if (tripal_core_chado_select($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
- watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
- return FALSE;
- }
- }
- }
-
- if (!is_array($table_desc['fields'])) {
- $table_desc['fields'] = array();
- watchdog('tripal_core', "tripal_core_chado_insert: %table missing fields: \n %schema",
- array('%table' => $table, '%schema' => print_r($table_desc, 1)), WATCHDOG_WARNING);
- }
- foreach ($table_desc['fields'] as $field => $def) {
-
-
- if (array_key_exists('NOT NULL', $def) and
- !array_key_exists($field, $insert_values) and
- !array_key_exists('default', $def) and
- strcmp($def['type'], serial) != 0) {
- watchdog('tripal_core', "tripal_core_chado_insert: Field $table.$field cannot be NULL: " .
- print_r($values, 1), array(), 'WATCHDOG_ERROR');
- return FALSE;
- }
- }
- }
-
- $ifields = array();
- $itypes = array();
- $ivalues = array();
- $i = 1;
- foreach ($insert_values as $field => $value) {
- $ifields[] = $field;
- $ivalues[":$field"] = $value;
- $i++;
- if (strcmp($value, '__NULL__')==0) {
- $itypes[] = "NULL";
- }
- else {
- $itypes[] = ":$field";
- }
- }
-
- $sql = 'INSERT INTO {' . $table . '} (' . implode(", ", $ifields) . ") VALUES (" . implode(", ", $itypes) . ")";
- $result = chado_query($sql, $ivalues);
-
- if ($options['return_record'] == TRUE and $result) {
- if (array_key_exists('primary key', $table_desc) and is_array($table_desc['primary key'])) {
- foreach ($table_desc['primary key'] as $field) {
- $sql = "SELECT CURRVAL('{" . $table . "_" . $field . "_seq}')";
- $results = chado_query($sql);
- $value = $results->fetchObject();
- if (!$value) {
- watchdog('tripal_core', "tripal_core_chado_insert: not able to retrieve primary key after insert: %sql",
- array('%sql' => $sql), WATCHDOG_ERROR);
- return FALSE;
- }
- $values[$field] = $value;
- }
- }
- return $values;
- }
- elseif ($options['return_record'] == FALSE and $result) {
- return TRUE;
- }
- else {
- watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert record into '%table': " . print_r($values, 1),
- array('%table' => $table), 'WATCHDOG_ERROR');
- return FALSE;
- }
- return FALSE;
- }
- function tripal_core_chado_update($table, $match, $values, $options = NULL) {
- if (!is_array($values)) {
- watchdog('tripal_core', 'Cannot pass non array as values for updating.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
- if (count($values)==0) {
- watchdog('tripal_core', 'Cannot pass an empty array as values for updating.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
- if (!is_array($match)) {
- watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
- if (count($match)==0) {
- watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('return_record', $options)) {
- $options['return_record'] = FALSE;
- }
- $update_values = array();
- $update_matches = array();
-
- $table_desc = tripal_core_get_chado_table_schema($table);
-
-
-
- $pkeys = array();
- if ($options['return_record'] == TRUE) {
- if (array_key_exists('primary key', $table_desc) and is_array($table_desc['primary key'])) {
- $columns = array();
- $stmt_suffix = '';
- foreach ($table_desc['primary key'] as $field) {
- $columns[] = $field;
- $stmt_suffix .= substr($field, 0, 2);
- }
- $options2 = array('statement_name' => 'sel_' . $table . '_' . $stmt_suffix);
- $results = tripal_core_chado_select($table, $columns, $match, $options2);
- if (count($results) > 0) {
- foreach ($results as $index => $pkey) {
- $pkeys[] = $pkey;
- }
- }
- }
- }
-
- foreach ($match as $field => $value) {
- if (is_array($value)) {
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value);
- if (sizeof($results) > 1) {
- 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);
- }
- elseif (sizeof($results) < 1) {
-
- }
- else {
- $update_matches[$field] = $results[0];
- }
- }
- else {
- $update_matches[$field] = $value;
- }
- }
-
- foreach ($values as $field => $value) {
- if (is_array($value)) {
- $foreign_options = array();
-
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
- if (sizeof($results) > 1) {
- 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);
- }
- elseif (sizeof($results) < 1) {
-
- }
- else {
- $update_values[$field] = $results[0];
- }
- }
- else {
- $update_values[$field] = $value;
- }
- }
-
- $sql = 'UPDATE {' . $table . '} SET ';
- $args = array();
- foreach ($update_values as $field => $value) {
- if (strcmp($value, '__NULL__') == 0) {
- $sql .= " $field = NULL, ";
- }
- else {
- $sql .= " $field = :$field, ";
- $args[":$field"] = $value;
- }
- }
- $sql = drupal_substr($sql, 0, -2);
- $sql .= " WHERE ";
- foreach ($update_matches as $field => $value) {
- if (strcmp($value, '__NULL__')==0) {
- $sql .= " $field = NULL AND ";
- }
- else {
- $sql .= " $field = :$field AND ";
- $args[":$field"] = $value;
- }
- }
- $sql = drupal_substr($sql, 0, -4);
- $result = chado_query($sql, $args);
-
- if ($options['return_record'] == TRUE and $result) {
-
-
- if (count($pkeys) == 1) {
- foreach ($pkeys as $index => $pkey) {
- foreach ($pkey as $field => $fvalue) {
- $values[$field] = $fvalue;
- }
- }
- }
- return $values;
- }
- elseif ($options['return_record'] == FALSE and $result) {
- return TRUE;
- }
- else {
- watchdog('tripal_core', "Cannot update record in $table table. \nMatch:" . print_r($match, 1) . "\nValues: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
- return FALSE;
- }
- return FALSE;
- }
- function tripal_core_chado_delete($table, $match, $options = NULL) {
- if (!is_array($match)) {
- watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
- if (count($match)==0) {
- watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
-
-
- if (!is_array($options)) {
- $options = array();
- }
- $delete_matches = array();
-
- $table_desc = tripal_core_get_chado_table_schema($table);
- $fields = $table_desc['fields'];
-
- foreach ($match as $field => $value) {
- if (is_array($value)) {
-
-
- if (array_values($value) === $value) {
- $delete_matches[$field] = $value;
- }
- else {
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value);
- if (sizeof($results) > 1) {
- 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);
- }
- elseif (sizeof($results) < 1) {
-
- }
- else {
- $delete_matches[$field] = $results[0];
- }
- }
- }
- else {
- $delete_matches[$field] = $value;
- }
- }
-
- $sql = 'DELETE FROM {' . $table . '} WHERE ';
- $args = array();
- foreach ($delete_matches as $field => $value) {
-
-
-
- if (count($value) > 1) {
- $sql .= "$field IN (";
- $index = 0;
- foreach ($value as $v) {
- $sql .= ":$field" . $index . ", ";
- $args[":$field" . $index] = $v;
- $index++;
- }
- $sql = drupal_substr($sql, 0, -2);
- $sql .= ") AND ";
- }
- else {
- if (strcmp($value, '__NULL__') == 0) {
- $sql .= " $field = NULL AND ";
- }
- else {
- $sql .= " $field = :$field AND ";
- $args[":$field"] = $value;
- }
- }
- }
- $sql = drupal_substr($sql, 0, -4);
-
- $result = chado_query($sql, $args);
- if ($result) {
- return TRUE;
- }
- else {
- watchdog('tripal_core', "Cannot delete record in $table table. Match:" . print_r($match, 1) . ". Values: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
- return FALSE;
- }
- return FALSE;
- }
- function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
- if (!is_array($values)) {
- watchdog('tripal_core', 'Cannot pass non array as values for selecting.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
- if (!is_array($columns)) {
- watchdog('tripal_core', 'Cannot pass non array as columns for selecting.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
- if (count($columns)==0) {
- watchdog('tripal_core', 'Cannot pass an empty array as columns for selecting.', array(),
- WATCHDOG_ERROR);
- return FALSE;
- }
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('case_insensitive_columns', $options)) {
- $options['case_insensitive_columns'] = array();
- }
- if (!array_key_exists('regex_columns', $options)) {
- $options['regex_columns'] = array();
- }
- if (!array_key_exists('order_by', $options)) {
- $options['order_by'] = array();
- }
- if (!array_key_exists('return_sql', $options)) {
- $options['return_sql'] = FALSE;
- }
- if (!array_key_exists('has_record', $options)) {
- $options['has_record'] = FALSE;
- }
- if (!array_key_exists('is_duplicate', $options)) {
- $options['is_duplicate'] = FALSE;
- }
- $pager = array();
- if (array_key_exists('pager', $options)) {
- $pager = $options['pager'];
- }
-
- if (!is_array($columns)) {
- watchdog('tripal_core', 'the $columns argument for tripal_core_chado_select must be an array.');
- return FALSE;
- }
- if (!is_array($values)) {
- watchdog('tripal_core', 'the $values argument for tripal_core_chado_select must be an array.');
- return FALSE;
- }
-
- $table_desc = tripal_core_get_chado_table_schema($table);
- $select = '';
- $from = '';
- $where = array();
- $args = array();
-
-
- if ($options['is_duplicate'] and array_key_exists('unique keys', $table_desc)) {
- $ukeys = $table_desc['unique keys'];
- $has_results = 0;
-
-
- foreach ($ukeys as $cname => $fields) {
- if ($has_results) {
- continue;
- }
- $new_values = array();
- $new_columns = array();
- $new_options = array();
- $uq_sname = "uq_" . $table . "_";
- $has_pkey = 0;
-
- if (array_key_exists('primary key', $table_desc)) {
- $has_pkey = 1;
- $pkeys = $table_desc['primary key'];
- foreach ($pkeys as $index => $key) {
- array_push($new_columns, $key);
- }
- }
-
- foreach ($fields as $field) {
- if (array_key_exists($field, $values)) {
- $new_values[$field] = $values[$field];
- $uq_sname .= substr($field, 0, 2);
-
- if (!$has_pkey) {
- array_push($new_columns, $field);
- }
- }
-
-
- elseif (array_key_exists('default', $table_desc['fields'][$field])) {
- $new_values[$field] = $table_desc['fields'][$field]['default'];
- $uq_sname .= substr($field, 0, 2);
- if (!$has_pkey) {
- array_push($new_columns, $field);
- }
- }
-
-
- elseif (!$table_desc['fields'][$field]['not null']) {
- $new_values[$field] = NULL;
- $uq_sname .= "n" . substr($field, 0, 2);
- if (!$has_pkey) {
- array_push($new_columns, $field);
- }
- }
-
-
-
- else {
- watchdog('tripal_core', "tripal_core_chado_select: There is no value for %field"
- . " thus we cannot check if this record is unique",
- array('%field' => $field), WATCHDOG_ERROR);
- return FALSE;
- }
- }
- $new_options['statement_name'] = $uq_sname;
- $results = tripal_core_chado_select($table, $new_columns, $new_values, $new_options);
-
- if (count($results) > 0) {
- $has_results = 1;
- }
- unset($new_columns);
- unset($new_values);
- unset($new_options);
- }
- if ($options['has_record'] and $has_results) {
- return TRUE;
- }
- else {
- return $results;
- }
- }
- foreach ($values as $field => $value) {
-
-
- if (!array_key_exists($field, $table_desc['fields'])) {
- watchdog('tripal_core', "tripal_core_chado_select: The field '%field' does not exist " .
- "for the table '%table'. Cannot perform query. Values: %array",
- array('%field' => $field, '%table' => $table, '%array' => print_r($values, 1)), WATCHDOG_ERROR);
- return array();
- }
- $select[] = $field;
- if (is_array($value)) {
-
-
-
- if (array_values($value) === $value) {
- $where[$field] = $value;
- }
- else {
-
- $foreign_options = array(
- 'regex_columns' => $options['regex_columns'],
- );
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
- if (!$results or count($results)==0) {
- return array();
- }
- else {
- $where[$field] = $results;
- }
- }
- }
- else {
-
-
- if ($value === NULL) {
- $where[$field] = NULL;
- }
- elseif ($table_desc['fields'][$field]['type'] == 'int') {
- $where[$field][] = (int) $value;
- }
- else {
- $where[$field][] = $value;
- }
- }
- }
-
-
-
-
- if (empty($where)) {
-
- $sql = "SELECT " . implode(', ', $columns) . " ";
- $sql .= 'FROM {' . $table . '} ';
-
- $prepared = FALSE;
- }
- else {
- $sql = "SELECT " . implode(', ', $columns) . " ";
- $sql .= 'FROM {' . $table . '} ';
-
- if (!empty($values)) {
- $sql .= "WHERE ";
- }
- foreach ($where as $field => $value) {
-
-
- if (count($value) > 1) {
- $sql .= "$field IN (";
- $index = 0;
- foreach ($value as $v) {
- $sql .= ":$field" . $index . ', ';
- $args[":$field" . $index] = $v;
- $index++;
- }
- $sql = drupal_substr($sql, 0, -2);
- $sql .= ") AND ";
- }
-
- elseif ($value === NULL) {
- $sql .= "$field IS NULL AND ";
-
- }
-
- else {
- $operator = '=';
- if (in_array($field, $options['regex_columns'])) {
- $operator = '~*';
- }
- if (in_array($field, $options['case_insensitive_columns'])) {
- $sql .= "lower($field) $operator lower(:$field) AND ";
- $args[":$field"] = $value[0];
- }
- else {
- $sql .= "$field $operator :$field AND ";
- $args[":$field"] = $value[0];
- }
- }
- }
- $sql = drupal_substr($sql, 0, -4);
- }
-
- if (count($options['order_by']) > 0) {
- $sql .= " ORDER BY ";
- foreach ($options['order_by'] as $field => $dir) {
- $sql .= "$field $dir, ";
- }
- $sql = drupal_substr($sql, 0, -2);
- }
-
-
-
- if ($options['return_sql'] == TRUE) {
- return array('sql' => $sql, 'args' => $args);
- }
- if (array_key_exists('limit', $pager)) {
- $resource = chado_pager_query($sql, $pager['limit'], $pager['element'], NULL, $args);
- }
- else {
- $resource = chado_query($sql, $args);
- }
-
- $results = array();
- foreach ($resource as $r) {
- $results[] = $r;
- }
- if ($options['has_record']) {
- return count($results);
- }
- return $results;
- }
- function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $options = NULL) {
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('case_insensitive_columns', $options)) {
- $options['case_insensitive_columns'] = array();
- }
- if (!array_key_exists('regex_columns', $options)) {
- $options['regex_columns'] = array();
- }
-
-
- $fkeys = '';
- if (array_key_exists('foreign keys', $table_desc)) {
- $fkeys = $table_desc['foreign keys'];
- }
- if ($fkeys) {
- foreach ($fkeys as $name => $def) {
- if (is_array($def['table'])) {
-
- $message = "The foreign key " . $name . " was defined twice. Please check modules "
- . "to determine if hook_chado_schema_<version>_" . $table_desc['table'] . "() was "
- . "implemented and defined this foreign key when it wasn't supposed to. Modules "
- . "this hook was implemented in: " . implode(', ',
- module_implements("chado_" . $table_desc['table'] . "_schema")) . ".";
- watchdog('tripal_core', $message);
- drupal_set_message(check_plain($message), 'error');
- continue;
- }
- $table = $def['table'];
- $columns = $def['columns'];
-
- foreach ($columns as $left => $right) {
-
- if (strcmp($field, $left) == 0) {
-
-
- $select_cols = array($right);
- $result = tripal_core_chado_select($table, $select_cols, $values, $options);
- $fields = array();
- if ($result and count($result) > 0) {
- foreach ($result as $obj) {
- $fields[] = $obj->$right;
- }
- return $fields;
- }
- }
- }
- }
- }
- else {
-
-
-
- $version = tripal_core_get_chado_version(TRUE);
- $message = t("There is no foreign key relationship defined for " . $field . " .
- To define a foreign key relationship, determine the table this foreign
- key referrs to (<foreign table>) and then implement
- hook_chado_chado_schema_v<version>_<foreign table>(). See
- tripal_feature_chado_v1_2_schema_feature for an example. Chado version: $version");
- watchdog('tripal_core', $message);
- drupal_set_message(check_plain($message), 'error');
- }
- return array();
- }
- function tripal_core_generate_chado_var($table, $values, $base_options = array()) {
- $all = new stdClass();
- $return_array = 0;
- if (array_key_exists('return_array', $base_options)) {
- $return_array = 1;
- }
- $include_fk = 0;
- if (array_key_exists('include_fk', $base_options)) {
- $include_fk = $base_options['include_fk'];
- }
- $pager = array();
- if (array_key_exists('pager', $base_options)) {
- $pager = $base_options['pager'];
- }
-
- $table_desc = tripal_core_get_chado_table_schema($table);
- if (!$table_desc or count($table_desc) == 0) {
- watchdog('tripal_core', "tripal_core_generate_chado_var: The table '%table' has not been defined. " .
- "and cannot be expanded. If this is a custom table, please add it using the Tripal " .
- "custom table interface.", array('%table' => $table), WATCHDOG_ERROR);
- if ($return_array) {
- return array();
- }
- return FALSE;
- }
- $table_primary_key = $table_desc['primary key'][0];
- $table_columns = array_keys($table_desc['fields']);
-
- $all->expandable_fields = array();
- if ($table_desc['referring_tables']) {
- $all->expandable_tables = $table_desc['referring_tables'];
- }
- else {
- $all->expandable_tables = array();
- }
- $all->expandable_nodes = array();
-
-
- $results = tripal_core_chado_select($table, $table_columns, $values, $base_options);
-
- if ($results) {
- foreach ($results as $key => $object) {
-
- $object->expandable_fields = $all->expandable_fields;
- $object->expandable_tables = $all->expandable_tables;
- $object->expandable_nodes = $all->expandable_nodes;
-
- $object->tablename = $table;
-
-
- if (db_table_exists('chado_' . $table)) {
-
-
- $sql = "
- SELECT $table_primary_key, nid
- FROM {chado_$table}
- WHERE $table_primary_key = :$table_primary_key";
- $mapping = db_query($sql, array(":$table_primary_key" => $object->{$table_primary_key}))->fetchObject();
- if ($mapping->{$table_primary_key}) {
- $object->nid = $mapping->nid;
- $object->expandable_nodes[] = $table;
- }
- }
-
-
- if ($table_desc['foreign keys']) {
- foreach ($table_desc['foreign keys'] as $foreign_key_array) {
- $foreign_table = $foreign_key_array['table'];
- foreach ($foreign_key_array['columns'] as $foreign_key => $primary_key) {
-
-
-
- if (empty($object->{$foreign_key})) {
- continue;
- }
- if ($include_fk) {
-
-
- if (is_array($include_fk) and !array_key_exists($foreign_key, $include_fk)) {
- continue;
- }
-
- if (!is_array($include_fk)) {
- continue;
- }
- }
-
- $foreign_values = array($primary_key => $object->{$foreign_key});
- $options = array();
- if (is_array($include_fk)) {
- $options['include_fk'] = $include_fk[$foreign_key];
- }
- $foreign_object = tripal_core_generate_chado_var($foreign_table, $foreign_values, $options);
-
- $object->{$foreign_key} = $foreign_object;
-
- if (is_array($object->{$foreign_key}->expandable_fields)) {
- $object->expandable_fields = array_merge(
- $object->expandable_fields,
- $object->{$foreign_key}->expandable_fields
- );
- unset($object->{$foreign_key}->expandable_fields);
- }
- if (is_array($object->{$foreign_key}->expandable_tables)) {
- $object->expandable_tables = array_merge(
- $object->expandable_tables,
- $object->{$foreign_key}->expandable_tables
- );
- unset($object->{$foreign_key}->expandable_tables);
- }
- if (is_array($object->{$foreign_key}->expandable_nodes)) {
- $object->expandable_nodes = array_merge(
- $object->expandable_nodes,
- $object->{$foreign_key}->expandable_nodes
- );
- unset($object->{$foreign_key}->expandable_nodes);
- }
- }
- }
- $results[$key] = $object;
- }
- }
- }
-
-
- $results_arr = array();
- foreach ($results as $record) {
- $results_arr[] = $record;
- }
-
- if (!$return_array) {
- if (sizeof($results_arr) == 1) {
-
- return $results_arr[0];
- }
- elseif (!empty($results_arr)) {
- return $results_arr;
- }
- else {
-
- }
- }
-
-
- else {
- if (!$results_arr) {
- return array();
- }
- else {
- return $results_arr;
- }
- }
- }
- function tripal_core_expand_chado_vars($object, $type, $to_expand, $table_options = array()) {
-
- if (!$object) {
- watchdog('tripal_core', 'Cannot pass non array as argument, $object, to tripal_core_expand_chado_vars function.', array(), WATCHDOG_ERROR);
- return $object;
- }
-
- if (is_array($object)) {
- foreach ($object as $index => $o) {
- $object[$index] = tripal_core_expand_chado_vars($o, $type, $to_expand);
- }
- return $object;
- }
-
- $base_table = $object->tablename;
- switch ($type) {
- case "field":
- if (preg_match('/(\w+)\.(\w+)/', $to_expand, $matches)) {
- $tablename = $matches[1];
- $fieldname = $matches[2];
- $table_desc = tripal_core_get_chado_table_schema($tablename);
- $values = array();
- foreach ($table_desc['primary key'] as $key) {
- $values[$key] = $object->{$key};
- }
- if ($base_table == $tablename) {
-
- $results = tripal_core_chado_select($tablename, array($fieldname), $values);
- $object->{$fieldname} = $results[0]->{$fieldname};
- $object->expanded = $to_expand;
- }
- else {
-
- foreach ((array) $object as $field_name => $field_value) {
- if (is_object($field_value)) {
- $object->{$field_name} = tripal_core_expand_chado_vars(
- $field_value,
- 'field',
- $to_expand
- );
- }
- }
- }
- }
- else {
- watchdog('tripal_core', 'tripal_core_expand_chado_vars: Field (%field) not in the right format. " .
- "It should be <tablename>.<fieldname>', WATCHDOG_ERROR);
- }
- break;
- case "table":
- $foreign_table = $to_expand;
-
- if (array_key_exists($foreign_table, $object)) {
- return $object;
- }
- $foreign_table_desc = tripal_core_get_chado_table_schema($foreign_table);
-
- if ($foreign_table_desc['foreign keys'][$base_table]) {
- foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
-
- if (!$object->{$right}) {
- continue;
- }
-
-
-
- $new_options = $table_options;
-
- $foreign_object = tripal_core_generate_chado_var($foreign_table, array($left => $object->{$right}), $new_options);
-
- if ($foreign_object) {
-
-
-
- if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
- if (!is_object($object->{$foreign_table})) {
- $object->{$foreign_table} = new stdClass();
- }
- $object->{$foreign_table}->{$left} = $foreign_object;
- $object->expanded = $to_expand;
- }
- else {
- $object->{$foreign_table} = $foreign_object;
- $object->expanded = $to_expand;
- }
- }
-
- else {
- if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
- $object->{$foreign_table}->{$left} = NULL;
- }
- else {
- $object->{$foreign_table} = NULL;
- }
- }
- }
- }
-
- else {
-
- $did_expansion = 0;
- foreach ((array) $object as $field_name => $field_value) {
-
- if (is_object($field_value)) {
- $did_expansion = 1;
- $object->{$field_name} = tripal_core_expand_chado_vars($field_value, 'table', $foreign_table);
- }
- }
-
-
- if (!$did_expansion) {
- watchdog('tripal_core', 'tripal_core_expand_chado_vars: Could not expand table, %table. It is ' .
- 'not in a foreign key relationship with the base object nor with any other expanded table. ' .
- 'Check the table definition to ensure that a proper foreign key relationship is present.',
- array('%table' => $foreign_table), WATCHDOG_ERROR);
- }
- }
- break;
- case "node":
-
- if ($object->tablename == $to_expand) {
- $node = node_load($object->nid);
- if ($node) {
- $object->expanded = $to_expand;
- $node->expandable_fields = $object->expandable_fields;
- unset($object->expandable_fields);
- $node->expandable_tables = $object->expandable_tables;
- unset($object->expandable_tables);
- $node->expandable_nodes = $object->expandable_nodes;
- unset($object->expandable_nodes);
- $node->{$base_table} = $object;
- $object = $node;
- }
- else {
- watchdog('tripal_core', 'tripal_core_expand_chado_vars: No node matches the nid (%nid) supplied.',
- array('%nid' => $object->nid), WATCHDOG_ERROR);
- }
- }
- else {
-
- foreach ((array) $object as $field_name => $field_value) {
- if (is_object($field_value)) {
- $object->{$field_name} = tripal_core_expand_chado_vars(
- $field_value,
- 'node',
- $to_expand
- );
- }
- }
- }
- break;
- default:
- watchdog('tripal_core', 'tripal_core_expand_chado_vars: Unrecognized type (%type). Should be one of "field", "table", "node".',
- array('%type' => $type), WATCHDOG_ERROR);
- return FALSE;
- }
-
- if (!$object->expanded) {
-
- foreach ( (array)$object as $field_name => $field_value) {
- if (is_object($field_value)) {
- if (isset($field_value->expanded)) {
- $object->expanded = $field_value->expanded;
- unset($field_value->expanded);
- }
- }
- }
- }
-
- if ($object->expanded) {
- $expandable_name = 'expandable_' . $type . 's';
- if ($object->{$expandable_name}) {
- $key_to_remove = array_search($object->expanded, $object->{$expandable_name});
- unset($object->{$expandable_name}[$key_to_remove]);
- unset($object->expanded);
- }
- else {
-
-
-
-
-
-
-
-
- }
- }
- return $object;
- }
- function tripal_core_exclude_type_by_default() {
- return array('text' => 'strlen(">field_value< ") > 100');
- }
- function tripal_core_exclude_field_from_feature_by_default() {
- return array();
- }
- function chado_pager_query($query, $limit, $element, $count_query) {
-
-
-
- global $pager_page_array, $pager_total, $pager_total_items;
- $page = isset($_GET['page']) ? $_GET['page'] : '';
-
- $args = func_get_args();
- $args = array_slice($args, 4);
-
- if (isset($args[0]) && is_array($args[0])) {
- $args = $args[0];
- }
-
- if (!isset($count_query)) {
- $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
- }
-
- $pager_page_array = explode(',', $page);
-
- $pager_total_items[$element] = db_result(chado_query($count_query, $args));
- $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
- $pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], ((int) $pager_total[$element]) - 1));
- return chado_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
- }
- function chado_query_range($query, $args, $from, $count) {
- $query .= ' LIMIT ' . (int) $count . ' OFFSET ' . (int) $from;
- return chado_query($query, $args);
- }
- function chado_query($sql, $args = array()) {
-
- $is_local = tripal_core_is_chado_local();
-
-
-
- if ($is_local) {
- $sql = preg_replace('/\n/', '', $sql);
- $sql = preg_replace('/\{(.*?)\}/', 'chado.$1', $sql);
- $results = db_query($sql, $args);
- }
-
-
- else {
- $previous_db = tripal_db_set_active('chado') ;
- $results = db_query($sql);
- tripal_db_set_active($previous_db);
- }
-
- return $results;
- }
- function chado_get_id_for_node($table, $nid) {
- $sql = "SELECT " . $table . "_id as id FROM {chado_$table} WHERE nid = :nid";
- $result = db_query($sql, array(':nid' => $nid))->fetchObject();
-
- return $result->id;
- }
- function chado_get_node_id($table, $id) {
- $sql = "SELECT nid FROM {chado_$table} WHERE " . $table . "_id = :" . $table . "_id";
- $result = db_query($sql, array(":" . $table . "_id" => $id))->fetchObject();
- return $results->nid;
- }
- function tripal_core_get_property($basetable, $record_id, $property, $cv_name) {
-
- $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
- $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
-
- $values = array(
- $fkcol => $record_id,
- 'type_id' => array(
- 'cv_id' => array(
- 'name' => $cv_name,
- ),
- 'name' => $property,
- 'is_obsolete' => 0
- ),
- );
- $results = tripal_core_generate_chado_var($basetable . 'prop', $values);
- if ($results) {
- $results = tripal_core_expand_chado_vars($results, 'field', $basetable . 'prop.value');
- }
- return $results;
- }
- function tripal_core_insert_property($basetable, $record_id, $property,
- $cv_name, $value, $update_if_present = 0) {
-
-
-
- $props = tripal_core_get_property($basetable, $record_id, $property, $cv_name);
- if (!is_array($props) and $props) {
- $props = array($props);
- }
-
- $rank = 0;
- if (count($props) > 0) {
- if ($update_if_present) {
- return tripal_core_update_property($basetable, $record_id, $property, $cv_name, $value);
- }
- else {
-
-
-
- foreach ($props as $p) {
- if ($p->rank > $rank) {
- $rank = $p->rank;
- }
- if (strcmp($p->value, $value) == 0) {
- return TRUE;
- }
- }
-
- $rank++;
- }
- }
-
-
- $values = array(
- 'cv_id' => array(
- 'name' => $cv_name,
- ),
- 'name' => $property,
- );
-
- $options = array('statement_name' => 'sel_cvterm_cv');
- $term = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
- if (!$term or count($term) == 0) {
- watchdog('tripal_core', "Cannot find property '%prop_name'.",
- array('%prop_name' => $property), WATCHDOG_ERROR);
- return FALSE;
- }
-
-
- $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
- $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
-
- $values = array(
- $fkcol => $record_id,
- 'type_id' => array(
- 'cv_id' => array(
- 'name' => $cv_name,
- ),
- 'name' => $property,
- ),
- 'value' => $value,
- 'rank' => $rank,
- );
- $options = array('statement_name' => 'ins_' . $basetable . 'prop_' . substr($fkcol, 0, 2) . 'tyvara');
- $result = tripal_core_chado_insert($basetable . 'prop', $values, $options);
- return $result;
- }
- function tripal_core_update_property($basetable, $record_id, $property,
- $cv_name, $value, $insert_if_missing = 0) {
-
- $prop = tripal_core_get_property($basetable, $record_id, $property, $cv_name);
- if (count($prop)==0) {
- if ($insert_if_missing) {
- return tripal_core_insert_property($basetable, $record_id, $property, $cv_name, $value);
- }
- else {
- return FALSE;
- }
- }
-
- $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
- $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
-
- $match = array(
- $fkcol => $record_id,
- 'type_id' => array(
- 'cv_id' => array(
- 'name' => $cv_name,
- ),
- 'name' => $property,
- ),
- );
-
- $values = array(
- 'value' => $value,
- );
- return tripal_core_chado_update($basetable . 'prop', $match, $values);
- }
- function tripal_core_update_property_by_id($basetable, $record_id, $property,
- $cv_name, $value) {
-
- $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
- $pkcol = $table_desc['primary key'][0];
-
- $match = array(
- $pkcol => $record_id,
- );
-
- $values = array(
- 'type_id' => array(
- 'cv_id' => array(
- 'name' => $cv_name,
- ),
- 'name' => $property,
- ),
- 'value' => $value,
- );
- return tripal_core_chado_update($basetable . 'prop', $match, $values);
- }
- function tripal_core_delete_property($basetable, $record_id, $property, $cv_name) {
-
- $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
- $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
-
- $match = array(
- $fkcol => $record_id,
- 'type_id' => array(
- 'cv_id' => array(
- 'name' => $cv_name,
- ),
- 'name' => $property,
- ),
- );
- return tripal_core_chado_delete($basetable . 'prop', $match);
- }
- function tripal_core_delete_property_by_id($basetable, $record_id) {
-
- $table_desc = tripal_core_get_chado_table_schema($basetable . 'prop');
- $pkcol = $table_desc['primary key'][0];
-
- $match = array(
- $pkcol => $record_id,
- );
- return tripal_core_chado_delete($basetable . 'prop', $match);
- }
- function tripal_db_start_transaction() {
- }
- function tripal_db_set_savepoint_transaction($savepoint, $release = FALSE) {
-
- if ($release) {
- chado_query("RELEASE SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
- }
- chado_query("SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
- }
- function tripal_db_commit_transaction() {
- chado_query("COMMIT");
- }
- function tripal_db_rollback_transaction($savepoint = NULL, $commit = TRUE) {
- if ($savepoint) {
- chado_query("ROLLBACK TO SAVEPOINT :savepoint", array(':savepoint' => $savepoint));
- }
- else {
- chado_query("ROLLBACK");
- }
- if ($commit) {
- tripal_db_commit_transaction();
- }
- }
- function tripal_get_chado_custom_schema($table) {
- $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
- $results = db_query($sql, array(':table_name' => $table));
- $custom = $results->fetchObject();
- if (!$custom) {
- return FALSE;
- }
- else {
- return unserialize($custom->schema);
- }
- }
- function chado_table_exists($table) {
- global $databases;
-
- $default_db = $databases['default']['default']['database'];
- $sql = "
- SELECT 1
- FROM information_schema.tables
- WHERE
- table_name = :table_name AND
- table_schema = 'chado' AND
- table_catalog = '$default_db'
- ";
- $results = db_query($sql, array(':table_name' => $table));
- $exists = $results->fetchObject();
- if (!$exists) {
- return FALSE;
- }
- return TRUE;
- }
- function tripal_core_chado_schema_exists() {
- $exists = variable_get('chado_schema_exists', FALSE);
-
- if (!$exists) {
-
-
- $sql = "
- SELECT nspname
- FROM pg_catalog.pg_namespace
- WHERE nspname = 'chado'
- ";
- $results = db_query($sql);
- $name = $results->fetchObject();
- if ($name) {
- variable_set('chado_schema_exists', TRUE);
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- return TRUE;
- }
- function tripal_core_schema_exists($schema) {
-
- $sql = "
- SELECT nspname
- FROM pg_namespace
- WHERE
- has_schema_privilege(nspname, 'USAGE') AND
- nspname = :nspname
- ORDER BY nspname
- ";
- $results = db_query($sql, array(':nspname' => $schema));
- $name = $results->fetchObject();
- if (strcmp($name->nspname, $schema) != 0) {
- return FALSE;
- }
- return TRUE;
- }
- function tripal_core_get_chado_tables($include_custom = NULL) {
-
- $v = tripal_core_get_chado_version();
- $tables = array();
- if ($v == '1.2') {
- $tables_v1_2 = tripal_core_chado_get_v1_2_tables();
- foreach ($tables_v1_2 as $table) {
- $tables[$table] = $table;
- }
- }
- if ($v == '1.11' or $v == '1.11 or older') {
- $tables_v1_11 = tripal_core_chado_get_v1_11_tables();
- foreach ($tables_v1_11 as $table) {
- $tables[$table] = $table;
- }
- }
-
- if ($include_custom) {
- $sql = "SELECT table_name FROM {tripal_custom_tables}";
- $resource = db_query($sql);
- foreach ($resource as $r) {
- $tables[$r->table_name] = $r->table_name;
- }
- }
- asort($tables);
- return $tables;
- }
- function tripal_core_set_chado_version() {
- global $databases;
- $is_local = 0;
-
- $chado_exists = tripal_core_chado_schema_exists();
- if (!$chado_exists) {
-
-
- if (!array_key_exists('chado', $databases)) {
-
-
- return 'not installed';
- }
- $previous_db = tripal_db_set_active('chado');
- $prop_exists = db_table_exists('chadoprop');
- tripal_db_set_active($previous_db);
- }
- else {
- $is_local = 1;
- $prop_exists = db_table_exists('chado.chadoprop');
- }
-
-
-
- if (!$prop_exists) {
- return "1.11 or older";
- }
- $sql = "
- SELECT value
- FROM {chadoprop} CP
- INNER JOIN {cvterm} CVT on CVT.cvterm_id = CP.type_id
- INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
- WHERE CV.name = 'chado_properties' and CVT.name = 'version'
- ";
- if (!$is_local) {
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql);
- tripal_db_set_active($previous_db);
- }
- else {
- $results = chado_query($sql);
- }
- $v = $results->fetchObject();
-
-
- if (!$v) {
- variable_set('chado_version', "1.11 or older");
- return "1.11 or older";
- }
- variable_set('chado_version', $v->value);
- return $v->value;
- }
- function tripal_core_get_chado_version($exact = FALSE, $warn_if_unsupported = FALSE) {
-
- $exact_version = variable_get('chado_version', '');
- if (!$exact_version) {
- $exact_version = tripal_core_set_chado_version();
- }
-
-
-
- if (strcmp($exact_version, '1.11 or older') == 0) {
- $exact_version = "1.11";
- if ($warn_if_unsupported) {
- drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11. If you are certain this is v1.11
- or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
- 'warning');
- }
- }
-
-
- $effective_version = $exact_version;
- if (preg_match('/^1\.2\d+$/', $effective_version)) {
- $effective_version = "1.2";
- }
- if ($warn_if_unsupported and ($effective_version != 1.11 and $effective_version != 1.2 and $effective_version != 'not installed')) {
- drupal_set_message(t("WARNING: The currently installed version of Chado, v$exact_version, is not fully compatible with Tripal."), 'warning');
- }
-
- if ($exact) {
- return $exact_version;
- }
- return $effective_version;
- }
- function tripal_core_get_chado_table_schema($table) {
-
- $v = tripal_core_get_chado_version();
-
- $v = preg_replace("/\./", "_", $v);
- $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);
-
- if (!is_array($table_arr) or count($table_arr) == 0) {
- $table_arr = tripal_get_chado_custom_schema($table);
- }
- return $table_arr;
- }
- function tripal_core_clean_orphaned_nodes($table, $job_id) {
- $count = 0;
-
- $dsql = "SELECT * FROM {node} WHERE type = 'chado_" . $table . "' order by nid";
- $nsql = "SELECT * FROM {node} WHERE nid = :nid";
- $csql = "SELECT * FROM {chado_" . $table . "} WHERE nid = :nid ";
- $clsql= "SELECT * FROM {chado_" . $table . "}";
- $lsql = "SELECT * FROM {" . $table . "} where " . $table . "_id = :" . $table . "_id ";
-
- print "Getting nodes\n";
- $nodes = array();
- $res = db_query($dsql);
- foreach ($res as $node) {
- $nodes[$count] = $node;
- $count++;
- }
-
- print "Getting chado_$table\n";
- $cnodes = array();
- $res = db_query($clsql);
- foreach ($res as $node) {
- $cnodes[$count] = $node;
- $count++;
- }
- $interval = intval($count * 0.01);
- if ($interval < 1) {
- $interval = 1;
- }
-
-
- print "Verifying all chado_$table Entries\n";
- $deleted = 0;
- foreach ($cnodes as $nid) {
-
- if ($job_id and $i % $interval == 0) {
- tripal_job_set_progress($job_id, intval(($i / $count) * 100));
- }
-
- $results = db_query($nsql, array(':nid' => $nid->nid));
- $node = $results->fetchObject();
- if (!$node) {
- $deleted++;
- db_query("DELETE FROM {chado_" . $table . "} WHERE nid = :nid", array(':nid' => $nid->nid));
- $message = "chado_$table missing node.... DELETING: $nid->nid";
- watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
- }
-
- $table_id = $table . "_id";
- $results = chado_query($lsql, array(":" . $table . "_id " => $nid->$table_id));
- $record = $results->fetchObject();
- if (!$record) {
- $deleted++;
- $sql = "DELETE FROM {chado_" . $table . "} WHERE " . $table . "_id = :" . $table . "_id";
- db_query($sql, array(":" . $table . "_id" => $nid->$table_id));
- $message = "chado_$table missing $table.... DELETING entry.";
- watchdog('tripal_core', $message, array(), WATCHDOG_WARNING);
- }
- $i++;
- }
- print "\t$deleted chado_$table entries missing either a node or chado entry.\n";
-
-
- $deleted = 0;
- foreach ($nodes as $node) {
-
- if ($job_id and $i % $interval == 0) {
- tripal_job_set_progress($job_id, intval(($i / $count) * 100));
- }
-
-
- $results = db_query($csql, array(":nid" => $node->nid));
- $link = $results->fetchObject();
- if (!$link) {
- if (node_access('delete', $node)) {
- $deleted++;
- $message = "Node missing in chado_$table table.... DELETING node $node->nid";
- watchdog("tripal_core", $message, array(), WATCHDOG_WARNING);
- node_delete($node->nid);
- }
- else {
- $message = "Node missing in chado_$table table.... but cannot delete due to improper permissions (node $node->nid)";
- watchdog("tripal_core", $message, array(), WATCHDOG_WARNING);
- }
- }
- $i++;
- }
- print "\t$deleted nodes did not have corresponding chado_$table entries.\n";
- return '';
- }
- function tripal_core_is_chado_installed() {
- global $databases;
-
- if (array_key_exists('chado', $databases)) {
- return TRUE;
- }
-
- return tripal_core_chado_schema_exists();
- }
- function tripal_core_is_chado_local() {
- global $db_url, $db_type;
- $is_installed = tripal_core_is_chado_installed();
- if ($is_installed) {
- if (is_array($db_url)) {
- if (isset($db_url['chado'])) {
- return FALSE;
- }
- }
- return TRUE;
- }
- return FALSE;
- }
- function tripal_core_is_tripal_node_type($chado_table) {
- $linking_table = 'chado_' . $chado_table;
- if (db_table_exists($linking_table)) {
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- function tripal_db_set_active($dbname = 'default') {
- global $databases, $active_db;
-
- $chado_exists = variable_get('chado_schema_exists', FALSE);
- if ($chado_exists) {
- if ($dbname == 'chado') {
- db_query('set search_path to chado');
- return 'default';
- }
- else {
- db_query('set search_path to public');
- return 'chado';
- }
- }
-
-
-
- elseif (array_key_exists($dbname, $databases)) {
- return db_set_active($dbname);
- }
- }
|