入口分析:
-
org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement#execute
这里是分库分表的切入口
-
org.apache.shardingsphere.core.BaseShardingEngine#shard
3. org.apache.shardingsphere.core.PreparedQueryShardingEngine#route
4. org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine#route
这里开始对逻辑sql进行解析
4.1. org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter#parse
4.2 org.apache.shardingsphere.core.parse.SQLParsingEngine#parse
- 首先判断缓存中是否存在解析结果
- 解析逻辑sql到SQLStatement
- org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter#route
6.1 org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine#route()
6.2 org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine#getDataNodes
6.3 org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine#routeByShardingConditions
6.4 org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine#routeByShardingConditionsWithCondition
6.5 org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine#routeTables
这里会根据是否含有分片键来判断是直接查询所有表还是根据分片键查询对应的实际表。
6.6 org.apache.shardingsphere.core.strategy.route.standard.StandardShardingStrategy#doSharding
这里最终会进入到你定义的分表规则,去获取到最终真实要查询的表名称
6.7 com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm#doSharding
7. org.apache.shardingsphere.core.BaseShardingEngine#rewriteAndConvert
重写sql语句
7.1 org.apache.shardingsphere.core.rewrite.SQLRewriteEngine#rewrite
在这里将表名进行改写:
insert int user(id,name) values(1, ‘xly’);
改写为
insert into user_1(id,name) values(1, ‘xly’);
到这里就结束了源码的解析。