Search in sources :

Example 1 with SQLPlaceholderExpression

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

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

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

the class WhereClauseParser method parseOffsetCondition.

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

Example 4 with SQLPlaceholderExpression

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

the class InsertStatement method appendGenerateKeyToken.

private void appendGenerateKeyToken(final ShardingRule shardingRule, final TableRule tableRule, final ItemsToken valuesToken) {
    if (0 == getParametersIndex()) {
        Number generatedKey = shardingRule.generateKey(tableRule.getLogicTable());
        valuesToken.getItems().add(generatedKey.toString());
        getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLNumberExpression(generatedKey)), shardingRule);
        this.generatedKey = new GeneratedKey(tableRule.getLogicTable(), -1, generatedKey);
    } else {
        valuesToken.getItems().add(Symbol.QUESTION.getLiterals());
        getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLPlaceholderExpression(getParametersIndex())), shardingRule);
        generatedKey = new GeneratedKey(tableRule.getGenerateKeyColumn(), getParametersIndex(), null);
    }
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) SQLNumberExpression(io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression) GeneratedKey(io.shardingjdbc.core.parsing.parser.context.GeneratedKey) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) SQLPlaceholderExpression(io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)

Example 5 with SQLPlaceholderExpression

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

SQLNumberExpression (io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression)5 SQLPlaceholderExpression (io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression)5 SQLExpression (io.shardingjdbc.core.parsing.parser.expression.SQLExpression)4 Limit (io.shardingjdbc.core.parsing.parser.context.limit.Limit)3 LimitValue (io.shardingjdbc.core.parsing.parser.context.limit.LimitValue)3 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)2 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)2 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)2 GeneratedKey (io.shardingjdbc.core.parsing.parser.context.GeneratedKey)1 SQLParsingException (io.shardingjdbc.core.parsing.parser.exception.SQLParsingException)1 SQLIgnoreExpression (io.shardingjdbc.core.parsing.parser.expression.SQLIgnoreExpression)1 SQLTextExpression (io.shardingjdbc.core.parsing.parser.expression.SQLTextExpression)1 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)1