public class SimpleStatement extends RegularStatement
RegularStatement
implementation built directly from a query
string.idempotent, NULL_PAYLOAD_VALUE
Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
static ByteBuffer[] |
convert(Object[] values,
ProtocolVersion protocolVersion,
CodecRegistry codecRegistry)
Utility method to serialize user-provided values.
|
String |
getKeyspace()
Returns the keyspace this query operates on.
|
Object |
getObject(int i)
Returns the
i th value as the Java type matching its CQL type. |
String |
getQueryString(CodecRegistry codecRegistry)
Returns the query string for this statement.
|
ByteBuffer |
getRoutingKey(ProtocolVersion protocolVersion,
CodecRegistry codecRegistry)
Returns the routing key for the query.
|
ByteBuffer[] |
getValues(ProtocolVersion protocolVersion,
CodecRegistry codecRegistry)
The values to use for this statement.
|
boolean |
hasValues(CodecRegistry codecRegistry)
Whether or not this statement has values, that is if
getValues
will return null or not. |
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.
|
int |
valuesCount()
The number of values for this statement, that is the size of the array
that will be returned by
getValues . |
getQueryString, hasValues, toString
disableTracing, enableTracing, getConsistencyLevel, getDefaultTimestamp, getFetchSize, getRetryPolicy, getSerialConsistencyLevel, isIdempotent, isTracing, setConsistencyLevel, setDefaultTimestamp, setFetchSize, setIdempotent, setOutgoingPayload, setPagingState, setPagingState, setPagingStateUnsafe, setRetryPolicy, setSerialConsistencyLevel
public SimpleStatement(String query)
SimpleStatement
with the provided query string (and no values).query
- the query string.public SimpleStatement(String query, Object... values)
SimpleStatement
with 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 to
PreparedStatement ps = session.prepare(query); session.execute(ps.bind(value1, value2, value3));except that the former version:
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). The codec to serialize each value will be chosen in the codec registry
associated with this session's cluster, based on the value's Java type
(this is the equivalent to calling CodecRegistry.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, an
InvalidQueryException
will be thrown
by Cassandra at execution time. A CodecNotFoundException
may be
thrown by this constructor however, if the codec registry does not know how to
handle one of the values.query
- the query string.values
- values required for the execution of query
.IllegalArgumentException
- if the number of values is greater than 65535.public String getQueryString(CodecRegistry codecRegistry)
RegularStatement
getQueryString
in class RegularStatement
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-arg RegularStatement.getQueryString()
depending on the type of statement this is called on.RegularStatement.getQueryString()
public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
RegularStatement
null
) are not supported with the native protocol version 1: you
will get an UnsupportedProtocolVersionException
when submitting
one if version 1 of the protocol is in use (i.e. if you've force version
1 through Cluster.Builder.withProtocolVersion(com.datastax.driver.core.ProtocolVersion)
or you use
Cassandra 1.2).getValues
in class RegularStatement
protocolVersion
- the protocol version that will be used to serialize
the values.codecRegistry
- the codec registry that will be used to serialize the
values.SimpleStatement(String, Object...)
public int valuesCount()
getValues
.public boolean hasValues(CodecRegistry codecRegistry)
RegularStatement
getValues
will return null
or not.hasValues
in class RegularStatement
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-arg
RegularStatement.hasValues()
depending on the type of
statement this is called on.false
if RegularStatement.getValues(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
returns null
, true
otherwise.RegularStatement.hasValues()
public Object getObject(int i)
i
th value as the Java type matching its CQL type.i
- the index to retrieve.i
th value of this statement.IllegalStateException
- if this statement does not have values.IndexOutOfBoundsException
- if i
is not a valid index for this object.public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
setRoutingKey(java.nio.ByteBuffer)
, this method will return null
to
avoid having to parse the query string to retrieve the partition key.getRoutingKey
in class Statement
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).setRoutingKey(java.nio.ByteBuffer)
if such a key
was set, null
otherwise.Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
public SimpleStatement setRoutingKey(ByteBuffer routingKey)
setRoutingKey(ByteBuffer...)
method instead to build the
routing key.routingKey
- the raw (binary) value to use as routing key.SimpleStatement
object.Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
public String getKeyspace()
setKeyspace(java.lang.String)
,
this method will return null
to avoid having to parse the query
string.getKeyspace
in class Statement
setKeyspace(java.lang.String)
if such keyspace was
set, null
otherwise.Statement.getKeyspace()
public SimpleStatement setKeyspace(String keyspace)
keyspace
- the name of the keyspace this query operates on.SimpleStatement
object.Statement.getKeyspace()
public SimpleStatement setRoutingKey(ByteBuffer... routingKeyComponents)
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.routingKeyComponents
- the raw (binary) values to compose to obtain
the routing key.SimpleStatement
object.Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
public static ByteBuffer[] convert(Object[] values, ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
SimpleStatement
or a BuiltStatement
(Query Builder) contain values;
in these places, the driver has no way to determine the right CQL type to use.
This method performs a best-effort heuristic to guess which codec to use.
Note that this is not particularly efficient as the codec registry needs to iterate over
the registered codecs until it finds a suitable one.values
- The values to convert.protocolVersion
- The protocol version to use.codecRegistry
- The CodecRegistry
to use.