Skip to content

Latest commit

 

History

History
168 lines (116 loc) · 4.82 KB

Query.md

File metadata and controls

168 lines (116 loc) · 4.82 KB

Query

After create a Session instance from SessionFactory, you can perform database query by use the Session::query function.
The signature of Session::query is:

seastar::future<ResultSet> query(Command&& command);

Command

Command can built with chain style or command style:

auto command = cql::Command("select * from batchlog")
	.setConsistency(cql::ConsistencyLevel::Quorum)
	.setPageSize(100);

Equals

cql::Command command("select * from batchlog");
command.setConsistency(cql::ConsistencyLevel::Quorum);
command.setPageSize(100);

Then pass to Session::query:

session.query(std::move(command)).then([] (cql::ResultSet result) {
	std::cout << "columns count: " << result.getColumnsCount() << std::endl;
	std::cout << "rows count: " << result.getRowsCount() << std::endl;
	cql::Int id;
	cql::Text name;
	for (std::size_t i = 0, j = result.getRowsCount(); i < j; ++i) {
		result.fill(id, name);
		std::cout << id << " " << name << std::endl;
	}
});

Command contains following options:

prepareQuery(bool)

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.

setConsistency(ConsistencyLevel)

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

setPageSize(std::size_t)

Set the page size of this query.
You may also want to set the page state if you want to query the next page.

setPagingState(std::string&&)

Set the paging state of this query.
For the first page this is unnecessary.
Please sure you called the setPageSize before this function.

addParameter(T&&)

Add single query parameter bound by position.
The position is incremental, when this function is called.

addParameters(Args&&...)

Add multiple query parameters bound by position.
The position is incremental, when this function is called.

For example:

auto command = cql::Command("insert into exmple_ks.my_table (id, name) values (?,?)")
	.addParameters(cql::Int(1), cql::Text("a"));

Equals

auto command = cql::Command("insert into exmple_ks.my_table (id, name) values (?,?)")
	.addParameter(cql::Int(1))
	.addParameter(cql::Text("a"));

Named parameter is unsupported, and it's not recommended to use.

setSerialConsistency(ConsistencyLevel)

Set the serial consistency level of this query.
Can only be either SERIAL or LOCAL_SERIAL.

setDefaultTimestamp(std::chrono::system_clock::time_point)

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.

setMaxRetries(std::size_t)

Set the maximum retry times after the first try has failed, default is 0.
Please ensure the statement is idempotent, this driver won't check it.

ResultSet

ResultSet contains the result of the query.
Notice query will return a empty(0 rows) ResultSet if the statement isn't select.

ResultSet contains following functions:

bool isValid()

Check whether this is a valid result set (will be false if moved).

std::size_t getRowsCount()

Get how many rows present in this result.

std::size_t getColumnsCount()

Get how many columns selected by the query that produced this result.

const std::string& getPagingState()

Get the value used to retrieve the next page of results.

void fill(Args&... values)

Fill the values with specificed types.
The position is incremental, when this function is called.
Notice it won't check the actual type is matched.

For example, if you selected two columns and their types is Int and Text, you can use following code to get all records:

cql::Int id;
cql::Text name;
for (std::size_t i = 0, j = result.getRowsCount(); i < j; ++i) {
	result.fill(id, name);
	std::cout << id << " " << name << std::endl;
}

Get column index by it's name is unsupported, so don't use select * from ....

For more column types, see the document of column types.