Skip to content

Commit

Permalink
fix #370
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 12, 2017
1 parent 830e710 commit fdeac9b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

1. [ISSUE #356](https://github.com/dangdangdotcom/sharding-jdbc/issues/356) 在SQL的Where条件中兼容不是分片列的REGEXP操作符
1. [ISSUE #362](https://github.com/dangdangdotcom/sharding-jdbc/issues/362) 读写分离使用PreparedStatement并未调用setParameter方法导致出错
1. [ISSUE #370](https://github.com/dangdangdotcom/sharding-jdbc/issues/370) 使用原生自增主键调用getGeneratedKeys出错

## 1.5.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,9 @@ private Collection<PreparedStatement> generatePreparedStatementForDDL(final SQLE
}

private PreparedStatement generatePreparedStatement(final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
Optional<GeneratedKey> generatedKey = getGeneratedKey();
Connection connection = getConnection().getConnection(sqlExecutionUnit.getDataSource(), routeResult.getSqlStatement().getType());
if (returnGeneratedKeys && generatedKey.isPresent()) {
return connection.prepareStatement(sqlExecutionUnit.getSql(), RETURN_GENERATED_KEYS);
}
return connection.prepareStatement(sqlExecutionUnit.getSql(), resultSetType, resultSetConcurrency, resultSetHoldability);
}

private Optional<GeneratedKey> getGeneratedKey() {
if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) {
return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey());
}
return Optional.absent();
return returnGeneratedKeys ? connection.prepareStatement(sqlExecutionUnit.getSql(), RETURN_GENERATED_KEYS)
: connection.prepareStatement(sqlExecutionUnit.getSql(), resultSetType, resultSetConcurrency, resultSetHoldability);
}

@Override
Expand All @@ -211,28 +201,6 @@ public void addBatch() throws SQLException {
}
}

@Override
public int[] executeBatch() throws SQLException {
try {
return new BatchPreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(),
getConnection().getShardingContext().getDatabaseType(), routeResult.getSqlStatement().getType(), batchStatementUnits, parameterSets).executeBatch();
} finally {
clearBatch();
}
}

@Override
public ResultSet getGeneratedKeys() throws SQLException {
Optional<GeneratedKey> generatedKey = getGeneratedKey();
if (returnGeneratedKeys && generatedKey.isPresent()) {
return new GeneratedKeysResultSet(routeResult.getGeneratedKeys().iterator(), generatedKey.get().getColumn(), this);
}
if (1 == routedStatements.size()) {
return routedStatements.iterator().next().getGeneratedKeys();
}
return new GeneratedKeysResultSet();
}

private List<BatchPreparedStatementUnit> routeBatch() throws SQLException {
List<BatchPreparedStatementUnit> result = new ArrayList<>();
routeResult = routingEngine.route(getParameters());
Expand Down Expand Up @@ -260,6 +228,35 @@ public boolean apply(final BatchPreparedStatementUnit input) {
return result;
}

@Override
public int[] executeBatch() throws SQLException {
try {
return new BatchPreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(),
getConnection().getShardingContext().getDatabaseType(), routeResult.getSqlStatement().getType(), batchStatementUnits, parameterSets).executeBatch();
} finally {
clearBatch();
}
}

@Override
public ResultSet getGeneratedKeys() throws SQLException {
Optional<GeneratedKey> generatedKey = getGeneratedKey();
if (returnGeneratedKeys && generatedKey.isPresent()) {
return new GeneratedKeysResultSet(routeResult.getGeneratedKeys().iterator(), generatedKey.get().getColumn(), this);
}
if (1 == routedStatements.size()) {
return routedStatements.iterator().next().getGeneratedKeys();
}
return new GeneratedKeysResultSet();
}

private Optional<GeneratedKey> getGeneratedKey() {
if (null != routeResult && routeResult.getSqlStatement() instanceof InsertStatement) {
return Optional.fromNullable(((InsertStatement) routeResult.getSqlStatement()).getGeneratedKey());
}
return Optional.absent();
}

@Override
public ResultSet getResultSet() throws SQLException {
if (null != currentResultSet) {
Expand Down

0 comments on commit fdeac9b

Please sign in to comment.