001    package com.sptci.rwt;
002    
003    import java.sql.Connection;
004    import java.sql.DatabaseMetaData;
005    import java.sql.ParameterMetaData;
006    import java.sql.PreparedStatement;
007    import java.sql.ResultSet;
008    import java.sql.SQLException;
009    
010    import java.util.logging.Level;
011    
012    import java.util.ArrayList;
013    import java.util.Collection;
014    
015    import com.sptci.util.CloseJDBCResources;
016    
017    /**
018     * An abstract base class for database engine specific procedure analysers.
019     * Sub-class implement the relevant techniques for retrieving procedure
020     * information.
021     *
022     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
023     * @author Rakesh Vidyadharan 2007-11-03
024     * @version $Id: AbstractProcedureAnalyser.java 4123 2008-05-25 21:49:01Z rakesh $
025     * @since Version 1.2
026     */
027    abstract class AbstractProcedureAnalyser extends Analyser
028    {
029      /**
030       * Create a new instance of the class using the specified connection
031       * manager.
032       *
033       * @param manager The manager for obtaining database connections.
034       */
035      AbstractProcedureAnalyser( final ConnectionManager manager )
036      {
037        super( manager );
038      }
039    
040      /**
041       * Returns a collection of {@link ProcedureMetaData} objects that contain
042       * the basic information pertaining to the procedures in the
043       * catalog/schema.  You should invoke {@link #getAdditionalAttributes}
044       * to fetch the additional attributes that are available in
045       * <code>information_schema</code>.
046       *
047       * @see Analyser#analyse
048       * @see #getNames
049       * @param parameters Must contain one parameter which is a {@link
050       *   RootMetaData} that represents the <code>catalog</code> or 
051       *   <code>schema</code> to restrict the analysis to.
052       */
053      @Override
054      public Collection<ProcedureMetaData> analyse(
055          final MetaData... parameters ) throws SQLException
056      {
057        final Collection<ProcedureMetaData> collection =
058          new ArrayList<ProcedureMetaData>();
059    
060        Connection connection = null;
061        ResultSet resultSet = null;
062    
063        try
064        {
065          final CatalogueSchema cs = getNames( parameters[0] );
066    
067          connection = manager.open();
068          DatabaseMetaData dmd = connection.getMetaData();
069          resultSet = dmd.getProcedures( cs.getCatalogue(), cs.getSchema(), "%" );
070    
071          while ( resultSet.next() )
072          {
073            ProcedureMetaData pmd = new ProcedureMetaData();
074    
075            pmd.setName( resultSet.getString( "procedure_name" ) );
076            pmd.setComment( resultSet.getString( "remarks" ) );
077    
078            if ( parameters[0] != null )
079            {
080              pmd.setRoot( (RootMetaData) parameters[0] );
081            }
082    
083            collection.add( pmd );
084          }
085    
086          if ( parameters[0] != null )
087          {
088            ( (RootMetaData) parameters[0] ).setProcedures( collection );
089          }
090        }
091        finally
092        {
093          CloseJDBCResources.close( resultSet );
094          CloseJDBCResources.close( connection );
095        }
096    
097        return collection;
098      }
099    
100      /**
101       * Fetch additional meta data available from the <code>information
102       * schema</code> to the object.
103       *
104       * @since Version 1.1
105       * @param pmd The meta data object that is to be updated.
106       */
107      protected abstract void getAdditionalAttributes( final ProcedureMetaData pmd );
108    }