当前位置:网站首页>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有所帮助.
️ 感谢大家
如果你觉得这篇内容对你挺有有帮助的话:
- 欢迎关注我️,点赞,评论,转发
- 关注
盼盼小课堂
,定期为你推送好文,还有群聊不定期抽奖活动,可以畅所欲言,与大神们一起交流,一起学习. - 有不当之处欢迎批评指正.
边栏推荐
- [Message Notification] How about using the official account template message?
- pdb药物综合数据库
- Dart 命名参数语法
- One service layer needs to call the other two service layers to obtain data and assemble it into the final data. The data is all lists. How to design the cache?
- Introduction to the Elastic Stack
- The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation
- 带wiringPi库在unbutu 编译 并且在树莓派运行
- Lua introductory case of actual combat 1234 custom function and the standard library function
- Flutter "Hello world" program code
- 【消息通知】用公众号模板消息怎么样?
猜你喜欢
智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
leetcode6132. Make all elements in an array equal to zero (simple, weekly)
ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
Input输入框光标在前输入后自动跳到最后面的bug
The fledgling Xiao Li's 114th blog project notes: Wisdom cloud intelligent flower watering device combat (3) - basic Demo implementation
Message queue MySQL table for storing message data
如何下载Keil包
TypeScript simplifies running ts-node
普通用户无法访问hgfs目录
随机推荐
HCIP(15)
阿叶的目标
test
Take you to experience a type programming practice
【uniCloud】云对象的应用与提升
Dart named parameter syntax
button去除黑框
更换树莓派内核
【 Make YOLO Great Again 】 YOLOv1 v7 full range with large parsing (Neck)
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
Error using ts-node
"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
leetcode:126. Word Solitaire II
一个往年的朋友
Dynamic Programming 01 Backpack
Nmap 操作手册 - 完整版
[Message Notification] How about using the official account template message?
带wiringPi库在unbutu 编译 并且在树莓派运行
《少年派2》:新男友竟脚踩两只船,林妙妙与钱三一感情回温
Flutter “Hello world“ 程代码