drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.52k stars 1.1k forks source link

(Database) Result in websocket project does not compile... #997

Closed Aventun closed 3 years ago

Aventun commented 3 years ago

I just created a project like this:

$drogon_ctl create project wss cd wss cd build cmake .. make ./wss

Ok.

Then I replaced main.c with the source code from drogon/examples/WebSocketServer.cc (entirely) Then added also to it:

include <drogon/orm/Mapper.h>

using namespace drogon::orm;

and main function became like this:

int main() { std::cout << "Server lauched in 127.0.0.1:8848" << std::endl; drogon::app().loadConfigFile("../config.json"); app().addListener("127.0.0.1", 8848).run(); }

In the config.json I added database details as usually...

In the WebSocketChat::handleNewConnection a added this source code:


...

    std::string sql=std::string("select * from users where id = 10");
    auto db = app().getDbClient();
    if(db==NULL)
    {
        std::cout << "DB connection is not ok!" << std::endl;
        return;
    }

    try
    {
        db->execSqlAsync(sql,[](Result &r)
              {
                  if (r.size() > 0)
                  {
                      return;
                  }
              },
              [](const DrogonDbException &e)
              {
                  std::cerr << "error:" << e.base().what() << std::endl;
              }
        );

    }
    catch (const drogon::orm::DrogonDbException &e)
    {
        std::cout << "catch:" << e.base().what() << std::endl;
    }

....

When I compile the project again I get this:

... /usr/local/include/drogon/orm/SqlBinder.h:174:18: required from here /usr/local/include/drogon/orm/SqlBinder.h:223:72: error: static assertion failed: your sql callback function argument type must be value type or const left-reference type 223 | CallbackArgTypeTraits<NthArgumentType<sizeof...(Values)>>::isValid, | ^~~ /usr/local/include/drogon/orm/SqlBinder.h:231:27: error: no matching function for call to ‘drogon::orm::Result::Result()’ 231 | ValueType value = ValueType(); | ^~~ In file included from /usr/local/include/drogon/orm/Field.h:23, from /usr/local/include/drogon/orm/DbClient.h:19, from /usr/local/include/drogon/HttpAppFramework.h:31, from /usr/local/include/drogon/WebSocketController.h:18, from /home/user1/drogon/wss/main.cc:1: /usr/local/include/drogon/orm/Result.h:64:5: note: candidate: ‘drogon::orm::Result::Result(drogon::orm::Result&&)’ 64 | Result(Result &&) noexcept = default; | ^~ /usr/local/include/drogon/orm/Result.h:64:5: note: candidate expects 1 argument, 0 provided /usr/local/include/drogon/orm/Result.h:63:5: note: candidate: ‘drogon::orm::Result::Result(const drogon::orm::Result&)’ 63 | Result(const Result &r) noexcept = default; | ^~ /usr/local/include/drogon/orm/Result.h:63:5: note: candidate expects 1 argument, 0 provided /usr/local/include/drogon/orm/Result.h:60:5: note: candidate: ‘drogon::orm::Result::Result(const ResultImplPtr&)’ 60 | Result(const ResultImplPtr &ptr) : resultPtr_(ptr)

...

It seems that is something related with: Result &r

However, in a project that has base class like : public HttpController<...>

this source code for datsbase compiles and works fine.

Any help would be very appreciated.

Thanks Av

Env: ubuntu

marty1885 commented 3 years ago

There's 2 issues in your code:

First, as the error message said: our sql callback function argument type must be value type or const left-reference type. But you've gave it a non-const left-value reference db->execSqlAsync(sql,[](Result &r).... Please change it to a const l-value reference. db->execSqlAsync(sql,[](const Result &r)....

Second, the async interface is exception-free. All errors goes to the second callback. So the try...catch... block is unnecessary. Only the sync and coroutine interface can throw.