com.datastax.driver.core
Class SimpleStatement

java.lang.Object
  extended by com.datastax.driver.core.Statement
      extended by com.datastax.driver.core.RegularStatement
          extended by com.datastax.driver.core.SimpleStatement

public class SimpleStatement
extends RegularStatement

A simple RegularStatement implementation built directly from a query string.


Constructor Summary
SimpleStatement(String query)
          Creates a new SimpleStatement with the provided query string (and no values).
SimpleStatement(String query, Object... values)
          Creates a new SimpleStatement with the provided query string and values.
 
Method Summary
 String getKeyspace()
          Returns the keyspace this query operates on.
 String getQueryString()
          Returns the query string.
 ByteBuffer getRoutingKey()
          Returns the routing key for the query.
 ByteBuffer[] getValues()
          The values to use for this statement.
 SimpleStatement setKeyspace(String keyspace)
          Sets the keyspace this query operates on.
 SimpleStatement setRoutingKey(ByteBuffer... routingKeyComponents)
          Sets the routing key for this query.
 SimpleStatement setRoutingKey(ByteBuffer routingKey)
          Sets the routing key for this query.
 
Methods inherited from class com.datastax.driver.core.RegularStatement
toString
 
Methods inherited from class com.datastax.driver.core.Statement
disableTracing, enableTracing, getConsistencyLevel, getFetchSize, getRetryPolicy, getSerialConsistencyLevel, isTracing, setConsistencyLevel, setFetchSize, setRetryPolicy, setSerialConsistencyLevel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SimpleStatement

public SimpleStatement(String query)
Creates a new SimpleStatement with the provided query string (and no values).

Parameters:
query - the query string.

SimpleStatement

public SimpleStatement(String query,
                       Object... values)
Creates a new SimpleStatement with the provided query string and values.

This version of SimpleStatement is useful when you do not want to execute a query only once (and thus do not want to resort to prepared statement), but do not want to convert all column values to string (typically, if you have blob values, encoding them to a hexidecimal string is not very efficient). In that case, you can provide a query string with bind marker to this constructor along with the values for those bind variables. When executed, the server will prepare the provided, bind the provided values to that prepare statement and execute the resulting statement. Thus,

   session.execute(new SimpleStatement(query, value1, value2, value3));
 
is functionally equivalent to
   PreparedStatement ps = session.prepare(query);
   session.execute(ps.bind(value1, value2, value3));
 
except that the former version:

Note that the type of the values provided to this method will not be validated by the driver as is done by BoundStatement.bind(java.lang.Object...) since query is not parsed (and hence the driver cannot know what those value should be). If too much or too little values are provided or if a value is not a valid one for the variable it is bound to, an InvalidQueryException will be thrown by Cassandra at execution time. An IllegalArgumentException may be thrown by this constructor however if one of the value does not correspond to any CQL3 type (for instance, if it is a custom class).

Parameters:
query - the query string.
values - values required for the execution of query.
Throws:
IllegalArgumentException - if one of values is not of a type corresponding to a CQL3 type, i.e. is not a Class that could be returned by DataType.asJavaClass().
Method Detail

getQueryString

public String getQueryString()
Returns the query string.

Specified by:
getQueryString in class RegularStatement
Returns:
the query string;

getValues

public ByteBuffer[] getValues()
Description copied from class: RegularStatement
The values to use for this statement.

Specified by:
getValues in class RegularStatement
Returns:
the values to use for this statement or null if there is no such values.
See Also:
SimpleStatement(String, Object...)

getRoutingKey

public ByteBuffer getRoutingKey()
Returns the routing key for the query.

Unless the routing key has been explicitly set through setRoutingKey(java.nio.ByteBuffer), this method will return null to avoid having to parse the query string to retrieve the partition key.

Specified by:
getRoutingKey in class Statement
Returns:
the routing key set through setRoutingKey(java.nio.ByteBuffer) if such a key was set, null otherwise.
See Also:
Statement.getRoutingKey()

setRoutingKey

public SimpleStatement setRoutingKey(ByteBuffer routingKey)
Sets the routing key for this query.

This method allows you to manually provide a routing key for this query. It is thus optional since the routing key is only an hint for token aware load balancing policy but is never mandatory.

If the partition key for the query is composite, use the setRoutingKey(ByteBuffer...) method instead to build the routing key.

Parameters:
routingKey - the raw (binary) value to use as routing key.
Returns:
this SimpleStatement object.
See Also:
Statement.getRoutingKey()

getKeyspace

public String getKeyspace()
Returns the keyspace this query operates on.

Unless the keyspace has been explicitly set through setKeyspace(java.lang.String), this method will return null to avoid having to parse the query string.

Specified by:
getKeyspace in class Statement
Returns:
the keyspace set through setKeyspace(java.lang.String) if such keyspace was set, null otherwise.
See Also:
Statement.getKeyspace()

setKeyspace

public SimpleStatement setKeyspace(String keyspace)
Sets the keyspace this query operates on.

This method allows you to manually provide a keyspace for this query. It is thus optional since the value returned by this method is only an hint for token aware load balancing policy but is never mandatory.

Do note that if the query does not use a fully qualified keyspace, then you do not need to set the keyspace through that method as the currently logged in keyspace will be used.

Parameters:
keyspace - the name of the keyspace this query operates on.
Returns:
this SimpleStatement object.
See Also:
Statement.getKeyspace()

setRoutingKey

public SimpleStatement setRoutingKey(ByteBuffer... routingKeyComponents)
Sets the routing key for this query.

See setRoutingKey(ByteBuffer) for more information. This method is a variant for when the query partition key is composite and thus the routing key must be built from multiple values.

Parameters:
routingKeyComponents - the raw (binary) values to compose to obtain the routing key.
Returns:
this SimpleStatement object.
See Also:
Statement.getRoutingKey()


Copyright © 2013. All rights reserved.