001 package com.sptci.rwt;
002
003 import java.sql.Connection;
004 import java.sql.PreparedStatement;
005 import java.sql.ResultSet;
006 import java.sql.SQLException;
007
008 import java.util.ArrayList;
009 import java.util.Collection;
010 import java.util.logging.Level;
011
012 import com.sptci.util.CloseJDBCResources;
013
014 /**
015 * An abstract analyser for analysing trigger type objects in the database.
016 * Serves as the base class for implementation specific trigger analysers.
017 *
018 * <p>© Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
019 * @author Rakesh Vidyadharan 2007-11-02
020 * @version $Id: AbstractTriggerAnalyser.java 4123 2008-05-25 21:49:01Z rakesh $
021 * @since Version 1.2
022 */
023 public abstract class AbstractTriggerAnalyser extends Analyser
024 {
025 /**
026 * Create a new instance of the class using the specified connection
027 * manager.
028 *
029 * @param manager The manager for obtaining database connections.
030 */
031 protected AbstractTriggerAnalyser( final ConnectionManager manager )
032 {
033 super( manager );
034 }
035
036 /**
037 * Returns a collection of {@link TriggerMetaData} objects that contain
038 * the basic information pertaining to the triggers in the schema and
039 * table (optional) specified.
040 *
041 * @see Analyser#analyse
042 * @param parameters Must contain at least one parameter which is a {@link
043 * SchemaMetaData} that represents the <code>schema</code> to restrict
044 * the analysis to. An optional additional {@link TableMetaData}
045 * parameter may be specified to restrict the analysis to a specified
046 * table.
047 */
048 @Override
049 public Collection<TriggerMetaData> analyse( final MetaData... parameters )
050 throws SQLException
051 {
052 Collection<TriggerMetaData> collection = new ArrayList<TriggerMetaData>();
053 Connection connection = null;
054 PreparedStatement statement = null;
055 ResultSet resultSet = null;
056
057 try
058 {
059 connection = manager.open();
060 statement = createStatement( connection, parameters );
061 resultSet = statement.executeQuery();
062 createMetaData( resultSet, collection, parameters );
063
064 if ( parameters.length == 1 )
065 {
066 ( (RootMetaData) parameters[0] ).setTriggers( collection );
067 }
068 else if ( parameters.length > 1 )
069 {
070 ( (TableMetaData) parameters[1] ).setTriggers( collection );
071 }
072 }
073 catch ( SQLException sex )
074 {
075 logger.log( Level.FINE,
076 "Error fetching trigger details from information_schema", sex );
077 }
078 finally
079 {
080 CloseJDBCResources.close( resultSet );
081 CloseJDBCResources.close( statement );
082 CloseJDBCResources.close( connection );
083 }
084
085 return collection;
086 }
087
088 /**
089 * Create a {@link java.sql.PreparedStatement} based on the
090 * <code>parameters</code> passed in.
091 *
092 * @see #getNames
093 * @param connection The database connection to use.
094 * @param parameters The array of {@link SchemaMetaData} and {@link
095 * TableMetaData} objects.
096 * @return PreparedStatement The initialised statement that can be executed.
097 * @throws SQLException If errors are encountered while creating the
098 * statement.
099 */
100 protected abstract PreparedStatement createStatement(
101 final Connection connection, final MetaData... parameters )
102 throws SQLException;
103
104 /**
105 * Create a new metadata object out of the data in the specified result
106 * set.
107 *
108 * @param resultSet The result set from which to create the metadata.
109 * @param collection The collection to which the metadata objects are
110 * to be added.
111 * @param parameters The array of {@link RootMetaData} and {@link
112 * TableMetaData} objects.
113 * @throws SQLException If errors are encountered while fetching the
114 * data from the result set.
115 */
116 protected abstract void createMetaData( final ResultSet resultSet,
117 final Collection<TriggerMetaData> collection,
118 final MetaData... parameters ) throws SQLException;
119 }