db_conn  v0.2.1-alpha
Database Connection API
connection.hpp
1 /*
2  * File: connection.hpp
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef CONNECTION_HPP
20 #define CONNECTION_HPP
21 
22 #include <memory>
23 #include "statement.hpp"
24 
25 // forward declaration
26 namespace vgi { namespace dbconn { namespace dbd {
27 
28  template <typename T> class driver;
29 
30 } } } // namespace vgi::dbconn::dbd
31 
32 namespace vgi { namespace dbconn { namespace dbi {
33 
39 {
40  virtual ~iconnection() { }
41  virtual bool connect() = 0;
42  virtual void disconnect() = 0;
43  virtual void commit() = 0;
44  virtual void rollback() = 0;
45  virtual void autocommit(bool ac) = 0;
46  virtual bool connected() const = 0;
47  virtual bool alive() const = 0;
48  virtual istatement* get_statement(iconnection&) = 0;
49 };
50 
51 
62 class connection {
63 
64 public:
69  connection(connection&& conn) : conn_impl(std::move(conn.conn_impl))
70  {
71  }
72 
79  {
80  if (this != &conn)
81  conn_impl = std::move(conn.conn_impl);
82  return *this;
83  }
84 
89  {
90  disconnect();
91  }
92 
97  bool connect()
98  {
99  return conn_impl->connect();
100  }
101 
105  void disconnect()
106  {
107  conn_impl->disconnect();
108  }
109 
114  void autocommit(bool ac)
115  {
116  return conn_impl->autocommit(ac);
117  }
118 
122  void commit()
123  {
124  conn_impl->commit();
125  }
126 
130  void rollback()
131  {
132  conn_impl->rollback();
133  }
134 
139  bool connected() const
140  {
141  return conn_impl->connected();
142  }
143 
148  bool alive() const
149  {
150  return conn_impl->alive();
151  }
152 
158  {
159  return statement(conn_impl->get_statement(*(conn_impl.get())));
160  }
161 
166  template <typename T>
167  explicit operator T&() const
168  {
169  return dynamic_cast<T&>(*conn_impl);
170  }
171 
172 
173 
174 private:
175  template <typename T> friend class dbd::driver;
176  friend class statement;
177 
178  connection(iconnection* conn) : conn_impl(conn) { }
179  connection(const connection&) = delete;
180  connection& operator=(const connection&) = delete;
181 
182 private:
183  std::unique_ptr<iconnection> conn_impl;
184 
185 }; // connection
186 
187 } } } // namespace vgi::dbconn::dbi
188 
189 #endif // CONNECTION_HPP
190 
void autocommit(bool ac)
Function sets auto-commit option, default is &#39;true&#39;.
Definition: connection.hpp:114
void disconnect()
Function closes connection to the database.
Definition: connection.hpp:105
statement - is a class that manages native driver statement handle.
Definition: statement.hpp:72
driver is a singleton template class which creates a concrete driver on load() and returns its instan...
Definition: connection.hpp:28
connection(connection &&conn)
Move constructor.
Definition: connection.hpp:69
statement get_statement()
Function returns statement object for the connection.
Definition: connection.hpp:157
STL namespace.
istatement - is an interface that describes common functionality for all concrete native implementati...
Definition: statement.hpp:32
iconnection - is an interface that describes common functionality for all concrete native implementat...
Definition: connection.hpp:38
bool alive() const
Function checks if connection to the database is alive.
Definition: connection.hpp:148
connection & operator=(connection &&conn)
Move assign operator.
Definition: connection.hpp:78
bool connect()
Function opens connection to the database.
Definition: connection.hpp:97
bool connected() const
Function checks if connection to the database is opened.
Definition: connection.hpp:139
connection - is a class that manages native driver connection handle.
Definition: connection.hpp:62
void commit()
Function commits changes to the database.
Definition: connection.hpp:122
void rollback()
Function rolls back uncommitted changes.
Definition: connection.hpp:130