Class: Cql::Client::PreparedStatement
- Inherits:
-
Object
- Object
- Cql::Client::PreparedStatement
- Defined in:
- lib/cql/client/prepared_statement.rb
Overview
A prepared statement are CQL queries that have been sent to the server to be precompiled, so that when executed only their ID and not the whole CQL string need to be sent. They support bound values, or placeholders for values.
Using a prepared statement for any query that you execute more than once is highly recommended. Besides the benefit of having less network overhead, and less processing overhead on the server side, they don't require you to build CQL strings and escape special characters, or format non-character data such as UUIDs, different numeric types, or collections, in the correct way.
You should only prepare a statement once and reuse the prepared statement object every time you want to execute that particular query. The statement object will make sure that it is prepared on all connections, and will (lazily, but transparently) make sure it is prepared on any new connections.
It is an anti-pattern to prepare the same query over and over again. It is bad for performance, since every preparation requires a roundtrip to all connected servers, and because of some bookeeping that is done to support automatic preparation on new connections, it will lead to unnecessary extra memory usage. There is no performance benefit in creating multiple prepared statement objects for the same query.
Prepared statement objects are completely thread safe and can be shared across all threads in your application.
Instance Attribute Summary collapse
-
#metadata ⇒ ResultMetadata
readonly
Metadata describing the bound values.
-
#result_metadata ⇒ ResultMetadata
readonly
Metadata about the result (i.e. rows) that is returned when executing this prepared statement.
Instance Method Summary collapse
-
#batch(type = :logged, options = {}) {|batch| ... } ⇒ Cql::Client::VoidResult, Cql::Client::Batch
Yields a batch when called with a block.
-
#execute(*args) ⇒ nil, ...
Execute the prepared statement with a list of values to be bound to the statements parameters.
Instance Attribute Details
#metadata ⇒ ResultMetadata (readonly)
Metadata describing the bound values
37 38 39 |
# File 'lib/cql/client/prepared_statement.rb', line 37 def @metadata end |
#result_metadata ⇒ ResultMetadata (readonly)
Metadata about the result (i.e. rows) that is returned when executing this prepared statement.
43 44 45 |
# File 'lib/cql/client/prepared_statement.rb', line 43 def @result_metadata end |
Instance Method Details
#batch(type = :logged, options = {}) {|batch| ... } ⇒ Cql::Client::VoidResult, Cql::Client::Batch
Yields a batch when called with a block. The batch is automatically executed at the end of the block and the result is returned.
Returns a batch when called wihtout a block. The batch will remember the options given and merge these with any additional options given when Cql::Client::PreparedStatementBatch#execute is called.
The batch yielded or returned by this method is not identical to the regular batch objects yielded or returned by Client#batch. These prepared statement batch objects can be used only to add multiple executions of the same prepared statement.
Please note that the batch object returned by this method is not thread safe.
The type parameter can be ommitted and the options can then be given as first parameter.
125 126 |
# File 'lib/cql/client/prepared_statement.rb', line 125 def batch(type=:logged, ={}) end |
#execute(*args) ⇒ nil, ...
Execute the prepared statement with a list of values to be bound to the statements parameters.
The number of arguments must equal the number of bound parameters. You can also specify options as the last argument, or a symbol as a shortcut for just specifying the consistency.
Because you can specify options, or not, there is an edge case where if
the last parameter of your prepared statement is a map, and you forget
to specify a value for your map, the options will end up being sent to
Cassandra. Most other cases when you specify the wrong number of
arguments should result in an ArgumentError
or TypeError
being
raised.
86 87 |
# File 'lib/cql/client/prepared_statement.rb', line 86 def execute(*args) end |