sptk2 logo
SPTK Home Page
ODBCConnection.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 <array>
34#include <sptk5/db/DatabaseField.h>
35#include <sptk5/db/ODBCEnvironment.h>
36#include <sptk5/db/PoolDatabaseConnection.h>
37
38namespace sptk {
39
50class SP_DRIVER_EXPORT ODBCConnection
51 : public PoolDatabaseConnection
52{
53 friend class Query;
54
55public:
60 explicit ODBCConnection(const String& connectionString = "", std::chrono::seconds connectTimeout = std::chrono::seconds(60));
61
62 ODBCConnection(const ODBCConnection&) = delete;
63
64 ODBCConnection(ODBCConnection&&) noexcept = default;
65
69 ~ODBCConnection() override
70 {
71 close();
72 }
73
74 ODBCConnection& operator=(const ODBCConnection&) = delete;
75
76 ODBCConnection& operator=(ODBCConnection&&) noexcept = default;
77
81 String nativeConnectionString() const override;
82
86 void closeDatabase() override;
87
91 bool active() const override;
92
96 DBHandle handle() const override;
97
101 String driverDescription() const override;
102
108 void objectList(DatabaseObjectType objectType, Strings& objects) override;
109
113 static std::map<ODBCConnection*, std::shared_ptr<ODBCConnection>> s_odbcConnections;
114
115protected:
119 void driverBeginTransaction() override;
120
125 void driverEndTransaction(bool commit) override;
126
127 // These methods implement the actions requested by CQuery
131 String queryError(const Query* query) const override;
132
136 void queryAllocStmt(Query* query) override;
137
141 void queryFreeStmt(Query* query) override;
142
146 void queryCloseStmt(Query* query) override;
147
151 void queryPrepare(Query* query) override;
152
156 void queryExecute(Query* query) override;
157
161 int queryColCount(Query* query) override;
162
166 void queryColAttributes(Query* query, int16_t column, int16_t descType, int32_t& value) override;
167
171 void queryColAttributes(Query* query, int16_t column, int16_t descType, char* buff, int len) override;
172
176 void queryBindParameters(Query* query) override;
177
181 void queryOpen(Query* query) override;
182
186 void queryFetch(Query* query) override;
187
191 static void ODBCtypeToCType(int odbcType, int32_t& ctype, VariantDataType& dataType);
192
196 ODBCConnectionBase* connection()
197 {
198 return m_connect.get();
199 }
200
205 static void listDataSources(Strings& dsns);
206
211 void _openDatabase(const String& connectionString) override;
212
221 void _executeBatchSQL(const sptk::Strings& batchSQL, Strings* errors) override;
222
223 void _bulkInsert(const String& tableName, const Strings& columnNames,
224 const std::vector<VariantVector>& data) override;
225
226private:
227 static constexpr size_t MAX_NAME_LEN = 256;
228
232 std::shared_ptr<ODBCConnectionBase> m_connect {std::make_shared<ODBCConnectionBase>()};
233
238 String queryError(SQLHSTMT stmt) const;
239
245 void parseColumns(Query* query, int count);
246
247 SQLHSTMT makeObjectListStatement(const DatabaseObjectType& objectType, sptk::Buffer& objectSchema, sptk::Buffer& objectName, short& procedureType) const;
248};
252} // namespace sptk
253#endif
254
255extern "C" {
256SP_DRIVER_EXPORT [[maybe_unused]] void* odbc_create_connection(const char* connectionString, size_t connectionTimeoutSeconds);
257SP_DRIVER_EXPORT [[maybe_unused]] void odbc_destroy_connection(void* connection);
258}
Definition: Buffer.h:51
Definition: Strings.h:52
DatabaseObjectType
Definition: PoolDatabaseConnection.h:70
VariantDataType
Definition: VariantData.h:44

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