relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
relx::connection::PostgreSQLConnection Class Reference

PostgreSQL implementation of the Connection interface. More...

#include <relx/connection/postgresql_connection.hpp>

Inheritance diagram for relx::connection::PostgreSQLConnection:
relx::connection::Connection

Public Member Functions

 PostgreSQLConnection (std::string_view connection_string)
 Constructor with connection parameters.
 
 PostgreSQLConnection (const PostgreSQLConnectionParams &params)
 Constructor with structured connection parameters.
 
 ~PostgreSQLConnection () override
 Destructor that ensures proper cleanup.
 
 PostgreSQLConnection (const PostgreSQLConnection &)=delete
 
PostgreSQLConnectionoperator= (const PostgreSQLConnection &)=delete
 
 PostgreSQLConnection (PostgreSQLConnection &&) noexcept
 
PostgreSQLConnectionoperator= (PostgreSQLConnection &&) noexcept
 
ConnectionResult< void > connect () override
 Connect to the PostgreSQL database.
 
ConnectionResult< void > disconnect () override
 Disconnect from the PostgreSQL database.
 
ConnectionResult< result::ResultSetexecute_raw (const std::string &sql, const std::vector< std::string > &params={}) override
 Execute a raw SQL query with parameters.
 
ConnectionResult< result::ResultSetexecute_raw_binary (const std::string &sql, const std::vector< std::string > &params, const std::vector< bool > &is_binary)
 Execute a raw SQL query with binary parameters.
 
template<typename... Args>
ConnectionResult< result::ResultSetexecute_typed (const std::string &sql, Args &&... args)
 Execute a raw SQL query with typed parameters.
 
bool is_connected () const override
 Check if the connection is open.
 
ConnectionResult< void > begin_transaction (IsolationLevel isolation_level=IsolationLevel::ReadCommitted) override
 Begin a new transaction with specified isolation level.
 
ConnectionResult< void > commit_transaction () override
 Commit the current transaction.
 
ConnectionResult< void > rollback_transaction () override
 Rollback the current transaction.
 
bool in_transaction () const override
 Check if a transaction is currently active.
 
std::unique_ptr< PostgreSQLStatementprepare_statement (const std::string &name, const std::string &sql, int param_count)
 Create a prepared statement.
 
PGconnget_pg_conn ()
 Get direct access to the PostgreSQL connection.
 
- Public Member Functions inherited from relx::connection::Connection
virtual ~Connection ()=default
 Virtual destructor.
 
template<query::SqlExpr Query>
ConnectionResult< result::ResultSetexecute (const Query &query)
 Execute a query expression.
 
template<typename T , query::SqlExpr Query>
ConnectionResult< T > execute (const Query &query)
 Execute a query and map results to a user-defined type using Boost.PFR.
 
template<typename T , query::SqlExpr Query>
ConnectionResult< std::vector< T > > execute_many (const Query &query)
 Execute a query and map results to a vector of user-defined types.
 

Static Public Member Functions

static std::string convert_placeholders (const std::string &sql)
 Convert SQL with ? placeholders to PostgreSQL's $n format.
 

Detailed Description

PostgreSQL implementation of the Connection interface.

Definition at line 25 of file postgresql_connection.hpp.

Constructor & Destructor Documentation

◆ PostgreSQLConnection() [1/4]

relx::connection::PostgreSQLConnection::PostgreSQLConnection ( std::string_view  connection_string)
explicit

Constructor with connection parameters.

Parameters
connection_stringPostgreSQL connection string (e.g. "host=localhost port=5432 dbname=mydb user=postgres password=password")

◆ PostgreSQLConnection() [2/4]

relx::connection::PostgreSQLConnection::PostgreSQLConnection ( const PostgreSQLConnectionParams params)
explicit

Constructor with structured connection parameters.

Parameters
paramsPostgreSQL connection parameters

◆ ~PostgreSQLConnection()

relx::connection::PostgreSQLConnection::~PostgreSQLConnection ( )
override

Destructor that ensures proper cleanup.

◆ PostgreSQLConnection() [3/4]

relx::connection::PostgreSQLConnection::PostgreSQLConnection ( const PostgreSQLConnection )
delete

◆ PostgreSQLConnection() [4/4]

relx::connection::PostgreSQLConnection::PostgreSQLConnection ( PostgreSQLConnection &&  )
noexcept

Member Function Documentation

◆ begin_transaction()

