diff options
author | Michael Shepanski <michael@codesynthesis.com> | 2014-11-06 16:33:35 +1100 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2014-11-10 16:48:13 +0200 |
commit | 51a01cfebf933d270bf7b7fadb0fb3ca3b7a4cd5 (patch) | |
tree | 40ca9efb48c65e88822bcb7538d64bf1299d7aaf /hello | |
parent | f7d0e9c620d7c3117074e2618f2bc75699d6f2f0 (diff) |
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).
Diffstat (limited to 'hello')
-rw-r--r-- | hello/driver.cxx | 21 |
1 files changed, 11 insertions, 10 deletions
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<person> (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<person> joe ( + db->query_one<person> (query::first == "Joe" && + query::last == "Dirt")); - if (i != r.end ()) + if (joe.get () != 0) { - auto_ptr<person> 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<person_stat> r (db->query<person_stat> ()); - - // 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<person_stat> ()); cout << endl << "count : " << ps.count << endl |