Loading sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/adapter/AbstractConnectionAdapter.java +2 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import java.sql.SQLWarning; import java.util.Collection; import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationConnection; import com.dangdang.ddframe.rdb.sharding.metrics.ThreadLocalObjectContainer; /** * 数据库连接适配类. Loading Loading @@ -79,6 +80,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera each.close(); } closed = true; ThreadLocalObjectContainer.clear(); } @Override Loading sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AccumulationAggregationUnit.java +7 −2 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ package com.dangdang.ddframe.rdb.sharding.merger.aggregation; import java.math.BigDecimal; import com.dangdang.ddframe.rdb.sharding.merger.common.ResultSetUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; Loading @@ -35,10 +34,16 @@ public final class AccumulationAggregationUnit extends AbstractAggregationUnit { private final Class<?> returnType; private BigDecimal result = new BigDecimal(0); private BigDecimal result; @Override public void doMerge(final Comparable<?>... values) { if (null == values || null == values[0]) { return; } if (null == result) { result = new BigDecimal("0"); } result = result.add(new BigDecimal(values[0].toString())); log.trace("Accumulation result: {}", result.toString()); } Loading sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AggregationInvokeHandler.java +2 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import java.sql.SQLException; import com.dangdang.ddframe.rdb.sharding.merger.common.AbstractMergerInvokeHandler; import com.dangdang.ddframe.rdb.sharding.merger.common.ResultSetQueryIndex; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn; import com.dangdang.ddframe.rdb.sharding.util.SQLUtil; import com.google.common.base.Optional; /** Loading Loading @@ -52,7 +53,7 @@ public final class AggregationInvokeHandler extends AbstractMergerInvokeHandler< return Optional.of(each); } else if (each.getAlias().isPresent() && each.getAlias().get().equals(resultSetQueryIndex.getQueryName())) { return Optional.of(each); } else if (each.getExpression().equalsIgnoreCase(resultSetQueryIndex.getQueryName())) { } else if (SQLUtil.getExactlyValue(each.getExpression()).equalsIgnoreCase(SQLUtil.getExactlyValue(resultSetQueryIndex.getQueryName()))) { return Optional.of(each); } } Loading sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AggregationUnitFactory.java +2 −4 Original line number Diff line number Diff line Loading @@ -17,9 +17,7 @@ package com.dangdang.ddframe.rdb.sharding.merger.aggregation; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; Loading @@ -34,9 +32,9 @@ public final class AggregationUnitFactory { public static AggregationUnit create(final AggregationType type, final Class<?> returnType) { switch (type) { case MAX: return new ComparableAggregationUnit(false); return new ComparableAggregationUnit(false, returnType); case MIN: return new ComparableAggregationUnit(true); return new ComparableAggregationUnit(true, returnType); case SUM: case COUNT: return new AccumulationAggregationUnit(returnType); Loading sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AvgAggregationUnit.java +14 −3 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ package com.dangdang.ddframe.rdb.sharding.merger.aggregation; import java.math.BigDecimal; import com.dangdang.ddframe.rdb.sharding.merger.common.ResultSetUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; Loading @@ -35,12 +34,21 @@ public class AvgAggregationUnit extends AbstractAggregationUnit { private final Class<?> returnType; private BigDecimal count = new BigDecimal(0); private BigDecimal count; private BigDecimal sum = new BigDecimal(0); private BigDecimal sum; @Override public void doMerge(final Comparable<?>... values) { if (null == values || null == values[0] || null == values[1]) { return; } if (null == count) { count = new BigDecimal("0"); } if (null == sum) { sum = new BigDecimal("0"); } count = count.add(new BigDecimal(values[0].toString())); sum = sum.add(new BigDecimal(values[1].toString())); log.trace("AVG result COUNT: {} SUM: {}", count, sum); Loading @@ -48,6 +56,9 @@ public class AvgAggregationUnit extends AbstractAggregationUnit { @Override public Comparable<?> getResult() { if (null == count || BigDecimal.ZERO.equals(count)) { return (Comparable<?>) ResultSetUtil.convertValue(count, returnType); } // TODO 通过metadata获取数据库的浮点数精度值 return (Comparable<?>) ResultSetUtil.convertValue(sum.divide(count, 4, BigDecimal.ROUND_HALF_UP), returnType); } Loading Loading
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/adapter/AbstractConnectionAdapter.java +2 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import java.sql.SQLWarning; import java.util.Collection; import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationConnection; import com.dangdang.ddframe.rdb.sharding.metrics.ThreadLocalObjectContainer; /** * 数据库连接适配类. Loading Loading @@ -79,6 +80,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera each.close(); } closed = true; ThreadLocalObjectContainer.clear(); } @Override Loading
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AccumulationAggregationUnit.java +7 −2 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ package com.dangdang.ddframe.rdb.sharding.merger.aggregation; import java.math.BigDecimal; import com.dangdang.ddframe.rdb.sharding.merger.common.ResultSetUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; Loading @@ -35,10 +34,16 @@ public final class AccumulationAggregationUnit extends AbstractAggregationUnit { private final Class<?> returnType; private BigDecimal result = new BigDecimal(0); private BigDecimal result; @Override public void doMerge(final Comparable<?>... values) { if (null == values || null == values[0]) { return; } if (null == result) { result = new BigDecimal("0"); } result = result.add(new BigDecimal(values[0].toString())); log.trace("Accumulation result: {}", result.toString()); } Loading
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AggregationInvokeHandler.java +2 −1 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ import java.sql.SQLException; import com.dangdang.ddframe.rdb.sharding.merger.common.AbstractMergerInvokeHandler; import com.dangdang.ddframe.rdb.sharding.merger.common.ResultSetQueryIndex; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn; import com.dangdang.ddframe.rdb.sharding.util.SQLUtil; import com.google.common.base.Optional; /** Loading Loading @@ -52,7 +53,7 @@ public final class AggregationInvokeHandler extends AbstractMergerInvokeHandler< return Optional.of(each); } else if (each.getAlias().isPresent() && each.getAlias().get().equals(resultSetQueryIndex.getQueryName())) { return Optional.of(each); } else if (each.getExpression().equalsIgnoreCase(resultSetQueryIndex.getQueryName())) { } else if (SQLUtil.getExactlyValue(each.getExpression()).equalsIgnoreCase(SQLUtil.getExactlyValue(resultSetQueryIndex.getQueryName()))) { return Optional.of(each); } } Loading
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AggregationUnitFactory.java +2 −4 Original line number Diff line number Diff line Loading @@ -17,9 +17,7 @@ package com.dangdang.ddframe.rdb.sharding.merger.aggregation; import com.dangdang.ddframe.rdb.sharding.parser.result.merger.AggregationColumn.AggregationType; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; Loading @@ -34,9 +32,9 @@ public final class AggregationUnitFactory { public static AggregationUnit create(final AggregationType type, final Class<?> returnType) { switch (type) { case MAX: return new ComparableAggregationUnit(false); return new ComparableAggregationUnit(false, returnType); case MIN: return new ComparableAggregationUnit(true); return new ComparableAggregationUnit(true, returnType); case SUM: case COUNT: return new AccumulationAggregationUnit(returnType); Loading
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/merger/aggregation/AvgAggregationUnit.java +14 −3 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ package com.dangdang.ddframe.rdb.sharding.merger.aggregation; import java.math.BigDecimal; import com.dangdang.ddframe.rdb.sharding.merger.common.ResultSetUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; Loading @@ -35,12 +34,21 @@ public class AvgAggregationUnit extends AbstractAggregationUnit { private final Class<?> returnType; private BigDecimal count = new BigDecimal(0); private BigDecimal count; private BigDecimal sum = new BigDecimal(0); private BigDecimal sum; @Override public void doMerge(final Comparable<?>... values) { if (null == values || null == values[0] || null == values[1]) { return; } if (null == count) { count = new BigDecimal("0"); } if (null == sum) { sum = new BigDecimal("0"); } count = count.add(new BigDecimal(values[0].toString())); sum = sum.add(new BigDecimal(values[1].toString())); log.trace("AVG result COUNT: {} SUM: {}", count, sum); Loading @@ -48,6 +56,9 @@ public class AvgAggregationUnit extends AbstractAggregationUnit { @Override public Comparable<?> getResult() { if (null == count || BigDecimal.ZERO.equals(count)) { return (Comparable<?>) ResultSetUtil.convertValue(count, returnType); } // TODO 通过metadata获取数据库的浮点数精度值 return (Comparable<?>) ResultSetUtil.convertValue(sum.divide(count, 4, BigDecimal.ROUND_HALF_UP), returnType); } Loading