Example usage for java.sql ResultSet absolute

List of usage examples for java.sql ResultSet absolute

Introduction

In this page you can find the example usage for java.sql ResultSet absolute.

Prototype

boolean absolute(int row) throws SQLException;

Source Link

Document

Moves the cursor to the given row number in this ResultSet object.

Usage

From source file:com.github.woonsan.jdbc.jcr.impl.JcrJdbcResultSetTest.java

@Test
public void testUnsupportedOperations() throws Exception {
    Statement statement = getConnection().createStatement();
    ResultSet rs = statement.executeQuery(SQL_EMPS);

    try {/*from   w w  w . j  a  va 2 s. c o m*/
        rs.getWarnings();
        fail();
    } catch (UnsupportedOperationException ignore) {
    }

    try {
        rs.clearWarnings();
        fail();
    } catch (UnsupportedOperationException ignore) {
    }

    try {
        rs.getCursorName();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getObject(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getObject("ename");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.isLast();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.beforeFirst();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.afterLast();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.first();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.last();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.absolute(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.relative(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.previous();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.moveToCurrentRow();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNull(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNull("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBoolean(1, true);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBoolean("col1", true);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateByte(1, (byte) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateByte("col1", (byte) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateShort(1, (short) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateShort("col1", (short) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateInt(1, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateInt("col1", 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateLong(1, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateLong("col1", (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateFloat(1, (float) 0.1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateFloat("col1", (float) 0.1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateDouble(1, 0.1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateDouble("col1", 0.1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBigDecimal(1, new BigDecimal("100000000"));
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBigDecimal("col1", new BigDecimal("100000000"));
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateString(1, "Unknown");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateString("col1", "Unknown");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBytes(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBytes("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateDate(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateDate("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateTime(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateTime("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateTimestamp(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateTimestamp("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream(1, null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream("col1", null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream(1, null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream("col1", null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream(1, null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream("col1", null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateObject(1, null, 1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateObject("col1", null, 1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateObject(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateObject("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.insertRow();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateRow();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.deleteRow();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.refreshRow();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.cancelRowUpdates();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.moveToInsertRow();
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getObject(1, (Map<String, Class<?>>) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getObject("col1", (Map<String, Class<?>>) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getRef(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getRef("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getBlob(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getBlob("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getClob(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getClob("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getURL(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getURL("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateRef(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateRef("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBlob(1, (Blob) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBlob("col1", (Blob) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateClob(1, (Clob) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateClob("col1", (Clob) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateArray(1, (Array) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateArray("col1", (Array) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getRowId(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getRowId("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateRowId(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateRowId("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNString(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNString("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNClob(1, (NClob) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNClob("col1", (NClob) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getNClob(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getNClob("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getSQLXML(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getSQLXML("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateSQLXML(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateSQLXML("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getNString(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getNString("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getNCharacterStream(1);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getNCharacterStream("col1");
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNCharacterStream(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNCharacterStream(1, null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNCharacterStream("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNCharacterStream("col1", null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream(1, null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream("col1", null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream(1, null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream("col1", null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream(1, null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream("col1", null, (long) 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBlob(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBlob("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateClob(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateClob("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNClob(1, null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNClob("col1", null, 0);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNCharacterStream(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNCharacterStream("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateAsciiStream("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBinaryStream("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream(1, null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateCharacterStream("col1", null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBlob(1, (InputStream) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateBlob("col1", (InputStream) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateClob(1, (Reader) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateClob("col1", (Reader) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNClob(1, (Reader) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.updateNClob("col1", (Reader) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getObject(1, (Class<?>) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    try {
        rs.getObject("col1", (Class<?>) null);
        fail();
    } catch (SQLFeatureNotSupportedException ignore) {
    }

    rs.close();
    assertTrue(rs.isClosed());

    statement.close();
    assertTrue(statement.isClosed());
}

From source file:cc.aileron.dao.db.G2DaoImpl.java

@Override
public <C> G2DaoWhere<T> where(final C condition, final Class<C> wtype) {
    return new G2DaoWhere<T>() {
        @Override/*  ww w . j a  v a 2  s  .c o  m*/
        public int delete() {
            final Number result = G2DaoImpl.this.execute(DELETE, null, condition, wtype);
            return result != null ? result.intValue() : 0;
        }

        @Override
        public void execute() {
            G2DaoImpl.this.execute(EXECUTE, null, condition, wtype);
        }

        @Override
        public G2DaoFinder<T> find() {
            final PojoAccessorManager manager = G2DaoImpl.this.manager;
            final Provider<PojoAccessor<T>> provider = G2DaoImpl.this.provider;
            return new G2DaoFinder<T>() {
                @Override
                public void bind(final T object) {
                    if (object == null) {
                        throw new IllegalArgumentException("bindable object is null");
                    }
                    final PojoAccessor<T> accessor = manager.from(object);
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            if (!resultSet.next()) {
                                return;
                            }
                            bind(accessor, columnNames, resultSet);
                        }
                    });
                }

                @Override
                public int count() {
                    final ObjectContainer<Integer> c = new ObjectContainer<Integer>(0);
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException {
                            if (resultSet.next()) {
                                c.value = resultSet.getInt(1);
                            }
                        }

                        @Override
                        public G2DaoMethod method() {
                            return G2DaoMethod.COUNT;
                        }
                    });
                    return c.value;
                }

                @Override
                public void each(final Procedure<T> procedure) {
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            while (resultSet.next()) {
                                final PojoAccessor<T> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                final T target = accessor.toTarget();
                                procedure.call(target);
                            }
                        }

                        @Override
                        public boolean isEach() {
                            return true;
                        }
                    });
                }

                @Override
                public void each(final Procedure<T> procedure, final G2DaoPaging paging) {
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            final int offset = paging.offset();
                            final int size = paging.limit();

                            int i = 0;
                            boolean isNext = offset == 0 ? resultSet.next() : resultSet.absolute(offset);
                            while (isNext) {
                                i += 1;

                                if (size < i) {
                                    resultSet.close();
                                    break;
                                }

                                final PojoAccessor<T> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                final T target = accessor.toTarget();
                                procedure.call(target);

                                isNext = resultSet.next();
                            }
                        }
                    });
                }

                @Override
                public boolean exist() {
                    final ObjectContainer<Boolean> c = new ObjectContainer<Boolean>(false);
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException {
                            c.value = resultSet.next();
                        }
                    });
                    return c.value;
                }

                @Override
                public List<T> list() {
                    final ObjectContainer<List<T>> c = new ObjectContainer<List<T>>(new ArrayList<T>(50));
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            while (resultSet.next()) {
                                final PojoAccessor<T> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                c.value.add(accessor.toTarget());
                            }
                        }
                    });
                    return c.value;
                }

                @Override
                public List<T> list(final G2DaoPaging paging) {
                    final ObjectContainer<List<T>> c = new ObjectContainer<List<T>>(new ArrayList<T>(50));
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            final int offset = paging.offset();
                            final int size = paging.limit();

                            int i = 0;
                            boolean isNext = offset == 0 ? resultSet.next() : resultSet.absolute(offset);

                            while (isNext) {
                                i += 1;

                                if (size < i) {
                                    resultSet.close();
                                    break;
                                }

                                final PojoAccessor<T> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                c.value.add(accessor.toTarget());

                                isNext = resultSet.next();
                            }
                        }
                    });
                    return c.value;
                }

                @Override
                public T one() {
                    final String key;
                    if (isCacheable) {
                        final StringBuilder buff = new StringBuilder();

                        for (final Method method : (wtype != null ? wtype
                                : condition.getClass().getInterfaces()[0]).getDeclaredMethods()) {
                            try {
                                final Object v = method.invoke(condition);
                                buff.append(v).append('\0');
                            } catch (final IllegalArgumentException e) {
                                throw new Error(e);
                            } catch (final IllegalAccessException e) {
                                throw new Error(e);
                            } catch (final InvocationTargetException e) {
                                throw new Error(e);
                            }
                        }

                        key = buff.toString();
                        final T val = Cast.<T>cast(cache.get(key));
                        if (val != null) {
                            return val;
                        }
                    } else {
                        key = null;
                    }

                    final ObjectContainer<T> c = new ObjectContainer<T>();
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final PojoAccessor<T> accessor = provider.get();
                            final List<String> columnNames = getColumnNames(resultSet);
                            if (!resultSet.next()) {
                                return;
                            }
                            bind(accessor, columnNames, resultSet);
                            c.value = accessor.toTarget();
                        }
                    });
                    if (isCacheable) {
                        cache.put(key, c.value);
                    }
                    return c.value;
                }

            };
        }

        @Override
        public <R> G2DaoFinder<R> find(final Factory<R, ? super T> factory) {
            final PojoAccessorManager manager = G2DaoImpl.this.manager;
            final Provider<PojoAccessor<R>> provider = new Provider<PojoAccessor<R>>() {
                @Override
                public PojoAccessor<R> get() {
                    return manager.from(factory.get(G2DaoImpl.this.provider.get().toTarget()));
                }
            };
            return new G2DaoFinder<R>() {
                @Override
                public void bind(final R object) {
                    if (object == null) {
                        throw new IllegalArgumentException("bindable object is null");
                    }
                    final PojoAccessor<R> accessor = manager.from(object);
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            if (!resultSet.next()) {
                                return;
                            }
                            bind(accessor, columnNames, resultSet);
                        }
                    });
                }

                @Override
                public int count() {
                    final ObjectContainer<Integer> c = new ObjectContainer<Integer>(0);
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException {
                            if (resultSet.next()) {
                                c.value = resultSet.getInt(1);
                            }
                        }

                        @Override
                        public G2DaoMethod method() {
                            return G2DaoMethod.COUNT;
                        }
                    });
                    return c.value;
                }

                @Override
                public void each(final Procedure<R> procedure) {
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            while (resultSet.next()) {
                                final PojoAccessor<R> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                final R target = accessor.toTarget();
                                procedure.call(target);
                            }
                        }
                    });
                }

                @Override
                public void each(final Procedure<R> procedure, final G2DaoPaging paging) {
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            final int offset = paging.offset();
                            final int size = paging.limit();

                            int i = 0;
                            boolean isNext = offset == 0 ? resultSet.next() : resultSet.absolute(offset);
                            while (isNext) {
                                i += 1;

                                if (size < i) {
                                    resultSet.close();
                                    break;
                                }

                                final PojoAccessor<R> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                final R target = accessor.toTarget();
                                procedure.call(target);

                                isNext = resultSet.next();
                            }
                        }
                    });
                }

                @Override
                public boolean exist() {
                    final ObjectContainer<Boolean> c = new ObjectContainer<Boolean>(false);
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException {
                            c.value = resultSet.next();
                        }
                    });
                    return c.value;
                }

                @Override
                public List<R> list() {
                    final ObjectContainer<List<R>> c = new ObjectContainer<List<R>>(new ArrayList<R>(50));
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            while (resultSet.next()) {
                                final PojoAccessor<R> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                c.value.add(accessor.toTarget());
                            }
                        }
                    });
                    return c.value;
                }

                @Override
                public List<R> list(final G2DaoPaging paging) {
                    final ObjectContainer<List<R>> c = new ObjectContainer<List<R>>(new ArrayList<R>(50));
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final List<String> columnNames = getColumnNames(resultSet);
                            final int offset = paging.offset();
                            final int size = paging.limit();

                            int i = 0;
                            boolean isNext = offset == 0 ? resultSet.next() : resultSet.absolute(offset);

                            while (isNext) {
                                i += 1;

                                if (size < i) {
                                    resultSet.close();
                                    break;
                                }

                                final PojoAccessor<R> accessor = provider.get();
                                bind(accessor, columnNames, resultSet);
                                c.value.add(accessor.toTarget());

                                isNext = resultSet.next();
                            }
                        }
                    });
                    return c.value;
                }

                @Override
                public R one() {
                    final String key;
                    if (isCacheable) {
                        final StringBuilder buff = new StringBuilder();
                        for (final Method method : (wtype != null ? wtype
                                : condition.getClass().getInterfaces()[0]).getDeclaredMethods()) {
                            try {
                                buff.append(method.invoke(condition).toString()).append('\0');
                            } catch (final IllegalArgumentException e) {
                                throw new Error(e);
                            } catch (final IllegalAccessException e) {
                                throw new Error(e);
                            } catch (final InvocationTargetException e) {
                                throw new Error(e);
                            }
                        }
                        key = buff.toString();
                        final R val = Cast.<R>cast(cache.get(key));
                        if (val != null) {
                            return val;
                        }
                    } else {
                        key = null;
                    }

                    final ObjectContainer<R> c = new ObjectContainer<R>();
                    find(wtype, condition, new G2DaoResultHandler() {
                        @Override
                        public void execute(final ResultSet resultSet) throws SQLException,
                                PojoAccessorValueNotFoundException, PojoPropertiesNotFoundException {
                            final PojoAccessor<R> accessor = provider.get();
                            final List<String> columnNames = getColumnNames(resultSet);
                            if (!resultSet.next()) {
                                return;
                            }
                            bind(accessor, columnNames, resultSet);
                            c.value = accessor.toTarget();
                        }
                    });
                    if (isCacheable) {
                        cache.put(key, c.value);
                    }
                    return c.value;
                }

            };
        }

        @Override
        public long insert(final T value) {
            final Number result = G2DaoImpl.this.execute(INSERT, value, condition, wtype);
            return result != null ? result.longValue() : 0;
        }

        @Override
        public int update(final T value) {
            final Number result = G2DaoImpl.this.execute(UPDATE, value, condition, wtype);
            return result != null ? result.intValue() : 0;
        }

        /**
         * @param invoker
         * @param condition
         */
        void find(final Class<?> wtype, final Object condition, final G2DaoResultHandler invoker) {
            if (condition == null) {
                return;
            }
            final Connection connection = transactionManager.get();
            try {
                executor.execute(transactionManager.db(), connection, wtype, condition, invoker);
            } catch (final Throwable e) {
                throw new Error("G2Dao [" + type.getName() + "] find by "
                        + (condition == G2DaoNoCondition.NO_CONDITION ? "all" : sqlName.get(condition, wtype)),
                        e);
            } finally {
                transactionManager.close(connection);
            }
        }
    };
}

From source file:nl.nn.adapterframework.jdbc.JdbcQuerySenderBase.java

protected String executeSelectQuery(PreparedStatement statement, Object blobSessionVar, Object clobSessionVar,
        HttpServletResponse response, String contentType, String contentDisposition) throws SenderException {
    ResultSet resultset = null;
    try {//from w  ww . j  av  a  2 s .  c o m
        if (getMaxRows() > 0) {
            statement.setMaxRows(getMaxRows() + (getStartRow() > 1 ? getStartRow() - 1 : 0));
        }

        log.debug(getLogPrefix() + "executing a SELECT SQL command");
        resultset = statement.executeQuery();

        if (getStartRow() > 1) {
            resultset.absolute(getStartRow() - 1);
            log.debug(getLogPrefix() + "Index set at position: " + resultset.getRow());
        }
        return getResult(resultset, blobSessionVar, clobSessionVar, response, contentType, contentDisposition);
    } catch (SQLException sqle) {
        throw new SenderException(getLogPrefix() + "got exception executing a SELECT SQL command", sqle);
    } catch (JdbcException e) {
        throw new SenderException(getLogPrefix() + "got exception executing a SELECT SQL command", e);
    } catch (IOException e) {
        throw new SenderException(getLogPrefix() + "got exception executing a SELECT SQL command", e);
    } catch (JMSException e) {
        throw new SenderException(getLogPrefix() + "got exception executing a SELECT SQL command", e);
    } finally {
        try {
            if (resultset != null) {
                resultset.close();
            }
        } catch (SQLException e) {
            log.warn(new SenderException(getLogPrefix() + "got exception closing resultset", e));
        }
    }
}

From source file:org.alinous.plugin.derby.DerbyDataSource.java

private void fetchWithOffset(ResultSet rs, ResultSetMetaData metaData, List<Record> retList,
        LimitOffsetClause limit, PostContext context, VariableRepository provider, AdjustWhere adjWhere,
        TypeHelper helper) throws ExecutionException, SQLException {
    ISQLStatement limitStmt = limit.getLimit();
    ISQLStatement offsetStmt = limit.getOffset();

    int nLimit = 0, nOffset = 0;
    if (limitStmt != null && limitStmt.isReady(context, provider, adjWhere)) {
        String str = limitStmt.extract(context, provider, adjWhere, null, helper);
        nLimit = Integer.parseInt(str);
    }//w  ww  .j  av  a2  s. c  o m
    if (offsetStmt != null && offsetStmt.isReady(context, provider, adjWhere)) {
        String str = offsetStmt.extract(context, provider, adjWhere, null, helper);
        nOffset = Integer.parseInt(str);
    }

    if (offsetStmt != null) {
        rs.absolute(nOffset);
    }

    int count = 0;
    while (rs.next()) {
        if (count >= nLimit) {
            break;
        }
        count++;

        int cnt = metaData.getColumnCount();
        Record rec = new Record();
        for (int i = 0; i < cnt; i++) {
            String colName = metaData.getColumnName(i + 1).toUpperCase();
            String value = rs.getString(i + 1);

            int colType = metaData.getColumnType(i + 1);
            rec.addFieldValue(colName, value, colType);
        }

        retList.add(rec);
    }

}

From source file:org.apache.ibatis.executor.resultset.FastResultSetHandler.java

protected void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
    if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
        if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET) {
            rs.absolute(rowBounds.getOffset());
        }//from   w  ww .  j  a va2s .co  m
    } else {
        for (int i = 0; i < rowBounds.getOffset(); i++)
            rs.next();
    }
}

From source file:org.eclipse.ecr.core.storage.sql.jdbc.JDBCMapper.java

@Override
public PartialList<Serializable> query(String query, QueryFilter queryFilter, boolean countTotal)
        throws StorageException {
    QueryMaker queryMaker = findQueryMaker(query);
    if (queryMaker == null) {
        throw new StorageException("No QueryMaker accepts query: " + query);
    }//from   w  w  w .  j  a va 2  s .c om
    QueryMaker.Query q = queryMaker.buildQuery(sqlInfo, model, pathResolver, query, queryFilter);

    if (q == null) {
        logger.log("Query cannot return anything due to conflicting clauses");
        return new PartialList<Serializable>(Collections.<Serializable>emptyList(), 0);
    }

    long limit = queryFilter.getLimit();
    long offset = queryFilter.getOffset();

    if (logger.isLogEnabled()) {
        String sql = q.selectInfo.sql;
        if (limit != 0) {
            sql += " -- LIMIT " + limit + " OFFSET " + offset;
        }
        if (countTotal) {
            sql += " -- COUNT TOTAL";
        }
        logger.logSQL(sql, q.selectParams);
    }

    String sql = q.selectInfo.sql;

    if (!countTotal && limit > 0 && sqlInfo.dialect.supportsPaging()) {
        // full result set not needed for counting
        sql += " " + sqlInfo.dialect.getPagingClause(limit, offset);
        limit = 0;
        offset = 0;
    }

    PreparedStatement ps = null;
    try {
        ps = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int i = 1;
        for (Object object : q.selectParams) {
            if (object instanceof Calendar) {
                Calendar cal = (Calendar) object;
                Timestamp ts = new Timestamp(cal.getTimeInMillis());
                ps.setTimestamp(i++, ts, cal); // cal passed for timezone
            } else if (object instanceof String[]) {
                Array array = sqlInfo.dialect.createArrayOf(Types.VARCHAR, (Object[]) object, connection);
                ps.setArray(i++, array);
            } else {
                ps.setObject(i++, object);
            }
        }
        ResultSet rs = ps.executeQuery();

        // limit/offset
        long totalSize = -1;
        boolean available;
        if ((limit == 0) || (offset == 0)) {
            available = rs.first();
            if (!available) {
                totalSize = 0;
            }
            if (limit == 0) {
                limit = -1; // infinite
            }
        } else {
            available = rs.absolute((int) offset + 1);
        }

        Column column = q.selectInfo.whatColumns.get(0);
        List<Serializable> ids = new LinkedList<Serializable>();
        int rowNum = 0;
        while (available && (limit != 0)) {
            Serializable id = column.getFromResultSet(rs, 1);
            ids.add(id);
            rowNum = rs.getRow();
            available = rs.next();
            limit--;
        }

        // total size
        if (countTotal && (totalSize == -1)) {
            if (!available && (rowNum != 0)) {
                // last row read was the actual last
                totalSize = rowNum;
            } else {
                // available if limit reached with some left
                // rowNum == 0 if skipped too far
                rs.last();
                totalSize = rs.getRow();
            }
        }

        if (logger.isLogEnabled()) {
            logger.logIds(ids, countTotal, totalSize);
        }

        return new PartialList<Serializable>(ids, totalSize);
    } catch (Exception e) {
        checkConnectionReset(e);
        throw new StorageException("Invalid query: " + query, e);
    } finally {
        if (ps != null) {
            try {
                closeStatement(ps);
            } catch (SQLException e) {
                log.error("Cannot close connection", e);
            }
        }
    }
}

From source file:org.jobjects.dao.annotation.Manager.java

/**
 * @param min Numero de ligne minimum/*  w w  w  .j  ava2s . c o  m*/
 * @param max Numero de ligne maximum
 * @param wherefields Filtre
 * @param orderfields Ordonancement
 * @return la liste des beans
 * @throws FinderException retourne un exception si il y a une erreur.
 * La liste peut tre vide.
 */
public final List<T> findAll(final int min, final int max, final WhereFields wherefields,
        final OrderFields orderfields) throws FinderException {
    List<T> returnValue = null;
    try {
        returnValue = new ArrayList<T>(); // 26.375

        if (null == sql_findAll) {
            sql_findAll = loadSqlFindAll(usualTable, fields);
        }

        String sql = sql_findAll + getSqlWhereAndOrder(wherefields, orderfields);
        // sql = "SELECT * FROM (SELECT ROWNUM N, P.* FROM (" + sql;
        // sql += ") P WHERE ROWNUM < " + max + ")";
        // sql += "WHERE (N>" + min + ")AND(N<" + max + ")";
        try {
            Connection connection = getConnection();
            try {
                Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                try {
                    ResultSet rs = stmt.executeQuery(sql);
                    rs.absolute(min);
                    stmt.setMaxRows(max);
                    try {
                        while (rs.next()) {
                            T data = entityClass.newInstance();
                            int i = 1;

                            for (Field field : fields) {
                                Annotation[] annotations = field.getAnnotations();
                                for (Annotation annotation : annotations) {
                                    if (annotation instanceof DaoField) {
                                        BeanUtils.setProperty(returnValue, field.getName(), rs.getObject(i++));
                                        break;
                                    }
                                }
                            }

                            returnValue.add(data);
                        }
                    } finally {
                        rs.close();
                    }
                    rs = null;
                } finally {
                    stmt.close();
                }
                stmt = null;
            } finally {
                connection.close();
            }
            connection = null;
        } catch (SQLException sqle) {
            log.error(sql, sqle);
            throw new FinderException(sql, sqle);
        }
    } catch (Exception e) {
        throw new FinderException(e);
    }
    return returnValue;

}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.JDBCMapper.java

@Override
public PartialList<Serializable> query(String query, String queryType, QueryFilter queryFilter, long countUpTo)
        throws StorageException {
    if (dialect.needsPrepareUserReadAcls()) {
        prepareUserReadAcls(queryFilter);
    }/*  w  w w.  j a  v  a 2  s.c  o  m*/
    QueryMaker queryMaker = findQueryMaker(queryType);
    if (queryMaker == null) {
        throw new StorageException("No QueryMaker accepts query: " + queryType + ": " + query);
    }
    QueryMaker.Query q = queryMaker.buildQuery(sqlInfo, model, pathResolver, query, queryFilter);

    if (q == null) {
        logger.log("Query cannot return anything due to conflicting clauses");
        return new PartialList<Serializable>(Collections.<Serializable>emptyList(), 0);
    }
    long limit = queryFilter.getLimit();
    long offset = queryFilter.getOffset();

    if (logger.isLogEnabled()) {
        String sql = q.selectInfo.sql;
        if (limit != 0) {
            sql += " -- LIMIT " + limit + " OFFSET " + offset;
        }
        if (countUpTo != 0) {
            sql += " -- COUNT TOTAL UP TO " + countUpTo;
        }
        logger.logSQL(sql, q.selectParams);
    }

    String sql = q.selectInfo.sql;

    if (countUpTo == 0 && limit > 0 && dialect.supportsPaging()) {
        // full result set not needed for counting
        sql = dialect.addPagingClause(sql, limit, offset);
        limit = 0;
        offset = 0;
    } else if (countUpTo > 0 && dialect.supportsPaging()) {
        // ask one more row
        sql = dialect.addPagingClause(sql, Math.max(countUpTo + 1, limit + offset), 0);
    }

    PreparedStatement ps = null;
    try {
        ps = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int i = 1;
        for (Serializable object : q.selectParams) {
            setToPreparedStatement(ps, i++, object);
        }
        ResultSet rs = ps.executeQuery();
        countExecute();

        // limit/offset
        long totalSize = -1;
        boolean available;
        if ((limit == 0) || (offset == 0)) {
            available = rs.first();
            if (!available) {
                totalSize = 0;
            }
            if (limit == 0) {
                limit = -1; // infinite
            }
        } else {
            available = rs.absolute((int) offset + 1);
        }

        Column column = q.selectInfo.whatColumns.get(0);
        List<Serializable> ids = new LinkedList<Serializable>();
        int rowNum = 0;
        while (available && (limit != 0)) {
            Serializable id = column.getFromResultSet(rs, 1);
            ids.add(id);
            rowNum = rs.getRow();
            available = rs.next();
            limit--;
        }

        // total size
        if (countUpTo != 0 && (totalSize == -1)) {
            if (!available && (rowNum != 0)) {
                // last row read was the actual last
                totalSize = rowNum;
            } else {
                // available if limit reached with some left
                // rowNum == 0 if skipped too far
                rs.last();
                totalSize = rs.getRow();
            }
            if (countUpTo > 0 && totalSize > countUpTo) {
                // the result where truncated we don't know the total size
                totalSize = -2;
            }
        }

        if (logger.isLogEnabled()) {
            logger.logIds(ids, countUpTo != 0, totalSize);
        }

        return new PartialList<Serializable>(ids, totalSize);
    } catch (Exception e) {
        checkConnectionReset(e);
        throw new StorageException("Invalid query: " + query, e);
    } finally {
        if (ps != null) {
            try {
                closeStatement(ps);
            } catch (SQLException e) {
                log.error("Cannot close connection", e);
            }
        }
    }
}

From source file:org.opoo.oqs.core.AbstractQuery.java

/**
 * //from  w w w  .j av a 2 s . co m
 *
 * @param rs ResultSet
 * @throws SQLException
 */
private void advance(ResultSet rs) throws SQLException {
    if (dialect == null) {
        return;
    }

    boolean useLimit = dialect.supportsLimit() && maxResults > 0;
    if (!dialect.supportsLimitOffset() || !useLimit) {
        int firstRow = firstResult;
        if (firstRow > 0) {
            try {
                // we can go straight to the first required row
                rs.absolute(firstRow);
                log.debug("using absolute");
            } catch (Exception e) {
                // we need to step through the rows one row at a time (slow)
                for (int m = 0; m < firstRow; m++) {
                    rs.next();
                }
            }
        }
    }
}

From source file:org.orbisgis.corejdbc.internal.ReadRowSetImpl.java

/**
 * Read the content of the DB near the current row id
 *///from  ww w  .ja v  a 2 s .co  m
protected void refreshRowCache() throws SQLException {
    if (!cache.containsKey(rowId) && rowId > 0 && rowId <= getRowCount()) {
        try (Resource res = resultSetHolder.getResource()) {
            ResultSet rs = res.getResultSet();
            final int columnCount = getColumnCount();
            if (cachedColumnNames == null) {
                cacheColumnNames();
            }
            // Do not use pk if not available or if using indeterminate fetch without filtering
            if (pk_name.isEmpty()) {
                boolean validRow = false;
                if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
                    if (rowId < rs.getRow()) {
                        // If the result set is Forward only, we have to re-execute the request in order to read the row
                        resultSetHolder.close();
                        res.close();
                        try (Resource res2 = resultSetHolder.getResource()) {
                            rs = res2.getResultSet();
                        }
                    }
                    while (rs.getRow() < rowId) {
                        validRow = rs.next();
                    }
                } else {
                    validRow = rs.absolute((int) rowId);
                }
                if (validRow) {
                    Object[] row = new Object[columnCount];
                    for (int idColumn = 1; idColumn <= columnCount; idColumn++) {
                        Object obj = rs.getObject(idColumn);
                        if (obj instanceof Clob) {
                            Clob clob = (Clob) obj;
                            obj = clob.getSubString(1, (int) clob.length());
                        }
                        row[idColumn - 1] = obj;
                    }
                    cache.put(rowId, new Row(row, null));
                }
            } else {
                // Fetch block pk of current row
                final int targetBatch = (int) (rowId - 1) / fetchSize;
                if (currentBatchId != targetBatch) {
                    if (targetBatch >= rowFetchFirstPk.size()
                            || (targetBatch != 0 && rowFetchFirstPk.get(targetBatch) == null)) {
                        // For optimisation sake
                        // Like binary search if the gap of target batch is too wide, require average PK values
                        int topBatchCount = getBatchCount();
                        int lowerBatchCount = 0;
                        int intermediateBatchFetching = 0;
                        while (lowerBatchCount + ((topBatchCount - lowerBatchCount) / 2) != targetBatch
                                && intermediateBatchFetching < MAX_INTERMEDIATE_BATCH) {
                            int midleBatchTarget = lowerBatchCount + ((topBatchCount - lowerBatchCount) / 2);
                            if (targetBatch < midleBatchTarget) {
                                topBatchCount = midleBatchTarget;
                            } else {
                                if (midleBatchTarget >= rowFetchFirstPk.size()
                                        || rowFetchFirstPk.get(midleBatchTarget) == null) {
                                    fetchBatchPk(midleBatchTarget);
                                }
                                intermediateBatchFetching++;
                                lowerBatchCount = midleBatchTarget;
                            }
                        }
                        fetchBatchPk(targetBatch);
                    }
                    // Fetch all data of current batch
                    Long firstPk = fetchBatch(rowFetchFirstPk.get(targetBatch), true, 0);
                    if (firstPk != null) {
                        if (targetBatch + 1 < rowFetchFirstPk.size()) {
                            rowFetchFirstPk.set(targetBatch + 1, firstPk);
                        } else {
                            rowFetchFirstPk.add(firstPk);
                        }
                    }
                    currentBatchId = targetBatch;
                }
                // Ok, still in current batch
                int targetRowInBatch = (int) (rowId - 1) % fetchSize;
                if (targetRowInBatch < currentBatch.size()) {
                    cache.put(rowId, currentBatch.get(targetRowInBatch));
                }
            }
        }
    }
    currentRow = cache.get(rowId);
}