Wt: Wt::Dbo::dbo_traits< C > Class Template Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Types | Static Public Member Functions | List of all members
Wt::Dbo::dbo_traits< C > Class Template Reference
Database Objects (Wt::Dbo)

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

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

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

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

## 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> {};
}
}
Wt::Dbo::Dbo
A base class for database objects.
Definition ptr.h:438
Wt
The namespace for Wt.
Definition strand.hpp:29
Wt::Dbo::dbo_default_traits
Default traits for a class mapped with Wt::Dbo.
Definition ptr.h:159
Wt::Dbo::dbo_traits
Traits for a class mapped with Wt::Dbo.
Definition WGlobal.h:281
Wt::Dbo::dbo_traits::surrogateIdField
static const char * surrogateIdField()
Configures the surrogate primary key field.

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

## ◆ IdType

template<class C >

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");
}
}
}
Wt::Dbo::field
void field(Action &action, V &value, const std::string &name, int size=-1)
Maps a database object field.

## Member Function Documentation

## ◆ invalidId()

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.

## ◆ surrogateIdField()

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().

## ◆ versionField()

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.