From 290d22210040a7b373e2dc9084240cdeffa1fa3a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 21 Mar 2012 08:37:25 +0200 Subject: Polymorphic inheritance support --- odb/mssql/polymorphic-object-statements.txx | 136 ++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 odb/mssql/polymorphic-object-statements.txx (limited to 'odb/mssql/polymorphic-object-statements.txx') diff --git a/odb/mssql/polymorphic-object-statements.txx b/odb/mssql/polymorphic-object-statements.txx new file mode 100644 index 0000000..87ed696 --- /dev/null +++ b/odb/mssql/polymorphic-object-statements.txx @@ -0,0 +1,136 @@ +// file : odb/mssql/polymorphic-object-statements.txx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include // std::memset + +#include +#include + +#include +#include +#include + +namespace odb +{ + namespace mssql + { + // + // polymorphic_root_object_statements + // + + template + polymorphic_root_object_statements:: + ~polymorphic_root_object_statements () + { + } + + template + polymorphic_root_object_statements:: + polymorphic_root_object_statements (connection_type& conn) + : object_statements (conn), + discriminator_image_binding_ (discriminator_image_bind_, + discriminator_column_count + + managed_optimistic_column_count), + discriminator_id_image_binding_ (discriminator_id_image_bind_, + id_column_count) + { + discriminator_image_.version = 0; + discriminator_id_image_.version = 0; + + discriminator_image_version_ = 0; + discriminator_id_image_version_ = 0; + + std::memset ( + discriminator_image_bind_, 0, sizeof (discriminator_image_bind_)); + std::memset ( + discriminator_id_image_bind_, 0, sizeof (discriminator_id_image_bind_)); + } + + // + // polymorphic_derived_object_statements + // + + template + polymorphic_derived_object_statements:: + ~polymorphic_derived_object_statements () + { + } + + template + polymorphic_derived_object_statements:: + polymorphic_derived_object_statements (connection_type& conn) + : statements_base (conn), + root_statements_ (conn.statement_cache ().find_object ()), + base_statements_ (conn.statement_cache ().find_object ()), + container_statement_cache_ (conn), + insert_image_binding_ (insert_image_bind_, insert_column_count), + update_image_binding_ (update_image_bind_, + update_column_count + id_column_count) + { + image_.base = &base_statements_.image (); + image_.version = 0; + + for (std::size_t i (0); i < object_traits::depth; ++i) + select_image_versions_[i] = 0; + + for (std::size_t i (0); + i < (object_traits::abstract ? 1 : object_traits::depth); + ++i) + { + select_image_bindings_[i].bind = select_image_bind_; + select_image_bindings_[i].count = object_traits::find_column_counts[i]; + select_image_bindings_[i].change_callback = 0; + } + + // Statements other than the first one (which goes all the way to + // the root) can never override the image because they are used to + // load up the dynamic part of the object only after the static + // part has been loaded (and triggered the callback if necessary). + // + select_image_bindings_[0].change_callback = + root_statements_.image ().change_callback (); + + insert_image_version_ = 0; + insert_id_binding_version_ = 0; + update_image_version_ = 0; + update_id_binding_version_ = 0; + + std::memset (insert_image_bind_, 0, sizeof (insert_image_bind_)); + std::memset (update_image_bind_, 0, sizeof (update_image_bind_)); + std::memset (select_image_bind_, 0, sizeof (select_image_bind_)); + } + + template + void polymorphic_derived_object_statements:: + delayed_loader (odb::database& db, const id_type& id, root_type& robj) + { + connection_type& conn (transaction::current ().connection ()); + polymorphic_derived_object_statements& sts ( + conn.statement_cache ().find_object ()); + root_statements_type& rsts (sts.root_statements ()); + + object_type& obj (static_cast (robj)); + + // The same code as in object_statements::load_delayed_(). + // + if (!object_traits::find_ (sts, &id)) + throw object_not_persistent (); + + auto_result ar (*sts.find_[0]); + + object_traits::callback (db, obj, callback_event::pre_load); + object_traits::init (obj, sts.image (), &db); + sts.find_[0]->stream_result (); + ar.free (); + object_traits::load_ (sts, obj); // Load containers, etc. + + rsts.load_delayed (); + + { + typename root_statements_type::auto_unlock u (rsts); + object_traits::callback (db, obj, callback_event::post_load); + } + } + } +} -- cgit v1.1