1 package com.quantum.sql;
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.io.UnsupportedEncodingException;
8 import java.sql.Connection;
9 import java.sql.ResultSet;
10 import java.sql.ResultSetMetaData;
11 import java.sql.SQLException;
12 import java.util.ArrayList;
13 import java.util.List;
15 import com.quantum.model.Bookmark;
16 import com.quantum.model.Entity;
22 public class SQLStandardResultSetResults extends SQLResultSetResults implements Scrollable {
25 * Some columns -- especially BLOBS and CLOBS can be very wide. This variable
26 * sets the maximum width of the column.
28 private static final int MAX_COLUMN_WIDTH = 1024 * 2;
30 private boolean hasMore = false;
31 private int start = 1;
32 private int numberOfRowsPerPage;
33 private int totalNumberOfRows = -1;
35 private boolean fullMode = false;
41 * @param numberOfRowsPerPage
43 protected SQLStandardResultSetResults(
44 Bookmark bookmark, String query, Entity entity,
45 int numberOfRowsPerPage) {
46 super(query, bookmark, entity);
47 this.numberOfRowsPerPage = numberOfRowsPerPage;
50 static SQLResultSetResults create(
51 ResultSet set, Bookmark bookmark,
52 String query, Entity entity, int numberOfRows) throws SQLException {
54 SQLStandardResultSetResults results = new SQLStandardResultSetResults(
55 bookmark, query, entity, numberOfRows);
57 results.parseResultSet(set);
68 * @throws SQLException
70 protected void parseResultSet(ResultSet set) throws SQLException {
73 ResultSetMetaData metaData = set.getMetaData();
74 int columnCount = metaData.getColumnCount();
75 List columns = new ArrayList();
76 for (int i = 1; i <= columnCount; i++) {
77 columns.add(new Column(
78 metaData.getColumnName(i),
79 metaData.getColumnTypeName(i),
80 metaData.getColumnDisplaySize(i)));
82 setColumns((Column[]) columns.toArray(new Column[columns.size()]));
84 boolean exitEarly = false;
85 int firstRow = (this.fullMode) ? 0 : this.start;
86 int lastRow = (this.fullMode) ? Integer.MAX_VALUE : this.start + this.numberOfRowsPerPage - 1;
87 List rowList = new ArrayList();
89 boolean disable = this.start < 1 || lastRow < 1;
90 if (disable || ((rowCount >= firstRow) && (rowCount <= lastRow))) {
91 List row = new ArrayList();
92 for (int i = 1, length = columns.size(); i <= length; i++) {
94 if (getColumn(i).getSize() < MAX_COLUMN_WIDTH) {
95 value = getEncodedString(set, getEncoding(), i);
98 if ("".equals(getEncoding())) { //$NON-NLS-1$
99 value = getStringFromCharacterSteam(set, i);
101 value = getEncodedStringFromBinaryStream(set, getEncoding(), i);
103 } catch (IOException e) {
104 value = set.getString(i);
105 } catch (RuntimeException e) {
106 // hack for mysql which doesn't implement
108 value = set.getString(i);
111 if (value == null && !set.wasNull()) {
112 value = set.getString(i);
114 row.add(value == null || set.wasNull() ? "<NULL>" : value); //$NON-NLS-1$
116 rowList.add(new Row(row));
119 if (!disable && (rowCount > lastRow)) {
125 this.hasMore = set.next();
127 this.totalNumberOfRows = Math.max(0, rowCount-1);
128 this.hasMore = false;
130 setRows((Row[]) rowList.toArray(new Row[rowList.size()]));
137 * @param columnNumber
138 * @throws SQLException
139 * @throws IOException
140 * @throws UnsupportedEncodingException
142 private String getEncodedStringFromBinaryStream(ResultSet set, String encoding, int columnNumber)
143 throws SQLException, IOException, UnsupportedEncodingException {
144 InputStream binaryStream = set.getBinaryStream(columnNumber);
145 if (binaryStream != null) {
146 ByteArrayOutputStream baos = new ByteArrayOutputStream();
148 for (int c = binaryStream.read(), count = 0;
149 c >= 0 && count <= MAX_COLUMN_WIDTH;
150 c = binaryStream.read(), count++) {
154 binaryStream.close();
156 return new String(baos.toByteArray(), encoding);
165 * @param columnNumber
166 * @throws SQLException
167 * @throws IOException
169 private String getStringFromCharacterSteam(ResultSet set, int columnNumber)
170 throws SQLException, IOException {
171 Reader reader = set.getCharacterStream(columnNumber);
172 if (reader != null) {
173 StringBuffer buffer = new StringBuffer();
174 int retVal = reader.read();
176 while (retVal >= 0) {
177 buffer.append((char) retVal);
178 retVal = reader.read();
180 if (count > MAX_COLUMN_WIDTH) {
181 buffer.append("...>>>"); //$NON-NLS-1$
186 return buffer.toString();
198 * @throws SQLException
200 private String getEncodedString(ResultSet set, String encoding, int index)
201 throws SQLException {
203 return encoding == null || encoding.trim().length() == 0
204 ? set.getString(index)
205 : new String(set.getBytes(index), encoding);
206 } catch (UnsupportedEncodingException e) {
207 return set.getString(index);
211 public boolean hasMore() {
215 public int getTotalNumberOfRows() {
216 return this.totalNumberOfRows;
218 public void nextPage(Connection connection) throws SQLException {
220 this.start += this.numberOfRowsPerPage;
226 * @see com.quantum.sql.Scrollable#previousPage(java.sql.Connection)
228 public void previousPage(Connection connection) throws SQLException {
229 if (hasPreviousPage()) {
230 this.start = Math.max(1, this.start - this.numberOfRowsPerPage);
236 * @see com.quantum.sql.Scrollable#hasNextPage()
238 public boolean hasNextPage() {
243 * @see com.quantum.sql.Scrollable#hasPreviousPage()
245 public boolean hasPreviousPage() {
246 return this.start > 1;
249 public void setFullMode(boolean fullMode) {
250 this.fullMode = fullMode;
252 public boolean isFullMode() {
253 return this.fullMode;
257 * @see com.quantum.sql.Scrollable#getStart()
259 public int getStart() {
260 return getRowCount() == 0 ? 0 : (this.fullMode ? 1 : this.start);
264 * @see com.quantum.sql.Scrollable#getEnd()
266 public int getEnd() {
269 : this.start + getRowCount() - 1;
273 * @see com.quantum.sql.Scrollable#getLast()
275 public int getLast() {
276 return this.totalNumberOfRows;
280 public void setFilterSort(FilterSort filterSort) {
281 super.setFilterSort(filterSort);