前面几篇文章介绍了SQL解析,SQL路由,SQL改写的过程,包括组装成最后的Collection<PreparedStatementUnit>
这篇文章就开始介绍SQL的执行了
public boolean execute() throws SQLException {
try {
① Collection<PreparedStatementUnit> preparedStatementUnits = route();
② return new PreparedStatementExecutor(
getConnection().getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), preparedStatementUnits, getParameters()).execute();
} finally {
clearBatch();
}
}
①也就是我们之前文章介绍的过程
下面我们重点介绍②,我们看到这里其实分为两步:
1.生成PreparedStatementExecutor对象,也就是执行sql的对象
2.调用PreparedStatementExecutor的execute, executeUpdate, executeQuery 方法,进行SQL的执行
这里我们主要以execute方法进行详细展开
public boolean execute() throws SQLException {
List<Boolean> result = executorEngine.executePreparedStatement(sqlType, preparedStatementUnits, parameters, new ExecuteCallback<Boolean>() {
@Override
public Boolean execute(final BaseStatementUnit baseStatementUnit) throws Exception {
return ((PreparedStatement) baseStatementUnit.getStatement()).execute();
}
});
if (null == result || result.isEmpty() || null == result.get(0)) {
return false;
}
return result.get(0);
}