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 import java.sql.Statement;
010
011 import java.util.ArrayList;
012 import java.util.Collection;
013 import java.util.logging.Level;
014
015 import com.sptci.util.CloseJDBCResources;
016
017 /**
018 * An abstractanalyser for analysing view type objects in the database.
019 * Sub-classes perform database engine specific analysis since not all
020 * database engines support the SQL92 standard information schema.
021 *
022 * <p>© Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
023 * @author Rakesh Vidyadharan 2007-11-02
024 * @version $Id: AbstractViewAnalyser.java 4123 2008-05-25 21:49:01Z rakesh $
025 * @since Version 1.2
026 */
027 public abstract class AbstractViewAnalyser extends Analyser
028 {
029 /**
030 * The <code>types</code> parameters to specify while invoking the
031 * {@link java.sql.DatabaseMetaData#getTables} method.
032 */
033 protected static final String[] TYPE = new String[] { "VIEW" };
034
035 /**
036 * Create a new instance of the class using the specified connection
037 * manager.
038 *
039 * @param manager The manager for obtaining database connections.
040 */
041 protected AbstractViewAnalyser( final ConnectionManager manager )
042 {
043 super( manager );
044 }
045
046 /**
047 * Returns a collection of {@link ViewMetaData} objects that contain the
048 * basic information pertaining to the views in the schema. You must
049 * invoke {@link #getAdditionalAttributes} to fetch information from
050 * the <code>information_schema</code>.
051 *
052 * @see Analyser#analyse
053 * @see #getNames
054 * @param parameters Must contain one parameter which is a {@link
055 * RootMetaData} that represents the <code>catalog</code> or
056 * <code>schema</code> to restrict the analysis to. The name of the
057 * catalogue or schema may be <code>null</code> or an empty string.
058 */
059 @Override
060 public Collection<ViewMetaData> analyse( final MetaData... parameters )
061 throws SQLException
062 {
063 Collection<ViewMetaData> collection = new ArrayList<ViewMetaData>();
064 Connection connection = null;
065 ResultSet resultSet = null;
066
067 try
068 {
069 final CatalogueSchema cs = getNames( parameters[0] );
070
071 connection = manager.open();
072 DatabaseMetaData dmd = connection.getMetaData();
073 resultSet = dmd.getTables( cs.getCatalogue(), cs.getSchema(), "%", TYPE );
074
075 while ( resultSet.next() )
076 {
077 ViewMetaData vmd = new ViewMetaData();
078 vmd.setName( resultSet.getString( "table_name" ) );
079 vmd.setComment( resultSet.getString( "remarks" ) );
080 vmd.setRoot( (RootMetaData) parameters[0] );
081
082 collection.add( vmd );
083 }
084
085 if ( parameters[0] != null )
086 {
087 ( (RootMetaData) parameters[0] ).setViews( collection );
088 }
089 }
090 finally
091 {
092 CloseJDBCResources.close( resultSet );
093 CloseJDBCResources.close( connection );
094 }
095
096 if ( collection.isEmpty() )
097 {
098 getViews( (RootMetaData) parameters[0], collection );
099 }
100
101 return collection;
102 }
103
104 /**
105 * Fetch the views directly from <code>information_schema</code> if no
106 * information is available through {@link java.sql.DatabaseMetaData}.
107 *
108 * @see #getNames
109 * @param root The metadata object that represents the catalog/schema for
110 * which the views are to be retrieved.
111 * @param collection The collection to which the {@link ViewMetaData}
112 * objects are to be added.
113 * @throws SQLException If errors are encountered while fetching the views.
114 */
115 protected abstract void getViews( final RootMetaData root,
116 final Collection<ViewMetaData> collection ) throws SQLException;
117
118 /**
119 * Fetch additional meta data about the specified view from the
120 * <code>information_schema</code>.
121 *
122 * @since Version 1.1
123 * @param vmd The meta data object that is to have additional attributes
124 * populated.
125 */
126 protected abstract void getAdditionalAttributes( final ViewMetaData vmd );
127 }