diff options
-rw-r--r-- | hello/README | 6 | ||||
-rw-r--r-- | hello/driver.cxx | 18 | ||||
-rw-r--r-- | 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<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 |