sptk2 logo
SPTK Home Page
ODBCEnvironment.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/sptk.h>
30
31#ifdef HAVE_ODBC
32
33#include <sptk5/Strings.h>
34
35#ifdef WIN32
36#include <sys/types.h>
37#include <time.h>
38#include <winsock2.h>
39
40#include <windows.h>
41#else
42#define LPCVOID const void*
43#endif
44
45#include <cassert>
46#include <sqlext.h>
47
48#include <mutex>
49#include <sptk5/db/QueryParameterList.h>
50
51namespace sptk {
52
58class ODBCEnvironment;
59
60class ODBCConnectionBase;
61
67class SP_DRIVER_EXPORT ODBCBase
68 : public std::mutex
69{
70 friend class ODBCConnection;
71
72public:
73 static constexpr const char* cantSetConnectOption = "Can't set connect option";
74 static constexpr const char* cantEndTranscation = "Can't end transaction";
75 static constexpr const char* cantGetInformation = "Can't get connect information";
76
80 ~ODBCBase() = default;
81
85 [[noreturn]] static void exception(const String& text, int line);
86
87protected:
91 ODBCBase() = default;
92
93private:
97 ODBCBase& operator=(const ODBCBase& d);
98
102 ODBCBase(const ODBCBase&);
103};
104
110class SP_DRIVER_EXPORT ODBCEnvironment
111 : public ODBCBase
112{
113 friend class ODBCConnectionBase;
114
115public:
119 SQLHENV handle() const
120 {
121 return m_hEnvironment.get();
122 }
123
124protected:
128 ODBCEnvironment() = default;
129
133 void allocEnv();
134
138 bool valid() const
139 {
140 return m_hEnvironment != nullptr;
141 }
142
143private:
147 std::shared_ptr<uint8_t> m_hEnvironment;
148};
149
155class SP_DRIVER_EXPORT ODBCConnectionBase
156 : public ODBCBase
157{
158public:
162 void allocConnect();
163
167 void freeConnect();
168
173 void connect(const String& ConnectionString, String& FinalConnectionString, bool EnableDriverPrompt = false);
174
178 void disconnect();
179
183 SQLHDBC handle() const
184 {
185 return (SQLHDBC) m_hConnection.get();
186 }
187
191 bool isConnected()
192 {
193 std::scoped_lock lock(*this);
194 return m_connected;
195 }
196
200 void setConnectOption(UWORD fOption, UDWORD vParam);
201
205 String connectString()
206 {
207 std::scoped_lock lock(*this);
208 return m_connectString;
209 }
210
214 String driverDescription()
215 {
216 std::scoped_lock lock(*this);
217 return m_driverDescription;
218 }
219
223 void beginTransaction();
224
228 void transact(uint16_t fType);
229
233 void commit()
234 {
235 transact(SQL_COMMIT);
236 }
237
241 void rollback()
242 {
243 transact(SQL_ROLLBACK);
244 }
245
249 static ODBCEnvironment& getEnvironment()
250 {
251 return m_env;
252 }
253
258 String errorInformation(const char* action) const;
259
260protected:
264 bool valid() const
265 {
266 return m_hConnection != SQL_NULL_HDBC;
267 }
268
273 void execQuery(const char* query);
274
275private:
276 ODBCEnvironment& m_cEnvironment {getEnvironment()};
277 std::shared_ptr<uint8_t> m_hConnection;
278 bool m_connected {false};
279 String m_connectString;
280 String m_driverDescription;
281 static ODBCEnvironment m_env;
282};
283
287String removeDriverIdentification(const char* error);
288
292} // namespace sptk
293#endif

Fri Oct 14 2022 09:58:31: SPTK 5.4.1