99#include " sqlgen/internal/random.hpp"
1010#include " sqlgen/internal/strings/strings.hpp"
1111#include " sqlgen/postgres/Iterator.hpp"
12+ #include " sqlgen/postgres/PostgresV2Result.hpp"
1213
1314namespace sqlgen ::postgres {
1415
15- Connection::Connection (const Credentials& _credentials)
16- : conn_(make_conn(_credentials.to_str())), credentials_(_credentials) {}
16+ Connection::Connection (const Conn& _conn) : conn_(_conn) {}
1717
18- Connection::~Connection () = default ;
18+ Connection::Connection (const Credentials& _credentials)
19+ : conn_(PostgresV2Connection::make(_credentials.to_str()).value()) {}
1920
2021Result<Nothing> Connection::begin_transaction () noexcept {
2122 return execute (" BEGIN TRANSACTION;" );
@@ -24,20 +25,19 @@ Result<Nothing> Connection::begin_transaction() noexcept {
2425Result<Nothing> Connection::commit () noexcept { return execute (" COMMIT;" ); }
2526
2627Result<Nothing> Connection::execute (const std::string& _sql) noexcept {
27- return exec (conn_, _sql).transform ([](auto &&) { return Nothing{}; });
28+ return PostgresV2Result::make (_sql, conn_).transform ([](auto &&) {
29+ return Nothing{};
30+ });
2831}
2932
3033Result<Nothing> Connection::end_write () {
31- if (PQputCopyEnd (conn_.get (), NULL ) == -1 ) {
32- return error (PQerrorMessage (conn_.get ()));
34+ if (PQputCopyEnd (conn_.ptr (), NULL ) == -1 ) {
35+ return error (PQerrorMessage (conn_.ptr ()));
3336 }
34- const auto res = PQgetResult (conn_.get ());
35- if (PQresultStatus (res) != PGRES_COMMAND_OK) {
36- const auto err = error (PQerrorMessage (conn_.get ()));
37- PQclear (res);
38- return err;
37+ const auto res = PostgresV2Result (PQgetResult (conn_.ptr ()));
38+ if (PQresultStatus (res.ptr ()) != PGRES_COMMAND_OK) {
39+ return error (PQerrorMessage (conn_.ptr ()));
3940 }
40- PQclear (res);
4141 return Nothing{};
4242}
4343
@@ -53,14 +53,14 @@ Result<Nothing> Connection::insert_impl(
5353
5454 const auto sql = to_sql_impl (_stmt);
5555
56- const auto res = PQprepare (conn_. get (), name. c_str (), sql. c_str (),
57- _data.at (0 ).size (), nullptr );
56+ const auto res = PostgresV2Result ( PQprepare (
57+ conn_. ptr (), name. c_str (), sql. c_str (), _data.at (0 ).size (), nullptr ) );
5858
59- const auto status = PQresultStatus (res);
59+ const auto status = PQresultStatus (res. ptr () );
6060
6161 if (status != PGRES_COMMAND_OK) {
6262 return error (" Generating prepared statement for '" + sql +
63- " ' failed: " + PQresultErrorMessage (res));
63+ " ' failed: " + PQresultErrorMessage (res. ptr () ));
6464 }
6565
6666 std::vector<const char *> current_row (_data[0 ].size ());
@@ -71,7 +71,6 @@ Result<Nothing> Connection::insert_impl(
7171 const auto & d = _data[i];
7272
7373 if (d.size () != current_row.size ()) {
74- execute (" ROLLBACK;" );
7574 execute (" DEALLOCATE " + name + " ;" );
7675 return error (" Error in entry " + std::to_string (i) + " : Expected " +
7776 std::to_string (current_row.size ()) + " entries, got " +
@@ -82,60 +81,38 @@ Result<Nothing> Connection::insert_impl(
8281 current_row[j] = d[j] ? d[j]->c_str () : nullptr ;
8382 }
8483
85- const auto res = PQexecPrepared (conn_.get (), // conn
86- name.c_str (), // stmtName
87- n_params, // nParams
88- current_row.data (), // paramValues
89- nullptr , // paramLengths
90- nullptr , // paramFormats
91- 0 // resultFormat
92- );
84+ const auto res =
85+ PostgresV2Result (PQexecPrepared (conn_.ptr (), // conn
86+ name.c_str (), // stmtName
87+ n_params, // nParams
88+ current_row.data (), // paramValues
89+ nullptr , // paramLengths
90+ nullptr , // paramFormats
91+ 0 // resultFormat
92+ ));
93+
94+ const auto status = PQresultStatus (res.ptr ());
9395
94- const auto status = PQresultStatus (res);
9596 if (status != PGRES_COMMAND_OK) {
96- PQclear (res);
9797 const auto err = error (std::string (" Executing INSERT failed: " ) +
98- PQresultErrorMessage (res));
99- execute (" ROLLBACK;" );
98+ PQresultErrorMessage (res.ptr ()));
10099 execute (" DEALLOCATE " + name + " ;" );
101100 return err;
102101 }
103- PQclear (res);
104102 }
105103
106104 return execute (" DEALLOCATE " + name + " ;" );
107105}
108106
109107rfl::Result<Ref<Connection>> Connection::make (
110108 const Credentials& _credentials) noexcept {
111- try {
112- return Ref<Connection>::make (_credentials);
113- } catch (std::exception& e) {
114- return error (e.what ());
115- }
116- }
117-
118- typename Connection::ConnPtr Connection::make_conn (
119- const std::string& _conn_str) {
120- const auto raw_ptr = PQconnectdb (_conn_str.c_str ());
121-
122- if (PQstatus (raw_ptr) != CONNECTION_OK) {
123- const auto msg = std::string (" Connection to postgres failed: " ) +
124- PQerrorMessage (raw_ptr);
125- PQfinish (raw_ptr);
126- throw std::runtime_error (msg.c_str ());
127- }
128-
129- return ConnPtr::make (std::shared_ptr<PGconn>(raw_ptr, &PQfinish)).value ();
109+ return PostgresV2Connection::make (_credentials.to_str ())
110+ .transform ([](auto && _conn) { return Ref<Connection>::make (_conn); });
130111}
131112
132113Result<Ref<Iterator>> Connection::read_impl (const dynamic::SelectFrom& _query) {
133114 const auto sql = postgres::to_sql_impl (_query);
134- try {
135- return Ref<Iterator>::make (sql, conn_);
136- } catch (std::exception& e) {
137- return error (e.what ());
138- }
115+ return Ref<Iterator>::make (sql, conn_);
139116}
140117
141118Result<Nothing> Connection::rollback () noexcept { return execute (" ROLLBACK;" ); }
@@ -172,17 +149,15 @@ Result<Nothing> Connection::write_impl(
172149 const std::vector<std::vector<std::optional<std::string>>>& _data) {
173150 for (const auto & line : _data) {
174151 const auto buffer = to_buffer (line);
175- const auto success = PQputCopyData (conn_.get (), buffer.c_str (),
152+ const auto success = PQputCopyData (conn_.ptr (), buffer.c_str (),
176153 static_cast <int >(buffer.size ()));
177154 if (success != 1 ) {
178- PQputCopyEnd (conn_.get (), NULL );
179- while (auto res = PQgetResult (conn_.get ()))
180- PQclear (res);
181-
155+ PQputCopyEnd (conn_.ptr (), NULL );
182156 return error (" Error occurred while writing data to postgres." );
183157 }
184158 }
185159 return Nothing{};
186160}
187161
188162} // namespace sqlgen::postgres
163+
0 commit comments