Skip to content

Commit

Permalink
feat postgresql: Allow passing ParameterStore to QueryQueue
Browse files Browse the repository at this point in the history
Allow passing ParameterStore to QueryQueue
commit_hash:a1891874f17ffc8e179ce3d2f02bef9d062e220a
  • Loading branch information
ivsudarikov committed Jan 27, 2025
1 parent 598550b commit 7036a0a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions postgresql/include/userver/storages/postgres/query_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ USERVER_NAMESPACE_BEGIN

namespace storages::postgres {

class ParameterStore;

/// @brief A container to enqueue queries in FIFO order and execute them all
/// within a single network round-trip.
///
Expand Down Expand Up @@ -59,10 +61,12 @@ class QueryQueue final {
/// timeout for later execution.
template <typename... Args>
void Push(CommandControl cc, const Query& query, const Args&... args);
void Push(CommandControl cc, const Query& query, const ParameterStore& store);

/// Add a query into the queue with default command-control.
template <typename... Args>
void Push(const Query& query, const Args&... args);
void Push(const Query& query, const ParameterStore& store);

/// Collect results of all the queued queries, with specified timeout.
/// Either returns a vector of N `ResultSet`s, where N is the number of
Expand Down
6 changes: 6 additions & 0 deletions postgresql/src/storages/postgres/query_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ void QueryQueue::DoPush(CommandControl cc, const Query& query, ParamsHolder&& pa

void QueryQueue::ValidateUsage() const { UINVARIANT(conn_, "The query queue is finalized and no longer usable."); }

void QueryQueue::Push(CommandControl cc, const Query& query, const ParameterStore& store) {
DoPush(cc, query, ParamsHolder{{}, detail::QueryParameters(store.GetInternalData())});
}

void QueryQueue::Push(const Query& query, const ParameterStore& store) { Push(default_cc_, query, store); }

} // namespace storages::postgres

USERVER_NAMESPACE_END
22 changes: 22 additions & 0 deletions postgresql/src/storages/postgres/tests/query_queue_pgtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ UTEST_P(PostgreConnection, QueryQueueSelectMultiple) {
}
}

UTEST_P(PostgreConnection, QueryQueueParametersStore) {
CheckConnection(GetConn());
if (!GetConn()->IsPipelineActive()) {
return;
}

using RowTuple = std::tuple<int, std::string>;
const auto values = RowTuple{1, "str"};

pg::QueryQueue query_queue{kDefaultCC, std::move(GetConn())};
pg::ParameterStore store;
store.PushBack(std::get<0>(values));
store.PushBack(std::get<1>(values));

UEXPECT_NO_THROW(query_queue.Push(kDefaultCC, "SELECT $1, $2", store));
QueryQueueResult result{};
UEXPECT_NO_THROW(result = query_queue.Collect(kCollectTimeout));

ASSERT_EQ(1, result.size());
EXPECT_EQ(values, result.front().AsSingleRow<RowTuple>(pg::kRowTag));
}

UTEST_P(PostgreConnection, QueryQueueTimeout) {
CheckConnection(GetConn());
if (!GetConn()->IsPipelineActive()) {
Expand Down

0 comments on commit 7036a0a

Please sign in to comment.