1 61 62 package org.apache.commons.dbutils; 63 64 import java.lang.reflect.InvocationHandler ; 65 import java.lang.reflect.Method ; 66 import java.sql.ResultSet ; 67 import java.sql.ResultSetMetaData ; 68 import java.sql.SQLException ; 69 import java.util.Arrays ; 70 import java.util.Collections ; 71 import java.util.Iterator ; 72 73 78 public class MockResultSet implements InvocationHandler { 79 80 private ResultSetMetaData metaData = null; 81 82 private Iterator iter = null; 83 84 private Object [] currentRow = null; 85 86 private Boolean wasNull = Boolean.FALSE; 87 88 98 public static ResultSet create( 99 ResultSetMetaData metaData, 100 Object [][] rows) { 101 102 return ProxyFactory.instance().createResultSet( 103 new MockResultSet(metaData, rows)); 104 } 105 106 111 public MockResultSet(ResultSetMetaData metaData, Object [][] rows) { 112 super(); 113 this.metaData = metaData; 114 this.iter = 115 (rows == null) 116 ? Collections.EMPTY_LIST.iterator() 117 : Arrays.asList(rows).iterator(); 118 } 119 120 public Object invoke(Object proxy, Method method, Object [] args) 121 throws Throwable { 122 123 String methodName = method.getName(); 124 125 if (methodName.equals("getMetaData")) { 126 return this.getMetaData(); 127 128 } else if (methodName.equals("next")) { 129 return this.next(); 130 131 } else if (methodName.equals("previous")) { 132 133 } else if (methodName.equals("close")) { 134 135 } else if (methodName.equals("getObject")) { 136 137 if (args[0] instanceof Integer ) { 138 int col = ((Integer ) args[0]).intValue(); 139 return this.getObject(col); 140 141 } else if (args[0] instanceof String ) { 142 return this.getObject((String ) args[0]); 143 } 144 145 } else if (methodName.equals("getString")) { 146 147 if (args[0] instanceof Integer ) { 148 int col = ((Integer ) args[0]).intValue(); 149 return this.getString(col); 150 151 } else if (args[0] instanceof String ) { 152 return this.getString((String ) args[0]); 153 } 154 155 } else if (methodName.equals("wasNull")) { 156 return this.wasNull(); 157 158 } else if (methodName.equals("isLast")) { 159 return this.isLast(); 160 } 161 162 return null; 163 } 164 165 protected Boolean isLast() throws SQLException { 166 return this.iter.hasNext() ? Boolean.FALSE : Boolean.TRUE; 167 } 168 169 174 protected Object getObject(int columnIndex) throws SQLException { 175 Object obj = this.currentRow[columnIndex - 1]; 176 if (obj == null) { 177 this.wasNull = (obj == null) ? Boolean.TRUE : Boolean.FALSE; 178 } 179 180 return obj; 181 } 182 183 protected Object getObject(String columnName) throws SQLException { 184 return this.getObject(this.findColumnIndex(columnName)); 185 } 186 187 192 private int findColumnIndex(String columnName) throws SQLException { 193 for (int i = 0; i < this.currentRow.length; i++) { 194 int c = i + 1; 195 if (this.metaData.getColumnName(c).equalsIgnoreCase(columnName)) { 196 return c; 197 } 198 } 199 200 throw new SQLException (columnName + " is not a valid column name."); 201 } 202 203 208 protected String getString(int columnIndex) throws SQLException { 209 Object obj = this.getObject(columnIndex); 210 return (obj == null) ? null : obj.toString(); 211 } 212 213 protected String getString(String columnName) throws SQLException { 214 Object obj = this.getObject(this.findColumnIndex(columnName)); 215 return (obj == null) ? null : obj.toString(); 216 } 217 218 protected Boolean next() throws SQLException { 219 if (!this.iter.hasNext()) { 220 return Boolean.FALSE; 221 } else { 222 this.currentRow = (Object []) iter.next(); 223 return Boolean.TRUE; 224 } 225 } 226 227 protected ResultSetMetaData getMetaData() throws SQLException { 228 return this.metaData; 229 } 230 231 protected Boolean wasNull() throws SQLException { 232 return this.wasNull; 233 } 234 } | Popular Tags |