Search in sources :

Example 1 with Condition

use of com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition in project sharding-jdbc by dangdangdotcom.

the class AbstractBaseParseTest method getDataParameter.

private static Object[] getDataParameter(final Assert assertObj) {
    Object[] result = new Object[6];
    result[0] = assertObj.getId();
    result[1] = assertObj.getSql();
    result[2] = assertObj.getExpectedSQL();
    result[3] = Lists.transform(assertObj.getTables(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.Table, Table>() {

        @Override
        public Table apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.Table input) {
            return new Table(input.getName(), input.getAlias());
        }
    });
    if (null == assertObj.getConditionContexts()) {
        result[4] = Collections.<ConditionContext>emptyList();
    } else {
        result[4] = Lists.transform(assertObj.getConditionContexts(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.ConditionContext, ConditionContext>() {

            @Override
            public ConditionContext apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.ConditionContext input) {
                ConditionContext result = new ConditionContext();
                if (null == input.getConditions()) {
                    return result;
                }
                for (com.dangdang.ddframe.rdb.sharding.parser.jaxb.Condition each : input.getConditions()) {
                    Condition condition = new Condition(new Column(each.getColumnName(), each.getTableName()), BinaryOperator.valueOf(each.getOperator().toUpperCase()));
                    condition.getValues().addAll(Lists.transform(each.getValues(), new Function<Value, Comparable<?>>() {

                        @Override
                        public Comparable<?> apply(final Value input) {
                            return input.getValueWithType();
                        }
                    }));
                    if (null != each.getValueIndices()) {
                        condition.getValueIndices().addAll(each.getValueIndices());
                    }
                    result.add(condition);
                }
                return result;
            }
        });
    }
    MergeContext mergeContext = new MergeContext();
    if (null != assertObj.getOrderByColumns()) {
        mergeContext.getOrderByColumns().addAll(Lists.transform(assertObj.getOrderByColumns(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.OrderByColumn, OrderByColumn>() {

            @Override
            public OrderByColumn apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.OrderByColumn input) {
                return Strings.isNullOrEmpty(input.getName()) ? new OrderByColumn(input.getIndex(), OrderByType.valueOf(input.getOrderByType().toUpperCase())) : new OrderByColumn(Optional.fromNullable(input.getOwner()), input.getName(), Optional.fromNullable(input.getAlias()), OrderByType.valueOf(input.getOrderByType().toUpperCase()));
            }
        }));
    }
    if (null != assertObj.getGroupByColumns()) {
        mergeContext.getGroupByColumns().addAll(Lists.transform(assertObj.getGroupByColumns(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.GroupByColumn, GroupByColumn>() {

            @Override
            public GroupByColumn apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.GroupByColumn input) {
                return new GroupByColumn(Optional.fromNullable(input.getOwner()), input.getName(), Optional.fromNullable(input.getAlias()), OrderByType.valueOf(input.getOrderByType().toUpperCase()));
            }
        }));
    }
    if (null != assertObj.getAggregationColumns()) {
        mergeContext.getAggregationColumns().addAll(Lists.transform(assertObj.getAggregationColumns(), new Function<com.dangdang.ddframe.rdb.sharding.parser.jaxb.AggregationColumn, AggregationColumn>() {

            @Override
            public AggregationColumn apply(final com.dangdang.ddframe.rdb.sharding.parser.jaxb.AggregationColumn input) {
                AggregationColumn result = new AggregationColumn(input.getExpression(), AggregationType.valueOf(input.getAggregationType().toUpperCase()), Optional.fromNullable(input.getAlias()), Optional.fromNullable(input.getOption()));
                if (null != input.getIndex()) {
                    result.setColumnIndex(input.getIndex());
                }
                for (com.dangdang.ddframe.rdb.sharding.parser.jaxb.AggregationColumn each : input.getDerivedColumns()) {
                    result.getDerivedColumns().add(new AggregationColumn(each.getExpression(), AggregationType.valueOf(each.getAggregationType().toUpperCase()), Optional.fromNullable(each.getAlias()), Optional.fromNullable(each.getOption())));
                }
                return result;
            }
        }));
    }
    if (null != assertObj.getLimit()) {
        mergeContext.setLimit(new Limit(assertObj.getLimit().getOffset(), assertObj.getLimit().getRowCount(), assertObj.getLimit().getOffsetParameterIndex(), assertObj.getLimit().getRowCountParameterIndex()));
    }
    result[5] = mergeContext;
    return result;
}
Also used : Condition(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition) Table(com.dangdang.ddframe.rdb.sharding.parser.result.router.Table) AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) Function(com.google.common.base.Function) ConditionContext(com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) Column(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.Column) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn) Value(com.dangdang.ddframe.rdb.sharding.parser.jaxb.Value) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext) Limit(com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit)

