From ec6b9f59d40b2c389496f8e6af6bce64944af998 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Aug 2010 11:16:42 +0200 Subject: Add query support --- odb/database.hxx | 16 +++++ odb/database.txx | 24 +++++++ odb/forward.hxx | 3 + odb/query.hxx | 38 +++++++++++ odb/result.hxx | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ odb/result.txx | 13 ++++ 6 files changed, 282 insertions(+) create mode 100644 odb/query.hxx create mode 100644 odb/result.hxx create mode 100644 odb/result.txx diff --git a/odb/database.hxx b/odb/database.hxx index bfd6b0b..1b3a6d6 100644 --- a/odb/database.hxx +++ b/odb/database.hxx @@ -8,6 +8,8 @@ #include #include +#include +#include namespace odb { @@ -66,6 +68,20 @@ namespace odb void erase (typename object_traits::id_type const& id); + // Object query API. + // + template + shared_ptr > + query (); + + template + shared_ptr > + query (const std::string&); + + template + shared_ptr > + query (const odb::query&); + // Transaction API. // public: diff --git a/odb/database.txx b/odb/database.txx index 402fc0b..7440127 100644 --- a/odb/database.txx +++ b/odb/database.txx @@ -92,4 +92,28 @@ namespace odb object_traits::erase (*this, id); } + + template + shared_ptr > database:: + query () + { + return query (odb::query ()); + } + + template + shared_ptr > database:: + query (const std::string& q) + { + return query (odb::query (q)); + } + + template + shared_ptr > database:: + query (const odb::query& q) + { + if (!transaction::has_current ()) + throw not_in_transaction (); + + return object_traits::query (*this, q); + } } diff --git a/odb/forward.hxx b/odb/forward.hxx index 8e91407..7c1fb27 100644 --- a/odb/forward.hxx +++ b/odb/forward.hxx @@ -12,6 +12,9 @@ namespace odb class transaction; template + class query; + + template class shared_ptr; class access diff --git a/odb/query.hxx b/odb/query.hxx new file mode 100644 index 0000000..d52e1cc --- /dev/null +++ b/odb/query.hxx @@ -0,0 +1,38 @@ +// file : odb/query.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_QUERY_HXX +#define ODB_QUERY_HXX + +#include + +#include +#include + +namespace odb +{ + template + class query: public object_traits::query_type + { + public: + typedef typename object_traits::query_type base_type; + + query () + { + } + + query (const std::string& q) + : base_type (q) + { + } + + query (const base_type& q) + : base_type (q) + { + } + }; +} + +#endif // ODB_QUERY_HXX diff --git a/odb/result.hxx b/odb/result.hxx new file mode 100644 index 0000000..979988a --- /dev/null +++ b/odb/result.hxx @@ -0,0 +1,188 @@ +// file : odb/result.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_RESULT_HXX +#define ODB_RESULT_HXX + +#include // std::ptrdiff_t, std::size_t +#include // iterator categories + +#include +#include + +namespace odb +{ + template + class result; + + template + class result_impl; + + template + class result_iterator + { + public: + typedef typename object_traits::pointer_type value_type; + typedef value_type& reference; + typedef value_type* pointer; + typedef std::ptrdiff_t difference_type; + typedef std::input_iterator_tag iterator_category; + + public: + result_iterator () + : res_ (0) + { + } + + explicit + result_iterator (result_impl& res) + : res_ (&res) + { + } + + // Input iterator requirements. + // + public: + value_type + operator* () const + { + return res_->current (true); + } + + // Our value_type is already a pointer so return it instead of + // a pointer to it (operator-> will just have to go one deeper + // in the latter case). + // + value_type + operator-> () const + { + return res_->current (false); + } + + result_iterator& + operator++ () + { + res_->next (); + return *this; + } + + result_iterator + operator++ (int) + { + // All non-end iterators for a result object move together. + // + res_->next (); + return *this; + } + + bool + equal (const result_iterator& j) + { + return (res_ ? res_->current (false) : 0) == + (j.res_ ? j.res_->current (false) : 0); + } + + private: + result_impl* res_; + }; + + // Input iterator requirements. + // + template + inline bool + operator== (result_iterator i, result_iterator j) + { + return i.equal (j); + } + + template + inline bool + operator!= (result_iterator i, result_iterator j) + { + return !i.equal (j); + } + + // + // + template + class result_impl: public shared_base + { + public: + virtual + ~result_impl (); + + protected: + friend class result; + friend class result_iterator; + + virtual typename object_traits::pointer_type + current (bool release) = 0; + + virtual void + next () = 0; + }; + + template + class result + { + public: + typedef typename object_traits::pointer_type value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; + + typedef result_iterator iterator; + + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + public: + result (shared_ptr > impl) + : impl_ (impl) + { + } + + /* + result& + operator= (shared_ptr > impl) + { + impl_ = impl; + } + */ + + public: + iterator + begin () + { + return iterator (*impl_); + } + + iterator + end () + { + return iterator (); + } + + public: + operator shared_ptr > () + { + return impl_; + } + + // Copying or assignment of results is not supported. + // + private: + result (const result&); + result& operator= (const result&); + + private: + shared_ptr > impl_; + }; +} + +#include + +#endif // ODB_RESULT_HXX diff --git a/odb/result.txx b/odb/result.txx new file mode 100644 index 0000000..4b77c7e --- /dev/null +++ b/odb/result.txx @@ -0,0 +1,13 @@ +// file : odb/result.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + template + result_impl:: + ~result_impl () + { + } +} -- cgit v1.1