dc.executeStatement(trigger);
}
}
private void addTriggerToFolder() throws SQLException {
DotConnect dc = new DotConnect();
String trigger = "";
if(DbConnectionFactory.isPostgres()){
trigger = "CREATE OR REPLACE FUNCTION folder_identifier_check() RETURNS trigger AS '\n" +
"DECLARE\n" +
"versionsCount integer;\n" +
"BEGIN\n" +
"IF (tg_op = ''DELETE'') THEN\n" +
"select count(*) into versionsCount from folder where identifier = OLD.identifier;\n" +
"IF (versionsCount = 0)THEN\n" +
"DELETE from identifier where id = OLD.identifier;\n" +
"ELSE\n" +
"RETURN OLD;\n" +
"END IF;\n" +
"END IF;\n" +
"RETURN NULL;\n" +
"END\n" +
"' LANGUAGE plpgsql;\n" +
"CREATE TRIGGER folder_identifier_check_trigger AFTER DELETE\n" +
"ON folder FOR EACH ROW\n" +
"EXECUTE PROCEDURE folder_identifier_check();\n";
}else if(DbConnectionFactory.isMsSql()){
trigger = "CREATE Trigger folder_identifier_check\n" +
"ON folder\n" +
"FOR DELETE AS\n" +
"DECLARE @totalCount int\n" +
"DECLARE @identifier varchar(36)\n" +
"DECLARE folder_cur_Deleted cursor LOCAL FAST_FORWARD for\n" +
"Select identifier\n" +
"from deleted\n" +
"for Read Only\n" +
"open folder_cur_Deleted\n" +
"fetch next from folder_cur_Deleted into @identifier\n" +
"while @@FETCH_STATUS <> -1\n" +
"BEGIN\n" +
"select @totalCount = count(*) from folder where identifier = @identifier\n" +
"IF (@totalCount = 0)\n" +
"BEGIN\n" +
"DELETE from identifier where id = @identifier\n" +
"END\n" +
"fetch next from folder_cur_Deleted into @identifier\n" +
"END;\n";
}else if(DbConnectionFactory.isOracle()){
String oracleTrigger = "CREATE OR REPLACE PACKAGE folder_pkg as\n" +
"type array is table of folder%rowtype index by binary_integer;\n" +
"oldvals array;\n" +
"empty array;\n" +
"END;\n" +
"/\n" +
"CREATE OR REPLACE trigger folder_identifier_bd\n" +
"BEFORE DELETE ON folder\n" +
"BEGIN\n" +
"folder_pkg.oldvals := folder_pkg.empty;\n" +
"END;\n" +
"/\n" +
"CREATE OR REPLACE TRIGGER folder_identifier_bdfer\n" +
"BEFORE DELETE ON folder\n" +
"FOR EACH ROW\n" +
"BEGIN\n" +
"folder_pkg.oldvals(folder_pkg.oldvals.count+1).identifier := :old.identifier;\n" +
"END;\n" +
"/\n" +
"CREATE OR REPLACE TRIGGER folder_identifier_trigger\n" +
"AFTER DELETE ON folder\n" +
"DECLARE\n" +
"versionsCount integer;\n" +
"BEGIN\n" +
"for i in 1 .. folder_pkg.oldvals.count LOOP\n" +
"select count(*) into versionsCount from folder where identifier = folder_pkg.oldvals(i).identifier;\n" +
"IF (versionsCount = 0)THEN\n" +
"DELETE from identifier where id = folder_pkg.oldvals(i).identifier;\n" +
"END IF;\n" +
"END LOOP;\n" +
"END;\n" +
"/\n";
List<String> triggers = SQLUtil.tokenize(oracleTrigger);
for(String t:triggers){
dc.executeStatement(t);
}
}else{
String checkVersions = "DROP PROCEDURE IF EXISTS checkVersions;\n" +
"CREATE PROCEDURE checkVersions(IN ident varchar(36),IN tableName VARCHAR(20),OUT versionsCount INT)\n" +
"BEGIN\n" +
"SET versionsCount := 0;\n" +
"IF(tableName = 'htmlpage') THEN\n" +
"select count(inode) into versionsCount from htmlpage where identifier = ident;\n" +
"END IF;\n" +
"IF(tableName = 'file_asset') THEN\n" +
"select count(inode) into versionsCount from file_asset where identifier = ident;\n" +
"END IF;\n" +
"IF(tableName = 'links') THEN\n" +
"select count(inode) into versionsCount from links where identifier = ident;\n" +
"END IF;\n" +
"IF(tableName = 'containers') THEN\n" +
"select count(inode) into versionsCount from containers where identifier = ident;\n" +
"END IF;\n" +
"IF(tableName = 'template') THEN\n" +
"select count(inode) into versionsCount from template where identifier = ident;\n" +
"END IF;\n" +
"IF(tableName = 'contentlet') THEN\n" +
"select count(inode) into versionsCount from contentlet where identifier = ident;\n" +
"END IF;\n" +
"IF(tableName = 'folder') THEN\n" +
"select count(inode) into versionsCount from folder where identifier = ident;\n" +
"END IF;\n" +
"END\n" +
"#" ;
String folderTrigger = "DROP TRIGGER IF EXISTS folder_identifier_check;\n" +
"CREATE TRIGGER folder_identifier_check BEFORE DELETE\n" +
"on folder\n" +
"FOR EACH ROW\n" +
"BEGIN\n" +
"DECLARE tableName VARCHAR(20);\n" +
"DECLARE count INT;\n" +
"SET tableName = 'folder';\n" +
"CALL checkVersions(OLD.identifier,tableName,count);\n" +
"IF(count = 0)THEN\n" +
"delete from identifier where id = OLD.identifier;\n" +
"END IF;\n" +
"END\n" +
"#";
List<String> triggers = SQLUtil.tokenize(checkVersions + folderTrigger);
for(String t:triggers){
dc.executeStatement(t);
}
}
if(UtilMethods.isSet(trigger)){
dc.executeStatement(trigger);
}
}