danvratil / qcoro

C++ Coroutines for Qt
https://qcoro.dvratil.cz
MIT License
326 stars 53 forks source link

Add support to work with database #188

Open klappdev opened 11 months ago

klappdev commented 11 months ago

QCoro is an amazing library. One of the places where coroutines are useful is working with databases.

Boost 1.82 added the boost.mysql library. https://www.boost.org/doc/libs/1_83_0/libs/mysql/doc/html/index.html And it has support for working with coroutines in particular. https://www.boost.org/doc/libs/1_83_0/libs/mysql/doc/html/mysql/examples/async_coroutinescpp20.html

It would be great if you added support for the database with coroutines.

danvratil commented 11 months ago

Hi, I'm glad to hear that you like QCoro :)

Supporting sql is tricky - this library is designed to wrap existing Qt types and make certain operations awaitable - which is possible thanks to most relevant Qt classes providing an async API already (you call some method and eventually the object will emit a signal that it's done), which is trivial to wrap into an awaitable wrapper.

Unfortunately, QtSql is completely synchronous (QSqlQuery::exec() blocks until the database engine is done). The only option would be to move the query to an another thread, execute it there and then asynchronously wait for the thread to finish. This is, however, much more complicated than just wrapping an existing Qt type into an awaitable - there could be unintended or unexpected consequences of moving the query between threads. So instead, I'd have to write a custom equivalent of QSqlQuery or even a completely custom SQL "drivers" using native asynchronous APIs of mysql or postgres, which would be a non-trivial amount of work to write and maintain, and would almost certainly be a separate library v from QCoro.

That said, I initially started exploring coroutines exactly to see how SQL could be done asynchronously and I do have a use case for it myself. So I'm not saying it ain't gonna happen, just likely won't happen any time soon, sorry.

jbruechert commented 11 months ago

For SQLite I have https://invent.KDE.org/libraries/futuresql, which works with QCoro. Although it could be used for other database types, it would be inefficient. It just uses a single thread to run queries, so it doesn't block the GUI, but can't run queries in parallel since it is still based on QtSqls blocking API.