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.ArrayList;
009    import java.util.Collection;
010    
011    import com.sptci.util.CloseJDBCResources;
012    
013    /**
014     * An analyser for analysing column type objects in the database.
015     *
016     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
017     * @author Rakesh Vidyadharan 2007-09-25
018     * @version $Id: ColumnAnalyser.java 4123 2008-05-25 21:49:01Z rakesh $
019     * @see java.sql.DatabaseMetaData#getColumns
020     */
021    public class ColumnAnalyser extends Analyser
022    {
023      /**
024       * Create a new instance of the class using the specified connection
025       * manager.
026       *
027       * @param manager The manager for obtaining database connections.
028       */
029      public ColumnAnalyser( final ConnectionManager manager )
030      {
031        super( manager );
032      }
033    
034      /**
035       * Returns a collection of {@link ColumnMetaData} objects that contain all
036       * information pertaining to the columns in the specified table.
037       *
038       * @see Analyser#analyse
039       * @see #getNames
040       * @param parameters Must contain two parameters which are the names of
041       *   the <code>catalog/schema</code> and table type from which column
042       *   metadata are to be retrieved.  Table types are table, view, etc.
043       */
044      @Override
045      public Collection<ColumnMetaData> analyse( final MetaData... parameters )
046        throws SQLException
047      {
048        final Collection<ColumnMetaData> collection =
049          new ArrayList<ColumnMetaData>();
050        Connection connection = null;
051        ResultSet resultSet = null;
052    
053        try
054        {
055          final CatalogueSchema cs = getNames( parameters[0] );
056          final String table = parameters[1].getName();
057    
058          connection = manager.open();
059          final DatabaseMetaData dmd = connection.getMetaData();
060          resultSet = dmd.getColumns(
061              cs.getCatalogue(), cs.getSchema(), table, "%" );
062    
063          while ( resultSet.next() )
064          {
065            final ColumnMetaData cmd = new ColumnMetaData();
066    
067            cmd.setName( resultSet.getString( "column_name" ) );
068            cmd.setComment( resultSet.getString( "remarks" ) );
069            cmd.setDefaultValue( resultSet.getString( "column_def" ) );
070            cmd.setTypeName( resultSet.getString( "type_name" ) );
071            cmd.setNullable( resultSet.getString( "nullable" ) );
072    
073            cmd.setSize( resultSet.getInt( "column_size" ) );
074            cmd.setType( resultSet.getInt( "data_type" ) );
075            cmd.setTable( (TableTypeMetaData) parameters[1] );
076    
077            collection.add( cmd );
078          }
079    
080          ( (TableTypeMetaData) parameters[1] ).setColumns( collection );
081        }
082        finally
083        {
084          CloseJDBCResources.close( resultSet );
085          CloseJDBCResources.close( connection );
086        }
087    
088        return collection;
089      }
090    }