use of com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext in project sharding-jdbc by dangdangdotcom.
the class PreparedSQLRouter method generateId.
private void generateId(final List<Object> parameters) {
Optional<TableRule> tableRuleOptional = shardingRule.tryFindTableRule(sqlParsedResult.getRouteContext().getTables().iterator().next().getName());
if (!tableRuleOptional.isPresent()) {
return;
}
TableRule tableRule = tableRuleOptional.get();
GeneratedKeyContext generatedKeyContext = sqlParsedResult.getGeneratedKeyContext();
for (String each : generatedKeyContext.getColumns()) {
Object id = tableRule.generateId(each);
parameters.add(id);
generatedKeyContext.putValue(each, id);
}
}
use of com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext in project sharding-jdbc by dangdangdotcom.
the class MySQLInsertVisitor method supplyAutoIncrementColumn.
private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
boolean isPreparedStatement = !getParameters().isEmpty();
GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
if (isPreparedStatement) {
generatedKeyContext.getColumns().addAll(autoIncrementColumns);
}
TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
for (String each : autoIncrementColumns) {
SQLExpr sqlExpr;
Object id = tableRule.generateId(each);
generatedKeyContext.putValue(each, id);
if (isPreparedStatement) {
sqlExpr = new SQLVariantRefExpr("?");
getParameters().add(id);
((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
} else {
sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
}
getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
columns.add(new SQLIdentifierExpr(each));
values.add(sqlExpr);
}
}
Aggregations