diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2015-02-04 17:23:54 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2015-02-04 17:23:54 +0200 |
commit | 0ac8f52ddfae8537651c65b8ab8b32db47756e74 (patch) | |
tree | be6239b8e7f528c7c25a05f83af2ec806043fbd6 | |
parent | 613da395f986ff748448d5afc06319f84a734397 (diff) |
Implement object loading views
See section 10.2 in the manual for details.
-rw-r--r-- | view/README | 5 | ||||
-rw-r--r-- | view/driver.cxx | 38 | ||||
-rw-r--r-- | view/employee.hxx | 15 | ||||
-rw-r--r-- | view/makefile | 2 |
4 files changed, 57 insertions, 3 deletions
diff --git a/view/README b/view/README index 0ade8ea..cba23b9 100644 --- a/view/README +++ b/view/README @@ -28,13 +28,14 @@ employee.sql following command line: odb -d <database> --generate-schema --generate-query \ - --default-pointer std::tr1::shared_ptr employee.hxx + --default-pointer std::tr1::shared_ptr --generate-session employee.hxx Where <database> stands for the database system we are using, for example, 'mysql'. The --default-pointer option is used to make TR1 shared_ptr the default - object pointer. + object pointer. The --generate-session option is used to enable session + support for all the objects which is required to use object loading views. database.hxx Contains the create_database() function which instantiates the concrete diff --git a/view/driver.cxx b/view/driver.cxx index cbe26ee..74d6eed 100644 --- a/view/driver.cxx +++ b/view/driver.cxx @@ -277,6 +277,44 @@ main (int argc, char* argv[]) t.commit (); } + // The same but using the object loading view. + // + { + typedef odb::query<employee_country_objects> query; + typedef odb::result<employee_country_objects> result; + + transaction t (db->begin ()); + + // We have to use a session in order for the object pointers + // in our view and object pointers inside objects that we load + // to point to the same instances, where appropriate. + // + session s; + + result r (db->query<employee_country_objects> ( + query::res::name == query::nat::name)); + + cout << "Employees residing inside the country of nationality" << endl; + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + assert (i->e->nationality () == i->nat); + assert (i->e->residence () == i->res); + + const employee& e (*i->e); + const country& r (*i->res); + const country& n (*i->nat); + + cout << " " << e.first () << " " << e.last () << " " + << r.name () << " " << n.name () << endl; + } + + cout << endl; + + t.commit (); + } + + // Get the list of employees that have accumulated vacation days. // { diff --git a/view/employee.hxx b/view/employee.hxx index 39d494f..87f0bce 100644 --- a/view/employee.hxx +++ b/view/employee.hxx @@ -239,6 +239,21 @@ struct employee_country std::string nat_country_name; }; +// An example of an object loading view. It is a different version of +// the above view that loads the complete objects instead of a subset +// of their data members. +// +#pragma db view object(employee) \ + object(country = res: employee::residence_) \ + object(country = nat: employee::nationality_) +struct employee_country_objects +{ + shared_ptr<employee> e; + shared_ptr<country> res; + shared_ptr<country> nat; +}; + + // An example of a native view that provides a complete query and is based // on an ad-hoc table. This view allows us to load the employee vacation // information from the legacy employee_extra table. diff --git a/view/makefile b/view/makefile index cfb061a..ca85d9b 100644 --- a/view/makefile +++ b/view/makefile @@ -46,7 +46,7 @@ gen := $(addprefix $(out_base)/,$(genf)) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): export odb_options += --generate-query --generate-schema \ ---default-pointer std::tr1::shared_ptr --table-prefix view_ +--default-pointer std::tr1::shared_ptr --generate-session --table-prefix view_ $(gen): cpp_options := -I$(src_base) $(gen): $(odb.l.cpp-options) |