Wt  4.0.2
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":

namespace Wt {
namespace Dbo {
template<>
struct dbo_traits<Foo> : dbo_default_traits
{
static const char *surrogateIdField() { return "foo_id"; }
};
// Necessary if you want to use ptr<const Foo>
template<> struct dbo_traits<const Foo> : dbo_traits<Foo> {};
}
}
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:
class Foo;
namespace Wt {
namespace Dbo {
template<> struct dbo_traits<Foo> : ... { };
}
}
class Foo {
// definition here, including the persist() function
};

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:

struct Coordinate {
int x, y;
Coordinate()
: x(-1), y(-1) { }
bool operator== (const Coordinate& other) const {
return x == other.x && y == other.y;
}
bool operator< (const Coordinate& other) const {
if (x < other.x)
return true;
else if (x == other.x)
return y < other.y;
else
return false;
}
};
std::ostream& operator<< (std::ostream& o, const Coordinate& c)
{
return o << "(" << c.x << ", " << c.y << ")";
}
namespace Wt {
namespace Dbo {
template <class Action>
void field(Action& action, Coordinate& coordinate, const std::string& name, int size = -1)
{
field(action, coordinate.x, name + "_x");
field(action, coordinate.y, name + "_y");
}
}
}

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 nullptr 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 nullptr instead.


Generated on Fri Nov 24 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11