| 1 |
/** |
| 2 |
* ======================================== |
| 3 |
* JFreeReport : a free Java report library |
| 4 |
* ======================================== |
| 5 |
* |
| 6 |
* Project Info: http://reporting.pentaho.org/ |
| 7 |
* |
| 8 |
* (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or modify it under the terms |
| 11 |
* of the GNU Lesser General Public License as published by the Free Software Foundation; |
| 12 |
* either version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* See the GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License along with this |
| 19 |
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
* [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 23 |
* in the United States and other countries.] |
| 24 |
* |
| 25 |
* ------------ |
| 26 |
* $Id: SQLReportData.java 10756 2009-12-02 15:58:24Z tmorgner $ |
| 27 |
* ------------ |
| 28 |
* (C) Copyright 2000-2005, by Object Refinery Limited. |
| 29 |
* (C) Copyright 2005-2007, by Pentaho Corporation. |
| 30 |
*/ |
| 31 |
package org.jfree.report.modules.data.sql; |
| 32 |
|
| 33 |
import java.sql.ResultSet; |
| 34 |
import java.sql.ResultSetMetaData; |
| 35 |
import java.sql.SQLException; |
| 36 |
|
| 37 |
import org.jfree.report.DataSourceException; |
| 38 |
import org.jfree.report.ReportData; |
| 39 |
|
| 40 |
/** |
| 41 |
* Creation-Date: 19.02.2006, 17:37:42 |
| 42 |
* |
| 43 |
* @author Thomas Morgner |
| 44 |
*/ |
| 45 |
public class SQLReportData implements ReportData |
| 46 |
{ |
| 47 |
private ResultSet resultSet; |
| 48 |
private int rowCount; |
| 49 |
private int columnCount; |
| 50 |
private int cursor; |
| 51 |
private String[] columnNames; |
| 52 |
private boolean labelMapping; |
| 53 |
|
| 54 |
public SQLReportData(final ResultSet resultSet, |
| 55 |
final boolean labelMapping) |
| 56 |
throws SQLException, DataSourceException |
| 57 |
{ |
| 58 |
if (resultSet == null) |
| 59 |
{ |
| 60 |
throw new NullPointerException(); |
| 61 |
} |
| 62 |
if (resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) |
| 63 |
{ |
| 64 |
throw new IllegalArgumentException(); |
| 65 |
} |
| 66 |
this.resultSet = resultSet; |
| 67 |
this.labelMapping = labelMapping; |
| 68 |
|
| 69 |
if (resultSet.last()) |
| 70 |
{ |
| 71 |
rowCount = resultSet.getRow(); |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
rowCount = 0; |
| 76 |
} |
| 77 |
|
| 78 |
final ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); |
| 79 |
columnCount = resultSetMetaData.getColumnCount(); |
| 80 |
columnNames = new String[columnCount]; |
| 81 |
for (int i = 1; i <= columnCount; i++) |
| 82 |
{ |
| 83 |
if (labelMapping) |
| 84 |
{ |
| 85 |
columnNames[i - 1] = resultSetMetaData.getColumnLabel(i); |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
columnNames[i - 1] = resultSetMetaData.getColumnName(i); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (resultSet.first() == false) |
| 94 |
{ |
| 95 |
throw new DataSourceException("Unable to reset the dataset."); |
| 96 |
} |
| 97 |
cursor = 0; |
| 98 |
} |
| 99 |
|
| 100 |
public boolean isLabelMapping() |
| 101 |
{ |
| 102 |
return labelMapping; |
| 103 |
} |
| 104 |
|
| 105 |
public int getRowCount() throws DataSourceException |
| 106 |
{ |
| 107 |
return rowCount; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* This operation checks, whether a call to next will be likely to succeed. If |
| 112 |
* there is a next data row, this should return true. |
| 113 |
* |
| 114 |
* @return |
| 115 |
* @throws org.jfree.report.DataSourceException |
| 116 |
* |
| 117 |
*/ |
| 118 |
public boolean isAdvanceable() throws DataSourceException |
| 119 |
{ |
| 120 |
return cursor < rowCount; |
| 121 |
} |
| 122 |
|
| 123 |
public int getColumnCount() throws DataSourceException |
| 124 |
{ |
| 125 |
return columnCount; |
| 126 |
} |
| 127 |
|
| 128 |
public boolean setCursorPosition(final int row) throws DataSourceException |
| 129 |
{ |
| 130 |
if (row < 0) |
| 131 |
{ |
| 132 |
throw new DataSourceException("Negative row number is not valid"); |
| 133 |
} |
| 134 |
if (row > rowCount) |
| 135 |
{ |
| 136 |
return false; |
| 137 |
// throw new DataSourceException("OutOfBounds:"); |
| 138 |
} |
| 139 |
|
| 140 |
try |
| 141 |
{ |
| 142 |
if (cursor == 0) |
| 143 |
{ |
| 144 |
resultSet.beforeFirst(); |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
if (resultSet.absolute(row)) |
| 149 |
{ |
| 150 |
cursor = row; |
| 151 |
return true; |
| 152 |
} |
| 153 |
return false; |
| 154 |
//throw new DataSourceException("Unable to scroll the resultset."); |
| 155 |
} |
| 156 |
catch (SQLException e) |
| 157 |
{ |
| 158 |
throw new DataSourceException("Failed to move the cursor: ", e); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public boolean next() throws DataSourceException |
| 163 |
{ |
| 164 |
try |
| 165 |
{ |
| 166 |
if (resultSet.next()) |
| 167 |
{ |
| 168 |
cursor += 1; |
| 169 |
return true; |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
return false; |
| 174 |
} |
| 175 |
} |
| 176 |
catch (SQLException e) |
| 177 |
{ |
| 178 |
throw new DataSourceException("Failed to move the cursor: ", e); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public void close() throws DataSourceException |
| 183 |
{ |
| 184 |
try |
| 185 |
{ |
| 186 |
resultSet.close(); |
| 187 |
} |
| 188 |
catch (SQLException e) |
| 189 |
{ |
| 190 |
throw new DataSourceException("Failed to close the resultset: ", e); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public String getColumnName(final int column) throws DataSourceException |
| 195 |
{ |
| 196 |
return columnNames[column]; |
| 197 |
} |
| 198 |
|
| 199 |
public Object get(final int column) throws DataSourceException |
| 200 |
{ |
| 201 |
if (isReadable() == false) |
| 202 |
{ |
| 203 |
throw new DataSourceException("Cannot read from this datasource"); |
| 204 |
} |
| 205 |
|
| 206 |
try |
| 207 |
{ |
| 208 |
final Object retval = resultSet.getObject(column + 1); |
| 209 |
if (resultSet.wasNull()) |
| 210 |
{ |
| 211 |
return null; |
| 212 |
} |
| 213 |
return retval; |
| 214 |
} |
| 215 |
catch (SQLException e) |
| 216 |
{ |
| 217 |
throw new DataSourceException("Failed to query data", e); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
public int getCursorPosition() throws DataSourceException |
| 222 |
{ |
| 223 |
return cursor; |
| 224 |
} |
| 225 |
|
| 226 |
public boolean isReadable() throws DataSourceException |
| 227 |
{ |
| 228 |
return cursor > 0 && rowCount > 0; |
| 229 |
} |
| 230 |
} |