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 | |
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).
-rw-r--r-- | composite/driver.cxx | 10 | ||||
-rw-r--r-- | hello/driver.cxx | 21 | ||||
-rw-r--r-- | inverse/driver.cxx | 8 | ||||
-rw-r--r-- | prepared/driver.cxx | 9 | ||||
-rw-r--r-- | query/driver.cxx | 50 | ||||
-rw-r--r-- | relationship/driver.cxx | 7 | ||||
-rw-r--r-- | 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<person> query; - typedef odb::result<person> result; transaction t (db->begin ()); - result r (db->query<person> ( - query::name.extras.nickname == "Squeaky")); + auto_ptr<person> p ( + db->query_one<person> ( + 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<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 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<employee> query; - typedef odb::result<employee> result; session s; transaction t (db->begin ()); @@ -216,10 +215,9 @@ main (int argc, char* argv[]) // shared_ptr<position> se (new position ("Software Engineer")); - result r (db->query<employee> (query::first == "John" && - query::last == "Doe")); - - shared_ptr<employee> john (r.begin ().load ()); + shared_ptr<employee> john ( + db->query_one<employee> (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<person_count> query; typedef odb::prepared_query<person_count> prep_query; - typedef odb::result<person_count> result; transaction t (db->begin ()); @@ -215,11 +214,11 @@ main (int argc, char* argv[]) prep_query pq ( db->prepare_query<person_count> ("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<person> query; typedef odb::result<person> 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<person> p (db->query_one<person> (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<person> (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<employer> csi (db->load<employer> ("Complex Systems Inc")); shared_ptr<project> ch (db->load<project> ("Complex Hardware")); - result r (db->query<employee> (query::first == "John" && - query::last == "Doe")); - - shared_ptr<employee> john (r.begin ().load ()); + shared_ptr<employee> john ( + db->query_one<employee> (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<employee_count> r ( - db->query<employee_count> (query<employee_count>::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<employee_count> ( + query<employee_count>::last == "Doe")); + + cout << ec.count << " employees with the Doe last name" << endl << endl; t.commit (); |