yandex / ozo

OZO is a C++17 Boost.Asio based header-only library for asyncronous communication with PostgreSQL DBMS.
PostgreSQL License
226 stars 45 forks source link

Using string_view #242

Closed nobleread closed 3 years ago

nobleread commented 4 years ago

Is it feasible to somehow use std::string_view for retrieving results from db and can it be possibly faster than std::string? I've tried to form ozo::rows_of with std::string_view, but I cannot get it to compile.

thed636 commented 4 years ago

Hi!

std::string_view does not own data, so making it keep a pointer to PGresult internal buffer is very unsafe. That's why out-of-box std::string_view being treated as a read-only object. But, the library allows you to implement such functionality by specializing ozo::recv_impl for std::string_view. The only thing you need to keep in mind in this case - life-time of the ozo::result object and the corresponding std::string_view object.

With best regard, Sergei.

nobleread commented 4 years ago

Okay, thanks for explaining this. I generally was wondering whether it would be possible to get raw data from postgres and then just create const views (like std::string_view) that allow to iterate over raw data and read them in correct format. This would probably be some kind of wrapper around ozo::result and would perform ad-hoc parsing "on the fly". Of course, this solution has its disadvantages, but I can see some advantages too.

thed636 commented 4 years ago

You can always work with raw data as it accessible via ozo::value if the copy is really a performance issue. But this approach is suitable only for strings and byte arrays. In the other cases copy is needed. To parse on-the-fly you may use ozo::recv function overload for ozo::value. So there are all the necessary tools to construct the desired functionality.

thed636 commented 3 years ago

I close the issue due to no more activity in this question. Please feel free to reopen if necessary.