[Gtdb-commits] r47 - in pkg/gt.db: R inst/schema

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Mar 12 07:38:10 CET 2010


Author: dahinds
Date: 2010-03-12 07:38:07 +0100 (Fri, 12 Mar 2010)
New Revision: 47

Modified:
   pkg/gt.db/R/misc.R
   pkg/gt.db/inst/schema/mk_mysql.sql
   pkg/gt.db/inst/schema/mk_sqlite.sql
Log:
- added triggers to enforce foreign keys to either restrict or cascade
  deletes from low cardinality tables.



Modified: pkg/gt.db/R/misc.R
===================================================================
--- pkg/gt.db/R/misc.R	2010-03-09 07:37:16 UTC (rev 46)
+++ pkg/gt.db/R/misc.R	2010-03-12 06:38:07 UTC (rev 47)
@@ -85,11 +85,12 @@
                      SQLiteConnection='mk_sqlite.sql',
                      MySQLConnection='mk_mysql.sql',
                      OraConnection='mk_oracle.sql')
-    file <- paste(path, 'schema', schema, sep='/')
-    s <- scan(file, what='character', sep='&')
-    s <- s[-grep('^--',s)]
-    s <- strsplit(paste(s, collapse='\n'), ';\n')[[1]]
-    sapply(s, sql.exec, db=gt.db::.gt.db, USE.NAMES=FALSE)
+    st <- readLines(paste(path, 'schema', schema, sep='/'))
+    st <- st[!grepl('^--',st)]
+    # split into statements at ';' followed by empty line
+    st <- strsplit(paste(st, collapse='\n'), ';\n\n')[[1]]
+    st <- st[!grepl('^\\s*delimiter\\s+',st)]
+    sapply(st, sql.exec, db=gt.db::.gt.db, USE.NAMES=FALSE)
     .gt.db.options(db.mode=db.mode)
     sql.exec(gt.db::.gt.db, 'insert into gtdb_option values (:1,:2)',
              'db.mode', db.mode)

Modified: pkg/gt.db/inst/schema/mk_mysql.sql
===================================================================
--- pkg/gt.db/inst/schema/mk_mysql.sql	2010-03-09 07:37:16 UTC (rev 46)
+++ pkg/gt.db/inst/schema/mk_mysql.sql	2010-03-12 06:38:07 UTC (rev 47)
@@ -334,3 +334,108 @@
     references prcomp(prcomp_id) on delete cascade,
   foreign key (sample_id) references sample(sample_id)
 );
+
+--
+-- Triggers to programmatically enforce foreign key constraints
+-- to restrict or cascade deletes, where appropriate
+--
+
+set @msg = 'Foreign key constraint violated!';
+
+create table error_msg (message varchar(128) primary key);
+
+insert into error_msg values (@msg);
+
+delimiter ;;
+
+create trigger delete_platform
+before delete on platform
+for each row begin
+  if (select count(*) from dataset where platform_id=old.platform_id)!=0 or
+     (select count(*) from mapping where platform_id=old.platform_id)!=0
+  then
+    insert error_msg values (@msg);
+  end if;
+  delete from assay where platform_id=old.platform_id;
+  delete from assay_flag where platform_id=old.platform_id;
+end;
+;;
+
+create trigger delete_mapping
+before delete on mapping
+for each row begin
+  delete from assay_position where mapping_id=old.mapping_id;
+end;
+;;
+
+create trigger delete_project
+before delete on project
+for each row begin
+  if (select count(*) from dataset where project_id=old.project_id)!=0
+  then
+    insert error_msg values (@msg);
+  end if;
+  delete from subject where project_id=old.project_id;
+  delete from subject_attr where project_id=old.project_id;
+end;
+;;
+
+create trigger delete_dataset
+before delete on dataset
+for each row begin
+  if (select count(*) from prcomp where dataset_id=old.dataset_id)!=0 or
+     (select count(*) from test where dataset_id=old.dataset_id)!=0
+  then
+    insert error_msg values (@msg);
+  end if;
+  delete from sample where dataset_id=old.dataset_id;
+  delete from sample_attr where dataset_id=old.dataset_id;
+  delete from assay_data where dataset_id=old.dataset_id;
+  delete from assay_data_flag where dataset_id=old.dataset_id;
+end;
+;;
+
+create trigger delete_subject_attr
+before delete on subject_attr
+for each row begin
+  delete from subject_value where subject_attr_id=old.subject_attr_id;
+end;
+;;
+
+create trigger delete_sample_attr
+before delete on sample_attr
+for each row begin
+  delete from sample_value where sample_attr_id=old.sample_attr_id;
+end;
+;;
+
+create trigger delete_subject
+before delete on subject
+for each row begin
+  delete from subject_value where subject_id=old.subject_id;
+end;
+;;
+
+create trigger delete_sample
+before delete on sample
+for each row begin
+  delete from sample_value where sample_id=old.sample_id;
+end;
+;;
+
+create trigger delete_test
+before delete on test
+for each row begin
+  delete from test_result where test_id=old.test_id;
+end;
+;;
+
+create trigger delete_prcomp
+before delete on prcomp
+for each row begin
+  delete from prcomp_loading where prcomp_id=old.prcomp_id;
+  delete from prcomp_component where prcomp_id=old.prcomp_id;
+end;
+;;
+
+delimiter ;

