Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
a1460d57
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,发现更多精彩内容 >>
提交
a1460d57
编写于
9月 08, 2019
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
for #2993, simplify for the code
上级
f91f7f4f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
18 addition
and
35 deletion
+18
-35
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingUpdateOptimizeEngine.java
...ize/sharding/engnie/dml/ShardingUpdateOptimizeEngine.java
+6
-22
sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingUpdateOptimizeEngineTest.java
...sharding/engnie/dml/ShardingUpdateOptimizeEngineTest.java
+12
-13
未找到文件。
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingUpdateOptimizeEngine.java
浏览文件 @
a1460d57
...
...
@@ -29,8 +29,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.Assignmen
import
org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
java.util.Collection
;
import
java.util.LinkedList
;
import
java.util.List
;
/**
...
...
@@ -44,34 +42,20 @@ public final class ShardingUpdateOptimizeEngine implements ShardingOptimizeEngin
@Override
public
ShardingConditionOptimizedStatement
optimize
(
final
ShardingRule
shardingRule
,
final
TableMetas
tableMetas
,
final
String
sql
,
final
List
<
Object
>
parameters
,
final
UpdateStatement
sqlStatement
)
{
checkUpdateShardKey
(
shardingRule
,
sqlStatement
);
checkUpdateShard
ing
Key
(
shardingRule
,
sqlStatement
);
WhereClauseShardingConditionEngine
shardingConditionEngine
=
new
WhereClauseShardingConditionEngine
(
shardingRule
,
tableMetas
);
WhereClauseEncryptConditionEngine
encryptConditionEngine
=
new
WhereClauseEncryptConditionEngine
(
shardingRule
.
getEncryptRule
(),
tableMetas
);
return
new
ShardingConditionOptimizedStatement
(
sqlStatement
,
new
ShardingConditions
(
shardingConditionEngine
.
createShardingConditions
(
sqlStatement
,
parameters
)),
new
EncryptConditions
(
encryptConditionEngine
.
createEncryptConditions
(
sqlStatement
)));
}
private
void
checkUpdateShardKey
(
final
ShardingRule
shardingRule
,
final
UpdateStatement
updateStatement
)
{
if
(
updateStatement
.
getTables
().
size
()
>
1
)
{
return
;
}
private
void
checkUpdateShardingKey
(
final
ShardingRule
shardingRule
,
final
UpdateStatement
updateStatement
)
{
String
tableName
=
new
Tables
(
updateStatement
).
getSingleTableName
();
Collection
<
String
>
updateColumns
=
extractUpdateColumns
(
updateStatement
);
for
(
String
column
:
updateColumns
)
{
boolean
isShardingColumn
=
shardingRule
.
isShardingColumn
(
column
,
tableName
);
if
(
isShardingColumn
)
{
throw
new
UnsupportedOperationException
(
String
.
format
(
"Can not update shard key,logicTable: [%s],colum: [%s]."
,
tableName
,
column
));
for
(
AssignmentSegment
each
:
updateStatement
.
getSetAssignment
().
getAssignments
())
{
if
(
shardingRule
.
isShardingColumn
(
each
.
getColumn
().
getName
(),
tableName
))
{
throw
new
UnsupportedOperationException
(
String
.
format
(
"Can not update sharding key, logic table: [%s], column: [%s]."
,
tableName
,
each
));
}
}
}
private
Collection
<
String
>
extractUpdateColumns
(
final
UpdateStatement
updateStatement
)
{
List
<
String
>
result
=
new
LinkedList
<>();
Collection
<
AssignmentSegment
>
assignmentSegments
=
updateStatement
.
getSetAssignment
().
getAssignments
();
for
(
AssignmentSegment
each
:
assignmentSegments
)
{
result
.
add
(
each
.
getColumn
().
getName
());
}
return
result
;
}
}
sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingUpdateOptimizeEngineTest.java
浏览文件 @
a1460d57
...
...
@@ -30,7 +30,6 @@ import org.apache.shardingsphere.core.rule.ShardingRule;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
...
...
@@ -40,17 +39,17 @@ import static org.mockito.Mockito.mock;
import
static
org
.
mockito
.
Mockito
.
when
;
public
final
class
ShardingUpdateOptimizeEngineTest
{
private
ShardingRule
shardingRule
;
private
TableMetas
tableMetas
;
private
String
sql
;
private
List
<
Object
>
parameters
;
private
UpdateStatement
sqlStatement
;
@Before
public
void
setUp
()
{
sql
=
"update user set id = 100"
;
...
...
@@ -64,23 +63,23 @@ public final class ShardingUpdateOptimizeEngineTest {
when
(
columnSegment
.
getName
()).
thenReturn
(
"id"
);
when
(
assignmentSegment
.
getColumn
()).
thenReturn
(
columnSegment
);
when
(
tableAvailable
.
getTableName
()).
thenReturn
(
"user"
);
when
(
sqlStatement
.
findSQLSegments
(
TableAvailable
.
class
)).
thenReturn
(
Arrays
.
as
List
(
tableAvailable
));
when
(
sqlStatement
.
findSQLSegments
(
TableAvailable
.
class
)).
thenReturn
(
Collections
.
singleton
List
(
tableAvailable
));
SetAssignmentsSegment
setAssignmentsSegment
=
mock
(
SetAssignmentsSegment
.
class
);
when
(
setAssignmentsSegment
.
getAssignments
()).
thenReturn
(
Arrays
.
as
List
(
assignmentSegment
));
when
(
setAssignmentsSegment
.
getAssignments
()).
thenReturn
(
Collections
.
singleton
List
(
assignmentSegment
));
when
(
sqlStatement
.
getSetAssignment
()).
thenReturn
(
setAssignmentsSegment
);
when
(
sqlStatement
.
getWhere
()).
thenReturn
(
Optional
.<
WhereSegment
>
absent
());
}
@Test
public
void
assertUpdateWithoutShardKey
()
{
public
void
assertUpdateWithoutShard
ing
Key
()
{
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
false
);
ShardingUpdateOptimizeEngine
shardingUpdateOptimizeEngine
=
new
ShardingUpdateOptimizeEngine
();
ShardingConditionOptimizedStatement
optimizedStatement
=
shardingUpdateOptimizeEngine
.
optimize
(
shardingRule
,
tableMetas
,
sql
,
parameters
,
sqlStatement
);
assertThat
(
optimizedStatement
,
instanceOf
(
ShardingConditionOptimizedStatement
.
class
));
}
@Test
(
expected
=
UnsupportedOperationException
.
class
)
public
void
assertUpdateWithShardKey
()
{
public
void
assertUpdateWithShard
ing
Key
()
{
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
true
);
ShardingUpdateOptimizeEngine
shardingUpdateOptimizeEngine
=
new
ShardingUpdateOptimizeEngine
();
shardingUpdateOptimizeEngine
.
optimize
(
shardingRule
,
tableMetas
,
sql
,
parameters
,
sqlStatement
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录