1 28 29 package com.caucho.jstl; 30 31 import javax.servlet.jsp.jstl.sql.Result; 32 import java.sql.ResultSet ; 33 import java.sql.ResultSetMetaData ; 34 import java.sql.SQLException ; 35 import java.util.ArrayList ; 36 import java.util.SortedMap ; 37 import java.util.TreeMap ; 38 39 42 public class ResultImpl implements Result { 43 private ArrayList _rows = new ArrayList (); 44 private Object [][]_objectRows; 45 private SortedMap []_sortedRows; 46 private String []_columnNames; 47 private boolean _isLimitedByMaxRows; 48 49 public ResultImpl(ResultSet rs, int maxRows) 50 throws SQLException 51 { 52 ResultSetMetaData metaData = rs.getMetaData(); 53 int columnCount = metaData.getColumnCount(); 54 55 _columnNames = new String [columnCount]; 56 for (int i = 0; i < _columnNames.length; i++) 57 _columnNames[i] = metaData.getColumnName(i + 1); 58 59 if (maxRows < 0) 60 maxRows = Integer.MAX_VALUE; 61 62 while (rs.next() && maxRows-- > 0) { 63 Object []row = new Object [columnCount]; 64 65 for (int i = 0; i < columnCount; i++) 66 row[i] = rs.getObject(i + 1); 67 68 _rows.add(row); 69 } 70 71 if (maxRows == 0) 72 _isLimitedByMaxRows = true; 73 } 74 75 public SortedMap [] getRows() 76 { 77 if (_sortedRows == null) { 78 _sortedRows = new SortedMap [_rows.size()]; 79 80 for (int i = _rows.size() - 1; i >=0; i--) { 81 SortedMap map = new TreeMap (String.CASE_INSENSITIVE_ORDER); 82 _sortedRows[i] = map; 83 Object []row = (Object []) _rows.get(i); 84 85 for (int j = _columnNames.length - 1; j >= 0; j--) 86 map.put(_columnNames[j], row[j]); 87 } 88 } 89 90 return _sortedRows; 91 } 92 93 public Object [][] getRowsByIndex() 94 { 95 if (_objectRows == null) { 96 _objectRows = new Object [_rows.size()][]; 97 _rows.toArray(_objectRows); 98 } 99 100 return _objectRows; 101 } 102 103 public String []getColumnNames() 104 { 105 return _columnNames; 106 } 107 108 public int getRowCount() 109 { 110 return _rows.size(); 111 } 112 113 public boolean isLimitedByMaxRows() 114 { 115 return _isLimitedByMaxRows; 116 } 117 } 118 | Popular Tags |