artyom-beilis / cppcms

CppCMS Framework
Other
443 stars 107 forks source link

cppdb: get original statement and full query for debugging or logging purposes #77

Open dreaming-augustin opened 3 years ago

dreaming-augustin commented 3 years ago

This is an old request, and I have at last found some time to have a go at it, although the patch I am sharing below is incomplete and hackish at best. But it can help get the conversation started.

Basically, when the MySQL backend throws cppdb_error, we only get the error string reported by MySQL itself. Sometimes, at least for debugging purposes, it would be very convenient to get the full query, with or without the bound values for prepared statements.

As pointed out by Artyom in an earlier discussion, SQL queries can leak sensitive information, so it is understood that a developer would not leak such information to end users. Instead, dedicated calls can be made for debugging and logging purposes, for the use of the developer.

Anyway, I am not very competent, but I still had a go at it. The patch below is only a stub: it introduces new methods to cppdb_error to get the original statement, and the bound parameters. It's at best a work in progress, and parts of it are hackish, but it's enough for debugging purposes, and at least we get something that can be discussed and that can be improved on.

error-show-full-query.patch.txt

Here is a sample usage:

    catch (cppdb::cppdb_error& e) {
            BOOSTER_DEBUG("DEBUG - CppcmsApplication::main") << "cppdb::cppdb_error = "  << e.what();
            BOOSTER_DEBUG("DEBUG - CppcmsApplication::main") << "cppdb::cppdb statement = " << e.get_statement();
            BOOSTER_DEBUG("DEBUG - CppcmsApplication::main") << "cppdb::cppdb query = "  << e.get_query();
   }
dreaming-augustin commented 3 years ago

Updated to include e.get_errno():

error-show-full-query.patch.txt