Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
48acae5a
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,体验更适合开发者的 AI 搜索 >>
未验证
提交
48acae5a
编写于
9月 06, 2020
作者:
L
Liang Zhang
提交者:
GitHub
9月 06, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor shadow route module (#7277)
* Refactor shadow route module * Refactor ShadowRouteDecorator
上级
cc4020d4
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
88 addition
and
60 deletion
+88
-60
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java
...rdingsphere/shadow/route/engine/ShadowRouteDecorator.java
+14
-13
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/judge/ShadowDataSourceJudgeEngine.java
...hadow/route/engine/judge/ShadowDataSourceJudgeEngine.java
+31
-0
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/judge/impl/PreparedShadowDataSourceJudgeEngine.java
...ngine/judge/impl/PreparedShadowDataSourceJudgeEngine.java
+9
-10
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/judge/impl/SimpleShadowDataSourceJudgeEngine.java
.../engine/judge/impl/SimpleShadowDataSourceJudgeEngine.java
+10
-10
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/judge/util/ShadowValueJudgeUtil.java
.../shadow/route/engine/judge/util/ShadowValueJudgeUtil.java
+13
-16
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/judge/impl/PreparedShadowDataSourceRouterTest.java
...engine/judge/impl/PreparedShadowDataSourceRouterTest.java
+3
-3
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/judge/impl/SimpleShadowDataSourceRouterTest.java
...e/engine/judge/impl/SimpleShadowDataSourceRouterTest.java
+8
-8
未找到文件。
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java
浏览文件 @
48acae5a
...
@@ -18,9 +18,10 @@
...
@@ -18,9 +18,10 @@
package
org.apache.shardingsphere.shadow.route.engine
;
package
org.apache.shardingsphere.shadow.route.engine
;
import
org.apache.shardingsphere.shadow.constant.ShadowOrder
;
import
org.apache.shardingsphere.shadow.constant.ShadowOrder
;
import
org.apache.shardingsphere.shadow.route.engine.judge.ShadowDataSourceJudgeEngine
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.shadow.route.engine.
impl.PreparedShadowDataSourceRouter
;
import
org.apache.shardingsphere.shadow.route.engine.
judge.impl.PreparedShadowDataSourceJudgeEngine
;
import
org.apache.shardingsphere.shadow.route.engine.
impl.SimpleShadowDataSourceRouter
;
import
org.apache.shardingsphere.shadow.route.engine.
judge.impl.SimpleShadowDataSourceJudgeEngine
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement
;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement
;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement
;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement
;
...
@@ -48,18 +49,18 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
...
@@ -48,18 +49,18 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
}
}
private
RouteContext
getRouteContext
(
final
RouteContext
routeContext
,
final
ShadowRule
shadowRule
)
{
private
RouteContext
getRouteContext
(
final
RouteContext
routeContext
,
final
ShadowRule
shadowRule
)
{
SQLStatementContext
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
SQLStatementContext
<?>
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
SQLStatement
sqlStatement
=
sqlStatementContext
.
getSqlStatement
();
SQLStatement
sqlStatement
=
sqlStatementContext
.
getSqlStatement
();
RouteResult
routeResult
=
new
RouteResult
();
RouteResult
routeResult
=
new
RouteResult
();
List
<
Object
>
parameters
=
routeContext
.
getParameters
();
List
<
Object
>
parameters
=
routeContext
.
getParameters
();
if
(!(
sqlStatement
instanceof
DMLStatement
))
{
if
(!(
sqlStatement
instanceof
DMLStatement
))
{
shadowRule
.
getShadowMappings
().
forEach
((
k
,
v
)
->
{
shadowRule
.
getShadowMappings
().
forEach
((
k
ey
,
value
)
->
{
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
k
,
k
),
Collections
.
emptyList
()));
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
k
ey
,
key
),
Collections
.
emptyList
()));
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
v
,
v
),
Collections
.
emptyList
()));
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
v
alue
,
value
),
Collections
.
emptyList
()));
});
});
return
new
RouteContext
(
sqlStatementContext
,
parameters
,
routeResult
);
return
new
RouteContext
(
sqlStatementContext
,
parameters
,
routeResult
);
}
}
if
(
isShadow
SQL
(
routeContext
,
shadowRule
))
{
if
(
isShadow
(
routeContext
,
shadowRule
))
{
shadowRule
.
getShadowMappings
().
values
().
forEach
(
each
->
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
())));
shadowRule
.
getShadowMappings
().
values
().
forEach
(
each
->
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
())));
}
else
{
}
else
{
shadowRule
.
getShadowMappings
().
keySet
().
forEach
(
each
->
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
())));
shadowRule
.
getShadowMappings
().
keySet
().
forEach
(
each
->
routeResult
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
())));
...
@@ -79,7 +80,7 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
...
@@ -79,7 +80,7 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
return
routeContext
;
return
routeContext
;
}
}
Collection
<
RouteUnit
>
toBeRemoved
=
new
LinkedList
<>();
Collection
<
RouteUnit
>
toBeRemoved
=
new
LinkedList
<>();
if
(
isShadow
SQL
(
routeContext
,
shadowRule
))
{
if
(
isShadow
(
routeContext
,
shadowRule
))
{
for
(
RouteUnit
each
:
routeContext
.
getRouteResult
().
getRouteUnits
())
{
for
(
RouteUnit
each
:
routeContext
.
getRouteResult
().
getRouteUnits
())
{
toBeRemoved
.
add
(
each
);
toBeRemoved
.
add
(
each
);
String
shadowDataSourceName
=
shadowRule
.
getShadowMappings
().
get
(
each
.
getDataSourceMapper
().
getActualName
());
String
shadowDataSourceName
=
shadowRule
.
getShadowMappings
().
get
(
each
.
getDataSourceMapper
().
getActualName
());
...
@@ -91,12 +92,12 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
...
@@ -91,12 +92,12 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
return
routeContext
;
return
routeContext
;
}
}
private
boolean
isShadow
SQL
(
final
RouteContext
routeContext
,
final
ShadowRule
shadowRule
)
{
private
boolean
isShadow
(
final
RouteContext
routeContext
,
final
ShadowRule
shadowRule
)
{
List
<
Object
>
parameters
=
routeContext
.
getParameters
();
List
<
Object
>
parameters
=
routeContext
.
getParameters
();
SQLStatementContext
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
SQLStatementContext
<?>
sqlStatementContext
=
routeContext
.
getSqlStatementContext
();
ShadowDataSource
Router
shadowDataSourceRouter
=
parameters
.
isEmpty
()
?
new
SimpleShadowDataSourceRouter
(
shadowRule
,
sqlStatementContext
)
ShadowDataSource
JudgeEngine
shadowDataSourceRouter
=
parameters
.
isEmpty
()
?
new
SimpleShadowDataSourceJudgeEngine
(
shadowRule
,
sqlStatementContext
)
:
new
PreparedShadowDataSource
Router
(
shadowRule
,
sqlStatementContext
,
parameters
);
:
new
PreparedShadowDataSource
JudgeEngine
(
shadowRule
,
sqlStatementContext
,
parameters
);
return
shadowDataSourceRouter
.
isShadow
SQL
();
return
shadowDataSourceRouter
.
isShadow
();
}
}
@Override
@Override
...
...
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/judge/ShadowDataSourceJudgeEngine.java
0 → 100644
浏览文件 @
48acae5a
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.shadow.route.engine.judge
;
/**
* Shadow data source judge engine.
*/
public
interface
ShadowDataSourceJudgeEngine
{
/**
* Judge whether shadow.
*
* @return is shadow or not
*/
boolean
isShadow
();
}
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/
impl/PreparedShadowDataSourceRouter
.java
→
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/
judge/impl/PreparedShadowDataSourceJudgeEngine
.java
浏览文件 @
48acae5a
...
@@ -15,12 +15,13 @@
...
@@ -15,12 +15,13 @@
* limitations under the License.
* limitations under the License.
*/
*/
package
org.apache.shardingsphere.shadow.route.engine.impl
;
package
org.apache.shardingsphere.shadow.route.engine.
judge.
impl
;
import
com.google.common.base.Preconditions
;
import
com.google.common.base.Preconditions
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.shadow.route.engine.judge.util.ShadowValueJudgeUtil
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.shadow.route.engine.
ShadowDataSourceRouter
;
import
org.apache.shardingsphere.shadow.route.engine.
judge.ShadowDataSourceJudgeEngine
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.type.WhereAvailable
;
import
org.apache.shardingsphere.sql.parser.binder.type.WhereAvailable
;
...
@@ -36,26 +37,25 @@ import java.util.List;
...
@@ -36,26 +37,25 @@ import java.util.List;
import
java.util.Optional
;
import
java.util.Optional
;
/**
/**
*
Shadow judgement engine for prepared
.
*
Prepared shadow data source judge engine
.
*/
*/
@RequiredArgsConstructor
@RequiredArgsConstructor
public
final
class
PreparedShadowDataSource
Router
implements
ShadowDataSourceRouter
{
public
final
class
PreparedShadowDataSource
JudgeEngine
implements
ShadowDataSourceJudgeEngine
{
private
final
ShadowRule
shadowRule
;
private
final
ShadowRule
shadowRule
;
private
final
SQLStatementContext
sqlStatementContext
;
private
final
SQLStatementContext
<?>
sqlStatementContext
;
private
final
List
<
Object
>
parameters
;
private
final
List
<
Object
>
parameters
;
@Override
@Override
public
boolean
isShadow
SQL
()
{
public
boolean
isShadow
()
{
if
(
sqlStatementContext
instanceof
InsertStatementContext
)
{
if
(
sqlStatementContext
instanceof
InsertStatementContext
)
{
Collection
<
ColumnSegment
>
columnSegments
=
(((
InsertStatementContext
)
sqlStatementContext
).
getSqlStatement
()).
getColumns
();
Collection
<
ColumnSegment
>
columnSegments
=
(((
InsertStatementContext
)
sqlStatementContext
).
getSqlStatement
()).
getColumns
();
int
count
=
0
;
int
count
=
0
;
for
(
ColumnSegment
each
:
columnSegments
)
{
for
(
ColumnSegment
each
:
columnSegments
)
{
if
(
each
.
getIdentifier
().
getValue
().
equals
(
shadowRule
.
getColumn
()))
{
if
(
each
.
getIdentifier
().
getValue
().
equals
(
shadowRule
.
getColumn
()))
{
Object
value
=
parameters
.
get
(
count
);
return
ShadowValueJudgeUtil
.
isShadowValue
(
parameters
.
get
(
count
));
return
isShadowField
(
value
);
}
}
count
++;
count
++;
}
}
...
@@ -82,8 +82,7 @@ public final class PreparedShadowDataSourceRouter implements ShadowDataSourceRou
...
@@ -82,8 +82,7 @@ public final class PreparedShadowDataSourceRouter implements ShadowDataSourceRou
Preconditions
.
checkArgument
(
each
.
getRightValue
()
instanceof
PredicateCompareRightValue
,
"must be PredicateCompareRightValue"
);
Preconditions
.
checkArgument
(
each
.
getRightValue
()
instanceof
PredicateCompareRightValue
,
"must be PredicateCompareRightValue"
);
PredicateCompareRightValue
rightValue
=
(
PredicateCompareRightValue
)
each
.
getRightValue
();
PredicateCompareRightValue
rightValue
=
(
PredicateCompareRightValue
)
each
.
getRightValue
();
int
parameterMarkerIndex
=
((
ParameterMarkerExpressionSegment
)
rightValue
.
getExpression
()).
getParameterMarkerIndex
();
int
parameterMarkerIndex
=
((
ParameterMarkerExpressionSegment
)
rightValue
.
getExpression
()).
getParameterMarkerIndex
();
Object
value
=
parameters
.
get
(
parameterMarkerIndex
);
return
ShadowValueJudgeUtil
.
isShadowValue
(
parameters
.
get
(
parameterMarkerIndex
));
return
isShadowField
(
value
);
}
}
}
}
return
false
;
return
false
;
...
...
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/
impl/SimpleShadowDataSourceRouter
.java
→
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/
judge/impl/SimpleShadowDataSourceJudgeEngine
.java
浏览文件 @
48acae5a
...
@@ -15,13 +15,14 @@
...
@@ -15,13 +15,14 @@
* limitations under the License.
* limitations under the License.
*/
*/
package
org.apache.shardingsphere.shadow.route.engine.impl
;
package
org.apache.shardingsphere.shadow.route.engine.
judge.
impl
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.shadow.condition.ShadowCondition
;
import
org.apache.shardingsphere.shadow.condition.ShadowCondition
;
import
org.apache.shardingsphere.shadow.condition.ShadowConditionEngine
;
import
org.apache.shardingsphere.shadow.condition.ShadowConditionEngine
;
import
org.apache.shardingsphere.shadow.route.engine.ShadowDataSourceRouter
;
import
org.apache.shardingsphere.shadow.route.engine.judge.ShadowDataSourceJudgeEngine
;
import
org.apache.shardingsphere.shadow.route.engine.judge.util.ShadowValueJudgeUtil
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.sql.parser.binder.segment.insert.values.InsertValueContext
;
import
org.apache.shardingsphere.sql.parser.binder.segment.insert.values.InsertValueContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext
;
import
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext
;
...
@@ -33,17 +34,17 @@ import java.util.List;
...
@@ -33,17 +34,17 @@ import java.util.List;
import
java.util.Optional
;
import
java.util.Optional
;
/**
/**
* Simple shadow
judgement
engine.
* Simple shadow
data source judge
engine.
*/
*/
@RequiredArgsConstructor
@RequiredArgsConstructor
public
final
class
SimpleShadowDataSource
Router
implements
ShadowDataSourceRouter
{
public
final
class
SimpleShadowDataSource
JudgeEngine
implements
ShadowDataSourceJudgeEngine
{
private
final
ShadowRule
shadowRule
;
private
final
ShadowRule
shadowRule
;
private
final
SQLStatementContext
sqlStatementContext
;
private
final
SQLStatementContext
<?>
sqlStatementContext
;
@Override
@Override
public
boolean
isShadow
SQL
()
{
public
boolean
isShadow
()
{
if
(
sqlStatementContext
instanceof
InsertStatementContext
)
{
if
(
sqlStatementContext
instanceof
InsertStatementContext
)
{
for
(
InsertValueContext
each
:
((
InsertStatementContext
)
sqlStatementContext
).
getInsertValueContexts
())
{
for
(
InsertValueContext
each
:
((
InsertStatementContext
)
sqlStatementContext
).
getInsertValueContexts
())
{
if
(
judgeShadowSqlForInsert
(
each
,
(
InsertStatementContext
)
sqlStatementContext
))
{
if
(
judgeShadowSqlForInsert
(
each
,
(
InsertStatementContext
)
sqlStatementContext
))
{
...
@@ -58,7 +59,7 @@ public final class SimpleShadowDataSourceRouter implements ShadowDataSourceRoute
...
@@ -58,7 +59,7 @@ public final class SimpleShadowDataSourceRouter implements ShadowDataSourceRoute
return
false
;
return
false
;
}
}
List
<
Object
>
values
=
shadowCondition
.
get
().
getValues
(
Collections
.
emptyList
());
List
<
Object
>
values
=
shadowCondition
.
get
().
getValues
(
Collections
.
emptyList
());
return
!
values
.
isEmpty
()
&&
isShadowField
(
values
.
get
(
0
));
return
!
values
.
isEmpty
()
&&
ShadowValueJudgeUtil
.
isShadowValue
(
values
.
get
(
0
));
}
}
return
false
;
return
false
;
}
}
...
@@ -69,8 +70,7 @@ public final class SimpleShadowDataSourceRouter implements ShadowDataSourceRoute
...
@@ -69,8 +70,7 @@ public final class SimpleShadowDataSourceRouter implements ShadowDataSourceRoute
String
columnName
=
descendingColumnNames
.
next
();
String
columnName
=
descendingColumnNames
.
next
();
if
(
shadowRule
.
getColumn
().
equals
(
columnName
))
{
if
(
shadowRule
.
getColumn
().
equals
(
columnName
))
{
int
columnIndex
=
insertStatementContext
.
getColumnNames
().
indexOf
(
columnName
);
int
columnIndex
=
insertStatementContext
.
getColumnNames
().
indexOf
(
columnName
);
Object
value
=
insertValueContext
.
getValue
(
columnIndex
);
return
ShadowValueJudgeUtil
.
isShadowValue
(
insertValueContext
.
getValue
(
columnIndex
));
return
isShadowField
(
value
);
}
}
}
}
return
false
;
return
false
;
...
...
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/
ShadowDataSourceRouter
.java
→
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/
judge/util/ShadowValueJudgeUtil
.java
浏览文件 @
48acae5a
...
@@ -15,28 +15,25 @@
...
@@ -15,28 +15,25 @@
* limitations under the License.
* limitations under the License.
*/
*/
package
org.apache.shardingsphere.shadow.route.engine
;
package
org.apache.shardingsphere.shadow.route.engine.judge.util
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
/**
/**
*
Route decorator for shadow
.
*
Shadow value judge util
.
*/
*/
public
interface
ShadowDataSourceRouter
{
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
public
final
class
ShadowValueJudgeUtil
{
/**
* Judge whether SQL is shadow.
*
* @return SQL is shadow or not
*/
boolean
isShadowSQL
();
/**
/**
* Judge whether field is shadow.
* Judge whether shadow value.
* @param value the field
*
* @return field is shadow or not
* @param value value to be judged
* @return is shadow value ot not
*/
*/
default
boolean
isShadowField
(
final
Object
value
)
{
public
static
boolean
isShadowValue
(
final
Object
value
)
{
return
(
value
instanceof
Boolean
&&
(
Boolean
)
value
)
return
(
value
instanceof
Boolean
&&
(
Boolean
)
value
)
||
(
value
instanceof
Integer
&&
1
==
(
Integer
)
value
)
||
(
value
instanceof
Integer
&&
1
==
(
Integer
)
value
)
||
(
value
instanceof
String
&&
Boolean
.
parseBoolean
((
String
)
value
));
||
(
value
instanceof
String
&&
Boolean
.
parseBoolean
((
String
)
value
));
}
}
}
}
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/impl/PreparedShadowDataSourceRouterTest.java
→
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/
judge/
impl/PreparedShadowDataSourceRouterTest.java
浏览文件 @
48acae5a
...
@@ -15,7 +15,7 @@
...
@@ -15,7 +15,7 @@
* limitations under the License.
* limitations under the License.
*/
*/
package
org.apache.shardingsphere.shadow.route.engine.impl
;
package
org.apache.shardingsphere.shadow.route.engine.
judge.
impl
;
import
org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration
;
import
org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
...
@@ -49,7 +49,7 @@ public final class PreparedShadowDataSourceRouterTest {
...
@@ -49,7 +49,7 @@ public final class PreparedShadowDataSourceRouterTest {
Arrays
.
asList
(
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"id"
)),
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"name"
)),
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"shadow"
))));
Arrays
.
asList
(
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"id"
)),
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"name"
)),
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"shadow"
))));
insertStatement
.
setInsertColumns
(
insertColumnsSegment
);
insertStatement
.
setInsertColumns
(
insertColumnsSegment
);
InsertStatementContext
insertStatementContext
=
new
InsertStatementContext
(
schemaMetaData
,
Arrays
.
asList
(
1
,
"Tom"
,
2
,
"Jerry"
,
3
,
true
),
insertStatement
);
InsertStatementContext
insertStatementContext
=
new
InsertStatementContext
(
schemaMetaData
,
Arrays
.
asList
(
1
,
"Tom"
,
2
,
"Jerry"
,
3
,
true
),
insertStatement
);
PreparedShadowDataSource
Router
preparedShadowDataSourceRouter
=
new
PreparedShadowDataSourceRouter
(
shadowRule
,
insertStatementContext
,
Arrays
.
asList
(
1
,
"Tom"
,
true
));
PreparedShadowDataSource
JudgeEngine
preparedShadowDataSourceRouter
=
new
PreparedShadowDataSourceJudgeEngine
(
shadowRule
,
insertStatementContext
,
Arrays
.
asList
(
1
,
"Tom"
,
true
));
assertTrue
(
"should be shadow"
,
preparedShadowDataSourceRouter
.
isShadow
SQL
());
assertTrue
(
"should be shadow"
,
preparedShadowDataSourceRouter
.
isShadow
());
}
}
}
}
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/impl/SimpleShadowDataSourceRouterTest.java
→
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/
judge/
impl/SimpleShadowDataSourceRouterTest.java
浏览文件 @
48acae5a
...
@@ -15,7 +15,7 @@
...
@@ -15,7 +15,7 @@
* limitations under the License.
* limitations under the License.
*/
*/
package
org.apache.shardingsphere.shadow.route.engine.impl
;
package
org.apache.shardingsphere.shadow.route.engine.
judge.
impl
;
import
org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration
;
import
org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
import
org.apache.shardingsphere.shadow.rule.ShadowRule
;
...
@@ -71,14 +71,14 @@ public final class SimpleShadowDataSourceRouterTest {
...
@@ -71,14 +71,14 @@ public final class SimpleShadowDataSourceRouterTest {
insertStatement
.
getValues
().
addAll
(
Collections
.
singletonList
(
new
InsertValuesSegment
(
insertStatement
.
getValues
().
addAll
(
Collections
.
singletonList
(
new
InsertValuesSegment
(
0
,
0
,
Arrays
.
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
1
),
new
LiteralExpressionSegment
(
0
,
0
,
"name"
),
new
LiteralExpressionSegment
(
0
,
0
,
true
)))));
0
,
0
,
Arrays
.
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
1
),
new
LiteralExpressionSegment
(
0
,
0
,
"name"
),
new
LiteralExpressionSegment
(
0
,
0
,
true
)))));
InsertStatementContext
insertStatementContext
=
new
InsertStatementContext
(
schemaMetaData
,
Collections
.
emptyList
(),
insertStatement
);
InsertStatementContext
insertStatementContext
=
new
InsertStatementContext
(
schemaMetaData
,
Collections
.
emptyList
(),
insertStatement
);
SimpleShadowDataSource
Router
simpleShadowDataSourceRouter
=
new
SimpleShadowDataSourceRouter
(
shadowRule
,
insertStatementContext
);
SimpleShadowDataSource
JudgeEngine
simpleShadowDataSourceRouter
=
new
SimpleShadowDataSourceJudgeEngine
(
shadowRule
,
insertStatementContext
);
assertTrue
(
"should be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
SQL
());
assertTrue
(
"should be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
());
insertStatement
.
getValues
().
clear
();
insertStatement
.
getValues
().
clear
();
insertStatement
.
getValues
().
addAll
(
Collections
.
singletonList
(
insertStatement
.
getValues
().
addAll
(
Collections
.
singletonList
(
new
InsertValuesSegment
(
0
,
0
,
Arrays
.
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
1
),
new
LiteralExpressionSegment
(
0
,
0
,
"name"
),
new
LiteralExpressionSegment
(
0
,
0
,
false
)))));
new
InsertValuesSegment
(
0
,
0
,
Arrays
.
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
1
),
new
LiteralExpressionSegment
(
0
,
0
,
"name"
),
new
LiteralExpressionSegment
(
0
,
0
,
false
)))));
insertStatementContext
=
new
InsertStatementContext
(
schemaMetaData
,
Collections
.
emptyList
(),
insertStatement
);
insertStatementContext
=
new
InsertStatementContext
(
schemaMetaData
,
Collections
.
emptyList
(),
insertStatement
);
simpleShadowDataSourceRouter
=
new
SimpleShadowDataSource
Router
(
shadowRule
,
insertStatementContext
);
simpleShadowDataSourceRouter
=
new
SimpleShadowDataSource
JudgeEngine
(
shadowRule
,
insertStatementContext
);
assertFalse
(
"should not be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
SQL
());
assertFalse
(
"should not be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
());
}
}
@Test
@Test
...
@@ -95,13 +95,13 @@ public final class SimpleShadowDataSourceRouterTest {
...
@@ -95,13 +95,13 @@ public final class SimpleShadowDataSourceRouterTest {
projectionsSegment
.
getProjections
().
addAll
(
Collections
.
singletonList
(
new
ExpressionProjectionSegment
(
0
,
0
,
"true"
)));
projectionsSegment
.
getProjections
().
addAll
(
Collections
.
singletonList
(
new
ExpressionProjectionSegment
(
0
,
0
,
"true"
)));
selectStatement
.
setProjections
(
projectionsSegment
);
selectStatement
.
setProjections
(
projectionsSegment
);
SelectStatementContext
selectStatementContext
=
new
SelectStatementContext
(
schemaMetaData
,
Collections
.
emptyList
(),
selectStatement
);
SelectStatementContext
selectStatementContext
=
new
SelectStatementContext
(
schemaMetaData
,
Collections
.
emptyList
(),
selectStatement
);
SimpleShadowDataSource
Router
simpleShadowDataSourceRouter
=
new
SimpleShadowDataSourceRouter
(
shadowRule
,
selectStatementContext
);
SimpleShadowDataSource
JudgeEngine
simpleShadowDataSourceRouter
=
new
SimpleShadowDataSourceJudgeEngine
(
shadowRule
,
selectStatementContext
);
assertTrue
(
"should be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
SQL
());
assertTrue
(
"should be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
());
andPredicate
.
getPredicates
().
clear
();
andPredicate
.
getPredicates
().
clear
();
andPredicate
.
getPredicates
().
addAll
(
Collections
.
singletonList
(
andPredicate
.
getPredicates
().
addAll
(
Collections
.
singletonList
(
new
PredicateSegment
(
0
,
0
,
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"shadow"
)),
new
PredicateCompareRightValue
(
0
,
0
,
"="
,
new
LiteralExpressionSegment
(
0
,
0
,
false
)))));
new
PredicateSegment
(
0
,
0
,
new
ColumnSegment
(
0
,
0
,
new
IdentifierValue
(
"shadow"
)),
new
PredicateCompareRightValue
(
0
,
0
,
"="
,
new
LiteralExpressionSegment
(
0
,
0
,
false
)))));
projectionsSegment
.
getProjections
().
clear
();
projectionsSegment
.
getProjections
().
clear
();
projectionsSegment
.
getProjections
().
addAll
(
Collections
.
singletonList
(
new
ExpressionProjectionSegment
(
0
,
0
,
"false"
)));
projectionsSegment
.
getProjections
().
addAll
(
Collections
.
singletonList
(
new
ExpressionProjectionSegment
(
0
,
0
,
"false"
)));
assertFalse
(
"should not be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
SQL
());
assertFalse
(
"should not be shadow"
,
simpleShadowDataSourceRouter
.
isShadow
());
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录