From d83a7296e228cf0af74992020f51c566a03108c9 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/sqlite/forward.hxx | 3 + odb/sqlite/object-result.hxx | 53 ++++++++++++++--- odb/sqlite/object-result.txx | 115 +++++++++++++++++++++++++++++++---- odb/sqlite/object-statements.hxx | 125 +++++++++++++++++++++++++++++++++++++++ odb/sqlite/object-statements.txx | 34 +++++++++++ odb/sqlite/result.hxx | 3 - odb/sqlite/statement-cache.hxx | 10 ++-- odb/sqlite/view-result.hxx | 15 +++-- odb/sqlite/view-result.txx | 16 ++--- 9 files changed, 330 insertions(+), 44 deletions(-) diff --git a/odb/sqlite/forward.hxx b/odb/sqlite/forward.hxx index 23d995e..cc1fbfc 100644 --- a/odb/sqlite/forward.hxx +++ b/odb/sqlite/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/sqlite/object-result.hxx b/odb/sqlite/object-result.hxx index 401c83a..fb7865c 100644 --- a/odb/sqlite/object-result.hxx +++ b/odb/sqlite/object-result.hxx @@ -22,12 +22,11 @@ namespace odb namespace sqlite { template - class result_impl: - public odb::result_impl, - public result_impl_base + class object_result_impl: public odb::object_result_impl, + public result_impl_base { public: - typedef odb::result_impl base_type; + typedef odb::object_result_impl base_type; typedef typename base_type::object_traits object_traits; typedef typename base_type::object_type object_type; @@ -37,11 +36,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&); @@ -67,6 +66,44 @@ namespace odb private: object_statements& statements_; }; + + template + class object_result_impl_no_id: public odb::object_result_impl_no_id, + public result_impl_base + { + public: + typedef odb::object_result_impl_no_id base_type; + + typedef typename base_type::object_traits object_traits; + typedef typename base_type::object_type object_type; + + 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: + object_statements_no_id& statements_; + }; } } diff --git a/odb/sqlite/object-result.txx b/odb/sqlite/object-result.txx index 6e3a0c9..dc258cc 100644 --- a/odb/sqlite/object-result.txx +++ b/odb/sqlite/object-result.txx @@ -12,17 +12,21 @@ namespace odb { namespace sqlite { + // + // object_result_impl + // + template - result_impl:: - ~result_impl () + object_result_impl:: + ~object_result_impl () { } template - result_impl:: - result_impl (const query& q, - details::shared_ptr statement, - object_statements& statements) + object_result_impl:: + object_result_impl (const query& q, + details::shared_ptr statement, + object_statements& statements) : base_type (statements.connection ().database ()), result_impl_base (q, statement), statements_ (statements) @@ -30,7 +34,7 @@ namespace odb } template - void result_impl:: + void object_result_impl:: load (object_type& obj) { load_image (); @@ -67,8 +71,8 @@ namespace odb } template - typename result_impl::id_type - result_impl:: + typename object_result_impl::id_type + object_result_impl:: load_id () { load_image (); @@ -76,7 +80,7 @@ namespace odb } template - void result_impl:: + void object_result_impl:: next () { this->current (pointer_type ()); @@ -86,7 +90,7 @@ namespace odb } template - void result_impl:: + void object_result_impl:: load_image () { // The image can grow between calls to load() as a result of other @@ -121,13 +125,98 @@ namespace odb } template - void result_impl:: + void object_result_impl:: + cache () + { + } + + template + std::size_t object_result_impl:: + size () + { + throw result_not_cached (); + } + + // + // 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& q, + details::shared_ptr statement, + object_statements_no_id& statements) + : base_type (statements.connection ().database ()), + result_impl_base (q, statement), + statements_ (statements) + { + } + + template + void object_result_impl_no_id:: + load (object_type& obj) + { + // The image can grow between calls to load() as a result of other + // statements execution. + // + 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++; + } + + select_statement::result r (statement_->load ()); + + if (r == select_statement::truncated) + { + 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_->reload (); + } + } + + odb::database& db (this->database ()); + + object_traits::callback (db, obj, callback_event::pre_load); + object_traits::init (obj, im, db); + object_traits::callback (db, obj, callback_event::post_load); + } + + template + void object_result_impl_no_id:: + next () + { + this->current (pointer_type ()); + + if (!statement_->next ()) + this->end_ = true; + } + + template + void object_result_impl_no_id:: cache () { } template - std::size_t result_impl:: + std::size_t object_result_impl_no_id:: size () { throw result_not_cached (); diff --git a/odb/sqlite/object-statements.hxx b/odb/sqlite/object-statements.hxx index a651507..15c848d 100644 --- a/odb/sqlite/object-statements.hxx +++ b/odb/sqlite/object-statements.hxx @@ -29,6 +29,28 @@ namespace odb { namespace sqlite { + 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_SQLITE_EXPORT object_statements_base: public statements_base { public: @@ -416,6 +438,109 @@ 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 sqlite::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. + // + 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_;} + + 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_)); + + persist_->cached (true); + } + + 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 insert_column_count = + object_traits::column_count; + + static const std::size_t select_column_count = + object_traits::column_count; + + private: + image_type image_; + + // Select binding. + // + std::size_t select_image_version_; + binding select_image_binding_; + bind select_image_bind_[select_column_count]; + bool select_image_truncated_[select_column_count]; + + // Insert binding. + // + std::size_t insert_image_version_; + binding insert_image_binding_; + bind insert_image_bind_[insert_column_count]; + + details::shared_ptr persist_; + }; } } diff --git a/odb/sqlite/object-statements.txx b/odb/sqlite/object-statements.txx index fd596e7..d2bd06f 100644 --- a/odb/sqlite/object-statements.txx +++ b/odb/sqlite/object-statements.txx @@ -16,6 +16,10 @@ namespace odb { namespace sqlite { + // + // object_statements + // + template object_statements:: ~object_statements () @@ -115,5 +119,35 @@ namespace odb delayed_.clear (); } + + // + // object_statements_no_id + // + + 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].truncated = select_image_truncated_ + i; + } } } diff --git a/odb/sqlite/result.hxx b/odb/sqlite/result.hxx index 1417a6c..b8b759e 100644 --- a/odb/sqlite/result.hxx +++ b/odb/sqlite/result.hxx @@ -35,9 +35,6 @@ namespace odb details::shared_ptr params_; details::shared_ptr statement_; }; - - template - class result_impl; } } diff --git a/odb/sqlite/statement-cache.hxx b/odb/sqlite/statement-cache.hxx index 16e0c15..1bdefb6 100644 --- a/odb/sqlite/statement-cache.hxx +++ b/odb/sqlite/statement-cache.hxx @@ -72,16 +72,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/sqlite/view-result.hxx b/odb/sqlite/view-result.hxx index fe4fca5..32507fd 100644 --- a/odb/sqlite/view-result.hxx +++ b/odb/sqlite/view-result.hxx @@ -22,12 +22,11 @@ namespace odb namespace sqlite { template - class result_impl: - public odb::result_impl, - public result_impl_base + class view_result_impl: public odb::view_result_impl, + public result_impl_base { 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; @@ -36,11 +35,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/sqlite/view-result.txx b/odb/sqlite/view-result.txx index 2facc24..f4449eb 100644 --- a/odb/sqlite/view-result.txx +++ b/odb/sqlite/view-result.txx @@ -13,14 +13,14 @@ namespace odb namespace sqlite { template - result_impl:: - ~result_impl () + view_result_impl:: + ~view_result_impl () { } template - result_impl:: - result_impl (const query& q, + view_result_impl:: + view_result_impl (const query& q, details::shared_ptr statement, view_statements& statements) : base_type (statements.connection ().database ()), @@ -30,7 +30,7 @@ namespace odb } template - void result_impl:: + void view_result_impl:: load (view_type& view) { // The image can grow between calls to load() as a result of other @@ -71,7 +71,7 @@ namespace odb } template - void result_impl:: + void view_result_impl:: next () { this->current (pointer_type ()); @@ -81,13 +81,13 @@ namespace odb } template - void result_impl:: + void view_result_impl:: cache () { } template - std::size_t result_impl:: + std::size_t view_result_impl:: size () { throw result_not_cached (); -- cgit v1.1