19class PostgreSQLStatement;
60 const
std::
string& sql, const
std::vector<
std::
string>& params = {})
override;
69 const std::string& sql,
const std::vector<std::string>& params,
70 const std::vector<bool>& is_binary);
77 template <
typename... Args>
80 std::vector<std::string> param_strings;
81 param_strings.reserve(
sizeof...(Args));
84 auto add_param = [¶m_strings](
auto&& param) {
85 using ParamType = std::remove_cvref_t<
decltype(param)>;
87 if constexpr (std::is_same_v<ParamType, std::nullptr_t>) {
89 param_strings.push_back(
"NULL");
90 }
else if constexpr (std::is_same_v<ParamType, std::string> ||
91 std::is_same_v<ParamType, const char*> ||
92 std::is_same_v<ParamType, std::string_view>) {
94 param_strings.push_back(std::string(param));
95 }
else if constexpr (std::is_arithmetic_v<ParamType>) {
97 param_strings.push_back(std::to_string(param));
98 }
else if constexpr (std::is_same_v<ParamType, bool>) {
100 param_strings.push_back(param ?
"t" :
"f");
103 std::ostringstream ss;
105 param_strings.push_back(ss.str());
110 (add_param(std::forward<Args>(args)), ...);
144 const std::string& sql,
int param_count);
156 std::string connection_string_;
157 PGconn* pg_conn_ =
nullptr;
158 bool is_connected_ =
false;
159 bool in_transaction_ =
false;
Abstract base class for database connections.
PostgreSQL implementation of the Connection interface.
bool is_connected() const override
Check if the connection is open.
PostgreSQLConnection(const PostgreSQLConnection &)=delete
ConnectionResult< result::ResultSet > execute_raw(const std::string &sql, const std::vector< std::string > ¶ms={}) override
Execute a raw SQL query with parameters.
ConnectionResult< void > connect() override
Connect to the PostgreSQL database.
ConnectionResult< result::ResultSet > execute_typed(const std::string &sql, Args &&... args)
Execute a raw SQL query with typed parameters.
std::unique_ptr< PostgreSQLStatement > prepare_statement(const std::string &name, const std::string &sql, int param_count)
Create a prepared statement.
PostgreSQLConnection(PostgreSQLConnection &&) noexcept
PostgreSQLConnection(const PostgreSQLConnectionParams ¶ms)
Constructor with structured connection parameters.
PostgreSQLConnection(std::string_view connection_string)
Constructor with connection parameters.
ConnectionResult< void > commit_transaction() override
Commit the current transaction.
ConnectionResult< void > disconnect() override
Disconnect from the PostgreSQL database.
~PostgreSQLConnection() override
Destructor that ensures proper cleanup.
ConnectionResult< result::ResultSet > execute_raw_binary(const std::string &sql, const std::vector< std::string > ¶ms, const std::vector< bool > &is_binary)
Execute a raw SQL query with binary parameters.
PGconn * get_pg_conn()
Get direct access to the PostgreSQL connection.
PostgreSQLConnection & operator=(const PostgreSQLConnection &)=delete
ConnectionResult< void > begin_transaction(IsolationLevel isolation_level=IsolationLevel::ReadCommitted) override
Begin a new transaction with specified isolation level.
ConnectionResult< void > rollback_transaction() override
Rollback the current transaction.
static std::string convert_placeholders(const std::string &sql)
Convert SQL with ? placeholders to PostgreSQL's $n format.
bool in_transaction() const override
Check if a transaction is currently active.
Represents the result set from a database query.
IsolationLevel
Transaction isolation levels.
@ ReadCommitted
Prevents dirty reads.
std::expected< T, ConnectionError > ConnectionResult
Type alias for result of connection operations.
Basic parameters for a PostgreSQL connection.