Class SimpleStatement
- java.lang.Object
-
- com.datastax.driver.core.Statement
-
- com.datastax.driver.core.RegularStatement
-
- com.datastax.driver.core.SimpleStatement
-
public class SimpleStatement extends RegularStatement
A simpleRegularStatementimplementation built directly from a query string.
-
-
Field Summary
-
Fields inherited from class com.datastax.driver.core.Statement
idempotent, NULL_PAYLOAD_VALUE
-
-
Constructor Summary
Constructors Constructor Description SimpleStatement(String query)Creates a newSimpleStatementwith the provided query string (and no values).SimpleStatement(String query, Object... values)Creates a newSimpleStatementwith the provided query string and values.SimpleStatement(String query, Map<String,Object> values)Creates a newSimpleStatementwith the provided query string and named values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetKeyspace()Returns the keyspace this query operates on.Map<String,ByteBuffer>getNamedValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)The named values to use for this statement.ObjectgetObject(int i)Returns theith positional value as the Java type matching its CQL type.ObjectgetObject(String name)Returns a named value as the Java type matching its CQL type.StringgetQueryString(CodecRegistry codecRegistry)Returns the query string for this statement.ByteBuffergetRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)Returns the routing key for the query.Set<String>getValueNames()Returns the names of the named values of this statement.ByteBuffer[]getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)The positional values to use for this statement.booleanhasValues(CodecRegistry codecRegistry)Whether or not this statement has values, that is ifgetValueswill returnnullor not.SimpleStatementsetKeyspace(String keyspace)Sets the keyspace this query operates on.SimpleStatementsetRoutingKey(ByteBuffer routingKey)Sets the routing key for this query.SimpleStatementsetRoutingKey(ByteBuffer... routingKeyComponents)Sets the routing key for this query.booleanusesNamedValues()Whether this statement uses named values.intvaluesCount()The number of values for this statement, that is the size of the array that will be returned bygetValues.-
Methods inherited from class com.datastax.driver.core.RegularStatement
getQueryString, hasValues, requestSizeInBytes, toString
-
Methods inherited from class com.datastax.driver.core.Statement
disableTracing, enableTracing, getConsistencyLevel, getDefaultTimestamp, getFetchSize, getHost, getLastHost, getNowInSeconds, getOutgoingPayload, getPartitioner, getReadTimeoutMillis, getRetryPolicy, getSerialConsistencyLevel, isBatchIdempotent, isIdempotent, isLWT, isTracing, setConsistencyLevel, setDefaultTimestamp, setFetchSize, setHost, setIdempotent, setLastHost, setNowInSeconds, setOutgoingPayload, setPagingState, setPagingState, setPagingStateUnsafe, setReadTimeoutMillis, setRetryPolicy, setSerialConsistencyLevel
-
-
-
-
Constructor Detail
-
SimpleStatement
public SimpleStatement(String query)
Creates a newSimpleStatementwith the provided query string (and no values).- Parameters:
query- the query string.
-
SimpleStatement
public SimpleStatement(String query, Object... values)
Creates a newSimpleStatementwith the provided query string and values.This version of SimpleStatement is useful when you 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 hexadecimal string is not very efficient). In that case, you can provide a query string with bind markers 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 toPreparedStatement ps = session.prepare(query); session.execute(ps.bind(value1, value2, value3));
except that the former version:- Requires only one round-trip to a Cassandra node.
- Does not left any prepared statement stored in memory (neither client or server side) once it has been executed.
Note that the types of the
valuesprovided to this method will not be validated by the driver as is done byBoundStatement.bind(java.lang.Object...)sincequeryis not parsed (and hence the driver cannot know what those values should be). The codec to serialize each value will be chosen in the codec registry associated with the cluster executing this statement, based on the value's Java type (this is the equivalent to callingCodecRegistry.codecFor(Object)). If too many or too few values are provided, or if a value is not a valid one for the variable it is bound to, anInvalidQueryExceptionwill be thrown by Cassandra at execution time. ACodecNotFoundExceptionmay be thrown by this constructor however, if the codec registry does not know how to handle one of the values.If you have a single value of type
Map<String, Object>, you can't call this constructor using the varargs syntax, because the signature collides withSimpleStatement(String, Map). To prevent this, pass an explicit array object:new SimpleStatement("...", new Object[]{m});- Parameters:
query- the query string.values- values required for the execution ofquery.- Throws:
IllegalArgumentException- if the number of values is greater than 65535.
-
SimpleStatement
public SimpleStatement(String query, Map<String,Object> values)
Creates a newSimpleStatementwith the provided query string and named values.This constructor requires that the query string use named placeholders, for example:
Make sure that the map is correctly typednew SimpleStatement("SELECT * FROM users WHERE id = :i", ImmutableMap.<String, Object>of("i", 1));Map<String, Object>, otherwise you might accidentally callSimpleStatement(String, Object...)with a positional value of type map.The types of the values will be handled the same way as with anonymous placeholders (see
SimpleStatement(String, Object...)).Simple statements with named values are only supported starting with native protocol
v3. With earlier versions, anUnsupportedFeatureExceptionwill be thrown at execution time.- Parameters:
query- the query string.values- named values required for the execution ofquery.- Throws:
IllegalArgumentException- if the number of values is greater than 65535.
-
-
Method Detail
-
getQueryString
public String getQueryString(CodecRegistry codecRegistry)
Description copied from class:RegularStatementReturns the query string for this statement.It is important to note that the query string is merely a CQL representation of this statement, but it does not convey all the information stored in
Statementobjects.For example,
Statementobjects carry numerous protocol-level settings, such as theconsistency levelto use, or theidempotence flag, among others. None of these settings will be included in the resulting query string.Similarly, if values have been set on this statement because it has bind markers, these values will not appear in the resulting query string.
Note: the consistency level was conveyed at CQL level in older versions of the CQL grammar, but since CASSANDRA-4734 it is now a protocol-level setting and consequently does not appear in the query string.
- Specified by:
getQueryStringin classRegularStatement- Parameters:
codecRegistry- the codec registry that will be used if the actual implementation needs to serialize Java objects in the process of generating the query. Note that it might be possible to use the no-argRegularStatement.getQueryString()depending on the type of statement this is called on.- Returns:
- a valid CQL query string.
- See Also:
RegularStatement.getQueryString()
-
getValues
public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
Description copied from class:RegularStatementThe positional values to use for this statement.A statement can use either positional or named values, but not both. So if this method returns a non-null result,
RegularStatement.getNamedValues(ProtocolVersion, CodecRegistry)will returnnull.Values for a RegularStatement (i.e. if either method does not return
null) are not supported with the native protocol version 1: you will get anUnsupportedProtocolVersionExceptionwhen submitting one if version 1 of the protocol is in use (i.e. if you've forced version 1 throughCluster.Builder.withProtocolVersion(com.datastax.driver.core.ProtocolVersion)or you use Cassandra 1.2).- Specified by:
getValuesin classRegularStatement- Parameters:
protocolVersion- the protocol version that will be used to serialize the values.codecRegistry- the codec registry that will be used to serialize the values.- See Also:
SimpleStatement(String, Object...)
-
getNamedValues
public Map<String,ByteBuffer> getNamedValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
Description copied from class:RegularStatementThe named values to use for this statement.A statement can use either positional or named values, but not both. So if this method returns a non-null result,
RegularStatement.getValues(ProtocolVersion, CodecRegistry)will returnnull.Values for a RegularStatement (i.e. if either method does not return
null) are not supported with the native protocol version 1: you will get anUnsupportedProtocolVersionExceptionwhen submitting one if version 1 of the protocol is in use (i.e. if you've forced version 1 throughCluster.Builder.withProtocolVersion(com.datastax.driver.core.ProtocolVersion)or you use Cassandra 1.2).- Specified by:
getNamedValuesin classRegularStatement- Parameters:
protocolVersion- the protocol version that will be used to serialize the values.codecRegistry- the codec registry that will be used to serialize the values.- Returns:
- the named values.
- See Also:
SimpleStatement(String, Map)
-
valuesCount
public int valuesCount()
The number of values for this statement, that is the size of the array that will be returned bygetValues.- Returns:
- the number of values.
-
hasValues
public boolean hasValues(CodecRegistry codecRegistry)
Description copied from class:RegularStatementWhether or not this statement has values, that is ifgetValueswill returnnullor not.- Specified by:
hasValuesin classRegularStatement- Parameters:
codecRegistry- the codec registry that will be used if the actual implementation needs to serialize Java objects in the process of determining if the query has values. Note that it might be possible to use the no-argRegularStatement.hasValues()depending on the type of statement this is called on.- Returns:
falseif bothRegularStatement.getValues(ProtocolVersion, CodecRegistry)andRegularStatement.getNamedValues(ProtocolVersion, CodecRegistry)returnnull,trueotherwise.- See Also:
RegularStatement.hasValues()
-
usesNamedValues
public boolean usesNamedValues()
Description copied from class:RegularStatementWhether this statement uses named values.- Specified by:
usesNamedValuesin classRegularStatement- Returns:
falseifRegularStatement.getNamedValues(ProtocolVersion, CodecRegistry)returnsnull,trueotherwise.
-
getObject
public Object getObject(int i)
Returns theith positional value as the Java type matching its CQL type.Note that, unlike with other driver types like
Row, you can't retrieve named values by position. This getter will throw an exception if the statement was created with named values (or no values at all). CallusesNamedValues()to check the type of values, andgetObject(String)if they are positional.- Parameters:
i- the index to retrieve.- Returns:
- the
ith value of this statement. - Throws:
IllegalStateException- if this statement does not have positional values.IndexOutOfBoundsException- ifiis not a valid index for this object.
-
getObject
public Object getObject(String name)
Returns a named value as the Java type matching its CQL type.- Parameters:
name- the name of the value to retrieve.- Returns:
- the value that matches the name, or
nullif there is no such name. - Throws:
IllegalStateException- if this statement does not have named values.
-
getValueNames
public Set<String> getValueNames()
Returns the names of the named values of this statement.- Returns:
- the names of the named values of this statement.
- Throws:
IllegalStateException- if this statement does not have named values.
-
getRoutingKey
public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
Returns the routing key for the query.Unless the routing key has been explicitly set through
setRoutingKey(java.nio.ByteBuffer), this method will returnnullto avoid having to parse the query string to retrieve the partition key.- Specified by:
getRoutingKeyin classStatement- Parameters:
protocolVersion- unused by this implementation (no internal serialization is required to compute the key).codecRegistry- unused by this implementation (no internal serialization is required to compute the key).- Returns:
- the routing key set through
setRoutingKey(java.nio.ByteBuffer)if such a key was set,nullotherwise. - See Also:
Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
-
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
SimpleStatementobject. - See Also:
Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
-
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 returnnullto avoid having to parse the query string.- Specified by:
getKeyspacein classStatement- Returns:
- the keyspace set through
setKeyspace(java.lang.String)if such keyspace was set,nullotherwise. - 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
SimpleStatementobject. - 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
SimpleStatementobject. - See Also:
Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
-
-