sqlpp11 icon indicating copy to clipboard operation
sqlpp11 copied to clipboard

Async support work around.

Open scmp197 opened this issue 4 years ago • 7 comments

Hey! Thanks for this library.

I am trying to figure out how to setup sqlpp11 to work with boost asio in an asynchronous manner.

I think for the first pass, I just want to retrieve the string sqlpp11 is passing into the connector libraries and then directly use it to talk to psql. Is there a way to get at the sql string that the db object passes to the connector?

scmp197 avatar Jul 07 '20 05:07 scmp197

Hi, thanks for the feedback. Please let me know how this works out for you!

Something like the following should work % typos:

::sqlpp::postgresql::connection::_serializer_context context;
std::string serialized_query = serialize(query, context).str();

rbock avatar Jul 07 '20 07:07 rbock

 auto query = sqlpp::select(all_of(_w_map))...

 ::sqlpp::postgresql::connection::_serializer_context_t context(db);

 std::string serialized_query = serialize(query, context).str();

^ This works for me. Are there any plans on adding async support to the library? Especially with coroutines. That would be a cool feature.

If I wanted to contribute something like that any starting point you recommend?

scmp197 avatar Jul 07 '20 20:07 scmp197

Thanks for letting me know!

As of now, I have no plans for adding async support, and I know very little about coroutines. That being said, I believe the best place to start would be the connector libraries and/or the result_t template. You probably could add a yield function there?

rbock avatar Jul 08 '20 04:07 rbock

BTW, How to do the opposite, i.e. parse returned results into C++ objects ? (I did a quick search, but did not mange to find, as unfamiliar with the codebase )

anton-potapov avatar Sep 14 '23 16:09 anton-potapov

Not sure I understand the question?

The returned results are C++ objects. Each row has data members that have the name of the returned columns, e.g.

for (const auto& row : result) {
  std:: cout << row.foo;
}

rbock avatar Sep 15 '23 04:09 rbock

having this :

::sqlpp::postgresql::connection::_serializer_context context;
std::string serialized_query = serialize(query, context).str();

DSL expression can be serialized into text to be sent (asynchronously) to the DB. Once the reply is received (in text) - it has to be converted to the C++ objects according to DDL. I believe sqlpp does this under the hood.

The question is how to do this explicitly (if I do wish to send query and receive results by myself and use sqlpp to serialize and desirialize )

anton-potapov avatar Sep 15 '23 10:09 anton-potapov

I see. This very much depends on the individual database and the type of the query. The most prominent result type is an array of char*. These are then parsed.

See for instance include/sqlpp11/sqlite3/bind_result.h.

Note that these functions are implementation details. While currently stable, they might change any time.

rbock avatar Sep 16 '23 05:09 rbock