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.DatabaseMetaData;
19  import java.sql.ResultSet;
20  import java.sql.ResultSetMetaData;
21  import java.sql.SQLException;
22  import java.util.HashMap;
23  import java.util.Map;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.log4j.Logger;
26  
27  /***
28   * Implemention of {@link ResultSetExtractor} that writes the results of a
29   * show tables command to the object's print streams.
30   *
31   * @author  Steven Caswell
32   * @version $Id: PrintStreamedShowTablesResultSetExtractor.java,v 1.2 2004/09/07 22:24:21 mungoknotwise Exp $
33   */
34  public class PrintStreamedShowTablesResultSetExtractor
35    extends BasicPrintStreamed
36    implements ResultSetExtractor
37  {
38    
39    //----------------------------------------------------------------------------
40    // Static variables
41    //----------------------------------------------------------------------------
42    
43    private static Logger logger = Logger.getLogger(PrintStreamedShowTablesResultSetExtractor.class);
44    
45    //----------------------------------------------------------------------------
46    // Static methods
47    //----------------------------------------------------------------------------
48    
49    //----------------------------------------------------------------------------
50    // Constants
51    //----------------------------------------------------------------------------
52    
53    private static final int CATALOG_NAME_INDEX = 1;
54    private static final int SCHEMA_NAME_INDEX = 2;
55    private static final int TABLE_NAME_INDEX = 3;
56  
57    private static final int META_DATA_TABLE_NAME = 3;
58  
59    //----------------------------------------------------------------------------
60    // Instance variables
61    //----------------------------------------------------------------------------
62  
63    private Map attributes;
64    private int catalogNameColumnSize;
65    private DatabaseMetaData databaseMetaData;
66    private String fieldSeparator = "|";
67    private String headerSeparator = "+";
68    private int rowCount = 0;
69    private int schemaNameColumnSize;
70    private int tableNameColumnSize;
71    
72    //----------------------------------------------------------------------------
73    // Constructors
74    //----------------------------------------------------------------------------
75    
76    /***
77     * Constructs a new instance of
78     * <code>PrintStreamedShowTablesResultSetExtractor</code>.
79     */
80    public PrintStreamedShowTablesResultSetExtractor()
81    {
82      this.attributes = new HashMap();
83    }
84    
85    //----------------------------------------------------------------------------
86    // Interface implementations
87    //----------------------------------------------------------------------------
88    //----------------------------------------------------------------------------
89    // Implementation of interface ResultSetExtractor
90    //----------------------------------------------------------------------------
91  
92    /***
93     * {@inheritDoc}
94     */
95    public Object extractData(final ResultSet resultSet) throws SQLException
96    {
97      ResultSetMetaData metaData = resultSet.getMetaData();
98      if(this.databaseMetaData != null)
99      {
100       this.tableNameColumnSize = this.databaseMetaData.getMaxTableNameLength();
101     }
102     else
103     {
104       this.tableNameColumnSize = metaData.getColumnDisplaySize(META_DATA_TABLE_NAME);
105     }
106     if(logger.isDebugEnabled())
107     {
108       logger.debug("table name column size: " + this.tableNameColumnSize);
109     }
110     this.rowCount = 0;
111     while(resultSet.next())
112     {
113       if(this.rowCount == 0)
114       {
115         this.writeHeader();
116       }
117       ++this.rowCount;
118       if(logger.isDebugEnabled())
119       {
120         logger.debug("row: " + this.rowCount);
121       }
122       String catalogName = resultSet.getString(CATALOG_NAME_INDEX);
123       String schemaName = resultSet.getString(SCHEMA_NAME_INDEX);
124       String tableName = resultSet.getString(TABLE_NAME_INDEX);
125       if(logger.isDebugEnabled())
126       {
127         logger.debug("table name: " + (tableName == null ? "null" : tableName));
128       }
129       this.print(this.fieldSeparator);
130       this.print(StringUtils.rightPad(tableName, tableNameColumnSize));
131       this.println(this.fieldSeparator);
132     }
133     if(this.rowCount > 0)
134     {
135       this.writeFooter();
136     }
137     else
138     {
139       this.writeNoTables();
140     }
141     return null;
142   }
143   
144   /***
145    * {@inheritDoc}
146    */
147   public void setAttribute(final String key, final Object value)
148   {
149     this.attributes.put(key, value);
150   }
151   
152   //----------------------------------------------------------------------------
153   // Extends overrides
154   //----------------------------------------------------------------------------
155   //----------------------------------------------------------------------------
156   // Override of class Class1
157   //----------------------------------------------------------------------------
158   
159   
160   //----------------------------------------------------------------------------
161   // Public methods exposed by this class
162   //----------------------------------------------------------------------------
163 
164   /***
165    * Sets the database metadata for use by this instance.
166    *
167    * @param databaseMetaData databaseMetaData
168    */
169   public void setDatabaseMetaData(final DatabaseMetaData databaseMetaData)
170   {
171     this.databaseMetaData = databaseMetaData;
172   }
173   
174   //----------------------------------------------------------------------------
175   // Protected abstract methods
176   //----------------------------------------------------------------------------
177   
178   //----------------------------------------------------------------------------
179   // Protected methods for use by subclasses
180   //----------------------------------------------------------------------------
181   
182   //----------------------------------------------------------------------------
183   // Other methods
184   //----------------------------------------------------------------------------
185 
186   private void writeNoTables()
187   {
188     this.println("No tables found");
189   }
190   
191   /***
192    * Writes the footer to the print streams.
193    */
194   public void writeFooter()
195   {
196     this.writeHeaderLine();
197     this.println("");
198     this.println(this.rowCount + " row(s)");
199     this.println("");
200   }
201 
202   /***
203    * Writes the header to the print streams.
204    */
205   public void writeHeader()
206   {
207     this.println("");
208     this.writeHeaderLine();
209     this.print(this.fieldSeparator);
210     this.print(StringUtils.rightPad("table", this.tableNameColumnSize));
211     this.println(this.fieldSeparator);
212     this.writeHeaderLine();
213   }
214 
215   private void writeHeaderLine()
216   {
217     this.print(this.headerSeparator);
218     this.print(StringUtils.repeat("-", this.tableNameColumnSize));
219     this.println(this.headerSeparator);
220   }
221 
222   //----------------------------------------------------------------------------
223   // Member classes
224   //----------------------------------------------------------------------------
225   
226 }