Modified: pkg/gt.db/inst/schema/mk_sqlite.sql
===================================================================
--- pkg/gt.db/inst/schema/mk_sqlite.sql	2010-03-09 07:37:16 UTC (rev 46)
+++ pkg/gt.db/inst/schema/mk_sqlite.sql	2010-03-12 06:38:07 UTC (rev 47)
@@ -334,3 +334,94 @@
     references prcomp(prcomp_id) on delete cascade,
   foreign key (sample_id) references sample(sample_id)
 );
+
+--
+-- Triggers to programmatically enforce foreign key constraints
+-- to restrict or cascade deletes, where appropriate
+--
+
+create trigger delete_platform
+before delete on platform
+for each row begin
+  select raise(rollback, 'Foreign key constraint violated!')
+  where (
+    select platform_id from dataset
+    where platform_id=old.platform_id
+  ) is not null or (
+    select platform_id from mapping
+    where platform_id=old.platform_id
+  ) is not null;
+  delete from assay where platform_id=old.platform_id;
+  delete from assay_flag where platform_id=old.platform_id;
+end;
+
+create trigger delete_mapping
+before delete on mapping
+for each row begin
+  delete from assay_position where mapping_id=old.mapping_id;
+end;
+
+create trigger delete_project
+before delete on project
+for each row begin
+  select raise(rollback, 'Foreign key constraint violated!')
+  where (
+    select dataset_id from dataset where dataset_id=old.dataset_id
+  ) is not null;
+  delete from subject where project_id=old.project_id;
+  delete from subject_attr where project_id=old.project_id;
+end;
+
+create trigger delete_dataset
+before delete on dataset
+for each row begin
+  select raise(rollback, 'Foreign key constraint violated!')
+  where (
+    select dataset_id from prcomp
+    where dataset_id_id=old.dataset_id
+  ) is not null or (
+    select dataset_id from test
+    where dataset_id_id=old.dataset_id
+  ) is not null;
+  delete from sample where dataset_id=old.dataset_id;
+  delete from sample_attr where dataset_id=old.dataset_id;
+  delete from assay_data where dataset_id=old.dataset_id;
+  delete from assay_data_flag where dataset_id=old.dataset_id;
+end;
+
+create trigger delete_subject_attr
+before delete on subject_attr
+for each row begin
+  delete from subject_value where subject_attr_id=old.subject_attr_id;
+end;
+
+create trigger delete_sample_attr
+before delete on sample_attr
+for each row begin
+  delete from sample_value where sample_attr_id=old.sample_attr_id;
+end;
+
+create trigger delete_subject
+before delete on subject
+for each row begin
+  delete from subject_value where subject_id=old.subject_id;
+end;
+
+create trigger delete_sample
+before delete on sample
+for each row begin
+  delete from sample_value where sample_id=old.sample_id;
+end;
+
+create trigger delete_test
+before delete on test
+for each row begin
+  delete from test_result where test_id=old.test_id;
+end;
+
+create trigger delete_prcomp
+before delete on prcomp
+for each row begin
+  delete from prcomp_loading where prcomp_id=old.prcomp_id;
+  delete from prcomp_component where prcomp_id=old.prcomp_id;
+end;



More information about the Gtdb-commits mailing list