From 2f920671a7e4be7eb488724ae19360a87d66860c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 27 Oct 2011 15:16:49 +0200 Subject: Add support for persistent classes without object ids New pragma id (object). New test: common/no-id. --- odb/mysql/forward.hxx | 3 + odb/mysql/object-result.hxx | 56 +++++++++++-- odb/mysql/object-result.txx | 170 +++++++++++++++++++++++++++++++++++++--- odb/mysql/object-statements.hxx | 122 ++++++++++++++++++++++++++++ odb/mysql/object-statements.txx | 34 ++++++++ odb/mysql/result.hxx | 9 --- odb/mysql/statement-cache.hxx | 10 ++- odb/mysql/view-result.hxx | 12 +-- odb/mysql/view-result.txx | 22 +++--- 9 files changed, 388 insertions(+), 50 deletions(-) diff --git a/odb/mysql/forward.hxx b/odb/mysql/forward.hxx index 5d665dc..8ed934c 100644 --- a/odb/mysql/forward.hxx +++ b/odb/mysql/forward.hxx @@ -37,6 +37,9 @@ namespace odb class object_statements; template + class object_statements_no_id; + + template class view_statements; template diff --git a/odb/mysql/object-result.hxx b/odb/mysql/object-result.hxx index ff99111..d2c6ab0 100644 --- a/odb/mysql/object-result.hxx +++ b/odb/mysql/object-result.hxx @@ -22,11 +22,10 @@ namespace odb namespace mysql { template - class result_impl: - public odb::result_impl + class object_result_impl: public odb::object_result_impl { public: - typedef odb::result_impl base_type; + typedef odb::object_result_impl base_type; typedef typename base_type::object_type object_type; typedef typename base_type::object_traits object_traits; @@ -36,11 +35,11 @@ namespace odb typedef typename base_type::pointer_traits pointer_traits; virtual - ~result_impl (); + ~object_result_impl (); - result_impl (const query&, - details::shared_ptr, - object_statements&); + object_result_impl (const query&, + details::shared_ptr, + object_statements&); virtual void load (object_type&); @@ -68,6 +67,49 @@ namespace odb object_statements& statements_; std::size_t count_; }; + + template + class object_result_impl_no_id: public odb::object_result_impl_no_id + { + public: + typedef odb::object_result_impl_no_id base_type; + + typedef typename base_type::object_type object_type; + typedef typename base_type::object_traits object_traits; + + typedef typename base_type::pointer_type pointer_type; + typedef typename base_type::pointer_traits pointer_traits; + + virtual + ~object_result_impl_no_id (); + + object_result_impl_no_id (const query&, + details::shared_ptr, + object_statements_no_id&); + + virtual void + load (object_type&); + + virtual void + next (); + + virtual void + cache (); + + virtual std::size_t + size (); + + using base_type::current; + + private: + void + fetch (); + + private: + details::shared_ptr statement_; + object_statements_no_id& statements_; + std::size_t count_; + }; } } diff --git a/odb/mysql/object-result.txx b/odb/mysql/object-result.txx index d745537..e8b3560 100644 --- a/odb/mysql/object-result.txx +++ b/odb/mysql/object-result.txx @@ -12,17 +12,21 @@ namespace odb { namespace mysql { + // + // object_result_impl + // + template - result_impl:: - ~result_impl () + object_result_impl:: + ~object_result_impl () { } template - result_impl:: - result_impl (const query&, - details::shared_ptr statement, - object_statements& statements) + object_result_impl:: + object_result_impl (const query&, + details::shared_ptr statement, + object_statements& statements) : base_type (statements.connection ().database ()), statement_ (statement), statements_ (statements), @@ -31,7 +35,7 @@ namespace odb } template - void result_impl:: + void object_result_impl:: load (object_type& obj) { if (count_ > statement_->fetched ()) @@ -69,8 +73,8 @@ namespace odb } template - typename result_impl::id_type - result_impl:: + typename object_result_impl::id_type + object_result_impl:: load_id () { if (count_ > statement_->fetched ()) @@ -80,7 +84,147 @@ namespace odb } template - void result_impl:: + void object_result_impl:: + 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 object 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 + void object_result_impl:: + 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 object_traits::image_type& im (statements_.image ()); + + if (im.version != statements_.select_image_version ()) + { + binding& b (statements_.select_image_binding ()); + object_traits::bind (b.bind, im, statement_select); + statements_.select_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 object_traits::image_type& im (statements_.image ()); + + if (object_traits::grow ( + im, statements_.select_image_truncated ())) + im.version++; + + if (im.version != statements_.select_image_version ()) + { + binding& b (statements_.select_image_binding ()); + object_traits::bind (b.bind, im, statement_select); + statements_.select_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 + void object_result_impl:: + cache () + { + if (!statement_->cached ()) + { + statement_->cache (); + + if (count_ >= statement_->result_size ()) + this->end_ = true; + } + } + + template + std::size_t object_result_impl:: + size () + { + if (!statement_->cached ()) + throw result_not_cached (); + + return statement_->result_size (); + } + + // + // object_result_impl_no_id + // + + template + object_result_impl_no_id:: + ~object_result_impl_no_id () + { + } + + template + object_result_impl_no_id:: + object_result_impl_no_id (const query&, + details::shared_ptr statement, + object_statements_no_id& statements) + : base_type (statements.connection ().database ()), + statement_ (statement), + statements_ (statements), + count_ (0) + { + } + + template + void object_result_impl_no_id:: + load (object_type& obj) + { + if (count_ > statement_->fetched ()) + fetch (); + + odb::database& db (this->database ()); + + object_traits::callback (db, obj, callback_event::pre_load); + object_traits::init (obj, statements_.image (), db); + object_traits::callback (db, obj, callback_event::post_load); + } + + template + void object_result_impl_no_id:: next () { this->current (pointer_type ()); @@ -99,7 +243,7 @@ namespace odb } template - void result_impl:: + void object_result_impl_no_id:: fetch () { // If the result is cached, the image can grow between calls @@ -161,7 +305,7 @@ namespace odb } template - void result_impl:: + void object_result_impl_no_id:: cache () { if (!statement_->cached ()) @@ -174,7 +318,7 @@ namespace odb } template - std::size_t result_impl:: + std::size_t object_result_impl_no_id:: size () { if (!statement_->cached ()) diff --git a/odb/mysql/object-statements.hxx b/odb/mysql/object-statements.hxx index f259feb..9ea9933 100644 --- a/odb/mysql/object-statements.hxx +++ b/odb/mysql/object-statements.hxx @@ -29,6 +29,28 @@ namespace odb { namespace mysql { + template + class object_statements; + + template + class object_statements_no_id; + + template ::id_type> + struct object_statements_selector + { + typedef object_statements type; + }; + + template + struct object_statements_selector + { + typedef object_statements_no_id type; + }; + + // + // Implementation for objects with object id. + // + class LIBODB_MYSQL_EXPORT object_statements_base: public statements_base { // Locking. @@ -399,6 +421,106 @@ namespace odb delayed_loads& dl_; }; }; + + // + // Implementation for objects without object id. + // + + template + class object_statements_no_id: public statements_base + { + public: + typedef T object_type; + typedef odb::object_traits object_traits; + typedef typename object_traits::pointer_type pointer_type; + typedef typename object_traits::image_type image_type; + + typedef mysql::insert_statement insert_statement_type; + + public: + object_statements_no_id (connection_type&); + + virtual + ~object_statements_no_id (); + + // Object image. + // + image_type& + image () {return image_;} + + // Insert binding. + // + std::size_t + insert_image_version () const { return insert_image_version_;} + + void + insert_image_version (std::size_t v) {insert_image_version_ = v;} + + binding& + insert_image_binding () {return insert_image_binding_;} + + // Select binding (needed for query support). + // + std::size_t + select_image_version () const { return select_image_version_;} + + void + select_image_version (std::size_t v) {select_image_version_ = v;} + + binding& + select_image_binding () {return select_image_binding_;} + + my_bool* + select_image_truncated () {return select_image_truncated_;} + + // Statements. + // + insert_statement_type& + persist_statement () + { + if (persist_ == 0) + persist_.reset ( + new (details::shared) insert_statement_type ( + conn_, + object_traits::persist_statement, + insert_image_binding_)); + + return *persist_; + } + + private: + object_statements_no_id (const object_statements_no_id&); + object_statements_no_id& operator= (const object_statements_no_id&); + + private: + // select = total + // insert = total - inverse; inverse == 0 for object without id + // + + static const std::size_t select_column_count = + object_traits::column_count; + + static const std::size_t insert_column_count = + object_traits::column_count; + + private: + image_type image_; + + // Select binding. + // + std::size_t select_image_version_; + binding select_image_binding_; + MYSQL_BIND select_image_bind_[select_column_count]; + my_bool select_image_truncated_[select_column_count]; + + // Insert binding. + // + std::size_t insert_image_version_; + binding insert_image_binding_; + MYSQL_BIND insert_image_bind_[insert_column_count]; + + details::shared_ptr persist_; + }; } } diff --git a/odb/mysql/object-statements.txx b/odb/mysql/object-statements.txx index 740a7d9..372118d 100644 --- a/odb/mysql/object-statements.txx +++ b/odb/mysql/object-statements.txx @@ -16,6 +16,10 @@ namespace odb { namespace mysql { + // + // object_statements + // + template object_statements:: ~object_statements () @@ -115,5 +119,35 @@ namespace odb delayed_.clear (); } + + // + // object_statements + // + + template + object_statements_no_id:: + ~object_statements_no_id () + { + } + + template + object_statements_no_id:: + object_statements_no_id (connection_type& conn) + : statements_base (conn), + select_image_binding_ (select_image_bind_, select_column_count), + insert_image_binding_ (insert_image_bind_, insert_column_count) + { + image_.version = 0; + select_image_version_ = 0; + insert_image_version_ = 0; + + std::memset (insert_image_bind_, 0, sizeof (insert_image_bind_)); + std::memset (select_image_bind_, 0, sizeof (select_image_bind_)); + std::memset ( + select_image_truncated_, 0, sizeof (select_image_truncated_)); + + for (std::size_t i (0); i < select_column_count; ++i) + select_image_bind_[i].error = select_image_truncated_ + i; + } } } diff --git a/odb/mysql/result.hxx b/odb/mysql/result.hxx index 33df541..cf6b15b 100644 --- a/odb/mysql/result.hxx +++ b/odb/mysql/result.hxx @@ -14,15 +14,6 @@ #include #include -namespace odb -{ - namespace mysql - { - template - class result_impl; - } -} - #include #endif // ODB_MYSQL_RESULT_HXX diff --git a/odb/mysql/statement-cache.hxx b/odb/mysql/statement-cache.hxx index d5ede48..d1aad0a 100644 --- a/odb/mysql/statement-cache.hxx +++ b/odb/mysql/statement-cache.hxx @@ -38,16 +38,18 @@ namespace odb } template - object_statements& + typename object_statements_selector::type& find_object () { + typedef typename object_statements_selector::type object_statements; + map::iterator i (map_.find (&typeid (T))); if (i != map_.end ()) - return static_cast&> (*i->second); + return static_cast (*i->second); - details::shared_ptr > p ( - new (details::shared) object_statements (conn_)); + details::shared_ptr p ( + new (details::shared) object_statements (conn_)); map_.insert (map::value_type (&typeid (T), p)); return *p; diff --git a/odb/mysql/view-result.hxx b/odb/mysql/view-result.hxx index 8acb97d..a2a9f3c 100644 --- a/odb/mysql/view-result.hxx +++ b/odb/mysql/view-result.hxx @@ -22,10 +22,10 @@ namespace odb namespace mysql { template - class result_impl: public odb::result_impl + class view_result_impl: public odb::view_result_impl { public: - typedef odb::result_impl base_type; + typedef odb::view_result_impl base_type; typedef typename base_type::view_type view_type; typedef typename base_type::view_traits view_traits; @@ -34,11 +34,11 @@ namespace odb typedef typename base_type::pointer_traits pointer_traits; virtual - ~result_impl (); + ~view_result_impl (); - result_impl (const query&, - details::shared_ptr, - view_statements&); + view_result_impl (const query&, + details::shared_ptr, + view_statements&); virtual void load (view_type&); diff --git a/odb/mysql/view-result.txx b/odb/mysql/view-result.txx index 94d9ae6..8b5b598 100644 --- a/odb/mysql/view-result.txx +++ b/odb/mysql/view-result.txx @@ -13,16 +13,16 @@ namespace odb namespace mysql { template - result_impl:: - ~result_impl () + view_result_impl:: + ~view_result_impl () { } template - result_impl:: - result_impl (const query&, - details::shared_ptr statement, - view_statements& statements) + view_result_impl:: + view_result_impl (const query&, + details::shared_ptr statement, + view_statements& statements) : base_type (statements.connection ().database ()), statement_ (statement), statements_ (statements), @@ -31,7 +31,7 @@ namespace odb } template - void result_impl:: + void view_result_impl:: load (view_type& view) { if (count_ > statement_->fetched ()) @@ -45,7 +45,7 @@ namespace odb } template - void result_impl:: + void view_result_impl:: next () { this->current (pointer_type ()); @@ -64,7 +64,7 @@ namespace odb } template - void result_impl:: + void view_result_impl:: fetch () { // If the result is cached, the image can grow between calls @@ -125,7 +125,7 @@ namespace odb } template - void result_impl:: + void view_result_impl:: cache () { if (!statement_->cached ()) @@ -138,7 +138,7 @@ namespace odb } template - std::size_t result_impl:: + std::size_t view_result_impl:: size () { if (!statement_->cached ()) -- cgit v1.1