Search in sources :

Example 6 with ItemsToken

use of io.shardingjdbc.core.parsing.parser.token.ItemsToken in project sharding-jdbc by shardingjdbc.

the class InsertStatement method appendGenerateKeyToken.

/**
 * Append generate key token.
 *
 * @param shardingRule databases and tables sharding rule
 */
public void appendGenerateKeyToken(final ShardingRule shardingRule) {
    if (null != generatedKey) {
        return;
    }
    Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByLogicTable(getTables().getSingleTableName());
    if (!tableRule.isPresent()) {
        return;
    }
    Optional<GeneratedKeyToken> generatedKeysToken = findGeneratedKeyToken();
    if (!generatedKeysToken.isPresent()) {
        return;
    }
    ItemsToken valuesToken = new ItemsToken(generatedKeysToken.get().getBeginPosition());
    appendGenerateKeyToken(shardingRule, tableRule.get(), valuesToken);
    getSqlTokens().remove(generatedKeysToken.get());
    getSqlTokens().add(valuesToken);
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) GeneratedKeyToken(io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken)

Example 7 with ItemsToken

use of io.shardingjdbc.core.parsing.parser.token.ItemsToken in project sharding-jdbc by shardingjdbc.

the class AbstractSelectParser method appendDerivedColumns.

private void appendDerivedColumns(final SelectStatement selectStatement) {
    ItemsToken itemsToken = new ItemsToken(selectStatement.getSelectListLastPosition());
    appendAvgDerivedColumns(itemsToken, selectStatement);
    appendDerivedOrderColumns(itemsToken, selectStatement.getOrderByItems(), ORDER_BY_DERIVED_ALIAS, selectStatement);
    appendDerivedOrderColumns(itemsToken, selectStatement.getGroupByItems(), GROUP_BY_DERIVED_ALIAS, selectStatement);
    if (!itemsToken.getItems().isEmpty()) {
        selectStatement.getSqlTokens().add(itemsToken);
    }
}
Also used : ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken)

Aggregations

ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)7 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)4 Test (org.junit.Test)3 GeneratedKeyToken (io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken)2 IndexToken (io.shardingjdbc.core.parsing.parser.token.IndexToken)1 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)1 OrderByToken (io.shardingjdbc.core.parsing.parser.token.OrderByToken)1 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)1 SQLToken (io.shardingjdbc.core.parsing.parser.token.SQLToken)1 SchemaToken (io.shardingjdbc.core.parsing.parser.token.SchemaToken)1 TableRule (io.shardingjdbc.core.rule.TableRule)1