ConditionValueBetweenOperatorGenerator implements ConditionValueGenerator<BetweenExpression> { @Override public Optional<RouteValue> generate(final BetweenExpression predicate, final Column column, final List<Object> parameters) { Optional<Comparable<?>> betweenRouteValue = new ConditionValue(predicate.getBetweenExpr(), parameters).getValue(); Optional<Comparable<?>> andRouteValue = new ConditionValue(predicate.getAndExpr(), parameters).getValue(); if (betweenRouteValue.isPresent() && andRouteValue.isPresent()) { return Optional.of(new RangeRouteValue<>(column.getName(), column.getTableName(), SafeNumberOperationUtils.safeClosed(betweenRouteValue.get(), andRouteValue.get()))); } Date date = new SPITimeService().getTime(); if (!betweenRouteValue.isPresent() && ExpressionConditionUtils.isNowExpression(predicate.getBetweenExpr())) { betweenRouteValue = Optional.of(date); } if (!andRouteValue.isPresent() && ExpressionConditionUtils.isNowExpression(predicate.getAndExpr())) { andRouteValue = Optional.of(date); } return betweenRouteValue.isPresent() && andRouteValue.isPresent() ? Optional.of(new RangeRouteValue<>(column.getName(), column.getTableName(), Range.closed(betweenRouteValue.get(), andRouteValue.get()))) : Optional.empty(); }  @Override Optional<RouteValue> generate(final BetweenExpression predicate, final Column column, final List<Object> parameters);  }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValue() { int between = 1; int and = 2; ExpressionSegment betweenSegment = new LiteralExpressionSegment(0, 0, between); ExpressionSegment andSegment = new LiteralExpressionSegment(0, 0, and); BetweenExpression value = new BetweenExpression(0, 0, null, betweenSegment, andSegment, false); Optional<RouteValue> routeValue = generator.generate(value, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); RangeRouteValue<Integer> rangeRouteValue = (RangeRouteValue<Integer>) routeValue.get(); assertThat(rangeRouteValue.getColumnName(), is(column.getName())); assertThat(rangeRouteValue.getTableName(), is(column.getTableName())); assertTrue(rangeRouteValue.getValueRange().contains(between)); assertTrue(rangeRouteValue.getValueRange().contains(and)); }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValueWithDifferentNumericType() { int between = 3; long and = 3147483647L; ExpressionSegment betweenSegment = new LiteralExpressionSegment(0, 0, between); ExpressionSegment andSegment = new LiteralExpressionSegment(0, 0, and); BetweenExpression value = new BetweenExpression(0, 0, null, betweenSegment, andSegment, false); Optional<RouteValue> routeValue = generator.generate(value, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); RangeRouteValue<Comparable<?>> rangeRouteValue = (RangeRouteValue<Comparable<?>>) routeValue.get(); assertThat(rangeRouteValue.getColumnName(), is(column.getName())); assertThat(rangeRouteValue.getTableName(), is(column.getTableName())); assertTrue(SafeNumberOperationUtils.safeContains(rangeRouteValue.getValueRange(), between)); assertTrue(SafeNumberOperationUtils.safeContains(rangeRouteValue.getValueRange(), 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()"); BetweenExpression value = new BetweenExpression(0, 0, null, betweenSegment, andSegment, false); generator.generate(value, column, new LinkedList<>()); }
@SuppressWarnings("unchecked") @Test public void assertGenerateOneNowConditionValue() { Date date = new Date(); ExpressionSegment betweenSegment = new LiteralExpressionSegment(0, 0, date); ExpressionSegment andSegment = new CommonExpressionSegment(0, 0, "now()"); BetweenExpression value = new BetweenExpression(0, 0, null, betweenSegment, andSegment, false); Optional<RouteValue> routeValue = generator.generate(value, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); RangeRouteValue<Date> rangeRouteValue = (RangeRouteValue<Date>) routeValue.get(); assertThat(rangeRouteValue.getColumnName(), is(column.getName())); assertThat(rangeRouteValue.getTableName(), is(column.getTableName())); assertThat(rangeRouteValue.getValueRange().lowerEndpoint(), is(date)); }
@SuppressWarnings("unchecked") @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()"); BetweenExpression value = new BetweenExpression(0, 0, null, betweenSegment, andSegment, false); Optional<RouteValue> routeValue = generator.generate(value, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); RangeRouteValue<Date> rangeRouteValue = (RangeRouteValue<Date>) routeValue.get(); assertThat(rangeRouteValue.getColumnName(), is(column.getName())); assertThat(rangeRouteValue.getTableName(), is(column.getTableName())); assertTrue(rangeRouteValue.getValueRange().upperEndpoint().before(after)); }