KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > data > connection > sql > SQLResultSet


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Aug 31, 2005
14  * @author wseyler
15  */

16 package org.pentaho.data.connection.sql;
17
18 import java.sql.ResultSet JavaDoc;
19 import java.sql.SQLException JavaDoc;
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 /**
29  * @author wseyler
30  *
31  * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
32  */

33 public class SQLResultSet implements IPentahoResultSet {
34   ResultSet JavaDoc 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   /**
42    *
43    */

44   public SQLResultSet(ResultSet JavaDoc nativeResultSet, SQLConnection nativeConnection) {
45     super();
46     this.connection = nativeConnection;
47     this.nativeResultSet = nativeResultSet;
48   }
49
50   /*
51    * (non-Javadoc)
52    *
53    * @see org.pentaho.connection.IPentahoResultSet#getMetaData()
54    */

55   public IPentahoMetaData getMetaData() {
56     try {
57       return new SQLMetaData(nativeResultSet.getMetaData());
58     } catch (SQLException JavaDoc e) {
59       // TODO Auto-generated catch block
60
log.error(Messages.getErrorString("SQLResultSet.ERROR_0004_GET_METADATA"), e); //$NON-NLS-1$
61
// e.printStackTrace();
62
throw new RuntimeException JavaDoc(e);
63     }
64     // return null;
65
}
66
67   /*
68    * (non-Javadoc)
69    *
70    * @see org.pentaho.connection.IPentahoResultSet#next() returns null if no more rows
71    */

72   public Object JavaDoc[] next() {
73     try {
74       int columns = nativeResultSet.getMetaData().getColumnCount();
75       if (nativeResultSet.next()) {
76         Object JavaDoc[] row = new Object JavaDoc[columns];
77         for (int column = 0; column < columns; column++) {
78           row[column] = nativeResultSet.getObject(column + 1);
79         }
80         return row;
81       }
82     } catch (SQLException JavaDoc e) {
83       // TODO Auto-generated catch block
84
// TODO surface this error
85
log.error(Messages.getErrorString("SQLResultSet.ERROR_0005_NEXT"), e); //$NON-NLS-1$
86
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 JavaDoc ignored) {
97       }
98     }
99     connection = null;
100   }
101
102   public void close() {
103     if (nativeResultSet != null) {
104       try {
105         nativeResultSet.close();
106       } catch (SQLException JavaDoc e) {
107         // TODO surface this error
108
}
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 JavaDoc ex) {
123       log.warn(Messages.getString("SQLResultSet.WARN_RESULTSET_TYPE_UNDETERMINED")); //$NON-NLS-1$
124
}
125     if (resultSetType == ResultSet.TYPE_FORWARD_ONLY) {
126       return false;
127     } else {
128       return true;
129     }
130   }
131
132   /**
133    * Returns the column count from the result set.
134    *
135    * @return the column count.
136    */

137   public int getColumnCount() {
138     if (columnCount != COUNT_NEVER_OBTAINED) {
139       // We have already calculated column count, return what we have.
140
return columnCount;
141     }
142     if (nativeResultSet == null) {
143       return 0;
144     }
145     try {
146       columnCount = nativeResultSet.getMetaData().getColumnCount();
147       return columnCount;
148     } catch (SQLException JavaDoc ex) {
149       // TODO: Surfase this exception.
150
log.error(Messages.getErrorString("SQLResultSet.ERROR_0006_GET_COLUMNCOUNT"), ex); //$NON-NLS-1$
151
}
152     return 0;
153   }
154
155   /**
156    * Get a rowCount from the resultset. If the resultset
157    *
158    * @return the row count.
159    */

160   public int getRowCount() {
161     if (rowCount != COUNT_NEVER_OBTAINED) {
162       // We have already calculated rowcount, return what we have
163
return rowCount;
164     }
165     // No resultset
166
if (nativeResultSet == null) {
167       return 0;
168     }
169     try {
170       // Get current row in the resultset
171
int curRow = nativeResultSet.getRow();
172       try {
173         // Seek to the end of the resultset. This could be very
174
// bad for performance if the cursor is client-side.
175
if (nativeResultSet.last()) {
176           // Get the rownumber of the last row
177
rowCount = nativeResultSet.getRow();
178           // Boundary case
179
if (rowCount <= 0) {
180             rowCount = 0;
181           }
182         } else {
183           // Couldn't seek to last row - Scrollable resultsets not
184
// supported?
185
// TODO: Possibly throw an exception in this case
186
rowCount = 0;
187         }
188       } finally {
189         // There is no row 0 - if the curRow was 0, go to before the
190
// first row in the resultset
191
if (curRow == 0) {
192           nativeResultSet.beforeFirst();
193         } else {
194           // Go back where we started
195
nativeResultSet.absolute(curRow);
196         }
197       }
198     } catch (SQLException JavaDoc sqle) {
199       log.error(Messages.getErrorString("SQLResultSet.ERROR_0001_OBTAINING_ROWCOUNT"), sqle); //$NON-NLS-1$
200
rowCount = 0;
201     }
202     return rowCount;
203   }
204
205   /**
206    * Returns the value of the specified row and the specified column from within the resultset.
207    *
208    * @param row
209    * the row index.
210    * @param column
211    * the column index.
212    * @return the value.
213    */

214   public Object JavaDoc 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 JavaDoc ex) {
220         log.error(Messages.getErrorString("SQLResultSet.ERROR_0002_GET_VALUE"), ex); //$NON-NLS-1$
221
ex.printStackTrace();
222       }
223     }
224     return null;
225   }
226
227   public IPentahoResultSet memoryCopy() {
228     try {
229       IPentahoMetaData metadata = getMetaData();
230       Object JavaDoc columnHeaders[][] = metadata.getColumnHeaders();
231       MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null);
232       MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData);
233       Object JavaDoc[] 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 JavaDoc e) {
248       log.error(Messages.getErrorString("SQLResultSet.ERROR_0003_BEFORE_FIRST"), e); //$NON-NLS-1$
249
// TODO Auto-generated catch block
250
e.printStackTrace();
251     }
252   }
253
254   public Object JavaDoc[] getDataColumn(int column) {
255     Object JavaDoc[] result = null;
256     result = new Object JavaDoc[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 JavaDoc[] getDataRow(int row) {
264     Object JavaDoc[] rowData = new Object JavaDoc[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