Skip to content

Commit 506b393

Browse files
committed
UPD | result leak
1 parent 82809aa commit 506b393

3 files changed

Lines changed: 33 additions & 18 deletions

File tree

include/ext/pq/AsyncPostgreImpl.ipp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ manapi::future<manapi::ext::pq::status_or<manapi::ext::pq::result>> manapi::ext:
402402
}
403403

404404
token.cancel();
405+
if (result.ok()) {
406+
co_return result.unwrap().copy();
407+
}
408+
405409
co_return std::move(result);
406410
}
407411

include/ext/pq/AsyncPostgreResult.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ namespace manapi::ext::pq {
5959
return this->res_.get();
6060
}
6161

62+
result copy () const {
63+
return {PQcopyResult(this->res_.get(), PG_COPYRES_ATTRS | PG_COPYRES_TUPLES)};
64+
}
65+
6266
MANAPIHTTP_NODISCARD pq::sql_states sqlstate() const MANAPIHTTP_NOEXCEPT {
6367
if (this->sqlstate_.has_value()) {
6468
return static_cast<sql_states>(this->sqlstate_.value());

main.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include <ManapiHttp.hpp>
44
#include <ManapiInitTools.hpp>
5-
6-
#include "cache/ManapiLRU.hpp"
75
#include "ext/pq/AsyncPostgreClient.hpp"
86
#include "ext/pq/AsyncPostgrePool.hpp"
97
#define FOLDER "/home/Timur/Downloads/anime-main/"
@@ -20,27 +18,36 @@ int main() {
2018
ctx->run(0, [server_ctx] (auto cb) -> void {
2119
using http = manapi::net::http::server;
2220

23-
manapi::lru_cache<std::string, std::string> caching (10);
24-
2521
auto route = manapi::net::http::server::create(server_ctx).unwrap();
2622
auto db = manapi::ext::pq::db::create().unwrap();
2723

28-
route.GET ("/", [db, &caching] (http::req &req, http::resp &resp) mutable
24+
route.GET ("/", [db] (http::req &req, http::resp &resp) mutable
2925
-> manapi::future<> {
30-
if (req.contains_get_param("k") && req.contains_get_param("v")) {
31-
auto key = req.get("k").unwrap();
32-
auto val = req.get("v").unwrap();
33-
caching.put(std::string{key}, std::string(val), val.size());
34-
co_return resp.text("OK!").unwrap();
35-
}
36-
else if (req.contains_get_param("k")) {
37-
auto key = req.get("k").unwrap();
38-
auto res = caching.get(std::string{key});
39-
co_return resp.text(*res.unwrap()).unwrap();
40-
}
41-
else {
42-
co_return resp.text("k param").unwrap();
26+
27+
manapi::ext::pq::result res = manapi::unwrap(co_await db->exec(manapi::ext::pq::kSlave, "SELECT * FROM test;"));
28+
std::string content;
29+
for (auto row : res) {
30+
content += row["text"].as<std::string>() + "\n";
4331
}
32+
33+
resp.replacers({
34+
{"data", std::move(content)}
35+
}).unwrap();
36+
37+
co_return resp.file("../test.html").unwrap();
38+
}).unwrap();
39+
40+
route.POST ("/", [db] (http::req &req, http::resp &resp) mutable -> manapi::future<> {
41+
std::string name;
42+
manapi::unwrap(co_await req.form([&name] (std::string key) {
43+
if (key != "text") {
44+
throw std::runtime_error ("Invalid param");
45+
}
46+
return manapi::net::formdata_recv::save_string(&name, 500);
47+
}));
48+
manapi::ext::pq::result res = manapi::unwrap(co_await db->execl(manapi::ext::pq::kMaster, "INSERT INTO test (text) VALUES ($1);",
49+
manapi::ctokens::timeout(500), name));
50+
co_return resp.json({{"code", 0}, {"msg", "OK"}}).unwrap();
4451
}).unwrap();
4552

4653
manapi::async::run ([route, db] () mutable -> manapi::future<> {

0 commit comments

Comments
 (0)