12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012 |
- <?php
- function chado_get_table_max_rank($tablename, $where_options) {
- $where_clauses = array();
- $where_args = array();
-
-
- $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;
- }
- }
- function hook_chado_connection_alter(&$settings) {
-
-
-
-
- if ('chado_only' == $settings['dbname']) {
- $settings['new_active_db'] = 'chado';
-
- $settings['new_search_path'] = 'chado';
- }
- }
- function chado_set_active($dbname = 'default') {
-
- if (!array_key_exists('chado_active_db', $GLOBALS)) {
- $GLOBALS['chado_active_db'] = 'default';
- }
- $previous_db = $active_db = $GLOBALS['chado_active_db'];
- $search_path = tripal_get_schema_name('drupal');
-
-
-
-
- if ($dbname == 'chado') {
- $active_db = 'chado';
- $search_path = tripal_get_schema_name('chado') . ',' . tripal_get_schema_name('drupal');
- }
- else {
- $active_db = $dbname;
- }
- $settings = array(
- 'dbname' => $dbname,
- 'new_active_db' => &$active_db,
- 'new_search_path' => &$search_path,
- );
-
-
- drupal_alter('chado_connection', $settings);
-
- $GLOBALS['chado_active_db'] = $active_db;
-
- db_query('SET search_path TO ' . $search_path);
- return $previous_db;
- }
- function chado_insert_record($table, $values, $options = array()) {
- $print_errors = (isset($options['print_errors'])) ? $options['print_errors'] : FALSE;
- if (!is_array($values)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass non array as values for inserting.', array(),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- if (count($values)==0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass an empty array as values for inserting.',
- array(),array('print' => $print_errors)
- );
- return FALSE;
- }
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('skip_validation', $options)) {
- $options['skip_validation'] = FALSE;
- }
- if (!array_key_exists('return_record', $options)) {
- $options['return_record'] = TRUE;
- }
- $insert_values = array();
- if (array_key_exists('skip_validation', $options)) {
- $validate = !$options['skip_validation'];
- }
- else {
- $validate = TRUE;
- }
-
- $table_desc = chado_get_schema($table);
- if (!$table_desc) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING,
- 'chado_insert_record; There is no table description for !table_name',
- array('!table_name' => $table), array('print' => $print_errors)
- );
- return;
- }
-
-
-
- foreach ($values as $field => $value) {
-
-
- if (!array_key_exists($field, $table_desc['fields'])) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- "chado_insert_record; 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)) {
-
- $results = chado_schema_get_foreign_key($table_desc, $field, $value);
- if (sizeof($results) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_insert_record: 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)
- );
- return FALSE;
- }
- elseif (sizeof($results) < 1) {
- tripal_report_error('tripal_chado', TRIPAL_DEBUG,
- 'chado_insert_record: 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)
- );
- return FALSE;
- }
- else {
- $insert_values[$field] = $results[0];
- }
- }
- else {
- $insert_values[$field] = $value;
- }
- }
- if ($validate) {
-
- $ukeys = array();
- if (array_key_exists('unique keys', $table_desc)) {
- $ukeys = $table_desc['unique keys'];
- }
- $ukselect_cols = array();
- $ukselect_vals = array();
- if ($ukeys) {
- foreach ($ukeys as $name => $fields) {
- foreach ($fields as $index => $field) {
-
- $ukselect_cols[] = $field;
- if (!array_key_exists($field, $insert_values)) {
- if (array_key_exists('default', $table_desc['fields'][$field])) {
- $ukselect_vals[$field] = $table_desc['fields'][$field]['default'];
- }
- }
- else {
- $ukselect_vals[$field] = $insert_values[$field];
- }
- }
-
- if (chado_select_record($table, $ukselect_cols, $ukselect_vals)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- "chado_insert_record; Cannot insert duplicate record into $table table: !values",
- array('!values' => print_r($values, TRUE)), array('print' => $print_errors)
- );
- return FALSE;
- }
- }
- }
-
-
- if (array_key_exists('primary key', $table_desc)) {
- $pkey = $table_desc['primary key'][0];
- if (array_key_exists($pkey, $insert_values)) {
- $coptions = array();
- if (chado_select_record($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_insert_record; Cannot insert duplicate primary key into !table table: !values',
- array('!table' => $table, '!values' => print_r($values, TRUE)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- }
- }
-
- if (!is_array($table_desc['fields'])) {
- $table_desc['fields'] = array();
- tripal_report_error('tripal_chado', TRIPAL_WARNING,
- "chado_insert_record; %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) {
-
-
- 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_report_error('tripal_chado', TRIPAL_ERROR,
- "chado_insert_record; Field %table.%field cannot be NULL: %values",
- array('%table' => $table, '%field' => $field, '%values' => print_r($values, 1)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- }
- }
-
-
- $ifields = array();
- $itypes = array();
- $ivalues = array();
- foreach ($insert_values as $field => $value) {
- $ifields[] = $field;
- if (strcmp($value, '__NULL__')==0) {
- $itypes[] = "NULL";
- }
- else {
- $itypes[] = ":$field";
- $ivalues[":$field"] = $value;
- }
- }
-
- $sql = 'INSERT INTO {' . $table . '} (' . implode(", ", $ifields) . ") VALUES (" . implode(", ", $itypes) . ")";
- $result = chado_query($sql, $ivalues);
-
- if ($options['return_record'] == TRUE and $result) {
- if (array_key_exists('primary key', $table_desc) and is_array($table_desc['primary key'])) {
- foreach ($table_desc['primary key'] as $field) {
- $sql = "SELECT CURRVAL('{" . $table . "_" . $field . "_seq}')";
- $results = chado_query($sql);
- $value = $results->fetchField();
- if (!$value) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- "chado_insert_record; 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_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_insert_record; Cannot insert record into "%table": %values',
- array('%table' => $table, '%values' => print_r($values, 1)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
- return FALSE;
- }
- function chado_update_record($table, $match, $values, $options = NULL) {
- $print_errors = (isset($options['print_errors'])) ? $options['print_errors'] : FALSE;
- if (!is_array($values)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass non array as values for updating.',
- array(), array('print' => $print_errors)
- );
- return FALSE;
- }
- if (count($values)==0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass an empty array as values for updating.',
- array(), array('print' => $print_errors)
- );
- return FALSE;
- }
- if (!is_array($match)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass non array as values for matching.',
- array(), array('print' => $print_errors)
- );
- return FALSE;
- }
- if (count($match)==0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass an empty array as values for matching.',
- array(), array('print' => $print_errors)
- );
- return FALSE;
- }
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('return_record', $options)) {
- $options['return_record'] = FALSE;
- }
- $update_values = array();
- $update_matches = array();
-
- $table_desc = chado_get_schema($table);
- if (!$table_desc) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'The table name, %table, does not exist.',
- array('%table', $table), array('print' => $print_errors)
- );
- return FALSE;
- }
-
-
-
- $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 = chado_select_record($table, $columns, $match, $options2);
- if (count($results) > 0) {
- foreach ($results as $index => $pkey) {
- $pkeys[] = $pkey;
- }
- }
- }
- }
-
- foreach ($match as $field => $value) {
- if (is_array($value)) {
- $results = chado_schema_get_foreign_key($table_desc, $field, $value);
- if (sizeof($results) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_update_record: 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)
- );
- return FALSE;
- }
- elseif (sizeof($results) < 1) {
- tripal_report_error('tripal_chado', TRIPAL_DEBUG,
- 'chado_update_record: 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)
- );
- return FALSE;
- }
- else {
- $update_matches[$field] = $results[0];
- }
- }
- else {
- $update_matches[$field] = $value;
- }
- }
-
- foreach ($values as $field => &$value) {
- if (is_array($value)) {
- $foreign_options = array();
-
- $results = chado_schema_get_foreign_key($table_desc, $field, $value, $foreign_options);
- if (sizeof($results) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_update_record: 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)
- );
- return FALSE;
- }
- elseif (sizeof($results) < 1) {
- tripal_report_error('tripal_chado', TRIPAL_DEBUG,
- 'chado_update_record: 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)
- );
- return FALSE;
- }
- else {
- $update_values[$field] = $results[0];
- }
- }
- else {
- $update_values[$field] = $value;
- }
- }
-
- $sql = 'UPDATE {' . $table . '} SET ';
- $args = array();
- foreach ($update_values as $field => $value) {
- if (strcmp($value, '__NULL__') == 0) {
- $sql .= " $field = NULL, ";
- }
- else {
- $sql .= " $field = :$field, ";
- $args[":$field"] = $value;
- }
- }
- $sql = drupal_substr($sql, 0, -2);
- $sql .= " WHERE ";
- foreach ($update_matches as $field => $value) {
- if (strcmp($value, '__NULL__')==0) {
- $sql .= " $field = NULL AND ";
- }
- else {
- $sql .= " $field = :$field AND ";
- $args[":$field"] = $value;
- }
- }
- $sql = drupal_substr($sql, 0, -4);
- $result = chado_query($sql, $args);
-
- if ($options['return_record'] == TRUE and $result) {
-
-
- if (count($pkeys) == 1) {
- foreach ($pkeys as $index => $pkey) {
- foreach ($pkey as $field => $fvalue) {
- $values[$field] = $fvalue;
- }
- }
- }
- return $values;
- }
- elseif ($options['return_record'] == FALSE and $result) {
- return TRUE;
- }
- else {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- "chado_update_record: 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;
- }
- function chado_delete_record($table, $match, $options = NULL) {
- $print_errors = (isset($options['print_errors'])) ? $options['print_errors'] : FALSE;
- if (!is_array($match)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass non array as values for matching.', array());
- return FALSE;
- }
- if (count($match)==0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'Cannot pass an empty array as values for matching.', array());
- return FALSE;
- }
-
-
- if (!is_array($options)) {
- $options = array();
- }
- $delete_matches = array();
-
- $table_desc = chado_get_schema($table);
- $fields = $table_desc['fields'];
- if (empty($table_desc)) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING,
- 'chado_delete_record; There is no table description for !table_name',
- array('!table_name' => $table), array('print' => $print_errors)
- );
- }
-
- foreach ($match as $field => $value) {
- if (is_array($value)) {
-
-
- if (array_values($value) === $value) {
- $delete_matches[$field] = $value;
- }
- else {
- $results = chado_schema_get_foreign_key($table_desc, $field, $value);
- if (sizeof($results) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_delete_record: 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)));
- return FALSE;
- }
- elseif (sizeof($results) < 1) {
-
- }
- else {
- $delete_matches[$field] = $results[0];
- }
- }
- }
- else {
- $delete_matches[$field] = $value;
- }
- }
-
- $sql = 'DELETE FROM {' . $table . '} WHERE ';
- $args = array();
- foreach ($delete_matches as $field => $value) {
-
- if (count($value) > 1) {
- $sql .= "$field IN (";
- $index = 0;
- foreach ($value as $v) {
- $sql .= ":$field" . $index . ", ";
- $args[":$field" . $index] = $v;
- $index++;
- }
- $sql = drupal_substr($sql, 0, -2);
- $sql .= ") AND ";
- }
- else {
- if (strcmp($value, '__NULL__') == 0) {
- $sql .= " $field = NULL AND ";
- }
- else {
- $sql .= " $field = :$field AND ";
- $args[":$field"] = $value;
- }
- }
- }
- $sql = drupal_substr($sql, 0, -4);
-
- $result = chado_query($sql, $args);
- if ($result) {
- return TRUE;
- }
- else {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- "Cannot delete record in $table table. Match:" . print_r($match, 1) . ". Values: " . print_r($values, 1), array());
- return FALSE;
- }
- return FALSE;
- }
- function chado_select_record($table, $columns, $values, $options = NULL) {
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('case_insensitive_columns', $options)) {
- $options['case_insensitive_columns'] = array();
- }
- if (!array_key_exists('regex_columns', $options)) {
- $options['regex_columns'] = array();
- }
- 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'];
- }
- $print_errors = FALSE;
- if (isset($options['print_errors'])) {
- $print_errors = $options['print_errors'];
- }
-
- if (!is_array($columns)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_select_record; 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_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_select_record; the $values argument must be an array. Values:%values',
- array('%values' => print_r($values, TRUE)),
- array('print' => $print_errors)
- );
- return FALSE;
- }
-
- $table_desc = chado_get_schema($table);
- if (!is_array($table_desc)) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING,
- 'chado_insert_record; There is no table description for !table_name',
- array('!table_name' => $table), array('print' => $print_errors)
- );
- return FALSE;
- }
- $where = array();
- $args = array();
- if ($options['is_duplicate'] and array_key_exists('unique keys', $table_desc)) {
- $ukeys = $table_desc['unique keys'];
- $has_results = 0;
-
-
- foreach ($ukeys as $cname => $fields) {
- if ($has_results) {
- continue;
- }
- $new_values = array();
- $new_columns = array();
- $new_options = array();
- $has_pkey = 0;
-
- if (array_key_exists('primary key', $table_desc)) {
- $has_pkey = 1;
- $pkeys = $table_desc['primary key'];
- foreach ($pkeys as $index => $key) {
- array_push($new_columns, $key);
- }
- }
-
- foreach ($fields as $field) {
- if (array_key_exists($field, $values)) {
- $new_values[$field] = $values[$field];
-
- if (!$has_pkey) {
- array_push($new_columns, $field);
- }
- }
-
-
- elseif (array_key_exists('default', $table_desc['fields'][$field])) {
- $new_values[$field] = $table_desc['fields'][$field]['default'];
- if (!$has_pkey) {
- array_push($new_columns, $field);
- }
- }
-
-
- elseif (!$table_desc['fields'][$field]['not null']) {
- $new_values[$field] = NULL;
- if (!$has_pkey) {
- array_push($new_columns, $field);
- }
- }
-
-
-
- else {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_select_record: There is no value for %field thus we cannot ' .
- 'check if this record for table, %table, is unique. %values',
- array('%field' => $field, '%table' => $table, '%values' => print_r($values, TRUE)),
- array('print' => $print_errors));
- return FALSE;
- }
- }
- $results = chado_select_record($table, $new_columns, $new_values, $new_options);
-
- if (count($results) > 0) {
- $has_results = 1;
- }
- unset($new_columns);
- unset($new_values);
- unset($new_options);
- }
- if ($options['has_record'] and $has_results) {
- return TRUE;
- }
- else {
- return $results;
- }
- }
-
-
-
-
- foreach ($values as $field => $value) {
-
- if (!array_key_exists($field, $table_desc['fields'])) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_select_record: 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();
- }
-
- if (is_array($value)) {
-
-
- if (count($value) == 1 AND is_int(key($value))) {
- $value = array_pop($value);
- $op = '=';
- chado_select_record_check_value_type($op, $value, $table_desc['fields'][$field]['type']);
- $where[] = array(
- 'field' => $field,
- 'op' => $op,
- 'data' => $value
- );
- }
-
-
- elseif (isset($value['data']) AND isset($value['op'])) {
- $value['field'] = $field;
- $where[] = $value;
- }
-
-
- elseif (is_int(key($value)) AND !is_array(current($value))) {
- $where[] = array(
- 'field' => $field,
- 'op' => 'IN',
- 'data' => $value
- );
- }
-
- else {
-
-
-
- if (isset($value[0]['data']) AND isset($value[0]['op'])) {
- foreach ($value as $subvalue) {
- $subvalue['field'] = $field;
- $where[] = $subvalue;
- }
- }
-
-
- else {
-
- $foreign_options = array(
- 'regex_columns' => $options['regex_columns'],
- );
- $results = chado_schema_get_foreign_key($table_desc, $field, $value, $foreign_options);
-
- if ($results === FALSE OR $results === NULL) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_select_record: could not follow the foreign key definition
- for %field where the definition supplied was %value',
- array('%field' => $field, '%value' => print_r($value, TRUE))
- );
- return array();
- }
-
- elseif (count($results)==0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_select_record: the foreign key definition for \'%field\' on table \'%table\' '.
- 'returned no results where the definition supplied was %value',
- array('%field' => $field, '%table' => $table, '%value' => print_r($value, TRUE))
- );
- return array();
- }
-
- elseif (count($results) == 1) {
- $results = array_pop($results);
- $op = '=';
- chado_select_record_check_value_type($op, $results, $table_desc['fields'][$field]['type']);
- $where[] = array(
- 'field' => $field,
- 'op' => $op,
- 'data' => $results
- );
- }
-
-
- else {
- $where[] = array(
- 'field' => $field,
- 'op' => 'IN',
- 'data' => $results
- );
- }
- }
- }
- }
-
- else {
- $op = '=';
- chado_select_record_check_value_type($op, $value, $table_desc['fields'][$field]['type']);
- $where[] = array(
- 'field' => $field,
- 'op' => $op,
- 'data' => $value
- );
- }
-
- $current_key = key($where);
- if (in_array($field, $options['regex_columns'])) {
- $where[$current_key]['op'] = '~*';
- }
- }
-
- if (empty($where)) {
-
- $sql = "SELECT " . implode(', ', $columns) . " ";
- $sql .= 'FROM {' . $table . '} ';
- }
- else {
- $sql = "SELECT " . implode(', ', $columns) . " ";
- $sql .= 'FROM {' . $table . '} ';
-
- if (!empty($values)) {
- $sql .= "WHERE ";
- }
- foreach ($where as $clause_num => $value_def) {
- switch ($value_def['op']) {
-
- case 'IN':
- $sql .= $value_def['field'] . " IN (";
- $index = 0;
- foreach ($value_def['data'] as $v) {
- $placeholder = ':' . $value_def['field'] . $clause_num .'_' . $index;
- $sql .= $placeholder . ', ';
- $args[$placeholder] = $v;
- $index++;
- }
- $sql = drupal_substr($sql, 0, -2);
- $sql .= ") AND ";
- break;
-
- case 'IS NULL':
- $sql .= $value_def['field'] . ' IS NULL AND ';
- break;
-
- default:
- $placeholder = ':'. $value_def['field'] . $clause_num;
-
- if (in_array($value_def['field'], $options['case_insensitive_columns'])) {
- $sql .= 'lower(' . $value_def['field'] .') '. $value_def['op'] .' lower('. $placeholder . ') AND ';
- }
- else {
- $sql .= $value_def['field'] .' '. $value_def['op'] .' '. $placeholder . ' AND ';
- }
- $args[$placeholder] = $value_def['data'];
- }
- }
- $sql = drupal_substr($sql, 0, -4);
- }
-
- if (count($options['order_by']) > 0) {
- $sql .= " ORDER BY ";
- foreach ($options['order_by'] as $field => $dir) {
- $sql .= "$field $dir, ";
- }
- $sql = drupal_substr($sql, 0, -2);
- }
-
- if (array_key_exists('limit', $options) and is_numeric($options['limit'])) {
- $sql .= " LIMIT " . $options['limit'];
- if (array_key_exists('offset', $options) and is_numeric($options['offset'])) {
- $sql .= " OFFSET " . $options['offset'];
- }
- }
-
- 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);
- }
-
- $results = array();
- foreach ($resource as $r) {
- $results[] = $r;
- }
- if ($options['has_record']) {
- return count($results);
- }
- return $results;
- }
- function chado_select_record_check_value_type(&$op, &$value, $type) {
- if ($value === NULL) {
- $op = 'IS NULL';
- }
- elseif ($type == 'int') {
- $value = (int) $value;
- }
- }
- function chado_query($sql, $args = array()) {
- $is_local = isset($GLOBALS["chado_is_local"]) && $GLOBALS["chado_is_local"];
-
- if (!is_array($args)) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- 'chado_query; Need to pass an array to chado_query, "%value" passed instead. Query: %query',
- array('%value' => $args, '%query' => $sql)
- );
- return FALSE;
- }
-
-
- if ($is_local) {
-
- $sql = preg_replace('/\n/', '', $sql);
-
-
-
-
- $chado_schema_name = tripal_get_schema_name('chado');
- $drupal_schema_name = tripal_get_schema_name('drupal');
- $sql = preg_replace('/\{(.*?)\}/', $chado_schema_name.'.$1', $sql);
- $sql = preg_replace('/\[(\w+)\]/', $drupal_schema_name.'.$1', $sql);
-
-
-
-
-
-
-
-
-
- drupal_alter('chado_query', $sql, $args);
-
-
-
- if (preg_match('/'.$chado_schema_name . '.featureloc/i', $sql) or preg_match('/' . $chado_schema_name . '.feature/i', $sql)) {
- $previous_db = chado_set_active('chado') ;
- try {
- $results = db_query($sql, $args);
- chado_set_active($previous_db);
- }
- catch (Exception $e) {
- chado_set_active($previous_db);
- throw $e;
- }
- }
-
- else {
- $results = db_query($sql, $args);
- }
- }
-
-
-
- else if (preg_match('/\[(\w*?)\]/', $sql)) {
- tripal_report_error('chado_query', TRIPAL_ERROR,
- 'The following query does not support external chado databases. Please file an issue with the Drupal.org Tripal Project. Query: @query',
- array('@query' => $sql)
- );
- return FALSE;
- }
-
-
- else {
- $previous_db = chado_set_active('chado');
- $results = db_query($sql, $args);
- chado_set_active($previous_db);
- }
- return $results;
- }
- function hook_chado_query_alter(&$sql, &$args) {
-
-
-
-
-
-
-
- if (preg_match('/(\w+)\.chado_feature/', $sql, $matches)) {
- $sql = str_replace(
- $matches[1] . '.chado_feature',
- 'chado_feature_view',
- $sql
- );
- }
- }
- function chado_pager_query($query, $args, $limit, $element, $count_query = '') {
-
- $page_arg = isset($_GET['page']) ? $_GET['page'] : '0';
- $pages = explode(',', $page_arg);
- $page = 0;
- if (count($pages) >= $element) {
- $page = key_exists($element, $pages) ? $pages[$element] : 0;
- }
- $offset = $limit * $page;
- $q = $_GET['q'];
-
- if (!isset($count_query)) {
- $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'),
- array('SELECT COUNT(*) FROM ', ''), $query);
- }
-
- $results = chado_query($count_query, $args);
- if (!$results) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- "chado_pager_query(): Query failed: %cq", array('%cq' => $count_query));
- return;
- }
- $total_records = $results->fetchField();
-
- $GLOBALS['chado_pager'][$q][$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;
- }
- function chado_pager_get_count($element) {
- $q = $_GET['q'];
- if (array_key_exists($q, $GLOBALS['chado_pager']) and
- array_key_exists($element, $GLOBALS['chado_pager'][$q])) {
- return $GLOBALS['chado_pager'][$q][$element]['total_records'];
- }
- else {
- return 0;
- }
- }
- function chado_schema_get_foreign_key($table_desc, $field, $values, $options = NULL) {
-
-
- if (!is_array($options)) {
- $options = array();
- }
- if (!array_key_exists('case_insensitive_columns', $options)) {
- $options['case_insensitive_columns'] = array();
- }
- if (!array_key_exists('regex_columns', $options)) {
- $options['regex_columns'] = array();
- }
-
-
- $fkeys = '';
- if (array_key_exists('foreign keys', $table_desc)) {
- $fkeys = $table_desc['foreign keys'];
- }
- if ($fkeys) {
- foreach ($fkeys as $name => $def) {
- if (is_array($def['table'])) {
-
- $message = "The foreign key " . $name . " was defined twice. Please check modules "
- . "to determine if hook_chado_schema_<version>_" . $table_desc['table'] . "() was "
- . "implemented and defined this foreign key when it wasn't supposed to. Modules "
- . "this hook was implemented in: " . implode(', ',
- module_implements("chado_" . $table_desc['table'] . "_schema")) . ".";
- tripal_report_error('tripal_chado', $message);
- drupal_set_message(check_plain($message), 'error');
- continue;
- }
- $table = $def['table'];
- $columns = $def['columns'];
-
- foreach ($columns as $left => $right) {
-
- if (strcmp($field, $left) == 0) {
-
-
- $select_cols = array($right);
- $result = chado_select_record($table, $select_cols, $values, $options);
- $fields = array();
- if ($result and count($result) > 0) {
- foreach ($result as $obj) {
- $fields[] = $obj->$right;
- }
- return $fields;
- }
- }
- }
- }
- }
- else {
-
-
-
- $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_chado_chado_v1_2_schema_feature for an example. Chado version: $version");
- tripal_report_error('tripal_chado', $message);
- drupal_set_message(check_plain($message), 'error');
- }
- return array();
- }
- function tripal_get_schema_name($schema = 'chado') {
-
- if ($schema == 'chado') {
- $schema_name = 'chado';
- }
- else {
- $schema_name = 'public';
- }
-
-
-
-
- $context = array('schema' => $schema);
- drupal_alter('tripal_get_schema_name', $schema_name, $context);
- return $schema_name;
- }
- function hook_tripal_get_schema_name_alter($schema_name, $context) {
-
-
-
- if ($context['schema'] == 'chado') {
- $schema_name = 'public';
- }
-
-
- elseif ($context['schema'] == 'drupal') {
- $schema_name = 'drupal';
- }
- }
- function chado_db_select($table, $alias = NULL, array $options = array()) {
- if (empty($options['target'])) {
- $options['target'] = 'default';
- }
- $conninfo = Database::getConnectionInfo();
- $conn = new ChadoDatabaseConnection($conninfo['default']);
- return $conn->select($table, $alias, $options);
- }
|