sharding-jdbc的源码分析一

路由的输入与执行

ShardingPreparedStatement的route()方法如下,routingEngine在构造时会传入需要路由的sql字符串以及对应的Connection关联的ShardingContext(一个javaBean),在执行路由的时候,route()方法会传入setParameter获得的参数,根据参数、参数值以及SQL进行路由分析产生执行单元PreparedStatementUnit列表,之后将PreparedStatementUnit列表传到谷歌的Excutor框架去对里面路由后的各个SQL通过PreparedStatement执行。
这里的getParameters()实际上获取的是父类的属性,在父类已经定义了,父类是AbstractShardingPreparedStatementAdapter抽象类,再通过几层关系实现了PreparedStatement的接口,ShardingPreparedStatement通过子类继承的方式封装并实现了PreparedStatement接口规范,实现了excuteQuery等方法,ShardingPreparedStatement的创建在ShardingConnection里进行创建

 private Collection route() throws SQLException {
        Collection result = new LinkedList<>();
        routeResult = routingEngine.route(getParameters());
        for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
            SQLType sqlType = routeResult.getSqlStatement().getType();
            Collection preparedStatements;
            if (SQLType.DDL == sqlType) {
                preparedStatements = generatePreparedStatementForDDL(each);
            } else {
                preparedStatements = Collections.singletonList(generatePreparedStatement(each));
            }
            routedStatements.addAll(preparedStatements);
            for (PreparedStatement preparedStatement : preparedStatements) {
                replaySetParameter(preparedStatement);
                result.add(new PreparedStatementUnit(each, preparedStatement));
            }
        }
        return result;
    }
    public ShardingPreparedStatement(final ShardingConnection connection, final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
        this.connection = connection;
        this.resultSetType = resultSetType;
        this.resultSetConcurrency = resultSetConcurrency;
        this.resultSetHoldability = resultSetHoldability;
        routingEngine = new PreparedStatementRoutingEngine(sql, connection.getShardingContext());
    }

SQL的解析到路由的解析到SQL的执行

从ShardingPreparedStatement的route()方法跟进下去,可以发现路由解析的过程是在PreparedStatementRoutingEngine里面,PreparedStatementRoutingEngine是上面说到的routingEngine的具体实现,在构造他的时候,他获取了sql字符串和Connection连接关联的ShardingContext(一个javaBean),在调用route()的时候获取了PreparedStatement的参数,因此路由解析的参数的三个输入就确定了下来;PreparedStatementRoutingEngine里面有三个属性以及一个方法,看到这里,基本上可以确定路由算法就是在SQLRouter接口的实现类实现的,从构造方法里面跟进去可以发现SQLRouter的实现类在2.0版本有两个,一个是DatabaseHintSQLRouter,一个是ParsingSQLRouter,他们都实现了SQLRouter的parse和route方法,parse解析的结果封装在SQLStatement接口,route的结果封装在SQLRouteResult(一个Bean)。DMLStatement继承AbstractSQLStatement从而实现SQLStatement接口,DMLStatement根据SQL的类型派生了例如InsertStatement、updateStatement等子类(alter、create、drop、insert、select、truncate、update)。
实际上这里的SQLStatement命名应该是参考了mysql-connector包里面的从下面的代码 route(final List parameters)我们也可以知道,sqlStatement封装的是SQL解析后的结果,sqlRouter利用了该结果去对SQL进行路由,封装成SQLRouteResult,最后SQL的执行就是利用了SQLRouteResult里面的信息,sqlRouter里面方法的执行顺序是先parse后route。

public final class PreparedStatementRoutingEngine {
    
    private final String logicSQL;
    
    private final SQLRouter sqlRouter;
    
    private SQLStatement sqlStatement;
    
    public PreparedStatementRoutingEngine(final String logicSQL, final ShardingContext shardingContext) {
        this.logicSQL = logicSQL;
        sqlRouter = SQLRouterFactory.createSQLRouter(shardingContext);
    }
    
    /**
     * SQL route.
     * 
     * 

First routing time will parse SQL, after second time will reuse first parsed result.

* * @param parameters parameters of SQL placeholder * @return route result */ public SQLRouteResult route(final List parameters) { if (null == sqlStatement) { sqlStatement = sqlRouter.parse(logicSQL, parameters.size()); } return sqlRouter.route(logicSQL, parameters, sqlStatement); } }

你可能感兴趣的:(sharding-jdbc的源码分析一)