[odb-users] Recommended way of creating a verbose/debug/log mode with ODB and sqlite?

Boris Kolpackov boris at codesynthesis.com
Mon Oct 3 08:41:02 EDT 2011


Hi Thomas,

Szumowski, Thomas <thomas.szumowski at lmco.com> writes:

> For debugging purposes, I'd like to be able to acquire a log of all the
> native SQL statements that ODB executes during a program run. In native
> sqlite, I believe this is possible through the sqlite_trace API. Is there
> something similar in ODB?

While we are planning to add support for database-independent statement
tracing to ODB, it is not ready yet. In the meantime, it is possible to
use the sqlite_trace() function, provided you have 1.6.0.a2 or later
(final 1.6.0 should be out in a few days).

If your application is single-threaded (i.e., uses one database connection
at any given time), then you can simply do:


odb::sqlite::database& db = ...

{
  odb::sqlite::connection_ptr c (db.connection ());
  sqlite3* handle (c.handle ());
  sqlite3_trace (handle, ...);  
}

If the application is multi-threaded (or may use multiple connections
simultaneously), then you can override the create() function in the
connection_pool_factory class and enable tracing for every SQLite
connection that the pool creates. For example:

class trace_connection_pool_factory: public 
  odb::sqlite::connection_pool_factory
{
public:
  trace_connection_pool_factory (std::size_t max_connections = 0,
                                 std::size_t min_connections = 0)
    : connection_pool_factory (max_connections, min_connections)
  {
  }

  virtual pooled_connection_ptr
  create ()
  {
    pooled_connection_ptr c (connection_pool_factory::create ());
    sqlite3* handle (c.handle ());
    sqlite3_trace (handle, ...);  
    return c;
  }
};

Then, when creating the database instance, we need to pass our custom
connection factory, for example:

auto_ptr<odb::sqlite::connection_factory> f (
  new trace_connection_pool_factory);

auto_ptr<odb::database> db (
  new sqlite::database (argc, argv, false, SQLITE_OPEN_READWRITE, f));

Note that the second approach will also work for the first case (single-
threaded applications).

Boris



More information about the odb-users mailing list