sptk2 logo
SPTK Home Page
PostgreSQLConnection.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 <mutex>
30#include <sptk5/db/PoolDatabaseConnection.h>
31
32#ifdef HAVE_POSTGRESQL
33
34#include <libpq-fe.h>
35
36#ifndef _WIN32
37
38#include <list>
39#include <netinet/in.h>
40
41#endif
42
43namespace sptk {
44
50class PostgreSQLStatement;
51
57class SP_EXPORT PostgreSQLConnection
58 : public PoolDatabaseConnection
59{
60 friend class Query;
61
62public:
63 enum class TimestampFormat : uint8_t
64 {
65 UNKNOWN,
66 DOUBLE,
67 INT64
68 };
69
73 PGconn* connection()
74 {
75 return m_connect;
76 }
77
81 static void PostgreTypeToCType(PostgreSQLDataType postgreType, VariantDataType& dataType);
82
86 static void CTypeToPostgreType(VariantDataType dataType, PostgreSQLDataType& postgreType, const String& paramName);
87
92 void _openDatabase(const String& connectionString) override;
93
104 void _bulkInsert(const String& tableName, const Strings& columnNames,
105 const std::vector<VariantVector>& data) override;
106
115 void _executeBatchSQL(const sptk::Strings& batchSQL, Strings* errors) override;
116
126 explicit PostgreSQLConnection(const String& connectionString = "", std::chrono::seconds connectTimeout = std::chrono::seconds(60));
127
131 ~PostgreSQLConnection() override;
132
136 String nativeConnectionString() const override;
137
141 void closeDatabase() override;
142
146 bool active() const override;
147
151 DBHandle handle() const override;
152
156 String driverDescription() const override;
157
163 void objectList(DatabaseObjectType objectType, Strings& objects) override;
164
168 static std::map<PostgreSQLConnection*, std::shared_ptr<PostgreSQLConnection>> s_postgresqlConnections;
169
170protected:
171 static Strings extractStatements(const Strings& sqlBatch);
172
176 void driverBeginTransaction() override;
177
182 void driverEndTransaction(bool commit) override;
183
184 // These methods implement the actions requested by Query
185
189 String queryError(const Query* query) const override;
190
194 void queryAllocStmt(Query* query) override;
195
199 void queryFreeStmt(Query* query) override;
200
204 void queryCloseStmt(Query* query) override;
205
209 void queryPrepare(Query* query) override;
210
214 void queryExecute(Query* query) override
215 {
216 // Not needed for PG driver
217 }
218
222 int queryColCount(Query* query) override;
223
227 void queryBindParameters(Query* query) override;
228
232 void queryOpen(Query* query) override;
233
237 void queryFetch(Query* query) override;
238
245 String paramMark(unsigned paramIndex) override;
246
251 TimestampFormat timestampsFormat() const
252 {
253 return m_timestampsFormat;
254 }
255
256 void queryColAttributes(Query* query, int16_t column, int16_t descType, int32_t& value) override;
257 void queryColAttributes(Query* query, int16_t column, int16_t descType, char* buff, int len) override;
258 void queryExecDirect(const Query* query);
259
260private:
261 mutable std::mutex m_mutex;
262 PGconn* m_connect {nullptr};
263 TimestampFormat m_timestampsFormat {TimestampFormat::UNKNOWN};
264};
265
269} // namespace sptk
270
271#endif
272
273extern "C" {
274SP_DRIVER_EXPORT void* postgresql_create_connection(const char* connectionString, size_t connectionTimeoutSeconds);
275SP_DRIVER_EXPORT void postgresql_destroy_connection(void* connection);
276}
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