From 1f2834e458c4c28fe3264968a80f451636c232e4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 22 Feb 2012 12:28:06 +0200 Subject: Add support for composite object ids New pragma id_type (member). New test: common/composite-id. The composite example has also been updated. --- composite/person.hxx | 63 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 4 deletions(-) (limited to 'composite/person.hxx') diff --git a/composite/person.hxx b/composite/person.hxx index 4496180..c80e2d8 100644 --- a/composite/person.hxx +++ b/composite/person.hxx @@ -129,16 +129,71 @@ private: typedef std::pair phone_numbers; #pragma db value(phone_numbers) +// We can also use a composite value type as an object id. +// +#pragma db value +class email_address +{ +public: + email_address () {} + email_address (const std::string& address) + { + std::string::size_type p (address.find ('@')); + recipient_.assign (address, 0, p); + domain_.assign (address, p + 1, std::string::npos); + } + + const std::string& + recipient () const + { + return recipient_; + } + + const std::string& + domain () const + { + return domain_; + } + + std::string + address () const + { + return recipient_ + '@' + domain_; + } + +private: + friend class odb::access; + + std::string recipient_; + std::string domain_; +}; + +inline bool +operator< (const email_address& x, const email_address& y) +{ + return x.recipient () < y.recipient () || + (x.recipient () == y.recipient () && x.domain() < y.domain ()); +} + #pragma db object class person { public: - person (const std::string& first, + person (const std::string& email, + const std::string& first, const std::string& last, const std::string& title, const phone_numbers& phone) - : name_ (first, last, title), phone_ (phone) + : email_ (email), name_ (first, last, title), phone_ (phone) + { + } + + // Email address. + // + const email_address& + email () const { + return email_; } // Name. @@ -170,8 +225,8 @@ private: person (): name_ ("", "", "") {} - #pragma db id auto - unsigned long id_; + #pragma db id + email_address email_; name_type name_; phone_numbers phone_; -- cgit v1.1