@Override protected void preloadTriggers() throws SQLException { DatabaseTrigger trigger; final IGenericDAO<DatabaseTrigger, Serializable> triggerDAO = HibernateDAOFactory.getTriggerDAO(); final String triggerSelect = "SELECT trigger_name, event_manipulation, definer, event_object_table, action_statement, action_timing FROM information_schema.triggers WHERE trigger_schema = '" + databaseConnection.getDatabase() + "'"; final Statement stm = sqlConnection.createStatement(); stm.setFetchSize(fetchSize); final ResultSet resultSet = stm.executeQuery(triggerSelect); // Create a new trigger data map. triggerData = new HashMap<String, String[]>(); // For each result (trigger), create a trigger object and store the // referring information in triggerData. while (resultSet.next()) { trigger = triggerDAO.create(); trigger.setTitle(resultSet.getString(1)); trigger.setConnection(databaseConnection); databaseConnection.addTrigger(trigger); triggers.add(trigger); // 1 trigger_name, 2 event_manipulation, 3 definer, 4 event_object_table, 5 action_statement, // 6 // action_timing triggerData.put( trigger.getTitle(), new String[] { resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), resultSet.getString(6) }); } // Clear all resources. resultSet.close(); stm.close(); firePropertyChange("numberOfTriggers", triggers.size()); }
@Override protected void analyzeTriggers() throws SQLException { for (final DatabaseTrigger trigger : triggers) { firePropertyChange("analyzingTrigger", trigger.getTitle()); // Create a new ddlschema object. final TriggerSchemaEditable schema = trigger.createTriggerSchemaEditableObject(); // Get all trigger data. final String[] data = triggerData.get(trigger.getTitle()); // 1 trigger_name, 2 trigger_type, 3 triggering_event, 4 table_owner, 5 base_object_type, 6 // table_name, 7 // column_name, 8 referencing_names, 9 when_clause, 10 status, 11 description, 12 action_type, // 13 // trigger_body, 14 crossedition, 15 before_statement, 16 before_row, 17 after_row, 18 // after_statement, 19 // instead_of_row // 1 trigger_name, 2 event_manipulation, 3 definer, 4 event_object_table, 5 action_statement, // 6 // action_timing schema.setName(trimStringOrNull(data[1 - 1])); schema.setType(trimStringOrNull(data[6 - 1])); schema.setEvents(Arrays.asList(trimStringOrNull(data[2 - 1]).split("(OR)"))); schema.setTableOwner(trimStringOrNull(data[3 - 1])); // schema.setBaseObjectType(trimStringOrNull(data[5 - 1])); schema.setTableName(trimStringOrNull(data[4 - 1])); // schema.setColumnName(trimStringOrNull(data[7 - 1])); // schema.setReferencingNames(trimStringOrNull(data[8 - 1])); // schema.setWhenClause(trimStringOrNull(data[9 - 1])); schema.setEnabled(true); // schema.setDescription(trimStringOrNull(data[11 - 1])); // schema.setActionType(trimStringOrNull(data[12 - 1])); // MySQL deletes the ending semicolon of the trigger-body, so we added it again schema.setBody(trimStringOrNull(data[5 - 1]) + ";"); // schema.setCrossEdition(!"NO".equalsIgnoreCase(trimStringOrNull(data[14 - 1]))); // schema.setInsteadOfRow(!"NO".equalsIgnoreCase(trimStringOrNull(data[19 - 1]))); // This columns are always "NO" and seem not containing the right information. As workaround, // we scan the // type column's content. // schema.setBeforeStatement(!"NO".equalsIgnoreCase(trimStringOrNull(data[15 - 1]))); // schema.setBeforeRow(!"NO".equalsIgnoreCase(trimStringOrNull(data[16 - 1]))); // schema.setAfterRow(!"NO".equalsIgnoreCase(trimStringOrNull(data[17 - 1]))); // schema.setAfterStatement(!"NO".equalsIgnoreCase(trimStringOrNull(data[18 - 1]))); // schema.setBeforeStatement(compareModeStrings(data[2 - 1], "BEFORE STATEMENT")); // schema.setBeforeRow(compareModeStrings(data[6 - 1], "BEFORE")); // schema.setAfterRow(compareModeStrings(data[6 - 1], "AFTER")); // schema.setAfterStatement(compareModeStrings(data[2 - 1], "AFTER STATEMENT")); // schema.setInsteadOfRow(compareModeStrings(data[2 - 1], "INSTEAD OF")); // Save the trigger schema. trigger.setTriggerSchemaObject(schema); // Add the trigger to the corresponding table. Otherwise ignore this trigger final DatabaseTable table = schema.getTable(); if (table != null) { final DdlSchemaEditable ddlschema = table.createDdlSchemaEditableObject(); ddlschema.addTrigger(schema.getName()); table.setDdlSchemaObject(ddlschema); } else { trigger.getConnection().removeTrigger(trigger); triggers.remove(trigger); } } }