From 7ce07d1c371e32e474897e8b03da7e330aaefb57 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 6 Sep 2011 16:56:07 +0200 Subject: Support for views; native part --- odb/pgsql/forward.hxx | 3 + odb/pgsql/makefile | 1 + odb/pgsql/object-result.hxx | 78 +++++++++++++++++++++++ odb/pgsql/object-result.txx | 135 ++++++++++++++++++++++++++++++++++++++++ odb/pgsql/object-statements.hxx | 23 +++---- odb/pgsql/object-statements.txx | 6 ++ odb/pgsql/query.hxx | 16 ++--- odb/pgsql/result.hxx | 64 ++++--------------- odb/pgsql/result.txx | 133 --------------------------------------- odb/pgsql/statement-cache.hxx | 28 +++++++-- odb/pgsql/statements-base.cxx | 17 +++++ odb/pgsql/statements-base.hxx | 51 +++++++++++++++ odb/pgsql/view-result.hxx | 70 +++++++++++++++++++++ odb/pgsql/view-result.txx | 95 ++++++++++++++++++++++++++++ odb/pgsql/view-statements.hxx | 111 +++++++++++++++++++++++++++++++++ odb/pgsql/view-statements.txx | 35 +++++++++++ 16 files changed, 651 insertions(+), 215 deletions(-) create mode 100644 odb/pgsql/object-result.hxx create mode 100644 odb/pgsql/object-result.txx delete mode 100644 odb/pgsql/result.txx create mode 100644 odb/pgsql/statements-base.cxx create mode 100644 odb/pgsql/statements-base.hxx create mode 100644 odb/pgsql/view-result.hxx create mode 100644 odb/pgsql/view-result.txx create mode 100644 odb/pgsql/view-statements.hxx create mode 100644 odb/pgsql/view-statements.txx diff --git a/odb/pgsql/forward.hxx b/odb/pgsql/forward.hxx index 046942d..0e1fb10 100644 --- a/odb/pgsql/forward.hxx +++ b/odb/pgsql/forward.hxx @@ -30,6 +30,9 @@ namespace odb class object_statements; template + class view_statements; + + template class container_statements; } diff --git a/odb/pgsql/makefile b/odb/pgsql/makefile index 8203235..23b7903 100644 --- a/odb/pgsql/makefile +++ b/odb/pgsql/makefile @@ -15,6 +15,7 @@ error.cxx \ exceptions.cxx \ object-statements.cxx \ statement.cxx \ +statements-base.cxx \ traits.cxx \ transaction.cxx \ transaction-impl.cxx \ diff --git a/odb/pgsql/object-result.hxx b/odb/pgsql/object-result.hxx new file mode 100644 index 0000000..1f5a9ca --- /dev/null +++ b/odb/pgsql/object-result.hxx @@ -0,0 +1,78 @@ +// file : odb/pgsql/object-result.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_PGSQL_OBJECT_RESULT_HXX +#define ODB_PGSQL_OBJECT_RESULT_HXX + +#include + +#include // std::size_t + +#include + +#include +#include // query, object_statements +#include +#include + +namespace odb +{ + namespace pgsql + { + template + class result_impl: + public odb::result_impl + { + public: + typedef odb::result_impl 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, + object_statements&); + + 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 + load_image (); + + private: + details::shared_ptr statement_; + object_statements& statements_; + }; + } +} + +#include + +#include + +#endif // ODB_PGSQL_OBJECT_RESULT_HXX diff --git a/odb/pgsql/object-result.txx b/odb/pgsql/object-result.txx new file mode 100644 index 0000000..f4ee961 --- /dev/null +++ b/odb/pgsql/object-result.txx @@ -0,0 +1,135 @@ +// file : odb/pgsql/object-result.txx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +#include + +namespace odb +{ + namespace pgsql + { + template + result_impl:: + ~result_impl () + { + } + + template + result_impl:: + result_impl (const query&, + details::shared_ptr statement, + object_statements& statements) + : base_type (statements.connection ().database ()), + statement_ (statement), + statements_ (statements) + { + } + + template + void result_impl:: + load (object_type& obj) + { + load_image (); + + // This is a top-level call so the statements cannot be locked. + // + assert (!statements_.locked ()); + typename object_statements::auto_lock l (statements_); + + odb::database& db (this->database ()); + object_traits::callback (db, obj, callback_event::pre_load); + + typename object_traits::image_type& i (statements_.image ()); + object_traits::init (obj, i, db); + + // Initialize the id image and binding and load the rest of the object + // (containers, etc). + // + typename object_traits::id_image_type& idi (statements_.id_image ()); + object_traits::init (idi, object_traits::id (i)); + + binding& idb (statements_.id_image_binding ()); + if (idi.version != statements_.id_image_version () || idb.version == 0) + { + object_traits::bind (idb.bind, idi); + statements_.id_image_version (idi.version); + idb.version++; + } + + object_traits::load_ (statements_, obj); + statements_.load_delayed (); + l.unlock (); + object_traits::callback (db, obj, callback_event::post_load); + } + + template + typename result_impl::id_type + result_impl:: + load_id () + { + load_image (); + return object_traits::id (statements_.image ()); + } + + template + void result_impl:: + next () + { + this->current (pointer_type ()); + + if (!statement_->next ()) + this->end_ = true; + } + + template + void result_impl:: + load_image () + { + // 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_.out_image_version ()) + { + binding& b (statements_.out_image_binding ()); + object_traits::bind (b.bind, im, true); + statements_.out_image_version (im.version); + b.version++; + } + + select_statement::result r (statement_->load ()); + + if (r == select_statement::truncated) + { + if (object_traits::grow (im, statements_.out_image_truncated ())) + im.version++; + + if (im.version != statements_.out_image_version ()) + { + binding& b (statements_.out_image_binding ()); + object_traits::bind (b.bind, im, true); + statements_.out_image_version (im.version); + b.version++; + statement_->reload (); + } + } + } + + template + void result_impl:: + cache () + { + } + + template + std::size_t result_impl:: + size () + { + return statement_->result_size (); + } + } +} diff --git a/odb/pgsql/object-statements.hxx b/odb/pgsql/object-statements.hxx index ca5aecd..dc3c0db 100644 --- a/odb/pgsql/object-statements.hxx +++ b/odb/pgsql/object-statements.hxx @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -28,20 +29,9 @@ namespace odb { namespace pgsql { - class connection; - - class LIBODB_PGSQL_EXPORT object_statements_base: - public details::shared_base + class LIBODB_PGSQL_EXPORT object_statements_base: public statements_base { public: - typedef pgsql::connection connection_type; - - connection_type& - connection () - { - return conn_; - } - // Locking. // void @@ -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_; }; @@ -152,10 +141,12 @@ namespace odb bool locked_; }; - // - // + public: object_statements (connection_type&); + virtual + ~object_statements (); + // Delayed loading. // void diff --git a/odb/pgsql/object-statements.txx b/odb/pgsql/object-statements.txx index c568ca7..c204ae6 100644 --- a/odb/pgsql/object-statements.txx +++ b/odb/pgsql/object-statements.txx @@ -18,6 +18,12 @@ namespace odb { template object_statements:: + ~object_statements () + { + } + + template + object_statements:: object_statements (connection_type& conn) : object_statements_base (conn), container_statement_cache_ (conn), diff --git a/odb/pgsql/query.hxx b/odb/pgsql/query.hxx index 96fa126..7633cc4 100644 --- a/odb/pgsql/query.hxx +++ b/odb/pgsql/query.hxx @@ -1674,7 +1674,7 @@ namespace odb namespace odb { template - class query: public object_traits::query_type + class query: public query_selector::type { public: // We don't define any typedefs here since they may clash with @@ -1687,40 +1687,40 @@ namespace odb explicit query (const std::string& q) - : object_traits::query_type (q) + : query_selector::type (q) { } template explicit query (pgsql::val_bind v) - : object_traits::query_type (pgsql::query (v)) + : query_selector::type (pgsql::query (v)) { } template explicit query (pgsql::ref_bind r) - : object_traits::query_type (pgsql::query (r)) + : query_selector::type (pgsql::query (r)) { } query (const pgsql::query& q) - : object_traits::query_type (q) + : query_selector::type (q) { } template query (const pgsql::query_column& qc) - : object_traits::query_type (qc) + : query_selector::type (qc) { } std::string clause () const { - return object_traits::query_type::clause ( - object_traits::table_name); + return query_selector::type::clause ( + query_selector::table_name ()); } }; } diff --git a/odb/pgsql/result.hxx b/odb/pgsql/result.hxx index 6a8b6a4..0e53e72 100644 --- a/odb/pgsql/result.hxx +++ b/odb/pgsql/result.hxx @@ -8,70 +8,28 @@ #include -#include // std::size_t - +#include #include -#include #include -#include // query, query_params -#include - -#include +#include namespace odb { namespace pgsql { - template - class result_impl: public odb::result_impl - { - public: - typedef typename odb::result_impl::pointer_type pointer_type; - typedef typename odb::result_impl::pointer_traits pointer_traits; - - typedef typename odb::result_impl::object_type object_type; - typedef typename odb::result_impl::id_type id_type; - typedef typename odb::result_impl::object_traits object_traits; - - - virtual - ~result_impl (); - - result_impl (const query&, - details::shared_ptr, - object_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::current; - - private: - void - load_image (); - - private: - details::shared_ptr statement_; - object_statements& statements_; - }; + template + class result_impl; } } -#include - #include #endif // ODB_PGSQL_RESULT_HXX + +// Include result specializations so that the user code only needs +// to include this header. +// + +#include +#include diff --git a/odb/pgsql/result.txx b/odb/pgsql/result.txx deleted file mode 100644 index a0dc2f8..0000000 --- a/odb/pgsql/result.txx +++ /dev/null @@ -1,133 +0,0 @@ -// file : odb/pgsql/result.txx -// author : Constantin Michael -// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#include -#include - -namespace odb -{ - namespace pgsql - { - template - result_impl:: - ~result_impl () - { - } - - template - result_impl:: - result_impl (const query&, - details::shared_ptr st, - object_statements& sts) - : odb::result_impl (sts.connection ().database ()), - statement_ (st), - statements_ (sts) - { - } - - template - void result_impl:: - load (object_type& obj) - { - load_image (); - - // This is a top-level call so the statements cannot be locked. - // - assert (!statements_.locked ()); - typename object_statements::auto_lock l (statements_); - - odb::database& db (this->database ()); - object_traits::callback (db, obj, callback_event::pre_load); - - typename object_traits::image_type& i (statements_.image ()); - object_traits::init (obj, i, db); - - // Initialize the id image and binding and load the rest of the object - // (containers, etc). - // - typename object_traits::id_image_type& idi (statements_.id_image ()); - object_traits::init (idi, object_traits::id (i)); - - binding& idb (statements_.id_image_binding ()); - if (idi.version != statements_.id_image_version () || idb.version == 0) - { - object_traits::bind (idb.bind, idi); - statements_.id_image_version (idi.version); - idb.version++; - } - - object_traits::load_ (statements_, obj); - statements_.load_delayed (); - l.unlock (); - object_traits::callback (db, obj, callback_event::post_load); - } - - template - typename result_impl::id_type result_impl:: - load_id () - { - load_image (); - return object_traits::id (statements_.image ()); - } - - template - void result_impl:: - next () - { - this->current (pointer_type ()); - - if (!statement_->next ()) - this->end_ = true; - } - - template - void result_impl:: - load_image () - { - // 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_.out_image_version ()) - { - binding& b (statements_.out_image_binding ()); - object_traits::bind (b.bind, im, true); - statements_.out_image_version (im.version); - b.version++; - } - - select_statement::result r (statement_->load ()); - - if (r == select_statement::truncated) - { - if (object_traits::grow (im, statements_.out_image_truncated ())) - im.version++; - - if (im.version != statements_.out_image_version ()) - { - binding& b (statements_.out_image_binding ()); - object_traits::bind (b.bind, im, true); - statements_.out_image_version (im.version); - b.version++; - statement_->reload (); - } - } - } - - template - void result_impl:: - cache () - { - } - - template - std::size_t result_impl:: - size () - { - return statement_->result_size (); - } - } -} diff --git a/odb/pgsql/statement-cache.hxx b/odb/pgsql/statement-cache.hxx index 8164cd8..986aa9a 100644 --- a/odb/pgsql/statement-cache.hxx +++ b/odb/pgsql/statement-cache.hxx @@ -13,12 +13,14 @@ #include -#include -#include - #include #include +#include +#include +#include +#include + #include namespace odb @@ -37,7 +39,7 @@ namespace odb template object_statements& - find () + find_object () { map::iterator i (map_.find (&typeid (T))); @@ -51,9 +53,25 @@ namespace odb return *p; } + template + view_statements& + find_view () + { + map::iterator i (map_.find (&typeid (T))); + + if (i != map_.end ()) + return static_cast&> (*i->second); + + details::shared_ptr > p ( + new (details::shared) view_statements (conn_)); + + map_.insert (map::value_type (&typeid (T), p)); + return *p; + } + private: typedef std::map, + details::shared_ptr, details::type_info_comparator> map; connection& conn_; diff --git a/odb/pgsql/statements-base.cxx b/odb/pgsql/statements-base.cxx new file mode 100644 index 0000000..749f8ce --- /dev/null +++ b/odb/pgsql/statements-base.cxx @@ -0,0 +1,17 @@ +// file : odb/pgsql/statements-base.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace pgsql + { + statements_base:: + ~statements_base () + { + } + } +} diff --git a/odb/pgsql/statements-base.hxx b/odb/pgsql/statements-base.hxx new file mode 100644 index 0000000..54474dd --- /dev/null +++ b/odb/pgsql/statements-base.hxx @@ -0,0 +1,51 @@ +// file : odb/pgsql/statements-base.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_PGSQL_STATEMENTS_BASE_HXX +#define ODB_PGSQL_STATEMENTS_BASE_HXX + +#include + +#include + +#include +#include // connection + +#include + +namespace odb +{ + namespace pgsql + { + class LIBODB_PGSQL_EXPORT statements_base: public details::shared_base + { + public: + typedef pgsql::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 + +#endif // ODB_PGSQL_STATEMENTS_BASE_HXX diff --git a/odb/pgsql/view-result.hxx b/odb/pgsql/view-result.hxx new file mode 100644 index 0000000..629778b --- /dev/null +++ b/odb/pgsql/view-result.hxx @@ -0,0 +1,70 @@ +// file : odb/pgsql/view-result.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_PGSQL_VIEW_RESULT_HXX +#define ODB_PGSQL_VIEW_RESULT_HXX + +#include + +#include // std::size_t + +#include + +#include +#include // query, view_statements +#include +#include + +namespace odb +{ + namespace pgsql + { + template + class result_impl: + public odb::result_impl + { + public: + typedef odb::result_impl 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, + view_statements&); + + virtual void + load (view_type&); + + virtual void + next (); + + virtual void + cache (); + + virtual std::size_t + size (); + + using base_type::current; + + private: + details::shared_ptr statement_; + view_statements& statements_; + }; + } +} + +#include + +#include + +#endif // ODB_PGSQL_VIEW_RESULT_HXX diff --git a/odb/pgsql/view-result.txx b/odb/pgsql/view-result.txx new file mode 100644 index 0000000..63e5444 --- /dev/null +++ b/odb/pgsql/view-result.txx @@ -0,0 +1,95 @@ +// file : odb/pgsql/view-result.txx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +#include + +namespace odb +{ + namespace pgsql + { + template + result_impl:: + ~result_impl () + { + } + + template + result_impl:: + result_impl (const query&, + details::shared_ptr statement, + view_statements& statements) + : base_type (statements.connection ().database ()), + statement_ (statement), + statements_ (statements) + { + } + + template + void result_impl:: + load (view_type& view) + { + // The image can grow between calls to load() as a result of other + // statements execution. + // + 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++; + } + + select_statement::result r (statement_->load ()); + + if (r == select_statement::truncated) + { + 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_->reload (); + } + } + + odb::database& db (this->database ()); + + view_traits::callback (db, view, callback_event::pre_load); + view_traits::init (view, im); + view_traits::callback (db, view, callback_event::post_load); + } + + template + void result_impl:: + next () + { + this->current (pointer_type ()); + + if (!statement_->next ()) + this->end_ = true; + } + + template + void result_impl:: + cache () + { + } + + template + std::size_t result_impl:: + size () + { + return statement_->result_size (); + } + } +} diff --git a/odb/pgsql/view-statements.hxx b/odb/pgsql/view-statements.hxx new file mode 100644 index 0000000..e1e5614 --- /dev/null +++ b/odb/pgsql/view-statements.hxx @@ -0,0 +1,111 @@ +// file : odb/pgsql/view-statements.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_PGSQL_VIEW_STATEMENTS_HXX +#define ODB_PGSQL_VIEW_STATEMENTS_HXX + +#include + +#include // std::size_t + +#include +#include + +#include + +#include +#include +#include +#include + +namespace odb +{ + namespace pgsql + { + template + class view_statements: public statements_base + { + public: + typedef T view_type; + typedef odb::view_traits view_traits; + typedef typename view_traits::pointer_type pointer_type; + typedef typename view_traits::image_type image_type; + + typedef pgsql::select_statement query_statement_type; + + public: + view_statements (connection_type&); + + virtual + ~view_statements (); + + // 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_; + } + + 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_name, + 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_; + bind image_bind_[view_traits::column_count]; + bool image_truncated_[view_traits::column_count]; + + details::shared_ptr query_; + }; + } +} + +#include + +#include + +#endif // ODB_PGSQL_VIEW_STATEMENTS_HXX diff --git a/odb/pgsql/view-statements.txx b/odb/pgsql/view-statements.txx new file mode 100644 index 0000000..3ab3260 --- /dev/null +++ b/odb/pgsql/view-statements.txx @@ -0,0 +1,35 @@ +// file : odb/pgsql/view-statements.txx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include // std::size_t +#include // std::memset + +namespace odb +{ + namespace pgsql + { + template + view_statements:: + ~view_statements () + { + } + + template + view_statements:: + 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].truncated = image_truncated_ + i; + } + } +} -- cgit v1.1