Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
48ddf2b9
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,发现更多精彩内容 >>
未验证
提交
48ddf2b9
编写于
8月 24, 2020
作者:
J
Juan Pan(Trista)
提交者:
GitHub
8月 24, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor ShardingDeleteStatementValidator to simplify the parameter list (#7033)
上级
665fd5ff
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
66 addition
and
31 deletion
+66
-31
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
...gsphere/sharding/route/engine/ShardingRouteDecorator.java
+1
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
...ng/route/engine/validator/ShardingStatementValidator.java
+3
-6
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
...gine/validator/impl/ShardingDeleteStatementValidator.java
+3
-3
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
...gine/validator/impl/ShardingInsertStatementValidator.java
+4
-3
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
...gine/validator/impl/ShardingUpdateStatementValidator.java
+6
-4
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java
.../validator/impl/ShardingDeleteStatementValidatorTest.java
+4
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
.../validator/impl/ShardingInsertStatementValidatorTest.java
+16
-7
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
.../validator/impl/ShardingUpdateStatementValidatorTest.java
+14
-6
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/RuleSchemaMetaData.java
...rdingsphere/infra/metadata/schema/RuleSchemaMetaData.java
+15
-0
未找到文件。
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
浏览文件 @
48ddf2b9
...
...
@@ -62,7 +62,7 @@ public final class ShardingRouteDecorator implements RouteDecorator<ShardingRule
List
<
Object
>
parameters
=
routeContext
.
getParameters
();
SQLStatement
sqlStatement
=
sqlStatementContext
.
getSqlStatement
();
Optional
<
ShardingStatementValidator
>
shardingStatementValidator
=
ShardingStatementValidatorFactory
.
newInstance
(
sqlStatement
);
shardingStatementValidator
.
ifPresent
(
validator
->
validator
.
preValidate
(
shardingRule
,
sqlStatementContext
,
parameters
));
shardingStatementValidator
.
ifPresent
(
validator
->
validator
.
preValidate
(
shardingRule
,
routeContext
));
ShardingConditions
shardingConditions
=
getShardingConditions
(
parameters
,
sqlStatementContext
,
metaData
.
getSchema
().
getConfiguredSchemaMetaData
(),
shardingRule
);
boolean
needMergeShardingValues
=
isNeedMergeShardingValues
(
sqlStatementContext
,
shardingRule
);
if
(
sqlStatement
instanceof
DMLStatement
&&
needMergeShardingValues
)
{
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
浏览文件 @
48ddf2b9
...
...
@@ -17,13 +17,11 @@
package
org.apache.shardingsphere.sharding.route.engine.validator
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteResult
;
import
org.apache.shardingsphere.sharding.rule.ShardingRule
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
import
org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement
;
import
java.util.List
;
/**
* Sharding statement validator.
*
...
...
@@ -35,10 +33,9 @@ public interface ShardingStatementValidator<T extends SQLStatement> {
* Validate whether sharding operation is supported before route.
*
* @param shardingRule sharding rule
* @param sqlStatementContext SQL statement context
* @param parameters SQL parameters
* @param routeContext route context
*/
void
preValidate
(
ShardingRule
shardingRule
,
SQLStatementContext
<
T
>
sqlStatementContext
,
List
<
Object
>
parameters
);
void
preValidate
(
ShardingRule
shardingRule
,
RouteContext
routeContext
);
/**
* Validate whether sharding operation is supported after route.
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
浏览文件 @
48ddf2b9
...
...
@@ -18,6 +18,7 @@
package
org.apache.shardingsphere.sharding.route.engine.validator.impl
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteResult
;
import
org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator
;
import
org.apache.shardingsphere.sharding.rule.ShardingRule
;
...
...
@@ -26,15 +27,14 @@ import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import
org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement
;
import
org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement
;
import
java.util.List
;
/**
* Sharding delete statement validator.
*/
public
final
class
ShardingDeleteStatementValidator
implements
ShardingStatementValidator
<
DeleteStatement
>
{
@Override
public
void
preValidate
(
final
ShardingRule
shardingRule
,
final
SQLStatementContext
<
DeleteStatement
>
sqlStatementContext
,
final
List
<
Object
>
parameters
)
{
public
void
preValidate
(
final
ShardingRule
shardingRule
,
final
RouteContext
routeContext
)
{
SQLStatementContext
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
if
(
1
!=
((
TableAvailable
)
sqlStatementContext
).
getAllTables
().
size
())
{
throw
new
ShardingSphereException
(
"Cannot support Multiple-Table for '%s'."
,
sqlStatementContext
.
getSqlStatement
());
}
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
浏览文件 @
48ddf2b9
...
...
@@ -18,6 +18,7 @@
package
org.apache.shardingsphere.sharding.route.engine.validator.impl
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteResult
;
import
org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator
;
import
org.apache.shardingsphere.sharding.rule.ShardingRule
;
...
...
@@ -33,7 +34,6 @@ import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Optional
;
/**
...
...
@@ -42,11 +42,12 @@ import java.util.Optional;
public
final
class
ShardingInsertStatementValidator
implements
ShardingStatementValidator
<
InsertStatement
>
{
@Override
public
void
preValidate
(
final
ShardingRule
shardingRule
,
final
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
,
final
List
<
Object
>
parameters
)
{
public
void
preValidate
(
final
ShardingRule
shardingRule
,
final
RouteContext
routeContext
)
{
SQLStatementContext
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
if
(
null
==
((
InsertStatementContext
)
sqlStatementContext
).
getInsertSelectContext
()
&&
1
!=
((
TableAvailable
)
sqlStatementContext
).
getAllTables
().
size
())
{
throw
new
ShardingSphereException
(
"Cannot support Multiple-Table for '%s'."
,
sqlStatementContext
.
getSqlStatement
());
}
InsertStatement
sqlStatement
=
sqlStatementContext
.
getSqlStatement
();
InsertStatement
sqlStatement
=
(
InsertStatement
)
sqlStatementContext
.
getSqlStatement
();
Optional
<
OnDuplicateKeyColumnsSegment
>
onDuplicateKeyColumnsSegment
=
sqlStatement
.
getOnDuplicateKeyColumns
();
String
tableName
=
sqlStatement
.
getTable
().
getTableName
().
getIdentifier
().
getValue
();
if
(
onDuplicateKeyColumnsSegment
.
isPresent
()
&&
isUpdateShardingKey
(
shardingRule
,
onDuplicateKeyColumnsSegment
.
get
(),
tableName
))
{
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
浏览文件 @
48ddf2b9
...
...
@@ -18,6 +18,7 @@
package
org.apache.shardingsphere.sharding.route.engine.validator.impl
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteResult
;
import
org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator
;
import
org.apache.shardingsphere.sharding.rule.ShardingRule
;
...
...
@@ -46,20 +47,21 @@ import java.util.Optional;
public
final
class
ShardingUpdateStatementValidator
implements
ShardingStatementValidator
<
UpdateStatement
>
{
@Override
public
void
preValidate
(
final
ShardingRule
shardingRule
,
final
SQLStatementContext
<
UpdateStatement
>
sqlStatementContext
,
final
List
<
Object
>
parameters
)
{
public
void
preValidate
(
final
ShardingRule
shardingRule
,
final
RouteContext
routeContext
)
{
SQLStatementContext
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
if
(
1
!=
((
TableAvailable
)
sqlStatementContext
).
getAllTables
().
size
())
{
throw
new
ShardingSphereException
(
"Cannot support Multiple-Table for '%s'."
,
sqlStatementContext
.
getSqlStatement
());
}
UpdateStatement
sqlStatement
=
sqlStatementContext
.
getSqlStatement
();
UpdateStatement
sqlStatement
=
(
UpdateStatement
)
sqlStatementContext
.
getSqlStatement
();
String
tableName
=
sqlStatement
.
getTables
().
iterator
().
next
().
getTableName
().
getIdentifier
().
getValue
();
for
(
AssignmentSegment
each
:
sqlStatement
.
getSetAssignment
().
getAssignments
())
{
String
shardingColumn
=
each
.
getColumn
().
getIdentifier
().
getValue
();
if
(
shardingRule
.
isShardingColumn
(
shardingColumn
,
tableName
))
{
Optional
<
Object
>
shardingColumnSetAssignmentValue
=
getShardingColumnSetAssignmentValue
(
each
,
parameters
);
Optional
<
Object
>
shardingColumnSetAssignmentValue
=
getShardingColumnSetAssignmentValue
(
each
,
routeContext
.
getParameters
()
);
Optional
<
Object
>
shardingValue
=
Optional
.
empty
();
Optional
<
WhereSegment
>
whereSegmentOptional
=
sqlStatement
.
getWhere
();
if
(
whereSegmentOptional
.
isPresent
())
{
shardingValue
=
getShardingValue
(
whereSegmentOptional
.
get
(),
parameters
,
shardingColumn
);
shardingValue
=
getShardingValue
(
whereSegmentOptional
.
get
(),
routeContext
.
getParameters
()
,
shardingColumn
);
}
if
(
shardingColumnSetAssignmentValue
.
isPresent
()
&&
shardingValue
.
isPresent
()
&&
shardingColumnSetAssignmentValue
.
get
().
equals
(
shardingValue
.
get
()))
{
continue
;
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java
浏览文件 @
48ddf2b9
...
...
@@ -18,6 +18,8 @@
package
org.apache.shardingsphere.sharding.route.engine.validator.impl
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteResult
;
import
org.apache.shardingsphere.sharding.rule.ShardingRule
;
import
org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
...
...
@@ -42,7 +44,8 @@ public final class ShardingDeleteStatementValidatorTest {
DeleteStatement
sqlStatement
=
new
DeleteStatement
();
sqlStatement
.
getTables
().
addAll
(
createMultiTablesContext
().
getTables
());
SQLStatementContext
<
DeleteStatement
>
sqlStatementContext
=
new
DeleteStatementContext
(
sqlStatement
);
new
ShardingDeleteStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingDeleteStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
private
TablesContext
createMultiTablesContext
()
{
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
浏览文件 @
48ddf2b9
...
...
@@ -18,6 +18,8 @@
package
org.apache.shardingsphere.sharding.route.engine.validator.impl
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteResult
;
import
org.apache.shardingsphere.sharding.rule.ShardingRule
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData
;
import
org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext
;
...
...
@@ -57,21 +59,24 @@ public final class ShardingInsertStatementValidatorTest {
public
void
assertValidateInsertModifyMultiTables
()
{
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
=
new
InsertStatementContext
(
new
SchemaMetaData
(
Collections
.
emptyMap
()),
Collections
.
singletonList
(
1
),
createInsertStatement
());
sqlStatementContext
.
getTablesContext
().
getTables
().
addAll
(
createMultiTablesContext
().
getTables
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
public
void
assertValidateOnDuplicateKeyWithoutShardingKey
()
{
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
false
);
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
=
new
InsertStatementContext
(
new
SchemaMetaData
(
Collections
.
emptyMap
()),
Collections
.
singletonList
(
1
),
createInsertStatement
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
(
expected
=
ShardingSphereException
.
class
)
public
void
assertValidateOnDuplicateKeyWithShardingKey
()
{
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
true
);
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
=
new
InsertStatementContext
(
new
SchemaMetaData
(
Collections
.
emptyMap
()),
Collections
.
singletonList
(
1
),
createInsertStatement
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
(
expected
=
ShardingSphereException
.
class
)
...
...
@@ -80,7 +85,8 @@ public final class ShardingInsertStatementValidatorTest {
when
(
shardingRule
.
isGenerateKeyColumn
(
"id"
,
"user"
)).
thenReturn
(
false
);
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
=
new
InsertStatementContext
(
new
SchemaMetaData
(
Collections
.
emptyMap
()),
Collections
.
singletonList
(
1
),
createInsertSelectStatement
());
sqlStatementContext
.
getTablesContext
().
getTables
().
addAll
(
createSingleTablesContext
().
getTables
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
...
...
@@ -89,7 +95,8 @@ public final class ShardingInsertStatementValidatorTest {
when
(
shardingRule
.
isGenerateKeyColumn
(
"id"
,
"user"
)).
thenReturn
(
true
);
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
=
new
InsertStatementContext
(
new
SchemaMetaData
(
Collections
.
emptyMap
()),
Collections
.
singletonList
(
1
),
createInsertSelectStatement
());
sqlStatementContext
.
getTablesContext
().
getTables
().
addAll
(
createSingleTablesContext
().
getTables
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
(
expected
=
ShardingSphereException
.
class
)
...
...
@@ -100,7 +107,8 @@ public final class ShardingInsertStatementValidatorTest {
when
(
shardingRule
.
isAllBindingTables
(
multiTablesContext
.
getTableNames
())).
thenReturn
(
false
);
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
=
new
InsertStatementContext
(
new
SchemaMetaData
(
Collections
.
emptyMap
()),
Collections
.
singletonList
(
1
),
createInsertSelectStatement
());
sqlStatementContext
.
getTablesContext
().
getTables
().
addAll
(
multiTablesContext
.
getTables
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
...
...
@@ -111,7 +119,8 @@ public final class ShardingInsertStatementValidatorTest {
when
(
shardingRule
.
isAllBindingTables
(
multiTablesContext
.
getTableNames
())).
thenReturn
(
true
);
SQLStatementContext
<
InsertStatement
>
sqlStatementContext
=
new
InsertStatementContext
(
new
SchemaMetaData
(
Collections
.
emptyMap
()),
Collections
.
singletonList
(
1
),
createInsertSelectStatement
());
sqlStatementContext
.
getTablesContext
().
getTables
().
addAll
(
multiTablesContext
.
getTables
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingInsertStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
private
InsertStatement
createInsertStatement
()
{
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
浏览文件 @
48ddf2b9
...
...
@@ -18,6 +18,8 @@
package
org.apache.shardingsphere.sharding.route.engine.validator.impl
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteResult
;
import
org.apache.shardingsphere.sharding.rule.ShardingRule
;
import
org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
...
...
@@ -57,26 +59,30 @@ public final class ShardingUpdateStatementValidatorTest {
public
void
assertValidateUpdateModifyMultiTables
()
{
SQLStatementContext
<
UpdateStatement
>
sqlStatementContext
=
new
UpdateStatementContext
(
createUpdateStatement
());
sqlStatementContext
.
getTablesContext
().
getTables
().
addAll
(
createMultiTablesContext
().
getTables
());
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
sqlStatementContext
,
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
sqlStatementContext
,
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
public
void
assertValidateUpdateWithoutShardingKey
()
{
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
false
);
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
new
UpdateStatementContext
(
createUpdateStatement
()),
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
new
UpdateStatementContext
(
createUpdateStatement
()),
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
(
expected
=
ShardingSphereException
.
class
)
public
void
assertValidateUpdateWithShardingKey
()
{
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
true
);
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
new
UpdateStatementContext
(
createUpdateStatement
()),
Collections
.
emptyList
());
RouteContext
routeContext
=
new
RouteContext
(
new
UpdateStatementContext
(
createUpdateStatement
()),
Collections
.
emptyList
(),
new
RouteResult
());
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
public
void
assertValidateUpdateWithoutShardingKeyAndParameters
()
{
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
false
);
List
<
Object
>
parameters
=
Arrays
.
asList
(
1
,
1
);
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
new
UpdateStatementContext
(
createUpdateStatement
()),
parameters
);
RouteContext
routeContext
=
new
RouteContext
(
new
UpdateStatementContext
(
createUpdateStatement
()),
parameters
,
new
RouteResult
());
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
...
...
@@ -84,7 +90,8 @@ public final class ShardingUpdateStatementValidatorTest {
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
true
);
List
<
Object
>
parameters
=
Arrays
.
asList
(
1
,
1
);
SQLStatementContext
<
UpdateStatement
>
updateStatementContext
=
new
UpdateStatementContext
(
createUpdateStatementAndParameters
(
1
));
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
updateStatementContext
,
parameters
);
RouteContext
routeContext
=
new
RouteContext
(
updateStatementContext
,
parameters
,
new
RouteResult
());
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
@Test
(
expected
=
ShardingSphereException
.
class
)
...
...
@@ -92,7 +99,8 @@ public final class ShardingUpdateStatementValidatorTest {
when
(
shardingRule
.
isShardingColumn
(
"id"
,
"user"
)).
thenReturn
(
true
);
List
<
Object
>
parameters
=
Arrays
.
asList
(
1
,
1
);
SQLStatementContext
<
UpdateStatement
>
updateStatementContext
=
new
UpdateStatementContext
(
createUpdateStatementAndParameters
(
2
));
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
updateStatementContext
,
parameters
);
RouteContext
routeContext
=
new
RouteContext
(
updateStatementContext
,
parameters
,
new
RouteResult
());
new
ShardingUpdateStatementValidator
().
preValidate
(
shardingRule
,
routeContext
);
}
private
UpdateStatement
createUpdateStatement
()
{
...
...
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/RuleSchemaMetaData.java
浏览文件 @
48ddf2b9
...
...
@@ -21,6 +21,8 @@ import lombok.Getter;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData
;
import
java.util.Collection
;
import
java.util.LinkedList
;
import
java.util.Map
;
/**
...
...
@@ -45,4 +47,17 @@ public final class RuleSchemaMetaData {
result
.
merge
(
configuredSchemaMetaData
);
return
result
;
}
/**
* Get all table names.
*
* @return all table names
*/
public
Collection
<
String
>
getAllTableNames
()
{
Collection
<
String
>
result
=
new
LinkedList
<>(
configuredSchemaMetaData
.
getAllTableNames
());
for
(
SchemaMetaData
each
:
unconfiguredSchemaMetaDataMap
.
values
())
{
result
.
addAll
(
each
.
getAllTableNames
());
}
return
result
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录