userver: storages::mysql::StatementResultSet Class Reference
Loading...
Searching...
No Matches
storages::mysql::StatementResultSet Class Referencefinal

#include </data/code/userver/mysql/include/userver/storages/mysql/statement_result_set.hpp>

Detailed Description

A wrapper for statement execution result.

This type can't be constructed in user code and is always retrieved from storages::mysql::Cluster or storages::mysql::Transaction methods.

Definition at line 37 of file statement_result_set.hpp.

Public Member Functions

 StatementResultSet (impl::StatementFetcher &&fetcher, tracing::Span &&span)
 
 StatementResultSet (infra::ConnectionPtr &&connection, impl::StatementFetcher &&fetcher, tracing::Span &&span)
 
 StatementResultSet (const StatementResultSet &other)=delete
 
 StatementResultSet (StatementResultSet &&other) noexcept
 
template<typename T >
std::vector< T > AsVector () &&
 Parse statement result set as std::vector<T>. T is expected to be an aggregate of supported types. See MySQL supported types for better understanding of T requirements.
 
template<typename T >
std::vector< T > AsVector (FieldTag) &&
 Parse statement result set as std::vector<T>. Result set is expected to have a single column, T is expected to be one of supported types. See MySQL supported types for supported typed.
 
template<typename Container >
Container AsContainer () &&
 Parse statement result set as Container<T>. T is expected to be an aggregate of supported types, Container is expected to meet std::Container requirements. See MySQL supported types for better understanding of Container::value_type requirements.
 
template<typename Container >
Container AsContainer (FieldTag) &&
 Parse statement result as Container<T>. Result set is expected to have a single column, T is expected to be one of supported types, Container is expected to meed std::Container requirements. See MySQL supported types for supported types.
 
template<typename T >
AsSingleRow () &&
 Parse statement result as T. Result set is expected to have a single row, T is expected to be an aggregate of supported types. See MySQL supported types for better understanding of T requirements.
 
template<typename T >
AsSingleField () &&
 Parse statement result as T. Result set is expected to have a single row and a single column, T is expected to be one of supported types. See MySQL supported types for supported types.
 
template<typename T >
std::optional< T > AsOptionalSingleRow () &&
 Parse statement result as std::optional<T>. Result set is expected to have not more than one row, T is expected to be an aggregate of supported types. See MySQL supported types for better understanding of T requirements.
 
template<typename T >
std::optional< T > AsOptionalSingleField () &&
 Parse statement result as T. Result set is expected to have not more than one row, T is expected to be one of supported types. See MySQL supported types for supported types.
 
template<typename DbType >
MappedStatementResultSet< DbType > MapFrom () &&
 Converts to an interface for on-the-flight mapping statement result set from DbType. DbType is expected to be an aggregate of supported types. See MySQL supported types for better understanding of DbType requirements.
 
ExecutionResult AsExecutionResult () &&
 Get statement execution metadata.
 

Member Function Documentation

◆ AsContainer() [1/2]

template<typename Container >
Container storages::mysql::StatementResultSet::AsContainer ( ) &&

Parse statement result set as Container<T>. T is expected to be an aggregate of supported types, Container is expected to meet std::Container requirements. See MySQL supported types for better understanding of Container::value_type requirements.

UINVARIANTs on columns count mismatch or types mismatch.

struct SampleRow final {
std::int32_t id;
std::string value;
bool operator<(const SampleRow& other) const {
return id < other.id || (id == other.id && value < other.value);
}
bool operator==(const SampleRow& other) const {
return id == other.id && value == other.value;
}
};
void PerformAsContainer(const Cluster& cluster,
const std::vector<SampleRow>& data) {
cluster.ExecuteBulk(ClusterHostType::kPrimary,
"INSERT INTO SampleTable(id, value) VALUES(?, ?)", data);
const auto set_or_rows = cluster
.Execute(ClusterHostType::kPrimary,
"SELECT id, value FROM SampleTable")
.AsContainer<std::set<SampleRow>>();
static_assert(
std::is_same_v<const std::set<SampleRow>, decltype(set_or_rows)>);
const auto input_as_set = std::set<SampleRow>{data.begin(), data.end()};
EXPECT_EQ(set_or_rows, input_as_set);
}

