sptk2 logo
SPTK Home Page
OracleConnection.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_ORACLE
33
34#include "DatabaseField.h"
35#include <sptk5/db/OracleEnvironment.h>
36#include <sptk5/db/OracleStatement.h>
37#include <src/drivers/Oracle/OracleBulkInsertQuery.h>
38
39namespace sptk {
40
46class OracleStatement;
47
48class OracleBulkInsertQuery;
49
55class SP_EXPORT OracleConnection
56 : public PoolDatabaseConnection
57{
58 friend class Query;
59
60 friend class OracleStatement;
61
62public:
63 using Environment = oracle::occi::Environment;
64 using Connection = oracle::occi::Connection;
65 using Statement = oracle::occi::Statement;
66 using Type = oracle::occi::Type;
67
71 Connection* connection() const
72 {
73 return m_connection.get();
74 }
75
79 Environment* environment() const
80 {
81 return m_environment.handle();
82 }
83
84 Statement* createStatement(const std::string& sql)
85 {
86 return m_connection->createStatement(sql);
87 }
88
93 void _openDatabase(const String& connectionString) override;
94
105 void _bulkInsert(const String& fullTableName, const Strings& columnNames,
106 const std::vector<VariantVector>& data) override;
107
116 void _executeBatchSQL(const Strings& batchSQL, Strings* errors) override;
117
122 explicit OracleConnection(const String& connectionString = "", std::chrono::seconds connectTimeout = std::chrono::seconds(60));
123
127 ~OracleConnection() override;
128
132 void closeDatabase() override;
133
137 bool active() const override;
138
142 DBHandle handle() const override;
143
147 String nativeConnectionString() const override;
148
152 String driverDescription() const override;
153
159 void objectList(DatabaseObjectType objectType, Strings& objects) override;
160
164 static std::map<OracleConnection*, std::shared_ptr<OracleConnection>> s_oracleConnections;
165
166protected:
170 void driverBeginTransaction() override;
171
176 void driverEndTransaction(bool commit) override;
177
178 // These methods implement the actions requested by CQuery
182 String queryError(const Query* query) const override;
183
187 void queryAllocStmt(Query* query) override;
188
192 void queryFreeStmt(Query* query) override;
193
197 void queryCloseStmt(Query* query) override;
198
202 void queryPrepare(Query* query) override;
203
207 void queryExecute(Query* query) override;
208
212 int queryColCount(Query* query) override;
213
217 void queryBindParameters(Query* query) override;
218
222 void queryOpen(Query* query) override;
223
224 void queryColAttributes(Query* query, int16_t column, int16_t descType, int32_t& value) override;
225 void queryColAttributes(Query* query, int16_t column, int16_t descType, char* buff, int len) override;
226
230 void queryFetch(Query* query) override;
231
238 String paramMark(unsigned paramIndex) override;
239
240private:
241 using SConnection = std::shared_ptr<Connection>;
242
243 mutable std::mutex m_mutex;
244 OracleEnvironment m_environment;
245 SConnection m_connection;
246 std::string m_lastError;
247
248 void executeMultipleStatements(const Strings& statements, Strings* errors);
249
250 static void setMaxParamSizes(const CParamVector& enumeratedParams, Statement* stmt,
251 const QueryColumnTypeSizeMap& columnTypeSizes);
252
253 static void createQueryFieldsFromMetadata(Query* query, oracle::occi::ResultSet* resultSet);
254
255 static void bulkInsertSingleRow(const Strings& columnNames, const QueryColumnTypeSizeVector& columnTypeSizeVector,
256 sptk::OracleBulkInsertQuery& insertQuery, const VariantVector& row);
257};
258
264VariantDataType OracleTypeToVariantType(OracleConnection::Type oracleType, int scale);
265
271OracleConnection::Type VariantTypeToOracleType(VariantDataType dataType);
272
273#define throwOracleException(description) \
274 { \
275 m_lastError = description; \
276 throwDatabaseException(m_lastError); \
277 }
278
282} // namespace sptk
283
284#endif
285
286extern "C" {
287[[maybe_unused]] SP_DRIVER_EXPORT void* oracle_create_connection(const char* connectionString, size_t connectionTimeoutSeconds);
288[[maybe_unused]] SP_DRIVER_EXPORT void oracle_destroy_connection(void* connection);
289}
DatabaseObjectType
Definition: PoolDatabaseConnection.h:70
std::map< std::string, QueryColumnTypeSize, std::less<> > QueryColumnTypeSizeMap
Definition: PoolDatabaseConnection.h:95
std::vector< SQueryParameter > CParamVector
Definition: QueryParameterList.h:47
std::vector< QueryColumnTypeSize > QueryColumnTypeSizeVector
Definition: PoolDatabaseConnection.h:90
VariantDataType
Definition: VariantData.h:44
std::vector< Variant > VariantVector
Definition: Variant.h:567

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