aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--composite/README17
-rw-r--r--composite/driver.cxx11
-rw-r--r--composite/person.hxx63
3 files changed, 76 insertions, 15 deletions
diff --git a/composite/README b/composite/README
index e33e7a9..2368784 100644
--- a/composite/README
+++ b/composite/README
@@ -1,16 +1,17 @@
This example shows how to use composite value types as data members in objects
-and other value types, as element types in containers, and as base types for
-other composite value types. It also shows how to use composite value type
-data members in queries.
+(including as object id members) and other value types, as element types in
+containers, and as base types for other composite value types. It also shows
+how to use composite value type data members in queries.
The example consists of the following files:
person.hxx
- Header file defining the 'basic_name', 'name_extras', and 'name' composite
- value types. It also defines the 'phone_numbers' composite value type as
- an instantiation of the 'std::pair' class template. Finally it defines
- the 'person' persistent class which uses the 'name' and 'phone_numbers'
- value types in its data members.
+ Header file defining the 'basic_name', 'name_extras', 'name', and
+ 'email_address' composite value types. It also defines the 'phone_numbers'
+ composite value type as an instantiation of the 'std::pair' class template
+ Finally it defines the 'person' persistent class which uses 'email_address'
+ as its object id as well as 'name' and 'phone_numbers' in its other data
+ members.
person-odb.hxx
person-odb.ixx
diff --git a/composite/driver.cxx b/composite/driver.cxx
index cb77acd..aaedf17 100644
--- a/composite/driver.cxx
+++ b/composite/driver.cxx
@@ -24,9 +24,13 @@ main (int argc, char* argv[])
// Create a person object.
//
- unsigned int id;
+ email_address id;
{
- person p ("Joe", "Dirt", "Mr", phone_numbers ("555 5555", "666 6666"));
+ person p ("joe@example.com",
+ "Joe",
+ "Dirt",
+ "Mr",
+ phone_numbers ("555 5555", "666 6666"));
transaction t (db->begin ());
id = db->persist (p);
@@ -57,7 +61,8 @@ main (int argc, char* argv[])
name& n (joe->name ());
- cout << n.title () << " " << n.first () << " " << n.last () << endl;
+ cout << n.title () << " " << n.first () << " " << n.last () << " "
+ << '<' << joe->email ().address () << '>' << endl;
name_extras& ne (n.extras ());
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_;