6#ifndef PT_DB_CONNECTION_H
7#define PT_DB_CONNECTION_H
10#include <Pt/SmartPtr.h>
11#include <Pt/Db/IConnection.h>
12#include <Pt/Db/Statement.h>
13#include <Pt/Db/Result.h>
15#include <Pt/Db/Value.h>
19#if __cplusplus >= 202002L
21#include <Pt/Coroutine.h>
38#if __cplusplus >= 202002L
44class ConnectionAwaiter;
99 typedef std::size_t size_type;
169 void open(
const std::string& connStr);
282 friend class Transaction;
284#if __cplusplus >= 202002L
285 friend class ConnectionAwaiter;
287 void attachAwaiter(ConnectionAwaiter& awaiter);
289 void detachAwaiter(ConnectionAwaiter& awaiter);
292 void startTransaction(
const char* sql =
nullptr);
294 void commitTransaction(
const char* sql =
nullptr);
296 void rollbackTransaction(
const char* sql =
nullptr);
298 void beginStartTransaction(Transaction& txn,
const char* sql);
300 void endStartTransaction();
302 void beginCommitTransaction(Transaction& txn,
const char* sql);
304 void endCommitTransaction();
306 void beginRollbackTransaction(Transaction& txn,
const char* sql);
308 void endRollbackTransaction();
310#if __cplusplus >= 202002L
354 InternalRefCounted<IConnection> > ConnectionImplPtr;
356 ConnectionImplPtr _connection;
358#if __cplusplus >= 202002L
359 ConnectionAwaiter* _awaiter =
nullptr;
363#if __cplusplus >= 202002L
376 _conn->attachAwaiter(*
this); ;
384 _conn->detachAwaiter(*
this);
399 throw std::logic_error(
"invalid connection");
418class AsyncOpen :
public ConnectionAwaiter
421 AsyncOpen(
Connection& conn,
const std::string& connStr)
422 : ConnectionAwaiter(conn)
428 connection().endOpen();
432 void onBegin()
override
439 std::string _connStr;
446class AsyncClose :
public ConnectionAwaiter
450 : ConnectionAwaiter(conn)
454 { connection().endClose(); }
457 void onBegin()
override
470class AsyncExecute :
public ConnectionAwaiter
473 AsyncExecute(
Connection& conn,
const std::string& sql)
474 : ConnectionAwaiter(conn)
478 Connection::size_type await_resume()
479 {
return connection().endExecute(); }
482 void onBegin()
override
497class AsyncSelect :
public ConnectionAwaiter
500 AsyncSelect(
Connection& conn,
const std::string& sql)
501 : ConnectionAwaiter(conn)
506 {
return connection().endSelect(); }
509 void onBegin()
override
524class AsyncPing :
public ConnectionAwaiter
528 : ConnectionAwaiter(conn)
533 return connection().endPing();
537 void onBegin()
override
Provides the base class for I/O-driven co_await-able operations.
Definition Coroutine.h:126
void setReady()
Resumes the waiting coroutine.
Definition Coroutine.h:162
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Awaitable for asynchronous close.
Definition Connection.h:447
Awaitable for asynchronous execute.
Definition Connection.h:471
Awaitable for asynchronous open.
Definition Connection.h:419
Awaitable for asynchronous ping.
Definition Connection.h:525
Awaitable for asynchronous select.
Definition Connection.h:498
void onCancel() override
Aborts the in-flight operation.
Definition Connection.h:404
Database session for a registered driver.
Definition Connection.h:97
bool isOpen() const
Returns true if the database is open.
void beginPrepareCached(const std::string &query)
Starts asynchronous compile and cache of query.
void cancel()
Cancels any pending async operation.
Pt::Signal & selectFinished()
Signal emitted when an asynchronous select completes.
void beginExecute(const std::string &sql)
Starts asynchronous execution of sql.
long long lastInsertId(const std::string &name=std::string())
Returns the last generated row id.
void setActive(Pt::System::EventLoop &loop)
Attaches loop for asynchronous operations.
bool ping()
Returns true if the backend still answers.
const IConnection * impl() const
Returns the backend implementation.
Pt::Signal & prepareCachedFinished()
Signal emitted when asynchronous prepareCached completes.
Pt::Signal & openFinished()
Signal emitted when an asynchronous open completes.
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.
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.
void beginOpen(const std::string &connStr)
Starts an asynchronous open with connStr.
Pt::Signal & executeFinished()
Signal emitted when asynchronous execution completes.
AsyncExecute executeAsync(const std::string &sql)
Returns an awaitable that executes sql.
void close()
Closes the database connection.
bool operator!() const
Returns true if the database is not open.
AsyncOpen openAsync(const std::string &connStr)
Returns an awaitable that opens the database with connStr.
~Connection()
Cancels a pending async operation and destroys the connection.
Statement endPrepareCached()
Completes asynchronous prepareCached.
bool isIdle() const
Returns true if no asynchronous operation is pending.
Connection(IConnection *conn)
Takes ownership of the backend conn.
bool hasTransaction() const
Returns true if a transaction is active.
Pt::Signal & prepareFinished()
Signal emitted when asynchronous prepare completes.
void open(const std::string &connStr)
Opens the database with connStr.
size_type endExecute()
Completes asynchronous execution.
Statement endPrepare()
Completes asynchronous compile.
size_type execute(const std::string &query)
Executes query and returns the number of rows changed.
Connection(const std::string &driver)
Creates a connection for driver.
Pt::Signal & closeFinished()
Signal emitted when an asynchronous close completes.
void clearStatementCache()
Clears the prepared-statement cache.
IConnection * impl()
Returns the backend implementation.
void beginClose()
Starts an asynchronous close.
void beginPing()
Starts an asynchronous ping.
AsyncSelect selectAsync(const std::string &sql)
Returns an awaitable that selects sql.
AsyncClose closeAsync()
Returns an awaitable that closes the database.
Statement prepare(const std::string &query)
Compiles query into a prepared statement.
Statement prepareCached(const std::string &query)
Compiles query and caches the prepared statement.
void endClose()
Completes an asynchronous close.
AsyncPing pingAsync()
Returns an awaitable that pings the database.
void beginSelect(const std::string &sql)
Starts an asynchronous select of sql.
Database connection backend.
Definition IConnection.h:44
Buffered query result with random-access rows.
Definition Result.h:42
Prepared SQL statement with named host variables.
Definition Statement.h:65
Multicast Signal to call multiple slots.
Definition Signal.h:190
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition ConstMethod.h:172
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