Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
8e2c6e1b
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8e2c6e1b
编写于
9月 01, 2017
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor WhereClauseParser
上级
809817b6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
41 addition
and
35 deletion
+41
-35
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/clause/WhereClauseParser.java
...rdb/sharding/parsing/parser/clause/WhereClauseParser.java
+41
-35
未找到文件。
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/clause/WhereClauseParser.java
浏览文件 @
8e2c6e1b
...
...
@@ -70,39 +70,48 @@ public class WhereClauseParser implements SQLClauseParser {
private
void
parseComparisonCondition
(
final
ShardingRule
shardingRule
,
final
SQLStatement
sqlStatement
,
final
List
<
SelectItem
>
items
)
{
lexerEngine
.
skipIfEqual
(
Symbol
.
LEFT_PAREN
);
SQLExpression
left
=
expressionClauseParser
.
parse
(
sqlStatement
);
if
(
lexerEngine
.
equalAny
(
Symbol
.
EQ
))
{
if
(
lexerEngine
.
skipIfEqual
(
Symbol
.
EQ
))
{
parseEqualCondition
(
shardingRule
,
sqlStatement
,
left
);
lexerEngine
.
skipIfEqual
(
Symbol
.
RIGHT_PAREN
);
return
;
}
if
(
lexerEngine
.
equalAny
(
DefaultKeyword
.
IN
))
{
if
(
lexerEngine
.
skipIfEqual
(
DefaultKeyword
.
IN
))
{
parseInCondition
(
shardingRule
,
sqlStatement
,
left
);
lexerEngine
.
skipIfEqual
(
Symbol
.
RIGHT_PAREN
);
return
;
}
if
(
lexerEngine
.
equalAny
(
DefaultKeyword
.
BETWEEN
))
{
if
(
lexerEngine
.
skipIfEqual
(
DefaultKeyword
.
BETWEEN
))
{
parseBetweenCondition
(
shardingRule
,
sqlStatement
,
left
);
lexerEngine
.
skipIfEqual
(
Symbol
.
RIGHT_PAREN
);
return
;
}
if
(
lexerEngine
.
equalAny
(
Symbol
.
LT
,
Symbol
.
GT
,
Symbol
.
LT_EQ
,
Symbol
.
G
T_EQ
))
{
if
(
lexerEngine
.
skipIfEqual
(
Symbol
.
LT
,
Symbol
.
L
T_EQ
))
{
if
(
left
instanceof
SQLIdentifierExpression
&&
sqlStatement
instanceof
SelectStatement
&&
isRowNumberCondition
(
items
,
((
SQLIdentifierExpression
)
left
).
getName
()))
{
parseRow
Number
Condition
((
SelectStatement
)
sqlStatement
);
parseRow
Count
Condition
((
SelectStatement
)
sqlStatement
);
}
else
if
(
left
instanceof
SQLPropertyExpression
&&
sqlStatement
instanceof
SelectStatement
&&
isRowNumberCondition
(
items
,
((
SQLPropertyExpression
)
left
).
getName
()))
{
parseRow
Number
Condition
((
SelectStatement
)
sqlStatement
);
parseRow
Count
Condition
((
SelectStatement
)
sqlStatement
);
}
else
{
parseOtherCondition
(
sqlStatement
);
}
}
else
if
(
lexerEngine
.
equalAny
(
Symbol
.
LT_GT
,
Symbol
.
BANG_EQ
,
Symbol
.
BANG_GT
,
Symbol
.
BANG_LT
,
DefaultKeyword
.
LIKE
))
{
}
else
if
(
lexerEngine
.
skipIfEqual
(
Symbol
.
GT
,
Symbol
.
GT_EQ
))
{
if
(
left
instanceof
SQLIdentifierExpression
&&
sqlStatement
instanceof
SelectStatement
&&
isRowNumberCondition
(
items
,
((
SQLIdentifierExpression
)
left
).
getName
()))
{
parseOffsetCondition
((
SelectStatement
)
sqlStatement
);
}
else
if
(
left
instanceof
SQLPropertyExpression
&&
sqlStatement
instanceof
SelectStatement
&&
isRowNumberCondition
(
items
,
((
SQLPropertyExpression
)
left
).
getName
()))
{
parseOffsetCondition
((
SelectStatement
)
sqlStatement
);
}
else
{
parseOtherCondition
(
sqlStatement
);
}
}
else
if
(
lexerEngine
.
skipIfEqual
(
Symbol
.
LT_GT
,
Symbol
.
BANG_EQ
,
Symbol
.
BANG_GT
,
Symbol
.
BANG_LT
,
DefaultKeyword
.
LIKE
))
{
parseOtherCondition
(
sqlStatement
);
}
lexerEngine
.
skipIfEqual
(
Symbol
.
RIGHT_PAREN
);
}
private
void
parseEqualCondition
(
final
ShardingRule
shardingRule
,
final
SQLStatement
sqlStatement
,
final
SQLExpression
left
)
{
lexerEngine
.
nextToken
();
SQLExpression
right
=
expressionClauseParser
.
parse
(
sqlStatement
);
// TODO if have more tables, and cannot find column belong to, should not add to condition, should parse binding table rule.
if
((
sqlStatement
.
getTables
().
isSingleTable
()
||
left
instanceof
SQLPropertyExpression
)
...
...
@@ -115,13 +124,10 @@ public class WhereClauseParser implements SQLClauseParser {
}
private
void
parseInCondition
(
final
ShardingRule
shardingRule
,
final
SQLStatement
sqlStatement
,
final
SQLExpression
left
)
{
lexerEngine
.
nextToken
();
lexerEngine
.
accept
(
Symbol
.
LEFT_PAREN
);
List
<
SQLExpression
>
rights
=
new
LinkedList
<>();
do
{
if
(
lexerEngine
.
equalAny
(
Symbol
.
COMMA
))
{
lexerEngine
.
nextToken
();
}
lexerEngine
.
skipIfEqual
(
Symbol
.
COMMA
);
rights
.
add
(
expressionClauseParser
.
parse
(
sqlStatement
));
}
while
(!
lexerEngine
.
equalAny
(
Symbol
.
RIGHT_PAREN
));
Optional
<
Column
>
column
=
find
(
sqlStatement
.
getTables
(),
left
);
...
...
@@ -132,7 +138,6 @@ public class WhereClauseParser implements SQLClauseParser {
}
private
void
parseBetweenCondition
(
final
ShardingRule
shardingRule
,
final
SQLStatement
sqlStatement
,
final
SQLExpression
left
)
{
lexerEngine
.
nextToken
();
List
<
SQLExpression
>
rights
=
new
LinkedList
<>();
rights
.
add
(
expressionClauseParser
.
parse
(
sqlStatement
));
lexerEngine
.
accept
(
DefaultKeyword
.
AND
);
...
...
@@ -147,36 +152,37 @@ public class WhereClauseParser implements SQLClauseParser {
return
false
;
}
private
void
parseRowNumberCondition
(
final
SelectStatement
selectStatement
)
{
Symbol
symbol
=
(
Symbol
)
lexerEngine
.
getCurrentToken
().
getType
();
lexerEngine
.
nextToken
();
private
void
parseRowCountCondition
(
final
SelectStatement
selectStatement
)
{
SQLExpression
sqlExpression
=
expressionClauseParser
.
parse
(
selectStatement
);
if
(
null
==
selectStatement
.
getLimit
())
{
selectStatement
.
setLimit
(
new
Limit
(
false
));
}
if
(
Symbol
.
LT
==
symbol
||
Symbol
.
LT_EQ
==
symbol
)
{
if
(
sqlExpression
instanceof
SQLNumberExpression
)
{
int
rowCount
=
((
SQLNumberExpression
)
sqlExpression
).
getNumber
().
intValue
();
selectStatement
.
getLimit
().
setRowCount
(
new
LimitValue
(
rowCount
,
-
1
));
selectStatement
.
getSqlTokens
().
add
(
new
RowCountToken
(
lexerEngine
.
getCurrentToken
().
getEndPosition
()
-
String
.
valueOf
(
rowCount
).
length
()
-
lexerEngine
.
getCurrentToken
().
getLiterals
().
length
(),
rowCount
));
}
else
if
(
sqlExpression
instanceof
SQLPlaceholderExpression
)
{
selectStatement
.
getLimit
().
setRowCount
(
new
LimitValue
(-
1
,
((
SQLPlaceholderExpression
)
sqlExpression
).
getIndex
()));
}
}
else
if
(
Symbol
.
GT
==
symbol
||
Symbol
.
GT_EQ
==
symbol
)
{
if
(
sqlExpression
instanceof
SQLNumberExpression
)
{
int
offset
=
((
SQLNumberExpression
)
sqlExpression
).
getNumber
().
intValue
();
selectStatement
.
getLimit
().
setOffset
(
new
LimitValue
(
offset
,
-
1
));
selectStatement
.
getSqlTokens
().
add
(
new
OffsetToken
(
lexerEngine
.
getCurrentToken
().
getEndPosition
()
-
String
.
valueOf
(
offset
).
length
()
-
lexerEngine
.
getCurrentToken
().
getLiterals
().
length
(),
offset
));
}
else
if
(
sqlExpression
instanceof
SQLPlaceholderExpression
)
{
selectStatement
.
getLimit
().
setOffset
(
new
LimitValue
(-
1
,
((
SQLPlaceholderExpression
)
sqlExpression
).
getIndex
()));
}
if
(
sqlExpression
instanceof
SQLNumberExpression
)
{
int
rowCount
=
((
SQLNumberExpression
)
sqlExpression
).
getNumber
().
intValue
();
selectStatement
.
getLimit
().
setRowCount
(
new
LimitValue
(
rowCount
,
-
1
));
selectStatement
.
getSqlTokens
().
add
(
new
RowCountToken
(
lexerEngine
.
getCurrentToken
().
getEndPosition
()
-
String
.
valueOf
(
rowCount
).
length
()
-
lexerEngine
.
getCurrentToken
().
getLiterals
().
length
(),
rowCount
));
}
else
if
(
sqlExpression
instanceof
SQLPlaceholderExpression
)
{
selectStatement
.
getLimit
().
setRowCount
(
new
LimitValue
(-
1
,
((
SQLPlaceholderExpression
)
sqlExpression
).
getIndex
()));
}
}
private
void
parseOffsetCondition
(
final
SelectStatement
selectStatement
)
{
SQLExpression
sqlExpression
=
expressionClauseParser
.
parse
(
selectStatement
);
if
(
null
==
selectStatement
.
getLimit
())
{
selectStatement
.
setLimit
(
new
Limit
(
false
));
}
if
(
sqlExpression
instanceof
SQLNumberExpression
)
{
int
offset
=
((
SQLNumberExpression
)
sqlExpression
).
getNumber
().
intValue
();
selectStatement
.
getLimit
().
setOffset
(
new
LimitValue
(
offset
,
-
1
));
selectStatement
.
getSqlTokens
().
add
(
new
OffsetToken
(
lexerEngine
.
getCurrentToken
().
getEndPosition
()
-
String
.
valueOf
(
offset
).
length
()
-
lexerEngine
.
getCurrentToken
().
getLiterals
().
length
(),
offset
));
}
else
if
(
sqlExpression
instanceof
SQLPlaceholderExpression
)
{
selectStatement
.
getLimit
().
setOffset
(
new
LimitValue
(-
1
,
((
SQLPlaceholderExpression
)
sqlExpression
).
getIndex
()));
}
}
private
void
parseOtherCondition
(
final
SQLStatement
sqlStatement
)
{
lexerEngine
.
nextToken
();
expressionClauseParser
.
parse
(
sqlStatement
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录