aboutsummaryrefslogtreecommitdiff
path: root/composite/person.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-02-22 12:28:06 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-02-22 12:28:06 +0200
commit1f2834e458c4c28fe3264968a80f451636c232e4 (patch)
treeca5d05879fd3fb24afca9b2f2780955a114ce501 /composite/person.hxx
parent8a17d4e73d956ba6d95ffb685760d821bf185a3c (diff)
Add support for composite object ids
New pragma id_type (member). New test: common/composite-id. The composite example has also been updated.
Diffstat (limited to 'composite/person.hxx')
-rw-r--r--composite/person.hxx63
1 files changed, 59 insertions, 4 deletions
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<std::string, std::string> 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_;