Skip to content

Commit

Permalink
for #290, 1st version InsertStatement: single generatedKey and values…
Browse files Browse the repository at this point in the history
…ListLastPosition => generatedKeys and valuesListLastPositionList
  • Loading branch information
terrymanu committed Jul 17, 2017
1 parent 7524f38 commit d02ac34
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ public ResultSet getGeneratedKeys() throws SQLException {

protected final Optional<GeneratedKey> getGeneratedKey() {
if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) {
return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey());
List<GeneratedKey> generatedKeys = ((InsertStatement) routeResult.getSqlStatement()).getGeneratedKeys();
return generatedKeys.isEmpty() ? Optional.<GeneratedKey>absent() : Optional.of(generatedKeys.get(0));
}
return Optional.absent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ private void parseValues() {
do {
sqlExpressions.add(sqlParser.parseExpression());
} while (sqlParser.skipIfEqual(Symbol.COMMA));
insertStatement.setValuesListLastPosition(sqlParser.getLexer().getCurrentToken().getEndPosition() - sqlParser.getLexer().getCurrentToken().getLiterals().length());
insertStatement.getValuesListLastPositionList().add(sqlParser.getLexer().getCurrentToken().getEndPosition() - sqlParser.getLexer().getCurrentToken().getLiterals().length());
int count = 0;
for (Column each : insertStatement.getColumns()) {
SQLExpression sqlExpression = sqlExpressions.get(count);
insertStatement.getConditions().add(new Condition(each, sqlExpression), getShardingRule());
if (generateKeyColumnIndex == count) {
insertStatement.setGeneratedKey(createGeneratedKey(each, sqlExpression));
insertStatement.getGeneratedKeys().add(createGeneratedKey(each, sqlExpression));
}
count++;
}
Expand Down Expand Up @@ -188,12 +188,12 @@ protected void parseCustomizedInsert() {
private void appendGenerateKey() {
String tableName = insertStatement.getTables().getSingleTableName();
Optional<String> generateKeyColumn = shardingRule.getGenerateKeyColumn(tableName);
if (!generateKeyColumn.isPresent() || null != insertStatement.getGeneratedKey()) {
if (!generateKeyColumn.isPresent() || !insertStatement.getGeneratedKeys().isEmpty()) {
return;
}
ItemsToken columnsToken = new ItemsToken(insertStatement.getColumnsListLastPosition());
columnsToken.getItems().add(generateKeyColumn.get());
insertStatement.getSqlTokens().add(columnsToken);
insertStatement.getSqlTokens().add(new GeneratedKeyToken(insertStatement.getValuesListLastPosition()));
insertStatement.getSqlTokens().add(new GeneratedKeyToken(insertStatement.getValuesListLastPositionList().get(0)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.condition.Column;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.GeneratedKey;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.condition.Column;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.condition.Condition;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLNumberExpression;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.expression.SQLPlaceholderExpression;
Expand All @@ -36,6 +36,7 @@

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
* Insert SQL语句对象.
Expand All @@ -49,11 +50,11 @@ public final class InsertStatement extends AbstractSQLStatement {

private final Collection<Column> columns = new LinkedList<>();

private GeneratedKey generatedKey;
private final List<GeneratedKey> generatedKeys = new LinkedList<>();

private int columnsListLastPosition;
private final List<Integer> valuesListLastPositionList = new LinkedList<>();

private int valuesListLastPosition;
private int columnsListLastPosition;

public InsertStatement() {
super(SQLType.INSERT);
Expand All @@ -66,7 +67,7 @@ public InsertStatement() {
* @param parametersSize 参数个数
*/
public void appendGenerateKeyToken(final ShardingRule shardingRule, final int parametersSize) {
if (null != generatedKey) {
if (!generatedKeys.isEmpty()) {
return;
}
Optional<TableRule> tableRule = shardingRule.tryFindTableRule(getTables().getSingleTableName());
Expand All @@ -91,13 +92,13 @@ private void appendGenerateKeyToken(final ShardingRule shardingRule, final Table
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);
generatedKeys.add(new GeneratedKey(tableRule.getLogicTable(), -1, generatedKey));
}

private void appendGenerateKeyToken(final ShardingRule shardingRule, final TableRule tableRule, final ItemsToken valuesToken, final int parametersSize) {
valuesToken.getItems().add("?");
getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLPlaceholderExpression(parametersSize)), shardingRule);
generatedKey = new GeneratedKey(tableRule.getGenerateKeyColumn(), parametersSize, null);
generatedKeys.add(new GeneratedKey(tableRule.getGenerateKeyColumn(), parametersSize, null));
}

private Optional<GeneratedKeyToken> findGeneratedKeyToken() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public SQLStatement parse(final String logicSQL, final int parametersSize) {
public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
final Context context = MetricsContext.start("Route SQL");
SQLRouteResult result = new SQLRouteResult(sqlStatement);
if (sqlStatement instanceof InsertStatement && null != ((InsertStatement) sqlStatement).getGeneratedKey()) {
if (sqlStatement instanceof InsertStatement && !((InsertStatement) sqlStatement).getGeneratedKeys().isEmpty()) {
processGeneratedKey(parameters, (InsertStatement) sqlStatement, result);
}
RoutingResult routingResult = route(parameters, sqlStatement);
Expand Down Expand Up @@ -138,7 +138,7 @@ private void logSQL(final String logicSQL, final SQLStatement sqlStatement, fina
}

private void processGeneratedKey(final List<Object> parameters, final InsertStatement insertStatement, final SQLRouteResult sqlRouteResult) {
GeneratedKey generatedKey = insertStatement.getGeneratedKey();
GeneratedKey generatedKey = insertStatement.getGeneratedKeys().get(0);
if (parameters.isEmpty()) {
sqlRouteResult.getGeneratedKeys().add(generatedKey.getValue());
} else if (parameters.size() == generatedKey.getIndex()) {
Expand Down

0 comments on commit d02ac34

Please sign in to comment.