6#ifndef PT_DB_ICONNECTION_H
7#define PT_DB_ICONNECTION_H
10#include <Pt/RefCounted.h>
11#include <Pt/SmartPtr.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>
18#include <Pt/Db/Value.h>
43class PT_DB_API IConnection :
public RefCounted
46 typedef std::size_t size_type;
70 {
return _pingFinished; }
82 {
return _pendingOp ==
nullptr; }
87 {
return _inTransaction; }
92 void open(
const std::string& connStr);
109 {
return _closeFinished; }
127 {
return _openFinished; }
182 {
return _executeFinished; }
220 {
return _prepareCachedFinished; }
233 {
return _prepareFinished; }
251 {
return _selectFinished; }
309 virtual
void onSetActive(
Pt::
System::EventLoop* loop) = 0;
311 virtual
void onOpen(const std::
string& connStr) = 0;
313 virtual
void onClose() = 0;
315 virtual
void onCancelPending() noexcept = 0;
317 virtual
void onBeginClose() = 0;
319 virtual
void onEndClose() = 0;
321 virtual
void onNotifyPreparedCached() = 0;
323 virtual
void onBeginOpen(const std::
string& connStr) = 0;
325 virtual
void onEndOpen() = 0;
327 virtual
void onBeginExec(const std::
string& sql) = 0;
329 virtual size_type onEndExec() = 0;
331 virtual
void onBeginSelect(const std::
string& sql) = 0;
333 virtual
Result onEndSelect() = 0;
335 virtual
void onBeginPrepare(const std::
string& query) = 0;
339 virtual
void onBeginPrepareCachedMiss(const std::
string& query) = 0;
341 virtual
Statement onEndPrepareCachedMiss() = 0;
343 virtual
void onBeginStartTransaction(
Transaction& txn, const
char* sql) = 0;
345 virtual
void onEndStartTransaction() = 0;
347 virtual
void onBeginCommitTransaction(
Transaction& txn, const
char* sql) = 0;
349 virtual
void onEndCommitTransaction() = 0;
351 virtual
void onBeginRollbackTransaction(
Transaction& txn, const
char* sql) = 0;
353 virtual
void onEndRollbackTransaction() = 0;
355 virtual size_type onExecute(const std::
string& query) = 0;
357 virtual
Result onSelect(const std::
string& query) = 0;
359 virtual
Statement onPrepare(const std::
string& query) = 0;
361 virtual
bool onPing() = 0;
363 virtual
void onBeginPing() = 0;
365 virtual
bool onEndPing() = 0;
367 virtual
long long onLastInsertId(const std::
string& name) = 0;
369 virtual
void onStartTransaction(const
char* sql) = 0;
371 virtual
void onCommitTransaction(const
char* sql) = 0;
373 virtual
void onRollbackTransaction(const
char* sql) = 0;
380 Pt::
Signal<> _prepareCachedFinished;
382 bool _prepareCachedHit;
388 friend class Connection;
415 typedef std::map<std::string, StatementPtr> StatementCache;
416 StatementCache _stmtCache;
418 std::string _pendingPrepareCachedQuery;
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