Example 2 with Condition

use of com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition in project sharding-jdbc by dangdangdotcom.

the class SQLParsedResultTest method generateConditionContext.

private ConditionContext generateConditionContext() {
    ConditionContext result = new ConditionContext();
    Condition condition = new Condition(new Column("id", "order"), BinaryOperator.IN);
    condition.getValues().addAll(Arrays.asList(1, 2, 3));
    result.add(condition);
    return result;
}
Also used : Condition(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition) ConditionContext(com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext) OrderByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn) Column(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.Column) AggregationColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn) GroupByColumn(com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn)

Example 3 with Condition

use of com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition in project sharding-jdbc by dangdangdotcom.

the class SingleRouterUtilTest method testConvertConditionToShardingValue.

@Test
public void testConvertConditionToShardingValue() throws Exception {
    Condition condition = new Condition(new Condition.Column("test", "test"), Condition.BinaryOperator.EQUAL);
    condition.getValues().add(1);
    ShardingValue<?> shardingValue = SingleRouterUtil.convertConditionToShardingValue(condition);
    assertThat(shardingValue.getType(), is(ShardingValue.ShardingValueType.SINGLE));
    assertThat((Integer) shardingValue.getValue(), is(1));
    condition = new Condition(new Condition.Column("test", "test"), Condition.BinaryOperator.IN);
    condition.getValues().add(1);
    condition.getValues().add(2);
    shardingValue = SingleRouterUtil.convertConditionToShardingValue(condition);
    assertThat(shardingValue.getType(), is(ShardingValue.ShardingValueType.LIST));
    Iterator<?> iterator = shardingValue.getValues().iterator();
    assertThat((Integer) iterator.next(), is(1));
    assertThat((Integer) iterator.next(), is(2));
    condition = new Condition(new Condition.Column("test", "test"), Condition.BinaryOperator.BETWEEN);
    condition.getValues().add(1);
    condition.getValues().add(2);
    shardingValue = SingleRouterUtil.convertConditionToShardingValue(condition);
    assertThat(shardingValue.getType(), is(ShardingValue.ShardingValueType.RANGE));
    assertThat((Integer) shardingValue.getValueRange().lowerEndpoint(), is(1));
    assertThat((Integer) shardingValue.getValueRange().upperEndpoint(), is(2));
}
Also used : Condition(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition) Test(org.junit.Test)

Example 4 with Condition

use of com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition in project sharding-jdbc by dangdangdotcom.

the class ParseContext method addCondition.

private void addCondition(final Column column, final BinaryOperator operator, final List<ValuePair> valuePairs) {
    Optional<Condition> optionalCondition = currentConditionContext.find(column.getTableName(), column.getColumnName(), operator);
    Condition condition;
    // TODO 待讨论
    if (optionalCondition.isPresent()) {
        condition = optionalCondition.get();
    } else {
        condition = new Condition(column, operator);
        currentConditionContext.add(condition);
    }
    for (ValuePair each : valuePairs) {
        condition.getValues().add(each.value);
        if (each.paramIndex > -1) {
            condition.getValueIndices().add(each.paramIndex);
        }
    }
}
Also used : Condition(com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition)

Aggregations

Condition (com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition)4 AggregationColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn)2 GroupByColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.GroupByColumn)2 OrderByColumn (com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn)2 Column (com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.Column)2 ConditionContext (com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext)2 Value (com.dangdang.ddframe.rdb.sharding.parser.jaxb.Value)1 Limit (com.dangdang.ddframe.rdb.sharding.parser.result.merger.Limit)1 MergeContext (com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext)1 Table (com.dangdang.ddframe.rdb.sharding.parser.result.router.Table)1 Function (com.google.common.base.Function)1 Test (org.junit.Test)1