diff options
author | Michael Shepanski <michael@codesynthesis.com> | 2014-11-05 14:23:54 +1100 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2014-11-10 16:48:59 +0200 |
commit | 64b27b86025d160e49bf617143d80671ccb1e0e4 (patch) | |
tree | 1abc0df10d797d0d845e0590c1cb9a077e4a42af /common/prepared/driver.cxx | |
parent | dee5f10d1d75eee43596ad1dfb47ecec9e0b1307 (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 'common/prepared/driver.cxx')
-rw-r--r-- | common/prepared/driver.cxx | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/common/prepared/driver.cxx b/common/prepared/driver.cxx index dfbdf13..ee690a1 100644 --- a/common/prepared/driver.cxx +++ b/common/prepared/driver.cxx @@ -411,6 +411,46 @@ main (int argc, char* argv[]) t.commit (); } + + // Test execute_one() and execute_value(). + // + { + transaction t (db->begin ()); + + person p ("John Doe", 23); + db->persist (p); + + prep_query pq1 ( + db->prepare_query<person> ("query-1", query::id == p.id_)); + prep_query pq0 ( + db->prepare_query<person> ("query-0", query::id == p.id_ + 1)); + + { + auto_ptr<person> p (pq1.execute_one ()); + assert (p.get () != 0 && p->name_ == "John Doe"); + } + + { + auto_ptr<person> p (pq0.execute_one ()); + assert (p.get () == 0); + } + + { + person p; + assert (pq1.execute_one (p) && p.name_ == "John Doe"); + } + + { + person p ("", 0); + assert (!pq0.execute_one (p) && + p.id_ == 0 && p.name_.empty () && p.age_ == 0); + } + + { + person p (pq1.execute_value ()); + assert (p.name_ == "John Doe"); + } + } } catch (const odb::exception& e) { |