提交 a515d93f 编写于 作者: P Pramy 提交者: Liang Zhang

support now() for sharding value (#3217)

Change-Id: I3fb454efc9089e10f1e3a8c5d4eb575327816548
上级 1a443379
/*
* 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());
}
}
......@@ -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)) {
result.getRouteValues().add(new ListRouteValue<>(columnName, tableName, Collections.singletonList(getRouteValue((SimpleExpressionSegment) each, parameters))));
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;
......
......@@ -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();
}
}
......@@ -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) {
......
......@@ -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));
......
/*
* 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();
}
}
/*
* 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;
}
}
/*
* 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();
}
#
# 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
/*
* 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;
}
}
/*
* 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()")));
}
}
/*
* 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));
}
}
/*
* 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());
}
}
/*
* 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);
}
}
/*
* 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();
}
}
#
# 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.
先完成此消息的编辑!
想要评论请 注册