1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils2.sql;
19
20 import java.lang.reflect.InvocationHandler;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Proxy;
23 import java.sql.ResultSetMetaData;
24 import java.sql.SQLException;
25 import java.sql.Types;
26
27
28
29
30
31
32
33 public class TestResultSetMetaData implements InvocationHandler {
34
35
36
37
38
39
40 public static ResultSetMetaData createProxy() {
41 return TestResultSetMetaData.createProxy(new TestResultSetMetaData());
42 }
43
44
45
46
47
48
49
50 public static ResultSetMetaData createProxy(final InvocationHandler invocationHandler) {
51 final ClassLoader classLoader = ResultSetMetaData.class.getClassLoader();
52 final Class<?>[] interfaces = new Class[] { ResultSetMetaData.class };
53 return (ResultSetMetaData) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
54 }
55
56
57
58
59
60
61 protected String[][] metadata = { { "bigDecimalProperty", java.math.BigDecimal.class.getName() }, { "booleanProperty", Boolean.class.getName() },
62 { "byteProperty", Byte.class.getName() }, { "dateProperty", java.sql.Date.class.getName() }, { "doubleProperty", Double.class.getName() },
63 { "floatProperty", Float.class.getName() }, { "intProperty", Integer.class.getName() }, { "longProperty", Long.class.getName() },
64 { "nullProperty", String.class.getName() }, { "shortProperty", Short.class.getName() }, { "stringProperty", String.class.getName() },
65 { "timeProperty", java.sql.Time.class.getName() }, { "timestampProperty", java.sql.Timestamp.class.getName() }, };
66
67 public String getCatalogName(final int columnIndex) throws SQLException {
68 throw new UnsupportedOperationException();
69 }
70
71 public String getColumnClassName(final int columnIndex) throws SQLException {
72 return metadata[columnIndex - 1][1];
73 }
74
75 public int getColumnCount() throws SQLException {
76 return metadata.length;
77 }
78
79 public int getColumnDisplaySize(final int columnIndex) throws SQLException {
80 throw new UnsupportedOperationException();
81 }
82
83 public String getColumnLabel(final int columnIndex) throws SQLException {
84 throw new UnsupportedOperationException();
85 }
86
87 public String getColumnName(final int columnIndex) throws SQLException {
88 return metadata[columnIndex - 1][0];
89 }
90
91 public Integer getColumnType(final int columnIndex) throws SQLException {
92 final String columnName = getColumnName(columnIndex);
93 int sqlType = Types.OTHER;
94 switch (columnName) {
95 case "bigDecimalProperty":
96 sqlType = Types.DECIMAL;
97
98
99
100 break;
101 case "byteProperty":
102 sqlType = Types.TINYINT;
103 break;
104 case "dateProperty":
105 sqlType = Types.DATE;
106 break;
107 case "doubleProperty":
108 sqlType = Types.DOUBLE;
109 break;
110 case "floatProperty":
111 sqlType = Types.FLOAT;
112 break;
113 case "intProperty":
114 sqlType = Types.INTEGER;
115 break;
116 case "longProperty":
117 sqlType = Types.BIGINT;
118 break;
119 case "nullProperty":
120 sqlType = Types.VARCHAR;
121 break;
122 case "shortProperty":
123 sqlType = Types.SMALLINT;
124 break;
125 case "stringProperty":
126 sqlType = Types.VARCHAR;
127 break;
128 case "timeProperty":
129 sqlType = Types.TIME;
130 break;
131 case "timestampProperty":
132 sqlType = Types.TIMESTAMP;
133 break;
134 default:
135 sqlType = Types.OTHER;
136 break;
137 }
138 return Integer.valueOf(sqlType);
139 }
140
141 public String getColumnTypeName(final int columnIndex) throws SQLException {
142 throw new UnsupportedOperationException();
143 }
144
145 public int getPrecision(final int columnIndex) throws SQLException {
146 throw new UnsupportedOperationException();
147 }
148
149 public int getScale(final int columnIndex) throws SQLException {
150 throw new UnsupportedOperationException();
151 }
152
153 public String getSchemaName(final int columnIndex) throws SQLException {
154 throw new UnsupportedOperationException();
155 }
156
157 public String getTableName(final int columnIndex) throws SQLException {
158 throw new UnsupportedOperationException();
159 }
160
161
162
163
164
165
166
167
168
169
170 @Override
171 public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
172 final String methodName = method.getName();
173 switch (methodName) {
174 case "getColumnClassName":
175 return getColumnClassName(((Integer) args[0]).intValue());
176 case "getColumnCount":
177 return Integer.valueOf(getColumnCount());
178 case "getColumnName":
179 return getColumnName(((Integer) args[0]).intValue());
180 case "getColumnType":
181 return getColumnType(((Integer) args[0]).intValue());
182 default:
183 break;
184 }
185
186 throw new UnsupportedOperationException(methodName + " not implemented");
187 }
188
189 public boolean isAutoIncrement(final int columnIndex) throws SQLException {
190 throw new UnsupportedOperationException();
191 }
192
193 public boolean isCaseSensitive(final int columnIndex) throws SQLException {
194 throw new UnsupportedOperationException();
195 }
196
197 public boolean isCurrency(final int columnIndex) throws SQLException {
198 throw new UnsupportedOperationException();
199 }
200
201 public boolean isDefinitelyWritable(final int columnIndex) throws SQLException {
202 throw new UnsupportedOperationException();
203 }
204
205 public int isNullable(final int columnIndex) throws SQLException {
206 throw new UnsupportedOperationException();
207 }
208
209 public boolean isReadOnly(final int columnIndex) throws SQLException {
210 throw new UnsupportedOperationException();
211 }
212
213 public boolean isSearchable(final int columnIndex) throws SQLException {
214 throw new UnsupportedOperationException();
215 }
216
217 public boolean isSigned(final int columnIndex) throws SQLException {
218 throw new UnsupportedOperationException();
219 }
220
221 public boolean isWritable(final int columnIndex) throws SQLException {
222 throw new UnsupportedOperationException();
223 }
224
225 }