sptk2 logo
SPTK Home Page
AutoDatabaseConnection.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 <memory>
30#include <sptk5/db/PoolDatabaseConnection.h>
31
32namespace sptk {
33
38class DatabaseConnectionPool;
39
44{
48 DatabaseConnectionPool& m_connectionPool;
49
53 SPoolDatabaseConnection m_connection {nullptr};
54
55public:
62
65
71
72 AutoDatabaseConnection& operator=(const AutoDatabaseConnection&) = delete;
74
79
86 void open(const String& connectionString = "") const
87 {
88 if (!m_connection)
89 throw Exception("DB driver not loaded");
90
91 m_connection->open(connectionString);
92 }
93
97 void close() const
98 {
99 m_connection->close();
100 }
101
105 bool active() const
106 {
107 return m_connection != nullptr && m_connection->active();
108 }
109
114 {
115 return m_connection->connectionString();
116 }
117
122 {
123 return m_connection->connectionType();
124 }
125
130 {
131 return m_connection->driverDescription();
132 }
133
137 void beginTransaction() const
138 {
139 m_connection->beginTransaction();
140 }
141
145 void commitTransaction() const
146 {
147 m_connection->commitTransaction();
148 }
149
154 {
155 m_connection->rollbackTransaction();
156 }
157
161 int inTransaction() const
162 {
163 return m_connection->inTransaction();
164 }
165
174 void objectList(DatabaseObjectType objectType, Strings& objects) const
175 {
176 m_connection->objectList(objectType, objects);
177 }
178
189 void bulkInsert(const String& tableName, const Strings& columnNames, const std::vector<VariantVector>& data) const
190 {
191 m_connection->bulkInsert(tableName, columnNames, data);
192 }
193
202 void executeBatchFile(const String& batchFileName, Strings* errors = nullptr) const
203 {
204 m_connection->executeBatchFile(batchFileName, errors);
205 }
206
215 void executeBatchSQL(const sptk::Strings& batchSQL, Strings* errors = nullptr) const
216 {
217 m_connection->executeBatchSQL(batchSQL, errors);
218 }
219};
220
221using DatabaseConnection = std::shared_ptr<AutoDatabaseConnection>;
222
226} // namespace sptk
Definition: AutoDatabaseConnection.h:44
PoolDatabaseConnection * connection()
bool active() const
Definition: AutoDatabaseConnection.h:105
String driverDescription() const
Definition: AutoDatabaseConnection.h:129
void executeBatchFile(const String &batchFileName, Strings *errors=nullptr) const
Definition: AutoDatabaseConnection.h:202
void commitTransaction() const
Definition: AutoDatabaseConnection.h:145
void open(const String &connectionString="") const
Definition: AutoDatabaseConnection.h:86
const DatabaseConnectionString & connectionString() const
Definition: AutoDatabaseConnection.h:113
void objectList(DatabaseObjectType objectType, Strings &objects) const
Definition: AutoDatabaseConnection.h:174
int inTransaction() const
Definition: AutoDatabaseConnection.h:161
void close() const
Definition: AutoDatabaseConnection.h:97
DatabaseConnectionType connectionType() const
Definition: AutoDatabaseConnection.h:121
void executeBatchSQL(const sptk::Strings &batchSQL, Strings *errors=nullptr) const
Definition: AutoDatabaseConnection.h:215
void rollbackTransaction() const
Definition: AutoDatabaseConnection.h:153
AutoDatabaseConnection(DatabaseConnectionPool &connectionPool)
void beginTransaction() const
Definition: AutoDatabaseConnection.h:137
void bulkInsert(const String &tableName, const Strings &columnNames, const std::vector< VariantVector > &data) const
Definition: AutoDatabaseConnection.h:189
Definition: DatabaseConnectionPool.h:96
Definition: DatabaseConnectionString.h:56
SPTK generic exception class.
Definition: Exception.h:56
Definition: PoolDatabaseConnection.h:231
Definition: String.h:49
Definition: Strings.h:52
DatabaseObjectType
Definition: PoolDatabaseConnection.h:70
DatabaseConnectionType
Definition: PoolDatabaseConnection.h:55

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