diff options
-rw-r--r-- | query/driver.cxx | 24 | ||||
-rw-r--r-- | query/person.hxx | 16 |
2 files changed, 36 insertions, 4 deletions
diff --git a/query/driver.cxx b/query/driver.cxx index 5b7c926..1be6107 100644 --- a/query/driver.cxx +++ b/query/driver.cxx @@ -27,7 +27,12 @@ print (result& r) { for (result::iterator i (r.begin ()); i != r.end (); ++i) { - cout << i->first () << " " << i->last () << " " << i->age () << endl; + cout << i->first () << " "; + + if (!i->middle ().null ()) + cout << i->middle ().get () << " "; + + cout << i->last () << " " << i->age () << endl; } cout << endl; @@ -48,9 +53,9 @@ main (int argc, char* argv[]) p.push_back (person ("John", "Doe", 21)); p.push_back (person ("John", "Smith", 22)); p.push_back (person ("Jack", "Johnson", 31)); - p.push_back (person ("John", "Jackson", 32)); - p.push_back (person ("Jane", "Doe", 23)); - p.push_back (person ("Jane", "Smith", 24)); + p.push_back (person ("John", "JJ", "Jackson", 32)); + p.push_back (person ("Jane", "JD", "Doe", 23)); + p.push_back (person ("Jane", "JS", "Smith", 24)); transaction t (db->begin ()); @@ -174,6 +179,17 @@ main (int argc, char* argv[]) t.commit (); } + + // Query that shows how to test for NULL values using the + // is_null()/is_not_null() functions. + // + { + transaction t (db->begin ()); + result r (db->query<person> (query::middle.is_not_null ())); + print (r); + t.commit (); + } + } catch (const odb::exception& e) { diff --git a/query/person.hxx b/query/person.hxx index 72da079..db312f2 100644 --- a/query/person.hxx +++ b/query/person.hxx @@ -8,6 +8,7 @@ #include <string> #include <odb/core.hxx> +#include <odb/nullable.hxx> #pragma db object class person @@ -20,12 +21,26 @@ public: { } + person (const std::string& first, + const std::string& middle, + const std::string& last, + unsigned short age) + : first_ (first), middle_ (middle), last_ (last), age_ (age) + { + } + const std::string& first () const { return first_; } + const odb::nullable<std::string>& + middle () const + { + return middle_; + } + const std::string& last () const { @@ -47,6 +62,7 @@ private: unsigned long id_; std::string first_; + odb::nullable<std::string> middle_; std::string last_; unsigned short age_; }; |