Interface PreparedStatement
-
public interface PreparedStatementA query with bind variables that has been pre-parsed by the database.Client applications create instances with
SyncCqlSession.prepare(SimpleStatement). Then they usebind(Object...)to obtain an executableBoundStatement.The default prepared statement implementation returned by the driver is thread-safe. Client applications can -- and are expected to -- prepare each query once and store the result in a place where it can be accessed concurrently by application threads (for example a final field). Preparing the same query string twice is suboptimal and a bad practice, and will cause the driver to log a warning.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description BoundStatementbind(Object... values)Builds an executable statement that associates a set of values with the bind variables.BoundStatementBuilderboundStatementBuilder(Object... values)Returns a builder to construct an executable statement.ByteBuffergetId()A unique identifier for this prepared statement.PartitionergetPartitioner()The partitioner to use for token-aware routing.List<Integer>getPartitionKeyIndices()The indices of the variables ingetVariableDefinitions()that correspond to the target table's partition key.StringgetQuery()RequestRoutingTypegetRequestRoutingType()Returns the request routing type for this prepared statement.ByteBuffergetResultMetadataId()A unique identifier for result metadata (essentially a hash ofgetResultSetDefinitions()).ColumnDefinitionsgetResultSetDefinitions()A description of the result set that will be returned when this prepared statement is bound and executed.ColumnDefinitionsgetVariableDefinitions()A description of the bind variables of this prepared statement.booleanisLWT()Informs if this is an LWT query.voidsetResultMetadata(ByteBuffer newResultMetadataId, ColumnDefinitions newResultSetDefinitions)UpdatesgetResultMetadataId()andgetResultSetDefinitions()atomically.
-
-
-
Method Detail
-
getId
@NonNull ByteBuffer getId()
A unique identifier for this prepared statement.Note: the returned buffer is read-only.
-
getQuery
@NonNull String getQuery()
-
getVariableDefinitions
@NonNull ColumnDefinitions getVariableDefinitions()
A description of the bind variables of this prepared statement.
-
getPartitioner
Partitioner getPartitioner()
The partitioner to use for token-aware routing. Ifnull, the cluster-wide partitioner will be used.
-
getPartitionKeyIndices
@NonNull List<Integer> getPartitionKeyIndices()
The indices of the variables ingetVariableDefinitions()that correspond to the target table's partition key.This is only present if all the partition key columns are expressed as bind variables. Otherwise, the list will be empty. For example, given the following schema:
CREATE TABLE foo (pk1 int, pk2 int, cc int, v int, PRIMARY KEY ((pk1, pk2), cc));
And the following definitions:PreparedStatement ps1 = session.prepare("UPDATE foo SET v = ? WHERE pk1 = ? AND pk2 = ? AND v = ?"); PreparedStatement ps2 = session.prepare("UPDATE foo SET v = ? WHERE pk1 = 1 AND pk2 = ? AND v = ?");Thenps1.getPartitionKeyIndices()contains 1 and 2, andps2.getPartitionKeyIndices()is empty (because one of the partition key components is hard-coded in the query string).
-
getResultMetadataId
@Nullable ByteBuffer getResultMetadataId()
A unique identifier for result metadata (essentially a hash ofgetResultSetDefinitions()).This information is mostly for internal use: with protocol
DefaultProtocolVersion.V5or higher, the driver sends it with every execution of the prepared statement, to validate that its result metadata is still up-to-date.Note: this method returns
nullfor protocolDefaultProtocolVersion.V4or lower; otherwise, the returned buffer is read-only.- See Also:
- CASSANDRA-10786
-
getResultSetDefinitions
@NonNull ColumnDefinitions getResultSetDefinitions()
A description of the result set that will be returned when this prepared statement is bound and executed.This information is only present for
SELECTqueries, otherwise it is always empty. Note that this is slightly incorrect for conditional updates (e.g.INSERT ... IF NOT EXISTS), which do return columns; for those cases, usePagingIterable.getColumnDefinitions()on the result, not this method.
-
isLWT
boolean isLWT()
Informs if this is an LWT query.Not guaranteed to return true for LWT queries (but guaranteed to return false for non-LWT ones). It can happen for several reasons, for example: using Cassandra instead of Scylla, using too old Scylla version, future changes in driver allowing channels to be created without sending OPTIONS request.
More information about LWT:
- See Also:
- Docs about LWT
-
getRequestRoutingType
@Nullable RequestRoutingType getRequestRoutingType()
Returns the request routing type for this prepared statement.
-
setResultMetadata
void setResultMetadata(@NonNull ByteBuffer newResultMetadataId, @NonNull ColumnDefinitions newResultSetDefinitions)UpdatesgetResultMetadataId()andgetResultSetDefinitions()atomically.This is for internal use by the driver. Calling this manually with incorrect information can cause existing queries to fail.
-
bind
@NonNull BoundStatement bind(@NonNull Object... values)
Builds an executable statement that associates a set of values with the bind variables.Note that the built-in bound statement implementation is immutable. If you need to set multiple execution parameters on the bound statement (such as
Statement.setExecutionProfileName(String),Statement.setPagingState(ByteBuffer), etc.), consider usingboundStatementBuilder(Object...)instead to avoid unnecessary allocations.- Parameters:
values- the values of the bound variables in the statement. You can provide less values than the actual number of variables (or even none at all), in which case the remaining variables will be left unset. However, this method will throw anIllegalArgumentExceptionif there are more values than variables. Individual values can benull, but the vararg array itself can't.
-
boundStatementBuilder
@NonNull BoundStatementBuilder boundStatementBuilder(@NonNull Object... values)
Returns a builder to construct an executable statement.Note that this builder is mutable and not thread-safe.
- See Also:
bind(Object...)
-
-