Search in sources :

Example 1 with SQLIgnoreExpression

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

the class InsertSetClauseParser method parse.

/**
 * Parse insert set.
 *
 * @param insertStatement insert statement
 */
public void parse(final InsertStatement insertStatement) {
    if (!lexerEngine.skipIfEqual(getCustomizedInsertKeywords())) {
        return;
    }
    do {
        Column column = new Column(SQLUtil.getExactlyValue(lexerEngine.getCurrentToken().getLiterals()), insertStatement.getTables().getSingleTableName());
        lexerEngine.nextToken();
        lexerEngine.accept(Symbol.EQ);
        SQLExpression sqlExpression;
        if (lexerEngine.equalAny(Literals.INT)) {
            sqlExpression = new SQLNumberExpression(Integer.parseInt(lexerEngine.getCurrentToken().getLiterals()));
        } else if (lexerEngine.equalAny(Literals.FLOAT)) {
            sqlExpression = new SQLNumberExpression(Double.parseDouble(lexerEngine.getCurrentToken().getLiterals()));
        } else if (lexerEngine.equalAny(Literals.CHARS)) {
            sqlExpression = new SQLTextExpression(lexerEngine.getCurrentToken().getLiterals());
        } else if (lexerEngine.equalAny(DefaultKeyword.NULL)) {
            sqlExpression = new SQLIgnoreExpression(DefaultKeyword.NULL.name());
        } else if (lexerEngine.equalAny(Symbol.QUESTION)) {
            sqlExpression = new SQLPlaceholderExpression(insertStatement.getParametersIndex());
            insertStatement.increaseParametersIndex();
        } else {
            throw new UnsupportedOperationException("");
        }
        lexerEngine.nextToken();
        if (lexerEngine.equalAny(Symbol.COMMA, DefaultKeyword.ON, Assist.END)) {
            insertStatement.getConditions().add(new Condition(column, sqlExpression), shardingRule);
        } else {
            lexerEngine.skipUntil(Symbol.COMMA, DefaultKeyword.ON);
        }
    } while (lexerEngine.skipIfEqual(Symbol.COMMA));
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) SQLTextExpression(io.shardingjdbc.core.parsing.parser.expression.SQLTextExpression) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) SQLIgnoreExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)

Example 2 with SQLIgnoreExpression

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

the class BasicExpressionParser method parseExpression.

// TODO complete more expression parse
private SQLExpression parseExpression(final SQLStatement sqlStatement) {
    String literals = lexerEngine.getCurrentToken().getLiterals();
    final int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - literals.length();
    final SQLExpression expression = getExpression(literals, sqlStatement);
    lexerEngine.nextToken();
    if (lexerEngine.skipIfEqual(Symbol.DOT)) {
        String property = lexerEngine.getCurrentToken().getLiterals();
        lexerEngine.nextToken();
        return skipIfCompositeExpression(sqlStatement) ? new SQLIgnoreExpression(lexerEngine.getInput().substring(beginPosition, lexerEngine.getCurrentToken().getEndPosition())) : new SQLPropertyExpression(new SQLIdentifierExpression(literals), property);
    }
    if (lexerEngine.equalAny(Symbol.LEFT_PAREN)) {
        lexerEngine.skipParentheses(sqlStatement);
        skipRestCompositeExpression(sqlStatement);
        return new SQLIgnoreExpression(lexerEngine.getInput().substring(beginPosition, lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length()).trim());
    }
    return skipIfCompositeExpression(sqlStatement) ? new SQLIgnoreExpression(lexerEngine.getInput().substring(beginPosition, lexerEngine.getCurrentToken().getEndPosition())) : expression;
}
Also used : SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLIdentifierExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression) SQLIgnoreExpression(io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)

Example 3 with SQLIgnoreExpression

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

the class GroupByClauseParser method addGroupByItem.

private void addGroupByItem(final SQLExpression sqlExpression, final SelectStatement selectStatement) {
    lexerEngine.unsupportedIfEqual(getUnsupportedKeywordBeforeGroupByItem());
    OrderDirection orderDirection = OrderDirection.ASC;
    if (lexerEngine.equalAny(DefaultKeyword.ASC)) {
        lexerEngine.nextToken();
    } else if (lexerEngine.skipIfEqual(DefaultKeyword.DESC)) {
        orderDirection = OrderDirection.DESC;
    }
    OrderItem orderItem;
    if (sqlExpression instanceof SQLPropertyExpression) {
        SQLPropertyExpression sqlPropertyExpression = (SQLPropertyExpression) sqlExpression;
        orderItem = new OrderItem(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName()), SQLUtil.getExactlyValue(sqlPropertyExpression.getName()), orderDirection, OrderDirection.ASC, selectStatement.getAlias(SQLUtil.getExactlyValue(sqlPropertyExpression.getOwner().getName() + "." + SQLUtil.getExactlyValue(sqlPropertyExpression.getName()))));
    } else if (sqlExpression instanceof SQLIdentifierExpression) {
        SQLIdentifierExpression sqlIdentifierExpression = (SQLIdentifierExpression) sqlExpression;
        orderItem = new OrderItem(SQLUtil.getExactlyValue(sqlIdentifierExpression.getName()), orderDirection, OrderDirection.ASC, selectStatement.getAlias(SQLUtil.getExactlyValue(sqlIdentifierExpression.getName())));
    } else if (sqlExpression instanceof SQLIgnoreExpression) {
        SQLIgnoreExpression sqlIgnoreExpression = (SQLIgnoreExpression) sqlExpression;
        orderItem = new OrderItem(sqlIgnoreExpression.getExpression(), orderDirection, OrderDirection.ASC, selectStatement.getAlias(sqlIgnoreExpression.getExpression()));
    } else {
        return;
    }
    selectStatement.getGroupByItems().add(orderItem);
}
Also used : SQLPropertyExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression) 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 4 with SQLIgnoreExpression

use of io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression 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)

Aggregations

SQLIgnoreExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)4 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)3 SQLIdentifierExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIdentifierExpression)3 SQLPropertyExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPropertyExpression)3 OrderDirection (io.shardingjdbc.core.constant.OrderDirection)2 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)2 SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)2 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)1 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)1 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)1 SQLPlaceholderExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)1 SQLTextExpression (io.shardingjdbc.core.parsing.parser.expression.SQLTextExpression)1