当前位置:网站首页>SQL Analysis of ShardingSphere
SQL Analysis of ShardingSphere
2022-08-01 03:58:00 【Jay Chou himself】
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情
本文已参与「开源摘星计划」,欢迎正在阅读的你加入.活动链接:github.com/weopenproje…
ShardingSphere的SQL解析
The source code of this article is based on 4.0.1版本
ShardingSphereThe sharding engine goes from parsing engine to routing engine to rewriting engine to execution engine to merge engine,The sharding operation is processed step by step,We start this article with the parsing engine,深入分析一下SqlThe parsing engine processing flow.
解析Sql的入口
SQLParseEngine这个类是sqlThe class corresponding to the parsing engine,by looking at itparse()方法,我们知道sqlThe process of parsing is constructionSQLStatement对象的过程,方法中调用了SQLParseKernel来创建对象,然后调用它的parse()方法来完成.So we focus on this method
解析Sql
SQLParseKernel的parse()方法:
public SQLStatement parse() {
SQLAST ast = parserEngine.parse();
Collection<SQLSegment> sqlSegments = extractorEngine.extract(ast);
Map<ParserRuleContext, Integer> parameterMarkerIndexes = ast.getParameterMarkerIndexes();
return fillerEngine.fill(sqlSegments, parameterMarkerIndexes.size(), ast.getSqlStatementRule());
}
复制代码- 将原始SQLParse to abstract syntax tree by parser
- Use an extractor to extract according to extraction rulesSql片段结合
- Use the filler to fill according to fill rulesSql片段生成SQLThe parsed result is returned
These three steps will be looked at in detail below
1. 将 SQL 解析为抽象语法树
This piece is correspondingSQLParserEngine的parse()方法,The main logic of this method is to use the factory classSQLParserFactory来创建Sql解析器实例,Due to different databases correspondingSQLThe parser is also different,So this piece of logic is also usedJava的SPImechanism to create a configurationSQLParserEntry实例对象,Choose according to different database typesSql解析器,最终会生成SQLAST对象,也就是SQL 的抽象语法树.
2. 提取Sql片段
这一步对应的是SQLSegmentsExtractorEngine的extract()方法,What is returned is allSqlA collection of fragments.
Traverse the abstract syntax treeSqlFragment extractor,There are two types of extractors,One is single nodeSqlFragment extractor,Get it directlySql片段,Just put it in a collection,The other is a tree-like nodeSqlFragment extraction line,Then you need to traverse the tree,Put the result into a collection.看!The traversal of the tree of data structures is used,Don't say data structures are useless in the future..
3. 填充Sql片段,Generate parsing results
The third step is to fill inSql片段了,对应的是SQLStatementFillerEngine的fill()方法
public SQLStatement fill(final Collection<SQLSegment> sqlSegments, final int parameterMarkerCount, final SQLStatementRule rule) {
SQLStatement result = rule.getSqlStatementClass().newInstance();
Preconditions.checkArgument(result instanceof AbstractSQLStatement, "%s must extends AbstractSQLStatement", result.getClass().getName());
((AbstractSQLStatement) result).setParametersCount(parameterMarkerCount);
result.getAllSQLSegments().addAll(sqlSegments);
for (SQLSegment each : sqlSegments) {
Optional<SQLSegmentFiller> filler = parseRuleRegistry.findSQLSegmentFiller(databaseTypeName, each.getClass());
if (filler.isPresent()) {
filler.get().fill(each, result);
}
}
return result;
}
复制代码- 获取SQLStatement对象,对SQLStatementCheck for legitimacy
- Sets the number of parameters for the result
- 将上一步中的SQLA collection of fragments is added to the result object
- 遍历Sql片段,根据数据库类型和SqlFragment foundSqlFragment filter,利用SqlFragment filter to populateSql片段
- Finally return the parsed oneSQLStatement
总结
这篇文章我们讲了ShardingSphere的解析Sql的功能,From its entry to its concrete parsingSql的过程,分三步走,The first step will be rawSQL解析成抽象语法树,The second step of extractionSql片段,The third step depends on the database type and SqlThe fragment selects the corresponding filler to fillSql片段,这就是解析Sqlthe overall process,希望对你理解ShardingSphere有所帮助.
️ 感谢大家
如果你觉得这篇内容对你挺有有帮助的话:
- 欢迎关注我️,点赞,评论,转发
- 关注
盼盼小课堂,定期为你推送好文,还有群聊不定期抽奖活动,可以畅所欲言,与大神们一起交流,一起学习. - 有不当之处欢迎批评指正.
边栏推荐
- 如何下载Keil包
- pdb drug comprehensive database
- 软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。
- [Message Notification] How about using the official account template message?
- MySQL4
- 【分层强化学习】HIRO:Data-Efficient Hierarchical Reinforcement Learning
- Message queue design based on mysql
- [kali-information collection] enumeration - DNS enumeration: DNSenum, fierce
- Browser download shortcut to desktop (PWA)
- Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
猜你喜欢
随机推荐
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
【入门教程】Rollup模块打包器整合
IDEA does not recognize the module (there is no blue square in the lower right corner of the module)
【搜索专题】看完必会的BFS解决最短路问题攻略
Error using ts-node
如何下载Keil包
Introduction to the Elastic Stack
lua entry case combat 123DIY
一个往年的朋友
Take you to experience a type programming practice
Dart 命名参数语法
【愚公系列】2022年07月 Go教学课程 024-函数
What is a programming language
Input input box cursor automatically jumps to the last bug after the previous input
在打开MYSQL表时,有的可以显示编辑,有的没有,如何设置。
【分层强化学习】HIRO:Data-Efficient Hierarchical Reinforcement Learning
button去除黑框
Nmap 操作手册 - 完整版
This map drawing tool is amazing, I recommend it~~
Browser download shortcut to desktop (PWA)









