-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.hpp
190 lines (160 loc) · 4.57 KB
/
connection.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* File: connection.hpp
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef CONNECTION_HPP
#define CONNECTION_HPP
#include <memory>
#include "statement.hpp"
// forward declaration
namespace vgi { namespace dbconn { namespace dbd {
template <typename T> class driver;
} } } // namespace vgi::dbconn::dbd
namespace vgi { namespace dbconn { namespace dbi {
/**
* iconnection - is an interface that describes common functionality for all
* concrete native implementations for a connection class
*/
struct iconnection
{
virtual ~iconnection() { }
virtual bool connect() = 0;
virtual void disconnect() = 0;
virtual void commit() = 0;
virtual void rollback() = 0;
virtual void autocommit(bool ac) = 0;
virtual bool connected() const = 0;
virtual bool alive() const = 0;
virtual istatement* get_statement(iconnection&) = 0;
};
/**
* connection - is a class that manages native driver connection handle.
* connection object cannot be instantiated directly, only via driver
* get_connection() function call. connection objects automatically delete the
* native connection handle they manage as soon as they themselves are destroyed,
* or as soon as their value changes by an assignment operation. When an
* assignment operation takes place between two connection objects, ownership of
* native connection handle is transferred, which means that the object losing
* ownership is no longer has access to the native connection handle.
*/
class connection {
public:
/**
* Move constructor
* @param conn
*/
connection(connection&& conn) : conn_impl(std::move(conn.conn_impl))
{
}
/**
* Move assign operator
* @param conn
* @return
*/
connection& operator=(connection&& conn)
{
if (this != &conn)
conn_impl = std::move(conn.conn_impl);
return *this;
}
/**
* Destructor
*/
~connection()
{
disconnect();
}
/**
* Function opens connection to the database
* @return true on success, false otherwise
*/
bool connect()
{
return conn_impl->connect();
}
/**
* Function closes connection to the database
*/
void disconnect()
{
conn_impl->disconnect();
}
/**
* Function sets auto-commit option, default is 'true'
* @param ac - true to set auto-commit ON, false - OFF
*/
void autocommit(bool ac)
{
return conn_impl->autocommit(ac);
}
/**
* Function commits changes to the database
*/
void commit()
{
conn_impl->commit();
}
/**
* Function rolls back uncommitted changes
*/
void rollback()
{
conn_impl->rollback();
}
/**
* Function checks if connection to the database is opened
* @return true on success, false otherwise
*/
bool connected() const
{
return conn_impl->connected();
}
/**
* Function checks if connection to the database is alive
* @return true on success, false otherwise
*/
bool alive() const
{
return conn_impl->alive();
}
/**
* Function returns statement object for the connection
* @return
*/
statement get_statement()
{
return statement(conn_impl->get_statement(*(conn_impl.get())));
}
/**
* Conversion operator to the concrete database connection implementation
* @return
*/
template <typename T>
explicit operator T&() const
{
return dynamic_cast<T&>(*conn_impl);
}
private:
template <typename T> friend class dbd::driver;
friend class statement;
connection(iconnection* conn) : conn_impl(conn) { }
connection(const connection&) = delete;
connection& operator=(const connection&) = delete;
private:
std::unique_ptr<iconnection> conn_impl;
}; // connection
} } } // namespace vgi::dbconn::dbi
#endif // CONNECTION_HPP