diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingStatement.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingStatement.java index 3f7216af83e227..c7e87275553747 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingStatement.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/core/statement/ShardingStatement.java @@ -230,7 +230,8 @@ public ResultSet getGeneratedKeys() throws SQLException { protected final Optional getGeneratedKey() { if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) { - return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey()); + List generatedKeys = ((InsertStatement) routeResult.getSqlStatement()).getGeneratedKeys(); + return generatedKeys.isEmpty() ? Optional.absent() : Optional.of(generatedKeys.get(0)); } return Optional.absent(); } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/AbstractInsertParser.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/AbstractInsertParser.java index f21f7d6004752b..75a408e0ded203 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/AbstractInsertParser.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/AbstractInsertParser.java @@ -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++; } @@ -188,12 +188,12 @@ protected void parseCustomizedInsert() { private void appendGenerateKey() { String tableName = insertStatement.getTables().getSingleTableName(); Optional 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))); } } diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/InsertStatement.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/InsertStatement.java index 3e873ac3bfe86b..ce3b2562d4a755 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/InsertStatement.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/statement/insert/InsertStatement.java @@ -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; @@ -36,6 +36,7 @@ import java.util.Collection; import java.util.LinkedList; +import java.util.List; /** * Insert SQL语句对象. @@ -49,11 +50,11 @@ public final class InsertStatement extends AbstractSQLStatement { private final Collection columns = new LinkedList<>(); - private GeneratedKey generatedKey; + private final List generatedKeys = new LinkedList<>(); - private int columnsListLastPosition; + private final List valuesListLastPositionList = new LinkedList<>(); - private int valuesListLastPosition; + private int columnsListLastPosition; public InsertStatement() { super(SQLType.INSERT); @@ -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 = shardingRule.tryFindTableRule(getTables().getSingleTableName()); @@ -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 findGeneratedKeyToken() { diff --git a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/routing/router/ParsingSQLRouter.java b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/routing/router/ParsingSQLRouter.java index 9469b461cfcd81..7ab3b8ba118aad 100644 --- a/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/routing/router/ParsingSQLRouter.java +++ b/sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/routing/router/ParsingSQLRouter.java @@ -83,7 +83,7 @@ public SQLStatement parse(final String logicSQL, final int parametersSize) { public SQLRouteResult route(final String logicSQL, final List 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); @@ -138,7 +138,7 @@ private void logSQL(final String logicSQL, final SQLStatement sqlStatement, fina } private void processGeneratedKey(final List 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()) {