pg_async
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
database.hpp
Go to the documentation of this file.
1 
8 #ifndef TIP_DB_PG_DATABASE_HPP_
9 #define TIP_DB_PG_DATABASE_HPP_
10 
11 #include <string>
12 #include <memory>
13 #include <functional>
14 #include <map>
15 #include <boost/asio.hpp>
16 #include <boost/system/error_code.hpp>
17 #include <boost/optional.hpp>
18 
19 #include <tip/db/pg/common.hpp>
20 
21 namespace tip {
22 namespace db {
23 namespace pg {
24 
25 namespace detail {
26 struct database_impl;
27 }
57 class db_service {
58 public:
60  typedef boost::system::error_code error_code;
61 
62  typedef std::map< std::string, std::string > connection_params;
63  typedef boost::optional<size_t> optional_size;
64 public:
65  static const size_t DEFAULT_POOOL_SIZE = 4;
66 public:
73  static void
74  initialize(size_t pool_size, connection_params const& defaults);
75 
86  static void
87  add_connection(std::string const& connection_string,
88  optional_size pool_size = optional_size());
89 
109  static void
110  begin(dbalias const&, transaction_callback const&,
111  error_callback const&);
112 
113  static void
114  run();
115  static void
116  stop();
117 
118  static boost::asio::io_service&
119  io_service();
120 private:
121  // No instances
122  db_service() {}
123 
124  typedef std::shared_ptr<detail::database_impl> pimpl;
125  static pimpl pimpl_;
126 
127  static pimpl
128  impl(size_t pool_size = DEFAULT_POOOL_SIZE,
129  connection_params const& defaults = {});
130 };
131 
132 } // namespace pg
133 } // namespace db
134 } // namespace tip
135 
136 #endif /* TIP_DB_PG_DATABASE_HPP_ */
static void initialize(size_t pool_size, connection_params const &defaults)
Initialize the database service with the default pool_size per alias and default connection parameter...
static const size_t DEFAULT_POOOL_SIZE
Definition: database.hpp:65
static boost::asio::io_service & io_service()
Short unique string to refer a database. Signature structure, to pass instead of connection string...
Definition: common.hpp:209
std::function< void(error::db_error const &) > error_callback
Callback for error handling.
Definition: common.hpp:350
boost::optional< size_t > optional_size
Definition: database.hpp:63
static void begin(dbalias const &, transaction_callback const &, error_callback const &)
Create a connection or retrieve a connection from the connection pool and start a transaction...
boost::system::error_code error_code
Definition: database.hpp:60
static void add_connection(std::string const &connection_string, optional_size pool_size=optional_size())
Add a connection specification.
std::map< std::string, std::string > connection_params
Definition: database.hpp:62
std::function< void(transaction_ptr) > transaction_callback
Callback for starting a transaction.
Definition: common.hpp:352