01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.sql.internal.rowset;
18:
19: import java.sql.ResultSet;
20: import java.sql.ResultSetMetaData;
21: import java.sql.SQLException;
22: import java.util.ArrayList;
23:
24: import javax.sql.RowSetInternal;
25: import javax.sql.RowSetReader;
26:
27: public class CachedRowSetReader implements RowSetReader {
28:
29: private ResultSet rs;
30:
31: private ResultSetMetaData metadata;
32:
33: public CachedRowSetReader(ResultSet rs) throws SQLException {
34: this .rs = rs;
35: this .metadata = rs.getMetaData();
36: }
37:
38: /**
39: * TODO disable all listeners
40: */
41: public void readData(RowSetInternal theCaller) throws SQLException {
42: CachedRowSetImpl cachedRowSet = (CachedRowSetImpl) theCaller;
43: int pageSize = cachedRowSet.getPageSize();
44: int maxRows = cachedRowSet.getMaxRows();
45:
46: ArrayList<CachedRow> data = new ArrayList<CachedRow>();
47: int columnCount = metadata.getColumnCount();
48:
49: while (rs.next()) {
50: Object[] columnData = new Object[columnCount];
51: for (int i = 0; i < columnCount; i++) {
52: columnData[i] = rs.getObject(i + 1);
53: }
54:
55: data.add(new CachedRow(columnData));
56:
57: if (maxRows > 0 && maxRows == data.size()) {
58: break;
59: }
60:
61: if (pageSize > 0 && data.size() == pageSize) {
62: break;
63: }
64:
65: }
66:
67: cachedRowSet.setRows(data, columnCount);
68: }
69: }
|