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

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Types | Static Public Member Functions
Wt::Dbo::dbo_traits< C > Class Template Reference
Database Objects (Wt::Dbo)

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

`#include <Wt/Dbo/Dbo>`

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

List of all members.

## Public Types
typedef YourIdType | 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.

## 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>
         {
            static const char *surrogateIdField() { return "foo_id"; }
         };
       }
     }

## 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");
         }
       }
     }

Reimplemented from Wt::Dbo::dbo_default_traits.

## 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.

Reimplemented from Wt::Dbo::dbo_default_traits.

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

Reimplemented from Wt::Dbo::dbo_default_traits.

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.

Reimplemented from Wt::Dbo::dbo_default_traits.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator