From 5d60da7c563025101a7a94012bb0facf88add351 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 2 Oct 2011 16:21:47 +0200 Subject: Add simple view to hello example --- hello/README | 6 ++++-- hello/driver.cxx | 18 ++++++++++++++++++ hello/person.hxx | 14 ++++++++++++++ 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 r (db->query ()); + + // 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 +#include // std::size_t #include @@ -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 -- cgit v1.1