123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613 |
- <?php
- require_once "tripal_core.schema_v1.2.api.inc";
- require_once "tripal_core.schema_v1.11.api.inc";
- // Globals used by Tripals Error catching functions
- // Should match those defined by watchdog
- define('TRIPAL_CRITICAL',2);
- define('TRIPAL_ERROR',3);
- define('TRIPAL_WARNING',4);
- define('TRIPAL_NOTICE',5);
- define('TRIPAL_INFO',6);
- define('TRIPAL_DEBUG',7);
- /**
- * This function is used to set the global Chado variables
- */
- function tripal_core_set_globals() {
- // these global variables are meant to be accessed by all Tripal
- // modules to find the chado version installed and if Chado is local.
- // these variables are stored as globals rather than using the drupal_set_variable
- // functions because the Drupal functions make databaes queries and for long
- // running loaders we don't want those queries repeatedly.
- $GLOBALS["chado_is_installed"] = tripal_core_is_chado_installed();
- if ($GLOBALS["chado_is_installed"]) {
- $GLOBALS["chado_is_local"] = tripal_core_is_chado_local();
- $GLOBALS["chado_version"] = tripal_core_get_chado_version();
- $GLOBALS["exact_chado_version"] = tripal_core_get_chado_version(TRUE);
- }
- }
- /**
- * Provide better error notice for Tripal
- * @param $type
- * The catagory to which this message belongs. Can be any string, but the general
- * practice is to use the name of the module.
- * @param $message
- * The message to store in the log. Keep $message translatable by not concatenating
- * dynamic values into it! Variables in the message should be added by using placeholder
- * strings alongside the variables argument to declare the value of the placeholders.
- * See t() for documentation on how $message and $variables interact.
- * @param $variables
- * Array of variables to replace in the message on display or NULL if message is
- * already translated or not possible to translate.
- * @param $severity
- * The severity of the message; one of the following values:
- * - TRIPAL_CRITICAL: Critical conditions.
- * - TRIPAL_ERROR: Error conditions.
- * - TRIPAL_WARNING: Warning conditions.
- * - TRIPAL_NOTICE: (default) Normal but significant conditions.
- * - TRIPAL_INFO: Informational messages.
- * - TRIPAL_DEBUG: Debug-level messages.
- * @param $options
- * An array of options. Some available options include:
- * - print: prints the error message to the screen. Useful when display is the command-line
- */
- function tripal_core_report_error($type, $severity, $message, $variables = array(), $options = array()) {
- // Get human-readable severity string
- $severity_string = '';
- switch ($severity) {
- case TRIPAL_CRITICAL:
- $severity_string = 'CRITICAL';
- break;
- case TRIPAL_ERROR:
- $severity_string = 'ERROR';
- break;
- case TRIPAL_WARNING:
- $severity_string = 'WARNING';
- break;
- case TRIPAL_NOTICE:
- $severity_string = 'NOTICE';
- break;
- case TRIPAL_INFO:
- $severity_string = 'INFO';
- break;
- case TRIPAL_DEBUG:
- $severity_string = 'DEBUG';
- break;
- }
- // Send to watchdog
- try {
- watchdog($type, $message, $variables, $severity);
- }
- catch (Exception $e) {
- print "CRITICAL (TRIPAL_CORE): Unable to register error message with watchdog";
- $options['print'] = TRUE;
- }
- // If print option supplied then print directly to the screen
- if (isset($options['print'])) {
- if (sizeof($variables) > 0) {
- $message = str_replace(array_keys($variables), $variables, $message);
- }
- print $severity_string . ' (' . strtoupper($type) . '):' . $message . "\n";
- }
- }
- /**
- * @file
- * The Tripal Core API
- *
- * This file provides the API needed for all other Tripal and Tripal dependent
- * modules.
- *
- *
- * @defgroup tripal_chado_api Chado API
- * @ingroup tripal_core_api
- * @{
- * Provides an application programming interface (API) to manage data withing the Chado database.
- * This includes functions for selecting, inserting, updating and deleting records
- * in Chado tables. The functions will ensure proper integrity contraints are met
- * for inserts and updates.
- *
- * Also, a set of functions is provided for creating template variables. First,
- * is the tripal_core_generate_chado_vars which is used to select one ore more
- * records from a table and return an array with foreign key relationships fully
- * populated. For example, if selecting a feature, the organism_id and type_id
- * would be present in the returned array as a nested array with their respective
- * foreign keys also nested. The only fields that are not included are text
- * fields (which may be very large) or many-to-many foreign key relationships.
- * However, these fields and relationships can be expanded using the
- * tripal_core_expand_chado_vars.
- *
- * When a row from a chado table is selected using these two functions, it provides
- * a way for users who want to cutomize Drupal template files to access all data
- * associate with a specific record.
- *
- * Finally, the property tables in Chado generally follow the same format. Therefore
- * there is a set of functions for inserting, updating and deleting properties for
- * any table. This provides quick lookup of properties (provided the CV term is
- * known).
- *
- * @}
- *
- */
- /**
- * Provides a generic routine for inserting into any Chado table
- *
- * Use this function to insert a record into any Chado table. The first
- * argument specifies the table for inserting and the second is an array
- * of values to be inserted. The array is mutli-dimensional such that
- * foreign key lookup values can be specified.
- *
- * @param $table
- * The name of the chado table for inserting
- * @param $values
- * An associative array containing the values for inserting.
- * @param $options
- * An array of options such as:
- * - skip_validation: TRUE or FALSE. If TRUE will skip all the validation steps and
- * just try to insert as is. This is much faster but results in unhandled
- * non user-friendly errors if the insert fails.
- * - return_record: by default, the function will return the record but with
- * the primary keys added after insertion. To simply return TRUE on success
- * set this option to FALSE
- *
- * @return
- * On success this function returns the inserted record with the new primary keys
- * added to the returned array. On failure, it returns FALSE.
- *
- * Example usage:
- * @code
- * $values = array(
- * 'organism_id' => array(
- * 'genus' => 'Citrus',
- * 'species' => 'sinensis',
- * ),
- * 'name' => 'orange1.1g000034m.g',
- * 'uniquename' => 'orange1.1g000034m.g',
- * 'type_id' => array (
- * 'cv_id' => array (
- * 'name' => 'sequence',
- * ),
- * 'name' => 'gene',
- * 'is_obsolete' => 0
- * ),
- * );
- * $result = tripal_core_chado_insert('feature',$values);
- * @endcode
- * The above code inserts a record into the feature table. The $values array is
- * nested such that the organism is selected by way of the organism_id foreign
- * key constraint by specifying the genus and species. The cvterm is also
- * specified using its foreign key and the cv_id for the cvterm is nested as
- * well.
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_chado_insert($table, $values, $options = array()) {
- $print_errors = (isset($options['print_errors'])) ? $options['print_errors'] : FALSE;
- if (!is_array($values)) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'Cannot pass non array as values for inserting.',
- array(),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- if (count($values)==0) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'Cannot pass an empty array as values for inserting.',
- array(),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- // set defaults for options. If we don't set defaults then
- // we get memory leaks when we try to access the elements
- 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;
- }
- // get the table description
- $table_desc = tripal_core_get_chado_table_schema($table);
- if (empty($table_desc)) {
- tripal_core_report_error('tripal_core', TRIPAL_WARNING,
- 'tripal_core_chado_insert; There is no table description for !table_name',
- array('!table_name' => $table), array('print' => $print_errors)
- );
- }
- // iterate through the values array and create a new 'insert_values' array
- // that has all the values needed for insert with all foreign relationsihps
- // resolved.
- foreach ($values as $field => $value) {
- // make sure the field is in the table description. If not then return an error
- // message
- if (!array_key_exists($field, $table_desc['fields'])) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- "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)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- if (is_array($value)) {
- // select the value from the foreign key relationship for this value
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value);
- if (sizeof($results) > 1) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- '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)),
- array('print' => $print_errors)
- );
- }
- elseif (sizeof($results) < 1) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_DEBUG,
- 'tripal_core_chado_insert: no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)',
- array('!foreign_key' => $field, '!criteria' => print_r($value, TRUE)),
- array('print' => $print_errors)
- );
- }
- else {
- $insert_values[$field] = $results[0];
- }
- }
- else {
- $insert_values[$field] = $value;
- }
- }
- if ($validate) {
- // check for violation of any unique constraints
- $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) {
- // build the arrays for performing a select that will check the contraint
- $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];
- }
- }
- // now check the constraint
- if (tripal_core_chado_select($table, $ukselect_cols, $ukselect_vals)) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR,
- "tripal_core_chado_insert; Cannot insert duplicate record into $table table: !values",
- array('!values' => print_r($values, TRUE)), array('print' => $print_errors)
- );
- return FALSE;
- }
- }
- }
- // if trying to insert a field that is the primary key, make sure it also is unique
- if (array_key_exists('primary key', $table_desc)) {
- $pkey = $table_desc['primary key'][0];
- if (array_key_exists($pkey, $insert_values)) {
- $coptions = array();
- if (tripal_core_chado_select($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR,
- 'tripal_core_chado_insert; Cannot insert duplicate primary key into !table table: !values',
- array('!table' => $table, '!values' => print_r($values, TRUE)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- }
- }
- // make sure required fields have a value
- if (!is_array($table_desc['fields'])) {
- $table_desc['fields'] = array();
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_WARNING,
- "tripal_core_chado_insert; %table missing fields: \n %schema",
- array('%table' => $table, '%schema' => print_r($table_desc, 1)),
- array('print' => $print_errors)
- );
- }
- foreach ($table_desc['fields'] as $field => $def) {
- // a field is considered missing if it cannot be NULL and there is no default
- // value for it or it is of type 'serial'
- 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) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- "tripal_core_chado_insert; Field %table.%field cannot be NULL: %values",
- array('%table' => $table, '%field' => $field, '%values' => print_r($values, 1)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- }
- } //end of validation
- // Now build the insert SQL statement
- $ifields = array(); // contains the names of the fields
- $itypes = array(); // contains placeholders for the sql query
- $ivalues = array(); // contains the values of the fields
- $i = 1;
- foreach ($insert_values as $field => $value) {
- $ifields[] = $field;
- $ivalues[":$field"] = $value;
- $i++;
- if (strcmp($value, '__NULL__')==0) {
- $itypes[] = "NULL";
- }
- else {
- $itypes[] = ":$field";
- }
- }
- // create the SQL
- $sql = 'INSERT INTO {' . $table . '} (' . implode(", ", $ifields) . ") VALUES (" . implode(", ", $itypes) . ")";
- $result = chado_query($sql, $ivalues);
- // if we have a result then add primary keys to return array
- 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->fetchField();
- if (!$value) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- "tripal_core_chado_insert; not able to retrieve primary key after insert: %sql",
- array('%sql' => $sql),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- $values[$field] = $value;
- }
- }
- return $values;
- }
- elseif ($options['return_record'] == FALSE and $result) {
- return TRUE;
- }
- else {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'tripal_core_chado_insert; Cannot insert record into "%table": %values',
- array('%table' => $table, '%values' => print_r($values, 1)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- return FALSE;
- }
- /**
- * Provides a generic routine for updating into any Chado table
- *
- * Use this function to update a record in any Chado table. The first
- * argument specifies the table for inserting, the second is an array
- * of values to matched for locating the record for updating, and the third
- * argument give the values to update. The arrays are mutli-dimensional such
- * that foreign key lookup values can be specified.
- *
- * @param $table
- * The name of the chado table for inserting
- * @param $match
- * An associative array containing the values for locating a record to update.
- * @param $values
- * An associative array containing the values for updating.
- * @param $options
- * An array of options such as:
- * - return_record: by default, the function will return the TRUE if the record
- * was succesfully updated. However, set this option to TRUE to return the
- * record that was updated. The returned record will have the fields provided
- * but the primary key (if available for the table) will be added to the record.
- * @return
- * On success this function returns TRUE. On failure, it returns FALSE.
- *
- * Example usage:
- * @code
- $umatch = array(
- 'organism_id' => array(
- 'genus' => 'Citrus',
- 'species' => 'sinensis',
- ),
- 'uniquename' => 'orange1.1g000034m.g7',
- 'type_id' => array (
- 'cv_id' => array (
- 'name' => 'sequence',
- ),
- 'name' => 'gene',
- 'is_obsolete' => 0
- ),
- );
- $uvalues = array(
- 'name' => 'orange1.1g000034m.g',
- 'type_id' => array (
- 'cv_id' => array (
- 'name' => 'sequence',
- ),
- 'name' => 'mRNA',
- 'is_obsolete' => 0
- ),
- );
- * $result = tripal_core_chado_update('feature',$umatch,$uvalues);
- * @endcode
- * The above code species that a feature with a given uniquename, organism_id,
- * and type_id (the unique constraint for the feature table) will be updated.
- * The organism_id is specified as a nested array that uses the organism_id
- * foreign key constraint to lookup the specified values to find the exact
- * organism_id. The same nested struture is also used for specifying the
- * values to update. The function will find the record that matches the
- * columns specified and update the record with the avlues in the $uvalues array.
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_chado_update($table, $match, $values, $options = NULL) {
- $print_errors = (isset($options['print_errors'])) ? $options['print_errors'] : FALSE;
- if (!is_array($values)) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'Cannot pass non array as values for updating.',
- array(),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- if (count($values)==0) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'Cannot pass an empty array as values for updating.',
- array(),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- if (!is_array($match)) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'Cannot pass non array as values for matching.',
- array(),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- if (count($match)==0) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'Cannot pass an empty array as values for matching.',
- array(),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- // set defaults for options. If we don't set defaults then
- // we get memory leaks when we try to access the elements
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('return_record', $options)) {
- $options['return_record'] = FALSE;
- }
- $update_values = array(); // contains the values to be updated
- $update_matches = array(); // contains the values for the where clause
- // get the table description
- $table_desc = tripal_core_get_chado_table_schema($table);
- // if the user wants us to return the record then we need to get the
- // unique primary key if one exists. That way we can add it to the
- // values that get returned at the end of the function
- $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();
- $results = tripal_core_chado_select($table, $columns, $match, $options2);
- if (count($results) > 0) {
- foreach ($results as $index => $pkey) {
- $pkeys[] = $pkey;
- }
- }
- }
- }
- // get the values needed for matching in the SQL statement
- foreach ($match as $field => $value) {
- if (is_array($value)) {
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value);
- if (sizeof($results) > 1) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR,
- '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)),
- array('print' => $print_errors)
- );
- }
- elseif (sizeof($results) < 1) {
- tripal_core_report_error('tripal_core',TRIPAL_DEBUG,
- 'tripal_core_chado_update: When trying to find record to update, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)',
- array('!foreign_key' => $field, '!criteria' => print_r($value, TRUE)),
- array('print' => $print_errors)
- );
- }
- else {
- $update_matches[$field] = $results[0];
- }
- }
- else {
- $update_matches[$field] = $value;
- }
- }
- // get the values used for updating
- foreach ($values as $field => $value) {
- if (is_array($value)) {
- $foreign_options = array();
- // select the value from the foreign key relationship for this value
- $results = tripal_core_chado_get_foreign_key($table_desc, $field, $value, $foreign_options);
- if (sizeof($results) > 1) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- '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)),
- array('print' => $print_errors)
- );
- }
- elseif (sizeof($results) < 1) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_DEBUG,
- 'tripal_core_chado_update: When trying to find update values, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)',
- array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)),
- array('print' => $print_errors)
- );
- }
- else {
- $update_values[$field] = $results[0];
- }
- }
- else {
- $update_values[$field] = $value;
- }
- }
- // now build the SQL statement
- $sql = 'UPDATE {' . $table . '} SET ';
- $args = array(); // arguments passed to chado_query
- 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); // get rid of the trailing comma & space
- $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); // get rid of the trailing 'AND'
- $result = chado_query($sql, $args);
- // if we have a result then add primary keys to return array
- if ($options['return_record'] == TRUE and $result) {
- // only if we have a single result do we want to add the primary keys to the values
- // array. If the update matched many records we can't add the pkeys
- 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 {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- "tripal_core_chado_update: Cannot update record in %table table. \nMatch: %match \nValues: %values",
- array('%table' => table, '%match' => print_r($match,TRUE), '%values' => print_r($values, 1)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- return FALSE;
- }
- /**
- * Provides a generic function for deleting a record(s) from any chado table
- *
- * Use this function to delete a record(s) in any Chado table. The first
- * argument specifies the table to delete from and the second is an array
- * of values to match for locating the record(s) to be deleted. The arrays
- * are mutli-dimensional such that foreign key lookup values can be specified.
- *
- * @param $table
- * The name of the chado table for inserting
- * @param $match
- * An associative array containing the values for locating a record to update.
- * @param $options
- * Currently there are no options
- * @return
- * On success this function returns TRUE. On failure, it returns FALSE.
- *
- * Example usage:
- * @code
- $umatch = array(
- 'organism_id' => array(
- 'genus' => 'Citrus',
- 'species' => 'sinensis',
- ),
- 'uniquename' => 'orange1.1g000034m.g7',
- 'type_id' => array (
- 'cv_id' => array (
- 'name' => 'sequence',
- ),
- 'name' => 'gene',
- 'is_obsolete' => 0
- ),
- );
- $uvalues = array(
- 'name' => 'orange1.1g000034m.g',
- 'type_id' => array (
- 'cv_id' => array (
- 'name' => 'sequence',
- ),
- 'name' => 'mRNA',
- 'is_obsolete' => 0
- ),
- );
- * $result = tripal_core_chado_update('feature', $umatch, $uvalues);
- * @endcode
- * The above code species that a feature with a given uniquename, organism_id,
- * and type_id (the unique constraint for the feature table) will be deleted.
- * The organism_id is specified as a nested array that uses the organism_id
- * foreign key constraint to lookup the specified values to find the exact
- * organism_id. The same nested struture is also used for specifying the
- * values to update. The function will find all records that match the
- * columns specified and delete them.
- *
- * @ingroup tripal_chado_api
- */
- 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;
- }
- // set defaults for options. If we don't set defaults then
- // we get memory leaks when we try to access the elements
- if (!is_array($options)) {
- $options = array();
- }
- $delete_matches = array(); // contains the values for the where clause
- // get the table description
- $table_desc = tripal_core_get_chado_table_schema($table);
- $fields = $table_desc['fields'];
- // get the values needed for matching in the SQL statement
- foreach ($match as $field => $value) {
- if (is_array($value)) {
- // if the user has specified an array of values to delete rather than
- // FK relationships the keep those in our match
- 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) {
- //watchdog('tripal_core', 'tripal_core_chado_delete: When trying to find record to delete, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
- }
- else {
- $delete_matches[$field] = $results[0];
- }
- }
- }
- else {
- $delete_matches[$field] = $value;
- }
- }
- // now build the SQL statement
- $sql = 'DELETE FROM {' . $table . '} WHERE ';
- $args = array();
- foreach ($delete_matches as $field => $value) {
- // if we have an array values then this is an "IN" clasue.
- // we cannot use prepared statements with these
- 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); // get rid of trailing ', '
- $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); // get rid of the trailing 'AND'
- // finally perform the delete. If successful, return the updated record
- $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;
- }
- /**
- * Provides a generic routine for selecting data from a Chado table
- *
- * Use this function to perform a simple select from any Chado table.
- *
- * @param $table
- * The name of the chado table for inserting
- * @param $columns
- * An array of column names
- * @param $values
- * An associative array containing the values for filtering the results. In the
- * case where multiple values for the same time are to be selected an additional
- * entry for the field should appear for each value
- * @param $options
- * An associative array of additional options where the key is the option
- * and the value is the value of that option.
- *
- * Additional Options Include:
- * - has_record
- * Set this argument to 'TRUE' to have this function return a numeric
- * value for the number of recrods rather than the array of records. this
- * can be useful in 'if' statements to check the presence of particula records.
- * - return_sql
- * Set this to 'TRUE' to have this function return an array where the first
- * element is the sql that would have been run and the second is an array of
- * arguments.
- * - case_insensitive_columns
- * An array of columns to do a case insensitive search on.
- * - regex_columns
- * An array of columns where the value passed in should be treated as a regular expression
- * - order_by
- * An associative array containing the column names of the table as keys
- * and the type of sort (i.e. ASC, DESC) as the values. The results in the
- * query will be sorted by the key values in the direction listed by the value
- * - is_duplicate: TRUE or FALSE. Checks the values submited to see if
- * they violate any of the unique constraints. If so, the record
- * is returned, if not, FALSE is returned.
- * - pager: Use this option if it is desired to return only a subset of results
- * so that they may be shown with in a Drupal-style pager. This should be
- * an array with two keys: 'limit' and 'element'. The value of 'limit'
- * should specify the number of records to return and 'element' is a
- * unique integer to differentiate between pagers when more than one
- * appear on a page. The 'element' should start with zero and increment by
- * one for each pager.
- *
- * @return
- * An array of results, FALSE if the query was not executed
- * correctly, an empty array if no records were matched, or the number of records
- * in the dataset if $has_record is set.
- * If the option 'is_duplicate' is provided and the record is a duplicate it
- * will return the duplicated record. If the 'has_record' option is provided
- * a value of TRUE will be returned if a record exists and FALSE will bee
- * returned if there are not records.
- *
- * Example usage:
- * @code
- * $columns = array('feature_id', 'name');
- * $values = array(
- * 'organism_id' => array(
- * 'genus' => 'Citrus',
- * 'species' => array('sinensis', 'clementina'),
- * ),
- * 'uniquename' => 'orange1.1g000034m.g',
- * 'type_id' => array (
- * 'cv_id' => array (
- * 'name' => 'sequence',
- * ),
- * 'name' => 'gene',
- * 'is_obsolete' => 0
- * ),
- * );
- * $options = array(
- * 'order_by' => array(
- * 'name' => 'ASC'
- * ),
- * );
- * $result = tripal_core_chado_select('feature',$columns,$values,$options);
- * @endcode
- * The above code selects a record from the feature table using the three fields
- * that uniquely identify a feature. The $columns array simply lists the columns
- * to select. The $values array is nested such that the organism is identified by
- * way of the organism_id foreign key constraint by specifying the genus and
- * species. The cvterm is also specified using its foreign key and the cv_id
- * for the cvterm is nested as well. In the example above, two different species
- * are allowed to match
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
- $print_errors = (isset($options['print_errors'])) ? $options['print_errors'] : FALSE;
- if (!is_array($values)) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR, 'Cannot pass non array as values for selecting.',
- array(), array('print' => $print_errors)
- );
- return FALSE;
- }
- if (!is_array($columns)) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR, 'Cannot pass non array as columns for selecting.',
- array(), array('print' => $print_errors)
- );
- return FALSE;
- }
- if (count($columns)==0) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR, 'Cannot pass an empty array as columns for selecting.',
- array(), array('print' => $print_errors)
- );
- return FALSE;
- }
- // set defaults for options. If we don't set defaults then
- // we get memory leaks when we try to access the elements
- 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'];
- }
- // check that our columns and values arguments are proper arrays
- if (!is_array($columns)) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'tripal_core_chado_select; the $columns argument must be an array. Columns:%columns',
- array('%columns' => print_r($columns, TRUE)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- if (!is_array($values)) {
- tripal_core_report_error(
- 'tripal_core',
- TRIPAL_ERROR,
- 'tripal_core_chado_select; the $values argument must be an array. Values:%values',
- array('%values' => print_r($values, TRUE)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- // get the table description
- $table_desc = tripal_core_get_chado_table_schema($table);
- $select = '';
- $from = '';
- $where = array();
- $args = array();
- // if the 'use_unique' option is turned on then we want
- // to remove all but unique keys
- if ($options['is_duplicate'] and array_key_exists('unique keys', $table_desc)) {
- $ukeys = $table_desc['unique keys'];
- $has_results = 0;
- // iterate through the unique constraints and reset the values and columns
- // arrays to only include these fields
- 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;
- // include the primary key in the results returned
- 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);
- }
- }
- // recreate the $values and $columns arrays
- foreach ($fields as $field) {
- if (array_key_exists($field, $values)) {
- $new_values[$field] = $values[$field];
- $uq_sname .= substr($field, 0, 2);
- // if there is no primary key then use the unique contraint fields
- if (!$has_pkey) {
- array_push($new_columns, $field);
- }
- }
- // if the field doesn't exist in the values array then
- // substitute any default values
- 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);
- }
- }
- // if there is no value (default or otherwise) check if this field is
- // allowed to be null
- 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);
- }
- }
- // if the array key doesn't exist in the values given by the caller
- // and there is no default value then we cannot check if the record
- // is a duplicate so return FALSE
- else {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR,
- 'tripal_core_chado_select: There is no value for %field thus we cannot check if this record is unique',
- array('%field' => $field), array('print' => $print_errors));
- return FALSE;
- }
- }
- $results = tripal_core_chado_select($table, $new_columns, $new_values, $new_options);
- // if we have a duplicate record then return the results
- 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) {
- // make sure the field is in the table description. If not then return an error
- // message
- if (!array_key_exists($field, $table_desc['fields'])) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR,
- '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)),
- array('print' => $print_errors)
- );
- return array();
- }
- $select[] = $field;
- if (is_array($value)) {
- // if the user has specified multiple values for matching then this we
- // want to catch that and save them in our $where array, otherwise
- // we'll descend for a foreign key relationship
- if (array_values($value) === $value) {
- $where[$field] = $value;
- }
- else {
- // select the value from the foreign key relationship for this value
- $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 {
- // need to catch a 0 and make int if integer field
- // but we don't want to catch a NULL
- if ($value === NULL) {
- $where[$field] = NULL;
- }
- elseif ($table_desc['fields'][$field]['type'] == 'int') {
- $where[$field][] = (int) $value;
- }
- else {
- $where[$field][] = $value;
- }
- }
- }
- // now build the SQL and prepared SQL statements. We may not use
- // the prepared statement if it wasn't requested in the options or if the
- // argument in a where statement has multiple values.
- if (empty($where)) {
- // sometimes want to select everything
- $sql = "SELECT " . implode(', ', $columns) . " ";
- $sql .= 'FROM {' . $table . '} ';
- // we don't prepare a statement if there is no where clause
- $prepared = FALSE;
- }
- else {
- $sql = "SELECT " . implode(', ', $columns) . " ";
- $sql .= 'FROM {' . $table . '} ';
- // if $values is empty then we want all results so no where clause
- if (!empty($values)) {
- $sql .= "WHERE ";
- }
- foreach ($where as $field => $value) {
- // if we have multiple values returned then we need an 'IN' statement
- // in our where statement
- 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); // remove trailing ', '
- $sql .= ") AND ";
- }
- // if we have a null value then we need an IS NULL in our where statement
- elseif ($value === NULL) {
- $sql .= "$field IS NULL AND ";
- // Need to remove one from the argument count b/c nulls don't add an argument
- }
- // if we have a single value then we need an = in our where statement
- 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];
- }
- }
- } // end foreach item in where clause
- $sql = drupal_substr($sql, 0, -4); // get rid of the trailing 'AND '
- } // end if (empty($where)){ } else {
- // finally add any ordering of the results to the SQL statement
- 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); // get rid of the trailing ', '
- }
- // if the caller has requested the SQL rather than the results then do so
- if ($options['return_sql'] == TRUE) {
- return array('sql' => $sql, 'args' => $args);
- }
- if (array_key_exists('limit', $pager)) {
- $total_records = 0;
- $resource = chado_pager_query($sql, $args, $pager['limit'], $pager['element'], NULL, $total_records);
- }
- else {
- $resource = chado_query($sql, $args);
- }
- // format results into an array
- $results = array();
- foreach ($resource as $r) {
- $results[] = $r;
- }
- if ($options['has_record']) {
- return count($results);
- }
- return $results;
- }
- /**
- * Gets the value of a foreign key relationship
- *
- * This function is used by tripal_core_chado_select, tripal_core_chado_insert,
- * and tripal_core_chado_update to iterate through the associate array of
- * values that gets passed to each of those routines. The values array
- * is nested where foreign key contraints are used to specify a value that. See
- * documentation for any of those functions for further information.
- *
- * @param $table_desc
- * A table description for the table with the foreign key relationship to be identified generated by
- * hook_chado_<table name>_schema()
- * @param $field
- * The field in the table that is the foreign key.
- * @param $values
- * An associative array containing the values
- * @param $options
- * An associative array of additional options where the key is the option
- * and the value is the value of that option. These options are passed on to tripal_core_chado_select.
- *
- * Additional Options Include:
- * - case_insensitive_columns
- * An array of columns to do a case insensitive search on.
- * - regex_columns
- * An array of columns where the value passed in should be treated as a regular expression
- *
- * @return
- * A string containg the results of the foreign key lookup, or FALSE if failed.
- *
- * Example usage:
- * @code
- *
- * $values = array(
- * 'genus' => 'Citrus',
- * 'species' => 'sinensis',
- * );
- * $value = tripal_core_chado_get_foreign_key('feature', 'organism_id',$values);
- *
- * @endcode
- * The above code selects a record from the feature table using the three fields
- * that uniquely identify a feature. The $columns array simply lists the columns
- * to select. The $values array is nested such that the organism is identified by
- * way of the organism_id foreign key constraint by specifying the genus and
- * species. The cvterm is also specified using its foreign key and the cv_id
- * for the cvterm is nested as well.
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $options = NULL) {
- // set defaults for options. If we don't set defaults then
- // we get memory leaks when we try to access the elements
- 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();
- }
- // get the list of foreign keys for this table description and
- // iterate through those until we find the one we're looking for
- $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'])) {
- //foreign key was described 2X
- $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'];
- // iterate through the columns of the foreign key relationship
- foreach ($columns as $left => $right) {
- // does the left column in the relationship match our field?
- if (strcmp($field, $left) == 0) {
- // the column name of the foreign key matches the field we want
- // so this is the right relationship. Now we want to select
- $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 {
- // TODO: what do we do if we get to this point and we have a fk
- // relationship expected but we don't have any definition for one in the
- // table schema??
- $version = $GLOBALS["chado_version"];
- $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();
- }
- /**
- * Generates an object containing the full details of a record(s) in chado.
- *
- * This differs from the objects returned by tripal_core_chado_select in so far as all foreign key
- * relationships have been followed meaning you have more complete details. Thus this function
- * should be used whenever you need a full variable and tripal_core_chado_select should be used if
- * you only case about a few columns.
- *
- * @param $table
- * The name of the base table to generate a variable for
- * @param $values
- * A select values array that selects the records you want from the base table
- * (this has the same form as tripal_core_chado_select)
- * @param $base_options
- * An array containing options for the base table. For example, an
- * option of 'order_by' may be used to sort results in the base table
- * if more than one are returned. The options must be compatible with
- * the options accepted by the tripal_core_chado_select() function.
- * Additionally, These options are available for this function:
- * -return_array:
- * can be provided to force the function to always return an array. Default
- * behavior is to return a single record if only one record exists or to return
- * an array if multiple records exist.
- * - include_fk:
- * an array of FK relationships to follow. By default, the
- * tripal_core_chado_select function will follow all FK relationships but this
- * may generate more queries then is desired slowing down this function call when
- * there are lots of FK relationships to follow. Provide an array specifying the
- * fields to include. For example, if expanding a property table (e.g. featureprop)
- * and you want the CV and accession but do not want the DB the following
- * array would work:
- *
- * $table_options = array(
- * 'include_fk' => array(
- * 'type_id' => array(
- * 'cv_id' => 1,
- * 'dbxref_id' => 1,
- * )
- * )
- * );
- *
- * The above array will expand the 'type_id' of the property table but only
- * further expand the cv_id and the dbxref_id and will go no further.
- * - pager:
- * Use this option if it is desired to return only a subset of results
- * so that they may be shown within a Drupal-style pager. This should be
- * an array with two keys: 'limit' and 'element'. The value of 'limit'
- * should specify the number of records to return and 'element' is a
- * unique integer to differentiate between pagers when more than one
- * appear on a page. The 'element' should start with zero and increment by
- * one for each pager. This only works when type is a 'table'.
- * @return
- * Either an object (if only one record was selected from the base table)
- * or an array of objects (if more than one record was selected from the base table).
- * If the option 'return_array' is provided the function always returns an array.
- *
- * Example Usage:
- * @code
- $values = array(
- 'name' => 'Medtr4g030710'
- );
- $features = tripal_core_generate_chado_var('feature', $values);
- * @endcode
- * This will return an object if there is only one feature with the name Medtr4g030710 or it will
- * return an array of feature objects if more than one feature has that name.
- *
- * Note to Module Designers: Fields can be excluded by default from these objects by implementing
- * one of the following hooks:
- * - hook_exclude_field_from_tablename_by_default (where tablename is the name of the table):
- * This hook allows you to add fields to be excluded on a per table basis. Simply implement
- * this hook to return an array of fields to be excluded. For example:
- * @code
- mymodule_exclude_field_from_feature_by_default() {
- return array('residues' => TRUE);
- }
- * @endcode
- * will ensure that feature.residues is ecluded from a feature object by default.
- * - hook_exclude_type_by_default:
- * This hook allows you to exclude fields from all tables that are of a given postgresql field
- * type. Simply implement this hook to return an array of postgresql types mapped to criteria.
- * Then all fields of that type where the criteria supplied returns TRUE will be excluded from
- * any table. Tokens available in criteria are >field_value< and >field_name< . For example:
- * @code
- mymodule_exclude_type_by_default() {
- return array('text' => 'length(>field_value< ) > 50');
- }
- * @endcode
- * will exclude all text fields with a length > 50. Thus if $feature.residues is longer than 50 * it will be excluded, otherwise it will be added.
- *
- * @ingroup tripal_chado_api
- */
- 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'];
- }
- // get description for the current table----------------------------------------------------------
- $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']);
- // Expandable fields without value needed for criteria--------------------------------------------
- $all->expandable_fields = array();
- if (array_key_exists('referring_tables', $table_desc) and $table_desc['referring_tables']) {
- $all->expandable_tables = $table_desc['referring_tables'];
- }
- else {
- $all->expandable_tables = array();
- }
- $all->expandable_nodes = array();
- /*
- // Get fields to be removed by name.................................
- $fields_to_remove = module_invoke_all('exclude_field_from_' . $table . '_by_default');
- foreach ($fields_to_remove as $field_name => $criteria) {
- //replace >field_name< with the current field name &
- $criteria = preg_replace('/>field_name< /', addslashes($field_name), $criteria);
- // if field_value needed we can't deal with this field yet
- if (preg_match('/>field_value< /', $criteria)) {
- break;
- }
- //if criteria then remove from query
- // @coder-ignore: only module designers can populate $criteria -not security risk
- $success = php_eval('<?php return ' . $criteria . '; ?>');
- if ($success) {
- unset($table_columns[array_search($field_name, $table_columns)]);
- unset($fields_to_remove[$field_name]);
- $all->expandable_fields[] = $table . '.' . $field_name;
- }
- }
- //Get fields to be removed by type................................
- $types_to_remove = module_invoke_all('exclude_type_by_default');
- $field_types = array();
- foreach ($table_desc['fields'] as $field_name => $field_array) {
- $field_types[$field_array['type']][] = $field_name;
- }
- foreach ($types_to_remove as $field_type => $criteria) {
- // if there are fields of that type to remove
- if (is_array($field_types[$field_type])) {
- //replace >field_name< with the current field name &
- $criteria = preg_replace('/>field_name< /', addslashes($field_name), $criteria);
- foreach ($field_types[$field_type] as $field_name) {
- // if field_value needed we can't deal with this field yet
- if (preg_match('/>field_value< /', $criteria)) {
- $fields_to_remove[$field_name] = $criteria;
- continue;
- }
- // if field_value needed we can't deal with this field yet
- if (preg_match('/>field_value< /', $criteria)) {
- break;
- }
- //if criteria then remove from query
- // @coder-ignore: only module designers can populate $criteria -not security risk
- $success = php_eval('<?php return ' . $criteria . '; ?>');
- if ($success) {
- unset($table_columns[array_search($field_name, $table_columns)]);
- $all->expandable_fields[] = $table . '.' . $field_name;
- }
- } //end of foreach field of that type
- }
- } //end of foreach type to be removed
- */
- // get the values for the record in the current table---------------------------------------------
- $results = tripal_core_chado_select($table, $table_columns, $values, $base_options);
- if ($results) {
- foreach ($results as $key => $object) {
- // Add empty expandable_x arrays
- $object->expandable_fields = $all->expandable_fields;
- $object->expandable_tables = $all->expandable_tables;
- $object->expandable_nodes = $all->expandable_nodes;
- // add curent table
- $object->tablename = $table;
- // check if the current table maps to a node type-----------------------------------------------
- // if this table is connected to a node there will be a chado_tablename table in drupal
- if (db_table_exists('chado_' . $table)) {
- // that has a foreign key to this one ($table_desc['primary key'][0]
- // and to the node table (nid)
- $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 and $mapping->$table_primary_key) {
- $object->nid = $mapping->nid;
- $object->expandable_nodes[] = $table;
- }
- }
- // remove any fields where criteria needs to be evalulated---------------------------------------
- /* foreach ($fields_to_remove as $field_name => $criteria) {
- if (!isset($object->{$field_name})) {
- break;
- }
- $criteria = preg_replace('/>field_value< /', addslashes($object->{$field_name}), $criteria);
- $success = php_eval('<?php return ' . $criteria . '; ?>');
- if ($success) {
- unset($object->{$field_name});
- $object->expandable_fields[] = $table . '.' . $field_name;
- }
- }
- */
- // recursively follow foreign key relationships nesting objects as we go------------------------
- 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) {
- // Note: Foreign key is the field in the current table whereas primary_key is the field in
- // the table referenced by the foreign key
- //Dont do anything if the foreign key is empty
- if (empty($object->{$foreign_key})) {
- continue;
- }
- if ($include_fk) {
- // don't recurse if the callee has supplied an $fk_include list and this
- // FK table is not in the list.
- if (is_array($include_fk) and !array_key_exists($foreign_key, $include_fk)) {
- continue;
- }
- // if we have the option but it is not an array then we don't recurse any furutehr
- if (!is_array($include_fk)) {
- continue;
- }
- }
- // get the record from the foreign table
- $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);
- // add the foreign record to the current object in a nested manner
- $object->{$foreign_key} = $foreign_object;
- // Flatten expandable_x arrays so only in the bottom object
- if (property_exists($object->{$foreign_key}, 'expandable_fields') and
- 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 (property_exists($object->{$foreign_key}, 'expandable_tables') and
- 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 (property_exists($object->{$foreign_key}, 'expandable_nodes') and
- 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;
- }
- }
- }
- // convert the results into an array
- $results_arr = array();
- foreach ($results as $record) {
- $results_arr[] = $record;
- }
- // check only one result returned
- if (!$return_array) {
- if (sizeof($results_arr) == 1) {
- // add results to object
- return $results_arr[0];
- }
- elseif (!empty($results_arr)) {
- return $results_arr;
- }
- else {
- // no results returned
- }
- }
- // the caller has requested results are always returned as
- // an array
- else {
- if (!$results_arr) {
- return array();
- }
- else {
- return $results_arr;
- }
- }
- }
- /**
- * Retrieves fields/tables/nodes that were excluded by default from a variable and adds them
- *
- * This function exists to allow tripal_core_generate_chado_var() to excldue some
- * fields/tables/nodes from the default form of a variable without making it extremely difficult for
- * the tripal admin to get at these variables if he/she wants them.
- *
- * @param $object
- * This must be an object generated using tripal_core_generate_chado_var()
- * @param $type
- * Must be one of 'field', 'table', 'node'. Indicates what is being expanded.
- * @param $to_expand
- * The name of the field/table/node to be expanded
- * @param $table_options
- * - order_by:
- * An array containing options for the base table. For example, an
- * option of 'order_by' may be used to sort results in the base table
- * if more than one are returned. The options must be compatible with
- * the options accepted by the tripal_core_chado_select() function.
- * - return_array:
- * Additionally, The option 'return_array' can be provided to force
- * the function to expand tables as an array. Default behavior is to expand
- * a table as single record if only one record exists or to expand as an array if
- * multiple records exist.
- * - include_fk:
- * an array of FK relationships to follow. By default, the
- * tripal_core_expand_chado_vars function will follow all FK relationships but this
- * may generate more queries then is desired slowing down this function call when
- * there are lots of FK relationships to follow. Provide an array specifying the
- * fields to include. For example, if expanding a property table (e.g. featureprop)
- * and you want the CV and accession but do not want the DB the following
- * array would work:
- * $table_options = array(
- * 'include_fk' => array(
- * 'type_id' => array(
- * 'cv_id' => 1,
- * 'dbxref_id' => 1,
- * )
- * )
- * );
- *
- * The above array will expand the 'type_id' of the property table but only
- * further expand the cv_id and the dbxref_id and will go no further.
- * - pager:
- * Use this option if it is desired to return only a subset of results
- * so that they may be shown within a Drupal-style pager. This should be
- * an array with two keys: 'limit' and 'element'. The value of 'limit'
- * should specify the number of records to return and 'element' is a
- * unique integer to differentiate between pagers when more than one
- * appear on a page. The 'element' should start with zero and increment by
- * one for each pager. This only works when type is a 'table'.
- * @return
- * A chado object supplemented with the field/table/node requested to be expanded.
- * If the type is a table and it has already been expanded no changes is made to the
- * returned object
- *
- * Example Usage:
- * @code
- // Get a chado object to be expanded
- $values = array(
- 'name' => 'Medtr4g030710'
- );
- $features = tripal_core_generate_chado_var('feature', $values);
- // Expand the organism node
- $feature = tripal_core_expand_chado_vars($feature, 'node', 'organism');
- // Expand the feature.residues field
- $feature = tripal_core_expand_chado_vars($feature, 'field', 'feature.residues');
- // Expand the feature properties (featureprop table)
- $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureprop');
- * @endcode
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_expand_chado_vars($object, $type, $to_expand, $table_options = array()) {
- // make sure we have a value
- if (!$object) {
- watchdog('tripal_core', 'Cannot pass non array as argument, $object, to tripal_core_expand_chado_vars function.', array(), WATCHDOG_ERROR);
- return $object;
- }
- // check to see if we are expanding an array of objects
- if (is_array($object)) {
- foreach ($object as $index => $o) {
- $object[$index] = tripal_core_expand_chado_vars($o, $type, $to_expand);
- }
- return $object;
- }
- // get the base table name
- $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) {
- if(property_exists($object, $key)) {
- $values[$key] = $object->{$key};
- }
- }
- if ($base_table == $tablename) {
- //get the field
- $results = tripal_core_chado_select($tablename, array($fieldname), $values);
- $object->{$fieldname} = $results[0]->{$fieldname};
- $object->expanded = $to_expand;
- }
- else {
- //We need to recurse -the field is in a nested object
- 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
- );
- }
- } //end of for each field in the current object
- }
- }
- 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;
- // don't expand the table it already is expanded
- if (array_key_exists($foreign_table, $object)) {
- return $object;
- }
- $foreign_table_desc = tripal_core_get_chado_table_schema($foreign_table);
- // If it's connected to the base table via a FK constraint
- if (array_key_exists($base_table, $foreign_table_desc['foreign keys'])) {
- foreach ($foreign_table_desc['foreign keys'][$base_table]['columns'] as $left => $right) {
- // if the FK value in the base table is not there then we can't expand it, so just skip it.
- if (!$object->{$right}) {
- continue;
- }
- // generate a new object for this table using the FK values in the base table.
- $new_options = $table_options;
- $foreign_object = tripal_core_generate_chado_var($foreign_table, array($left => $object->{$right}), $new_options);
- // if the generation of the object was successful, update the base object to include it.
- if ($foreign_object) {
- // in the case where the foreign key relationship exists more
- // than once with the same table we want to alter the array structure to
- // include the field name.
- if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
- if (!property_exists($object, $foreign_table)) {
- $object->{$foreign_table} = new stdClass();
- }
- $object->{$foreign_table}->{$left} = $foreign_object;
- $object->expanded = $to_expand;
- }
- else {
- if (!property_exists($object, $foreign_table)) {
- $object->{$foreign_table} = new stdClass();
- }
- $object->{$foreign_table} = $foreign_object;
- $object->expanded = $to_expand;
- }
- }
- // if the object returned is NULL then handle that
- else {
- // in the case where the foreign key relationship exists more
- // than once with the same table we want to alter the array structure to
- // include the field name.
- if (count($foreign_table_desc['foreign keys'][$base_table]['columns']) > 1) {
- if (!property_exists($object, $foreign_table)) {
- $object->{$foreign_table} = new stdClass();
- }
- $object->{$foreign_table}->{$left} = NULL;
- }
- else {
- $object->{$foreign_table} = NULL;
- }
- }
- }
- }
- // if the foreign table is not connected to the base table through a FK constraint
- else {
- // We need to recurse -the table has a relationship to one of the nested objects
- $did_expansion = 0;
- foreach ((array) $object as $field_name => $field_value) {
- // if we have a nested object ->expand the table in it
- // check to see if the $field_name is a valid chado table, we don't need
- // to call tripal_core_expand_chado_vars on fields that aren't tables
- $check = tripal_core_get_chado_table_schema($field_name);
- if ($check) {
- $did_expansion = 1;
- $object->{$field_name} = tripal_core_expand_chado_vars($field_value, 'table', $foreign_table);
- }
- }
- // if we did not expand this table we should return a message that the foreign table
- // could not be expanded
- 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 the node to be expanded is for our base table, then just expand it
- 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);
- } //end of if node
- }
- else {
- //We need to recurse -the node to expand is one of the nested objects
- 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
- );
- }
- } //end of for each field in the current object
- }
- 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;
- }
- // move extended array downwards
- if (!property_exists($object, 'expanded')) {
- //if there's no extended field then go hunting for it
- 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);
- }
- }
- }
- }
- //try again becasue now we might have moved it down
- if (property_exists($object, 'expanded')) {
- $expandable_name = 'expandable_' . $type . 's';
- if (property_exists($object, $expandable_name) and $object->{$expandable_name}) {
- $key_to_remove = array_search($object->expanded, $object->{$expandable_name});
- unset($object->{$expandable_name}[$key_to_remove]);
- unset($object->expanded);
- }
- else {
- // if there is an expandable array then we've reached the base object
- // if we get here and don't have anything expanded then something went wrong
- // watchdog(
- // 'tripal_core',
- // 'tripal_core_expand_chado_vars: Unable to expand the %type %to_expand',
- // array('%type'=>$type, '%to_expand'=>$to_expand),
- // WATCHDOG_ERROR
- // );
- } //end of it we've reached the base object
- }
- return $object;
- }
- /**
- * Implements hook_exclude_type_by_default()
- *
- * This hooks allows fields of a specified type that match a specified criteria to be excluded by
- * default from any table when tripal_core_generate_chado_var() is called. Keep in mind that if
- * fields are excluded by default they can always be expanded at a later date using
- * tripal_core_expand_chado_vars().
- *
- * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
- * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
- * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
- * contain the following tokens:
- * - >field_name<
- * Replaced by the name of the field to be excluded
- * - >field_value<
- * Replaced by the value of the field in the current record
- * Also keep in mind that if your criteria doesn't contain the >field_value< token then it will be
- * evaluated before the query is executed and if the field is excluded it won't be included in the
- * query.
- *
- * @return
- * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_exclude_type_by_default() {
- return array('text' => 'strlen(">field_value< ") > 100');
- }
- /**
- * Implements hook_exclude_field_from_<tablename>_by_default()
- *
- * This hooks allows fields from a specified table that match a specified criteria to be excluded by
- * default from any table when tripal_core_generate_chado_var() is called. Keep in mind that if
- * fields are excluded by default they can always be expanded at a later date using
- * tripal_core_expand_chado_vars().
- *
- * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
- * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
- * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
- * contain the following tokens:
- * - >field_name<
- * Replaced by the name of the field to be excluded
- * - >field_value<
- * Replaced by the value of the field in the current record
- * Also keep in mind that if your criteria doesn't contain the >field_value< token then it will be
- * evaluated before the query is executed and if the field is excluded it won't be included in the
- * query.
- *
- * @return
- * An array of type => criteria where the type is excluded if the criteria evaluates to TRUE
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_exclude_field_from_feature_by_default() {
- return array();
- }
- /**
- * Use this function instead of pager_query() when selecting a
- * subset of records from a Chado table.
- *
- * @param $query
- * The SQL statement to execute, this is followed by a variable number of args
- * used as substitution values in the SQL statement.
- * @param $args
- * The array of arguments for the query. They keys are the placeholders
- * @param $limit
- * The number of query results to display per page.
- * @param $element
- * An optional integer to distinguish between multiple pagers on one page.
- * @param $count_query
- * An SQL query used to count matching records.
- *
- * @returns
- * A database query result resource or FALSE if the query was not
- * executed correctly
- *
- * @ingroup tripal_chado_api
- */
- function chado_pager_query($query, $args, $limit, $element, $count_query = '') {
- // get the page and offset for the pager
- $page = isset($_GET['page']) ? $_GET['page'] : '0';
- $offset = $limit * $page;
- // Construct a count query if none was given.
- if (!isset($count_query)) {
- $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'),
- array('SELECT COUNT(*) FROM ', ''), $query);
- }
- // We calculate the total of pages as ceil(items / limit).
- $results = chado_query($count_query, $args);
- if (!$results) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR,
- "chado_pager_query(): Query failed: %cq", array('%cq' => $count_query));
- return;
- }
- $total_records = $results->fetchField();
- // set a session variable for storing the total number of records
- $_SESSION['chado_pager'][$element]['total_records'] = $total_records;
- pager_default_initialize($total_records, $limit, $element);
- $query .= ' LIMIT ' . (int) $limit . ' OFFSET ' . (int) $offset;
- $results = chado_query($query, $args);
- return $results;
- }
- /**
- * Use this function instead of db_query() to avoid switching databases
- * when making query to the chado database
- *
- * Will use a chado persistent connection if it already exists
- *
- * @param $sql
- * The sql statement to execute
- *
- * @param $args
- * The array of arguments, with the same structure as passed to
- * the db_query() function of Drupal.
- *
- * @return
- * DatabaseStatementInterface A prepared statement object, already executed.
- *
- * @ingroup tripal_chado_api
- */
- function chado_query($sql, $args = array()) {
- $is_local = $GLOBALS["chado_is_local"];
- // Args should be an array
- if (!is_array($args)) {
- tripal_core_report_error('tripal_core', TRIPAL_ERROR,
- 'chado_query; Need to pass an array to chado_query, "%value" passed instead. Query: %query',
- array('%value' => $args, '%query' => $sql)
- );
- $args = array($args);
- return FALSE;
- }
- // if Chado is local to the database then prefix the Chado table
- // names with 'chado'.
- if ($is_local) {
- $sql = preg_replace('/\n/', '', $sql); // remove carriage returns
- $sql = preg_replace('/\{(.*?)\}/', 'chado.$1', $sql);
- // the featureloc table has some indexes that use function that call other functions
- // and those calls do not reference a schema, therefore, any tables with featureloc
- // must automaticaly have the chado schema set as active to find
- if(preg_match('/chado.featureloc/i', $sql)) {
- $previous_db = tripal_db_set_active('chado') ;
- $results = db_query($sql, $args);
- tripal_db_set_active($previous_db);
- }
- // for all other tables we should have everything in scope so just run the query
- else {
- $results = db_query($sql, $args);
- }
- }
- // if Chado is not local to the Drupal database then we have to
- // switch to another database
- else {
- $previous_db = tripal_db_set_active('chado') ;
- $results = db_query($sql, $args);
- tripal_db_set_active($previous_db);
- }
- return $results;
- }
- /**
- * Get chado id for a node. E.g, if you want to get 'analysis_id' from the
- * 'analysis' table for a synced 'chado_analysis' node, (the same for
- * organisms and features):
- * $analysis_id = chado_get_id_for_node ('analysis', $node->nid)
- * $organism_id = chado_get_id_for_node ('organism', $node->nid)
- * $feature_id = chado_get_id_for_node ('feature', $node->nid)
- *
- * @param $table
- * @param $nid
- *
- * @ingroup tripal_chado_api
- */
- function chado_get_id_for_node($table, $nid) {
- $sql = "SELECT " . $table . "_id as id FROM {chado_$table} WHERE nid = :nid";
- return db_query($sql, array(':nid' => $nid))->fetchField();
- }
- /**
- * Get node id for a chado feature/organism/analysis. E.g, if you want to
- * get the node id for an analysis, use:
- * $nid = chado_get_node_id ('analysis', $analysis_id)
- * Likewise,
- * $nid = chado_get_node_id ('organism', $organism_id)
- * $nid = chado_get_node_id ('feature', $feature_id)
- *
- * @ingroup tripal_chado_api
- */
- function chado_get_node_id($table, $id) {
- $sql = "SELECT nid FROM {chado_$table} WHERE " . $table . "_id = :" . $table . "_id";
- return db_query($sql, array(":" . $table . "_id" => $id))->fetchField();
- }
- /**
- * Retrieves the schema in an array for the specified custom table.
- *
- * @param $table
- * The name of the table to create.
- *
- * @return
- * A Drupal-style Schema API array definition of the table. Returns
- * FALSE on failure.
- *
- * @ingroup tripal_core_api
- */
- 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);
- }
- }
- /**
- * Check that any given Chado table exists. This function
- * is necessary because Drupa's db_table_exists function
- * hardcodes the 'public'
- *
- * @return
- * TRUE/FALSE depending upon whether it exists
- */
- 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;
- }
- /**
- * Check that any given schema exists
- *
- * @param $schema
- * The name of the schema to check the existence of
- *
- * @return
- * TRUE/FALSE depending upon whether or not the schema exists
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_schema_exists($schema) {
- // check that the chado schema now exists
- $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;
- }
- /**
- * Retrieves the list tables in the Chado schema. By default it only retursn
- * the default Chado tables, but may also return custom tables added to the
- * Chado schema as well.
- *
- * @param $include_custom
- * Optional. Set as TRUE to include any custom tables created in the
- * Chado schema. Custom tables are added to Chado using the
- * tripal_core_chado_create_table() function.
- *
- * @returns
- * An associative array where the key and value pairs are the Chado table names.
- *
- * @ingroup tripal_core_api
- */
- function tripal_core_get_chado_tables($include_custom = NULL) {
- // first get the chado version that is installed
- $v = $GLOBALS["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;
- }
- }
- // now add in the custom tables too if requested
- 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;
- }
- /**
- * Returns the version number of the currently installed Chado instance.
- * It can return the real or effective version. Note, this function
- * is executed in the hook_init() of the tripal_core module which then
- * sets the $GLOBAL['exact_chado_version'] and $GLOBAL['chado_version']
- * variable. You can access these variables rather than calling this function.
- *
- * @param $exact
- * Set this argument to 1 to retrieve the exact version that is installed.
- * Otherwise, this function will set the version to the nearest 'tenth'.
- * Chado versioning numbers in the hundreds represent changes to the
- * software and not the schema. Changes in the tenth's represent changes
- * in the schema.
- *
- * @param $warn_if_unsupported
- * If the currently installed version of Chado is not supported by Tripal
- * this generates a Drupal warning.
- *
- * @returns
- * The version of Chado
- *
- * @ingroup tripal_core_api
- */
- function tripal_core_get_chado_version($exact = FALSE, $warn_if_unsupported = FALSE) {
- global $databases;
- $version = '';
- $is_local = 0;
- // check that Chado is installed if not return 'uninstalled as the version'
- $chado_exists = tripal_core_chado_schema_exists();
- if (!$chado_exists) {
- // if it's not in the drupal database check to see if it's specified in the $db_url
- // in the settings.php
- if (!array_key_exists('chado', $databases)) {
- // if it's not in the drupal database or specified in the $db_url then
- // return uninstalled as the version
- return 'not installed';
- }
- $is_local = 0;
- $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 the table doesn't exist then we don't know what version but we know
- // it must be 1.11 or older.
- if (!$prop_exists) {
- $version = "1.11 or older";
- }
- else {
- $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 we don't have a version in the chadoprop table then it must be
- // v1.11 or older
- if (!$v) {
- $version = "1.11 or older";
- }
- $version = $v->value;
- }
- // next get the exact Chado version that is installed
- $exact_version = $version;
- // Tripal only supports v1.11 or newer.. really this is the same as v1.1
- // but at the time the v1.11 schema API was written we didn't know that so
- // we'll return the version 1.11 so the schema API will work.
- 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');
- }
- }
- // if not returing an exact version, return the version to the nearest 10th.
- // return 1.2 for all versions of 1.2x
- $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 the callee has requested the exact version then return it
- if ($exact) {
- return $exact_version;
- }
- return $effective_version;
- }
- /**
- * Retrieves the chado tables Schema API array.
- *
- * @param $table
- * The name of the table to retrieve. The function will use the appopriate
- * Tripal chado schema API hooks (e.g. v1.11 or v1.2).
- *
- * @returns
- * A Drupal Schema API array defining the table.
- *
- * @ingroup tripal_core_api
- */
- function tripal_core_get_chado_table_schema($table) {
- // first get the chado version that is installed
- $v = $GLOBALS["chado_version"];
- // get the table array from the proper chado schema
- $v = preg_replace("/\./", "_", $v); // reformat version for hook name
- $table_arr = module_invoke_all("chado_schema_v" . $v . "_" . $table);
- // if the table_arr is empty then maybe this is a custom table
- if (!is_array($table_arr) or count($table_arr) == 0) {
- $table_arr = tripal_get_chado_custom_schema($table);
- }
- return $table_arr;
- }
- /**
- * Check that the Chado schema exists within the local database
- *
- * @return
- * TRUE/FALSE depending upon whether it exists
- */
- function tripal_core_chado_schema_exists() {
- // This is postgresql-specific code to check the existence of the chado schema
- // @coder-ignore: acting on pg_catalog schema rather then drupal schema therefore, table prefixing does not apply
- $sql = "
- SELECT nspname
- FROM pg_namespace
- WHERE
- has_schema_privilege(nspname, 'USAGE') AND
- nspname = 'chado'
- ";
- $results = db_query($sql);
- $name = $results->fetchObject();
- if ($name) {
- variable_set('chado_schema_exists', FALSE);
- return TRUE;
- }
- else {
- variable_set('chado_schema_exists', TRUE);
- return FALSE;
- }
- }
- /**
- * Check whether chado is installed (either in the same or a different database)
- *
- * @return
- * TRUE/FALSE depending upon whether chado is installed.
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_is_chado_installed() {
- global $databases;
- // first check if chado is in the $databases variable of the settings.php file
- if (array_key_exists('chado', $databases)) {
- return TRUE;
- }
- // check to make sure the chado schema exists
- return tripal_core_chado_schema_exists();
- }
- /**
- * Check whether chado is installed local to the Drupal database
- * in its own Chado schema.
- *
- * @return
- * TRUE/FALSE depending upon whether chado is local.
- *
- * @ingroup tripal_chado_api
- */
- function tripal_core_is_chado_local() {
- global $databases, $db_type;
- // first check if chado is in the $databases variable of the settings.php file
- if (array_key_exists('chado', $databases)) {
- return FALSE;
- }
- // check to make sure the chado schema exists
- return tripal_core_chado_schema_exists();
- }
- /**
- * Determine whether a given chado table is directly linked to a node
- *
- * @param $chado_table
- * The name of a chado table to check (ie: feature)
- * @return
- * TRUE if it is linked to a node and FALSE otherwise
- */
- 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;
- }
- }
- /**
- * Set the Tripal Database
- *
- * The tripal_db_set_active function is used to prevent namespace collisions
- * when chado and drupal are installed in the same database but in different
- * schemas. It is also used for backwards compatibility with older versions
- * of tripal or in cases where chado is located outside of the Drupal database.
- * or when using Drupal functions such as db_table_exists()
- *
- * @ingroup tripal_chado_api
- */
- function tripal_db_set_active($dbname = 'default') {
- global $databases, $active_db;
- if ($dbname ) {
- if ($dbname == 'chado') {
- db_query('set search_path to chado,public');
- return 'default';
- }
- else {
- db_query('set search_path to public');
- return 'chado';
- }
- }
- // if the 'chado' database is in the $db_url variable then chado is
- // not in the same Drupal database, so we don't need to set any
- // search_path and can just change the database
- elseif (array_key_exists($dbname, $databases)) {
- return db_set_active($dbname);
- }
- }
- /**
- * Get max rank for a given set of criteria
- * This function was developed with the many property tables in chado in mind but will
- * work for any table with a rank
- *
- * @params tablename: the name of the chado table you want to select the max rank from
- * this table must contain a rank column of type integer
- * @params where_options: array(
- * <column_name> => array(
- * 'type' => <type of column: INT/STRING>,
- * 'value' => <the value you want to filter on>,
- * 'exact' => <if TRUE use =; if FALSE use ~>,
- * )
- * )
- * where options should include the id and type for that table to correctly
- * group a set of records together where the only difference are the value and rank
- * @return the maximum rank
- *
- */
- function tripal_core_get_max_chado_rank($tablename, $where_options) {
- $where_clauses = array();
- $where_args = array();
- //generate the where clause from supplied options
- // the key is the column name
- $i = 0;
- $sql = "
- SELECT max(rank) as max_rank, count(rank) as count
- FROM {".$tablename."}
- WHERE
- ";
- foreach ($where_options as $key => $value) {
- $where_clauses[] = "$key = :$key";
- $where_args[":$key"] = $value;
- }
- $sql .= implode($where_clauses, ' AND ');
- $result = chado_query($sql, $where_args)->fetchObject();
- if ($result->count > 0) {
- return $result->max_rank;
- }
- else {
- return -1;
- }
- }
|