drogon: Cannot capture returned value of second sql query in transaction

Consider following code:

auto clientPtr = drogon::app().getFastDbClient("default");
        clientPtr->newTransactionAsync([=, this](const std::shared_ptr<Transaction> &transPtr) mutable {
            assert(transPtr);

            transPtr->execSqlAsync(
                "select t.id, t.visible, t.title, t.created_at , t.posted_by, t.updated_at, t.votes, t.slug, "
                "users.username, users.id as uid, array_agg(topic_tags.tag_id) as tag_id, array_agg(tags.name) as tags from topics t left "
                "join users on t.posted_by=users.id left join topic_tags on topic_tags.topic_id=t.id left join "
                "tags on topic_tags.tag_id = tags.id where t.op_id=0 group by t.id, users.id order by "
                "t.updated_at limit $1 offset $2",
                [=, this](const Result &rows) mutable {
                    if (rows.size() == 0)
                    {
                        return;
                    }
                    else
                    {
                        ret["topics"] = Json::arrayValue;
                        for (auto &r : rows)
                        {
                            Json::Value topic;
                            std::string answers;

                            topic["id"] = r["id"].as<std::string>();
                            topic["visible"] = r["visible"].as<bool>();
                            topic["title"] = r["title"].as<std::string>();
                            topic["created_at"] = r["created_at"].as<std::string>();
                            topic["updated_at"] = r["updated_at"].as<std::string>();
                            topic["votes"] = r["votes"].as<std::string>();
                            topic["slug"] = r["slug"].as<std::string>();
                            topic["username"] = r["username"].as<std::string>();
                            topic["uid"] = r["uid"].as<std::string>();
                            topic["tid"] = r["tag_id"].as<std::string>();
                            topic["tags"] = r["tags"].as<std::string>();
                            transPtr->execSqlAsync(
                                "select count(1) from topics where op_id is not null and op_id=$1",
                                [=, &answers](const Result &rows1) mutable {
                                    for (auto &r1 : rows1)
                                    {
                                        answers = r1["count"].as<std::string>();
                                        // topic["answers"] = r1["count"].as<std::string>();
                                        // ret["topics"].append(topic);
                                    }
                                },
                                [=](const DrogonDbException &e) mutable {
                                    LOG_DEBUG << e.base().what();
                                    ret["error"] = (std::string)e.base().what();
                                    callback(HttpResponse::newHttpJsonResponse(std::move(ret)));
                                },
                                r["id"].as<int64_t>());
                            LOG_DEBUG << answers;
                            ret["topics"].append(topic);
                        }
                    }

                    callback(jsonResponse(std::move(ret)));
                    return;
                },
                [=](const DrogonDbException &e) mutable {
                    LOG_DEBUG << e.base().what();
                    ret["error"] = (std::string)e.base().what();
                    callback(HttpResponse::newHttpJsonResponse(std::move(ret)));
                },
                (long)limit, (long)limit * (page_no - 1));
        });

The string answers is still empty after the second sql query.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

https://github.com/Nalanda-Labs/arth is where I am using Drogon. It is supposed to be a replacement for Wordpress and Discourse both. Long-long way to go. Perhaps you can add it to who is using Drogon. I will start sending X-Powered-by header set to Drogon.