[odb-users] update object without caching the query result

Boris Kolpackov boris at codesynthesis.com
Thu Sep 24 11:58:01 EDT 2015


Hi Tim,

Tim Tao <Tim.Tao at akunacapital.com> writes:

> using query = odb::query<A>;
> using result = odb::result<A>;
> 
> result r(db->query<A>(some conditions here));
> 
> for(result::iterator::iterator i (r.begin()); i != r.end(); ++i){
>             shared_ptr<A> ptr = i.load();
>             ptr->field = new_value;
>             db->update(ptr);
> }
> 
> So in this way I have to cache each result which seems unnecessary 
> to me since I don't need them in the future once I finished updating

Assuming A is not a session-enabled object and you don't have active
odb::session somewhere up the stack, nothing is cached here. On each
iteration, one object is loaded, updated, and freed.

If you do have a session and A is session-enabled but you don't
want to cache just this set of objects, then there are a couple
of ways you can do it. First is to load the data into a stack-
allocated object:

for(result::iterator::iterator i (r.begin()); i != r.end(); ++i){
            A a;
            i.load(a);
            a.field = new_value;
            db->update(a);
}

The other way is to temporarily "suspend" the session:

odb::session& s (odb::session::current ());
odb::session::reset_current (); // "Suspend".

for(result::iterator::iterator i (r.begin()); i != r.end(); ++i){
  ...
}

odb::session::current (s); // "Resume".

Boris



More information about the odb-users mailing list