Search in sources :

Example 1 with GeneratedKeyToken

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

the class AbstractInsertParser method appendGenerateKey.

private void appendGenerateKey(final InsertStatement insertStatement) {
    String tableName = insertStatement.getTables().getSingleTableName();
    Optional<String> generateKeyColumn = shardingRule.getGenerateKeyColumn(tableName);
    if (!generateKeyColumn.isPresent() || null != insertStatement.getGeneratedKey()) {
        return;
    }
    ItemsToken columnsToken = new ItemsToken(insertStatement.getColumnsListLastPosition());
    columnsToken.getItems().add(generateKeyColumn.get());
    insertStatement.getSqlTokens().add(columnsToken);
    insertStatement.getSqlTokens().add(new GeneratedKeyToken(insertStatement.getValuesListLastPosition()));
}
Also used : GeneratedKeyToken(io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken)

Example 2 with GeneratedKeyToken

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

Aggregations

GeneratedKeyToken (io.shardingjdbc.core.parsing.parser.token.GeneratedKeyToken)2 ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)2 TableRule (io.shardingjdbc.core.rule.TableRule)1