diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-05 10:20:47 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-05 10:20:47 +0200 |
commit | 0979184f66b49440758b7fc2b81172af7fe47ad8 (patch) | |
tree | 212b703960772b904f42f08d12ed37abe86297b7 | |
parent | 141253ea17264e122e440cae441bf3d952329d35 (diff) |
Support for views; native part
-rw-r--r-- | odb/mysql/forward.hxx | 3 | ||||
-rw-r--r-- | odb/mysql/makefile | 1 | ||||
-rw-r--r-- | odb/mysql/object-result.hxx | 79 | ||||
-rw-r--r-- | odb/mysql/object-result.txx (renamed from odb/mysql/result.txx) | 23 | ||||
-rw-r--r-- | odb/mysql/object-statements.hxx | 23 | ||||
-rw-r--r-- | odb/mysql/query.hxx | 16 | ||||
-rw-r--r-- | odb/mysql/result.hxx | 64 | ||||
-rw-r--r-- | odb/mysql/statement-cache.hxx | 22 | ||||
-rw-r--r-- | odb/mysql/statements-base.cxx | 17 | ||||
-rw-r--r-- | odb/mysql/statements-base.hxx | 51 | ||||
-rw-r--r-- | odb/mysql/view-result.hxx | 74 | ||||
-rw-r--r-- | odb/mysql/view-result.txx | 152 | ||||
-rw-r--r-- | odb/mysql/view-statements.hxx | 106 | ||||
-rw-r--r-- | odb/mysql/view-statements.txx | 29 |
14 files changed, 569 insertions, 91 deletions
diff --git a/odb/mysql/forward.hxx b/odb/mysql/forward.hxx index 94b175b..08ada00 100644 --- a/odb/mysql/forward.hxx +++ b/odb/mysql/forward.hxx @@ -30,6 +30,9 @@ namespace odb class object_statements; template <typename T> + class view_statements; + + template <typename T> class container_statements; } diff --git a/odb/mysql/makefile b/odb/mysql/makefile index 91eca19..19affbd 100644 --- a/odb/mysql/makefile +++ b/odb/mysql/makefile @@ -15,6 +15,7 @@ exceptions.cxx \ object-statements.cxx \ query.cxx \ statement.cxx \ +statements-base.cxx \ transaction.cxx \ transaction-impl.cxx \ traits.cxx diff --git a/odb/mysql/object-result.hxx b/odb/mysql/object-result.hxx new file mode 100644 index 0000000..6061536 --- /dev/null +++ b/odb/mysql/object-result.hxx @@ -0,0 +1,79 @@ +// file : odb/mysql/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_MYSQL_OBJECT_RESULT_HXX +#define ODB_MYSQL_OBJECT_RESULT_HXX + +#include <odb/pre.hxx> + +#include <cstddef> // std::size_t + +#include <odb/mysql/version.hxx> +#include <odb/mysql/forward.hxx> // query, object_statements +#include <odb/mysql/result.hxx> +#include <odb/mysql/statement.hxx> + +#include <odb/details/shared-ptr.hxx> + +namespace odb +{ + namespace mysql + { + template <typename T> + class result_impl<T, class_object>: + public odb::result_impl<T, class_object> + { + 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 + fetch (); + + private: + details::shared_ptr<select_statement> statement_; + object_statements<object_type>& statements_; + std::size_t count_; + }; + } +} + +#include <odb/mysql/object-result.txx> + +#include <odb/post.hxx> + +#endif // ODB_MYSQL_OBJECT_RESULT_HXX diff --git a/odb/mysql/result.txx b/odb/mysql/object-result.txx index a010458..3b0c0ca 100644 --- a/odb/mysql/result.txx +++ b/odb/mysql/object-result.txx @@ -1,4 +1,4 @@ -// file : odb/mysql/result.txx +// file : odb/mysql/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,22 +6,24 @@ #include <odb/callback.hxx> #include <odb/exceptions.hxx> +#include <odb/mysql/object-statements.hxx> + namespace odb { namespace mysql { 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&, details::shared_ptr<select_statement> statement, object_statements<object_type>& statements) - : odb::result_impl<T> (statements.connection ().database ()), + : base_type (statements.connection ().database ()), statement_ (statement), statements_ (statements), count_ (0) @@ -29,7 +31,7 @@ namespace odb } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: load (object_type& obj) { if (count_ > statement_->fetched ()) @@ -67,7 +69,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 () { if (count_ > statement_->fetched ()) @@ -77,7 +80,7 @@ namespace odb } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: next () { this->current (pointer_type ()); @@ -96,7 +99,7 @@ namespace odb } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: fetch () { // If the result is cached, the image can grow between calls @@ -157,7 +160,7 @@ namespace odb } template <typename T> - void result_impl<T>:: + void result_impl<T, class_object>:: cache () { if (!statement_->cached ()) @@ -170,7 +173,7 @@ namespace odb } template <typename T> - std::size_t result_impl<T>:: + std::size_t result_impl<T, class_object>:: size () { if (!statement_->cached ()) diff --git a/odb/mysql/object-statements.hxx b/odb/mysql/object-statements.hxx index 3f3196d..cd06010 100644 --- a/odb/mysql/object-statements.hxx +++ b/odb/mysql/object-statements.hxx @@ -16,11 +16,12 @@ #include <odb/traits.hxx> #include <odb/cache-traits.hxx> +#include <odb/details/shared-ptr.hxx> + #include <odb/mysql/mysql.hxx> #include <odb/mysql/version.hxx> #include <odb/mysql/statement.hxx> - -#include <odb/details/shared-ptr.hxx> +#include <odb/mysql/statements-base.hxx> #include <odb/mysql/details/export.hxx> @@ -28,22 +29,11 @@ namespace odb { namespace mysql { - class connection; - - class LIBODB_MYSQL_EXPORT object_statements_base: - public details::shared_base + class LIBODB_MYSQL_EXPORT object_statements_base: public statements_base { - public: - typedef mysql::connection connection_type; - - connection_type& - connection () - { - return conn_; - } - // Locking. // + public: void lock () { @@ -70,7 +60,7 @@ namespace odb protected: object_statements_base (connection_type& conn) - : conn_ (conn), locked_ (false) + : statements_base (conn), locked_ (false) { } @@ -91,7 +81,6 @@ namespace odb }; protected: - connection_type& conn_; bool locked_; }; diff --git a/odb/mysql/query.hxx b/odb/mysql/query.hxx index 0fea50c..17aa724 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -1773,7 +1773,7 @@ namespace odb namespace odb { template <typename T> - class query<T, mysql::query>: public object_traits<T>::query_type + class query<T, mysql::query>: public query_selector<T>::type { public: // We don't define any typedefs here since they may clash with @@ -1786,40 +1786,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 (mysql::val_bind<T2> v) - : object_traits<T>::query_type (mysql::query (v)) + : query_selector<T>::type (mysql::query (v)) { } template <typename T2> explicit query (mysql::ref_bind<T2> r) - : object_traits<T>::query_type (mysql::query (r)) + : query_selector<T>::type (mysql::query (r)) { } query (const mysql::query& q) - : object_traits<T>::query_type (q) + : query_selector<T>::type (q) { } template <mysql::database_type_id ID> query (const mysql::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/mysql/result.hxx b/odb/mysql/result.hxx index ebc3e02..33df541 100644 --- a/odb/mysql/result.hxx +++ b/odb/mysql/result.hxx @@ -8,72 +8,28 @@ #include <odb/pre.hxx> -#include <cstddef> // std::size_t - +#include <odb/traits.hxx> #include <odb/result.hxx> #include <odb/mysql/version.hxx> #include <odb/mysql/forward.hxx> -#include <odb/mysql/statement.hxx> - -#include <odb/details/shared-ptr.hxx> namespace odb { namespace mysql { - class query; - - template <typename T> - class result_impl: public odb::result_impl<T> - { - 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> statement, - object_statements<object_type>& statements); - - 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 - fetch (); - - private: - details::shared_ptr<select_statement> statement_; - object_statements<object_type>& statements_; - std::size_t count_; - }; + template <typename T, class_kind kind> + class result_impl; } } -#include <odb/mysql/result.txx> - #include <odb/post.hxx> #endif // ODB_MYSQL_RESULT_HXX + +// Include result specializations so that the user code only needs +// to include this header. +// + +#include <odb/mysql/object-result.hxx> +#include <odb/mysql/view-result.hxx> diff --git a/odb/mysql/statement-cache.hxx b/odb/mysql/statement-cache.hxx index 681bcf2..d5ede48 100644 --- a/odb/mysql/statement-cache.hxx +++ b/odb/mysql/statement-cache.hxx @@ -14,7 +14,9 @@ #include <odb/forward.hxx> #include <odb/mysql/version.hxx> +#include <odb/mysql/statements-base.hxx> #include <odb/mysql/object-statements.hxx> +#include <odb/mysql/view-statements.hxx> #include <odb/details/shared-ptr.hxx> #include <odb/details/type-info.hxx> @@ -37,7 +39,7 @@ namespace odb template <typename T> object_statements<T>& - find () + find_object () { map::iterator i (map_.find (&typeid (T))); @@ -51,9 +53,25 @@ 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: 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/mysql/statements-base.cxx b/odb/mysql/statements-base.cxx new file mode 100644 index 0000000..02d9b45 --- /dev/null +++ b/odb/mysql/statements-base.cxx @@ -0,0 +1,17 @@ +// file : odb/mysql/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/mysql/statements-base.hxx> + +namespace odb +{ + namespace mysql + { + statements_base:: + ~statements_base () + { + } + } +} diff --git a/odb/mysql/statements-base.hxx b/odb/mysql/statements-base.hxx new file mode 100644 index 0000000..24f5cb2 --- /dev/null +++ b/odb/mysql/statements-base.hxx @@ -0,0 +1,51 @@ +// file : odb/mysql/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_MYSQL_STATEMENTS_BASE_HXX +#define ODB_MYSQL_STATEMENTS_BASE_HXX + +#include <odb/pre.hxx> + +#include <odb/details/shared-ptr.hxx> + +#include <odb/mysql/version.hxx> +#include <odb/mysql/forward.hxx> // connection + +#include <odb/mysql/details/export.hxx> + +namespace odb +{ + namespace mysql + { + class LIBODB_MYSQL_EXPORT statements_base: public details::shared_base + { + public: + typedef mysql::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_MYSQL_STATEMENTS_BASE_HXX diff --git a/odb/mysql/view-result.hxx b/odb/mysql/view-result.hxx new file mode 100644 index 0000000..890ca42 --- /dev/null +++ b/odb/mysql/view-result.hxx @@ -0,0 +1,74 @@ +// file : odb/mysql/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_MYSQL_VIEW_RESULT_HXX +#define ODB_MYSQL_VIEW_RESULT_HXX + +#include <odb/pre.hxx> + +#include <cstddef> // std::size_t + +#include <odb/mysql/version.hxx> +#include <odb/mysql/forward.hxx> // query, view_statements +#include <odb/mysql/result.hxx> +#include <odb/mysql/statement.hxx> + +#include <odb/details/shared-ptr.hxx> + +namespace odb +{ + namespace mysql + { + template <typename T> + class result_impl<T, class_view>: public odb::result_impl<T, class_view> + { + 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: + void + fetch (); + + private: + details::shared_ptr<select_statement> statement_; + view_statements<view_type>& statements_; + std::size_t count_; + }; + } +} + +#include <odb/mysql/view-result.txx> + +#include <odb/post.hxx> + +#endif // ODB_MYSQL_VIEW_RESULT_HXX diff --git a/odb/mysql/view-result.txx b/odb/mysql/view-result.txx new file mode 100644 index 0000000..51f3d6f --- /dev/null +++ b/odb/mysql/view-result.txx @@ -0,0 +1,152 @@ +// file : odb/mysql/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/mysql/view-statements.hxx> + +namespace odb +{ + namespace mysql + { + template <typename T> + result_impl<T, class_view>:: + ~result_impl () + { + } + + template <typename T> + result_impl<T, class_view>:: + result_impl (const query&, + details::shared_ptr<select_statement> statement, + view_statements<view_type>& statements) + : base_type (statements.connection ().database ()), + statement_ (statement), + statements_ (statements), + count_ (0) + { + } + + template <typename T> + void result_impl<T, class_view>:: + load (view_type& view) + { + if (count_ > statement_->fetched ()) + fetch (); + + odb::database& db (this->database ()); + view_traits::callback (db, view, callback_event::pre_load); + + typename view_traits::image_type& i (statements_.image ()); + view_traits::init (view, i); + + view_traits::callback (db, view, callback_event::post_load); + } + + template <typename T> + void result_impl<T, class_view>:: + next () + { + this->current (pointer_type ()); + + // If we are cached, simply increment the position and + // postpone the actual row fetching until later. This way + // if the same view is loaded in between iteration, the + // image won't be messed up. + // + count_++; + + if (statement_->cached ()) + this->end_ = count_ > statement_->result_size (); + else + fetch (); + } + + template <typename T> + void result_impl<T, class_view>:: + fetch () + { + // If the result is cached, the image can grow between calls + // to fetch() as a result of other statements execution. + // + if (statement_->cached ()) + { + 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++; + } + } + + while (!this->end_ && count_ > statement_->fetched ()) + { + select_statement::result r (statement_->fetch ()); + + switch (r) + { + case select_statement::truncated: + { + // Don't re-fetch data we are skipping. + // + if (count_ != statement_->fetched ()) + continue; + + typename view_traits::image_type& im (statements_.image ()); + + 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_->refetch (); + } + // Fall throught. + } + case select_statement::success: + { + break; + } + case select_statement::no_data: + { + this->end_ = true; + break; + } + } + } + } + + template <typename T> + void result_impl<T, class_view>:: + cache () + { + if (!statement_->cached ()) + { + statement_->cache (); + + if (count_ >= statement_->result_size ()) + this->end_ = true; + } + } + + template <typename T> + std::size_t result_impl<T, class_view>:: + size () + { + if (!statement_->cached ()) + throw result_not_cached (); + + return statement_->result_size (); + } + } +} diff --git a/odb/mysql/view-statements.hxx b/odb/mysql/view-statements.hxx new file mode 100644 index 0000000..5a07b34 --- /dev/null +++ b/odb/mysql/view-statements.hxx @@ -0,0 +1,106 @@ +// file : odb/mysql/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_MYSQL_VIEW_STATEMENTS_HXX +#define ODB_MYSQL_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/mysql/mysql.hxx> +#include <odb/mysql/version.hxx> +#include <odb/mysql/statement.hxx> +#include <odb/mysql/statements-base.hxx> + +namespace odb +{ + namespace mysql + { + 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 mysql::select_statement query_statement_type; + + public: + view_statements (connection_type&); + + // 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_; + } + + my_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_)); + + 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_; + MYSQL_BIND image_bind_[view_traits::column_count]; + my_bool image_truncated_[view_traits::column_count]; + + details::shared_ptr<query_statement_type> query_; + }; + } +} + +#include <odb/mysql/view-statements.txx> + +#include <odb/post.hxx> + +#endif // ODB_MYSQL_VIEW_STATEMENTS_HXX diff --git a/odb/mysql/view-statements.txx b/odb/mysql/view-statements.txx new file mode 100644 index 0000000..36f5045 --- /dev/null +++ b/odb/mysql/view-statements.txx @@ -0,0 +1,29 @@ +// file : odb/mysql/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 mysql + { + 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].error = image_truncated_ + i; + } + } +} |