Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
b4286edf
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,发现更多精彩内容 >>
提交
b4286edf
编写于
5月 12, 2017
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove SQLBuilderContext
上级
8e7bc9d4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
24 addition
and
44 deletion
+24
-44
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLBuilderContext.java
...gdang/ddframe/rdb/sharding/rewrite/SQLBuilderContext.java
+0
-26
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
...ngdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
+24
-18
未找到文件。
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLBuilderContext.java
已删除
100644 → 0
浏览文件 @
8e7bc9d4
package
com.dangdang.ddframe.rdb.sharding.rewrite
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.token.SQLToken
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
java.util.Collection
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.TreeSet
;
/**
* SQL构建器上下文.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public
final
class
SQLBuilderContext
{
private
final
String
originalSQL
;
private
final
List
<
SQLToken
>
sqlTokens
=
new
LinkedList
<>();
private
final
Collection
<
String
>
tableNames
=
new
TreeSet
<>(
String
.
CASE_INSENSITIVE_ORDER
);
}
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
浏览文件 @
b4286edf
...
...
@@ -26,9 +26,12 @@ import com.dangdang.ddframe.rdb.sharding.parsing.parser.token.RowCountLimitToken
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.token.SQLToken
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.token.TableToken
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.TreeSet
;
/**
* SQL重写引擎.
...
...
@@ -37,16 +40,20 @@ import java.util.List;
*/
public
final
class
SQLRewriteEngine
{
private
final
SQLBuilderContext
sqlBuilderContext
;
private
final
String
originalSQL
;
private
final
List
<
SQLToken
>
sqlTokens
=
new
LinkedList
<>();
private
final
Collection
<
String
>
tableNames
=
new
TreeSet
<>(
String
.
CASE_INSENSITIVE_ORDER
);
private
SQLBuilder
sqlBuilder
;
public
SQLRewriteEngine
(
final
String
originalSQL
,
final
SQLContext
sqlContext
)
{
sqlBuilderContext
=
new
SQLBuilderContext
(
originalSQL
);
this
.
originalSQL
=
originalSQL
;
sqlTokens
.
addAll
(
sqlContext
.
getSqlTokens
());
for
(
TableContext
each
:
sqlContext
.
getTables
())
{
sqlBuilderContext
.
getTableNames
()
.
add
(
each
.
getName
());
tableNames
.
add
(
each
.
getName
());
}
sqlBuilderContext
.
getSqlTokens
().
addAll
(
sqlContext
.
getSqlTokens
());
}
/**
...
...
@@ -63,16 +70,15 @@ public final class SQLRewriteEngine {
private
SQLBuilder
rewriteInternal
()
{
SQLBuilder
result
=
new
SQLBuilder
();
if
(
sql
BuilderContext
.
getSqlTokens
()
.
isEmpty
())
{
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
()
);
if
(
sql
Tokens
.
isEmpty
())
{
result
.
append
(
originalSQL
);
return
result
;
}
int
count
=
0
;
List
<
SQLToken
>
sqlTokens
=
sqlBuilderContext
.
getSqlTokens
();
sortByBeginPosition
();
for
(
SQLToken
each
:
sqlTokens
)
{
if
(
0
==
count
)
{
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
()
.
substring
(
0
,
each
.
getBeginPosition
()));
result
.
append
(
originalSQL
.
substring
(
0
,
each
.
getBeginPosition
()));
}
if
(
each
instanceof
TableToken
)
{
appendTableToken
(
result
,
(
TableToken
)
each
,
count
,
sqlTokens
);
...
...
@@ -89,7 +95,7 @@ public final class SQLRewriteEngine {
}
private
void
sortByBeginPosition
()
{
Collections
.
sort
(
sql
BuilderContext
.
getSqlTokens
()
,
new
Comparator
<
SQLToken
>()
{
Collections
.
sort
(
sql
Tokens
,
new
Comparator
<
SQLToken
>()
{
@Override
public
int
compare
(
final
SQLToken
o1
,
final
SQLToken
o2
)
{
...
...
@@ -99,11 +105,11 @@ public final class SQLRewriteEngine {
}
private
void
appendTableToken
(
final
SQLBuilder
sqlBuilder
,
final
TableToken
tableToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
)
{
String
tableName
=
sqlBuilderContext
.
getTableNames
()
.
contains
(
tableToken
.
getTableName
())
?
tableToken
.
getTableName
()
:
tableToken
.
getOriginalLiterals
();
String
tableName
=
tableNames
.
contains
(
tableToken
.
getTableName
())
?
tableToken
.
getTableName
()
:
tableToken
.
getOriginalLiterals
();
sqlBuilder
.
appendToken
(
tableName
);
int
beginPosition
=
tableToken
.
getBeginPosition
()
+
tableToken
.
getOriginalLiterals
().
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
()
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
sqlBuilderContext
.
getOriginalSQL
()
.
substring
(
beginPosition
,
endPosition
));
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
originalSQL
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
originalSQL
.
substring
(
beginPosition
,
endPosition
));
}
private
void
appendItemsToken
(
final
SQLBuilder
sqlBuilder
,
final
ItemsToken
itemsToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
)
{
...
...
@@ -112,21 +118,21 @@ public final class SQLRewriteEngine {
sqlBuilder
.
append
(
item
);
}
int
beginPosition
=
itemsToken
.
getBeginPosition
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
()
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
sqlBuilderContext
.
getOriginalSQL
()
.
substring
(
beginPosition
,
endPosition
));
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
originalSQL
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
originalSQL
.
substring
(
beginPosition
,
endPosition
));
}
private
void
appendLimitRowCount
(
final
SQLBuilder
sqlBuilder
,
final
RowCountLimitToken
rowCountLimitToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
)
{
sqlBuilder
.
appendToken
(
RowCountLimitToken
.
COUNT_NAME
,
rowCountLimitToken
.
getRowCount
()
+
""
);
int
beginPosition
=
rowCountLimitToken
.
getBeginPosition
()
+
(
rowCountLimitToken
.
getRowCount
()
+
""
).
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
()
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
sqlBuilderContext
.
getOriginalSQL
()
.
substring
(
beginPosition
,
endPosition
));
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
originalSQL
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
originalSQL
.
substring
(
beginPosition
,
endPosition
));
}
private
void
appendLimitOffsetToken
(
final
SQLBuilder
sqlBuilder
,
final
OffsetLimitToken
offsetLimitToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
)
{
sqlBuilder
.
appendToken
(
OffsetLimitToken
.
OFFSET_NAME
,
offsetLimitToken
.
getOffset
()
+
""
);
int
beginPosition
=
offsetLimitToken
.
getBeginPosition
()
+
(
offsetLimitToken
.
getOffset
()
+
""
).
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
()
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
sqlBuilderContext
.
getOriginalSQL
()
.
substring
(
beginPosition
,
endPosition
));
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
originalSQL
.
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
sqlBuilder
.
append
(
originalSQL
.
substring
(
beginPosition
,
endPosition
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录