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

com.netflix.astyanax.cql.reads.DirectCqlQueryImpl Maven / Gradle / Ivy

package com.netflix.astyanax.cql.reads;

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

import org.apache.commons.lang.NotImplementedException;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;
import com.google.common.util.concurrent.ListenableFuture;
import com.netflix.astyanax.CassandraOperationType;
import com.netflix.astyanax.Serializer;
import com.netflix.astyanax.connectionpool.OperationResult;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.cql.CqlAbstractExecutionImpl;
import com.netflix.astyanax.cql.CqlKeyspaceImpl.KeyspaceContext;
import com.netflix.astyanax.cql.reads.model.DirectCqlResult;
import com.netflix.astyanax.cql.util.CFQueryContext;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.CqlResult;
import com.netflix.astyanax.query.CqlQuery;
import com.netflix.astyanax.query.PreparedCqlQuery;

/**
 * 
 * Impl for {@link CqlQuery} that allows users to directly send CQL3 over java driver
 * @author poberai
 *
 * @param 
 * @param 
 */
public class DirectCqlQueryImpl implements CqlQuery {

	private final KeyspaceContext ksContext;
	private final CFQueryContext cfContext;
	private final String basicCqlQuery; 
	
	public DirectCqlQueryImpl(KeyspaceContext ksCtx, CFQueryContext cfCtx, String basicCqlQuery) {
		this.ksContext = ksCtx;
		this.cfContext = cfCtx;
		this.basicCqlQuery = basicCqlQuery;
	}
	
	@Override
	public OperationResult> execute() throws ConnectionException {
		return new InternalExecutionImpl(new SimpleStatement(basicCqlQuery)).execute();
	}

	@Override
	public ListenableFuture>> executeAsync() throws ConnectionException {
		return new InternalExecutionImpl(new SimpleStatement(basicCqlQuery)).executeAsync();
	}
	
	@Override
	public CqlQuery useCompression() {
		throw new UnsupportedOperationException("Operation not supported");
	}

	protected class InternalPreparedStatement implements PreparedCqlQuery {
		
		private final PreparedStatement pStatement;
		
		protected InternalPreparedStatement() {
			pStatement = ksContext.getSession().prepare(basicCqlQuery);
		}
		
		@Override
		public  PreparedCqlQuery withByteBufferValue(V value, Serializer serializer) {
			return new InternalBoundStatement(pStatement).withByteBufferValue(value, serializer);
		}

		@Override
		public PreparedCqlQuery withValue(ByteBuffer value) {
			return new InternalBoundStatement(pStatement).withValue(value);
		}

		@Override
		public PreparedCqlQuery withValues(List values) {
			return new InternalBoundStatement(pStatement).withValues(values);
		}

		@Override
		public PreparedCqlQuery withStringValue(String value) {
			return new InternalBoundStatement(pStatement).withStringValue(value);
		}

		@Override
		public PreparedCqlQuery withIntegerValue(Integer value) {
			return new InternalBoundStatement(pStatement).withIntegerValue(value);
		}

		@Override
		public PreparedCqlQuery withBooleanValue(Boolean value) {
			return new InternalBoundStatement(pStatement).withBooleanValue(value);
		}

		@Override
		public PreparedCqlQuery withDoubleValue(Double value) {
			return new InternalBoundStatement(pStatement).withDoubleValue(value);
		}

		@Override
		public PreparedCqlQuery withLongValue(Long value) {
			return new InternalBoundStatement(pStatement).withLongValue(value);
		}

		@Override
		public PreparedCqlQuery withFloatValue(Float value) {
			return new InternalBoundStatement(pStatement).withFloatValue(value);
		}

		@Override
		public PreparedCqlQuery withShortValue(Short value) {
			return new InternalBoundStatement(pStatement).withShortValue(value);
		}

		@Override
		public PreparedCqlQuery withUUIDValue(UUID value) {
			return new InternalBoundStatement(pStatement).withUUIDValue(value);
		}

		@Override
		public OperationResult> execute() throws ConnectionException {
			throw new NotImplementedException();
		}

		@Override
		public ListenableFuture>> executeAsync() throws ConnectionException {
			throw new NotImplementedException();
		}
	}
	
	protected class InternalBoundStatement implements PreparedCqlQuery {
		
		final List bindList = new ArrayList();
		final BoundStatement boundStatement;
		
		protected InternalBoundStatement(PreparedStatement pStmt) {
			 boundStatement = new BoundStatement(pStmt);
		}
		
		@Override
		public OperationResult> execute() throws ConnectionException {
			boundStatement.bind(bindList.toArray());
			return new InternalExecutionImpl(boundStatement).execute();
		}

		@Override
		public ListenableFuture>> executeAsync() throws ConnectionException {
			boundStatement.bind(bindList.toArray());
			return new InternalExecutionImpl(boundStatement).executeAsync();
		}

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

		@Override
		public PreparedCqlQuery withValue(ByteBuffer value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withValues(List value) {
			bindList.addAll(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withStringValue(String value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withIntegerValue(Integer value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withBooleanValue(Boolean value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withDoubleValue(Double value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withLongValue(Long value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withFloatValue(Float value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withShortValue(Short value) {
			bindList.add(value);
			return this;
		}

		@Override
		public PreparedCqlQuery withUUIDValue(UUID value) {
			bindList.add(value);
			return this;
		}
	}
	
	
	@Override
	public PreparedCqlQuery asPreparedStatement() {
		return new InternalPreparedStatement();
	}
	
	private class InternalExecutionImpl extends CqlAbstractExecutionImpl> {

		private final Statement query;
		
		public InternalExecutionImpl(Statement query) {
			super(ksContext, cfContext);
			this.query = query;
		}

		@Override
		public CassandraOperationType getOperationType() {
			return CassandraOperationType.CQL;
		}

		@Override
		public Statement getQuery() {
			return query;
		}

		@Override
		public CqlResult parseResultSet(ResultSet resultSet) {
			
			boolean isCountQuery = basicCqlQuery.contains(" count(");
			if (isCountQuery) {
				return new DirectCqlResult(new Long(resultSet.one().getLong(0)));
			} else {
				return new DirectCqlResult(resultSet.all(), (ColumnFamily) cf);
			}
		}
	}
}