Tripal v1.0 (6.x-1.0)
|
00001 <?php 00030 function tripal_core_edit_custom_table($table_id, $table_name, $schema, $skip_creation = 1) { 00031 00032 // Create a new record 00033 $record = new stdClass(); 00034 $record->table_id = $table_id; 00035 $record->table_name = $table_name; 00036 $record->schema = serialize($schema); 00037 00038 // get the current custom table record 00039 $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = %d"; 00040 $custom_table = db_fetch_object(db_query($sql, $table_id)); 00041 00042 // if the user changed the table name, we want to drop the old one and force 00043 // creation of the new one. 00044 if ($custom_table->table_name != $table_name) { 00045 chado_query("DROP TABLE %s", $custom_table->table_name); 00046 $skip_creation = 0; // we want to create the table 00047 } 00048 00049 // if skip creation is not set, then drop the table from chado if it exists 00050 if(!$skip_creation){ 00051 if (db_table_exists($custom_table->table_name)) { 00052 chado_query("DROP TABLE %s", $custom_table->table_name); 00053 drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name))); 00054 } 00055 } 00056 00057 // update the custom table record and re-create the table in Chado 00058 if (drupal_write_record('tripal_custom_tables', $record, 'table_id')) { 00059 00060 // drop the table from chado if it exists 00061 if(!$skip_creation){ 00062 if (db_table_exists($custom_table->table_name)) { 00063 chado_query("DROP TABLE %s", $custom_table->table_name); 00064 drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name))); 00065 } 00066 00067 // re-create the table 00068 if (!tripal_core_create_custom_table ($ret, $table_name, $schema)) { 00069 drupal_set_message(t("Could not create the custom table. Check Drupal error report logs.")); 00070 } 00071 else { 00072 drupal_set_message(t("Custom table '%name' created", array('%name' => $table_name))); 00073 } 00074 } 00075 // TODO: add FK constraints 00076 } 00077 } 00078 00106 function tripal_core_create_custom_table(&$ret, $table, $schema, $skip_creation = 1) { 00107 $ret = array(); 00108 00109 // see if the table entry already exists in the tripal_custom_tables table. 00110 $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = '%s'"; 00111 $centry = db_fetch_object(db_query($sql, $table)); 00112 00113 // check to see if the table already exists in the chado schema 00114 $previous_db = tripal_db_set_active('chado'); // use chado database 00115 $exists = db_table_exists($table); 00116 tripal_db_set_active($previous_db); // now use drupal database 00117 00118 00119 // if the table does not exist then create it 00120 if (!$exists) { 00121 $previous_db = tripal_db_set_active('chado'); // use chado database 00122 db_create_table($ret, $table, $schema); 00123 tripal_db_set_active($previous_db); // now use drupal database 00124 if (count($ret)==0) { 00125 watchdog('tripal_core', "Error adding custom table '!table_name'.", 00126 array('!table_name' => $table), WATCHDOG_ERROR); 00127 return FALSE; 00128 } 00129 } 00130 00131 // if the table exists in Chado and in our custom table and 00132 // skip creation is turned off then drop and re-create the table 00133 if ($exists and is_object($centry) and !$skip_creation) { 00134 00135 // drop the table we'll recreate it with the new schema 00136 $previous_db = tripal_db_set_active('chado'); // use chado database 00137 db_drop_table($ret, $table); 00138 db_create_table($ret, $table, $schema); 00139 tripal_db_set_active($previous_db); // now use drupal database 00140 } 00141 00142 // add an entry in the tripal_custom_table 00143 $record = new stdClass(); 00144 $record->table_name = $table; 00145 $record->schema = serialize($schema); 00146 00147 // if an entry already exists then remove it 00148 if ($centry) { 00149 $sql = "DELETE FROM {tripal_custom_tables} WHERE table_name = '%s'"; 00150 db_query($sql, $table); 00151 } 00152 $success = drupal_write_record('tripal_custom_tables', $record); 00153 if (!$success) { 00154 watchdog('tripal_core', "Error adding custom table %table_name.", 00155 array('%table_name' => $table), WATCHDOG_ERROR); 00156 drupal_set_message(t("Could not add custom table %table_name. 00157 Please check the schema array.", array('%table_name' => $table)), 'error'); 00158 return FALSE; 00159 } 00160 00161 // now add any foreign key constraints 00162 if(!$skip_creation and array_key_exists('foreign keys', $schema)){ 00163 $fkeys = $schema['foreign keys']; 00164 foreach ($fkeys as $fktable => $fkdetails) { 00165 $relations = $fkdetails['columns']; 00166 foreach ($relations as $left => $right) { 00167 $sql = "ALTER TABLE $table ADD CONSTRAINT " . 00168 $table . "_" . $left . "_fkey FOREIGN KEY ($left) REFERENCES $fktable ($right) " . 00169 "ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED"; 00170 if(!chado_query($sql)){ 00171 watchdog('tripal_core', "Error, could not add foreign key contraint to custom table.", 00172 array('!table_name' => $table), WATCHDOG_ERROR); 00173 drupal_set_message(t("Could not add foreign key contraint to table %table_name. 00174 Please check the schema array and the report log for errors.", 00175 array('%table_name' => $table)), 'error'); 00176 return FALSE; 00177 } 00178 } 00179 } 00180 } 00181 00182 return TRUE; 00183 } 00195 function tripal_custom_tables_get_table_id($table_name) { 00196 $sql = "SELECT * FROM {tripal_custom_tables} ". 00197 "WHERE table_name = '%s'"; 00198 if (db_table_exists('tripal_custom_tables')) { 00199 $custom_table = db_fetch_object(db_query($sql, $table_name)); 00200 if ($custom_table) { 00201 return $custom_table->table_id; 00202 } 00203 } 00204 00205 return FALSE; 00206 }