Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
39345bbe
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,发现更多精彩内容 >>
提交
39345bbe
编写于
6月 03, 2017
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor append for SQLBuilder
上级
468e5a1f
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
25 addition
and
39 deletion
+25
-39
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLBuilder.java
...com/dangdang/ddframe/rdb/sharding/rewrite/SQLBuilder.java
+13
-27
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
...ngdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
+12
-12
未找到文件。
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLBuilder.java
浏览文件 @
39345bbe
...
...
@@ -49,51 +49,37 @@ public final class SQLBuilder {
}
/**
*
增加占位符
.
*
追加字面量
.
*
* @param
token 占位符
* @param
literals 字面量
*/
public
void
append
Token
(
final
String
token
)
{
appendToken
(
token
,
token
);
public
void
append
(
final
String
literals
)
{
currentSegment
.
append
(
literals
);
}
/**
*
增
加占位符.
*
追
加占位符.
*
* @param label 占位符标签
* @param token 占位符
*/
public
void
appendToken
(
final
String
label
,
final
String
token
)
{
SQLBuilderToken
stringToken
;
if
(
tokenMap
.
containsKey
(
label
))
{
stringToken
=
tokenMap
.
get
(
label
);
}
else
{
stringToken
=
new
SQLBuilderToken
(
label
,
token
);
tokenMap
.
put
(
label
,
stringToken
);
public
void
append
(
final
SQLBuilderToken
token
)
{
if
(!
tokenMap
.
containsKey
(
token
.
getLabel
()))
{
tokenMap
.
put
(
token
.
getLabel
(),
token
);
}
stringToken
.
getIndexes
().
add
(
segments
.
size
());
segments
.
add
(
stringToken
);
tokenMap
.
get
(
token
.
getLabel
())
.
getIndexes
().
add
(
segments
.
size
());
segments
.
add
(
tokenMap
.
get
(
token
.
getLabel
())
);
currentSegment
=
new
StringBuilder
();
segments
.
add
(
currentSegment
);
}
/**
* 追加字面量.
*
* @param literals 字面量
*/
public
void
append
(
final
String
literals
)
{
currentSegment
.
append
(
literals
);
}
/**
* 用实际的值替代占位符,并返回新的构建器.
*
* @return 新SQL构建器
*/
public
SQLBuilder
createNewSQLBuilder
(
final
Collection
<
SQLBuilderToken
>
stringT
okens
)
{
public
SQLBuilder
createNewSQLBuilder
(
final
Collection
<
SQLBuilderToken
>
t
okens
)
{
SQLBuilder
result
=
new
SQLBuilder
(
this
);
for
(
SQLBuilderToken
each
:
stringT
okens
)
{
for
(
SQLBuilderToken
each
:
t
okens
)
{
SQLBuilderToken
origin
=
tokenMap
.
get
(
each
.
getLabel
());
result
.
tokenMap
.
put
(
each
.
getLabel
(),
each
);
for
(
Integer
index
:
origin
.
getIndexes
())
{
...
...
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
浏览文件 @
39345bbe
...
...
@@ -103,7 +103,7 @@ public final class SQLRewriteEngine {
private
void
appendTableToken
(
final
SQLBuilder
sqlBuilder
,
final
TableToken
tableToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
)
{
String
tableName
=
tableNames
.
contains
(
tableToken
.
getTableName
())
?
tableToken
.
getTableName
()
:
tableToken
.
getOriginalLiterals
();
sqlBuilder
.
append
Token
(
tableName
);
sqlBuilder
.
append
(
new
SQLBuilderToken
(
tableName
,
tableName
)
);
int
beginPosition
=
tableToken
.
getBeginPosition
()
+
tableToken
.
getOriginalLiterals
().
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
originalSQL
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
originalSQL
.
substring
(
beginPosition
,
endPosition
));
...
...
@@ -121,7 +121,7 @@ public final class SQLRewriteEngine {
private
void
appendLimitRowCount
(
final
SQLBuilder
sqlBuilder
,
final
RowCountLimitToken
rowCountLimitToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
,
final
boolean
isRewrite
)
{
String
rowCount
=
isRewrite
?
String
.
valueOf
(
rowCountLimitToken
.
getRowCount
()
+
limit
.
getOffset
())
:
String
.
valueOf
(
rowCountLimitToken
.
getRowCount
());
sqlBuilder
.
append
Token
(
RowCountLimitToken
.
COUNT_NAME
,
rowCount
);
sqlBuilder
.
append
(
new
SQLBuilderToken
(
RowCountLimitToken
.
COUNT_NAME
,
rowCount
)
);
int
beginPosition
=
rowCountLimitToken
.
getBeginPosition
()
+
String
.
valueOf
(
rowCountLimitToken
.
getRowCount
()).
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
originalSQL
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
originalSQL
.
substring
(
beginPosition
,
endPosition
));
...
...
@@ -129,7 +129,7 @@ public final class SQLRewriteEngine {
private
void
appendLimitOffsetToken
(
final
SQLBuilder
sqlBuilder
,
final
OffsetLimitToken
offsetLimitToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
,
final
boolean
isRewrite
)
{
String
offset
=
isRewrite
?
"0"
:
String
.
valueOf
(
offsetLimitToken
.
getOffset
());
sqlBuilder
.
append
Token
(
OffsetLimitToken
.
OFFSET_NAME
,
offset
);
sqlBuilder
.
append
(
new
SQLBuilderToken
(
OffsetLimitToken
.
OFFSET_NAME
,
offset
)
);
int
beginPosition
=
offsetLimitToken
.
getBeginPosition
()
+
String
.
valueOf
(
offsetLimitToken
.
getOffset
()).
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
originalSQL
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
originalSQL
.
substring
(
beginPosition
,
endPosition
));
...
...
@@ -144,13 +144,13 @@ public final class SQLRewriteEngine {
* @return SQL构建器
*/
public
SQLBuilder
rewriteTable
(
final
TableUnit
tableUnit
,
final
SQLBuilder
sqlBuilder
,
final
ShardingRule
shardingRule
)
{
Collection
<
SQLBuilderToken
>
stringT
okens
=
new
LinkedList
<>();
stringT
okens
.
add
(
new
SQLBuilderToken
(
tableUnit
.
getLogicTableName
(),
tableUnit
.
getActualTableName
()));
Collection
<
SQLBuilderToken
>
t
okens
=
new
LinkedList
<>();
t
okens
.
add
(
new
SQLBuilderToken
(
tableUnit
.
getLogicTableName
(),
tableUnit
.
getActualTableName
()));
Optional
<
BindingTableRule
>
bindingTableRule
=
shardingRule
.
findBindingTableRule
(
tableUnit
.
getLogicTableName
());
if
(
bindingTableRule
.
isPresent
())
{
stringTokens
.
addAll
(
getBindingTableTokens
(
tableUnit
,
sqlBuilder
,
bindingTableRule
.
get
()));
tokens
.
addAll
(
getBindingTableTokens
(
tableUnit
,
bindingTableRule
.
get
()));
}
return
sqlBuilder
.
createNewSQLBuilder
(
stringT
okens
);
return
sqlBuilder
.
createNewSQLBuilder
(
t
okens
);
}
/**
...
...
@@ -162,18 +162,18 @@ public final class SQLRewriteEngine {
* @return SQL构建器
*/
public
SQLBuilder
rewriteTable
(
final
CartesianTableReference
cartesianTableReference
,
final
SQLBuilder
sqlBuilder
,
final
ShardingRule
shardingRule
)
{
Collection
<
SQLBuilderToken
>
stringT
okens
=
new
LinkedList
<>();
Collection
<
SQLBuilderToken
>
t
okens
=
new
LinkedList
<>();
for
(
TableUnit
each
:
cartesianTableReference
.
getTableUnits
())
{
stringT
okens
.
add
(
new
SQLBuilderToken
(
each
.
getLogicTableName
(),
each
.
getActualTableName
()));
t
okens
.
add
(
new
SQLBuilderToken
(
each
.
getLogicTableName
(),
each
.
getActualTableName
()));
Optional
<
BindingTableRule
>
bindingTableRule
=
shardingRule
.
findBindingTableRule
(
each
.
getLogicTableName
());
if
(
bindingTableRule
.
isPresent
())
{
stringTokens
.
addAll
(
getBindingTableTokens
(
each
,
sqlBuilder
,
bindingTableRule
.
get
()));
tokens
.
addAll
(
getBindingTableTokens
(
each
,
bindingTableRule
.
get
()));
}
}
return
sqlBuilder
.
createNewSQLBuilder
(
stringT
okens
);
return
sqlBuilder
.
createNewSQLBuilder
(
t
okens
);
}
private
Collection
<
SQLBuilderToken
>
getBindingTableTokens
(
final
TableUnit
tableUnit
,
final
SQLBuilder
sqlBuilder
,
final
BindingTableRule
bindingTableRule
)
{
private
Collection
<
SQLBuilderToken
>
getBindingTableTokens
(
final
TableUnit
tableUnit
,
final
BindingTableRule
bindingTableRule
)
{
Collection
<
SQLBuilderToken
>
result
=
new
LinkedList
<>();
for
(
String
eachTable
:
tableNames
)
{
if
(!
eachTable
.
equalsIgnoreCase
(
tableUnit
.
getLogicTableName
())
&&
bindingTableRule
.
hasLogicTable
(
eachTable
))
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录