aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-10-27 15:16:49 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-10-27 15:16:49 +0200
commit2f920671a7e4be7eb488724ae19360a87d66860c (patch)
treeba75343bc5e3daa6616658dfd158e83512ce4c19
parent16f68924f0704abba3598d261b4a4d74355179f1 (diff)
Add support for persistent classes without object ids
New pragma id (object). New test: common/no-id.
-rw-r--r--odb/mysql/forward.hxx3
-rw-r--r--odb/mysql/object-result.hxx56
-rw-r--r--odb/mysql/object-result.txx170
-rw-r--r--odb/mysql/object-statements.hxx122
-rw-r--r--odb/mysql/object-statements.txx34
-rw-r--r--odb/mysql/result.hxx9
-rw-r--r--odb/mysql/statement-cache.hxx10
-rw-r--r--odb/mysql/view-result.hxx12
-rw-r--r--odb/mysql/view-result.txx22
9 files changed, 388 insertions, 50 deletions
diff --git a/odb/mysql/forward.hxx b/odb/mysql/forward.hxx
index 5d665dc..8ed934c 100644
--- a/odb/mysql/forward.hxx
+++ b/odb/mysql/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/mysql/object-result.hxx b/odb/mysql/object-result.hxx
index ff99111..d2c6ab0 100644
--- a/odb/mysql/object-result.hxx
+++ b/odb/mysql/object-result.hxx
@@ -22,11 +22,10 @@ namespace odb
namespace mysql
{
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&);
@@ -68,6 +67,49 @@ namespace odb
object_statements<object_type>& statements_;
std::size_t count_;
};
+
+ 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:
+ void
+ fetch ();
+
+ private:
+ details::shared_ptr<select_statement> statement_;
+ object_statements_no_id<object_type>& statements_;
+ std::size_t count_;
+ };
}
}
diff --git a/odb/mysql/object-result.txx b/odb/mysql/object-result.txx
index d745537..e8b3560 100644
--- a/odb/mysql/object-result.txx
+++ b/odb/mysql/object-result.txx
@@ -12,17 +12,21 @@ namespace odb
{
namespace mysql
{
+ //
+ // 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),
@@ -31,7 +35,7 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_object>::
+ void object_result_impl<T>::
load (object_type& obj)
{
if (count_ > statement_->fetched ())
@@ -69,8 +73,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 ()
{
if (count_ > statement_->fetched ())
@@ -80,7 +84,147 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_object>::
+ void object_result_impl<T>::
+ next ()
+ {
+ this->current (pointer_type ());
+
+ // If we are cached, simply increment the position and
+ // postpone the actual row fetching until later. This way
+ // if the same object is loaded in between iteration, the
+ // image won't be messed up.
+ //
+ count_++;
+
+ if (statement_->cached ())
+ this->end_ = count_ > statement_->result_size ();
+ else
+ fetch ();
+ }
+
+ template <typename T>
+ void object_result_impl<T>::
+ fetch ()
+ {
+ // If the result is cached, the image can grow between calls
+ // to fetch() as a result of other statements execution.
+ //
+ if (statement_->cached ())
+ {
+ 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++;
+ }
+ }
+
+ while (!this->end_ && count_ > statement_->fetched ())
+ {
+ select_statement::result r (statement_->fetch ());
+
+ switch (r)
+ {
+ case select_statement::truncated:
+ {
+ // Don't re-fetch data we are skipping.
+ //
+ if (count_ != statement_->fetched ())
+ continue;
+
+ typename object_traits::image_type& im (statements_.image ());
+
+ 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_->refetch ();
+ }
+ // Fall throught.
+ }
+ case select_statement::success:
+ {
+ break;
+ }
+ case select_statement::no_data:
+ {
+ this->end_ = true;
+ break;
+ }
+ }
+ }
+ }
+
+ template <typename T>
+ void object_result_impl<T>::
+ cache ()
+ {
+ if (!statement_->cached ())
+ {
+ statement_->cache ();
+
+ if (count_ >= statement_->result_size ())
+ this->end_ = true;
+ }
+ }
+
+ template <typename T>
+ std::size_t object_result_impl<T>::
+ size ()
+ {
+ if (!statement_->cached ())
+ throw result_not_cached ();
+
+ 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),
+ count_ (0)
+ {
+ }
+
+ template <typename T>
+ void object_result_impl_no_id<T>::
+ load (object_type& obj)
+ {
+ if (count_ > statement_->fetched ())
+ fetch ();
+
+ odb::database& db (this->database ());
+
+ object_traits::callback (db, obj, callback_event::pre_load);
+ object_traits::init (obj, statements_.image (), 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 ());
@@ -99,7 +243,7 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_object>::
+ void object_result_impl_no_id<T>::
fetch ()
{
// If the result is cached, the image can grow between calls
@@ -161,7 +305,7 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_object>::
+ void object_result_impl_no_id<T>::
cache ()
{
if (!statement_->cached ())
@@ -174,7 +318,7 @@ namespace odb
}
template <typename T>
- std::size_t result_impl<T, class_object>::
+ std::size_t object_result_impl_no_id<T>::
size ()
{
if (!statement_->cached ())
diff --git a/odb/mysql/object-statements.hxx b/odb/mysql/object-statements.hxx
index f259feb..9ea9933 100644
--- a/odb/mysql/object-statements.hxx
+++ b/odb/mysql/object-statements.hxx
@@ -29,6 +29,28 @@ namespace odb
{
namespace mysql
{
+ 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_MYSQL_EXPORT object_statements_base: public statements_base
{
// Locking.
@@ -399,6 +421,106 @@ 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 mysql::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_;}
+
+ my_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_));
+
+ 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 select_column_count =
+ object_traits::column_count;
+
+ static const std::size_t insert_column_count =
+ object_traits::column_count;
+
+ private:
+ image_type image_;
+
+ // Select binding.
+ //
+ std::size_t select_image_version_;
+ binding select_image_binding_;
+ MYSQL_BIND select_image_bind_[select_column_count];
+ my_bool select_image_truncated_[select_column_count];
+
+ // Insert binding.
+ //
+ std::size_t insert_image_version_;
+ binding insert_image_binding_;
+ MYSQL_BIND insert_image_bind_[insert_column_count];
+
+ details::shared_ptr<insert_statement_type> persist_;
+ };
}
}
diff --git a/odb/mysql/object-statements.txx b/odb/mysql/object-statements.txx
index 740a7d9..372118d 100644
--- a/odb/mysql/object-statements.txx
+++ b/odb/mysql/object-statements.txx
@@ -16,6 +16,10 @@ namespace odb
{
namespace mysql
{
+ //
+ // object_statements
+ //
+
template <typename T>
object_statements<T>::
~object_statements ()
@@ -115,5 +119,35 @@ namespace odb
delayed_.clear ();
}
+
+ //
+ // object_statements
+ //
+
+ 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].error = select_image_truncated_ + i;
+ }
}
}
diff --git a/odb/mysql/result.hxx b/odb/mysql/result.hxx
index 33df541..cf6b15b 100644
--- a/odb/mysql/result.hxx
+++ b/odb/mysql/result.hxx
@@ -14,15 +14,6 @@
#include <odb/mysql/version.hxx>
#include <odb/mysql/forward.hxx>
-namespace odb
-{
- namespace mysql
- {
- template <typename T, class_kind kind>
- class result_impl;
- }
-}
-
#include <odb/post.hxx>
#endif // ODB_MYSQL_RESULT_HXX
diff --git a/odb/mysql/statement-cache.hxx b/odb/mysql/statement-cache.hxx
index d5ede48..d1aad0a 100644
--- a/odb/mysql/statement-cache.hxx
+++ b/odb/mysql/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/mysql/view-result.hxx b/odb/mysql/view-result.hxx
index 8acb97d..a2a9f3c 100644
--- a/odb/mysql/view-result.hxx
+++ b/odb/mysql/view-result.hxx
@@ -22,10 +22,10 @@ namespace odb
namespace mysql
{
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;
@@ -34,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/mysql/view-result.txx b/odb/mysql/view-result.txx
index 94d9ae6..8b5b598 100644
--- a/odb/mysql/view-result.txx
+++ b/odb/mysql/view-result.txx
@@ -13,16 +13,16 @@ namespace odb
namespace mysql
{
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),
@@ -31,7 +31,7 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_view>::
+ void view_result_impl<T>::
load (view_type& view)
{
if (count_ > statement_->fetched ())
@@ -45,7 +45,7 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_view>::
+ void view_result_impl<T>::
next ()
{
this->current (pointer_type ());
@@ -64,7 +64,7 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_view>::
+ void view_result_impl<T>::
fetch ()
{
// If the result is cached, the image can grow between calls
@@ -125,7 +125,7 @@ namespace odb
}
template <typename T>
- void result_impl<T, class_view>::
+ void view_result_impl<T>::
cache ()
{
if (!statement_->cached ())
@@ -138,7 +138,7 @@ namespace odb
}
template <typename T>
- std::size_t result_impl<T, class_view>::
+ std::size_t view_result_impl<T>::
size ()
{
if (!statement_->cached ())