1 52 package org.opencrx.kernel.tools; 53 54 import java.sql.ResultSet ; 55 import java.sql.ResultSetMetaData ; 56 import java.sql.SQLException ; 57 import java.util.ArrayList ; 58 import java.util.HashMap ; 59 import java.util.List ; 60 import java.util.Map ; 61 62 70 public class FastResultSet { 71 72 public FastResultSet( 74 ResultSet rs 75 ) throws SQLException { 76 this.rs = rs; 77 this.columnNames = FastResultSet.getColumnNames( 78 rs.getMetaData() 79 ); 80 } 81 82 public FastResultSet( 84 ResultSet rs, 85 List columnNames 86 ) throws SQLException { 87 this.rs = rs; 88 this.columnNames = columnNames; 89 } 90 91 static public List getColumnNames( 93 ResultSetMetaData rsmd 94 ) throws SQLException { 95 List columnNames = new ArrayList (); 96 for(int i = 0; i < rsmd.getColumnCount(); i++) { 97 columnNames.add( 98 rsmd.getColumnName(i+1).toLowerCase() 99 ); 100 } 101 return columnNames; 102 } 103 104 111 public Object getObject( 112 String columnName 113 ) throws SQLException { 114 String columnNameLowerCase = columnName.toLowerCase(); 115 Object value = this.columnValues.get(columnNameLowerCase); 116 if(value == null) { 117 int index = this.columnNames.indexOf(columnNameLowerCase); 118 if(index < 0) { 119 throw new SQLException ("AbstractDatabase_1: column " + columnName + " not found"); 120 } 121 while(this.currentColumnIndex < index) { 123 this.currentColumnIndex++; 124 this.columnValues.put( 125 this.columnNames.get(this.currentColumnIndex), 126 value = this.rs.getObject(this.currentColumnIndex+1) 127 ); 128 } 129 } 130 return value; 131 } 132 133 public void reset( 135 ) throws SQLException { 136 this.columnValues.clear(); 137 this.currentColumnIndex = -1; 138 } 139 140 public boolean next( 142 ) throws SQLException { 143 boolean hasMore = this.rs.next(); 144 this.reset(); 145 return hasMore; 146 } 147 148 public List getColumnNames( 150 ) { 151 return this.columnNames; 152 } 153 154 private final ResultSet rs; 158 private int currentColumnIndex = -1; 159 private final Map columnValues = new HashMap (); 160 private final List columnNames; 161 } 162 | Popular Tags |