sptk2 logo
SPTK Home Page
MySQLConnection.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/db/PoolDatabaseConnection.h>
30
31#ifdef HAVE_MYSQL
32
33#include <mutex>
34#include <sptk5/db/MySQLStatement.h>
35
36#ifdef _WIN32
37#define ULONG_CAST (unsigned long)
38#else
39#define ULONG_CAST
40#endif
41
42namespace sptk {
43
52class SP_EXPORT MySQLConnection
53 : public PoolDatabaseConnection
54{
55 friend class Query;
56
57 friend class MySQLStatement;
58
59public:
63 MYSQL* connection() const
64 {
65 return m_connection.get();
66 }
67
72 void _openDatabase(const String& connectionString) override;
73
82 void _executeBatchSQL(const sptk::Strings& batchSQL, Strings* errors) override;
83
93 explicit MySQLConnection(const String& connectionString = "", std::chrono::seconds connectTimeout = std::chrono::seconds(60));
94
95 MySQLConnection(const MySQLConnection&) = delete;
96
97 MySQLConnection(MySQLConnection&&) = delete;
98
99 MySQLConnection& operator=(const MySQLConnection&) = delete;
100
101 MySQLConnection& operator=(MySQLConnection&&) = delete;
102
103 ~MySQLConnection() override = default;
104
108 void closeDatabase() override;
109
113 bool active() const override;
114
118 DBHandle handle() const override;
119
123 String driverDescription() const override;
124
130 void objectList(DatabaseObjectType objectType, Strings& objects) override;
131
135 static std::map<MySQLConnection*, std::shared_ptr<MySQLConnection>> s_mysqlConnections;
136
137protected:
141 void driverBeginTransaction() override;
142
147 void driverEndTransaction(bool commit) override;
148
149 // These methods implement the actions requested by CQuery
153 String queryError(const Query* query) const override;
154
158 void queryAllocStmt(Query* query) override;
159
163 void queryFreeStmt(Query* query) override;
164
168 void queryCloseStmt(Query* query) override;
169
173 void queryPrepare(Query* query) override;
174
178 void queryExecute(Query* query) override;
179
183 int queryColCount(Query* query) override;
184
188 void queryBindParameters(Query* query) override;
189
193 void queryOpen(Query* query) override;
194
198 void queryFetch(Query* query) override;
199 void queryColAttributes(Query* query, int16_t column, int16_t descType, int32_t& value) override;
200 void queryColAttributes(Query* query, int16_t column, int16_t descType, char* buff, int len) override;
207 String paramMark(unsigned paramIndex) override;
208
209private:
210 std::shared_ptr<MYSQL> m_connection;
211 mutable std::mutex m_mutex;
212
216 void initConnection();
217
221 void executeCommand(const String& command);
222};
223
224#define throwMySQLException(info) throw DatabaseException(string(info) + ":" + string(mysql_error(m_connection.get())))
225
229} // namespace sptk
230
231#endif
232
233extern "C" {
234SP_DRIVER_EXPORT void* mysql_create_connection(const char* connectionString, size_t connectionTimeoutSeconds);
235SP_DRIVER_EXPORT void mysql_destroy_connection(void* connection);
236}
Definition: Strings.h:52
DatabaseObjectType
Definition: PoolDatabaseConnection.h:70

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