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