Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
48c118bf
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,发现更多精彩内容 >>
提交
48c118bf
编写于
5月 10, 2017
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor SQLRewriteEngine
上级
2543b09c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
38 addition
and
29 deletion
+38
-29
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
...ngdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
+38
-29
未找到文件。
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/rewrite/SQLRewriteEngine.java
浏览文件 @
48c118bf
...
...
@@ -47,12 +47,12 @@ public final class SQLRewriteEngine {
*/
public
SQLBuilder
rewrite
()
{
if
(
null
==
sqlBuilder
)
{
sqlBuilder
=
rewriteInternal
(
sqlBuilderContext
);
sqlBuilder
=
rewriteInternal
();
}
return
sqlBuilder
;
}
private
SQLBuilder
rewriteInternal
(
final
SQLBuilderContext
sqlBuilderContext
)
{
private
SQLBuilder
rewriteInternal
()
{
SQLBuilder
result
=
new
SQLBuilder
();
if
(
sqlBuilderContext
.
getSqlTokens
().
isEmpty
())
{
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
());
...
...
@@ -66,36 +66,13 @@ public final class SQLRewriteEngine {
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
().
substring
(
0
,
each
.
getBeginPosition
()));
}
if
(
each
instanceof
TableToken
)
{
String
tableName
=
((
TableToken
)
each
).
getTableName
();
if
(
sqlBuilderContext
.
getTableNames
().
contains
(
tableName
))
{
result
.
appendToken
(
tableName
);
int
beginPosition
=
each
.
getBeginPosition
()
+
((
TableToken
)
each
).
getOriginalLiterals
().
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
().
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
().
substring
(
beginPosition
,
endPosition
));
}
else
{
result
.
append
(((
TableToken
)
each
).
getOriginalLiterals
());
int
beginPosition
=
each
.
getBeginPosition
()
+
((
TableToken
)
each
).
getOriginalLiterals
().
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
().
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
().
substring
(
beginPosition
,
endPosition
));
}
appendTableToken
(
result
,
(
TableToken
)
each
,
count
,
sqlTokens
);
}
else
if
(
each
instanceof
ItemsToken
)
{
for
(
String
item
:
((
ItemsToken
)
each
).
getItems
())
{
result
.
append
(
", "
);
result
.
append
(
item
);
}
int
beginPosition
=
each
.
getBeginPosition
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
().
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
().
substring
(
beginPosition
,
endPosition
));
appendItemsToken
(
result
,
(
ItemsToken
)
each
,
count
,
sqlTokens
);
}
else
if
(
each
instanceof
RowCountLimitToken
)
{
result
.
appendToken
(
RowCountLimitToken
.
COUNT_NAME
,
((
RowCountLimitToken
)
each
).
getRowCount
()
+
""
);
int
beginPosition
=
each
.
getBeginPosition
()
+
(((
RowCountLimitToken
)
each
).
getRowCount
()
+
""
).
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
().
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
().
substring
(
beginPosition
,
endPosition
));
appendLimitRowCount
(
result
,
(
RowCountLimitToken
)
each
,
count
,
sqlTokens
);
}
else
if
(
each
instanceof
OffsetLimitToken
)
{
result
.
appendToken
(
OffsetLimitToken
.
OFFSET_NAME
,
((
OffsetLimitToken
)
each
).
getOffset
()
+
""
);
int
beginPosition
=
each
.
getBeginPosition
()
+
(((
OffsetLimitToken
)
each
).
getOffset
()
+
""
).
length
();
int
endPosition
=
sqlTokens
.
size
()
-
1
==
count
?
sqlBuilderContext
.
getOriginalSQL
().
length
()
:
sqlTokens
.
get
(
count
+
1
).
getBeginPosition
();
result
.
append
(
sqlBuilderContext
.
getOriginalSQL
().
substring
(
beginPosition
,
endPosition
));
appendLimitOffsetToken
(
result
,
(
OffsetLimitToken
)
each
,
count
,
sqlTokens
);
}
count
++;
}
...
...
@@ -111,4 +88,36 @@ 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
();
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
));
}
private
void
appendItemsToken
(
final
SQLBuilder
sqlBuilder
,
final
ItemsToken
itemsToken
,
final
int
count
,
final
List
<
SQLToken
>
sqlTokens
)
{
for
(
String
item
:
itemsToken
.
getItems
())
{
sqlBuilder
.
append
(
", "
);
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
));
}
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
));
}
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
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录