Commit 8752416f authored by terrymanu's avatar terrymanu
Browse files

refactor limit

parent 2f46267e
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ public final class LimitCouplingResultSet extends AbstractDelegateResultSet {
    }
    
    private boolean skipOffset() throws SQLException {
        for (int i = 0; i < limit.getOffset(); i++) {
        for (int i = 0; i < limit.getOffsetValue(); i++) {
            if (!getDelegate().next()) {
                return false;
            }
@@ -62,8 +62,8 @@ public final class LimitCouplingResultSet extends AbstractDelegateResultSet {
    }
    
    private boolean doNext() throws SQLException {
        if (limit.getRowCount() > 0) {
            return ++rowNumber <= limit.getRowCount() && getDelegate().next();
        if (limit.getRowCountValue() > 0) {
            return ++rowNumber <= limit.getRowCountValue() && getDelegate().next();
        }
        return getDelegate().next();
    }
+5 −6
Original line number Diff line number Diff line
@@ -25,8 +25,7 @@ import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.Symbol;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.condition.Column;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.condition.Condition;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.limit.Limit;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.limit.OffsetLimit;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.limit.RowCountLimit;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.limit.LimitValue;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.table.Table;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.table.Tables;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.exception.SQLParsingUnsupportedException;
@@ -350,15 +349,15 @@ public class SQLParser extends AbstractParser {
        }
        if (Symbol.LT == symbol || Symbol.LT_EQ == symbol) {
            if (sqlExpression instanceof SQLNumberExpression) {
                selectStatement.getLimit().setRowCountLimit(new RowCountLimit(((SQLNumberExpression) sqlExpression).getNumber().intValue(), -1));
                selectStatement.getLimit().setRowCountLimit(new LimitValue(((SQLNumberExpression) sqlExpression).getNumber().intValue(), -1));
            } else if (sqlExpression instanceof SQLPlaceholderExpression) {
                selectStatement.getLimit().setRowCountLimit(new RowCountLimit(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex()));
                selectStatement.getLimit().setRowCountLimit(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex()));
            }
        } else if (Symbol.GT == symbol || Symbol.GT_EQ == symbol) {
            if (sqlExpression instanceof SQLNumberExpression) {
                selectStatement.getLimit().setOffsetLimit(new OffsetLimit(((SQLNumberExpression) sqlExpression).getNumber().intValue(), -1));
                selectStatement.getLimit().setOffsetLimit(new LimitValue(((SQLNumberExpression) sqlExpression).getNumber().intValue(), -1));
            } else if (sqlExpression instanceof SQLPlaceholderExpression) {
                selectStatement.getLimit().setOffsetLimit(new OffsetLimit(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex()));
                selectStatement.getLimit().setOffsetLimit(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex()));
            }
        }
    }
+16 −16
Original line number Diff line number Diff line
@@ -41,17 +41,17 @@ public final class Limit {
    
    private final boolean rowCountRewriteFlag;
    
    private OffsetLimit offsetLimit;
    private LimitValue offsetLimit;
    
    private RowCountLimit rowCountLimit;
    private LimitValue rowCountLimit;
    
    /**
     * 获取分页偏移量.
     * 
     * @return 分页偏移量
     */
    public int getOffset() {
        return null != offsetLimit ? offsetLimit.getOffset() : 0;
    public int getOffsetValue() {
        return null != offsetLimit ? offsetLimit.getValue() : 0;
    }
    
    /**
@@ -59,8 +59,8 @@ public final class Limit {
     *
     * @return 分页行数
     */
    public int getRowCount() {
        return null != rowCountLimit ? rowCountLimit.getRowCount() : 0;
    public int getRowCountValue() {
        return null != rowCountLimit ? rowCountLimit.getValue() : 0;
    }
    
    /**
@@ -79,13 +79,13 @@ public final class Limit {
    private void fill(final List<Object> parameters) {
        int offset = 0;
        if (null != offsetLimit) {
            offset = -1 == offsetLimit.getOffsetParameterIndex() ? getOffset() : roundHalfUp(parameters.get(offsetLimit.getOffsetParameterIndex()));
            offsetLimit.setOffset(offset);
            offset = -1 == offsetLimit.getIndex() ? getOffsetValue() : roundHalfUp(parameters.get(offsetLimit.getIndex()));
            offsetLimit.setValue(offset);
        }
        int rowCount = 0;
        if (null != rowCountLimit) {
            rowCount = -1 == rowCountLimit.getRowCountParameterIndex() ? getRowCount() : roundHalfUp(parameters.get(rowCountLimit.getRowCountParameterIndex()));
            rowCountLimit.setRowCount(rowCount);
            rowCount = -1 == rowCountLimit.getIndex() ? getRowCountValue() : roundHalfUp(parameters.get(rowCountLimit.getIndex()));
            rowCountLimit.setValue(rowCount);
        }
        if (offset < 0 || rowCount < 0) {
            throw new SQLParsingException("LIMIT offset and row count can not be a negative value.");
@@ -96,15 +96,15 @@ public final class Limit {
        int rewriteOffset = 0;
        int rewriteRowCount;
        if (rowCountRewriteFlag) {
            rewriteRowCount = null == rowCountLimit ? -1 : getOffset() + rowCountLimit.getRowCount();
            rewriteRowCount = null == rowCountLimit ? -1 : getOffsetValue() + rowCountLimit.getValue();
        } else {
            rewriteRowCount = rowCountLimit.getRowCount();
            rewriteRowCount = rowCountLimit.getValue();
        }
        if (null != offsetLimit && offsetLimit.getOffsetParameterIndex() > -1) {
            parameters.set(offsetLimit.getOffsetParameterIndex(), rewriteOffset);
        if (null != offsetLimit && offsetLimit.getIndex() > -1) {
            parameters.set(offsetLimit.getIndex(), rewriteOffset);
        }
        if (null != rowCountLimit && rowCountLimit.getRowCountParameterIndex() > -1) {
            parameters.set(rowCountLimit.getRowCountParameterIndex(), rewriteRowCount);
        if (null != rowCountLimit && rowCountLimit.getIndex() > -1) {
            parameters.set(rowCountLimit.getIndex(), rewriteRowCount);
        }
    }
}
+5 −5
Original line number Diff line number Diff line
@@ -23,17 +23,17 @@ import lombok.Setter;
import lombok.ToString;

/**
 * 分页偏移量对象.
 * 分页对象.
 *
 * @author caohao
 * @author zhangliang
 */
@AllArgsConstructor
@Getter
@Setter
@ToString
public class OffsetLimit {
public final class LimitValue {
    
    private int offset;
    private int value;
    
    private int offsetParameterIndex;
    private int index;
}
+0 −39
Original line number Diff line number Diff line
/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * 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.
 * </p>
 */

package com.dangdang.ddframe.rdb.sharding.parsing.parser.context.limit;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 分页行数对象.
 *
 * @author caohao
 */
@AllArgsConstructor
@Getter
@Setter
@ToString
public class RowCountLimit {
    
    private int rowCount;
    
    private int rowCountParameterIndex;
}
Loading