Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
a515d93f
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,发现更多精彩内容 >>
提交
a515d93f
编写于
10月 11, 2019
作者:
P
Pramy
提交者:
Liang Zhang
10月 11, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support now() for sharding value (#3217)
Change-Id: I3fb454efc9089e10f1e3a8c5d4eb575327816548
上级
1a443379
变更
16
显示空白变更内容
内联
并排
Showing
16 changed file
with
604 addition
and
4 deletion
+604
-4
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/ExpressionConditionUtils.java
...e/router/sharding/condition/ExpressionConditionUtils.java
+38
-0
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/InsertClauseShardingConditionEngine.java
...condition/engine/InsertClauseShardingConditionEngine.java
+9
-2
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
...enerator/impl/ConditionValueBetweenOperatorGenerator.java
+15
-1
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
...enerator/impl/ConditionValueCompareOperatorGenerator.java
+9
-1
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueInOperatorGenerator.java
...ion/generator/impl/ConditionValueInOperatorGenerator.java
+7
-0
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/spi/DefaultTimeService.java
...che/shardingsphere/core/route/spi/DefaultTimeService.java
+34
-0
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/spi/SPITimeService.java
.../apache/shardingsphere/core/route/spi/SPITimeService.java
+49
-0
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/spi/TimeService.java
...org/apache/shardingsphere/core/route/spi/TimeService.java
+34
-0
sharding-core/sharding-core-route/src/main/resources/META-INF/services/org.apache.shardingsphere.core.route.spi.TimeService
...ices/org.apache.shardingsphere.core.route.spi.TimeService
+18
-0
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/fixture/TimeServiceFixture.java
...shardingsphere/core/route/fixture/TimeServiceFixture.java
+36
-0
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/ExpressionConditionUtilsTest.java
...uter/sharding/condition/ExpressionConditionUtilsTest.java
+35
-0
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
...ator/impl/ConditionValueBetweenOperatorGeneratorTest.java
+102
-0
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueCompareOperatorGeneratorTest.java
...ator/impl/ConditionValueCompareOperatorGeneratorTest.java
+74
-0
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueInOperatorGeneratorTest.java
...generator/impl/ConditionValueInOperatorGeneratorTest.java
+55
-0
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/spi/SPITimeServiceTest.java
...che/shardingsphere/core/route/spi/SPITimeServiceTest.java
+71
-0
sharding-core/sharding-core-route/src/test/resources/META-INF/services/org.apache.shardingsphere.core.route.spi.TimeService
...ices/org.apache.shardingsphere.core.route.spi.TimeService
+18
-0
未找到文件。
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/ExpressionConditionUtils.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.router.sharding.condition
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.complex.ComplexExpressionSegment
;
/**
* Expression judgment tool for route.
*
* @author chenchuangliu
*/
public
final
class
ExpressionConditionUtils
{
/**
* Judge now() expression.
* @param segment ExpressionSegment
* @return true or false
*/
public
static
boolean
isNowExpression
(
final
ExpressionSegment
segment
)
{
return
segment
instanceof
ComplexExpressionSegment
&&
"now()"
.
equals
(((
ComplexExpressionSegment
)
segment
).
getText
().
toLowerCase
());
}
}
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/InsertClauseShardingConditionEngine.java
浏览文件 @
a515d93f
...
...
@@ -25,8 +25,10 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegme
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.SimpleExpressionSegment
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.ExpressionConditionUtils
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.ShardingCondition
;
import
org.apache.shardingsphere.core.route.router.sharding.keygen.GeneratedKey
;
import
org.apache.shardingsphere.core.route.spi.SPITimeService
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.core.strategy.route.value.ListRouteValue
;
...
...
@@ -78,10 +80,15 @@ public final class InsertClauseShardingConditionEngine {
private
ShardingCondition
createShardingCondition
(
final
String
tableName
,
final
Iterator
<
String
>
columnNames
,
final
InsertValueContext
insertValueContext
,
final
List
<
Object
>
parameters
)
{
ShardingCondition
result
=
new
ShardingCondition
();
SPITimeService
timeService
=
new
SPITimeService
();
for
(
ExpressionSegment
each
:
insertValueContext
.
getValueExpressions
())
{
String
columnName
=
columnNames
.
next
();
if
(
each
instanceof
SimpleExpressionSegment
&&
shardingRule
.
isShardingColumn
(
columnName
,
tableName
))
{
if
(
shardingRule
.
isShardingColumn
(
columnName
,
tableName
))
{
if
(
each
instanceof
SimpleExpressionSegment
)
{
result
.
getRouteValues
().
add
(
new
ListRouteValue
<>(
columnName
,
tableName
,
Collections
.
singletonList
(
getRouteValue
((
SimpleExpressionSegment
)
each
,
parameters
))));
}
else
if
(
ExpressionConditionUtils
.
isNowExpression
(
each
))
{
result
.
getRouteValues
().
add
(
new
ListRouteValue
<>(
columnName
,
tableName
,
Collections
.
singletonList
(
timeService
.
getTime
())));
}
}
}
return
result
;
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
浏览文件 @
a515d93f
...
...
@@ -20,12 +20,15 @@ package org.apache.shardingsphere.core.route.router.sharding.condition.generator
import
com.google.common.base.Optional
;
import
com.google.common.collect.Range
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.Column
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.ExpressionConditionUtils
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.generator.ConditionValue
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.generator.ConditionValueGenerator
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateBetweenRightValue
;
import
org.apache.shardingsphere.core.route.spi.SPITimeService
;
import
org.apache.shardingsphere.core.strategy.route.value.RangeRouteValue
;
import
org.apache.shardingsphere.core.strategy.route.value.RouteValue
;
import
java.util.Date
;
import
java.util.List
;
/**
...
...
@@ -39,7 +42,18 @@ public final class ConditionValueBetweenOperatorGenerator implements ConditionVa
public
Optional
<
RouteValue
>
generate
(
final
PredicateBetweenRightValue
predicateRightValue
,
final
Column
column
,
final
List
<
Object
>
parameters
)
{
Optional
<
Comparable
>
betweenRouteValue
=
new
ConditionValue
(
predicateRightValue
.
getBetweenExpression
(),
parameters
).
getValue
();
Optional
<
Comparable
>
andRouteValue
=
new
ConditionValue
(
predicateRightValue
.
getAndExpression
(),
parameters
).
getValue
();
if
(
betweenRouteValue
.
isPresent
()
&&
andRouteValue
.
isPresent
())
{
return
Optional
.<
RouteValue
>
of
(
new
RangeRouteValue
<>(
column
.
getName
(),
column
.
getTableName
(),
Range
.
closed
(
betweenRouteValue
.
get
(),
andRouteValue
.
get
())));
}
Date
date
=
new
SPITimeService
().
getTime
();
if
(!
betweenRouteValue
.
isPresent
()
&&
ExpressionConditionUtils
.
isNowExpression
(
predicateRightValue
.
getBetweenExpression
()))
{
betweenRouteValue
=
Optional
.
of
((
Comparable
)
date
);
}
if
(!
andRouteValue
.
isPresent
()
&&
ExpressionConditionUtils
.
isNowExpression
(
predicateRightValue
.
getAndExpression
()))
{
andRouteValue
=
Optional
.
of
((
Comparable
)
date
);
}
return
betweenRouteValue
.
isPresent
()
&&
andRouteValue
.
isPresent
()
?
Optional
.<
RouteValue
>
of
(
new
RangeRouteValue
<>(
column
.
getName
(),
column
.
getTableName
(),
Range
.
closed
(
betweenRouteValue
.
get
(),
andRouteValue
.
get
())))
:
Optional
.<
RouteValue
>
absent
();
?
Optional
.<
RouteValue
>
of
(
new
RangeRouteValue
<>(
column
.
getName
(),
column
.
getTableName
(),
Range
.
closed
(
betweenRouteValue
.
get
(),
andRouteValue
.
get
())))
:
Optional
.<
RouteValue
>
absent
();
}
}
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
浏览文件 @
a515d93f
...
...
@@ -20,9 +20,11 @@ package org.apache.shardingsphere.core.route.router.sharding.condition.generator
import
com.google.common.base.Optional
;
import
com.google.common.collect.Lists
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.Column
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.ExpressionConditionUtils
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.generator.ConditionValue
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.generator.ConditionValueGenerator
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateCompareRightValue
;
import
org.apache.shardingsphere.core.route.spi.SPITimeService
;
import
org.apache.shardingsphere.core.strategy.route.value.ListRouteValue
;
import
org.apache.shardingsphere.core.strategy.route.value.RouteValue
;
...
...
@@ -41,7 +43,13 @@ public final class ConditionValueCompareOperatorGenerator implements ConditionVa
return
Optional
.
absent
();
}
Optional
<
Comparable
>
routeValue
=
new
ConditionValue
(
predicateRightValue
.
getExpression
(),
parameters
).
getValue
();
return
routeValue
.
isPresent
()
?
Optional
.<
RouteValue
>
of
(
new
ListRouteValue
<>(
column
.
getName
(),
column
.
getTableName
(),
Lists
.
newArrayList
(
routeValue
.
get
())))
:
Optional
.<
RouteValue
>
absent
();
if
(
routeValue
.
isPresent
())
{
return
Optional
.<
RouteValue
>
of
(
new
ListRouteValue
<>(
column
.
getName
(),
column
.
getTableName
(),
Lists
.
newArrayList
(
routeValue
.
get
())));
}
if
(
ExpressionConditionUtils
.
isNowExpression
(
predicateRightValue
.
getExpression
()))
{
return
Optional
.<
RouteValue
>
of
(
new
ListRouteValue
<>(
column
.
getName
(),
column
.
getTableName
(),
Lists
.
newArrayList
(
new
SPITimeService
().
getTime
())));
}
return
Optional
.
absent
();
}
private
boolean
isSupportedOperator
(
final
String
operator
)
{
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueInOperatorGenerator.java
浏览文件 @
a515d93f
...
...
@@ -19,10 +19,12 @@ package org.apache.shardingsphere.core.route.router.sharding.condition.generator
import
com.google.common.base.Optional
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.Column
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.ExpressionConditionUtils
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.generator.ConditionValue
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.generator.ConditionValueGenerator
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateInRightValue
;
import
org.apache.shardingsphere.core.route.spi.SPITimeService
;
import
org.apache.shardingsphere.core.strategy.route.value.ListRouteValue
;
import
org.apache.shardingsphere.core.strategy.route.value.RouteValue
;
...
...
@@ -39,10 +41,15 @@ public final class ConditionValueInOperatorGenerator implements ConditionValueGe
@Override
public
Optional
<
RouteValue
>
generate
(
final
PredicateInRightValue
predicateRightValue
,
final
Column
column
,
final
List
<
Object
>
parameters
)
{
List
<
Comparable
>
routeValues
=
new
LinkedList
<>();
SPITimeService
timeService
=
new
SPITimeService
();
for
(
ExpressionSegment
each
:
predicateRightValue
.
getSqlExpressions
())
{
Optional
<
Comparable
>
routeValue
=
new
ConditionValue
(
each
,
parameters
).
getValue
();
if
(
routeValue
.
isPresent
())
{
routeValues
.
add
(
routeValue
.
get
());
continue
;
}
if
(
ExpressionConditionUtils
.
isNowExpression
(
each
))
{
routeValues
.
add
(
timeService
.
getTime
());
}
}
return
routeValues
.
isEmpty
()
?
Optional
.<
RouteValue
>
absent
()
:
Optional
.<
RouteValue
>
of
(
new
ListRouteValue
<>(
column
.
getName
(),
column
.
getTableName
(),
routeValues
));
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/spi/DefaultTimeService.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.spi
;
import
java.util.Date
;
/**
* Default time service.
*
* @author chenchuangliu
*/
public
final
class
DefaultTimeService
implements
TimeService
{
@Override
public
Date
getTime
()
{
return
new
Date
();
}
}
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/spi/SPITimeService.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.spi
;
import
org.apache.shardingsphere.core.spi.NewInstanceServiceLoader
;
import
java.util.Collection
;
import
java.util.Date
;
/**
* Time service for SPI.
*
* @author chenchuangliu
*/
public
final
class
SPITimeService
implements
TimeService
{
private
final
Collection
<
TimeService
>
timeServices
=
NewInstanceServiceLoader
.
newServiceInstances
(
TimeService
.
class
);
static
{
NewInstanceServiceLoader
.
register
(
TimeService
.
class
);
}
@Override
public
Date
getTime
()
{
Date
date
=
null
;
for
(
TimeService
server
:
timeServices
)
{
date
=
server
.
getTime
();
if
(!(
server
instanceof
DefaultTimeService
)
&&
null
!=
date
)
{
return
date
;
}
}
return
date
;
}
}
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/spi/TimeService.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.spi
;
import
java.util.Date
;
/**
* Time service.
*
* @author chenchuangliu
*/
public
interface
TimeService
{
/**
* Get date from time server.
* @return date
*/
Date
getTime
();
}
sharding-core/sharding-core-route/src/main/resources/META-INF/services/org.apache.shardingsphere.core.route.spi.TimeService
0 → 100644
浏览文件 @
a515d93f
#
# 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.
#
org.apache.shardingsphere.core.route.spi.DefaultTimeService
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/fixture/TimeServiceFixture.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.fixture
;
import
lombok.Getter
;
import
lombok.Setter
;
import
org.apache.shardingsphere.core.route.spi.TimeService
;
import
java.util.Date
;
public
final
class
TimeServiceFixture
implements
TimeService
{
@Getter
@Setter
private
Date
date
;
@Override
public
Date
getTime
()
{
return
date
;
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/ExpressionConditionUtilsTest.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.router.sharding.condition
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.complex.CommonExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
final
class
ExpressionConditionUtilsTest
{
@Test
public
void
assertIsNowExpression
()
{
assertFalse
(
ExpressionConditionUtils
.
isNowExpression
(
new
LiteralExpressionSegment
(
0
,
0
,
new
Object
())));
assertFalse
(
ExpressionConditionUtils
.
isNowExpression
(
new
CommonExpressionSegment
(
0
,
0
,
"shardingsphere"
)));
assertTrue
(
ExpressionConditionUtils
.
isNowExpression
(
new
CommonExpressionSegment
(
0
,
0
,
"NOW()"
)));
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueBetweenOperatorGeneratorTest.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.router.sharding.condition.generator.impl
;
import
com.google.common.base.Optional
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.complex.CommonExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateBetweenRightValue
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.Column
;
import
org.apache.shardingsphere.core.strategy.route.value.RangeRouteValue
;
import
org.apache.shardingsphere.core.strategy.route.value.RouteValue
;
import
org.junit.Test
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.LinkedList
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
final
class
ConditionValueBetweenOperatorGeneratorTest
{
private
ConditionValueBetweenOperatorGenerator
generator
=
new
ConditionValueBetweenOperatorGenerator
();
private
Column
column
=
new
Column
(
"shardsphere"
,
"apache"
);
@Test
public
void
assertGenerateConditionValue
()
{
int
between
=
1
;
int
and
=
2
;
ExpressionSegment
betweenSegment
=
new
LiteralExpressionSegment
(
0
,
0
,
between
);
ExpressionSegment
andSegment
=
new
LiteralExpressionSegment
(
0
,
0
,
and
);
PredicateBetweenRightValue
value
=
new
PredicateBetweenRightValue
(
betweenSegment
,
andSegment
);
Optional
<
RouteValue
>
optional
=
generator
.
generate
(
value
,
column
,
new
LinkedList
<>());
assertTrue
(
optional
.
isPresent
());
assertTrue
(
optional
.
get
()
instanceof
RangeRouteValue
);
RangeRouteValue
<
Integer
>
rangeRouteValue
=
(
RangeRouteValue
<
Integer
>)
optional
.
get
();
assertEquals
(
rangeRouteValue
.
getColumnName
(),
column
.
getName
());
assertEquals
(
rangeRouteValue
.
getTableName
(),
column
.
getTableName
());
assertTrue
(
rangeRouteValue
.
getValueRange
().
contains
(
between
));
assertTrue
(
rangeRouteValue
.
getValueRange
().
contains
(
and
));
}
@Test
(
expected
=
ClassCastException
.
class
)
public
void
assertGenerateErrorConditionValue
()
{
int
between
=
1
;
ExpressionSegment
betweenSegment
=
new
LiteralExpressionSegment
(
0
,
0
,
between
);
ExpressionSegment
andSegment
=
new
CommonExpressionSegment
(
0
,
0
,
"now()"
);
PredicateBetweenRightValue
value
=
new
PredicateBetweenRightValue
(
betweenSegment
,
andSegment
);
generator
.
generate
(
value
,
column
,
new
LinkedList
<>());
}
@Test
public
void
assertGenerateOneNowConditionValue
()
{
Date
date
=
new
Date
();
ExpressionSegment
betweenSegment
=
new
LiteralExpressionSegment
(
0
,
0
,
date
);
ExpressionSegment
andSegment
=
new
CommonExpressionSegment
(
0
,
0
,
"now()"
);
PredicateBetweenRightValue
value
=
new
PredicateBetweenRightValue
(
betweenSegment
,
andSegment
);
Optional
<
RouteValue
>
optional
=
generator
.
generate
(
value
,
column
,
new
LinkedList
<>());
assertTrue
(
optional
.
isPresent
());
assertTrue
(
optional
.
get
()
instanceof
RangeRouteValue
);
RangeRouteValue
<
Date
>
rangeRouteValue
=
(
RangeRouteValue
<
Date
>)
optional
.
get
();
assertEquals
(
rangeRouteValue
.
getColumnName
(),
column
.
getName
());
assertEquals
(
rangeRouteValue
.
getTableName
(),
column
.
getTableName
());
assertThat
(
rangeRouteValue
.
getValueRange
().
lowerEndpoint
(),
is
(
date
));
}
@Test
public
void
assertGenerateNowConditionValue
()
{
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
add
(
Calendar
.
DATE
,
1
);
final
Date
after
=
calendar
.
getTime
();
ExpressionSegment
betweenSegment
=
new
CommonExpressionSegment
(
0
,
0
,
"now()"
);
ExpressionSegment
andSegment
=
new
CommonExpressionSegment
(
0
,
0
,
"now()"
);
PredicateBetweenRightValue
value
=
new
PredicateBetweenRightValue
(
betweenSegment
,
andSegment
);
Optional
<
RouteValue
>
optional
=
generator
.
generate
(
value
,
column
,
new
LinkedList
<>());
assertTrue
(
optional
.
isPresent
());
assertTrue
(
optional
.
get
()
instanceof
RangeRouteValue
);
RangeRouteValue
<
Date
>
rangeRouteValue
=
(
RangeRouteValue
<
Date
>)
optional
.
get
();
assertEquals
(
rangeRouteValue
.
getColumnName
(),
column
.
getName
());
assertEquals
(
rangeRouteValue
.
getTableName
(),
column
.
getTableName
());
assertTrue
(
rangeRouteValue
.
getValueRange
().
upperEndpoint
().
before
(
after
));
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueCompareOperatorGeneratorTest.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.router.sharding.condition.generator.impl
;
import
com.google.common.base.Optional
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.complex.CommonExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateCompareRightValue
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.Column
;
import
org.apache.shardingsphere.core.strategy.route.value.ListRouteValue
;
import
org.apache.shardingsphere.core.strategy.route.value.RouteValue
;
import
org.junit.Test
;
import
java.util.LinkedList
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
final
class
ConditionValueCompareOperatorGeneratorTest
{
private
ConditionValueCompareOperatorGenerator
generator
=
new
ConditionValueCompareOperatorGenerator
();
private
Column
column
=
new
Column
(
"shardsphere"
,
"apache"
);
@Test
public
void
assertGenerateConditionValue
()
{
int
value
=
1
;
PredicateCompareRightValue
rightValue
=
new
PredicateCompareRightValue
(
"="
,
new
LiteralExpressionSegment
(
0
,
0
,
value
));
Optional
<
RouteValue
>
routeValueOptional
=
generator
.
generate
(
rightValue
,
column
,
new
LinkedList
<>());
assertTrue
(
routeValueOptional
.
isPresent
());
assertTrue
(
routeValueOptional
.
get
()
instanceof
ListRouteValue
);
ListRouteValue
<
Integer
>
listRouteValue
=
(
ListRouteValue
<
Integer
>)
routeValueOptional
.
get
();
assertTrue
(
listRouteValue
.
getValues
().
contains
(
value
));
}
@Test
public
void
assertGenerateConditionValueWithErrorOperator
()
{
PredicateCompareRightValue
rightValue
=
new
PredicateCompareRightValue
(
"<"
,
new
LiteralExpressionSegment
(
0
,
0
,
1
));
Optional
<
RouteValue
>
routeValueOptional
=
generator
.
generate
(
rightValue
,
column
,
new
LinkedList
<>());
assertFalse
(
routeValueOptional
.
isPresent
());
}
@Test
public
void
assertGenerateConditionValueWithoutNowExpression
()
{
PredicateCompareRightValue
rightValue
=
new
PredicateCompareRightValue
(
"="
,
new
CommonExpressionSegment
(
0
,
0
,
"value"
));
Optional
<
RouteValue
>
routeValueOptional
=
generator
.
generate
(
rightValue
,
column
,
new
LinkedList
<>());
assertFalse
(
routeValueOptional
.
isPresent
());
}
@Test
public
void
assertGenerateConditionValueWithNowExpression
()
{
PredicateCompareRightValue
rightValue
=
new
PredicateCompareRightValue
(
"="
,
new
CommonExpressionSegment
(
0
,
0
,
"now()"
));
Optional
<
RouteValue
>
routeValueOptional
=
generator
.
generate
(
rightValue
,
column
,
new
LinkedList
<>());
assertTrue
(
routeValueOptional
.
isPresent
());
assertTrue
(
routeValueOptional
.
get
()
instanceof
ListRouteValue
);
ListRouteValue
<
Integer
>
listRouteValue
=
(
ListRouteValue
<
Integer
>)
routeValueOptional
.
get
();
assertFalse
(
listRouteValue
.
getValues
().
isEmpty
());
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/condition/generator/impl/ConditionValueInOperatorGeneratorTest.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.router.sharding.condition.generator.impl
;
import
com.google.common.base.Optional
;
import
com.google.common.collect.Lists
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.complex.CommonExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.value.PredicateInRightValue
;
import
org.apache.shardingsphere.core.route.router.sharding.condition.Column
;
import
org.apache.shardingsphere.core.strategy.route.value.ListRouteValue
;
import
org.apache.shardingsphere.core.strategy.route.value.RouteValue
;
import
org.junit.Test
;
import
java.util.Collection
;
import
java.util.Date
;
import
java.util.LinkedList
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
final
class
ConditionValueInOperatorGeneratorTest
{
private
ConditionValueInOperatorGenerator
generator
=
new
ConditionValueInOperatorGenerator
();
private
Column
column
=
new
Column
(
"shardsphere"
,
"apache"
);
@Test
public
void
assertNowExpression
()
{
Collection
<
ExpressionSegment
>
segmentCollection
=
Lists
.
newArrayList
();
segmentCollection
.
add
(
new
CommonExpressionSegment
(
0
,
0
,
"now()"
));
PredicateInRightValue
inRightValue
=
new
PredicateInRightValue
(
segmentCollection
);
Optional
<
RouteValue
>
optional
=
generator
.
generate
(
inRightValue
,
column
,
new
LinkedList
<>());
assertTrue
(
optional
.
isPresent
());
assertTrue
(
optional
.
get
()
instanceof
ListRouteValue
);
ListRouteValue
listRouteValue
=
(
ListRouteValue
)
optional
.
get
();
assertFalse
(
listRouteValue
.
getValues
().
isEmpty
());
assertTrue
(
listRouteValue
.
getValues
().
iterator
().
next
()
instanceof
Date
);
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/spi/SPITimeServiceTest.java
0 → 100644
浏览文件 @
a515d93f
/*
* 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.core.route.spi
;
import
com.google.common.base.Optional
;
import
lombok.SneakyThrows
;
import
org.apache.shardingsphere.core.route.fixture.TimeServiceFixture
;
import
org.junit.Test
;
import
java.lang.reflect.Field
;
import
java.util.Calendar
;
import
java.util.Collection
;
import
java.util.Date
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
SPITimeServiceTest
{
private
SPITimeService
timeService
=
new
SPITimeService
();
@Test
public
void
assertGetTime
()
{
Optional
<
TimeServiceFixture
>
optional
=
getFixtureHook
();
assertTrue
(
optional
.
isPresent
());
Date
date
=
new
Date
();
optional
.
get
().
setDate
(
date
);
Date
time
=
timeService
.
getTime
();
assertThat
(
date
,
is
(
time
));
}
@Test
public
void
assertGetTimeWithDefault
()
{
Optional
<
TimeServiceFixture
>
optional
=
getFixtureHook
();
assertTrue
(
optional
.
isPresent
());
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
add
(
Calendar
.
DATE
,
-
1
);
Date
date
=
calendar
.
getTime
();
Date
time
=
timeService
.
getTime
();
assertTrue
(
time
.
after
(
date
));
}
@SneakyThrows
private
Optional
<
TimeServiceFixture
>
getFixtureHook
()
{
Field
routingHooksField
=
SPITimeService
.
class
.
getDeclaredField
(
"timeServices"
);
routingHooksField
.
setAccessible
(
true
);
Collection
<
TimeService
>
timeServices
=
(
Collection
<
TimeService
>)
routingHooksField
.
get
(
timeService
);
for
(
TimeService
timeService
:
timeServices
)
{
if
(
timeService
instanceof
TimeServiceFixture
)
{
return
Optional
.
of
((
TimeServiceFixture
)
timeService
);
}
}
return
Optional
.
absent
();
}
}
sharding-core/sharding-core-route/src/test/resources/META-INF/services/org.apache.shardingsphere.core.route.spi.TimeService
0 → 100644
浏览文件 @
a515d93f
#
# 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.
#
org.apache.shardingsphere.core.route.fixture.TimeServiceFixture
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录