SOCI / soci

Official repository of the SOCI - The C++ Database Access Library
http://soci.sourceforge.net/
Boost Software License 1.0
1.37k stars 472 forks source link

Query-Logging with prepared statements #1068

Open wakille opened 10 months ago

wakille commented 10 months ago

Hello, for my use case it is important to log the exact query executed, which is by default not possible with prepared statements, as I unterstand. (You can only see the prepared statement before the preparation). Is there any good practice on achiving this with prepared statements or do you plan to include this feature in upcoming releases ?

Thanks

zann1x commented 10 months ago

There is no supported way to do this. logger::start_query is getting called way too early to even have a chance knowing about any parameters. This is done so logging is effective whether the query succeeds or not.

I can't speak of any plans of including such a feature, though.

wakille commented 10 months ago

For me you exactly explained the main issue with the current logging, I cant see afterwards if the query succeded or not. And I cant track afterwards what has been written to the database. How big do guess the effort to enable soci to support the logging described previously by me ?

zann1x commented 10 months ago

Generally speaking, you can assume that a query has succeeded when no exception is thrown. In terms of effort for adding this feature, I'd say it's probably just a matter of adding something like logger::execute_query(std::string query_with_values) in the execution step of soci::statement. I can definitely see the benefit in terms of tracing queries here. Feel free to create a PR for that!

wakille commented 10 months ago

Is there any plan for a 4.x version release ?

zann1x commented 10 months ago

I think that the next release is mostly blocked by #954, but I can't tell you when we're able to land these changes.

vadz commented 10 months ago

FWIW I agree that having a way to log the values of the parameters (if I understand correctly what you'd like to do here?) would be useful. And I'll try to merge #954 and make a release a.s.a.p.

wakille commented 10 months ago

Yeah, that would be really nice. I looked into the sqlite3 documentation and there is a method to get the expanded query as string. The main issue I saw in implementing this in your project is, that you are not logging in the backends. So either the way how you execute the the prepared query’s or the way you log has to change for this. I can also help implement this if you can give man like some plan how you want it implemented. Could we take this logging into the upcoming release ?

vadz commented 10 months ago

As statement_impl already has a session_ member, it could pass session_.get_logger() to some new backend log_statement_execution() function. Am I missing some difficulty here?