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 * Provides logic for executing a command to describe a table, and to handle
25 * the results.
26 *
27 * @author Steven Caswell
28 * @version $Id: DescribeCommand.java,v 1.3 2004/09/07 22:13:07 mungoknotwise Exp $
29 */
30 public class DescribeCommand
31 implements ResultSetExtractingCommand
32 {
33
34 //----------------------------------------------------------------------------
35 // Static variables
36 //----------------------------------------------------------------------------
37
38 private static Logger logger = Logger.getLogger(DescribeCommand.class);
39
40 //----------------------------------------------------------------------------
41 // Static methods
42 //----------------------------------------------------------------------------
43
44 //----------------------------------------------------------------------------
45 // Constants
46 //----------------------------------------------------------------------------
47
48 public static final String TABLE_NAME_KEY = "table_name";
49
50 //----------------------------------------------------------------------------
51 // Instance variables
52 //----------------------------------------------------------------------------
53
54 private Connection connection;
55 private ExceptionHandler exceptionHandler;
56 private ResultSetExtractor resultSetExtractor;
57 private String tableName;
58
59 //----------------------------------------------------------------------------
60 // Constructors
61 //----------------------------------------------------------------------------
62
63 /***
64 * Constructs a new instance of
65 * <code>DescribeCommand</code>.
66 */
67 public DescribeCommand()
68 {
69 }
70
71 //----------------------------------------------------------------------------
72 // Interface implementations
73 //----------------------------------------------------------------------------
74 //----------------------------------------------------------------------------
75 // Implementation of interface Interface1
76 //----------------------------------------------------------------------------
77
78 //----------------------------------------------------------------------------
79 // Extends overrides
80 //----------------------------------------------------------------------------
81 //----------------------------------------------------------------------------
82 // Override of class Class1
83 //----------------------------------------------------------------------------
84
85
86 //----------------------------------------------------------------------------
87 // Public methods exposed by this class
88 //----------------------------------------------------------------------------
89
90 /***
91 * {@inheritDoc}
92 */
93 public void execute()
94 {
95 try
96 {
97 DatabaseMetaData metaData = this.connection.getMetaData();
98 String findTableName = tableName;
99 if(metaData.storesLowerCaseIdentifiers())
100 {
101 findTableName = tableName.toLowerCase();
102 }
103 else if(metaData.storesUpperCaseIdentifiers())
104 {
105 findTableName = tableName.toUpperCase();
106 }
107 ResultSet resultSet = metaData.getColumns(null, null, findTableName, null);
108 this.resultSetExtractor.setAttribute(TABLE_NAME_KEY, tableName);
109 this.resultSetExtractor.extractData(resultSet);
110 }
111 catch(Exception e)
112 {
113 this.exceptionHandler.handleException(e);
114 }
115 }
116
117 /***
118 * Sets the connection to be used to execute this command.
119 *
120 * @param connection the connection
121 */
122 public void setConnection(final Connection connection)
123 {
124 this.connection = connection;
125 }
126
127 /***
128 * {@inheritDoc}
129 */
130 public void setExceptionHandler(final ExceptionHandler exceptionHandler)
131 {
132 this.exceptionHandler = exceptionHandler;
133 }
134
135 /***
136 * {@inheritDoc}
137 */
138 public void setResultSetExtractor(final ResultSetExtractor resultSetExtractor)
139 {
140 this.resultSetExtractor = resultSetExtractor;
141 }
142
143 /***
144 * Sets the name of the table that will be described by this command.
145 *
146 * @param tableName the table name
147 */
148 public void setTableName(final String tableName)
149 {
150 this.tableName = tableName;
151 }
152
153 //----------------------------------------------------------------------------
154 // Protected abstract methods
155 //----------------------------------------------------------------------------
156
157 //----------------------------------------------------------------------------
158 // Protected methods for use by subclasses
159 //----------------------------------------------------------------------------
160
161 //----------------------------------------------------------------------------
162 // Other methods
163 //----------------------------------------------------------------------------
164
165 //----------------------------------------------------------------------------
166 // Member classes
167 //----------------------------------------------------------------------------
168
169 }