prepared statements test
This commit is contained in:
parent
cbec10d5f0
commit
14d82ac475
16 changed files with 170 additions and 44 deletions
|
@ -23,9 +23,9 @@ import com.datastax.driver.core.CloseFuture;
|
|||
import com.datastax.driver.core.Session;
|
||||
import com.noorq.casser.core.operation.CountOperation;
|
||||
import com.noorq.casser.core.operation.DeleteOperation;
|
||||
import com.noorq.casser.core.operation.InsertOperation;
|
||||
import com.noorq.casser.core.operation.SelectOperation;
|
||||
import com.noorq.casser.core.operation.UpdateOperation;
|
||||
import com.noorq.casser.core.operation.InsertOperation;
|
||||
import com.noorq.casser.core.reflect.CasserPropertyNode;
|
||||
import com.noorq.casser.core.tuple.Tuple1;
|
||||
import com.noorq.casser.core.tuple.Tuple2;
|
||||
|
@ -35,7 +35,6 @@ import com.noorq.casser.core.tuple.Tuple5;
|
|||
import com.noorq.casser.core.tuple.Tuple6;
|
||||
import com.noorq.casser.core.tuple.Tuple7;
|
||||
import com.noorq.casser.mapping.CasserMappingEntity;
|
||||
import com.noorq.casser.mapping.CasserMappingProperty;
|
||||
import com.noorq.casser.mapping.CasserMappingRepository;
|
||||
import com.noorq.casser.mapping.MappingUtil;
|
||||
import com.noorq.casser.mapping.value.ColumnValuePreparer;
|
||||
|
@ -228,6 +227,10 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
|
|||
return new CountOperation(this, entity);
|
||||
}
|
||||
|
||||
public <V> UpdateOperation update() {
|
||||
return new UpdateOperation(this);
|
||||
}
|
||||
|
||||
public <V> UpdateOperation update(Getter<V> getter, V v) {
|
||||
Objects.requireNonNull(getter, "field is empty");
|
||||
Objects.requireNonNull(v, "value is empty");
|
||||
|
|
|
@ -18,6 +18,8 @@ package com.noorq.casser.core;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.datastax.driver.core.querybuilder.BindMarker;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.noorq.casser.mapping.OrderingDirection;
|
||||
|
||||
/**
|
||||
|
@ -30,6 +32,14 @@ public final class Query {
|
|||
private Query() {
|
||||
}
|
||||
|
||||
public static BindMarker marker() {
|
||||
return QueryBuilder.bindMarker();
|
||||
}
|
||||
|
||||
public static BindMarker marker(String name) {
|
||||
return QueryBuilder.bindMarker(name);
|
||||
}
|
||||
|
||||
public static Ordered asc(Getter<?> getter) {
|
||||
return new Ordered(getter, OrderingDirection.ASC);
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ import com.noorq.casser.core.Getter;
|
|||
import com.noorq.casser.core.Operator;
|
||||
import com.noorq.casser.core.Postulate;
|
||||
|
||||
public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperation<E, O>> extends AbstractEntityOperation<E, O> {
|
||||
public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperation<E, O>> extends AbstractOperation<E, O> {
|
||||
|
||||
protected List<Filter<?>> filters = null;
|
||||
|
||||
|
@ -61,7 +61,7 @@ public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperati
|
|||
return (O) this;
|
||||
}
|
||||
|
||||
public <V> O add(Getter<V> getter, Postulate<V> postulate) {
|
||||
public <V> O and(Getter<V> getter, Postulate<V> postulate) {
|
||||
|
||||
addFilter(Filter.create(getter, postulate));
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterS
|
|||
return (O) this;
|
||||
}
|
||||
|
||||
public <V> O add(Getter<V> getter, Postulate<V> postulate) {
|
||||
public <V> O and(Getter<V> getter, Postulate<V> postulate) {
|
||||
|
||||
addFilter(Filter.create(getter, postulate));
|
||||
|
||||
|
|
|
@ -18,19 +18,16 @@ package com.noorq.casser.core.operation;
|
|||
import com.datastax.driver.core.PreparedStatement;
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
import com.datastax.driver.core.ResultSetFuture;
|
||||
import com.datastax.driver.core.querybuilder.BuiltStatement;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.noorq.casser.core.AbstractSessionOperations;
|
||||
|
||||
public abstract class AbstractEntityOperation<E, O extends AbstractEntityOperation<E, O>> extends AbstractStatementOperation<E, O> {
|
||||
public abstract class AbstractOperation<E, O extends AbstractOperation<E, O>> extends AbstractStatementOperation<E, O> {
|
||||
|
||||
public abstract BuiltStatement buildStatement();
|
||||
|
||||
public abstract E transform(ResultSet resultSet);
|
||||
|
||||
public AbstractEntityOperation(AbstractSessionOperations sessionOperations) {
|
||||
public AbstractOperation(AbstractSessionOperations sessionOperations) {
|
||||
super(sessionOperations);
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ package com.noorq.casser.core.operation;
|
|||
|
||||
import com.datastax.driver.core.PreparedStatement;
|
||||
import com.datastax.driver.core.RegularStatement;
|
||||
import com.datastax.driver.core.Statement;
|
||||
import com.datastax.driver.core.querybuilder.BuiltStatement;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.noorq.casser.core.AbstractSessionOperations;
|
||||
|
@ -26,25 +27,34 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
|
|||
|
||||
protected final AbstractSessionOperations sessionOps;
|
||||
|
||||
public abstract BuiltStatement buildStatement();
|
||||
public abstract Statement buildStatement();
|
||||
|
||||
public AbstractStatementOperation(AbstractSessionOperations sessionOperations) {
|
||||
this.sessionOps = sessionOperations;
|
||||
}
|
||||
|
||||
public String cql() {
|
||||
return buildStatement().setForceNoValues(true).getQueryString();
|
||||
Statement statement = buildStatement();
|
||||
if (statement instanceof BuiltStatement) {
|
||||
BuiltStatement buildStatement = (BuiltStatement) statement;
|
||||
return buildStatement.setForceNoValues(true).getQueryString();
|
||||
}
|
||||
else {
|
||||
return statement.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public PreparedStatement prepareStatement() {
|
||||
|
||||
BuiltStatement builtStatement = buildStatement();
|
||||
Statement statement = buildStatement();
|
||||
|
||||
if (builtStatement instanceof RegularStatement) {
|
||||
if (statement instanceof RegularStatement) {
|
||||
|
||||
RegularStatement statement = (RegularStatement) builtStatement;
|
||||
RegularStatement regularStatement = (RegularStatement) statement;
|
||||
|
||||
return sessionOps.prepare(statement);
|
||||
|
||||
|
||||
return sessionOps.prepare(regularStatement);
|
||||
}
|
||||
|
||||
throw new CasserException("only RegularStatements can be prepared");
|
||||
|
@ -52,24 +62,13 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
|
|||
|
||||
public ListenableFuture<PreparedStatement> prepareStatementAsync() {
|
||||
|
||||
BuiltStatement builtStatement = buildStatement();
|
||||
Statement statement = buildStatement();
|
||||
|
||||
if (builtStatement instanceof RegularStatement) {
|
||||
if (statement instanceof RegularStatement) {
|
||||
|
||||
RegularStatement statement = (RegularStatement) builtStatement;
|
||||
RegularStatement regularStatement = (RegularStatement) statement;
|
||||
|
||||
return sessionOps.prepareAsync(statement);
|
||||
|
||||
/*
|
||||
return Futures.transform(preparedStatementFuture, new Function<PreparedStatement, Prepared<O>>() {
|
||||
|
||||
@Override
|
||||
public Prepared<O> apply(PreparedStatement preparedStatement) {
|
||||
return new Prepared<O>(preparedStatement, _this);
|
||||
}
|
||||
|
||||
});
|
||||
*/
|
||||
return sessionOps.prepareAsync(regularStatement);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -17,14 +17,14 @@ package com.noorq.casser.core.operation;
|
|||
|
||||
import com.datastax.driver.core.BoundStatement;
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
import com.datastax.driver.core.querybuilder.BuiltStatement;
|
||||
import com.datastax.driver.core.Statement;
|
||||
|
||||
public final class BoundOperation<E> extends AbstractEntityOperation<E, BoundOperation<E>> {
|
||||
public final class BoundOperation<E> extends AbstractOperation<E, BoundOperation<E>> {
|
||||
|
||||
private final BoundStatement boundStatement;
|
||||
private final AbstractEntityOperation<E, ?> delegate;
|
||||
private final AbstractOperation<E, ?> delegate;
|
||||
|
||||
public BoundOperation(BoundStatement boundStatement, AbstractEntityOperation<E, ?> operation) {
|
||||
public BoundOperation(BoundStatement boundStatement, AbstractOperation<E, ?> operation) {
|
||||
super(operation.sessionOps);
|
||||
this.boundStatement = boundStatement;
|
||||
this.delegate = operation;
|
||||
|
@ -36,8 +36,8 @@ public final class BoundOperation<E> extends AbstractEntityOperation<E, BoundOpe
|
|||
}
|
||||
|
||||
@Override
|
||||
public BuiltStatement buildStatement() {
|
||||
return null;
|
||||
public Statement buildStatement() {
|
||||
return boundStatement;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ import java.util.stream.Stream;
|
|||
|
||||
import com.datastax.driver.core.BoundStatement;
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
import com.datastax.driver.core.querybuilder.BuiltStatement;
|
||||
import com.datastax.driver.core.Statement;
|
||||
|
||||
public final class BoundStreamOperation<E> extends AbstractStreamOperation<E, BoundStreamOperation<E>> {
|
||||
|
||||
|
@ -38,8 +38,8 @@ public final class BoundStreamOperation<E> extends AbstractStreamOperation<E, Bo
|
|||
}
|
||||
|
||||
@Override
|
||||
public BuiltStatement buildStatement() {
|
||||
return null;
|
||||
public Statement buildStatement() {
|
||||
return boundStatement;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ import com.noorq.casser.mapping.MappingUtil;
|
|||
import com.noorq.casser.mapping.value.BeanColumnValueProvider;
|
||||
import com.noorq.casser.support.CasserMappingException;
|
||||
|
||||
public final class InsertOperation extends AbstractEntityOperation<ResultSet, InsertOperation> {
|
||||
public final class InsertOperation extends AbstractOperation<ResultSet, InsertOperation> {
|
||||
|
||||
private final List<Tuple2<CasserPropertyNode, Object>> values = new ArrayList<Tuple2<CasserPropertyNode, Object>>();
|
||||
private final boolean ifNotExists;
|
||||
|
|
|
@ -21,18 +21,27 @@ import com.datastax.driver.core.PreparedStatement;
|
|||
public final class PreparedOperation<E> {
|
||||
|
||||
private final PreparedStatement preparedStatement;
|
||||
private final AbstractEntityOperation<E, ?> operation;
|
||||
private final AbstractOperation<E, ?> operation;
|
||||
|
||||
public PreparedOperation(PreparedStatement statement, AbstractEntityOperation<E, ?> operation) {
|
||||
public PreparedOperation(PreparedStatement statement, AbstractOperation<E, ?> operation) {
|
||||
this.preparedStatement = statement;
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
|
||||
public PreparedStatement getPreparedStatement() {
|
||||
return preparedStatement;
|
||||
}
|
||||
|
||||
public BoundOperation<E> bind(Object... params) {
|
||||
|
||||
BoundStatement boundStatement = preparedStatement.bind(params);
|
||||
|
||||
return new BoundOperation<E>(boundStatement, operation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return preparedStatement.getQueryString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,6 +28,10 @@ public final class PreparedStreamOperation<E> {
|
|||
this.operation = operation;
|
||||
}
|
||||
|
||||
public PreparedStatement getPreparedStatement() {
|
||||
return preparedStatement;
|
||||
}
|
||||
|
||||
public BoundStreamOperation<E> bind(Object... params) {
|
||||
|
||||
BoundStatement boundStatement = preparedStatement.bind(params);
|
||||
|
@ -35,4 +39,10 @@ public final class PreparedStreamOperation<E> {
|
|||
return new BoundStreamOperation<E>(boundStatement, operation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return preparedStatement.getQueryString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -35,6 +35,13 @@ public final class UpdateOperation extends AbstractFilterOperation<ResultSet, Up
|
|||
|
||||
private final CasserPropertyNode[] props;
|
||||
private final Object[] vals;
|
||||
|
||||
public UpdateOperation(AbstractSessionOperations sessionOperations) {
|
||||
super(sessionOperations);
|
||||
|
||||
this.props = new CasserPropertyNode[0];
|
||||
this.vals = new Object[0];
|
||||
}
|
||||
|
||||
public UpdateOperation(AbstractSessionOperations sessionOperations, CasserPropertyNode p, Object v) {
|
||||
super(sessionOperations);
|
||||
|
|
|
@ -18,6 +18,7 @@ package com.noorq.casser.mapping.value;
|
|||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.datastax.driver.core.querybuilder.BindMarker;
|
||||
import com.noorq.casser.mapping.CasserMappingProperty;
|
||||
import com.noorq.casser.mapping.CasserMappingRepository;
|
||||
|
||||
|
@ -35,6 +36,10 @@ public final class StatementColumnValuePreparer implements ColumnValuePreparer {
|
|||
|
||||
if (value != null) {
|
||||
|
||||
if (value instanceof BindMarker) {
|
||||
return value;
|
||||
}
|
||||
|
||||
Optional<Function<Object, Object>> converter = prop.getWriteConverter(repository);
|
||||
|
||||
if (converter.isPresent()) {
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.util.function.Function;
|
|||
import com.datastax.driver.core.DataType;
|
||||
import com.datastax.driver.core.ProtocolVersion;
|
||||
import com.datastax.driver.core.UserType;
|
||||
import com.datastax.driver.core.querybuilder.BindMarker;
|
||||
import com.noorq.casser.mapping.CasserMappingProperty;
|
||||
import com.noorq.casser.mapping.CasserMappingRepository;
|
||||
|
||||
|
@ -39,6 +40,10 @@ public final class UDTColumnValuePreparer implements ColumnValuePreparer {
|
|||
|
||||
if (value != null) {
|
||||
|
||||
if (value instanceof BindMarker) {
|
||||
return value;
|
||||
}
|
||||
|
||||
Optional<Function<Object, Object>> converter = prop.getWriteConverter(repository);
|
||||
|
||||
if (converter.isPresent()) {
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.noorq.casser.test.integration.core.prepared;
|
||||
|
||||
import com.noorq.casser.mapping.Column;
|
||||
import com.noorq.casser.mapping.PartitionKey;
|
||||
import com.noorq.casser.mapping.Table;
|
||||
|
||||
@Table("cars")
|
||||
public interface Car {
|
||||
|
||||
@PartitionKey(ordinal=1)
|
||||
String make();
|
||||
|
||||
@PartitionKey(ordinal=2)
|
||||
String model();
|
||||
|
||||
@Column
|
||||
int year();
|
||||
|
||||
@Column
|
||||
double price();
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.noorq.casser.test.integration.core.prepared;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.datastax.driver.core.ResultSet;
|
||||
import com.datastax.driver.core.querybuilder.BindMarker;
|
||||
import com.noorq.casser.core.Casser;
|
||||
|
||||
import static com.noorq.casser.core.Query.*;
|
||||
|
||||
import com.noorq.casser.core.CasserSession;
|
||||
import com.noorq.casser.core.operation.PreparedOperation;
|
||||
import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest;
|
||||
|
||||
public class PreparedStatementTest extends AbstractEmbeddedCassandraTest {
|
||||
|
||||
Car car;
|
||||
|
||||
CasserSession session;
|
||||
|
||||
PreparedOperation<ResultSet> insertOp;
|
||||
|
||||
PreparedOperation<ResultSet> updateOp;
|
||||
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
|
||||
car = Casser.dsl(Car.class);
|
||||
|
||||
session = Casser.init(getSession()).showCql().add(Car.class).autoCreateDrop().get();
|
||||
|
||||
insertOp = session.insert()
|
||||
.value(car::make, marker())
|
||||
.value(car::model, marker())
|
||||
.value(car::year, 2004)
|
||||
.prepare();
|
||||
|
||||
updateOp = session.update()
|
||||
.set(car::price, marker())
|
||||
.where(car::make, eq(marker()))
|
||||
.and(car::model, eq(marker()))
|
||||
.prepare();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
insertOp.bind("Nissan", "350Z").sync();
|
||||
|
||||
updateOp.bind(Double.valueOf(10000.0), "Nissan", "350Z").sync();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue