From 6375147d5ae76a84dd64f1996cb0d9d501839db6 Mon Sep 17 00:00:00 2001 From: Michael Shepanski Date: Tue, 4 Nov 2014 16:10:03 +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). --- odb/database.hxx | 62 ++++++++++++++++++++++++++++ odb/database.ixx | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ odb/prepared-query.hxx | 23 +++++++++++ odb/result.hxx | 19 +++++++++ odb/result.txx | 52 +++++++++++++++++++++++ 5 files changed, 266 insertions(+) create mode 100644 odb/result.txx diff --git a/odb/database.hxx b/odb/database.hxx index 4e59aaf..1f56b7f 100644 --- a/odb/database.hxx +++ b/odb/database.hxx @@ -242,6 +242,56 @@ namespace odb result query (const odb::query&, bool cache = true); + // Query one API. + // + template + typename object_traits::pointer_type + query_one (); + + template + bool + query_one (T& object); + + template + T + query_value (); + + template + typename object_traits::pointer_type + query_one (const char*); + + template + bool + query_one (const char*, T& object); + + template + T + query_value (const char*); + + template + typename object_traits::pointer_type + query_one (const std::string&); + + template + bool + query_one (const std::string&, T& object); + + template + T + query_value (const std::string&); + + template + typename object_traits::pointer_type + query_one (const odb::query&); + + template + bool + query_one (const odb::query&, T& object); + + template + T + query_value (const odb::query&); + // Query preparation. // template @@ -487,6 +537,18 @@ namespace odb void erase_ (const typename object_traits::pointer_type&); + template + typename object_traits::pointer_type + query_one_ (const Q&); + + template + bool + query_one_ (const Q&, T&); + + template + T + query_value_ (const Q&); + template ::kind> diff --git a/odb/database.ixx b/odb/database.ixx index 6cfbeec..739db03 100644 --- a/odb/database.ixx +++ b/odb/database.ixx @@ -473,6 +473,90 @@ namespace odb } template + inline typename object_traits::pointer_type database:: + query_one () + { + return query_one (odb::query ()); + } + + template + inline bool database:: + query_one (T& o) + { + return query_one (odb::query (), o); + } + + template + inline T database:: + query_value () + { + return query_value (odb::query ()); + } + + template + inline typename object_traits::pointer_type database:: + query_one (const char* q) + { + return query_one (odb::query (q)); + } + + template + inline bool database:: + query_one (const char* q, T& o) + { + return query_one (odb::query (q), o); + } + + template + inline T database:: + query_value (const char* q) + { + return query_value (odb::query (q)); + } + + template + inline typename object_traits::pointer_type database:: + query_one (const std::string& q) + { + return query_one (odb::query (q)); + } + + template + inline bool database:: + query_one (const std::string& q, T& o) + { + return query_one (odb::query (q), o); + } + + template + inline T database:: + query_value (const std::string& q) + { + return query_value (odb::query (q)); + } + + template + inline bool database:: + query_one (const odb::query& q, T& o) + { + return query_one_ (q, o); + } + + template + inline typename object_traits::pointer_type database:: + query_one (const odb::query& q) + { + return query_one_ (q); + } + + template + inline T database:: + query_value (const odb::query& q) + { + return query_value_ (q); + } + + template inline prepared_query database:: prepare_query (const char* n, const char* q) { @@ -620,6 +704,32 @@ namespace odb erase_ (pointer_traits::get_ref (pobj)); } + template + inline typename object_traits::pointer_type database:: + query_one_ (const Q& q) + { + return query_::call (*this, q).one (); + } + + template + inline bool database:: + query_one_ (const Q& q, T& o) + { + return query_::call (*this, q).one (o); + } + + template + inline T database:: + query_value_ (const Q& q) + { + // Compiler error pointing here? The object must be default-constructible + // in order to use the return-by-value API. + // + T o; + query_::call (*this, q).value (o); + return o; + } + // execute() // inline unsigned long long database:: diff --git a/odb/prepared-query.hxx b/odb/prepared-query.hxx index b321564..5fc4783 100644 --- a/odb/prepared-query.hxx +++ b/odb/prepared-query.hxx @@ -86,6 +86,29 @@ namespace odb return r; } + typename object_traits::pointer_type + execute_one () + { + return execute (false).one (); + } + + bool + execute_one (T& object) + { + return execute (false).one (object); + } + + T + execute_value () + { + // Compiler error pointing here? The object must be default- + // constructible in order to use the return-by-value API. + // + T o; + execute (false).value (o); + return o; + } + const char* name () const { diff --git a/odb/result.hxx b/odb/result.hxx index 93c0dce..5f84342 100644 --- a/odb/result.hxx +++ b/odb/result.hxx @@ -209,6 +209,23 @@ namespace odb return impl_ ? impl_->size () : 0; } + // query_one() and query_value() implementation details. + // + public: + typename object_traits::pointer_type + one (); + + bool + one (T&); + + // We cannot return by value here since result can be instantiated + // for an abstract type (polymorphic abstract base) and it seems + // the signature must be valid to the point being able to call the + // necessary constructors. + // + void + value (T&); + private: friend class result; @@ -222,6 +239,8 @@ namespace odb } } +#include + #include #endif // ODB_RESULT_HXX diff --git a/odb/result.txx b/odb/result.txx new file mode 100644 index 0000000..c1bed1c --- /dev/null +++ b/odb/result.txx @@ -0,0 +1,52 @@ +// file : odb/result.txx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + template + typename object_traits::pointer_type result:: + one () + { + typedef typename object_traits::pointer_type pointer_type; + + iterator i (begin ()); + + if (i != end ()) + { + pointer_type o (i.load ()); + assert (++i == end ()); // More than one element in query_one() result. + return o; + } + + return pointer_type (); + } + + template + bool result:: + one (T& o) + { + iterator i (begin ()); + + if (i != end ()) + { + i.load (o); + assert (++i == end ()); // More than one element in query_one() result. + return true; + } + + return false; + } + + template + void result:: + value (T& o) + { + iterator i (begin ()); + assert (i != end ()); // Zero elements in query_value() result. + i.load (o); + assert (++i == end ()); // More than one element in query_value() result. + } +} -- cgit v1.1