sptk2 logo
SPTK Home Page
SQLite3Connection.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/sptk.h>
30
31#ifdef HAVE_SQLITE3
32
33#include <sptk5/db/PoolDatabaseConnection.h>
34#include <sqlite3.h>
35
36namespace sptk {
47class SP_EXPORT SQLite3Connection
48 : public PoolDatabaseConnection
49{
50 friend class Query;
51
52public:
57 explicit SQLite3Connection(const String& connectionString = "", std::chrono::seconds connectTimeout = std::chrono::seconds(60));
58
62 ~SQLite3Connection() override = default;
63
67 String nativeConnectionString() const override;
68
72 void closeDatabase() override;
73
77 bool active() const override;
78
82 DBHandle handle() const override;
83
87 String driverDescription() const override;
88
94 void objectList(DatabaseObjectType objectType, Strings& objects) override;
95
99 static std::map<SQLite3Connection*, std::shared_ptr<SQLite3Connection>> s_sqlite3Connections;
100
101protected:
105 void driverBeginTransaction() override;
106
111 void driverEndTransaction(bool commit) override;
112
113 // These methods implement the actions requested by CQuery
117 String queryError(const Query* query) const override;
118
122 void queryAllocStmt(Query* query) override;
123
127 void queryFreeStmt(Query* query) override;
128
132 void queryCloseStmt(Query* query) override;
133
137 void queryPrepare(Query* query) override;
138
142 void queryExecute(Query* query) override;
143 void queryColAttributes(Query* query, int16_t column, int16_t descType, int32_t& value) override;
144 void queryColAttributes(Query* query, int16_t column, int16_t descType, char* buff, int len) override;
148 int queryColCount(Query* query) override;
149
153 void queryBindParameters(Query* query) override;
154
158 void queryOpen(Query* query) override;
159
163 void queryFetch(Query* query) override;
164
168 sqlite3* connection()
169 {
170 return m_connect.get();
171 }
172
177 void _openDatabase(const String& connectionString = "") override;
178
187 void _executeBatchSQL(const sptk::Strings& batchSQL, Strings* errors) override;
188
189private:
190 using SQLHSTMT = sqlite3_stmt*;
191 using SQLHDBC = sqlite3*;
192
193 mutable std::mutex m_mutex;
194 std::shared_ptr<sqlite3> m_connect;
195 void bindParameter(const Query* query, uint32_t paramNumber) const;
196
197 void closeAndClean();
198 int transformDateTimeParameter(sqlite3_stmt* stmt, QueryParameter* param, short paramBindNumber) const;
199};
200
204} // namespace sptk
205
206#endif
207
208extern "C" {
209SP_DRIVER_EXPORT [[maybe_unused]] void* sqlite3_create_connection(const char* connectionString, size_t connectionTimeoutSeconds);
210SP_DRIVER_EXPORT [[maybe_unused]] void sqlite3_destroy_connection(void* connection);
211}
Definition: Strings.h:52
DatabaseObjectType
Definition: PoolDatabaseConnection.h:70

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