IConnection.h
1/* Copyright (C) 2006-2026 by Tommi Maekitalo
2 Copyright (C) 2006-2026 by Marc Boris Duerner
3 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
4*/
5
6#ifndef PT_DB_ICONNECTION_H
7#define PT_DB_ICONNECTION_H
8
9#include <Pt/Db/Api.h>
10#include <Pt/RefCounted.h>
11#include <Pt/SmartPtr.h>
12#include <Pt/Signal.h>
13#include <Pt/Db/ICursor.h>
14#include <Pt/Db/Cursor.h>
15#include <Pt/Db/IStatement.h>
16#include <Pt/Db/Result.h>
17#include <Pt/Db/Row.h>
18#include <Pt/Db/Value.h>
19
20#include <string>
21#include <map>
22
23namespace Pt {
24
25namespace System {
26class EventLoop;
27}
28
29namespace Db {
30
31class Result;
32class Row;
33class Value;
34class Statement;
35class Transaction;
36
43class PT_DB_API IConnection : public RefCounted
44{
45 public:
46 typedef std::size_t size_type;
47
48
52
55 bool ping();
56
59 void beginPing();
60
65 bool endPing();
66
70 { return _pingFinished; }
71
77 long long lastInsertId(const std::string& name);
78
81 bool isIdle() const
82 { return _pendingOp == nullptr; }
83
86 bool hasTransaction() const
87 { return _inTransaction; }
88
89 public:
92 void open(const std::string& connStr);
93
96 void close();
97
101
104 void endClose();
105
109 { return _closeFinished; }
110
113 bool isOpen() const
114 { return _isOpen; }
115
118 void beginOpen(const std::string& connStr);
119
122 void endOpen();
123
127 { return _openFinished; }
128
129 public:
132 void startTransaction(const char* sql = nullptr);
133
136 void commitTransaction(const char* sql = nullptr);
137
140 void rollbackTransaction(const char* sql = nullptr);
141
144 void beginStartTransaction(Transaction& txn, const char* sql);
145
149
152 void beginCommitTransaction(Transaction& txn, const char* sql);
153
157
160 void beginRollbackTransaction(Transaction& txn, const char* sql);
161
165
166 public:
169 size_type execute(const std::string& query);
170
173 void beginExecute(const std::string& sql);
174
177 size_type endExecute();
178
182 { return _executeFinished; }
183
186 size_type execute(IStatement& stmt);
187
191
194 size_type endExecute(IStatement& stmt);
195
196 public:
199 Statement prepare(const std::string& query);
200
203 virtual Statement prepareCached(const std::string& query) = 0;
204
207 virtual void clearStatementCache() = 0;
208
211 virtual void beginPrepareCached(const std::string& query) = 0;
212
216
220 { return _prepareCachedFinished; }
221
224 void beginPrepare(const std::string& query);
225
229
233 { return _prepareFinished; }
234
235 public:
238 Result select(const std::string& query);
239
242 void beginSelect(const std::string& sql);
243
247
251 { return _selectFinished; }
252
256
260
264
268
272
273 public:
276 Cursor getCursor(IStatement& stmt, size_type batchSize);
277
280 Result fetchBatch(ICursor& cursor, size_type batchSize);
281
284 void beginBatchFetch(ICursor& cursor, size_type batchSize);
285
289
292 void cancelCursor(ICursor& cursor);
293
297
298 protected:
299 IConnection();
300
303 void cancelPending() noexcept;
304
307 void cancelConnection() noexcept;
308
309 virtual void onSetActive(Pt::System::EventLoop* loop) = 0;
310
311 virtual void onOpen(const std::string& connStr) = 0;
312
313 virtual void onClose() = 0;
314
315 virtual void onCancelPending() noexcept = 0;
316
317 virtual void onBeginClose() = 0;
318
319 virtual void onEndClose() = 0;
320
321 virtual void onNotifyPreparedCached() = 0;
322
323 virtual void onBeginOpen(const std::string& connStr) = 0;
324
325 virtual void onEndOpen() = 0;
326
327 virtual void onBeginExec(const std::string& sql) = 0;
328
329 virtual size_type onEndExec() = 0;
330
331 virtual void onBeginSelect(const std::string& sql) = 0;
332
333 virtual Result onEndSelect() = 0;
334
335 virtual void onBeginPrepare(const std::string& query) = 0;
336
337 virtual Statement onEndPrepare() = 0;
338
339 virtual void onBeginPrepareCachedMiss(const std::string& query) = 0;
340
341 virtual Statement onEndPrepareCachedMiss() = 0;
342
343 virtual void onBeginStartTransaction(Transaction& txn, const char* sql) = 0;
344
345 virtual void onEndStartTransaction() = 0;
346
347 virtual void onBeginCommitTransaction(Transaction& txn, const char* sql) = 0;
348
349 virtual void onEndCommitTransaction() = 0;
350
351 virtual void onBeginRollbackTransaction(Transaction& txn, const char* sql) = 0;
352
353 virtual void onEndRollbackTransaction() = 0;
354
355 virtual size_type onExecute(const std::string& query) = 0;
356
357 virtual Result onSelect(const std::string& query) = 0;
358
359 virtual Statement onPrepare(const std::string& query) = 0;
360
361 virtual bool onPing() = 0;
362
363 virtual void onBeginPing() = 0;
364
365 virtual bool onEndPing() = 0;
366
367 virtual long long onLastInsertId(const std::string& name) = 0;
368
369 virtual void onStartTransaction(const char* sql) = 0;
370
371 virtual void onCommitTransaction(const char* sql) = 0;
372
373 virtual void onRollbackTransaction(const char* sql) = 0;
374
375 Pt::Signal<> _openFinished;
376 Pt::Signal<> _closeFinished;
377 Pt::Signal<> _executeFinished;
378 Pt::Signal<> _selectFinished;
379 Pt::Signal<> _prepareFinished;
380 Pt::Signal<> _prepareCachedFinished;
381 Pt::Signal<> _pingFinished;
382 bool _prepareCachedHit;
383 bool _isOpen;
384 bool _inTransaction;
385 Pt::System::EventLoop* _loop;
386 void* _pendingOp;
387
388 friend class Connection;
389};
390
391
394class PT_DB_API IStmtCacheConnection : public IConnection
395{
396 public:
399 virtual Statement prepareCached(const std::string& query);
400
403 virtual void clearStatementCache();
404
407 virtual void beginPrepareCached(const std::string& query);
408
412
413 private:
415 typedef std::map<std::string, StatementPtr> StatementCache;
416 StatementCache _stmtCache;
417 IStatement* _cachedHitStmt = nullptr;
418 std::string _pendingPrepareCachedQuery;
419};
420
421} // namespace Db
422
423} // namespace Pt
424
425#endif // PT_DB_ICONNECTION_H
Batch cursor for large query results.
Definition Cursor.h:58
bool isOpen() const
Returns true if the database is open.
Definition IConnection.h:113
virtual void clearStatementCache()=0
Clears the prepared-statement cache.
Pt::Signal & selectFinished()
Signal emitted when an asynchronous select completes.
Definition IConnection.h:250
void rollbackTransaction(const char *sql=nullptr)
Rolls back the active transaction with optional sql.
void beginExecute(const std::string &sql)
Starts asynchronous execution of sql.
void cancelStatement(IStatement &stmt)
Cancels a pending operation on stmt.
Result endBatchFetch(ICursor &cursor)
Completes an asynchronous batch fetch from cursor.
Result fetchBatch(ICursor &cursor, size_type batchSize)
Fetches the next batch from cursor.
bool ping()
Returns true if the backend still answers.
void beginRollbackTransaction(Transaction &txn, const char *sql)
Starts an asynchronous rollback.
long long lastInsertId(const std::string &name)
Returns the last generated row id.
Row selectRow(IStatement &stmt)
Executes stmt and returns the first row.
Pt::Signal & prepareCachedFinished()
Signal emitted when asynchronous prepareCached completes.
Definition IConnection.h:219
Pt::Signal & openFinished()
Signal emitted when an asynchronous open completes.
Definition IConnection.h:126
void endOpen()
Completes an asynchronous open.
Result select(const std::string &query)
Executes query and returns a buffered result.
Result endSelect()
Completes an asynchronous select.
Result endSelect(IStatement &stmt)
Completes an asynchronous select of stmt.
void beginPrepare(const std::string &query)
Starts asynchronous compile of query.
bool endPing()
Completes an asynchronous ping.
Pt::Signal & pingFinished()
Signal emitted when an asynchronous ping completes.
Definition IConnection.h:69
void beginOpen(const std::string &connStr)
Starts an asynchronous open with connStr.
void setActive(Pt::System::EventLoop *loop)
Attaches loop for asynchronous operations, or 0 to detach.
Pt::Signal & executeFinished()
Signal emitted when asynchronous execution completes.
Definition IConnection.h:181
void close()
Closes the database and cancels any pending operation.
void endRollbackTransaction()
Completes an asynchronous rollback.
virtual Statement endPrepareCached()=0
Completes asynchronous prepareCached.
void cancelCursor(ICursor &cursor)
Cancels a pending fetch on cursor.
size_type execute(IStatement &stmt)
Executes stmt and returns the number of rows changed.
Value selectValue(IStatement &stmt)
Executes stmt and returns the first column of the first row.
bool isIdle() const
Returns true if no asynchronous operation is pending.
Definition IConnection.h:81
bool hasTransaction() const
Returns true if a transaction is active.
Definition IConnection.h:86
size_type endExecute(IStatement &stmt)
Completes asynchronous execution of stmt.
Pt::Signal & prepareFinished()
Signal emitted when asynchronous prepare completes.
Definition IConnection.h:232
void open(const std::string &connStr)
Opens the database with connStr.
size_type endExecute()
Completes asynchronous execution.
Result select(IStatement &stmt)
Executes stmt and returns a buffered result.
void cancelPending() noexcept
Cancels any pending asynchronous operation. Never throws.
void cancelConnection() noexcept
Cancels a pending connection-level asynchronous operation.
void endStartTransaction()
Completes an asynchronous begin-transaction.
Statement endPrepare()
Completes asynchronous compile.
void startTransaction(const char *sql=nullptr)
Begins a transaction with optional sql.
virtual void beginPrepareCached(const std::string &query)=0
Starts asynchronous prepare with cache lookup.
size_type execute(const std::string &query)
Executes query and returns the number of rows changed.
Cursor getCursor(IStatement &stmt, size_type batchSize)
Opens a batch cursor on stmt.
Pt::Signal & closeFinished()
Signal emitted when an asynchronous close completes.
Definition IConnection.h:108
void beginStartTransaction(Transaction &txn, const char *sql)
Starts an asynchronous begin-transaction.
void beginClose()
Starts an asynchronous close.
void commitTransaction(const char *sql=nullptr)
Commits the active transaction with optional sql.
void beginPing()
Starts an asynchronous ping.
virtual Statement prepareCached(const std::string &query)=0
Compiles query and caches the prepared statement.
void beginExecute(IStatement &stmt)
Starts asynchronous execution of stmt.
void beginCommitTransaction(Transaction &txn, const char *sql)
Starts an asynchronous commit.
void endCommitTransaction()
Completes an asynchronous commit.
Statement prepare(const std::string &query)
Compiles query into a prepared statement.
void beginBatchFetch(ICursor &cursor, size_type batchSize)
Starts an asynchronous batch fetch from cursor.
void endClose()
Completes an asynchronous close.
void beginSelect(IStatement &stmt)
Starts an asynchronous select of stmt.
void beginSelect(const std::string &sql)
Starts an asynchronous select of sql.
Batch-cursor backend.
Definition ICursor.h:26
Prepared-statement backend.
Definition IStatement.h:33
Connection backend with a prepared-statement cache.
Definition IConnection.h:395
virtual Statement prepareCached(const std::string &query)
Compiles query and caches the prepared statement.
virtual void beginPrepareCached(const std::string &query)
Starts asynchronous prepare with cache lookup.
virtual void clearStatementCache()
Clears the prepared-statement cache.
virtual Statement endPrepareCached()
Completes asynchronous prepareCached.
Buffered query result with random-access rows.
Definition Result.h:42
One row of a buffered query result.
Definition Row.h:34
Prepared SQL statement with named host variables.
Definition Statement.h:65
Unit of work on a database connection.
Definition Transaction.h:50
One column value of a query result.
Definition Value.h:35
Multicast Signal to call multiple slots.
Definition Signal.h:190
Policy based smart pointer.
Definition SmartPtr.h:462
Event loop of a thread or process.
Definition EventLoop.h:83
Portable SQL database access.
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33