ConnectionResult< void > relx::connection::PostgreSQLConnection::begin_transaction ( IsolationLevel  isolation_level = IsolationLevel::ReadCommitted)
overridevirtual

Begin a new transaction with specified isolation level.

Parameters
isolation_levelThe isolation level for the transaction
Returns
Result indicating success or failure

Implements relx::connection::Connection.

◆ commit_transaction()

ConnectionResult< void > relx::connection::PostgreSQLConnection::commit_transaction ( )
overridevirtual

Commit the current transaction.

Returns
Result indicating success or failure

Implements relx::connection::Connection.

◆ connect()

ConnectionResult< void > relx::connection::PostgreSQLConnection::connect ( )
overridevirtual

Connect to the PostgreSQL database.

Returns
Result indicating success or failure

Implements relx::connection::Connection.

◆ convert_placeholders()

static std::string relx::connection::PostgreSQLConnection::convert_placeholders ( const std::string &  sql)
static

Convert SQL with ? placeholders to PostgreSQL's $n format.

Parameters
sqlSQL query with ? placeholders
Returns
Converted SQL with $1, $2, etc. placeholders

◆ disconnect()

ConnectionResult< void > relx::connection::PostgreSQLConnection::disconnect ( )
overridevirtual

Disconnect from the PostgreSQL database.

Returns
Result indicating success or failure

Implements relx::connection::Connection.

◆ execute_raw()

ConnectionResult< result::ResultSet > relx::connection::PostgreSQLConnection::execute_raw ( const std::string &  sql,
const std::vector< std::string > &  params = {} 
)
overridevirtual

Execute a raw SQL query with parameters.

Parameters
sqlThe SQL query string
paramsVector of parameter values
Returns
Result containing the query results or an error

Implements relx::connection::Connection.

◆ execute_raw_binary()

ConnectionResult< result::ResultSet > relx::connection::PostgreSQLConnection::execute_raw_binary ( const std::string &  sql,
const std::vector< std::string > &  params,
const std::vector< bool > &  is_binary 
)

Execute a raw SQL query with binary parameters.

Parameters
sqlThe SQL query string
paramsVector of parameter values
is_binaryVector of flags indicating whether each parameter is binary
Returns
Result containing the query results or an error

◆ execute_typed()

template<typename... Args>
ConnectionResult< result::ResultSet > relx::connection::PostgreSQLConnection::execute_typed ( const std::string &  sql,
Args &&...  args 
)
inline

Execute a raw SQL query with typed parameters.

Template Parameters
ArgsThe types of the parameters
Parameters
sqlThe SQL query string
argsThe parameter values
Returns
Result containing the query results or an error

Definition at line 78 of file postgresql_connection.hpp.

◆ get_pg_conn()

PGconn * relx::connection::PostgreSQLConnection::get_pg_conn ( )
inline

Get direct access to the PostgreSQL connection.

Returns
The PGconn pointer

Definition at line 148 of file postgresql_connection.hpp.

◆ in_transaction()

bool relx::connection::PostgreSQLConnection::in_transaction ( ) const
overridevirtual

Check if a transaction is currently active.

Returns
True if a transaction is active, false otherwise

Implements relx::connection::Connection.

◆ is_connected()

bool relx::connection::PostgreSQLConnection::is_connected ( ) const
overridevirtual

Check if the connection is open.

Returns
True if connected, false otherwise

Implements relx::connection::Connection.

◆ operator=() [1/2]

PostgreSQLConnection & relx::connection::PostgreSQLConnection::operator= ( const PostgreSQLConnection )
delete

◆ operator=() [2/2]

PostgreSQLConnection & relx::connection::PostgreSQLConnection::operator= ( PostgreSQLConnection &&  )
noexcept

◆ prepare_statement()

std::unique_ptr< PostgreSQLStatement > relx::connection::PostgreSQLConnection::prepare_statement ( const std::string &  name,
const std::string &  sql,
int  param_count 
)

Create a prepared statement.

Parameters
nameThe name of the prepared statement
sqlThe SQL query text
param_countThe number of parameters in the statement
Returns
A new prepared statement

◆ rollback_transaction()

ConnectionResult< void > relx::connection::PostgreSQLConnection::rollback_transaction ( )
overridevirtual

Rollback the current transaction.

Returns
Result indicating success or failure

Implements relx::connection::Connection.


The documentation for this class was generated from the following file: