Commit 61cfeb75 authored by terrymanu's avatar terrymanu
Browse files

refactor parseLimit

parent b5be563d
Loading
Loading
Loading
Loading
+25 −27
Original line number Diff line number Diff line
@@ -47,9 +47,8 @@ public final class MySQLParser extends SQLParser {
     * 
     * @param sqlStatement SQL语句对象
     * @param parametersIndex 参数索引
     * @return 分页
     */
    public Limit parseLimit(final SQLStatement sqlStatement, final int parametersIndex) {
    public void parseLimit(final SQLStatement sqlStatement, final int parametersIndex) {
        skipIfEqual(MySQLKeyword.LIMIT);
        int valueIndex = -1;
        int valueBeginPosition = getLexer().getCurrentToken().getEndPosition();
@@ -68,31 +67,32 @@ public final class MySQLParser extends SQLParser {
        }
        getLexer().nextToken();
        if (skipIfEqual(Symbol.COMMA)) {
            return getLimitWithComma(sqlStatement, parametersIndex, valueIndex, valueBeginPosition, value, isParameterForValue);
            sqlStatement.setLimit(getLimitWithComma(sqlStatement, parametersIndex, valueIndex, valueBeginPosition, value, isParameterForValue));
            return;
        }
        if (skipIfEqual(MySQLKeyword.OFFSET)) {
            return getLimitWithOffset(sqlStatement, parametersIndex, valueIndex, valueBeginPosition, value, isParameterForValue);
            sqlStatement.setLimit(getLimitWithOffset(sqlStatement, parametersIndex, valueIndex, valueBeginPosition, value, isParameterForValue));
            return;
        }
        if (!isParameterForValue) {
            sqlStatement.getSqlTokens().add(new RowCountToken(valueBeginPosition, value));
        }
        Limit result = new Limit(true);
        result.setRowCount(new LimitValue(value, valueIndex));
        return result;
        Limit limit = new Limit(true);
        limit.setRowCount(new LimitValue(value, valueIndex));
        sqlStatement.setLimit(limit);
    }
    
    private Limit getLimitWithComma(
            final SQLStatement sqlStatement, final int parametersIndex, final int valueIndex, final int valueBeginPosition, final int value, final boolean isParameterForValue) {
    private Limit getLimitWithComma(final SQLStatement sqlStatement, final int parametersIndex, final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue) {
        int rowCountBeginPosition = getLexer().getCurrentToken().getEndPosition();
        int rowCount;
        int rowCountValue;
        int rowCountIndex = -1;
        boolean isParameterForRowCount = false;
        if (equalAny(Literals.INT)) {
            rowCount = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
            rowCountBeginPosition = rowCountBeginPosition - (rowCount + "").length();
            rowCountValue = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
            rowCountBeginPosition = rowCountBeginPosition - (rowCountValue + "").length();
        } else if (equalAny(Symbol.QUESTION)) {
            rowCountIndex = -1 == valueIndex ? parametersIndex : valueIndex + 1;
            rowCount = -1;
            rowCountIndex = -1 == index ? parametersIndex : index + 1;
            rowCountValue = -1;
            rowCountBeginPosition--;
            isParameterForRowCount = true;
        } else {
@@ -103,26 +103,24 @@ public final class MySQLParser extends SQLParser {
            sqlStatement.getSqlTokens().add(new OffsetToken(valueBeginPosition, value));
        }
        if (!isParameterForRowCount) {
            sqlStatement.getSqlTokens().add(new RowCountToken(rowCountBeginPosition, rowCount));
            sqlStatement.getSqlTokens().add(new RowCountToken(rowCountBeginPosition, rowCountValue));
        }
        Limit result = new Limit(true);
        result.setRowCount(new LimitValue(rowCount, rowCountIndex));
        result.setOffset(new LimitValue(value, valueIndex));
        result.setRowCount(new LimitValue(rowCountValue, rowCountIndex));
        result.setOffset(new LimitValue(value, index));
        return result;
    }
    
    private Limit getLimitWithOffset(
            final SQLStatement sqlStatement, final int parametersIndex, final int valueIndex, final int valueBeginPosition, final int value, final boolean isParameterForValue) {
    private Limit getLimitWithOffset(final SQLStatement sqlStatement, final int parametersIndex, final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue) {
        int offsetBeginPosition = getLexer().getCurrentToken().getEndPosition();
        int offset;
        int offsetValue = -1;
        int offsetIndex = -1;
        boolean isParameterForOffset = false;
        if (equalAny(Literals.INT)) {
            offset = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
            offsetBeginPosition = offsetBeginPosition - (offset + "").length();
            offsetValue = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
            offsetBeginPosition = offsetBeginPosition - (offsetValue + "").length();
        } else if (equalAny(Symbol.QUESTION)) {
            offsetIndex = -1 == valueIndex ? parametersIndex : valueIndex + 1;
            offset = -1;
            offsetIndex = -1 == index ? parametersIndex : index + 1;
            offsetBeginPosition--;
            isParameterForOffset = true;
        } else {
@@ -130,14 +128,14 @@ public final class MySQLParser extends SQLParser {
        }
        getLexer().nextToken();
        if (!isParameterForOffset) {
            sqlStatement.getSqlTokens().add(new OffsetToken(offsetBeginPosition, offset));
            sqlStatement.getSqlTokens().add(new OffsetToken(offsetBeginPosition, offsetValue));
        }
        if (!isParameterForValue) {
            sqlStatement.getSqlTokens().add(new RowCountToken(valueBeginPosition, value));
        }
        Limit result = new Limit(true);
        result.setRowCount(new LimitValue(value, valueIndex));
        result.setOffset(new LimitValue(offset, offsetIndex));
        result.setRowCount(new LimitValue(value, index));
        result.setOffset(new LimitValue(offsetValue, offsetIndex));
        return result;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ public class MySQLSelectParser extends AbstractSelectParser {
    
    private void parseLimit() {
        if (getSqlParser().equalAny(MySQLKeyword.LIMIT)) {
            getSelectStatement().setLimit(((MySQLParser) getSqlParser()).parseLimit(getSelectStatement(), getParametersIndex()));
            ((MySQLParser) getSqlParser()).parseLimit(getSelectStatement(), getParametersIndex());
        }
    }
    
+0 −1
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ public class OracleSelectParser extends AbstractSelectParser {
        }
    }
    
    
    private void skipHierarchicalQueryClause() {
        skipConnect();
        skipStart();
+15 −15
Original line number Diff line number Diff line
@@ -87,46 +87,46 @@ public class PostgreSQLSelectParser extends AbstractSelectParser {
    
    private Optional<LimitValue> buildRowCount() {
        int parameterIndex = getParametersIndex();
        int rowCount = -1;
        int rowCountParameterIndex = -1;
        int rowCountValue = -1;
        int rowCountIndex = -1;
        int valueBeginPosition = getSqlParser().getLexer().getCurrentToken().getEndPosition();
        if (getSqlParser().equalAny(DefaultKeyword.ALL)) {
            getSqlParser().getLexer().nextToken();
        } else {
            if (getSqlParser().equalAny(Literals.INT, Literals.FLOAT)) {
                rowCount = roundHalfUp(getSqlParser().getLexer().getCurrentToken().getLiterals());
                valueBeginPosition = valueBeginPosition - (rowCount + "").length();
                getSelectStatement().getSqlTokens().add(new RowCountToken(valueBeginPosition, rowCount));
                rowCountValue = roundHalfUp(getSqlParser().getLexer().getCurrentToken().getLiterals());
                valueBeginPosition = valueBeginPosition - (rowCountValue + "").length();
                getSelectStatement().getSqlTokens().add(new RowCountToken(valueBeginPosition, rowCountValue));
            } else if (getSqlParser().equalAny(Symbol.QUESTION)) {
                rowCountParameterIndex = parameterIndex++;
                rowCountIndex = parameterIndex++;
                setParametersIndex(parameterIndex);
                rowCount = -1;
                rowCountValue = -1;
            } else {
                throw new SQLParsingException(getSqlParser().getLexer());
            }
            getSqlParser().getLexer().nextToken();
        }
        return Optional.of(new LimitValue(rowCount, rowCountParameterIndex));
        return Optional.of(new LimitValue(rowCountValue, rowCountIndex));
    }
    
    private Optional<LimitValue> buildOffset() {
        int parameterIndex = getParametersIndex();
        int offset = -1;
        int offsetParameterIndex = -1;
        int offsetValue = -1;
        int offsetIndex = -1;
        int offsetBeginPosition = getSqlParser().getLexer().getCurrentToken().getEndPosition();
        if (getSqlParser().equalAny(Literals.INT, Literals.FLOAT)) {
            offset = roundHalfUp(getSqlParser().getLexer().getCurrentToken().getLiterals());
            offsetBeginPosition = offsetBeginPosition - (offset + "").length();
            getSelectStatement().getSqlTokens().add(new OffsetToken(offsetBeginPosition, offset));
            offsetValue = roundHalfUp(getSqlParser().getLexer().getCurrentToken().getLiterals());
            offsetBeginPosition = offsetBeginPosition - (offsetValue + "").length();
            getSelectStatement().getSqlTokens().add(new OffsetToken(offsetBeginPosition, offsetValue));
        } else if (getSqlParser().equalAny(Symbol.QUESTION)) {
            offsetParameterIndex = parameterIndex++;
            offsetIndex = parameterIndex++;
            setParametersIndex(parameterIndex);
        } else {
            throw new SQLParsingException(getSqlParser().getLexer());
        }
        getSqlParser().getLexer().nextToken();
        getSqlParser().skipIfEqual(PostgreSQLKeyword.ROW, PostgreSQLKeyword.ROWS);
        return Optional.of(new LimitValue(offset, offsetParameterIndex));
        return Optional.of(new LimitValue(offsetValue, offsetIndex));
    }
    
    
+7 −9
Original line number Diff line number Diff line
@@ -93,13 +93,12 @@ public final class SQLServerParser extends SQLParser {
    
    public void parseOffset(final SelectStatement selectStatement) {
        getLexer().nextToken();
        int offset;
        int offsetValue = -1;
        int offsetIndex = -1;
        if (equalAny(Literals.INT)) {
            offset = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
            offsetValue = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
        } else if (equalAny(Symbol.QUESTION)) {
            offsetIndex = getParametersIndex();
            offset = -1;
            setParametersIndex(offsetIndex + 1);
        } else {
            throw new SQLParsingException(getLexer());
@@ -108,24 +107,23 @@ public final class SQLServerParser extends SQLParser {
        Limit limit = new Limit(true);
        if (skipIfEqual(DefaultKeyword.FETCH)) {
            getLexer().nextToken();
            int rowCount;
            int rowCountValue = -1;
            int rowCountIndex = -1;
            getLexer().nextToken();
            if (equalAny(Literals.INT)) {
                rowCount = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
                rowCountValue = Integer.parseInt(getLexer().getCurrentToken().getLiterals());
            } else if (equalAny(Symbol.QUESTION)) {
                rowCountIndex = getParametersIndex();
                rowCount = -1;
                setParametersIndex(rowCountIndex + 1);
            } else {
                throw new SQLParsingException(getLexer());
            }
            getLexer().nextToken();
            getLexer().nextToken();
            limit.setRowCount(new LimitValue(rowCount, rowCountIndex));
            limit.setOffset(new LimitValue(offset, offsetIndex));
            limit.setRowCount(new LimitValue(rowCountValue, rowCountIndex));
            limit.setOffset(new LimitValue(offsetValue, offsetIndex));
        } else {
            limit.setOffset(new LimitValue(offset, offsetIndex));
            limit.setOffset(new LimitValue(offsetValue, offsetIndex));
        }
        selectStatement.setLimit(limit);
    }