123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- function chado_edit_custom_table($table_id, $table_name, $schema, $skip_creation = 1) {
-
- $record = new stdClass();
- $record->table_id = $table_id;
- $record->table_name = $table_name;
- $record->schema = serialize($schema);
-
- $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_id = :table_id";
- $results = db_query($sql, array(':table_id' => $table_id));
- $custom_table = $results->fetchObject();
-
-
- if ($custom_table->table_name != $table_name) {
- chado_query("DROP TABLE %s", $custom_table->table_name);
- $skip_creation = 0;
- }
-
- if (!$skip_creation) {
- if (db_table_exists($custom_table->table_name)) {
- chado_query("DROP TABLE %s", $custom_table->table_name);
- drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name)));
- }
- }
-
- if (drupal_write_record('tripal_custom_tables', $record, 'table_id')) {
-
- if (!$skip_creation) {
- if (db_table_exists($custom_table->table_name)) {
- chado_query("DROP TABLE %s", $custom_table->table_name);
- drupal_set_message(t("Custom Table '%name' dropped", array('%name' => $custom_table->table_name)));
- }
-
- if (!chado_create_custom_table ($table_name, $schema)) {
- drupal_set_message(t("Could not create the custom table. Check Drupal error report logs."));
- }
- else {
- drupal_set_message(t("Custom table '%name' created", array('%name' => $table_name)));
- }
- }
-
- }
- }
- function chado_create_custom_table($table, $schema, $skip_creation = 1) {
- global $databases;
- $created = 0;
- $recreated = 0;
-
- $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = :table_name";
- $results = db_query($sql, array(':table_name' => $table));
- $centry = $results->fetchObject();
-
- $exists = chado_table_exists($table);
-
- if (!$exists) {
- try {
- $ret = db_create_table('chado.' . $table, $schema);
- $created = 1;
- }
- catch (Exception $e) {
- $error = $e->getMessage();
- tripal_report_error('tripal_core', TRIPAL_ERROR,
- "Error adding custom table: @message", array('@message' => $error));
- drupal_set_message("Could not add custom table. $error.", "error");
- return FALSE;
- }
- }
-
-
- if ($exists and is_object($centry) and !$skip_creation) {
-
- try {
- chado_query('DROP TABLE {' . $table . '}');
- db_create_table('chado.' . $table, $schema);
- $recreated = 1;
- }
- catch (Exception $e) {
- $error = $e->getMessage();
- tripal_report_error('tripal_core', TRIPAL_ERROR,
- "Error adding custom table: @message",
- array('@message' => $error));
- drupal_set_message("Could not add custom table. $error.", "error");
- return FALSE;
- }
- }
-
- $record = new stdClass();
- $record->table_name = $table;
- $record->schema = serialize($schema);
-
- if ($centry) {
- $sql = "DELETE FROM {tripal_custom_tables} WHERE table_name = :table_name";
- db_query($sql, array(':table_name' => $table));
- }
- $success = drupal_write_record('tripal_custom_tables', $record);
- if (!$success) {
- tripal_report_error('tripal_core', TRIPAL_ERROR,
- "Error adding custom table %table_name.",
- array('%table_name' => $table));
- drupal_set_message(t("Could not add custom table %table_name.
- Please check the schema array.", array('%table_name' => $table)), 'error');
- return FALSE;
- }
-
- if (!$skip_creation and array_key_exists('foreign keys', $schema)) {
-
- $fkeys = $schema['foreign keys'];
- foreach ($fkeys as $fktable => $fkdetails) {
- $relations = $fkdetails['columns'];
- foreach ($relations as $left => $right) {
- $sql = '
- ALTER TABLE {' . $table . '}
- ADD CONSTRAINT ' . $table . '_' . $left . '_fkey FOREIGN KEY (' . $left . ')
- REFERENCES {' . $fktable . '} (' . $right . ')
- ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
- ';
- try {
- chado_query($sql);
- }
- catch (Exception $e) {
- $error = $e->getMessage();
- tripal_report_error('tripal_core', TRIPAL_ERROR, "Error, could not add foreign key contraint to custom table: %error",
- array('%error' => $error));
- drupal_set_message("Could not add foreign key contraint to table: $error", 'error');
- return FALSE;
- }
- }
- }
- }
- if ($created) {
- drupal_set_message("Custom table created successfully.", 'status');
- }
- elseif ($recreated) {
- drupal_set_message("Custom table re-created successfully.", 'status');
- }
- else {
- drupal_set_message("Custom table already exists. Table structure not changed, but definition array has been saved.", 'status');
- }
- return TRUE;
- }
- function chado_get_custom_table_id($table_name) {
- if (db_table_exists('tripal_custom_tables')) {
- $sql = "SELECT * FROM {tripal_custom_tables} WHERE table_name = :table_name";
- $results = db_query($sql, array(':table_name' => $table_name));
- $custom_table = $results->fetchObject();
- if ($custom_table) {
- return $custom_table->table_id;
- }
- }
- return FALSE;
- }
|