/*
 * Copyright 2004-2006 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.seasar.dao.pager;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.seasar.extension.jdbc.impl.ResultSetWrapper;
import org.seasar.framework.exception.SQLRuntimeException;

/**
 * y[WpResultSetbp[B<p>
 * IuWFNgoffsetʒuAlimit܂ł͈̔͂̌ʂ
 * next\bhŕԂ܂B<p>
 * limit-1̏ꍇASĂ̌ʂnext\bhŕԂ܂B
 * 
 * @author Toshitaka Agata(Nulab,inc.)
 */
class PagerResultSetWrapper extends ResultSetWrapper {

    /** O */
    private static final Log log = LogFactory
            .getLog(PagerResultSetWrapper.class);

    /** JEg */
    private int counter = 0;

    /** IWiResultSet */
    private ResultSet original;

    /** IuWFNg */
    private PagerCondition condition;

    /** absolute\bhgp邩ǂ̃tO */
    private boolean useAbsolute = true;

    public void setUseAbsolute(boolean useAbsolute) {
        this.useAbsolute = useAbsolute;
    }

    /**
     * RXgN^
     * @param original IWiResultSet 
     * @param condition IuWFNg
     * @param useAbsolute
     * @throws SQLException
     */
    public PagerResultSetWrapper(ResultSet original, PagerCondition condition,
            boolean useAbsolute) {
        super(original);
        this.original = original;
        this.condition = condition;
        this.useAbsolute = useAbsolute;
        moveOffset();
    }

    /**
     * Jnʒu܂ŃJ[\i߂܂B
     * @throws SQLException
     */
    private void moveOffset() {
        if (isUseCursor()) {
            if (log.isDebugEnabled()) {
                log.debug("[S2Pager]Use scroll cursor.");
            }
            try {
                if (0 == condition.getOffset()) {
                    original.beforeFirst();
                } else {
                    original.absolute(condition.getOffset());
                }
                counter = original.getRow();
            } catch (SQLException e) {
                throw new SQLRuntimeException(e);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("[S2Pager]Not use scroll cursor.");
            }
            try {
                while (original.getRow() < condition.getOffset()
                        && original.next()) {
                    counter++;
                }
            } catch (SQLException e) {
                throw new SQLRuntimeException(e);
            }
        }
    }

    public boolean next() throws SQLException {
        boolean next = super.next();
        if ((condition.getLimit() == PagerCondition.NONE_LIMIT || counter < condition
                .getOffset()
                + condition.getLimit())
                && next) {
            counter += 1;
            return true;
        } else {
            if (isUseCursor()) {
                original.last();
                int count = original.getRow();
                condition.setCount(count);
            } else {
                if (next) {
                    counter++; // 
                    while (original.next()) {
                        counter++;
                    }
                }
                condition.setCount(counter);
            }
            return false;
        }
    }

    private boolean isUseCursor() {
        return useAbsolute && ResultSetUtil.isCursorSupport(original);
    }

}

@@ -733,95 +733,8 @@
 %0D%0A%0D%0A
-import org.apache.commons.logging.Log;%0D%0Aimport org.apache.commons.logging.LogFactory;%0D%0A
 impo
@@ -786,16 +786,16 @@
 apper;%0D%0A
+
 import o
@@ -845,16 +845,57 @@
 eption;%0D
+%0Aimport org.seasar.framework.log.Logger;%0D
 %0A%0D%0A/**%0D%0A
@@ -1154,35 +1154,29 @@
  Log
- log = LogFactory%0D%0A        
+ger LOGGER = Logger%0D%0A
     
@@ -1182,16 +1182,19 @@
  .getLog
+ger
 (PagerRe
@@ -2151,35 +2151,38 @@
             if (
-log
+LOGGER
 .isDebugEnabled(
@@ -2191,39 +2191,39 @@
  %7B%0D%0A            
-    log
+%09LOGGER
 .debug(%22%5BS2Pager
@@ -2670,19 +2670,22 @@
     if (
-log
+LOGGER
 .isDebug
@@ -2714,15 +2714,15 @@
     
-    log
+%09LOGGER
 .deb

change use org.sersar.framework.log.Logger