1 16 17 package javax.servlet.jsp.jstl.sql; 18 19 import java.io.Serializable ; 20 import java.sql.ResultSet ; 21 import java.sql.ResultSetMetaData ; 22 import java.sql.SQLException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.SortedMap ; 26 import java.util.TreeMap ; 27 28 39 40 class ResultImpl implements Result, Serializable { 41 private List rowMap; 42 private List rowByIndex; 43 private String [] columnNames; 44 private boolean isLimited; 45 46 58 public ResultImpl(ResultSet rs, int startRow, int maxRows) 59 throws SQLException 60 { 61 rowMap = new ArrayList (); 62 rowByIndex = new ArrayList (); 63 64 ResultSetMetaData rsmd = rs.getMetaData(); 65 int noOfColumns = rsmd.getColumnCount(); 66 67 columnNames = new String [noOfColumns]; 69 for (int i = 1; i <= noOfColumns; i++) { 70 columnNames[i-1] = rsmd.getColumnName(i); 71 } 72 73 for (int i = 0; i < startRow; i++) { 75 rs.next(); 76 } 77 78 int processedRows = 0; 80 while (rs.next()) { 81 if ((maxRows != -1) && (processedRows == maxRows)) { 82 isLimited = true; 83 break; 84 } 85 Object [] columns = new Object [noOfColumns]; 86 SortedMap columnMap = 87 new TreeMap (String.CASE_INSENSITIVE_ORDER); 88 89 for (int i = 1; i <= noOfColumns; i++) { 91 Object value = rs.getObject(i); 92 if (rs.wasNull()) { 93 value = null; 94 } 95 columns[i-1] = value; 96 columnMap.put(columnNames[i-1], value); 97 } 98 rowMap.add(columnMap); 99 rowByIndex.add(columns); 100 processedRows++; 101 } 102 } 103 104 113 public SortedMap [] getRows() { 114 if (rowMap == null) { 115 return null; 116 } 117 118 return (SortedMap []) rowMap.toArray(new SortedMap [0]); 120 } 121 122 123 130 public Object [][] getRowsByIndex() { 131 if (rowByIndex == null) { 132 return null; 133 } 134 135 return (Object [][])rowByIndex.toArray(new Object [0][0]); 137 } 138 139 146 public String [] getColumnNames() { 147 return columnNames; 148 } 149 150 156 public int getRowCount() { 157 if (rowMap == null) { 158 return -1; 159 } 160 return rowMap.size(); 161 } 162 163 168 public boolean isLimitedByMaxRows() { 169 return isLimited; 170 } 171 172 } 173 | Popular Tags |