1 16 package org.pentaho.data.connection.sql; 17 18 import java.sql.ResultSet ; 19 import java.sql.SQLException ; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.pentaho.core.connection.IPentahoMetaData; 23 import org.pentaho.core.connection.IPentahoResultSet; 24 import org.pentaho.core.connection.memory.MemoryMetaData; 25 import org.pentaho.core.connection.memory.MemoryResultSet; 26 import org.pentaho.messages.Messages; 27 28 33 public class SQLResultSet implements IPentahoResultSet { 34 ResultSet nativeResultSet = null; 35 SQLConnection connection; 36 private static final int COUNT_NEVER_OBTAINED = -2; 37 private int rowCount = COUNT_NEVER_OBTAINED; 38 private int columnCount = COUNT_NEVER_OBTAINED; 39 private static final Log log = LogFactory.getLog(SQLResultSet.class); 40 41 44 public SQLResultSet(ResultSet nativeResultSet, SQLConnection nativeConnection) { 45 super(); 46 this.connection = nativeConnection; 47 this.nativeResultSet = nativeResultSet; 48 } 49 50 55 public IPentahoMetaData getMetaData() { 56 try { 57 return new SQLMetaData(nativeResultSet.getMetaData()); 58 } catch (SQLException e) { 59 log.error(Messages.getErrorString("SQLResultSet.ERROR_0004_GET_METADATA"), e); throw new RuntimeException (e); 63 } 64 } 66 67 72 public Object [] next() { 73 try { 74 int columns = nativeResultSet.getMetaData().getColumnCount(); 75 if (nativeResultSet.next()) { 76 Object [] row = new Object [columns]; 77 for (int column = 0; column < columns; column++) { 78 row[column] = nativeResultSet.getObject(column + 1); 79 } 80 return row; 81 } 82 } catch (SQLException e) { 83 log.error(Messages.getErrorString("SQLResultSet.ERROR_0005_NEXT"), e); throw new SQLResultSetException(); 87 } 88 return null; 89 } 90 91 public void closeConnection() { 92 close(); 93 if (connection != null) { 94 try { 95 connection.close(); 96 } catch (Exception ignored) { 97 } 98 } 99 connection = null; 100 } 101 102 public void close() { 103 if (nativeResultSet != null) { 104 try { 105 nativeResultSet.close(); 106 } catch (SQLException e) { 107 } 109 rowCount = COUNT_NEVER_OBTAINED; 110 } 111 nativeResultSet = null; 112 } 113 114 public void dispose() { 115 closeConnection(); 116 } 117 118 public boolean isScrollable() { 119 int resultSetType = ResultSet.TYPE_FORWARD_ONLY; 120 try { 121 resultSetType = nativeResultSet.getType(); 122 } catch (SQLException ex) { 123 log.warn(Messages.getString("SQLResultSet.WARN_RESULTSET_TYPE_UNDETERMINED")); } 125 if (resultSetType == ResultSet.TYPE_FORWARD_ONLY) { 126 return false; 127 } else { 128 return true; 129 } 130 } 131 132 137 public int getColumnCount() { 138 if (columnCount != COUNT_NEVER_OBTAINED) { 139 return columnCount; 141 } 142 if (nativeResultSet == null) { 143 return 0; 144 } 145 try { 146 columnCount = nativeResultSet.getMetaData().getColumnCount(); 147 return columnCount; 148 } catch (SQLException ex) { 149 log.error(Messages.getErrorString("SQLResultSet.ERROR_0006_GET_COLUMNCOUNT"), ex); } 152 return 0; 153 } 154 155 160 public int getRowCount() { 161 if (rowCount != COUNT_NEVER_OBTAINED) { 162 return rowCount; 164 } 165 if (nativeResultSet == null) { 167 return 0; 168 } 169 try { 170 int curRow = nativeResultSet.getRow(); 172 try { 173 if (nativeResultSet.last()) { 176 rowCount = nativeResultSet.getRow(); 178 if (rowCount <= 0) { 180 rowCount = 0; 181 } 182 } else { 183 rowCount = 0; 187 } 188 } finally { 189 if (curRow == 0) { 192 nativeResultSet.beforeFirst(); 193 } else { 194 nativeResultSet.absolute(curRow); 196 } 197 } 198 } catch (SQLException sqle) { 199 log.error(Messages.getErrorString("SQLResultSet.ERROR_0001_OBTAINING_ROWCOUNT"), sqle); rowCount = 0; 201 } 202 return rowCount; 203 } 204 205 214 public Object getValueAt(int row, int column) { 215 if (nativeResultSet != null) { 216 try { 217 nativeResultSet.absolute(row + 1); 218 return nativeResultSet.getObject(column + 1); 219 } catch (SQLException ex) { 220 log.error(Messages.getErrorString("SQLResultSet.ERROR_0002_GET_VALUE"), ex); ex.printStackTrace(); 222 } 223 } 224 return null; 225 } 226 227 public IPentahoResultSet memoryCopy() { 228 try { 229 IPentahoMetaData metadata = getMetaData(); 230 Object columnHeaders[][] = metadata.getColumnHeaders(); 231 MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null); 232 MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData); 233 Object [] rowObjects = next(); 234 while (rowObjects != null) { 235 cachedResultSet.addRow(rowObjects); 236 rowObjects = next(); 237 } 238 return cachedResultSet; 239 } finally { 240 close(); 241 } 242 } 243 244 public void beforeFirst() { 245 try { 246 nativeResultSet.beforeFirst(); 247 } catch (SQLException e) { 248 log.error(Messages.getErrorString("SQLResultSet.ERROR_0003_BEFORE_FIRST"), e); e.printStackTrace(); 251 } 252 } 253 254 public Object [] getDataColumn(int column) { 255 Object [] result = null; 256 result = new Object [getRowCount()]; 257 for (int row = 0; row < result.length; row++) { 258 result[row] = getValueAt(row, column); 259 } 260 return result; 261 } 262 263 public Object [] getDataRow(int row) { 264 Object [] rowData = new Object [this.getColumnCount()]; 265 for (int column = 0; column < rowData.length; column++) { 266 rowData[column] = getValueAt(row, column); 267 } 268 return rowData; 269 } 270 } 271 | Popular Tags |