diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-27 15:16:49 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-27 15:16:49 +0200 |
commit | d83a7296e228cf0af74992020f51c566a03108c9 (patch) | |
tree | ab48b74c739049163839e4334e3855ad19906cb3 | |
parent | 2e8daa4a5880f47e547de7db4e7ff5074d365723 (diff) |
Add support for persistent classes without object ids
New pragma id (object). New test: common/no-id.
-rw-r--r-- | odb/sqlite/forward.hxx | 3 | ||||
-rw-r--r-- | odb/sqlite/object-result.hxx | 53 | ||||
-rw-r--r-- | odb/sqlite/object-result.txx | 115 | ||||
-rw-r--r-- | odb/sqlite/object-statements.hxx | 125 | ||||
-rw-r--r-- | odb/sqlite/object-statements.txx | 34 | ||||
-rw-r--r-- | odb/sqlite/result.hxx | 3 | ||||
-rw-r--r-- | odb/sqlite/statement-cache.hxx | 10 | ||||
-rw-r--r-- | odb/sqlite/view-result.hxx | 15 | ||||
-rw-r--r-- | 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 <typename T> + class object_statements_no_id; + + template <typename T> class view_statements; template <typename T> 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 <typename T> - class result_impl<T, class_object>: - public odb::result_impl<T, class_object>, - public result_impl_base + class object_result_impl: public odb::object_result_impl<T>, + public result_impl_base { public: - typedef odb::result_impl<T, class_object> base_type; + typedef odb::object_result_impl<T> 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<select_statement>, - object_statements<object_type>&); + object_result_impl (const query&, + details::shared_ptr<select_statement>, + object_statements<object_type>&); virtual void load (object_type&); @@ -67,6 +66,44 @@ namespace odb private: object_statements<object_type>& statements_; }; + + template <typename T> + class object_result_impl_no_id: public odb::object_result_impl_no_id<T>, + public result_impl_base + { + public: + typedef odb::object_result_impl_no_id<T> 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<select_statement>, + object_statements_no_id<object_type>&); + + 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<object_type>& 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 <typename T> - result_impl<T, class_object>:: - ~result_impl () + object_result_impl<T>:: + ~object_result_impl () { } template <typename T> - result_impl<T, class_object>:: - result_impl (const query& q, - details::shared_ptr<select_statement> statement, - object_statements<object_type>& statements) + object_result_impl<T>:: + object_result_impl (const query& q, + details::shared_ptr<select_statement> statement, + object_statements<object_type>& statements) : base_type (statements.connection ().database ()), result_impl_base (q, statement), statements_ (statements) @@ -30,7 +34,7 @@ namespace odb } template <typename T> - void result_impl<T, class_object>:: + void object_result_impl<T>:: load (object_type& obj) { load_image (); @@ -67,8 +71,8 @@ namespace odb } template <typename T> - typename result_impl<T, class_object>::id_type - result_impl<T, class_object>:: + typename object_result_impl<T>::id_type + object_result_impl<T>:: load_id () { load_image (); @@ -76,7 +80,7 @@ namespace odb } template <typename T> - void result_impl<T, class_object>:: + void object_result_impl<T>:: next () { this->current (pointer_type ()); @@ -86,7 +90,7 @@ namespace odb } template <typename T> - void result_impl<T, class_object>:: + void object_result_impl<T>:: load_image () { // The image can grow between calls to load() as a result of other @@ -121,13 +125,98 @@ namespace odb } template <typename T> - void result_impl<T, class_object>:: + void object_result_impl<T>:: + cache () + { + } + + template <typename T> + std::size_t object_result_impl<T>:: + size () + { + throw result_not_cached (); + } + + // + // object_result_impl_no_id + // + + template <typename T> + object_result_impl_no_id<T>:: + ~object_result_impl_no_id () + { + } + + template <typename T> + object_result_impl_no_id<T>:: + object_result_impl_no_id (const query& q, + details::shared_ptr<select_statement> statement, + object_statements_no_id<object_type>& statements) + : base_type (statements.connection ().database ()), + result_impl_base (q, statement), + statements_ (statements) + { + } + + template <typename T> + void object_result_impl_no_id<T>:: + 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 <typename T> + void object_result_impl_no_id<T>:: + next () + { + this->current (pointer_type ()); + + if (!statement_->next ()) + this->end_ = true; + } + + template <typename T> + void object_result_impl_no_id<T>:: cache () { } template <typename T> - std::size_t result_impl<T, class_object>:: + std::size_t object_result_impl_no_id<T>:: 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 <typename T> + class object_statements; + + template <typename T> + class object_statements_no_id; + + template <typename T, typename ID = typename object_traits<T>::id_type> + struct object_statements_selector + { + typedef object_statements<T> type; + }; + + template <typename T> + struct object_statements_selector<T, void> + { + typedef object_statements_no_id<T> 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 <typename T> + class object_statements_no_id: public statements_base + { + public: + typedef T object_type; + typedef odb::object_traits<object_type> 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<insert_statement_type> 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 <typename T> object_statements<T>:: ~object_statements () @@ -115,5 +119,35 @@ namespace odb delayed_.clear (); } + + // + // object_statements_no_id + // + + template <typename T> + object_statements_no_id<T>:: + ~object_statements_no_id () + { + } + + template <typename T> + object_statements_no_id<T>:: + 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<query_params> params_; details::shared_ptr<select_statement> statement_; }; - - template <typename T, class_kind kind> - 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 <typename T> - object_statements<T>& + typename object_statements_selector<T>::type& find_object () { + typedef typename object_statements_selector<T>::type object_statements; + map::iterator i (map_.find (&typeid (T))); if (i != map_.end ()) - return static_cast<object_statements<T>&> (*i->second); + return static_cast<object_statements&> (*i->second); - details::shared_ptr<object_statements<T> > p ( - new (details::shared) object_statements<T> (conn_)); + details::shared_ptr<object_statements> 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 <typename T> - class result_impl<T, class_view>: - public odb::result_impl<T, class_view>, - public result_impl_base + class view_result_impl: public odb::view_result_impl<T>, + public result_impl_base { public: - typedef odb::result_impl<T, class_view> base_type; + typedef odb::view_result_impl<T> 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<select_statement>, - view_statements<view_type>&); + view_result_impl (const query&, + details::shared_ptr<select_statement>, + view_statements<view_type>&); 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 <typename T> - result_impl<T, class_view>:: - ~result_impl () + view_result_impl<T>:: + ~view_result_impl () { } template <typename T> - result_impl<T, class_view>:: - result_impl (const query& q, + view_result_impl<T>:: + view_result_impl (const query& q, details::shared_ptr<select_statement> statement, view_statements<view_type>& statements) : base_type (statements.connection ().database ()), @@ -30,7 +30,7 @@ namespace odb } template <typename T> - void result_impl<T, class_view>:: + void view_result_impl<T>:: load (view_type& view) { // The image can grow between calls to load() as a result of other @@ -71,7 +71,7 @@ namespace odb } template <typename T> - void result_impl<T, class_view>:: + void view_result_impl<T>:: next () { this->current (pointer_type ()); @@ -81,13 +81,13 @@ namespace odb } template <typename T> - void result_impl<T, class_view>:: + void view_result_impl<T>:: cache () { } template <typename T> - std::size_t result_impl<T, class_view>:: + std::size_t view_result_impl<T>:: size () { throw result_not_cached (); |