LCOV - code coverage report
Current view: top level - irohad/ametsuchi/impl - postgres_wsv_query.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 23 33 69.7 %
Date: 2019-03-07 14:46:43 Functions: 14 15 93.3 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #include "ametsuchi/impl/postgres_wsv_query.hpp"
       7             : 
       8             : #include <soci/boost-tuple.h>
       9             : #include "ametsuchi/impl/soci_utils.hpp"
      10             : #include "common/result.hpp"
      11             : #include "cryptography/public_key.hpp"
      12             : #include "logger/logger.hpp"
      13             : 
      14             : namespace iroha {
      15             :   namespace ametsuchi {
      16             : 
      17             :     using shared_model::interface::types::AccountIdType;
      18             :     using shared_model::interface::types::AddressType;
      19             :     using shared_model::interface::types::PubkeyType;
      20             : 
      21             :     PostgresWsvQuery::PostgresWsvQuery(
      22             :         soci::session &sql,
      23             :         std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory,
      24             :         logger::LoggerPtr log)
      25        1889 :         : sql_(sql), factory_(std::move(factory)), log_(std::move(log)) {}
      26             : 
      27             :     PostgresWsvQuery::PostgresWsvQuery(
      28             :         std::unique_ptr<soci::session> sql,
      29             :         std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory,
      30             :         logger::LoggerPtr log)
      31        4285 :         : psql_(std::move(sql)),
      32        4285 :           sql_(*psql_),
      33        4285 :           factory_(std::move(factory)),
      34        4285 :           log_(std::move(log)) {}
      35             : 
      36             :     template <typename T>
      37             :     boost::optional<std::shared_ptr<T>> PostgresWsvQuery::fromResult(
      38             :         shared_model::interface::CommonObjectsFactory::FactoryResult<
      39             :             std::unique_ptr<T>> &&result) {
      40       11954 :       return result.match(
      41             :           [](iroha::expected::Value<std::unique_ptr<T>> &v) {
      42       11954 :             return boost::make_optional(std::shared_ptr<T>(std::move(v.value)));
      43           0 :           },
      44             :           [&](iroha::expected::Error<std::string> &e)
      45             :               -> boost::optional<std::shared_ptr<T>> {
      46           0 :             log_->error(e.error);
      47           0 :             return boost::none;
      48             :           });
      49             :     }
      50             : 
      51             :     template <typename T, typename F>
      52             :     auto PostgresWsvQuery::execute(F &&f) -> boost::optional<soci::rowset<T>> {
      53             :       try {
      54        5554 :         return soci::rowset<T>{std::forward<F>(f)()};
      55           0 :       } catch (const std::exception &e) {
      56           0 :         log_->error("Failed to execute query: {}", e.what());
      57           0 :         return boost::none;
      58           0 :       }
      59        5554 :     }
      60             : 
      61             :     boost::optional<std::vector<PubkeyType>> PostgresWsvQuery::getSignatories(
      62             :         const AccountIdType &account_id) {
      63             :       using T = boost::tuple<std::string>;
      64             :       auto result = execute<T>([&] {
      65          15 :         return (sql_.prepare
      66          15 :                     << "SELECT public_key FROM account_has_signatory WHERE "
      67          15 :                        "account_id = :account_id",
      68          15 :                 soci::use(account_id));
      69           0 :       });
      70             : 
      71             :       return mapValues<std::vector<PubkeyType>>(result, [&](auto &public_key) {
      72          22 :         return shared_model::crypto::PublicKey{
      73          22 :             shared_model::crypto::Blob::fromHexString(public_key)};
      74           0 :       });
      75          15 :     }
      76             : 
      77             :     boost::optional<std::vector<std::shared_ptr<shared_model::interface::Peer>>>
      78             :     PostgresWsvQuery::getPeers() {
      79             :       using T = boost::tuple<std::string, AddressType>;
      80             :       auto result = execute<T>([&] {
      81        5554 :         return (sql_.prepare << "SELECT public_key, address FROM peer");
      82             :       });
      83             : 
      84        5554 :       return flatMapValues<
      85             :           std::vector<std::shared_ptr<shared_model::interface::Peer>>>(
      86             :           result, [&](auto &public_key, auto &address) {
      87       11954 :             return this->fromResult(factory_->createPeer(
      88       11954 :                 address,
      89       11954 :                 shared_model::crypto::PublicKey{
      90       11954 :                     shared_model::crypto::Blob::fromHexString(public_key)}));
      91           0 :           });
      92        5554 :     }
      93             :   }  // namespace ametsuchi
      94             : }  // namespace iroha

Generated by: LCOV version 1.13