Skip to content

Commit e4645c0

Browse files
committed
UPD | some improvements #2
1 parent 66b754f commit e4645c0

27 files changed

Lines changed: 700 additions & 366 deletions

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ int main () {
125125

126126
auto router = manapi::net::http::server::create(router_ctx).unwrap();
127127

128-
auto db = manapi::ext::pq::connection::create().unwrap();
128+
auto db = manapi::ext::pq::db::create().unwrap();
129129

130130
router.GET ("/", [&cnt] (http::req &req, http::uresp resp) mutable -> void {
131131
resp->text(std::format("Hello World! Count: {}", cnt.fetch_add(1))).unwrap();
@@ -187,13 +187,14 @@ int main () {
187187
router.GET("/pq/[id]", [db](manapi::net::http::request& req, manapi::net::http::response& resp) mutable -> manapi::future<> {
188188
auto msg = req.param("id").unwrap();
189189
char *end;
190-
auto res1 = co_await db.exec("INSERT INTO for_test (id, str_col) VALUES ($2, $1);","no way", std::strtoll(msg.data(), &end, 10));
190+
auto res1 = co_await db->execl(manapi::ext::pq::kMaster, "INSERT INTO for_test (id, str_col) VALUES ($2, $1);",
191+
manapi::async::timeout_cancellation(2500), "no way", std::strtoll(msg.data(), &end, 10));
191192
if (!res1) {
192193
if (res1.sqlcode() != manapi::ext::pq::SQL_STATE_UNIQUE_VIOLATION)
193194
res1.err().log();
194195
}
195196

196-
auto res = co_await db.exec("SELECT * FROM for_test;");
197+
auto res = co_await db->exec(manapi::ext::pq::kSlave, "SELECT * FROM for_test;");
197198
if (res) {
198199
std::string content = "b";
199200
for (const auto &row: res.unwrap()) {
@@ -211,7 +212,10 @@ int main () {
211212
* works in this context as long as possible.
212213
*/
213214
manapi::async::run([router, db] () mutable -> manapi::future<> {
214-
manapi::unwrap(co_await db.connect("127.0.0.1", "7879", "development", "password", "db"));
215+
auto master = manapi::ext::pq::pool::create().unwrap();
216+
manapi::unwrap(co_await master->connect(2, "127.0.0.1", "7879", "development", "password", "db"));
217+
db->set_master(std::move(master)).unwrap();
218+
215219
manapi::unwrap(co_await router.config_object({
216220
{"pools", manapi::json::array({
217221
{

handlers.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
// -> manapi::future<> {
6161
// ssize_t len = 10737418240 / 2;
6262
//
63-
// resp.header(std::string{manapi::net::http::header::CONTENT_LENGTH}, std::to_string(len));
63+
// resp.header(std::string{manapi::net::http::H_CONTENT_LENGTH}, std::to_string(len));
6464
// co_return resp.callback_sync([current = (ssize_t)0, len] (char *buffer, ssize_t size, bool &flg) mutable
6565
// -> ssize_t {
6666
// size = std::min(size, len - current);
@@ -77,7 +77,7 @@
7777
// char *end;
7878
// ssize_t len = std::strtoll(req.param("size").unwrap().data(), &end, 10);
7979
//
80-
// resp.header(std::string{manapi::net::http::header::CONTENT_LENGTH}, std::to_string(len));
80+
// resp.header(std::string{manapi::net::http::H_CONTENT_LENGTH}, std::to_string(len));
8181
// co_return resp.callback_sync([current = (ssize_t)0, len] (char *buffer, ssize_t size, bool &flg) mutable
8282
// -> ssize_t {
8383
// size = std::min(size, len - current);
@@ -135,9 +135,9 @@
135135
//
136136
// router.POST ("/echo", [] (manapi::net::http::request &req, manapi::net::http::response &resp)
137137
// -> manapi::future<> {
138-
// if (req.contains_header(std::string{manapi::net::http::header::CONTENT_LENGTH}))
139-
// resp.header(std::string{manapi::net::http::header::CONTENT_LENGTH},
140-
// std::string{req.header(manapi::net::http::header::CONTENT_LENGTH).unwrap()}).unwrap();
138+
// if (req.contains_header(std::string{manapi::net::http::H_CONTENT_LENGTH}))
139+
// resp.header(std::string{manapi::net::http::H_CONTENT_LENGTH},
140+
// std::string{req.header(manapi::net::http::H_CONTENT_LENGTH).unwrap()}).unwrap();
141141
//
142142
// std::size_t sss = 0;
143143
// co_return resp.callback_stream([&sss, &resp, &req] (manapi::net::http::response::resp_stream_cb cb) -> manapi::future<> {

include/ManapiErrors.hpp

Lines changed: 128 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ namespace manapi {
191191
*/
192192
class exception final : public std::exception {
193193
public:
194+
explicit exception (messages msg);
194195
/**
195196
* initialize exception
196197
* @param errnum error code
@@ -236,8 +237,14 @@ namespace manapi {
236237

237238
virtual ~status ();
238239

240+
status (messages msg);
241+
242+
status (err_num code, const char *msg);
243+
239244
status (err_num code, std::string_view msg);
240245

246+
status (err_num code, std::string msg);
247+
241248
status (status &&n) MANAPIHTTP_NOEXCEPT;
242249

243250
status& operator= (status &&n) MANAPIHTTP_NOEXCEPT;
@@ -270,7 +277,7 @@ namespace manapi {
270277
/**
271278
* do log using the status
272279
*/
273-
virtual void log () const;
280+
void log () const;
274281

275282
/**
276283
* get the error code as a string
@@ -284,11 +291,19 @@ namespace manapi {
284291
*
285292
* @throws manapi::exception with the error code from the status
286293
*/
287-
virtual void unwrap () const;
294+
void unwrap () const;
295+
296+
MANAPIHTTP_NODISCARD virtual std::string fullmsg () const;
288297

289298
void stacktrace () const MANAPIHTTP_NOEXCEPT;
290299

291300
MANAPIHTTP_NODISCARD operator bool () const MANAPIHTTP_NOEXCEPT;
301+
302+
void data (messages data);
303+
304+
messages data ();
305+
306+
messages copy_data ();
292307
protected:
293308
messages m_data;
294309
};
@@ -375,26 +390,132 @@ namespace manapi {
375390
};
376391

377392
status status_ok ();
378-
status status_unknown (std::string_view msg);
393+
394+
status status_unknown ();
395+
379396
status status_cancelled ();
397+
398+
status status_invalid_argument ();
399+
400+
status status_deadline_exceeded ();
401+
402+
status status_not_found ();
403+
404+
status status_already_exists ();
405+
406+
status status_permission_denied ();
407+
408+
status status_resource_exhausted ();
409+
410+
status status_failed_precondition ();
411+
412+
status status_aborted ();
413+
414+
status status_unavailable ();
415+
416+
status status_out_of_range ();
417+
418+
status status_unimplemented ();
419+
420+
status status_internal ();
421+
422+
status status_data_loss ();
423+
424+
status status_data_loss (std::string msg);
425+
426+
status status_unknown (std::string msg);
427+
428+
status status_cancelled (std::string msg);
429+
430+
status status_invalid_argument (std::string msg);
431+
432+
status status_deadline_exceeded (std::string msg);
433+
434+
status status_not_found (std::string msg);
435+
436+
status status_already_exists (std::string msg);
437+
438+
status status_permission_denied (std::string msg);
439+
440+
status status_unauthenticated (std::string msg);
441+
442+
status status_resource_exhausted (std::string msg);
443+
444+
status status_failed_precondition (std::string msg);
445+
446+
status status_aborted (std::string msg);
447+
448+
status status_unavailable (std::string msg);
449+
450+
status status_out_of_range (std::string msg);
451+
452+
status status_unimplemented (std::string msg);
453+
454+
status status_internal (std::string msg);
455+
456+
status status_data_loss (const char * msg);
457+
458+
status status_unknown (const char * msg);
459+
460+
status status_cancelled (const char * msg);
461+
462+
status status_invalid_argument (const char * msg);
463+
464+
status status_deadline_exceeded (const char * msg);
465+
466+
status status_not_found (const char * msg);
467+
468+
status status_already_exists (const char * msg);
469+
470+
status status_permission_denied (const char * msg);
471+
472+
status status_unauthenticated (const char * msg);
473+
474+
status status_resource_exhausted (const char * msg);
475+
476+
status status_failed_precondition (const char * msg);
477+
478+
status status_aborted (const char * msg);
479+
480+
status status_unavailable (const char * msg);
481+
482+
status status_out_of_range (const char * msg);
483+
484+
status status_unimplemented (const char * msg);
485+
486+
status status_internal (const char * msg);
487+
488+
status status_data_loss (std::string_view msg);
489+
490+
status status_unknown (std::string_view msg);
491+
380492
status status_cancelled (std::string_view msg);
493+
381494
status status_invalid_argument (std::string_view msg);
495+
382496
status status_deadline_exceeded (std::string_view msg);
497+
383498
status status_not_found (std::string_view msg);
499+
384500
status status_already_exists (std::string_view msg);
385-
status status_already_exists ();
501+
386502
status status_permission_denied (std::string_view msg);
503+
387504
status status_unauthenticated (std::string_view msg);
388-
status status_resource_exhausted ();
505+
389506
status status_resource_exhausted (std::string_view msg);
507+
390508
status status_failed_precondition (std::string_view msg);
509+
391510
status status_aborted (std::string_view msg);
511+
392512
status status_unavailable (std::string_view msg);
513+
393514
status status_out_of_range (std::string_view msg);
515+
394516
status status_unimplemented (std::string_view msg);
517+
395518
status status_internal (std::string_view msg);
396-
status status_internal ();
397-
status status_data_loss (std::string_view msg);
398519

399520
template<typename T>
400521
auto unwrap (T status) {

include/ManapiEventStructures.hpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,15 +1022,12 @@ namespace manapi::ev {
10221022

10231023
~status () override;
10241024

1025-
/**
1026-
* initialize error status
1027-
*
1028-
* @param code the error code
1029-
* @param msg the error msg
1030-
* @param syserr the syserror code
1031-
*/
10321025
status (manapi::err_num code, std::string_view msg, int syserr);
10331026

1027+
status (manapi::err_num code, std::string msg, int syserr);
1028+
1029+
status (manapi::err_num code, const char * msg, int syserr);
1030+
10341031
status (status &&n) MANAPIHTTP_NOEXCEPT;
10351032

10361033
status &operator=(status &&n) MANAPIHTTP_NOEXCEPT;
@@ -1043,16 +1040,7 @@ namespace manapi::ev {
10431040

10441041
status &operator=(const status &n);
10451042

1046-
/**
1047-
* print log to the logger() if it exists,
1048-
* otherwise it prints to the stdout
1049-
*/
1050-
void log () const override;
1051-
1052-
/**
1053-
* throw a error if it exists, otherwise it does nothing
1054-
*/
1055-
void unwrap() const override;
1043+
MANAPIHTTP_NODISCARD std::string fullmsg() const override;
10561044

10571045
/**
10581046
* Get the system code error

include/ext/pq/AsyncPostgreError.hpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,12 @@ namespace manapi::ext::pq {
5353

5454
~status () override;
5555

56-
/**
57-
* initialize error status
58-
*
59-
* @param code error code
60-
* @param msg error msg
61-
* @param sqlcode sql status code
62-
* @param sqlmsg sql error message
63-
*/
6456
status (manapi::err_num code, std::string_view msg, std::size_t sqlcode, std::string sqlmsg);
6557

58+
status (manapi::err_num code, std::string msg, std::size_t sqlcode, std::string sqlmsg);
59+
60+
status (manapi::err_num code, const char * msg, std::size_t sqlcode, std::string sqlmsg);
61+
6662
status (status &&n) MANAPIHTTP_NOEXCEPT;
6763

6864
status &operator=(status &&n) MANAPIHTTP_NOEXCEPT;
@@ -73,16 +69,7 @@ namespace manapi::ext::pq {
7369

7470
status (const status &n);
7571

76-
/**
77-
* print log to the logger() if it exists,
78-
* otherwise it prints to the stdout
79-
*/
80-
void log () const override;
81-
82-
/**
83-
* throw a error if it exists, otherwise it does nothing
84-
*/
85-
void unwrap() const override;
72+
MANAPIHTTP_NODISCARD std::string fullmsg() const override;
8673

8774
MANAPIHTTP_NODISCARD bool is_sqlerr () const MANAPIHTTP_NOEXCEPT;
8875

0 commit comments

Comments
 (0)