1 21 22 package org.apache.derby.impl.tools.dblook; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 30 import java.util.HashMap ; 31 import java.util.StringTokenizer ; 32 33 import org.apache.derby.tools.dblook; 34 35 public class DB_Trigger { 36 37 44 45 public static void doTriggers (Connection conn) 46 throws SQLException 47 { 48 49 Statement stmt = conn.createStatement(); 50 ResultSet rs = stmt.executeQuery("SELECT TRIGGERNAME, SCHEMAID, " + 51 "EVENT, FIRINGTIME, TYPE, TABLEID, REFERENCEDCOLUMNS, " + 52 "TRIGGERDEFINITION, REFERENCINGOLD, REFERENCINGNEW, OLDREFERENCINGNAME, " + 53 "NEWREFERENCINGNAME FROM SYS.SYSTRIGGERS WHERE STATE != 'D'"); 54 55 boolean firstTime = true; 56 while (rs.next()) { 57 58 String trigName = dblook.addQuotes( 59 dblook.expandDoubleQuotes(rs.getString(1))); 60 String trigSchema = dblook.lookupSchemaId(rs.getString(2)); 61 62 if (dblook.isIgnorableSchema(trigSchema)) 63 continue; 64 65 trigName = trigSchema + "." + trigName; 66 String tableName = dblook.lookupTableId(rs.getString(6)); 67 68 72 if (!dblook.stringContainsTargetTable(rs.getString(8)) && 73 (dblook.isExcludedTable(tableName))) 74 continue; 75 76 if (firstTime) { 77 Logs.reportString("----------------------------------------------"); 78 Logs.reportMessage("DBLOOK_TriggersHeader"); 79 Logs.reportString("----------------------------------------------\n"); 80 } 81 82 String createTrigString = createTrigger(trigName, 83 tableName, rs); 84 85 Logs.writeToNewDDL(createTrigString); 86 Logs.writeStmtEndToNewDDL(); 87 Logs.writeNewlineToNewDDL(); 88 firstTime = false; 89 90 } 91 92 rs.close(); 93 stmt.close(); 94 95 } 96 97 106 107 private static String createTrigger(String trigName, String tableName, 108 ResultSet aTrig) throws SQLException 109 { 110 111 StringBuffer sb = new StringBuffer ("CREATE TRIGGER "); 112 sb.append(trigName); 113 114 if (aTrig.getString(4).charAt(0) == 'A') 116 sb.append(" AFTER "); 117 else 118 sb.append(" NO CASCADE BEFORE "); 119 120 switch (aTrig.getString(3).charAt(0)) { 122 case 'I': sb.append("INSERT"); 123 break; 124 case 'D': sb.append("DELETE"); 125 break; 126 case 'U': sb.append("UPDATE"); 127 String updateCols = aTrig.getString(7); 128 if (!aTrig.wasNull()) { 129 sb.append(" OF "); 130 sb.append(dblook.getColumnListFromDescription( 131 aTrig.getString(6), updateCols)); 132 } 133 break; 134 default: Logs.debug("INTERNAL ERROR: unexpected trigger event: " + 136 aTrig.getString(3), (String )null); 137 break; 138 } 139 140 sb.append(" ON "); 142 sb.append(tableName); 143 144 char trigType = aTrig.getString(5).charAt(0); 146 String oldReferencing = aTrig.getString(11); 147 String newReferencing = aTrig.getString(12); 148 if ((oldReferencing != null) || (newReferencing != null)) { 149 sb.append(" REFERENCING"); 150 if (aTrig.getBoolean(9)) { 151 sb.append(" OLD"); 152 if (trigType == 'S') 153 sb.append("_TABLE AS "); 155 else 156 sb.append(" AS "); 158 sb.append(oldReferencing); 159 } 160 if (aTrig.getBoolean(10)) { 161 sb.append(" NEW"); 162 if (trigType == 'S') 163 sb.append("_TABLE AS "); 165 else 166 sb.append(" AS "); 168 sb.append(newReferencing); 169 } 170 } 171 172 sb.append(" FOR EACH "); 174 if (trigType == 'S') 175 sb.append("STATEMENT "); 176 else 177 sb.append("ROW "); 178 179 sb.append("MODE DB2SQL "); 181 182 sb.append(dblook.removeNewlines(aTrig.getString(8))); 184 return sb.toString(); 185 186 } 187 188 } 189 | Popular Tags |