001 package com.mockrunner.mock.jdbc;
002
003 import java.sql.ResultSetMetaData;
004 import java.sql.SQLException;
005 import java.sql.Types;
006 import java.util.HashMap;
007 import java.util.Map;
008
009 import com.mockrunner.base.NestedApplicationException;
010
011 /**
012 * Mock implementation of <code>ResultSetMetaData</code>.
013 */
014 public class MockResultSetMetaData implements ResultSetMetaData, Cloneable
015 {
016 private int columnCount;
017 private Map columnDisplaySizeMap;
018 private Map columnTypeMap;
019 private Map precisionMap;
020 private Map scaleMap;
021 private Map isNullableMap;
022 private Map isAutoIncrementMap;
023 private Map isCaseSensitiveMap;
024 private Map isCurrencyMap;
025 private Map isDefinitelyWritableMap;
026 private Map isReadOnlyMap;
027 private Map isSearchableMap;
028 private Map isSignedMap;
029 private Map isWritableMap;
030 private Map catalogNameMap;
031 private Map columnClassNameMap;
032 private Map columnLabelMap;
033 private Map columnNameMap;
034 private Map columnTypeNameMap;
035 private Map schemaNameMap;
036 private Map tableNameMap;
037
038 public MockResultSetMetaData()
039 {
040 columnCount = 0;
041 columnDisplaySizeMap = new HashMap();
042 columnTypeMap = new HashMap();
043 precisionMap = new HashMap();
044 scaleMap = new HashMap();
045 isNullableMap = new HashMap();
046 isAutoIncrementMap = new HashMap();
047 isCaseSensitiveMap = new HashMap();
048 isCurrencyMap = new HashMap();
049 isDefinitelyWritableMap = new HashMap();;
050 isReadOnlyMap = new HashMap();
051 isSearchableMap = new HashMap();
052 isSignedMap = new HashMap();
053 isWritableMap = new HashMap();
054 catalogNameMap = new HashMap();
055 columnClassNameMap = new HashMap();
056 columnLabelMap = new HashMap();
057 columnNameMap = new HashMap();
058 columnTypeNameMap = new HashMap();
059 schemaNameMap = new HashMap();
060 tableNameMap = new HashMap();
061 }
062
063 public void setColumnCount(int columnCount)
064 {
065 this.columnCount = columnCount;
066 }
067
068 public void setColumnDisplaySize(int column, int displaySize)
069 {
070 columnDisplaySizeMap.put(new Integer(column), new Integer(displaySize));
071 }
072
073 public void setColumnType(int column, int columnType)
074 {
075 columnTypeMap.put(new Integer(column), new Integer(columnType));
076 }
077
078 public void setPrecision(int column, int precision)
079 {
080 precisionMap.put(new Integer(column), new Integer(precision));
081 }
082
083 public void setScale(int column, int scale)
084 {
085 scaleMap.put(new Integer(column), new Integer(scale));
086 }
087
088 public void setNullable(int column, int nullable)
089 {
090 isNullableMap.put(new Integer(column), new Integer(nullable));
091 }
092
093 public void setAutoIncrement(int column, boolean autoIncrement)
094 {
095 isAutoIncrementMap.put(new Integer(column), new Boolean(autoIncrement));
096 }
097
098 public void setCaseSensitive(int column, boolean caseSensitive)
099 {
100 isCaseSensitiveMap.put(new Integer(column), new Boolean(caseSensitive));
101 }
102
103 public void setCurrency(int column, boolean currency)
104 {
105 isCurrencyMap.put(new Integer(column), new Boolean(currency));
106 }
107
108 public void setDefinitelyWritable(int column, boolean definitelyWritable)
109 {
110 isDefinitelyWritableMap.put(new Integer(column), new Boolean(definitelyWritable));
111 }
112
113 public void setReadOnly(int column, boolean readOnly)
114 {
115 isReadOnlyMap.put(new Integer(column), new Boolean(readOnly));
116 }
117
118 public void setSearchable(int column, boolean searchable)
119 {
120 isSearchableMap.put(new Integer(column), new Boolean(searchable));
121 }
122
123 public void setSigned(int column, boolean signed)
124 {
125 isSignedMap.put(new Integer(column), new Boolean(signed));
126 }
127
128 public void setWritable(int column, boolean writable)
129 {
130 isWritableMap.put(new Integer(column), new Boolean(writable));
131 }
132
133 public void setCatalogName(int column, String catalogName)
134 {
135 catalogNameMap.put(new Integer(column), catalogName);
136 }
137
138 public void setColumnClassName(int column, String columnClassName)
139 {
140 columnClassNameMap.put(new Integer(column), columnClassName);
141 }
142
143 public void setColumnLabel(int column, String columnLabel)
144 {
145 columnLabelMap.put(new Integer(column), columnLabel);
146 }
147
148 public void setColumnName(int column, String columnName)
149 {
150 columnNameMap.put(new Integer(column), columnName);
151 }
152
153 public void setColumnTypeName(int column, String columnTypeName)
154 {
155 columnTypeNameMap.put(new Integer(column), columnTypeName);
156 }
157
158 public void setSchemaName(int column, String schemaName)
159 {
160 schemaNameMap.put(new Integer(column), schemaName);
161 }
162
163 public void setTableName(int column, String tableName)
164 {
165 tableNameMap.put(new Integer(column), tableName);
166 }
167
168 public int getColumnCount() throws SQLException
169 {
170 return columnCount;
171 }
172
173 public int getColumnDisplaySize(int column) throws SQLException
174 {
175 Integer columnDisplaySize = (Integer)columnDisplaySizeMap.get(new Integer(column));
176 if(null == columnDisplaySize) return getColumnCount();
177 return columnDisplaySize.intValue();
178 }
179
180 public int getColumnType(int column) throws SQLException
181 {
182 Integer columnType = (Integer)columnTypeMap.get(new Integer(column));
183 if(null == columnType) return Types.OTHER;
184 return columnType.intValue();
185 }
186
187 public int getPrecision(int column) throws SQLException
188 {
189 Integer precision = (Integer)precisionMap.get(new Integer(column));
190 if(null == precision) return 0;
191 return precision.intValue();
192 }
193
194 public int getScale(int column) throws SQLException
195 {
196 Integer scale = (Integer)scaleMap.get(new Integer(column));
197 if(null == scale) return 0;
198 return scale.intValue();
199 }
200
201 public int isNullable(int column) throws SQLException
202 {
203 Integer isNullable = (Integer)isNullableMap.get(new Integer(column));
204 if(null == isNullable) return columnNullable;
205 return isNullable.intValue();
206 }
207
208 public boolean isAutoIncrement(int column) throws SQLException
209 {
210 Boolean isAutoIncrement = (Boolean)isAutoIncrementMap.get(new Integer(column));
211 if(null == isAutoIncrement) return false;
212 return isAutoIncrement.booleanValue();
213 }
214
215 public boolean isCaseSensitive(int column) throws SQLException
216 {
217 Boolean isCaseSensitive = (Boolean)isCaseSensitiveMap.get(new Integer(column));
218 if(null == isCaseSensitive) return true;
219 return isCaseSensitive.booleanValue();
220 }
221
222 public boolean isCurrency(int column) throws SQLException
223 {
224 Boolean isCurrency = (Boolean)isCurrencyMap.get(new Integer(column));
225 if(null == isCurrency) return false;
226 return isCurrency.booleanValue();
227 }
228
229 public boolean isDefinitelyWritable(int column) throws SQLException
230 {
231 Boolean isDefinitelyWritable = (Boolean)isDefinitelyWritableMap.get(new Integer(column));
232 if(null == isDefinitelyWritable) return true;
233 return isDefinitelyWritable.booleanValue();
234 }
235
236 public boolean isReadOnly(int column) throws SQLException
237 {
238 Boolean isReadOnly = (Boolean)isReadOnlyMap.get(new Integer(column));
239 if(null == isReadOnly) return false;
240 return isReadOnly.booleanValue();
241 }
242
243 public boolean isSearchable(int column) throws SQLException
244 {
245 Boolean isSearchable = (Boolean)isSearchableMap.get(new Integer(column));
246 if(null == isSearchable) return true;
247 return isSearchable.booleanValue();
248 }
249
250 public boolean isSigned(int column) throws SQLException
251 {
252 Boolean isSigned = (Boolean)isSignedMap.get(new Integer(column));
253 if(null == isSigned) return false;
254 return isSigned.booleanValue();
255 }
256
257 public boolean isWritable(int column) throws SQLException
258 {
259 Boolean isWritable = (Boolean)isWritableMap.get(new Integer(column));
260 if(null == isWritable) return true;
261 return isWritable.booleanValue();
262 }
263
264 public String getCatalogName(int column) throws SQLException
265 {
266 String catalogName = (String)catalogNameMap.get(new Integer(column));
267 if(null == catalogName) return "";
268 return catalogName;
269 }
270
271 public String getColumnClassName(int column) throws SQLException
272 {
273 String columnClassName = (String)columnClassNameMap.get(new Integer(column));
274 if(null == columnClassName) return Object.class.getName();
275 return columnClassName;
276 }
277
278 public String getColumnLabel(int column) throws SQLException
279 {
280 String columnLabel = (String)columnLabelMap.get(new Integer(column));
281 if(null == columnLabel) return getColumnName(column);
282 return columnLabel;
283 }
284
285 public String getColumnName(int column) throws SQLException
286 {
287 String columnName = (String)columnNameMap.get(new Integer(column));
288 if(null == columnName) return "";
289 return columnName;
290 }
291
292 public String getColumnTypeName(int column) throws SQLException
293 {
294 String columnTypeName = (String)columnTypeNameMap.get(new Integer(column));
295 if(null == columnTypeName) return "";
296 return columnTypeName;
297 }
298
299 public String getSchemaName(int column) throws SQLException
300 {
301 String schemaName = (String)schemaNameMap.get(new Integer(column));
302 if(null == schemaName) return "";
303 return schemaName;
304 }
305
306 public String getTableName(int column) throws SQLException
307 {
308 String tableName = (String)tableNameMap.get(new Integer(column));
309 if(null == tableName) return "";
310 return tableName;
311 }
312
313 public Object clone() throws CloneNotSupportedException
314 {
315 try
316 {
317 MockResultSetMetaData copy = (MockResultSetMetaData)super.clone();
318 copy.columnDisplaySizeMap = new HashMap(columnDisplaySizeMap);
319 copy.columnTypeMap = new HashMap(columnTypeMap);
320 copy.precisionMap = new HashMap(precisionMap);
321 copy.scaleMap = new HashMap(scaleMap);
322 copy.isNullableMap = new HashMap(isNullableMap);
323 copy.isAutoIncrementMap = new HashMap(isAutoIncrementMap);
324 copy.isCurrencyMap = new HashMap(isCurrencyMap);
325 copy.isDefinitelyWritableMap = new HashMap(isDefinitelyWritableMap);
326 copy.isReadOnlyMap = new HashMap(isReadOnlyMap);
327 copy.isSearchableMap = new HashMap(isSearchableMap);
328 copy.isSignedMap = new HashMap(isSignedMap);
329 copy.isWritableMap = new HashMap(isWritableMap);
330 copy.catalogNameMap = new HashMap(catalogNameMap);
331 copy.columnClassNameMap = new HashMap(columnClassNameMap);
332 copy.columnLabelMap = new HashMap(columnLabelMap);
333 copy.columnNameMap = new HashMap(columnNameMap);
334 copy.columnTypeNameMap = new HashMap(columnTypeNameMap);
335 copy.schemaNameMap = new HashMap(schemaNameMap);
336 copy.tableNameMap = new HashMap(tableNameMap);
337 return copy;
338 }
339 catch(CloneNotSupportedException exc)
340 {
341 throw new NestedApplicationException(exc);
342 }
343 }
344
345 public boolean isWrapperFor(Class iface) throws SQLException
346 {
347 return false;
348 }
349
350 public Object unwrap(Class iface) throws SQLException
351 {
352 throw new SQLException("No object found for " + iface);
353 }
354 }