hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
pg_connection_init.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PG_CONNECTION_INIT_HPP
7 #define IROHA_PG_CONNECTION_INIT_HPP
8 
9 #include <soci/soci.h>
10 
11 #include <soci/callbacks.h>
12 #include <soci/postgresql/soci-postgresql.h>
13 #include <boost/algorithm/string.hpp>
14 #include <boost/range/algorithm/replace_if.hpp>
15 
20 #include "common/result.hpp"
22 #include "logger/logger_fwd.hpp"
24 
25 namespace iroha {
26  namespace ametsuchi {
27 
28  struct PoolWrapper;
29 
31  public:
32  static expected::Result<std::shared_ptr<soci::connection_pool>,
33  std::string>
34  initPostgresConnection(std::string &options_str, size_t pool_size);
35 
36  static expected::Result<std::shared_ptr<PoolWrapper>, std::string>
38  const ReconnectionStrategyFactory &reconnection_strategy_factory,
39  const PostgresOptions &options,
40  const int pool_size,
41  logger::LoggerManagerTreePtr log_manager);
42 
46  static bool preparedTransactionsAvailable(soci::session &sql);
47 
48  static iroha::expected::Result<void, std::string> rollbackPrepared(
49  soci::session &sql, const std::string &prepared_block_name);
50 
51  /*
52  * Check whether working database exists.
53  * @param pg_opt Database options.
54  * @return Result of bool that is true if the database exists and false
55  * otherwise, or error message if check has failed.
56  */
57  static expected::Result<bool, std::string> checkIfWorkingDatabaseExists(
58  const PostgresOptions &pg_opt);
59 
60  /*
61  * Create working database if it does not exist.
62  * @param pg_opt Database options.
63  * @return Result of bool that is true if the database was creates and
64  * false otherwise, or error message if something has gone wrong.
65  */
66  static expected::Result<bool, std::string> createDatabaseIfNotExist(
67  const PostgresOptions &pg_opt);
68 
69  /*
70  * Remove all records from the tables
71  * @return error message if reset has failed
72  */
73  static expected::Result<void, std::string> resetWsv(soci::session &sql);
74 
75  /*
76  * Drop working database.
77  * @return Error message if dropping has failed.
78  */
79  static expected::Result<void, std::string> dropWorkingDatabase(
80  const PostgresOptions &options);
81 
86  static expected::Result<void, std::string> resetPeers(soci::session &sql);
87 
89  static void prepareTables(soci::session &session);
90 
91  private:
106  template <typename RollbackFunction>
107  static void initializeConnectionPool(
108  soci::connection_pool &connection_pool,
109  size_t pool_size,
110  RollbackFunction try_rollback,
111  FailoverCallbackHolder &callback_factory,
112  const ReconnectionStrategyFactory &reconnection_strategy_factory,
113  const std::string &pg_reconnection_options,
114  logger::LoggerManagerTreePtr log_manager);
115  };
116  } // namespace ametsuchi
117 } // namespace iroha
118 
119 #endif // IROHA_PG_CONNECTION_INIT_HPP
Definition: postgres_options.hpp:19
static bool preparedTransactionsAvailable(soci::session &sql)
Definition: pg_connection_init.cpp:95
Definition: reconnection_strategy.hpp:35
static expected::Result< bool, std::string > checkIfWorkingDatabaseExists(const PostgresOptions &pg_opt)
Definition: pg_connection_init.cpp:116
Definition: pg_connection_init.hpp:30
static expected::Result< std::shared_ptr< PoolWrapper >, std::string > prepareConnectionPool(const ReconnectionStrategyFactory &reconnection_strategy_factory, const PostgresOptions &options, const int pool_size, logger::LoggerManagerTreePtr log_manager)
Definition: pg_connection_init.cpp:44
Definition: failover_callback_holder.hpp:13
static expected::Result< void, std::string > resetWsv(soci::session &sql)
Definition: pg_connection_init.cpp:323
Definition: block_query.hpp:14
std::shared_ptr< LoggerManagerTree > LoggerManagerTreePtr
Definition: logger_manager_fwd.hpp:14
static void prepareTables(soci::session &session)
Create tables in the given session. Left public for tests.
Definition: pg_connection_init.cpp:217
static expected::Result< std::shared_ptr< soci::connection_pool >, std::string > initPostgresConnection(std::string &options_str, size_t pool_size)
Definition: pg_connection_init.cpp:28
static iroha::expected::Result< void, std::string > rollbackPrepared(soci::session &sql, const std::string &prepared_block_name)
Definition: pg_connection_init.cpp:105
static expected::Result< void, std::string > dropWorkingDatabase(const PostgresOptions &options)
Definition: pg_connection_init.cpp:353
static expected::Result< bool, std::string > createDatabaseIfNotExist(const PostgresOptions &pg_opt)
Definition: pg_connection_init.cpp:137
static expected::Result< void, std::string > resetPeers(soci::session &sql)
Definition: pg_connection_init.cpp:366