Search in sources :

Example 1 with SQLExpression

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

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

the class InsertValuesClauseParser method parseValues.

private void parseValues(final InsertStatement insertStatement) {
    lexerEngine.accept(Symbol.LEFT_PAREN);
    List<SQLExpression> sqlExpressions = new LinkedList<>();
    do {
        sqlExpressions.add(basicExpressionParser.parse(insertStatement));
    } while (lexerEngine.skipIfEqual(Symbol.COMMA));
    insertStatement.setValuesListLastPosition(lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length());
    int count = 0;
    for (Column each : insertStatement.getColumns()) {
        SQLExpression sqlExpression = sqlExpressions.get(count);
        insertStatement.getConditions().add(new Condition(each, sqlExpression), shardingRule);
        if (insertStatement.getGenerateKeyColumnIndex() == count) {
            insertStatement.setGeneratedKey(createGeneratedKey(each, sqlExpression));
        }
        count++;
    }
    lexerEngine.accept(Symbol.RIGHT_PAREN);
}
Also used : 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) LinkedList(java.util.LinkedList)

Example 3 with SQLExpression

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

the class WhereClauseParser method parseRowCountCondition.

private void parseRowCountCondition(final SelectStatement selectStatement, final boolean includeRowCount) {
    SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
    if (null == selectStatement.getLimit()) {
        selectStatement.setLimit(new Limit(databaseType));
    }
    if (sqlExpression instanceof SQLNumberExpression) {
        int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
        selectStatement.getLimit().setRowCount(new LimitValue(rowCount, -1, includeRowCount));
        selectStatement.getSqlTokens().add(new RowCountToken(lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(rowCount).length() - lexerEngine.getCurrentToken().getLiterals().length(), rowCount));
    } else if (sqlExpression instanceof SQLPlaceholderExpression) {
        selectStatement.getLimit().setRowCount(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), includeRowCount));
    }
}
Also used : SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression) Limit(io.shardingjdbc.core.parsing.parser.context.limit.Limit) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) LimitValue(io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)

Example 4 with SQLExpression

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

the class WhereClauseParser method parseBetweenCondition.

private void parseBetweenCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final SQLExpression left) {
    List<SQLExpression> rights = new LinkedList<>();
    rights.add(basicExpressionParser.parse(sqlStatement));
    lexerEngine.accept(DefaultKeyword.AND);
    rights.add(basicExpressionParser.parse(sqlStatement));
    Optional<Column> column = find(sqlStatement.getTables(), left);
    if (column.isPresent()) {
        sqlStatement.getConditions().add(new Condition(column.get(), rights.get(0), rights.get(1)), shardingRule);
    }
}
Also used : 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) LinkedList(java.util.LinkedList)

Example 5 with SQLExpression

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

the class WhereClauseParser method parseInCondition.

private void parseInCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final SQLExpression left) {
    lexerEngine.accept(Symbol.LEFT_PAREN);
    List<SQLExpression> rights = new LinkedList<>();
    do {
        lexerEngine.skipIfEqual(Symbol.COMMA);
        rights.add(basicExpressionParser.parse(sqlStatement));
    } while (!lexerEngine.equalAny(Symbol.RIGHT_PAREN));
    Optional<Column> column = find(sqlStatement.getTables(), left);
    if (column.isPresent()) {
        sqlStatement.getConditions().add(new Condition(column.get(), rights), shardingRule);
    }
    lexerEngine.nextToken();
}
Also used : 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) LinkedList(java.util.LinkedList)

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