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). --- composite/driver.cxx | 10 +++++----- hello/driver.cxx | 21 +++++++++++---------- inverse/driver.cxx | 8 +++----- prepared/driver.cxx | 9 ++++----- query/driver.cxx | 50 ++++++++++++++++++++++++++++++++++++++++--------- relationship/driver.cxx | 7 +++---- view/driver.cxx | 12 +++++++----- 7 files changed, 74 insertions(+), 43 deletions(-) diff --git a/composite/driver.cxx b/composite/driver.cxx index aaedf17..fc228bf 100644 --- a/composite/driver.cxx +++ b/composite/driver.cxx @@ -84,16 +84,16 @@ main (int argc, char* argv[]) // { typedef odb::query query; - typedef odb::result result; transaction t (db->begin ()); - result r (db->query ( - query::name.extras.nickname == "Squeaky")); + auto_ptr p ( + db->query_one ( + query::name.extras.nickname == "Squeaky")); - if (!r.empty ()) + if (p.get () != 0) { - name& n (r.begin ()->name ()); + name& n (p->name ()); cout << n.title () << " " << n.first () << " " << n.last () << endl; } 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 diff --git a/inverse/driver.cxx b/inverse/driver.cxx index d521579..5a613e1 100644 --- a/inverse/driver.cxx +++ b/inverse/driver.cxx @@ -202,7 +202,6 @@ main (int argc, char* argv[]) // { typedef odb::query query; - typedef odb::result result; session s; transaction t (db->begin ()); @@ -216,10 +215,9 @@ main (int argc, char* argv[]) // shared_ptr se (new position ("Software Engineer")); - result r (db->query (query::first == "John" && - query::last == "Doe")); - - shared_ptr john (r.begin ().load ()); + shared_ptr john ( + db->query_one (query::first == "John" && + query::last == "Doe")); john->employer (csi); john->position (se); diff --git a/prepared/driver.cxx b/prepared/driver.cxx index 7d561a9..d805ddb 100644 --- a/prepared/driver.cxx +++ b/prepared/driver.cxx @@ -206,7 +206,6 @@ main (int argc, char* argv[]) { typedef odb::query query; typedef odb::prepared_query prep_query; - typedef odb::result result; transaction t (db->begin ()); @@ -215,11 +214,11 @@ main (int argc, char* argv[]) prep_query pq ( db->prepare_query ("person-count-age-query", q)); + // Because an aggregate query result always contains one element, + // we use execute_value() insetad of execute() as a shortcut: + // for (age = 90; age > 40; age -= 10) - { - result r (pq.execute ()); - cout << "over " << age << ": " << r.begin ()->count << endl; - } + cout << "over " << age << ": " << pq.execute_value ().count << endl; t.commit (); } diff --git a/query/driver.cxx b/query/driver.cxx index fbd148d..aec8ac8 100644 --- a/query/driver.cxx +++ b/query/driver.cxx @@ -22,17 +22,21 @@ typedef odb::query query; typedef odb::result result; static void -print (result& r) +print (person& p) { - for (result::iterator i (r.begin ()); i != r.end (); ++i) - { - cout << i->first () << " "; + cout << p.first () << " "; - if (!i->middle ().null ()) - cout << i->middle ().get () << " "; + if (!p.middle ().null ()) + cout << p.middle ().get () << " "; - cout << i->last () << " " << i->age () << endl; - } + cout << p.last () << " " << p.age () << endl; +} + +static void +print (result& r) +{ + for (result::iterator i (r.begin ()); i != r.end (); ++i) + print (*i); cout << endl; } @@ -90,7 +94,6 @@ main (int argc, char* argv[]) // /* person p ("", "", 0); - for (result::iterator i (r.begin ()); i != r.end (); ++i) { i.load (p); @@ -103,6 +106,35 @@ main (int argc, char* argv[]) t.commit (); } + // Use query_one() as a shortcut when there's no more than one element + // in the result. + // + { + transaction t (db->begin ()); + + auto_ptr p (db->query_one (query::age == 21)); + + if (p.get () != 0) + { + print (*p); + cout << endl; + } + + // Or we can load the state into an existing object. + // + /* + person p ("", "", 0); + + if (db->query_one (query::age == 21, p)) + { + print (p); + cout << endl; + } + */ + + t.commit (); + } + // Query that shows how to combine expressions with &&, ||, and ! // as well as use paranthesis to control evaluation order. // diff --git a/relationship/driver.cxx b/relationship/driver.cxx index 9af06a2..27a31b2 100644 --- a/relationship/driver.cxx +++ b/relationship/driver.cxx @@ -132,10 +132,9 @@ main (int argc, char* argv[]) shared_ptr csi (db->load ("Complex Systems Inc")); shared_ptr ch (db->load ("Complex Hardware")); - result r (db->query (query::first == "John" && - query::last == "Doe")); - - shared_ptr john (r.begin ().load ()); + shared_ptr john ( + db->query_one (query::first == "John" && + query::last == "Doe")); john->employer (csi); john->projects ().clear (); diff --git a/view/driver.cxx b/view/driver.cxx index 36274d6..cbe26ee 100644 --- a/view/driver.cxx +++ b/view/driver.cxx @@ -179,12 +179,14 @@ main (int argc, char* argv[]) { transaction t (db->begin ()); - result r ( - db->query (query::last == "Doe")); - - // Result of this aggregate query contains only one element. + // Result of an aggregate query contains only one element so let's + // use the query_value() shortcut. // - cout << r.begin ()->count << " employees with the Doe last name" << endl + employee_count ec ( + db->query_value ( + query::last == "Doe")); + + cout << ec.count << " employees with the Doe last name" << endl << endl; t.commit (); -- cgit v1.1