sptk2 logo
SPTK Home Page
PoolDatabaseConnection.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/Logger.h>
30#include <sptk5/Strings.h>
31#include <sptk5/Variant.h>
32#include <sptk5/db/DatabaseConnectionString.h>
33
34#include <list>
35#include <set>
36#include <vector>
37
38namespace sptk {
39
45class Query;
46
47using DBHandle = uint8_t*;
48using StmtHandle = uint8_t*;
49using SStmtHandle = std::shared_ptr<uint8_t>;
50
54enum class DatabaseConnectionType : uint16_t
55{
56 UNKNOWN = 0,
57 MYSQL = 1,
58 ORACLE = 2,
59 POSTGRES = 4,
60 SQLITE3 = 8,
61 FIREBIRD = 16,
62 GENERIC_ODBC = 32,
63 MSSQL_ODBC = 64
64};
65
69enum class DatabaseObjectType : uint8_t
70{
71 UNDEFINED,
72 TABLES,
73 VIEWS,
75 FUNCTIONS,
77};
78
84 size_t length;
85};
86
90using QueryColumnTypeSizeVector = std::vector<QueryColumnTypeSize>;
91
95using QueryColumnTypeSizeMap = std::map<std::string, QueryColumnTypeSize, std::less<>>;
96
98{
99 friend class Query;
100
101 friend class QueryStatementManagement;
102
103public:
104 virtual ~PoolDatabaseConnectionQueryMethods() = default;
105
106protected:
110 void querySetStmt(Query* q, SStmtHandle stmt);
111
115 static void querySetPrepared(Query* q, bool pf);
116
120 static void querySetActive(Query* q, bool af);
121
125 static void querySetEof(Query* q, bool eof);
126
127 // These methods implement the actions requested by CQuery
131 virtual String queryError(const Query* query) const = 0;
132
136 virtual void queryAllocStmt(Query* query) = 0;
137
141 virtual void queryFreeStmt(Query* query) = 0;
142
146 virtual void queryCloseStmt(Query* query) = 0;
147
151 virtual void queryPrepare(Query* query) = 0;
152
156 virtual void queryExecute(Query* query) = 0;
157
161 virtual int queryColCount(Query* query) = 0;
162
166 virtual void queryColAttributes(Query* query, int16_t column, int16_t descType, int32_t& value) = 0;
167
171 virtual void queryColAttributes(Query* query, int16_t column, int16_t descType, char* buff, int len) = 0;
172
176 virtual void queryBindParameters(Query* query) = 0;
177
181 virtual void queryOpen(Query* query) = 0;
182
186 virtual void queryFetch(Query* query) = 0;
187
194 virtual String paramMark(unsigned paramIndex);
195
200 [[noreturn]] void notImplemented(const String& methodName) const;
201
205 bool linkQuery(Query* q);
206
211
218
219private:
220 std::map<Query*, SStmtHandle> m_queryList;
221};
222
231{
232 friend class Query;
233
234 friend class QueryStatementManagement;
235
236public:
243 void open(const String& connectionString = "");
244
248 void close();
249
253 virtual bool active() const;
254
258 virtual DBHandle handle() const;
259
264 {
265 return m_connString;
266 }
267
272 void connectionString(const DatabaseConnectionString& connectionString);
273
278 {
279 return "";
280 }
281
286 {
287 return m_connType;
288 }
289
293 virtual String driverDescription() const
294 {
295 return m_driverDescription;
296 }
297
302
307
312
316 bool inTransaction() const
317 {
318 return m_inTransaction;
319 }
320
329 virtual void objectList(DatabaseObjectType objectType, Strings& objects) = 0;
330
341 void bulkInsert(const String& tableName, const Strings& columnNames, const std::vector<VariantVector>& data)
342 {
343 _bulkInsert(tableName, columnNames, data);
344 }
345
354 void executeBatchFile(const String& batchFileName, Strings* errors = nullptr)
355 {
356 _executeBatchFile(batchFileName, errors);
357 }
358
367 void executeBatchSQL(const sptk::Strings& batchSQL, Strings* errors = nullptr)
368 {
369 _executeBatchSQL(batchSQL, errors);
370 }
371
372protected:
373 bool getInTransaction() const;
374
375 void setInTransaction(bool inTransaction);
376
385 explicit PoolDatabaseConnection(const String& connectionString, DatabaseConnectionType connectionType, std::chrono::seconds connectTimeout);
386
388
390
391 PoolDatabaseConnection& operator=(const PoolDatabaseConnection&) = delete;
392
393 PoolDatabaseConnection& operator=(PoolDatabaseConnection&&) noexcept = default;
394
395 ~PoolDatabaseConnection() override;
396
403 virtual void _openDatabase(const String& connectionString);
404
410 virtual void closeDatabase();
411
417 virtual void driverBeginTransaction();
418
425 virtual void driverEndTransaction(bool commit);
426
434 [[noreturn]] static void logAndThrow(const String& method, const String& error);
435
446 virtual void _bulkInsert(const String& tableName, const Strings& columnNames,
447 const std::vector<VariantVector>& data);
448
457 virtual void _executeBatchFile(const String& batchFileName, Strings* errors);
458
467 virtual void _executeBatchSQL(const sptk::Strings& batchSQL, Strings* errors);
468
472 virtual void connectionType(DatabaseConnectionType connType)
473 {
474 m_connType = connType;
475 }
476
484 void bulkInsertRecords(const String& tableName, const Strings& columnNames,
485 const std::vector<VariantVector>::const_iterator& begin,
486 const std::vector<VariantVector>::const_iterator& end);
487
492 std::chrono::seconds connectTimout() const
493 {
494 return m_connectionTimeout;
495 }
496
497private:
498 DatabaseConnectionString m_connString;
499 DatabaseConnectionType m_connType;
500 String m_driverDescription;
501 bool m_inTransaction {false};
502 std::chrono::seconds m_connectionTimeout;
503};
504
505using SPoolDatabaseConnection = std::shared_ptr<PoolDatabaseConnection>;
506
513SP_EXPORT String escapeSQLString(const String& str, bool tsv = false);
514
518} // namespace sptk
Definition: DatabaseConnectionString.h:56
Definition: PoolDatabaseConnection.h:98
virtual String paramMark(unsigned paramIndex)
static void querySetEof(Query *q, bool eof)
virtual void queryFetch(Query *query)=0
virtual int queryColCount(Query *query)=0
static void querySetActive(Query *q, bool af)
virtual void queryPrepare(Query *query)=0
virtual void queryBindParameters(Query *query)=0
void notImplemented(const String &methodName) const
virtual void queryCloseStmt(Query *query)=0
virtual void queryExecute(Query *query)=0
virtual void queryOpen(Query *query)=0
virtual void queryFreeStmt(Query *query)=0
void querySetStmt(Query *q, SStmtHandle stmt)
virtual void queryAllocStmt(Query *query)=0
virtual void queryColAttributes(Query *query, int16_t column, int16_t descType, char *buff, int len)=0
virtual String queryError(const Query *query) const =0
virtual void queryColAttributes(Query *query, int16_t column, int16_t descType, int32_t &value)=0
static void querySetPrepared(Query *q, bool pf)
Definition: PoolDatabaseConnection.h:231
void bulkInsert(const String &tableName, const Strings &columnNames, const std::vector< VariantVector > &data)
Definition: PoolDatabaseConnection.h:341
virtual String driverDescription() const
Definition: PoolDatabaseConnection.h:293
virtual String nativeConnectionString() const
Definition: PoolDatabaseConnection.h:277
void bulkInsertRecords(const String &tableName, const Strings &columnNames, const std::vector< VariantVector >::const_iterator &begin, const std::vector< VariantVector >::const_iterator &end)
const DatabaseConnectionString & connectionString() const
Definition: PoolDatabaseConnection.h:263
PoolDatabaseConnection(const String &connectionString, DatabaseConnectionType connectionType, std::chrono::seconds connectTimeout)
virtual DatabaseConnectionType connectionType() const
Definition: PoolDatabaseConnection.h:285
void executeBatchFile(const String &batchFileName, Strings *errors=nullptr)
Definition: PoolDatabaseConnection.h:354
virtual void objectList(DatabaseObjectType objectType, Strings &objects)=0
virtual DBHandle handle() const
virtual bool active() const
std::chrono::seconds connectTimout() const
Definition: PoolDatabaseConnection.h:492
void connectionString(const DatabaseConnectionString &connectionString)
void executeBatchSQL(const sptk::Strings &batchSQL, Strings *errors=nullptr)
Definition: PoolDatabaseConnection.h:367
void open(const String &connectionString="")
bool inTransaction() const
Definition: PoolDatabaseConnection.h:316
Definition: Query.h:51
Definition: Query.h:239
Definition: String.h:49
Definition: Strings.h:52
DatabaseObjectType
Definition: PoolDatabaseConnection.h:70
SP_EXPORT String escapeSQLString(const String &str, bool tsv=false)
DatabaseConnectionType
Definition: PoolDatabaseConnection.h:55
std::map< std::string, QueryColumnTypeSize, std::less<> > QueryColumnTypeSizeMap
Definition: PoolDatabaseConnection.h:95
std::vector< QueryColumnTypeSize > QueryColumnTypeSizeVector
Definition: PoolDatabaseConnection.h:90
@ FUNCTIONS
Stored functions.
@ DATABASES
Available databases.
@ PROCEDURES
Stored procedures.
VariantDataType
Definition: VariantData.h:44
std::vector< Variant > VariantVector
Definition: Variant.h:567
Definition: PoolDatabaseConnection.h:82
size_t length
Column data size.
Definition: PoolDatabaseConnection.h:84
VariantDataType type
Column type.
Definition: PoolDatabaseConnection.h:83

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