diff --git a/src/main/java/casser/core/AbstractSessionOperations.java b/src/main/java/casser/core/AbstractSessionOperations.java index f0f1b35..2dcd577 100644 --- a/src/main/java/casser/core/AbstractSessionOperations.java +++ b/src/main/java/casser/core/AbstractSessionOperations.java @@ -20,8 +20,8 @@ import java.util.concurrent.Executor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import casser.mapping.CasserMappingRepository; import casser.mapping.ColumnValuePreparer; +import casser.mapping.ColumnValueProvider; import casser.support.CasserException; import com.datastax.driver.core.RegularStatement; @@ -43,7 +43,7 @@ public abstract class AbstractSessionOperations { abstract public Executor getExecutor(); - abstract public CasserMappingRepository getRepository(); + abstract public ColumnValueProvider getValueProvider(); abstract public ColumnValuePreparer getValuePreparer(); diff --git a/src/main/java/casser/core/CasserSession.java b/src/main/java/casser/core/CasserSession.java index a650024..5aff656 100644 --- a/src/main/java/casser/core/CasserSession.java +++ b/src/main/java/casser/core/CasserSession.java @@ -37,7 +37,9 @@ import casser.mapping.CasserMappingEntity; import casser.mapping.CasserMappingProperty; import casser.mapping.CasserMappingRepository; import casser.mapping.ColumnValuePreparer; +import casser.mapping.ColumnValueProvider; import casser.mapping.MappingUtil; +import casser.mapping.RowColumnValueProvider; import casser.mapping.StatementColumnValuePreparer; import com.datastax.driver.core.CloseFuture; @@ -52,6 +54,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl private final Executor executor; private final boolean dropSchemaOnClose; + private final RowColumnValueProvider valueProvider; private final StatementColumnValuePreparer valuePreparer; CasserSession(Session session, @@ -67,6 +70,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl this.executor = executor; this.dropSchemaOnClose = dropSchemaOnClose; + this.valueProvider = new RowColumnValueProvider(this.mappingRepository); this.valuePreparer = new StatementColumnValuePreparer(this.mappingRepository); } @@ -101,8 +105,8 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl } @Override - public CasserMappingRepository getRepository() { - return mappingRepository; + public ColumnValueProvider getValueProvider() { + return valueProvider; } @Override @@ -114,7 +118,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl Objects.requireNonNull(getter1, "field 1 is empty"); CasserMappingProperty p1 = MappingUtil.resolveMappingProperty(getter1); - return new SelectOperation>(this, new Tuple1.Mapper(p1), p1); + return new SelectOperation>(this, new Tuple1.Mapper(getValueProvider(), p1), p1); } public SelectOperation> select(Getter getter1, Getter getter2) { @@ -123,7 +127,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl CasserMappingProperty p1 = MappingUtil.resolveMappingProperty(getter1); CasserMappingProperty p2 = MappingUtil.resolveMappingProperty(getter2); - return new SelectOperation>(this, new Tuple2.Mapper(p1, p2), p1, p2); + return new SelectOperation>(this, new Tuple2.Mapper(getValueProvider(), p1, p2), p1, p2); } public SelectOperation> select(Getter getter1, Getter getter2, Getter getter3) { @@ -134,7 +138,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl CasserMappingProperty p1 = MappingUtil.resolveMappingProperty(getter1); CasserMappingProperty p2 = MappingUtil.resolveMappingProperty(getter2); CasserMappingProperty p3 = MappingUtil.resolveMappingProperty(getter3); - return new SelectOperation>(this, new Tuple3.Mapper(p1, p2, p3), p1, p2, p3); + return new SelectOperation>(this, new Tuple3.Mapper(getValueProvider(), p1, p2, p3), p1, p2, p3); } public SelectOperation> select( @@ -148,7 +152,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl CasserMappingProperty p2 = MappingUtil.resolveMappingProperty(getter2); CasserMappingProperty p3 = MappingUtil.resolveMappingProperty(getter3); CasserMappingProperty p4 = MappingUtil.resolveMappingProperty(getter4); - return new SelectOperation>(this, new Tuple4.Mapper(p1, p2, p3, p4), p1, p2, p3, p4); + return new SelectOperation>(this, new Tuple4.Mapper(getValueProvider(), p1, p2, p3, p4), p1, p2, p3, p4); } public SelectOperation> select( @@ -165,7 +169,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl CasserMappingProperty p4 = MappingUtil.resolveMappingProperty(getter4); CasserMappingProperty p5 = MappingUtil.resolveMappingProperty(getter5); return new SelectOperation>(this, - new Tuple5.Mapper(p1, p2, p3, p4, p5), + new Tuple5.Mapper(getValueProvider(), p1, p2, p3, p4, p5), p1, p2, p3, p4, p5); } @@ -186,7 +190,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl CasserMappingProperty p5 = MappingUtil.resolveMappingProperty(getter5); CasserMappingProperty p6 = MappingUtil.resolveMappingProperty(getter6); return new SelectOperation>(this, - new Tuple6.Mapper(p1, p2, p3, p4, p5, p6), + new Tuple6.Mapper(getValueProvider(), p1, p2, p3, p4, p5, p6), p1, p2, p3, p4, p5, p6); } @@ -210,7 +214,8 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl CasserMappingProperty p6 = MappingUtil.resolveMappingProperty(getter6); CasserMappingProperty p7 = MappingUtil.resolveMappingProperty(getter7); return new SelectOperation>(this, - new Tuple7.Mapper(p1, p2, p3, p4, p5, p6, p7), + new Tuple7.Mapper(getValueProvider(), + p1, p2, p3, p4, p5, p6, p7), p1, p2, p3, p4, p5, p6, p7); } diff --git a/src/main/java/casser/core/SessionInitializer.java b/src/main/java/casser/core/SessionInitializer.java index 32a0062..b6a0449 100644 --- a/src/main/java/casser/core/SessionInitializer.java +++ b/src/main/java/casser/core/SessionInitializer.java @@ -25,6 +25,8 @@ import casser.mapping.CasserEntityType; import casser.mapping.CasserMappingEntity; import casser.mapping.CasserMappingRepository; import casser.mapping.ColumnValuePreparer; +import casser.mapping.ColumnValueProvider; +import casser.mapping.RowColumnValueProvider; import casser.mapping.StatementColumnValuePreparer; import com.datastax.driver.core.KeyspaceMetadata; @@ -71,8 +73,8 @@ public class SessionInitializer extends AbstractSessionOperations { } @Override - public CasserMappingRepository getRepository() { - return mappingRepository; + public ColumnValueProvider getValueProvider() { + return new RowColumnValueProvider(mappingRepository); } @Override diff --git a/src/main/java/casser/core/operation/SelectOperation.java b/src/main/java/casser/core/operation/SelectOperation.java index b5883fc..8bdbd98 100644 --- a/src/main/java/casser/core/operation/SelectOperation.java +++ b/src/main/java/casser/core/operation/SelectOperation.java @@ -29,14 +29,12 @@ import casser.core.Filter; import casser.core.dsl.Getter; import casser.mapping.CasserMappingEntity; import casser.mapping.CasserMappingProperty; -import casser.mapping.CasserMappingRepository; -import casser.mapping.ColumnValueProvider; import casser.mapping.MappingUtil; import casser.mapping.OrderingDirection; -import casser.mapping.RowColumnValueProvider; import casser.support.CasserMappingException; import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; import com.datastax.driver.core.querybuilder.BuiltStatement; import com.datastax.driver.core.querybuilder.Ordering; import com.datastax.driver.core.querybuilder.QueryBuilder; @@ -47,13 +45,13 @@ import com.datastax.driver.core.querybuilder.Select.Where; public final class SelectOperation extends AbstractFilterStreamOperation> { - protected final Function rowMapper; + protected final Function rowMapper; protected final CasserMappingProperty[] props; protected List ordering = null; protected Integer limit = null; - public SelectOperation(AbstractSessionOperations sessionOperations, Function rowMapper, CasserMappingProperty... props) { + public SelectOperation(AbstractSessionOperations sessionOperations, Function rowMapper, CasserMappingProperty... props) { super(sessionOperations); this.rowMapper = rowMapper; this.props = props; @@ -160,12 +158,10 @@ public final class SelectOperation extends AbstractFilterStreamOperation transform(ResultSet resultSet) { - - CasserMappingRepository repository = sessionOps.getRepository(); return StreamSupport.stream( Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED) - , false).map(r -> new RowColumnValueProvider(repository, r)).map(rowMapper); + , false).map(rowMapper); } diff --git a/src/main/java/casser/core/tuple/Tuple1.java b/src/main/java/casser/core/tuple/Tuple1.java index eb80a4f..bda55f5 100644 --- a/src/main/java/casser/core/tuple/Tuple1.java +++ b/src/main/java/casser/core/tuple/Tuple1.java @@ -20,6 +20,8 @@ import java.util.function.Function; import casser.mapping.CasserMappingProperty; import casser.mapping.ColumnValueProvider; +import com.datastax.driver.core.Row; + public final class Tuple1 { public final V1 v1; @@ -28,17 +30,19 @@ public final class Tuple1 { this.v1 = v1; } - public final static class Mapper implements Function> { + public final static class Mapper implements Function> { + private final ColumnValueProvider provider; private final CasserMappingProperty p1; - public Mapper(CasserMappingProperty p1) { + public Mapper(ColumnValueProvider provider, CasserMappingProperty p1) { + this.provider = provider; this.p1 = p1; } @Override - public Tuple1 apply(ColumnValueProvider provider) { - return new Tuple1(provider.getColumnValue(0, p1)); + public Tuple1 apply(Row row) { + return new Tuple1(provider.getColumnValue(row, 0, p1)); } } diff --git a/src/main/java/casser/core/tuple/Tuple2.java b/src/main/java/casser/core/tuple/Tuple2.java index bf39233..eb92dba 100644 --- a/src/main/java/casser/core/tuple/Tuple2.java +++ b/src/main/java/casser/core/tuple/Tuple2.java @@ -20,6 +20,8 @@ import java.util.function.Function; import casser.mapping.CasserMappingProperty; import casser.mapping.ColumnValueProvider; +import com.datastax.driver.core.Row; + public final class Tuple2 { public final V1 v1; @@ -30,19 +32,23 @@ public final class Tuple2 { this.v2 = v2; } - public final static class Mapper implements Function> { + public final static class Mapper implements Function> { + private final ColumnValueProvider provider; private final CasserMappingProperty p1; private final CasserMappingProperty p2; - public Mapper(CasserMappingProperty p1, CasserMappingProperty p2) { + public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2) { + this.provider = provider; this.p1 = p1; this.p2 = p2; } @Override - public Tuple2 apply(ColumnValueProvider provider) { - return new Tuple2(provider.getColumnValue(0, p1), provider.getColumnValue(1, p2)); + public Tuple2 apply(Row row) { + return new Tuple2( + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2)); } } diff --git a/src/main/java/casser/core/tuple/Tuple3.java b/src/main/java/casser/core/tuple/Tuple3.java index 9aad6a8..90138e1 100644 --- a/src/main/java/casser/core/tuple/Tuple3.java +++ b/src/main/java/casser/core/tuple/Tuple3.java @@ -20,6 +20,8 @@ import java.util.function.Function; import casser.mapping.CasserMappingProperty; import casser.mapping.ColumnValueProvider; +import com.datastax.driver.core.Row; + public final class Tuple3 { public final V1 v1; @@ -32,24 +34,26 @@ public final class Tuple3 { this.v3 = v3; } - public final static class Mapper implements Function> { + public final static class Mapper implements Function> { + private final ColumnValueProvider provider; private final CasserMappingProperty p1; private final CasserMappingProperty p2; private final CasserMappingProperty p3; - public Mapper(CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3) { + public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3) { + this.provider = provider; this.p1 = p1; this.p2 = p2; this.p3 = p3; } @Override - public Tuple3 apply(ColumnValueProvider provider) { + public Tuple3 apply(Row row) { return new Tuple3( - provider.getColumnValue(0, p1), - provider.getColumnValue(1, p2), - provider.getColumnValue(2, p3) + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3) ); } } diff --git a/src/main/java/casser/core/tuple/Tuple4.java b/src/main/java/casser/core/tuple/Tuple4.java index 77e4e93..967e593 100644 --- a/src/main/java/casser/core/tuple/Tuple4.java +++ b/src/main/java/casser/core/tuple/Tuple4.java @@ -20,6 +20,8 @@ import java.util.function.Function; import casser.mapping.CasserMappingProperty; import casser.mapping.ColumnValueProvider; +import com.datastax.driver.core.Row; + public final class Tuple4 { public final V1 v1; @@ -34,19 +36,21 @@ public final class Tuple4 { this.v4 = v4; } - public final static class Mapper implements Function> { + public final static class Mapper implements Function> { + private final ColumnValueProvider provider; private final CasserMappingProperty p1; private final CasserMappingProperty p2; private final CasserMappingProperty p3; private final CasserMappingProperty p4; - public Mapper( + public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3, CasserMappingProperty p4 ) { + this.provider = provider; this.p1 = p1; this.p2 = p2; this.p3 = p3; @@ -54,12 +58,12 @@ public final class Tuple4 { } @Override - public Tuple4 apply(ColumnValueProvider provider) { + public Tuple4 apply(Row row) { return new Tuple4( - provider.getColumnValue(0, p1), - provider.getColumnValue(1, p2), - provider.getColumnValue(2, p3), - provider.getColumnValue(3, p4) + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4) ); } } diff --git a/src/main/java/casser/core/tuple/Tuple5.java b/src/main/java/casser/core/tuple/Tuple5.java index d3c1499..ca8af83 100644 --- a/src/main/java/casser/core/tuple/Tuple5.java +++ b/src/main/java/casser/core/tuple/Tuple5.java @@ -20,6 +20,8 @@ import java.util.function.Function; import casser.mapping.CasserMappingProperty; import casser.mapping.ColumnValueProvider; +import com.datastax.driver.core.Row; + public final class Tuple5 { public final V1 v1; @@ -36,17 +38,19 @@ public final class Tuple5 { this.v5 = v5; } - public final static class Mapper implements Function> { + public final static class Mapper implements Function> { + private final ColumnValueProvider provider; private final CasserMappingProperty p1, p2, p3, p4, p5; - public Mapper( + public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3, CasserMappingProperty p4, CasserMappingProperty p5 ) { + this.provider = provider; this.p1 = p1; this.p2 = p2; this.p3 = p3; @@ -55,13 +59,13 @@ public final class Tuple5 { } @Override - public Tuple5 apply(ColumnValueProvider provider) { + public Tuple5 apply(Row row) { return new Tuple5( - provider.getColumnValue(0, p1), - provider.getColumnValue(1, p2), - provider.getColumnValue(2, p3), - provider.getColumnValue(3, p4), - provider.getColumnValue(4, p5) + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4), + provider.getColumnValue(row, 4, p5) ); } } diff --git a/src/main/java/casser/core/tuple/Tuple6.java b/src/main/java/casser/core/tuple/Tuple6.java index 4049fc9..151ac08 100644 --- a/src/main/java/casser/core/tuple/Tuple6.java +++ b/src/main/java/casser/core/tuple/Tuple6.java @@ -20,6 +20,8 @@ import java.util.function.Function; import casser.mapping.CasserMappingProperty; import casser.mapping.ColumnValueProvider; +import com.datastax.driver.core.Row; + public final class Tuple6 { public final V1 v1; @@ -39,12 +41,13 @@ public final class Tuple6 { } public final static class Mapper implements - Function> { + private final ColumnValueProvider provider; private final CasserMappingProperty p1, p2, p3, p4, p5, p6; - public Mapper( + public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3, @@ -52,6 +55,7 @@ public final class Tuple6 { CasserMappingProperty p5, CasserMappingProperty p6 ) { + this.provider = provider; this.p1 = p1; this.p2 = p2; this.p3 = p3; @@ -61,14 +65,14 @@ public final class Tuple6 { } @Override - public Tuple6 apply(ColumnValueProvider provider) { + public Tuple6 apply(Row row) { return new Tuple6( - provider.getColumnValue(0, p1), - provider.getColumnValue(1, p2), - provider.getColumnValue(2, p3), - provider.getColumnValue(3, p4), - provider.getColumnValue(4, p5), - provider.getColumnValue(5, p6) + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4), + provider.getColumnValue(row, 4, p5), + provider.getColumnValue(row, 5, p6) ); } } diff --git a/src/main/java/casser/core/tuple/Tuple7.java b/src/main/java/casser/core/tuple/Tuple7.java index dba8b56..ea67b32 100644 --- a/src/main/java/casser/core/tuple/Tuple7.java +++ b/src/main/java/casser/core/tuple/Tuple7.java @@ -20,6 +20,8 @@ import java.util.function.Function; import casser.mapping.CasserMappingProperty; import casser.mapping.ColumnValueProvider; +import com.datastax.driver.core.Row; + public final class Tuple7 { public final V1 v1; @@ -41,12 +43,13 @@ public final class Tuple7 { } public final static class Mapper implements - Function> { + private final ColumnValueProvider provider; private final CasserMappingProperty p1, p2, p3, p4, p5, p6, p7; - public Mapper( + public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3, @@ -55,6 +58,7 @@ public final class Tuple7 { CasserMappingProperty p6, CasserMappingProperty p7 ) { + this.provider = provider; this.p1 = p1; this.p2 = p2; this.p3 = p3; @@ -65,15 +69,15 @@ public final class Tuple7 { } @Override - public Tuple7 apply(ColumnValueProvider provider) { + public Tuple7 apply(Row row) { return new Tuple7( - provider.getColumnValue(0, p1), - provider.getColumnValue(1, p2), - provider.getColumnValue(2, p3), - provider.getColumnValue(3, p4), - provider.getColumnValue(4, p5), - provider.getColumnValue(5, p6), - provider.getColumnValue(6, p7) + provider.getColumnValue(row, 0, p1), + provider.getColumnValue(row, 1, p2), + provider.getColumnValue(row, 2, p3), + provider.getColumnValue(row, 3, p4), + provider.getColumnValue(row, 4, p5), + provider.getColumnValue(row, 5, p6), + provider.getColumnValue(row, 6, p7) ); } } diff --git a/src/main/java/casser/mapping/ColumnValueProvider.java b/src/main/java/casser/mapping/ColumnValueProvider.java index 0f767e2..df603d1 100644 --- a/src/main/java/casser/mapping/ColumnValueProvider.java +++ b/src/main/java/casser/mapping/ColumnValueProvider.java @@ -15,8 +15,10 @@ */ package casser.mapping; +import com.datastax.driver.core.Row; + public interface ColumnValueProvider { - V getColumnValue(int columnIndex, CasserMappingProperty property); + V getColumnValue(Row row, int columnIndex, CasserMappingProperty property); } diff --git a/src/main/java/casser/mapping/RowColumnValueProvider.java b/src/main/java/casser/mapping/RowColumnValueProvider.java index 29f235b..e0e263f 100644 --- a/src/main/java/casser/mapping/RowColumnValueProvider.java +++ b/src/main/java/casser/mapping/RowColumnValueProvider.java @@ -30,22 +30,20 @@ import com.datastax.driver.core.Row; public final class RowColumnValueProvider implements ColumnValueProvider { private final CasserMappingRepository repository; - private final Row source; - private final ColumnDefinitions columnDefinitions; - public RowColumnValueProvider(CasserMappingRepository repository, Row source) { + public RowColumnValueProvider(CasserMappingRepository repository) { this.repository = repository; - this.source = source; - this.columnDefinitions = source.getColumnDefinitions(); } @Override - public V getColumnValue(int columnIndex, CasserMappingProperty property) { + public V getColumnValue(Row source, int columnIndex, CasserMappingProperty property) { if (source.isNull(columnIndex)) { return null; } + ColumnDefinitions columnDefinitions = source.getColumnDefinitions(); + DataType columnType = columnDefinitions.getType(columnIndex); if (columnType.isCollection()) {