aboutsummaryrefslogtreecommitdiff
path: root/hello
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-10-02 16:21:47 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-10-02 16:21:47 +0200
commit5d60da7c563025101a7a94012bb0facf88add351 (patch)
tree66eb7e3f2501fa4c761c08dcae351d0bc9625806 /hello
parentc428f1b17bbeee47cb16ecca6992f4bf19e382dd (diff)
Add simple view to hello example
Diffstat (limited to 'hello')
-rw-r--r--hello/README6
-rw-r--r--hello/driver.cxx18
-rw-r--r--hello/person.hxx14
3 files changed, 36 insertions, 2 deletions
diff --git a/hello/README b/hello/README
index 5929f52..7a5a04f 100644
--- a/hello/README
+++ b/hello/README
@@ -1,12 +1,14 @@
This is a "Hello World" example that shows how to use ODB to perform basic
database operations, such as making objects persistent, loading, updating
and deleting persistent objects, as well as querying the database for
-objects matching a certain criteria.
+objects matching a certain criteria. It also includes an example of a
+simple view.
The example consists of the following files:
person.hxx
- Header file defining the 'person' persistent class.
+ Header file defining the 'person' persistent class as well as the
+ 'person_stat' view.
person-odb.hxx
person-odb.ixx
diff --git a/hello/driver.cxx b/hello/driver.cxx
index 16b1b75..2b7ba1f 100644
--- a/hello/driver.cxx
+++ b/hello/driver.cxx
@@ -95,6 +95,24 @@ main (int argc, char* argv[])
}
*/
+ // Print some statistics about all the people in our database.
+ //
+ {
+ transaction t (db->begin ());
+
+ odb::result<person_stat> r (db->query<person_stat> ());
+
+ // The result of this query always has exactly one element.
+ //
+ const person_stat& ps (*r.begin ());
+
+ cout << endl
+ << "count : " << ps.count << endl
+ << "min age: " << ps.min_age << endl
+ << "max age: " << ps.max_age << endl;
+
+ t.commit ();
+ }
// John Doe is no longer in our database.
//
diff --git a/hello/person.hxx b/hello/person.hxx
index 69b1878..f480723 100644
--- a/hello/person.hxx
+++ b/hello/person.hxx
@@ -6,6 +6,7 @@
#define PERSON_HXX
#include <string>
+#include <cstddef> // std::size_t
#include <odb/core.hxx>
@@ -57,4 +58,17 @@ private:
unsigned short age_;
};
+#pragma db view object(person)
+struct person_stat
+{
+ #pragma db column("count(" + person::id_ + ")")
+ std::size_t count;
+
+ #pragma db column("min(" + person::age_ + ")")
+ unsigned short min_age;
+
+ #pragma db column("max(" + person::age_ + ")")
+ unsigned short max_age;
+};
+
#endif // PERSON_HXX