All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.astyanax.cql.direct.DirectCqlPreparedStatement Maven / Gradle / Ivy

package com.netflix.astyanax.cql.direct;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Session;
import com.google.common.util.concurrent.ListenableFuture;
import com.netflix.astyanax.Serializer;
import com.netflix.astyanax.connectionpool.OperationResult;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.cql.CqlOperationResultImpl;
import com.netflix.astyanax.cql.CqlPreparedStatement;
import com.netflix.astyanax.cql.CqlStatementResult;
import com.netflix.astyanax.cql.util.AsyncOperationResult;

/**
 * Impl of {@link CqlPreparedStatement} using java driver.
 * it manages a {@link Session} object that is used when actually performing the real query with the 
 * driver underneath. 
 * 
 * @author poberai
 */
public class DirectCqlPreparedStatement implements CqlPreparedStatement {

	private final Session session;
	private final PreparedStatement pStmt; 
	private final List bindValues = new ArrayList(); 
	
	public DirectCqlPreparedStatement(Session session, PreparedStatement pStmt) {
		this.session = session;
		this.pStmt = pStmt;
	}

	@Override
	public OperationResult execute() throws ConnectionException {
		
		BoundStatement bStmt = pStmt.bind(bindValues.toArray());
		ResultSet resultSet = session.execute(bStmt);
		
		CqlStatementResult result = new DirectCqlStatementResultImpl(resultSet);
		return new CqlOperationResultImpl(resultSet, result);
	}

	@Override
	public ListenableFuture> executeAsync() throws ConnectionException {

		BoundStatement bStmt = pStmt.bind(bindValues.toArray());
		ResultSetFuture rsFuture = session.executeAsync(bStmt);

		return new AsyncOperationResult(rsFuture) {

			@Override
			public OperationResult getOperationResult(ResultSet rs) {
				CqlStatementResult result = new DirectCqlStatementResultImpl(rs);
				return new CqlOperationResultImpl(rs, result);			}
		};
	}

	@Override
	public  CqlPreparedStatement withByteBufferValue(V value, Serializer serializer) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withValue(ByteBuffer value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withValues(List values) {
		bindValues.addAll(values);
		return this;
	}

	@Override
	public CqlPreparedStatement withStringValue(String value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withIntegerValue(Integer value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withBooleanValue(Boolean value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withDoubleValue(Double value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withLongValue(Long value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withFloatValue(Float value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withShortValue(Short value) {
		bindValues.add(value);
		return this;
	}

	@Override
	public CqlPreparedStatement withUUIDValue(UUID value) {
		bindValues.add(value);
		return this;
	}
	
	public PreparedStatement getInnerPreparedStatement() {
		return pStmt;
	}
}