1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875 |
- <?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_array($args)) {
- watchdog('tripal_core', 'Need to pass an array to chado_query, "%value" passed instead. Query: %query', array('%value' => $args, '%query' => $sql), WATCHDOG_ERROR);
- $args = array($args);
- }
-
-
- 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);
- }
- }
|