If you want to perform multiple data modification operations in a single roundtrip, Session::batchExecute
will be the choice.
The signature of Session::batchExecute
is:
seastar::future<> batchExecute(BatchCommand&& command);
Different to query
and execute
, batchExecute
takes BatchCommand
instead of Command
.
Only insert, update and delete statements can use in a batch command.
BatchCommand
can built with chain style or command style:
auto command = cql::BatchCommand()
.addQuery("insert into exmple_ks.my_table (id, name) values (?,?)")
.prepareQuery()
.openParameterSet()
.addParameters(cql::Int(1), cql::Text("a"))
.openParameterSet()
.addParameters(cql::Int(2), cql::Text("b"))
.openParameterSet()
.addParameters(cql::Int(3), cql::Text("c"))
.addQuery("update exmple_ks.my_table set name = ? where id = ?")
.addParameters(cql::Text("aaa"), cql::Int(1))
.addQuery("delete from exmple_ks.my_table where id = ?")
.addParameters(cql::Int(3));
Equals
cql::BatchCommand command;
command.addQuery("insert into exmple_ks.my_table (id, name) values (?,?)")
command.prepareQuery();
command.openParameterSet();
command.addParameters(cql::Int(1), cql::Text("a"));
command.openParameterSet();
command.addParameters(cql::Int(2), cql::Text("b"));
command.openParameterSet();
command.addParameters(cql::Int(3), cql::Text("c"));
command.addQuery("update exmple_ks.my_table set name = ? where id = ?");
command.addParameters(cql::Text("aaa"), cql::Int(1));
command.addQuery("delete from exmple_ks.my_table where id = ?");
command.addParameters(cql::Int(3));
Then pass to Session::batchExecute
:
session.batchExecute(std::move(command)).then([] {
std::cout << "batch was successful" << std::endl;
});
BatchCommand
contains following options:
Set the type of this batch, default is "Logged".
Supported batch types:
- BatchType::Logged
- BatchType::UnLogged
- BatchType::Counter
Set the consistency level of this query.
This will override the default setting in SessionConfiguration.
For more information see this page.
Supported consistencies:
- ConsistencyLevel::Any
- ConsistencyLevel::One
- ConsistencyLevel::Two
- ConsistencyLevel::Three
- ConsistencyLevel::Quorum
- ConsistencyLevel::All
- ConsistencyLevel::LocalQuorum
- ConsistencyLevel::EachQuorum
- ConsistencyLevel::Serial
- ConsistencyLevel::LocalSerial
- ConsistencyLevel::LocalOne
Add a new query to this batch. There multiple overloads of this function:
BatchCommand& addQuery(std::string&& query) &;
BatchCommand& addQuery(const char* query, std::size_t size) &;
template <std::size_t Size>
BatchCommand& addQuery(const char(&query)[Size]) &;
Notice overload 2 and 3 won't copy the string to the internal buffer in BatchCommand
,
it can avoid the unnecessary memory copy,
but you have to ensure the string is alive until Session::batchExecute
is finished.
Set should prepare the last query.
This will override the default setting in SessionConfiguration.
The prepare request will only be sent if the query isn't prepared before.
For more information see Prepare.
Open a new parameter set explicitly of the last query.
This function is optional if there only one parameter set, for example:
auto command = cql::BatchCommand()
.addQuery("update exmple_ks.my_table set name = ? where id = ?")
.addParameters(cql::Text("aaa"), cql::Int(1));
Euqals
auto command = cql::BatchCommand()
.openParameterSet()
.addQuery("update exmple_ks.my_table set name = ? where id = ?")
.addParameters(cql::Text("aaa"), cql::Int(1));
But it's required if you want to open multiple parameter sets, for example:
auto command = cql::BatchCommand()
.addQuery("insert into exmple_ks.my_table (id, name) values (?,?)")
.prepareQuery()
.openParameterSet()
.addParameters(cql::Int(1), cql::Text("a"))
.openParameterSet()
.addParameters(cql::Int(2), cql::Text("b"))
.openParameterSet()
.addParameters(cql::Int(3), cql::Text("c"));
Add single query parameter bound by position to the last parameter set.
The position is incremental, when this function is called.
Add multiple query parameters bound by position to the last parameter set.
The position is incremental, when this function is called.
For example:
auto command = cql::BatchCommand()
.addQuery("update exmple_ks.my_table set name = ? where id = ?")
.addParameters(cql::Text("aaa"), cql::Int(1));
Equals
auto command = cql::BatchCommand()
.addQuery("update exmple_ks.my_table set name = ? where id = ?")
.addParameter(cql::Text("aaa"))
.addParameter(cql::Int(1));
Set the serial consistency level of this query.
Can only be either SERIAL
or LOCAL_SERIAL
.
Set the default timestamp of this query.
This will replace the server side assigned timestamp as default timestamp.
A timestamp in the query itself will still override this timestamp.
Set the maximum retry times after the first try has failed, default is 0.
Please ensure all statements are idempotent, this driver won't check it.