From 1b1cb92a27592da6ab9300b2c5c8384f7588e2af Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 6 Sep 2011 16:54:06 +0200 Subject: Support for views; native part --- odb/sqlite/forward.hxx | 3 + odb/sqlite/makefile | 1 + odb/sqlite/object-result.hxx | 77 ++++++++++++++++++++++ odb/sqlite/object-result.txx | 136 +++++++++++++++++++++++++++++++++++++++ odb/sqlite/object-statements.hxx | 26 +++----- odb/sqlite/object-statements.txx | 6 ++ odb/sqlite/query.hxx | 16 ++--- odb/sqlite/result.hxx | 59 ++++------------- odb/sqlite/result.txx | 133 -------------------------------------- odb/sqlite/statement-cache.hxx | 24 ++++++- odb/sqlite/statements-base.cxx | 17 +++++ odb/sqlite/statements-base.hxx | 51 +++++++++++++++ odb/sqlite/view-result.hxx | 69 ++++++++++++++++++++ odb/sqlite/view-result.txx | 96 +++++++++++++++++++++++++++ odb/sqlite/view-statements.hxx | 113 ++++++++++++++++++++++++++++++++ odb/sqlite/view-statements.txx | 35 ++++++++++ 16 files changed, 656 insertions(+), 206 deletions(-) create mode 100644 odb/sqlite/object-result.hxx create mode 100644 odb/sqlite/object-result.txx delete mode 100644 odb/sqlite/result.txx create mode 100644 odb/sqlite/statements-base.cxx create mode 100644 odb/sqlite/statements-base.hxx create mode 100644 odb/sqlite/view-result.hxx create mode 100644 odb/sqlite/view-result.txx create mode 100644 odb/sqlite/view-statements.hxx create mode 100644 odb/sqlite/view-statements.txx diff --git a/odb/sqlite/forward.hxx b/odb/sqlite/forward.hxx index a4e3376..5203434 100644 --- a/odb/sqlite/forward.hxx +++ b/odb/sqlite/forward.hxx @@ -30,6 +30,9 @@ namespace odb class object_statements; template + class view_statements; + + template class container_statements; class query_params; diff --git a/odb/sqlite/makefile b/odb/sqlite/makefile index feb62eb..e9115a7 100644 --- a/odb/sqlite/makefile +++ b/odb/sqlite/makefile @@ -16,6 +16,7 @@ query.cxx \ result.cxx \ statement-cache.cxx \ statement.cxx \ +statements-base.cxx \ traits.cxx \ transaction.cxx \ transaction-impl.cxx diff --git a/odb/sqlite/object-result.hxx b/odb/sqlite/object-result.hxx new file mode 100644 index 0000000..b3dbf2b --- /dev/null +++ b/odb/sqlite/object-result.hxx @@ -0,0 +1,77 @@ +// file : odb/sqlite/object-result.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_OBJECT_RESULT_HXX +#define ODB_SQLITE_OBJECT_RESULT_HXX + +#include + +#include // std::size_t + +#include + +#include +#include // query, object_statements +#include +#include + +namespace odb +{ + namespace sqlite + { + template + class result_impl: + public odb::result_impl, + public result_impl_base + { + 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: + object_statements& statements_; + }; + } +} + +#include + +#include + +#endif // ODB_SQLITE_OBJECT_RESULT_HXX diff --git a/odb/sqlite/object-result.txx b/odb/sqlite/object-result.txx new file mode 100644 index 0000000..7675262 --- /dev/null +++ b/odb/sqlite/object-result.txx @@ -0,0 +1,136 @@ +// file : odb/sqlite/object-result.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include +#include + +#include + +namespace odb +{ + namespace sqlite + { + template + result_impl:: + ~result_impl () + { + } + + template + result_impl:: + result_impl (const query& q, + details::shared_ptr statement, + object_statements& statements) + : base_type (statements.connection ().database ()), + result_impl_base (q, 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 () + { + throw result_not_cached (); + } + } +} diff --git a/odb/sqlite/object-statements.hxx b/odb/sqlite/object-statements.hxx index c9c101d..4816d66 100644 --- a/odb/sqlite/object-statements.hxx +++ b/odb/sqlite/object-statements.hxx @@ -15,31 +15,23 @@ #include #include #include + #include #include #include #include +#include + #include namespace odb { namespace sqlite { - class connection; - - class LIBODB_SQLITE_EXPORT object_statements_base: - public details::shared_base + class LIBODB_SQLITE_EXPORT object_statements_base: public statements_base { public: - typedef sqlite::connection connection_type; - - connection_type& - connection () - { - return conn_; - } - // Locking. // void @@ -68,7 +60,7 @@ namespace odb protected: object_statements_base (connection_type& conn) - : conn_ (conn), locked_ (false) + : statements_base (conn), locked_ (false) { } @@ -89,7 +81,6 @@ namespace odb }; protected: - connection_type& conn_; bool locked_; }; @@ -150,10 +141,13 @@ namespace odb bool locked_; }; - // - // + + public: object_statements (connection_type&); + virtual + ~object_statements (); + // Delayed loading. // void diff --git a/odb/sqlite/object-statements.txx b/odb/sqlite/object-statements.txx index 9cd8344..04d57c8 100644 --- a/odb/sqlite/object-statements.txx +++ b/odb/sqlite/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/sqlite/query.hxx b/odb/sqlite/query.hxx index 3179bda..f413756 100644 --- a/odb/sqlite/query.hxx +++ b/odb/sqlite/query.hxx @@ -1199,7 +1199,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 @@ -1212,40 +1212,40 @@ namespace odb explicit query (const std::string& q) - : object_traits::query_type (q) + : query_selector::type (q) { } template explicit query (sqlite::val_bind v) - : object_traits::query_type (sqlite::query (v)) + : query_selector::type (sqlite::query (v)) { } template explicit query (sqlite::ref_bind r) - : object_traits::query_type (sqlite::query (r)) + : query_selector::type (sqlite::query (r)) { } query (const sqlite::query& q) - : object_traits::query_type (q) + : query_selector::type (q) { } template query (const sqlite::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/sqlite/result.hxx b/odb/sqlite/result.hxx index 4f1cc2c..1417a6c 100644 --- a/odb/sqlite/result.hxx +++ b/odb/sqlite/result.hxx @@ -8,14 +8,15 @@ #include -#include // std::size_t - +#include #include + #include #include #include // query, query_params #include + #include namespace odb @@ -35,54 +36,18 @@ namespace odb details::shared_ptr statement_; }; - template - class result_impl: public odb::result_impl, public result_impl_base - { - 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: - object_statements& statements_; - }; + template + class result_impl; } } -#include - #include #endif // ODB_SQLITE_RESULT_HXX + +// Include result specializations so that the user code only needs +// to include this header. +// + +#include +#include diff --git a/odb/sqlite/result.txx b/odb/sqlite/result.txx deleted file mode 100644 index 9bcfe5f..0000000 --- a/odb/sqlite/result.txx +++ /dev/null @@ -1,133 +0,0 @@ -// file : odb/sqlite/result.txx -// author : Boris Kolpackov -// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#include -#include - -namespace odb -{ - namespace sqlite - { - template - result_impl:: - ~result_impl () - { - } - - template - result_impl:: - result_impl (const query& q, - details::shared_ptr st, - object_statements& sts) - : odb::result_impl (sts.connection ().database ()), - result_impl_base (q, 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 () - { - throw result_not_cached (); - } - } -} diff --git a/odb/sqlite/statement-cache.hxx b/odb/sqlite/statement-cache.hxx index ca4ab2e..16e0c15 100644 --- a/odb/sqlite/statement-cache.hxx +++ b/odb/sqlite/statement-cache.hxx @@ -12,12 +12,16 @@ #include #include + #include #include #include #include +#include #include +#include + #include namespace odb @@ -69,7 +73,7 @@ namespace odb template object_statements& - find () + find_object () { map::iterator i (map_.find (&typeid (T))); @@ -83,6 +87,22 @@ 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: void begin_immediate_statement_ () const; @@ -92,7 +112,7 @@ namespace odb private: typedef std::map, + details::shared_ptr, details::type_info_comparator> map; connection& conn_; diff --git a/odb/sqlite/statements-base.cxx b/odb/sqlite/statements-base.cxx new file mode 100644 index 0000000..9379047 --- /dev/null +++ b/odb/sqlite/statements-base.cxx @@ -0,0 +1,17 @@ +// file : odb/sqlite/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 sqlite + { + statements_base:: + ~statements_base () + { + } + } +} diff --git a/odb/sqlite/statements-base.hxx b/odb/sqlite/statements-base.hxx new file mode 100644 index 0000000..a2d5a81 --- /dev/null +++ b/odb/sqlite/statements-base.hxx @@ -0,0 +1,51 @@ +// file : odb/sqlite/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_SQLITE_STATEMENTS_BASE_HXX +#define ODB_SQLITE_STATEMENTS_BASE_HXX + +#include + +#include + +#include +#include // connection + +#include + +namespace odb +{ + namespace sqlite + { + class LIBODB_SQLITE_EXPORT statements_base: public details::shared_base + { + public: + typedef sqlite::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_SQLITE_STATEMENTS_BASE_HXX diff --git a/odb/sqlite/view-result.hxx b/odb/sqlite/view-result.hxx new file mode 100644 index 0000000..7f65c4b --- /dev/null +++ b/odb/sqlite/view-result.hxx @@ -0,0 +1,69 @@ +// file : odb/sqlite/view-result.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_VIEW_RESULT_HXX +#define ODB_SQLITE_VIEW_RESULT_HXX + +#include + +#include // std::size_t + +#include + +#include +#include // query, view_statements +#include +#include + +namespace odb +{ + namespace sqlite + { + template + class result_impl: + public odb::result_impl, + public result_impl_base + { + 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: + view_statements& statements_; + }; + } +} + +#include + +#include + +#endif // ODB_SQLITE_VIEW_RESULT_HXX diff --git a/odb/sqlite/view-result.txx b/odb/sqlite/view-result.txx new file mode 100644 index 0000000..eec8b0a --- /dev/null +++ b/odb/sqlite/view-result.txx @@ -0,0 +1,96 @@ +// file : odb/sqlite/view-result.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include +#include + +#include + +namespace odb +{ + namespace sqlite + { + template + result_impl:: + ~result_impl () + { + } + + template + result_impl:: + result_impl (const query& q, + details::shared_ptr statement, + view_statements& statements) + : base_type (statements.connection ().database ()), + result_impl_base (q, 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 () + { + throw result_not_cached (); + } + } +} diff --git a/odb/sqlite/view-statements.hxx b/odb/sqlite/view-statements.hxx new file mode 100644 index 0000000..e940495 --- /dev/null +++ b/odb/sqlite/view-statements.hxx @@ -0,0 +1,113 @@ +// file : odb/sqlite/view-statements.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_SQLITE_VIEW_STATEMENTS_HXX +#define ODB_SQLITE_VIEW_STATEMENTS_HXX + +#include + +#include // std::size_t + +#include +#include + +#include + +#include +#include +#include +#include + +namespace odb +{ + namespace sqlite + { + 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 sqlite::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, + image_binding_)); + + query_->cached (true); + } + + 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_SQLITE_VIEW_STATEMENTS_HXX diff --git a/odb/sqlite/view-statements.txx b/odb/sqlite/view-statements.txx new file mode 100644 index 0000000..790a537 --- /dev/null +++ b/odb/sqlite/view-statements.txx @@ -0,0 +1,35 @@ +// file : odb/sqlite/view-statements.txx +// author : Boris Kolpackov +// 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 sqlite + { + 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