From 932cd7a53b3996468fee5cfa63c2b2998dbe971a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 4 Jul 2011 17:53:47 +0200 Subject: Implement support for database operations callbacks New object pragma: callback. New test: common/callback. New manual section: 10.1.4, "callback". --- doc/manual.xhtml | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/manual.xhtml b/doc/manual.xhtml index b60eafc..a06bfa6 100644 --- a/doc/manual.xhtml +++ b/doc/manual.xhtml @@ -414,6 +414,7 @@ for consistency. 10.1.1table 10.1.2pointer 10.1.3abstract + 10.1.4callback @@ -5749,8 +5750,13 @@ class person 10.1.3 - + + callback + database operations callback + 10.1.4 + +

10.1.1 table

@@ -5851,6 +5857,124 @@ class contractor: public person discussion of persistent class inheritance, refer to Chapter 8, "Inheritance".

+

10.1.4 callback

+ +

The callback specifier specifies the persist class + member function that should be called before and after a + database operation is performed on an object of this class. + For example:

+ +
+#include <odb/callback.hxx>
+
+#pragma db object callback(init)
+class person
+{
+  ...
+
+  void
+  init (odb::callback_event, odb::database&);
+};
+ 
+ +

The callback function has the following signature and can be + overloaded for constant objects:

+ +
+void
+name (odb::callback_event, odb::database&);
+
+void
+name (odb::callback_event, odb::database&) const;
+  
+ +

The first argument to the callback function is the event that + triggered this call. The odb::callback_event + enum-like type is defined in the <odb/callback.hxx> + header file and has the following interface:

+ +
+namespace odb
+{
+  struct callback_event
+  {
+    enum value
+    {
+      pre_persist,
+      post_persist,
+      pre_load,
+      post_load,
+      pre_update,
+      post_update,
+      pre_erase,
+      post_erase
+    };
+
+    callback_event (value v);
+    operator value () const;
+  };
+}
+  
+ +

The second argument to the callback function is the database + on which the operation is about to be performed or has just + been performed.

+ +

If only the non-const version of the callback function + is provided, then only database operations that are performed on + unrestricted objects will trigger callback calls. If only the + const version is provided, then the database + operations on both constant and unrestricted objects will trigger + callback calls but the object will always be passed as constant. + Finally, if both versions are provided, then the const + overload will be called for constant objects and the non-const + overload for unrestricted objects. These rules are modeled after + the standard C++ overload resolution rules. A callback function can + be inline or virtual.

+ +

A database operations callback can be used to implement object-specific + pre and post initializations, registrations, and cleanups. As an example, + the following code fragment outlines an implementation of a + person class that maintains the transient age + data member in addition to the persistent date of birth. A callback + is used to calculate the value of the former from the latter every + time a person object is loaded from the database.

+ +
+#include <odb/core.hxx>
+#include <odb/callback.hxx>
+
+#pragma db object callback(init)
+class person
+{
+  ...
+
+private:
+  friend class odb::access;
+
+  date born_;
+
+  #pragma db transient
+  unsigned short age_;
+
+  void
+  init (odb::callback_event e, odb::database&)
+  {
+    switch (e)
+    {
+    case odb::callback_event::post_load:
+    {
+      // Calculate age from the date of birth.
+      ...
+      break;
+    }
+    default:
+      break;
+    }
+  }
+};
+ 
+

10.2 Value Type Pragmas

A pragma with the value qualifier describes a value -- cgit v1.1