From 51a01cfebf933d270bf7b7fadb0fb3ca3b7a4cd5 Mon Sep 17 00:00:00 2001 From: Michael Shepanski Date: Thu, 6 Nov 2014 16:33:35 +1100 Subject: Implement {query,execute}_{one,value}() shortcut functions Useful in situations where the query is know to return at most one element (*_one) or exactly one element (*_value). --- hello/driver.cxx | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'hello') diff --git a/hello/driver.cxx b/hello/driver.cxx index a049c79..3a4de8c 100644 --- a/hello/driver.cxx +++ b/hello/driver.cxx @@ -78,14 +78,16 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); - result r (db->query (query::first == "Joe" && - query::last == "Dirt")); - - result::iterator i (r.begin ()); + // Here we know that there can be only one Joe Dirt in our + // database so we use the query_one() shortcut instead of + // manually iterating over the result returned by query(). + // + auto_ptr joe ( + db->query_one (query::first == "Joe" && + query::last == "Dirt")); - if (i != r.end ()) + if (joe.get () != 0) { - auto_ptr joe (i.load ()); joe->age (joe->age () + 1); db->update (*joe); } @@ -99,11 +101,10 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); - odb::result r (db->query ()); - - // The result of this query always has exactly one element. + // The result of this (aggregate) query always has exactly one element + // so use the query_value() shortcut. // - const person_stat& ps (*r.begin ()); + person_stat ps (db->query_value ()); cout << endl << "count : " << ps.count << endl -- cgit v1.1