Search in sources :

Example 1 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class MySQLLimitClauseParser method getLimitWithComma.

private Limit getLimitWithComma(final int index, final int valueBeginPosition, final int value, final boolean isParameterForValue, final SelectStatement selectStatement) {
    int rowCountBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    int rowCountValue;
    int rowCountIndex = -1;
    boolean isParameterForRowCount = false;
    if (lexerEngine.equalAny(Literals.INT)) {
        rowCountValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
        rowCountBeginPosition = rowCountBeginPosition - (rowCountValue + "").length();
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        rowCountIndex = -1 == index ? selectStatement.getParametersIndex() : index + 1;
        rowCountValue = -1;
        rowCountBeginPosition--;
        isParameterForRowCount = true;
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    if (isParameterForValue) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new OffsetToken(valueBeginPosition, value));
    }
    if (isParameterForRowCount) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new RowCountToken(rowCountBeginPosition, rowCountValue));
    }
    Limit result = new Limit(DatabaseType.MySQL);
    result.setRowCount(new LimitValue(rowCountValue, rowCountIndex, false));
    result.setOffset(new LimitValue(value, index, true));
    return result;
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 2 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class MySQLLimitClauseParser method parse.

/**
 * Parse limit.
 *
 * @param selectStatement select statement
 */
public void parse(final SelectStatement selectStatement) {
    if (!lexerEngine.skipIfEqual(MySQLKeyword.LIMIT)) {
        return;
    }
    int valueIndex = -1;
    int valueBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    int value;
    boolean isParameterForValue = false;
    if (lexerEngine.equalAny(Literals.INT)) {
        value = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
        valueBeginPosition = valueBeginPosition - (value + "").length();
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        valueIndex = selectStatement.getParametersIndex();
        value = -1;
        valueBeginPosition--;
        isParameterForValue = true;
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    if (lexerEngine.skipIfEqual(Symbol.COMMA)) {
        selectStatement.setLimit(getLimitWithComma(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement));
        return;
    }
    if (lexerEngine.skipIfEqual(MySQLKeyword.OFFSET)) {
        selectStatement.setLimit(getLimitWithOffset(valueIndex, valueBeginPosition, value, isParameterForValue, selectStatement));
        return;
    }
    if (isParameterForValue) {
        selectStatement.increaseParametersIndex();
    } else {
        selectStatement.getSqlTokens().add(new RowCountToken(valueBeginPosition, value));
    }
    Limit limit = new Limit(DatabaseType.MySQL);
    limit.setRowCount(new LimitValue(value, valueIndex, false));
    selectStatement.setLimit(limit);
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 3 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class PostgreSQLLimitClauseParser method buildRowCount.

private Optional<LimitValue> buildRowCount(final SelectStatement selectStatement) {
    int parameterIndex = selectStatement.getParametersIndex();
    int rowCountValue = -1;
    int rowCountIndex = -1;
    int valueBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    if (lexerEngine.equalAny(DefaultKeyword.ALL)) {
        lexerEngine.nextToken();
    } else {
        if (lexerEngine.equalAny(Literals.INT, Literals.FLOAT)) {
            rowCountValue = NumberUtil.roundHalfUp(lexerEngine.getCurrentToken().getLiterals());
            valueBeginPosition = valueBeginPosition - (rowCountValue + "").length();
            selectStatement.getSqlTokens().add(new RowCountToken(valueBeginPosition, rowCountValue));
        } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
            rowCountIndex = parameterIndex++;
            selectStatement.setParametersIndex(parameterIndex);
            rowCountValue = -1;
        } else {
            throw new SQLParsingException(lexerEngine);
        }
        lexerEngine.nextToken();
    }
    return Optional.of(new LimitValue(rowCountValue, rowCountIndex, false));
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 4 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class PostgreSQLLimitClauseParser method buildOffset.

private Optional<LimitValue> buildOffset(final SelectStatement selectStatement) {
    int parameterIndex = selectStatement.getParametersIndex();
    int offsetValue = -1;
    int offsetIndex = -1;
    int offsetBeginPosition = lexerEngine.getCurrentToken().getEndPosition();
    if (lexerEngine.equalAny(Literals.INT, Literals.FLOAT)) {
        offsetValue = NumberUtil.roundHalfUp(lexerEngine.getCurrentToken().getLiterals());
        offsetBeginPosition = offsetBeginPosition - (offsetValue + "").length();
        selectStatement.getSqlTokens().add(new OffsetToken(offsetBeginPosition, offsetValue));
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        offsetIndex = parameterIndex++;
        selectStatement.setParametersIndex(parameterIndex);
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    lexerEngine.skipIfEqual(DefaultKeyword.ROW, PostgreSQLKeyword.ROWS);
    return Optional.of(new LimitValue(offsetValue, offsetIndex, true));
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 5 with SQLParsingException

use of io.shardingjdbc.core.parsing.parser.exception.SQLParsingException in project sharding-jdbc by shardingjdbc.

the class SQLServerOffsetClauseParser method parse.

/**
 * Parse offset.
 *
 * @param selectStatement select statement
 */
public void parse(final SelectStatement selectStatement) {
    if (!lexerEngine.skipIfEqual(SQLServerKeyword.OFFSET)) {
        return;
    }
    int offsetValue = -1;
    int offsetIndex = -1;
    if (lexerEngine.equalAny(Literals.INT)) {
        offsetValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
    } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
        offsetIndex = selectStatement.getParametersIndex();
        selectStatement.increaseParametersIndex();
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.nextToken();
    Limit limit = new Limit(DatabaseType.SQLServer);
    if (lexerEngine.skipIfEqual(DefaultKeyword.FETCH)) {
        lexerEngine.nextToken();
        int rowCountValue = -1;
        int rowCountIndex = -1;
        lexerEngine.nextToken();
        if (lexerEngine.equalAny(Literals.INT)) {
            rowCountValue = Integer.parseInt(lexerEngine.getCurrentToken().getLiterals());
        } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
            rowCountIndex = selectStatement.getParametersIndex();
            selectStatement.increaseParametersIndex();
        } else {
            throw new SQLParsingException(lexerEngine);
        }
        lexerEngine.nextToken();
        lexerEngine.nextToken();
        limit.setRowCount(new LimitValue(rowCountValue, rowCountIndex, false));
        limit.setOffset(new LimitValue(offsetValue, offsetIndex, true));
    } else {
        limit.setOffset(new LimitValue(offsetValue, offsetIndex, true));
    }
    selectStatement.setLimit(limit);
}
Also used : SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Aggregations

SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)12 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)7 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)5 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)5 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)3 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)2 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)2 DDLStatement (io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement)2 OrderDirection (io.shardingjdbc.core.constant.OrderDirection)1 LexerEngine (io.shardingjdbc.core.parsing.lexer.LexerEngine)1 Tokenizer (io.shardingjdbc.core.parsing.lexer.analyzer.Tokenizer)1 MySQLKeyword (io.shardingjdbc.core.parsing.lexer.dialect.mysql.MySQLKeyword)1 Assist (io.shardingjdbc.core.parsing.lexer.token.Assist)1 DefaultKeyword (io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword)1 Keyword (io.shardingjdbc.core.parsing.lexer.token.Keyword)1 Token (io.shardingjdbc.core.parsing.lexer.token.Token)1 TokenType (io.shardingjdbc.core.parsing.lexer.token.TokenType)1 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)1 SQLIdentifierExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression)1 SQLIgnoreExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)1