View Javadoc

1   /***
2    * Copyright 2004 Steven Caswell
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.mungoknotwise.sqlcli;
17  
18  import java.sql.Connection;
19  import java.sql.DatabaseMetaData;
20  import java.sql.ResultSet;
21  import org.apache.log4j.Logger;
22  
23  /***
24   * Performs a show tables command and handles the results.
25   *
26   * @author  Steven Caswell
27   * @version $Id: ShowTablesCommand.java,v 1.2 2004/07/28 23:25:21 mungoknotwise Exp $
28   */
29  public class ShowTablesCommand
30    implements ResultSetExtractingCommand
31  {
32    
33    //----------------------------------------------------------------------------
34    // Static variables
35    //----------------------------------------------------------------------------
36    
37    private static Logger logger = Logger.getLogger(ShowTablesCommand.class);
38    
39    //----------------------------------------------------------------------------
40    // Static methods
41    //----------------------------------------------------------------------------
42    
43    //----------------------------------------------------------------------------
44    // Constants
45    //----------------------------------------------------------------------------
46    
47    //----------------------------------------------------------------------------
48    // Instance variables
49    //----------------------------------------------------------------------------
50    
51    private Connection connection;
52    private ExceptionHandler exceptionHandler;
53    private ResultSetExtractor resultSetExtractor;
54  
55    //----------------------------------------------------------------------------
56    // Constructors
57    //----------------------------------------------------------------------------
58    
59    /***
60     * Constructs a new instance of
61     * <code>ShowTablesCommand</code>.
62     */
63    public ShowTablesCommand()
64    {
65    }
66    
67    //----------------------------------------------------------------------------
68    // Interface implementations
69    //----------------------------------------------------------------------------
70    //----------------------------------------------------------------------------
71    // Implementation of interface Interface1
72    //----------------------------------------------------------------------------
73    
74    //----------------------------------------------------------------------------
75    // Extends overrides
76    //----------------------------------------------------------------------------
77    //----------------------------------------------------------------------------
78    // Override of class Class1
79    //----------------------------------------------------------------------------
80    
81    
82    //----------------------------------------------------------------------------
83    // Public methods exposed by this class
84    //----------------------------------------------------------------------------
85  
86    /***
87     * {@inheritDoc}
88     */
89    public void execute()
90    {
91      try
92      {
93        DatabaseMetaData metaData = this.connection.getMetaData();
94        String schema = metaData.getUserName();
95        String[] types = { "TABLE", "VIEW" };
96        ResultSet resultSet = metaData.getTables(null, schema, null, types);
97        this.resultSetExtractor.extractData(resultSet);
98      }
99      catch(Exception e)
100     {
101       this.exceptionHandler.handleException(e);
102     }
103   }
104 
105   /***
106    * Sets the connection for this command.
107    *
108    * @param connection the connection
109    */
110   public void setConnection(final Connection connection)
111   {
112     this.connection = connection;
113   }
114 
115   /***
116    * {@inheritDoc}
117    */
118   public void setExceptionHandler(final ExceptionHandler exceptionHandler)
119   {
120     this.exceptionHandler = exceptionHandler;
121   }
122 
123   /***
124    * {@inheritDoc}
125    */
126   public void setResultSetExtractor(final ResultSetExtractor resultSetExtractor)
127   {
128     this.resultSetExtractor = resultSetExtractor;
129   }
130 
131   //----------------------------------------------------------------------------
132   // Protected abstract methods
133   //----------------------------------------------------------------------------
134   
135   //----------------------------------------------------------------------------
136   // Protected methods for use by subclasses
137   //----------------------------------------------------------------------------
138   
139   //----------------------------------------------------------------------------
140   // Other methods
141   //----------------------------------------------------------------------------
142   
143   //----------------------------------------------------------------------------
144   // Member classes
145   //----------------------------------------------------------------------------
146   
147 }