sharding-jdbc源码解析-sql解析(五)
SQL解析流程
SQL解析引擎
SQLParsingEngine
sql分析引擎
会先使用词法分析引擎进行词法分析
1 |
|
SQLParserFactory
sql解析器工厂,负责根据sql第一个标记类型创建出相应的解析器
解析器工厂(负责根据数据库类型选择具体解析器)分类:
SELECT<—>SelectParserFactory、INSERT<—>InsertParserFactory、UPDATE<—>UpdateParserFactory、DELETE<—>DeleteParserFactory、CREATE<—>CreateParserFactory、ALTER<—>AlterParserFactory、DROP<—>DropParserFactory、TRUNCATE<—>TruncateParserFactory
解析器分类:
SELECT<—>AbstractSelectParser、INSERT<—>AbstractInsertParser、UPDATE<—>AbstractUpdateParser、DELETE<—>AbstractDeleteParser、CREATE<—>AbstractCreateParser、ALTER<—>AbstractAlterParser、DROP<—>AbstractDropParser、TRUNCATE<—>AbstractTruncateParser
SQL解析器
SQLParser
解析器
1 | public interface SQLParser { |
插入
以插入为例
插入语句解析器
1 | public abstract class AbstractInsertParser implements SQLParser { |
AbstractInsertClauseParserFacade
门面模式
1 |
|
InsertIntoClauseParser
INTO 部分解析
1 | public void parse(final InsertStatement insertStatement) { |
表解析器MySQLTableReferencesClauseParser
1 | public final void parse(final SQLStatement sqlStatement, final boolean isSingleTableOnly) { |
别名解析器AliasClauseParser
1 | public Optional<String> parse() { |
InsertColumnsClauseParser
列 部分解析
1 | public void parse(final InsertStatement insertStatement) { |
InsertValuesClauseParser
1 | public void parse(final InsertStatement insertStatement) { |
查询
select语法
1 | SELECT |
select_expr 指的是你想获取的列,至少要有一个
table_references 指的是表或者表中的行
1 | tbl_name [[AS] alias] [index_hint_list] |
select … partition 分区 。https://dev.mysql.com/doc/refman/5.7/en/partitioning-selection.html
Expression Syntax
1 | expr: |
Operator Precedence
运算符的优先级
1 | INTERVAL |
查询语句解析器
1 |
|
MySQLSelectParser
1 |
|
AbstractSelectClauseParserFacade
1 |
|
解析Distinct
1 |
|
解析Option
1 |
|
解析列
SelectListClauseParser
1 | public void parse(final SelectStatement selectStatement, final List<SelectItem> items) { |
1 | private String parseCommonSelectItem(final SelectStatement selectStatement) { |
解析FROM
1 | protected final void parseFrom(final SelectStatement selectStatement) { |
解析Where
1 | protected final void parseWhere(final ShardingRule shardingRule, final SelectStatement selectStatement, final List<SelectItem> items) { |
1 | private void parseConditions(final ShardingRule shardingRule, final SQLStatement sqlStatement, final List<SelectItem> items) { |
1 | private void parseComparisonCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final List<SelectItem> items) { |
解析Group By
1 | public final void parse(final SelectStatement selectStatement) { |
关于ROLLUP https://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html
解析Group By后面的东西
1 | private void addGroupByItem(final SQLExpression sqlExpression, final SelectStatement selectStatement) { |
合并子查询
1 | public SelectStatement mergeSubQueryStatement() { |