com.datastax.driver.core
Class PreparedStatement

java.lang.Object
  extended by com.datastax.driver.core.PreparedStatement

public class PreparedStatement
extends Object

Represents a prepared statement, a query with bound variables that has been prepared (pre-parsed) by the database.

A prepared statement can be executed once concrete values has been provided for the bound variables. The pair of a prepared statement and values for its bound variables is a BoundStatement and can be executed (by Session.execute(java.lang.String)).


Method Summary
 BoundStatement bind(Object... values)
          Creates a new BoundStatement object and bind its variables to the provided values.
 ConsistencyLevel getConsistencyLevel()
          The default consistency level set through setConsistencyLevel(com.datastax.driver.core.ConsistencyLevel).
 String getQueryKeyspace()
          The current keyspace at the time this prepared statement was prepared, i.e.
 String getQueryString()
          The string of the query that was prepared to yield this PreparedStatement.
 ColumnDefinitions getVariables()
          Returns metadata on the bounded variables of this prepared statement.
 PreparedStatement setConsistencyLevel(ConsistencyLevel consistency)
          Sets a default consistency level for all BoundStatement created from this object.
 PreparedStatement setRoutingKey(ByteBuffer... routingKeyComponents)
          Set the routing key for this query.
 PreparedStatement setRoutingKey(ByteBuffer routingKey)
          Set the routing key for this prepared statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getVariables

public ColumnDefinitions getVariables()
Returns metadata on the bounded variables of this prepared statement.

Returns:
the variables bounded in this prepared statement.

bind

public BoundStatement bind(Object... values)
Creates a new BoundStatement object and bind its variables to the provided values.

This method is a shortcut for new BoundStatement(this).bind(...).

Note that while no more values than bound variables can be provided, it is allowed to provide less values that there is variables. In that case, the remaining variables will have to be bound to values by another mean because the resulting BoundStatement being executable.

Parameters:
values - the values to bind to the variables of the newly created BoundStatement.
Returns:
the newly created BoundStatement with its variables bound to values.
Throws:
IllegalArgumentException - if more values are provided than there is of bound variables in this statement.
InvalidTypeException - if any of the provided value is not of correct type to be bound to the corresponding bind variable.
See Also:
BoundStatement.bind(java.lang.Object...)

setRoutingKey

public PreparedStatement setRoutingKey(ByteBuffer routingKey)
Set the routing key for this prepared statement.

This method allows to manually provide a fixed routing key for all executions of this prepared statement. It is never mandatory to provide a routing key through this method and this method should only be used if the partition key of the prepared query is not part of the prepared variables (i.e. if the partition key is fixed).

Note that if the partition key is part of the prepared variables, the routing key will be automatically computed once those variables are bound.

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

setRoutingKey

public PreparedStatement setRoutingKey(ByteBuffer... routingKeyComponents)
Set 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 PreparedStatement object.
See Also:
Query.getRoutingKey()

setConsistencyLevel

public PreparedStatement setConsistencyLevel(ConsistencyLevel consistency)
Sets a default consistency level for all BoundStatement created from this object.

If no consistency level is set through this method, the BoundStatement created from this object will use the default consistency level (ONE).

Changing the default consistency level is not retroactive, it only applies to BoundStatement created after the change.

Parameters:
consistency - the default consistency level to set.
Returns:
this PreparedStatement object.

getConsistencyLevel

public ConsistencyLevel getConsistencyLevel()
The default consistency level set through setConsistencyLevel(com.datastax.driver.core.ConsistencyLevel).

Returns:
the default consistency level. Returns null if no consistency level has been set through this object setConsistencyLevel method.

getQueryString

public String getQueryString()
The string of the query that was prepared to yield this PreparedStatement.

Note that a CQL3 query may implicitely apply on the current keyspace (that is, if the keyspace is not explicity qualified in the query itself). For prepared queries, the current keyspace used is the one at the time of the preparation, not the one at execution time. The current keyspace at the time of the preparation can be retrieved through #getKeyspaceAtPreparation.

Returns:
the query that was prepared to yield this PreparedStatement.

getQueryKeyspace

public String getQueryKeyspace()
The current keyspace at the time this prepared statement was prepared, i.e. the one on which this statement applies unless it specificy a keyspace directly.

Returns:
the current keyspace at the time this statement was prepared, or null if no keyspace was set when the query was prepared (which is possible since keyspaces can be explicitly qualified in queries and so may not require a current keyspace to be set).


Copyright © 2013. All Rights Reserved.