Search in sources :

Example 11 with SQLExpression

use of io.shardingjdbc.core.parsing.parser.expression.SQLExpression in project sharding-jdbc by shardingjdbc.

the class OrderByClauseParser method parseSelectOrderByItem.

private OrderItem parseSelectOrderByItem(final SelectStatement selectStatement) {
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    OrderDirection orderDirection = OrderDirection.ASC;
    if (lexerEngine.skipIfEqual(DefaultKeyword.ASC)) {
        orderDirection = OrderDirection.ASC;
    } else if (lexerEngine.skipIfEqual(DefaultKeyword.DESC)) {
        orderDirection = OrderDirection.DESC;
    }
    if (sqlExpression instanceof SQLNumberExpression) {
        return new OrderItem(((SQLNumberExpression) sqlExpression).getNumber().intValue(), orderDirection, getNullOrderDirection());
    }
    if (sqlExpression instanceof SQLIdentifierExpression) {
        return new OrderItem(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(((SQLIdentifierExpression) sqlExpression).getName())));
    }
    if (sqlExpression instanceof SQLPropertyExpression) {
        SQLPropertyExpression sqlPropertyExpression = (SQLPropertyExpression) sqlExpression;
        return new OrderItem(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()), SQLUtil.getExactlyValue(sqlPropertyExpression.getName()), orderDirection, getNullOrderDirection(), selectStatement.getAlias(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()) + "." + SQLUtil.getExactlyValue(sqlPropertyExpression.getName())));
    }
    if (sqlExpression instanceof SQLIgnoreExpression) {
        SQLIgnoreExpression sqlIgnoreExpression = (SQLIgnoreExpression) sqlExpression;
        return new OrderItem(sqlIgnoreExpression.getExpression(), orderDirection, getNullOrderDirection(), selectStatement.getAlias(sqlIgnoreExpression.getExpression()));
    }
    throw new SQLParsingException(lexerEngine);
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLIdentifierExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) SQLIgnoreExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression) OrderDirection(io.shardingjdbc.core.constant.OrderDirection)

Example 12 with SQLExpression

use of io.shardingjdbc.core.parsing.parser.expression.SQLExpression in project sharding-jdbc by shardingjdbc.

the class SQLServerTopClauseParser method parse.

/**
 * Parse top.
 *
 * @param selectStatement select statement
 */
public void parse(final SelectStatement selectStatement) {
    if (!lexerEngine.skipIfEqual(SQLServerKeyword.TOP)) {
        return;
    }
    int beginPosition = lexerEngine.getCurrentToken().getEndPosition();
    if (!lexerEngine.skipIfEqual(Symbol.LEFT_PAREN)) {
        beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
    }
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
    LimitValue rowCountValue;
    if (sqlExpression instanceof SQLNumberExpression) {
        int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
        rowCountValue = new LimitValue(rowCount, -1, false);
        selectStatement.getSqlTokens().add(new RowCountToken(beginPosition, rowCount));
    } else if (sqlExpression instanceof SQLPlaceholderExpression) {
        rowCountValue = new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), false);
    } else {
        throw new SQLParsingException(lexerEngine);
    }
    lexerEngine.unsupportedIfEqual(SQLServerKeyword.PERCENT);
    lexerEngine.skipIfEqual(DefaultKeyword.WITH, SQLServerKeyword.TIES);
    if (null == selectStatement.getLimit()) {
        Limit limit = new Limit(DatabaseType.SQLServer);
        limit.setRowCount(rowCountValue);
        selectStatement.setLimit(limit);
    } else {
        selectStatement.getLimit().setRowCount(rowCountValue);
    }
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLParsingException(io.shardingjdbc.core.parsing.parser.exception.SQLParsingException) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) 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)

Aggregations

SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)12 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)6 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)4 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)4 SQLPlaceholderExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)4 LinkedList (java.util.LinkedList)4 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)3 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)3 SQLIgnoreExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)3 SQLPropertyExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression)3 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)2 SQLIdentifierExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression)2 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)2 ListShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue)1 RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)1 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 OrderDirection (io.shardingjdbc.core.constant.OrderDirection)1 DefaultKeyword (io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword)1 Keyword (io.shardingjdbc.core.parsing.lexer.token.Keyword)1 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)1