Wt  3.3.6
Public Types | Static Public Member Functions | List of all members
Wt::Dbo::dbo_traits< C > Class Template Reference

Traits for a class mapped with Wt::Dbo. More...

#include <Wt/Dbo/Dbo>

Inheritance diagram for Wt::Dbo::dbo_traits< C >:
Inheritance graph
[legend]

Public Types

typedef YourIdType IdType
 Type of the primary key. More...
 
- Public Types inherited from Wt::Dbo::dbo_default_traits
typedef long long IdType
 Type of the primary key. More...
 

Static Public Member Functions

static IdType invalidId ()
 Returns the sentinel value for a null id. More...
 
static const char * surrogateIdField ()
 Configures the surrogate primary key field. More...
 
static const char * versionField ()
 Configures the optimistic concurrency version field. More...
 
- Static Public Member Functions inherited from Wt::Dbo::dbo_default_traits
static IdType invalidId ()
 Returns the sentinel value for a null id. More...
 
static const char * surrogateIdField ()
 Returns the database field name for the surrogate primary key. More...
 
static const char * versionField ()
 Configures the optimistic concurrency version field. More...
 

Detailed Description

template<class C>
class Wt::Dbo::dbo_traits< C >

Traits for a class mapped with Wt::Dbo.

The traits class provides some of the mapping properties related to the primary key and optimistic concurrency locking using a version field.

See dbo_default_traits for default values.

The following example changes the surrogate id field name for a class Foo from the default "id" to "foo_id":

1 namespace Wt {
2  namespace Dbo {
3 
4  template<>
5  struct dbo_traits<Foo> : dbo_default_traits
6  {
7  static const char *surrogateIdField() { return "foo_id"; }
8  };
9 
10  // Necessary if you want to use ptr<const Foo>
11  template<> struct dbo_traits<const Foo> : dbo_traits<Foo> {};
12  }
13 }
Note
The safe pattern to define traits is before the class definition, based on a forward declaration. This is necessary since the persist() function relies on this specialization:
1 class Foo;
2 
3 namespace Wt {
4  namespace Dbo {
5  template<> struct dbo_traits<Foo> : ... { };
6  }
7 }
8 
9 class Foo {
10  // definition here, including the persist() function
11 };

Member Typedef Documentation

template<class C>
typedef YourIdType Wt::Dbo::dbo_traits< C >::IdType

Type of the primary key.

This indicates the type of the primary key, which needs to be long long for a surrogate id, but can be any type supported by Wt::Dbo::field() (including composite types) for a natural primary key.

The following operations need to be supported for an id value:

  • default constructor
  • copy constructor
  • serialization to a string (for formatting an error message in exceptions) : std::ostream << id
  • comparison operator (for use as a key in a std::map): id == id
  • less than operator (for use as a key in a std::map): id < id

Only the default long long is supported for an auto-incrementing surrogate primary key. You need to change the default key type typically in conjuction with specifying a natural id, see Wt::Dbo::id().

The following example illustrates how to prepare a type to be usable as a composite id type:

1 struct Coordinate {
2  int x, y;
3 
4  Coordinate()
5  : x(-1), y(-1) { }
6 
7  bool operator== (const Coordinate& other) const {
8  return x == other.x && y == other.y;
9  }
10 
11  bool operator< (const Coordinate& other) const {
12  if (x < other.x)
13  return true;
14  else if (x == other.x)
15  return y < other.y;
16  else
17  return false;
18  }
19 };
20 
21 std::ostream& operator<< (std::ostream& o, const Coordinate& c)
22 {
23  return o << "(" << c.x << ", " << c.y << ")";
24 }
25 
26 namespace Wt {
27  namespace Dbo {
28 
29  template <class Action>
30  void field(Action& action, Coordinate& coordinate, const std::string& name, int size = -1)
31  {
32  field(action, coordinate.x, name + "_x");
33  field(action, coordinate.y, name + "_y");
34  }
35  }
36 }

Member Function Documentation

template<class C>
static IdType Wt::Dbo::dbo_traits< C >::invalidId ( )
static

Returns the sentinel value for a null id.

When used as a foreign key, this value is used to represent a null value.

template<class C>
static const char* Wt::Dbo::dbo_traits< C >::surrogateIdField ( )
static

Configures the surrogate primary key field.

Returns the field name which is the surrogate primary key, corresponding to the object's id.

You can disable this auto-incrementing surrogate id by returning 0 instead. In that case you will need to define a natural id for your class using Wt::Dbo::id().

template<class C>
static const char* Wt::Dbo::dbo_traits< C >::versionField ( )
static

Configures the optimistic concurrency version field.

Optimistic concurrency locking is used to detect concurrent updates by an object from multiple sessions. On each update, the version of a record is at the same time checked (to see if it matches the version of the record that was read), and incremented. A StaleObjectException is thrown if a record was modified by another session since it was read.

This method must return the database field name used for this version field.

You can disable optimistic locking using a version field all together for your class by returning 0 instead.


Generated on Mon Aug 8 2016 for the C++ Web Toolkit (Wt) by doxygen 1.8.11