relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
postgresql_statement.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <expected>
6#include <memory>
7#include <optional>
8#include <sstream>
9#include <string>
10#include <string_view>
11#include <type_traits>
12#include <vector>
13
14// Forward declaration
15struct pg_result;
16using PGresult = pg_result;
17namespace relx::connection {
18
21public:
27 PostgreSQLStatement(PostgreSQLConnection& connection, std::string name, std::string sql,
28 int param_count);
29
32
33 // Delete copy operations
36
37 // Allow move operations
40
44 ConnectionResult<result::ResultSet> execute(const std::vector<std::string>& params = {});
45
50 template <typename... Args>
52 // Convert each parameter to its string representation
53 std::vector<std::string> param_strings;
54 param_strings.reserve(sizeof...(Args));
55
56 // Helper to convert a parameter to string and add to vector
57 auto add_param = [&param_strings](auto&& param) {
58 using ParamType = std::remove_cvref_t<decltype(param)>;
59
60 if constexpr (std::is_same_v<ParamType, std::nullptr_t>) {
61 // Handle NULL values
62 param_strings.push_back("NULL");
63 } else if constexpr (std::is_same_v<ParamType, std::string> ||
64 std::is_same_v<ParamType, const char*> ||
65 std::is_same_v<ParamType, std::string_view>) {
66 // String types
67 param_strings.push_back(std::string(param));
68 } else if constexpr (std::is_arithmetic_v<ParamType>) {
69 // Numeric types
70 param_strings.push_back(std::to_string(param));
71 } else if constexpr (std::is_same_v<ParamType, bool>) {
72 // Boolean values
73 param_strings.push_back(param ? "t" : "f");
74 } else {
75 // Other types, try to use stream conversion
76 std::ostringstream ss;
77 ss << param;
78 param_strings.push_back(ss.str());
79 }
80 };
81
82 // Add each parameter to the vector
83 (add_param(std::forward<Args>(args)), ...);
84
85 // Call the string-based execute with our converted parameters
86 return execute(param_strings);
87 }
88
91 const std::string& name() const { return name_; }
92
95 const std::string& sql() const { return sql_; }
96
99 int param_count() const { return param_count_; }
100
103 bool is_valid() const { return is_valid_; }
104
105private:
106 PostgreSQLConnection& connection_;
107 std::string name_;
108 std::string sql_;
109 int param_count_;
110 bool is_valid_ = true;
111
116 ConnectionResult<PGresult*> execute_raw(const std::vector<std::string>& params,
117 int result_format = 0);
118
122 static ConnectionResult<result::ResultSet> process_result(PGresult* pg_result);
123
127 static std::string escape_string(const std::string& str);
128};
129
130} // namespace relx::connection
PostgreSQL implementation of the Connection interface.
Represents a prepared statement in PostgreSQL.
ConnectionResult< result::ResultSet > execute(const std::vector< std::string > &params={})
Execute the prepared statement with parameters.
PostgreSQLStatement(const PostgreSQLStatement &)=delete
~PostgreSQLStatement()
Destructor that deallocates the prepared statement.
PostgreSQLStatement(PostgreSQLStatement &&) noexcept
bool is_valid() const
Check if the statement is still valid.
PostgreSQLStatement(PostgreSQLConnection &connection, std::string name, std::string sql, int param_count)
Constructor.
ConnectionResult< result::ResultSet > execute_typed(Args &&... args)
Execute the prepared statement with typed parameters.
PostgreSQLStatement & operator=(const PostgreSQLStatement &)=delete
const std::string & name() const
Get the name of the prepared statement.
int param_count() const
Get the number of parameters.
const std::string & sql() const
Get the SQL query text.
Represents the result set from a database query.
Definition result.hpp:608
std::expected< T, ConnectionError > ConnectionResult
Type alias for result of connection operations.
STL namespace.
pg_result PGresult