Definition at line 276 of file statement_result_set.hpp.

◆ AsContainer() [2/2]

template<typename Container >
Container storages::mysql::StatementResultSet::AsContainer ( FieldTag ) &&

Parse statement result as Container<T>. Result set is expected to have a single column, T is expected to be one of supported types, Container is expected to meed std::Container requirements. See MySQL supported types for supported types.

UINVARIANTs on columns count not being equal to 1 or type mismatch.

void PerformAsContainerField(const Cluster& cluster) {
cluster.Execute(ClusterHostType::kPrimary,
"INSERT INTO SampleTable(id, value) VALUES(?, ?)", 1,
"some value");
const auto set_of_values =
cluster
.Execute(ClusterHostType::kPrimary, "SELECT value FROM SampleTable")
.AsContainer<std::set<std::string>>(kFieldTag);
static_assert(
std::is_same_v<const std::set<std::string>, decltype(set_of_values)>);
ASSERT_EQ(set_of_values.size(), 1);
EXPECT_EQ(*set_of_values.begin(), "some value");
}

Definition at line 285 of file statement_result_set.hpp.

◆ AsOptionalSingleField()

template<typename T >
std::optional< T > storages::mysql::StatementResultSet::AsOptionalSingleField ( ) &&

Parse statement result as T. Result set is expected to have not more than one row, T is expected to be one of supported types. See MySQL supported types for supported types.

UINVARIANTs on columns count not being equal to 1 or type mismatch. throws if result set contains more than one row.

void PerformAsOptionalSingleField(const Cluster& cluster) {
const auto field_optional =
cluster
.Execute(ClusterHostType::kPrimary, "SELECT value FROM SampleTable")
.AsOptionalSingleField<std::string>();
static_assert(std::is_same_v<const std::optional<std::string>,
decltype(field_optional)>);
EXPECT_FALSE(field_optional.has_value());
}

Definition at line 321 of file statement_result_set.hpp.

◆ AsOptionalSingleRow()

template<typename T >
std::optional< T > storages::mysql::StatementResultSet::AsOptionalSingleRow ( ) &&

Parse statement result as std::optional<T>. Result set is expected to have not more than one row, T is expected to be an aggregate of supported types. See MySQL supported types for better understanding of T requirements.

UINVARIANTs on columns count mismatch or types mismatch. throws if result set contains more than one row.

struct SampleRow final {
std::int32_t id;
std::string value;
};
void PerformAsOptionalSingleRow(const Cluster& cluster) {
const auto row_optional = cluster
.Execute(ClusterHostType::kPrimary,
"SELECT id, value FROM SampleTable")
.AsOptionalSingleRow<SampleRow>();
static_assert(
std::is_same_v<const std::optional<SampleRow>, decltype(row_optional)>);
EXPECT_FALSE(row_optional.has_value());
}

Definition at line 316 of file statement_result_set.hpp.

◆ AsSingleField()

template<typename T >
T storages::mysql::StatementResultSet::AsSingleField ( ) &&

Parse statement result as T. Result set is expected to have a single row and a single column, T is expected to be one of supported types. See MySQL supported types for supported types.

UINVARIANTs on columns count not being equal to 1 or type mismatch. throws if result set is empty of contains more than one row.

void PerformAsSingleField(const Cluster& cluster) {
cluster.Execute(ClusterHostType::kPrimary,
"INSERT INTO SampleTable(id, value) VALUES (?, ?)", //
1, "some value");
const auto value =
cluster
.Execute(ClusterHostType::kPrimary, "SELECT value FROM SampleTable")
.AsSingleField<std::string>();
EXPECT_EQ(value, "some value");
}

Definition at line 305 of file statement_result_set.hpp.

◆ AsSingleRow()

template<typename T >
T storages::mysql::StatementResultSet::AsSingleRow ( ) &&

Parse statement result as T. Result set is expected to have a single row, T is expected to be an aggregate of supported types. See MySQL supported types for better understanding of T requirements.

UINVARIANTs on columns count mismatch or types mismatch. throws if result set is empty or contains more than one row.

