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 | b64c3b462f99ef7337eb4bc7fa6bb258d619d8b3 (patch) | |
tree | faa051ca29584a40112dfdcb37bbf1fdff457b8c | |
parent | 6e7b115c7d136c6d99984eaae21e2404a948d550 (diff) |
Add support for persistent classes without object ids
New pragma id (object). New test: common/no-id.
-rw-r--r-- | odb/pgsql/forward.hxx | 3 | ||||
-rw-r--r-- | odb/pgsql/object-result.hxx | 51 | ||||
-rw-r--r-- | odb/pgsql/object-result.txx | 115 | ||||
-rw-r--r-- | odb/pgsql/object-statements.hxx | 131 | ||||
-rw-r--r-- | odb/pgsql/object-statements.txx | 40 | ||||
-rw-r--r-- | odb/pgsql/result.hxx | 9 | ||||
-rw-r--r-- | odb/pgsql/statement-cache.hxx | 10 | ||||
-rw-r--r-- | odb/pgsql/view-result.hxx | 13 | ||||
-rw-r--r-- | odb/pgsql/view-result.txx | 20 |
9 files changed, 342 insertions, 50 deletions
diff --git a/odb/pgsql/forward.hxx b/odb/pgsql/forward.hxx index 2ec44ec..640d379 100644 --- a/odb/pgsql/forward.hxx +++ b/odb/pgsql/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/pgsql/object-result.hxx b/odb/pgsql/object-result.hxx index e127d37..0fb68c1 100644 --- a/odb/pgsql/object-result.hxx +++ b/odb/pgsql/object-result.hxx @@ -22,11 +22,10 @@ namespace odb namespace pgsql { template <typename T> - class result_impl<T, class_object>: - public odb::result_impl<T, class_object> + class object_result_impl: public odb::object_result_impl<T> { public: - typedef odb::result_impl<T, class_object> base_type; + typedef odb::object_result_impl<T> 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<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 details::shared_ptr<select_statement> statement_; object_statements<object_type>& statements_; }; + + template <typename T> + class object_result_impl_no_id: public odb::object_result_impl_no_id<T> + { + public: + typedef odb::object_result_impl_no_id<T> 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<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: + details::shared_ptr<select_statement> statement_; + object_statements_no_id<object_type>& statements_; + }; } } diff --git a/odb/pgsql/object-result.txx b/odb/pgsql/object-result.txx index b10afe4..57a70d7 100644 --- a/odb/pgsql/object-result.txx +++ b/odb/pgsql/object-result.txx @@ -11,17 +11,21 @@ namespace odb { namespace pgsql { + // + // 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&, - details::shared_ptr<select_statement> statement, - object_statements<object_type>& statements) + object_result_impl<T>:: + object_result_impl (const query&, + details::shared_ptr<select_statement> statement, + object_statements<object_type>& statements) : base_type (statements.connection ().database ()), statement_ (statement), statements_ (statements) @@ -29,7 +33,7 @@ namespace odb } template <typename T> - void result_impl<T, class_object>:: + void object_result_impl<T>:: load (object_type& obj) { load_image (); @@ -66,8 +70,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 (); @@ -75,7 +79,7 @@ namespace odb } template <typename T> - void result_impl<T, class_object>:: + void object_result_impl<T>:: next () { this->current (pointer_type ()); @@ -85,7 +89,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 @@ -120,13 +124,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 () + { + return statement_->result_size (); + } + + // + // 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&, + details::shared_ptr<select_statement> statement, + object_statements_no_id<object_type>& statements) + : base_type (statements.connection ().database ()), + statement_ (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 () { return statement_->result_size (); diff --git a/odb/pgsql/object-statements.hxx b/odb/pgsql/object-statements.hxx index 912c864..6942361 100644 --- a/odb/pgsql/object-statements.hxx +++ b/odb/pgsql/object-statements.hxx @@ -29,6 +29,28 @@ namespace odb { namespace pgsql { + 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_PGSQL_EXPORT object_statements_base: public statements_base { public: @@ -433,6 +455,115 @@ 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 pgsql::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_;} + + 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_name, + object_traits::persist_statement, + object_traits::persist_statement_types, + insert_column_count, + insert_image_binding_, + insert_image_native_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 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]; + native_binding insert_image_native_binding_; + char* insert_image_values_[insert_column_count]; + int insert_image_lengths_[insert_column_count]; + int insert_image_formats_[insert_column_count]; + + details::shared_ptr<insert_statement_type> persist_; + }; } } diff --git a/odb/pgsql/object-statements.txx b/odb/pgsql/object-statements.txx index 409ed22..ba8b0f4 100644 --- a/odb/pgsql/object-statements.txx +++ b/odb/pgsql/object-statements.txx @@ -16,6 +16,10 @@ namespace odb { namespace pgsql { + // + // object_statements + // + template <typename T> object_statements<T>:: ~object_statements () @@ -132,5 +136,41 @@ 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 + select_image_binding_ (select_image_bind_, select_column_count), + // insert + insert_image_binding_ (insert_image_bind_, insert_column_count), + insert_image_native_binding_ (insert_image_values_, + insert_image_lengths_, + insert_image_formats_, + 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/pgsql/result.hxx b/odb/pgsql/result.hxx index 0e53e72..57c85c5 100644 --- a/odb/pgsql/result.hxx +++ b/odb/pgsql/result.hxx @@ -14,15 +14,6 @@ #include <odb/pgsql/version.hxx> #include <odb/pgsql/forward.hxx> -namespace odb -{ - namespace pgsql - { - template <typename T, class_kind kind> - class result_impl; - } -} - #include <odb/post.hxx> #endif // ODB_PGSQL_RESULT_HXX diff --git a/odb/pgsql/statement-cache.hxx b/odb/pgsql/statement-cache.hxx index 986aa9a..46fd257 100644 --- a/odb/pgsql/statement-cache.hxx +++ b/odb/pgsql/statement-cache.hxx @@ -38,16 +38,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/pgsql/view-result.hxx b/odb/pgsql/view-result.hxx index 115c128..f818113 100644 --- a/odb/pgsql/view-result.hxx +++ b/odb/pgsql/view-result.hxx @@ -22,11 +22,10 @@ namespace odb namespace pgsql { template <typename T> - class result_impl<T, class_view>: - public odb::result_impl<T, class_view> + class view_result_impl: public odb::view_result_impl<T> { 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; @@ -35,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<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/pgsql/view-result.txx b/odb/pgsql/view-result.txx index f7aff89..92137d4 100644 --- a/odb/pgsql/view-result.txx +++ b/odb/pgsql/view-result.txx @@ -12,16 +12,16 @@ namespace odb namespace pgsql { 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&, - details::shared_ptr<select_statement> statement, - view_statements<view_type>& statements) + view_result_impl<T>:: + 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) @@ -29,7 +29,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 @@ -70,7 +70,7 @@ namespace odb } template <typename T> - void result_impl<T, class_view>:: + void view_result_impl<T>:: next () { this->current (pointer_type ()); @@ -80,13 +80,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 () { return statement_->result_size (); |