diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-06 16:54:06 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-06 16:54:06 +0200 |
commit | 1b1cb92a27592da6ab9300b2c5c8384f7588e2af (patch) | |
tree | 2c4a92f14c303ff3aaced4e7b073f7180c0c5d6a | |
parent | 7d3f2b0badf4298bca53936d22a0a22c74538713 (diff) |
Support for views; native part
-rw-r--r-- | odb/sqlite/forward.hxx | 3 | ||||
-rw-r--r-- | odb/sqlite/makefile | 1 | ||||
-rw-r--r-- | odb/sqlite/object-result.hxx | 77 | ||||
-rw-r--r-- | odb/sqlite/object-result.txx (renamed from odb/sqlite/result.txx) | 31 | ||||
-rw-r--r-- | odb/sqlite/object-statements.hxx | 26 | ||||
-rw-r--r-- | odb/sqlite/object-statements.txx | 6 | ||||
-rw-r--r-- | odb/sqlite/query.hxx | 16 | ||||
-rw-r--r-- | odb/sqlite/result.hxx | 59 | ||||
-rw-r--r-- | odb/sqlite/statement-cache.hxx | 24 | ||||
-rw-r--r-- | odb/sqlite/statements-base.cxx | 17 | ||||
-rw-r--r-- | odb/sqlite/statements-base.hxx | 51 | ||||
-rw-r--r-- | odb/sqlite/view-result.hxx | 69 | ||||
-rw-r--r-- | odb/sqlite/view-result.txx | 96 | ||||
-rw-r--r-- | odb/sqlite/view-statements.hxx | 113 | ||||
-rw-r--r-- | odb/sqlite/view-statements.txx | 35 |
15 files changed, 537 insertions, 87 deletions
diff --git a/odb/sqlite/forward.hxx b/odb/sqlite/forward.hxx index a4e3376..5203434 100644 --- a/odb/sqlite/forward.hxx +++ b/odb/sqlite/forward.hxx @@ -30,6 +30,9 @@ namespace odb class object_statements; template <typename T> + class view_statements; + + template <typename T> class container_statements; class query_params; diff --git a/odb/sqlite/makefile b/odb/sqlite/makefile index feb62eb..e9115a7 100644 --- a/odb/sqlite/makefile +++ b/odb/sqlite/makefile @@ -16,6 +16,7 @@ query.cxx \ result.cxx \ statement-cache.cxx \ statement.cxx \ +statements-base.cxx \ traits.cxx \ transaction.cxx \ transaction-impl.cxx diff --git a/odb/sqlite/object-result.hxx b/odb/sqlite/object-result.hxx new file mode 100644 index 0000000..b3dbf2b --- /dev/null +++ b/odb/sqlite/object-result.hxx @@ -0,0 +1,77 @@ +// file : odb/sqlite/object-result.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_OBJECT_RESULT_HXX +#define ODB_SQLITE_OBJECT_RESULT_HXX + +#include <odb/pre.hxx> + +#include <cstddef> // std::size_t + +#include <odb/details/shared-ptr.hxx> + +#include <odb/sqlite/version.hxx> +#include <odb/sqlite/forward.hxx> // query, object_statements +#include <odb/sqlite/result.hxx> +#include <odb/sqlite/statement.hxx> + +namespace odb +{ + namespace sqlite + { + template <typename T> + class result_impl<T, class_object>: + public odb::result_impl<T, class_object>, + public result_impl_base + { + public: + typedef odb::result_impl<T, class_object> base_type; + + typedef typename base_type::pointer_type pointer_type; + typedef typename base_type::pointer_traits pointer_traits; + + typedef typename base_type::object_type object_type; + typedef typename base_type::id_type id_type; + typedef typename base_type::object_traits object_traits; + + virtual + ~result_impl (); + + result_impl (const query&, + details::shared_ptr<select_statement>, + object_statements<object_type>&); + + virtual void + load (object_type&); + + virtual id_type + load_id (); + + virtual void + next (); + + virtual void + cache (); + + virtual std::size_t + size (); + + using base_type::current; + + private: + void + load_image (); + + private: + object_statements<object_type>& statements_; + }; + } +} + +#include <odb/sqlite/object-result.txx> + +#include <odb/post.hxx> + +#endif // ODB_SQLITE_OBJECT_RESULT_HXX diff --git a/odb/sqlite/result.txx b/odb/sqlite/object-result.txx index 9bcfe5f..7675262 100644 --- a/odb/sqlite/result.txx +++ b/odb/sqlite/object-result.txx @@ -1,4 +1,4 @@ -// file : odb/sqlite/result.txx +// file : odb/sqlite/object-result.txx // author : Boris Kolpackov <boris@codesynthesis.com> // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file @@ -6,29 +6,31 @@ #include <odb/callback.hxx> #include <odb/exceptions.hxx> +#include <odb/sqlite/object-statements.hxx> + namespace odb { namespace sqlite { template <typename T> - result_impl<T>:: + result_impl<T, class_object>:: ~result_impl () { } template <typename T> - result_impl<T>:: + result_impl<T, class_object>:: result_impl (const query& q, - details::shared_ptr<select_statement> st, - object_statements<object_type>& sts) - : odb::result_impl<T> (sts.connection ().database ()), - result_impl_base (q, st), - statements_ (sts) + details::shared_ptr<select_statement> statement, + object_statements<object_type>& statements) + : base_type (statements.connection ().database ()), + result_impl_base (q, statement), + statements_ (statements) { } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: load (object_type& obj) { load_image (); @@ -65,7 +67,8 @@ namespace odb } template <typename T> - typename result_impl<T>::id_type result_impl<T>:: + typename result_impl<T, class_object>::id_type + result_impl<T, class_object>:: load_id () { load_image (); @@ -73,7 +76,7 @@ namespace odb } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: next () { this->current (pointer_type ()); @@ -83,7 +86,7 @@ namespace odb } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: load_image () { // The image can grow between calls to load() as a result of other @@ -118,13 +121,13 @@ namespace odb } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: cache () { } template <typename T> - std::size_t result_impl<T>:: + std::size_t result_impl<T, class_object>:: size () { throw result_not_cached (); diff --git a/odb/sqlite/object-statements.hxx b/odb/sqlite/object-statements.hxx index c9c101d..4816d66 100644 --- a/odb/sqlite/object-statements.hxx +++ b/odb/sqlite/object-statements.hxx @@ -15,31 +15,23 @@ #include <odb/forward.hxx> #include <odb/traits.hxx> #include <odb/cache-traits.hxx> + #include <odb/details/shared-ptr.hxx> #include <odb/sqlite/version.hxx> #include <odb/sqlite/binding.hxx> #include <odb/sqlite/statement.hxx> +#include <odb/sqlite/statements-base.hxx> + #include <odb/sqlite/details/export.hxx> namespace odb { namespace sqlite { - class connection; - - class LIBODB_SQLITE_EXPORT object_statements_base: - public details::shared_base + class LIBODB_SQLITE_EXPORT object_statements_base: public statements_base { public: - typedef sqlite::connection connection_type; - - connection_type& - connection () - { - return conn_; - } - // Locking. // void @@ -68,7 +60,7 @@ namespace odb protected: object_statements_base (connection_type& conn) - : conn_ (conn), locked_ (false) + : statements_base (conn), locked_ (false) { } @@ -89,7 +81,6 @@ namespace odb }; protected: - connection_type& conn_; bool locked_; }; @@ -150,10 +141,13 @@ namespace odb bool locked_; }; - // - // + + public: object_statements (connection_type&); + virtual + ~object_statements (); + // Delayed loading. // void diff --git a/odb/sqlite/object-statements.txx b/odb/sqlite/object-statements.txx index 9cd8344..04d57c8 100644 --- a/odb/sqlite/object-statements.txx +++ b/odb/sqlite/object-statements.txx @@ -18,6 +18,12 @@ namespace odb { template <typename T> object_statements<T>:: + ~object_statements () + { + } + + template <typename T> + object_statements<T>:: object_statements (connection_type& conn) : object_statements_base (conn), container_statement_cache_ (conn), diff --git a/odb/sqlite/query.hxx b/odb/sqlite/query.hxx index 3179bda..f413756 100644 --- a/odb/sqlite/query.hxx +++ b/odb/sqlite/query.hxx @@ -1199,7 +1199,7 @@ namespace odb namespace odb { template <typename T> - class query<T, sqlite::query>: public object_traits<T>::query_type + class query<T, sqlite::query>: public query_selector<T>::type { public: // We don't define any typedefs here since they may clash with @@ -1212,40 +1212,40 @@ namespace odb explicit query (const std::string& q) - : object_traits<T>::query_type (q) + : query_selector<T>::type (q) { } template <typename T2> explicit query (sqlite::val_bind<T2> v) - : object_traits<T>::query_type (sqlite::query (v)) + : query_selector<T>::type (sqlite::query (v)) { } template <typename T2> explicit query (sqlite::ref_bind<T2> r) - : object_traits<T>::query_type (sqlite::query (r)) + : query_selector<T>::type (sqlite::query (r)) { } query (const sqlite::query& q) - : object_traits<T>::query_type (q) + : query_selector<T>::type (q) { } template <sqlite::database_type_id ID> query (const sqlite::query_column<bool, ID>& qc) - : object_traits<T>::query_type (qc) + : query_selector<T>::type (qc) { } std::string clause () const { - return object_traits<T>::query_type::clause ( - object_traits<T>::table_name); + return query_selector<T>::type::clause ( + query_selector<T>::table_name ()); } }; } diff --git a/odb/sqlite/result.hxx b/odb/sqlite/result.hxx index 4f1cc2c..1417a6c 100644 --- a/odb/sqlite/result.hxx +++ b/odb/sqlite/result.hxx @@ -8,14 +8,15 @@ #include <odb/pre.hxx> -#include <cstddef> // std::size_t - +#include <odb/traits.hxx> #include <odb/result.hxx> + #include <odb/details/shared-ptr.hxx> #include <odb/sqlite/version.hxx> #include <odb/sqlite/forward.hxx> // query, query_params #include <odb/sqlite/statement.hxx> + #include <odb/sqlite/details/export.hxx> namespace odb @@ -35,54 +36,18 @@ namespace odb details::shared_ptr<select_statement> statement_; }; - template <typename T> - class result_impl: public odb::result_impl<T>, public result_impl_base - { - public: - typedef typename odb::result_impl<T>::pointer_type pointer_type; - typedef typename odb::result_impl<T>::pointer_traits pointer_traits; - - typedef typename odb::result_impl<T>::object_type object_type; - typedef typename odb::result_impl<T>::id_type id_type; - typedef typename odb::result_impl<T>::object_traits object_traits; - - - virtual - ~result_impl (); - - result_impl (const query&, - details::shared_ptr<select_statement>, - object_statements<object_type>&); - - virtual void - load (object_type&); - - virtual id_type - load_id (); - - virtual void - next (); - - virtual void - cache (); - - virtual std::size_t - size (); - - using odb::result_impl<T>::current; - - private: - void - load_image (); - - private: - object_statements<object_type>& statements_; - }; + template <typename T, class_kind kind> + class result_impl; } } -#include <odb/sqlite/result.txx> - #include <odb/post.hxx> #endif // ODB_SQLITE_RESULT_HXX + +// Include result specializations so that the user code only needs +// to include this header. +// + +#include <odb/sqlite/object-result.hxx> +#include <odb/sqlite/view-result.hxx> diff --git a/odb/sqlite/statement-cache.hxx b/odb/sqlite/statement-cache.hxx index ca4ab2e..16e0c15 100644 --- a/odb/sqlite/statement-cache.hxx +++ b/odb/sqlite/statement-cache.hxx @@ -12,12 +12,16 @@ #include <typeinfo> #include <odb/forward.hxx> + #include <odb/details/shared-ptr.hxx> #include <odb/details/type-info.hxx> #include <odb/sqlite/version.hxx> #include <odb/sqlite/statement.hxx> +#include <odb/sqlite/statements-base.hxx> #include <odb/sqlite/object-statements.hxx> +#include <odb/sqlite/view-statements.hxx> + #include <odb/sqlite/details/export.hxx> namespace odb @@ -69,7 +73,7 @@ namespace odb template <typename T> object_statements<T>& - find () + find_object () { map::iterator i (map_.find (&typeid (T))); @@ -83,6 +87,22 @@ namespace odb return *p; } + template <typename T> + view_statements<T>& + find_view () + { + map::iterator i (map_.find (&typeid (T))); + + if (i != map_.end ()) + return static_cast<view_statements<T>&> (*i->second); + + details::shared_ptr<view_statements<T> > p ( + new (details::shared) view_statements<T> (conn_)); + + map_.insert (map::value_type (&typeid (T), p)); + return *p; + } + private: void begin_immediate_statement_ () const; @@ -92,7 +112,7 @@ namespace odb private: typedef std::map<const std::type_info*, - details::shared_ptr<object_statements_base>, + details::shared_ptr<statements_base>, details::type_info_comparator> map; connection& conn_; diff --git a/odb/sqlite/statements-base.cxx b/odb/sqlite/statements-base.cxx new file mode 100644 index 0000000..9379047 --- /dev/null +++ b/odb/sqlite/statements-base.cxx @@ -0,0 +1,17 @@ +// file : odb/sqlite/statements-base.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/sqlite/statements-base.hxx> + +namespace odb +{ + namespace sqlite + { + statements_base:: + ~statements_base () + { + } + } +} diff --git a/odb/sqlite/statements-base.hxx b/odb/sqlite/statements-base.hxx new file mode 100644 index 0000000..a2d5a81 --- /dev/null +++ b/odb/sqlite/statements-base.hxx @@ -0,0 +1,51 @@ +// file : odb/sqlite/statements-base.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_STATEMENTS_BASE_HXX +#define ODB_SQLITE_STATEMENTS_BASE_HXX + +#include <odb/pre.hxx> + +#include <odb/details/shared-ptr.hxx> + +#include <odb/sqlite/version.hxx> +#include <odb/sqlite/forward.hxx> // connection + +#include <odb/sqlite/details/export.hxx> + +namespace odb +{ + namespace sqlite + { + class LIBODB_SQLITE_EXPORT statements_base: public details::shared_base + { + public: + typedef sqlite::connection connection_type; + + connection_type& + connection () + { + return conn_; + } + + public: + virtual + ~statements_base (); + + protected: + statements_base (connection_type& conn) + : conn_ (conn) + { + } + + protected: + connection_type& conn_; + }; + } +} + +#include <odb/post.hxx> + +#endif // ODB_SQLITE_STATEMENTS_BASE_HXX diff --git a/odb/sqlite/view-result.hxx b/odb/sqlite/view-result.hxx new file mode 100644 index 0000000..7f65c4b --- /dev/null +++ b/odb/sqlite/view-result.hxx @@ -0,0 +1,69 @@ +// file : odb/sqlite/view-result.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_VIEW_RESULT_HXX +#define ODB_SQLITE_VIEW_RESULT_HXX + +#include <odb/pre.hxx> + +#include <cstddef> // std::size_t + +#include <odb/details/shared-ptr.hxx> + +#include <odb/sqlite/version.hxx> +#include <odb/sqlite/forward.hxx> // query, view_statements +#include <odb/sqlite/result.hxx> +#include <odb/sqlite/statement.hxx> + +namespace odb +{ + namespace sqlite + { + template <typename T> + class result_impl<T, class_view>: + public odb::result_impl<T, class_view>, + public result_impl_base + { + public: + typedef odb::result_impl<T, class_view> base_type; + + typedef typename base_type::pointer_type pointer_type; + typedef typename base_type::pointer_traits pointer_traits; + + typedef typename base_type::view_type view_type; + typedef typename base_type::view_traits view_traits; + + virtual + ~result_impl (); + + result_impl (const query&, + details::shared_ptr<select_statement>, + view_statements<view_type>&); + + virtual void + load (view_type&); + + virtual void + next (); + + virtual void + cache (); + + virtual std::size_t + size (); + + using base_type::current; + + private: + view_statements<view_type>& statements_; + }; + } +} + +#include <odb/sqlite/view-result.txx> + +#include <odb/post.hxx> + +#endif // ODB_SQLITE_VIEW_RESULT_HXX diff --git a/odb/sqlite/view-result.txx b/odb/sqlite/view-result.txx new file mode 100644 index 0000000..eec8b0a --- /dev/null +++ b/odb/sqlite/view-result.txx @@ -0,0 +1,96 @@ +// file : odb/sqlite/view-result.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/callback.hxx> +#include <odb/exceptions.hxx> + +#include <odb/sqlite/view-statements.hxx> + +namespace odb +{ + namespace sqlite + { + template <typename T> + result_impl<T, class_view>:: + ~result_impl () + { + } + + template <typename T> + result_impl<T, class_view>:: + result_impl (const query& q, + details::shared_ptr<select_statement> statement, + view_statements<view_type>& statements) + : base_type (statements.connection ().database ()), + result_impl_base (q, statement), + statements_ (statements) + { + } + + template <typename T> + void result_impl<T, class_view>:: + load (view_type& view) + { + // The image can grow between calls to load() as a result of other + // statements execution. + // + typename view_traits::image_type& im (statements_.image ()); + + if (im.version != statements_.image_version ()) + { + binding& b (statements_.image_binding ()); + view_traits::bind (b.bind, im); + statements_.image_version (im.version); + b.version++; + } + + select_statement::result r (statement_->load ()); + + if (r == select_statement::truncated) + { + if (view_traits::grow (im, statements_.image_truncated ())) + im.version++; + + if (im.version != statements_.image_version ()) + { + binding& b (statements_.image_binding ()); + view_traits::bind (b.bind, im); + statements_.image_version (im.version); + b.version++; + statement_->reload (); + } + } + + odb::database& db (this->database ()); + + view_traits::callback (db, view, callback_event::pre_load); + view_traits::init (view, im); + view_traits::callback (db, view, callback_event::post_load); + } + + template <typename T> + void result_impl<T, class_view>:: + next () + { + this->current (pointer_type ()); + + if (!statement_->next ()) + this->end_ = true; + } + + template <typename T> + void result_impl<T, class_view>:: + cache () + { + } + + template <typename T> + std::size_t result_impl<T, class_view>:: + size () + { + throw result_not_cached (); + } + } +} diff --git a/odb/sqlite/view-statements.hxx b/odb/sqlite/view-statements.hxx new file mode 100644 index 0000000..e940495 --- /dev/null +++ b/odb/sqlite/view-statements.hxx @@ -0,0 +1,113 @@ +// file : odb/sqlite/view-statements.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_VIEW_STATEMENTS_HXX +#define ODB_SQLITE_VIEW_STATEMENTS_HXX + +#include <odb/pre.hxx> + +#include <cstddef> // std::size_t + +#include <odb/forward.hxx> +#include <odb/traits.hxx> + +#include <odb/details/shared-ptr.hxx> + +#include <odb/sqlite/version.hxx> +#include <odb/sqlite/binding.hxx> +#include <odb/sqlite/statement.hxx> +#include <odb/sqlite/statements-base.hxx> + +namespace odb +{ + namespace sqlite + { + template <typename T> + class view_statements: public statements_base + { + public: + typedef T view_type; + typedef odb::view_traits<view_type> view_traits; + typedef typename view_traits::pointer_type pointer_type; + typedef typename view_traits::image_type image_type; + + typedef sqlite::select_statement query_statement_type; + + public: + view_statements (connection_type&); + + virtual + ~view_statements (); + + // View image. + // + image_type& + image () + { + return image_; + } + + std::size_t + image_version () const + { + return image_version_; + } + + void + image_version (std::size_t v) + { + image_version_ = v; + } + + binding& + image_binding () + { + return image_binding_; + } + + bool* + image_truncated () + { + return image_truncated_; + } + + query_statement_type& + query_statement () + { + if (query_ == 0) + { + query_.reset ( + new (details::shared) query_statement_type ( + conn_, + view_traits::query_statement, + image_binding_)); + + query_->cached (true); + } + + return *query_; + } + + private: + view_statements (const view_statements&); + view_statements& operator= (const view_statements&); + + private: + image_type image_; + std::size_t image_version_; + binding image_binding_; + bind image_bind_[view_traits::column_count]; + bool image_truncated_[view_traits::column_count]; + + details::shared_ptr<query_statement_type> query_; + }; + } +} + +#include <odb/sqlite/view-statements.txx> + +#include <odb/post.hxx> + +#endif // ODB_SQLITE_VIEW_STATEMENTS_HXX diff --git a/odb/sqlite/view-statements.txx b/odb/sqlite/view-statements.txx new file mode 100644 index 0000000..790a537 --- /dev/null +++ b/odb/sqlite/view-statements.txx @@ -0,0 +1,35 @@ +// file : odb/sqlite/view-statements.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <cstddef> // std::size_t +#include <cstring> // std::memset + +namespace odb +{ + namespace sqlite + { + template <typename T> + view_statements<T>:: + ~view_statements () + { + } + + template <typename T> + view_statements<T>:: + view_statements (connection_type& conn) + : statements_base (conn), + image_binding_ (image_bind_, view_traits::column_count) + { + image_.version = 0; + image_version_ = 0; + + std::memset (image_bind_, 0, sizeof (image_bind_)); + std::memset (image_truncated_, 0, sizeof (image_truncated_)); + + for (std::size_t i (0); i < view_traits::column_count; ++i) + image_bind_[i].truncated = image_truncated_ + i; + } + } +} |