struct SampleRow final {
std::int32_t id;
std::string value;
bool operator==(const SampleRow& other) const {
return id == other.id && value == other.value;
}
};
void PerformAsSingleRow(const Cluster& cluster, const SampleRow& data) {
cluster.ExecuteDecompose(ClusterHostType::kPrimary,
"INSERT INTO SampleTable(id, value) VALUES (?, ?)",
data);
const auto db_row = cluster
.Execute(ClusterHostType::kPrimary,
"SELECT id, value FROM SampleTable")
.AsSingleRow<SampleRow>();
EXPECT_EQ(data, db_row);
}

Definition at line 294 of file statement_result_set.hpp.

◆ AsVector() [1/2]

template<typename T >
std::vector< T > storages::mysql::StatementResultSet::AsVector ( ) &&

Parse statement result set as std::vector<T>. T is expected to be an aggregate of supported types. See MySQL supported types for better understanding of T requirements.

UINVARIANTs on columns count mismatch or types mismatch.

struct SampleRow final {
std::int32_t id;
std::string value;
bool operator==(const SampleRow& other) const {
return id == other.id && value == other.value;
}
};
void PerformAsVector(const Cluster& cluster,
const std::vector<SampleRow>& expected_data) {
cluster.ExecuteBulk(ClusterHostType::kPrimary,
"INSERT INTO SampleTable(id, value) VALUES (?, ?)",
expected_data);
const auto db_rows = cluster
.Execute(ClusterHostType::kPrimary,
"SELECT id, value FROM SampleTable")
.AsVector<SampleRow>();
static_assert(
std::is_same_v<const std::vector<SampleRow>, decltype(db_rows)>);
EXPECT_EQ(expected_data, db_rows);
}

Definition at line 266 of file statement_result_set.hpp.

◆ AsVector() [2/2]

template<typename T >
std::vector< T > storages::mysql::StatementResultSet::AsVector ( FieldTag ) &&

Parse statement result set as std::vector<T>. Result set is expected to have a single column, T is expected to be one of supported types. See MySQL supported types for supported typed.

UINVARIANTs on columns count not being equal to 1 or type mismatch.

void PerformAsVectorFieldTag(const Cluster& cluster) {
const auto vector_of_field_values =
cluster
.Execute(ClusterHostType::kPrimary, "SELECT value FROM SampleTable")
.AsVector<std::string>(kFieldTag);
static_assert(std::is_same_v<const std::vector<std::string>,
decltype(vector_of_field_values)>);
}

Definition at line 271 of file statement_result_set.hpp.

◆ MapFrom()

template<typename DbType >
MappedStatementResultSet< DbType > storages::mysql::StatementResultSet::MapFrom ( ) &&

Converts to an interface for on-the-flight mapping statement result set from DbType. DbType is expected to be an aggregate of supported types. See MySQL supported types for better understanding of DbType requirements.

struct SampleRow final {
std::int32_t id;
std::string value;
bool operator==(const SampleRow& other) const {
return id == other.id && value == other.value;
}
};
std::pair<const std::int32_t, std::string> Convert(
SampleRow&& db_row,
storages::mysql::convert::To<std::pair<const std::int32_t, std::string>>) {
return {db_row.id, std::move(db_row.value)};
}
void PerformMapFrom(const Cluster& cluster,
const std::vector<SampleRow>& data) {
cluster.ExecuteBulk(ClusterHostType::kPrimary,
"INSERT INTO SampleTable(id, value) VALUES(?, ?)", data);
const auto id_value_map =
cluster
.Execute(ClusterHostType::kPrimary,
"SELECT id, value FROM SampleTable")
.MapFrom<SampleRow>()
.AsContainer<std::unordered_map<std::int32_t, std::string>>();
static_assert(
std::is_same_v<const std::unordered_map<std::int32_t, std::string>,
decltype(id_value_map)>);
EXPECT_EQ(id_value_map.size(), data.size());
for (const auto& [k, v] : data) {
ASSERT_EQ(id_value_map.at(k), v);
}
}

Definition at line 397 of file statement_result_set.hpp.

Friends And Related Symbol Documentation

◆ CursorResultSet

template<typename T >
friend class CursorResultSet
friend

Definition at line 177 of file statement_result_set.hpp.

◆ MappedStatementResultSet

template<typename DbType >
friend class MappedStatementResultSet
friend

Definition at line 180 of file statement_result_set.hpp.


The documentation for this class was generated from the following file: