当前位置:网站首页>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有所帮助.
️ 感谢大家
如果你觉得这篇内容对你挺有有帮助的话:
- 欢迎关注我️,点赞,评论,转发
- 关注
盼盼小课堂
,定期为你推送好文,还有群聊不定期抽奖活动,可以畅所欲言,与大神们一起交流,一起学习. - 有不当之处欢迎批评指正.
边栏推荐
- Nmap 操作手册 - 完整版
- When opening a MYSQL table, some can display editing, some do not, how to set.
- 雪糕和轮胎
- This map drawing tool is amazing, I recommend it~~
- second uncle
- A way to deal with infinite debugger
- Open source project site must-have & communication area function
- Ordinary users cannot access HGFS directory
- 初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现
- Unknown Bounded Array
猜你喜欢
如何下载Keil包
leetcode6132. 使数组中所有元素都等于零(简单,周赛)
Flutter "Hello world" program code
What is dynamic programming and what is the knapsack problem
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
Valentine's Day Romantic 3D Photo Wall [with source code]
The fledgling Xiao Li's 114th blog project notes: Wisdom cloud intelligent flower watering device combat (3) - basic Demo implementation
MySQL4
Difference Between Compiled and Interpreted Languages
认真对待每一个时刻
随机推荐
在打开MYSQL表时,有的可以显示编辑,有的没有,如何设置。
Dynamic Programming 01 Backpack
button去除黑框
一个往年的朋友
HCIP(15)
ARM 交叉编译
IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints
预言机简介
Flutter “Hello world“ 程代码
Step by step hand tearing carousel Figure 3 (nanny level tutorial)
lambda
When opening a MYSQL table, some can display editing, some do not, how to set.
动态规划 01背包
What is dynamic programming and what is the knapsack problem
Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink
MySQL modifies SQL statements to optimize performance
Interview Blitz 69: Is TCP Reliable?Why?
软件测试面试(三)
Flutter Tutorial 01 Configure the environment and run the demo program (tutorial includes source code)
New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment