From 5da0df2986a62e1254d27c8cfbc383ba580555e1 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Thu, 7 Apr 2011 12:47:27 +0200 Subject: Update examples to include qt/basic and qt/date-time usage --- qt/README | 24 +++++---- qt/driver.cxx | 131 +++++++++++++++++++++++++++++++++++++++++++--- qt/employee.hxx | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qt/makefile | 24 ++++----- qt/person.hxx | 41 --------------- 5 files changed, 305 insertions(+), 72 deletions(-) create mode 100644 qt/employee.hxx delete mode 100644 qt/person.hxx (limited to 'qt') diff --git a/qt/README b/qt/README index 94663e1..b67eea4 100644 --- a/qt/README +++ b/qt/README @@ -4,13 +4,19 @@ containers, and value types with the help of the Qt profile library The example consists of the following files: -person.hxx - Describe contents. - -person-odb.hxx -person-odb.ixx -person-odb.cxx -person.sql +employee.hxx + Header file defining the 'employee' and 'employer' persistent classes. + We use QSharedPointer/QWeakPointer smart pointers provided by Qt (as + well as their lazy versions provided by the Qt profile library) to + establish a bidirectional employee-employer relationship. We also use + the QDateTime type to store the employee's date of birth and QString + to store the employee's first and last name. Finally we use QByteArray + to represent the persons genetic fingerprint. + +employee-odb.hxx +employee-odb.ixx +employee-odb.cxx +employee.sql The first three files contain the database support code and the last file contains the database schema for the employee.hxx header. @@ -39,8 +45,8 @@ driver.cxx instance. It then creates a number of 'employee' and 'employer' objects and persists them in the database. The next transaction loads all the employees of a particular employer using the employee-employer relationship. Finally, - the driver performs a database query which uses a data member of the Boost - gregorian::date type in its criterion. + the driver performs a database query which uses a data member of the Qt + QString type in its criterion. To run the example we first need to create the database schema. Using MySQL as an example, this can be achieved with the following command: diff --git a/qt/driver.cxx b/qt/driver.cxx index 1d4e2b3..5dfe13f 100644 --- a/qt/driver.cxx +++ b/qt/driver.cxx @@ -2,7 +2,6 @@ // author : Constantin Michael // copyright : not copyrighted - public domain -#include // std::auto_ptr #include #include @@ -11,12 +10,18 @@ #include "database.hxx" // create_database -#include "person.hxx" -#include "person-odb.hxx" +#include "employee.hxx" +#include "employee-odb.hxx" using namespace std; using namespace odb::core; +ostream& operator << (ostream& os, const QString& s) +{ + os << s.toStdString (); + return os; +} + int main (int argc, char* argv[]) { @@ -24,15 +29,125 @@ main (int argc, char* argv[]) { auto_ptr db (create_database (argc, argv)); - person p1; - p1.name = "Constantin Michael"; - p1.date_of_birth.setDate (1979, 03, 07); + // Create a few persistent objects. + // + { + // Simple Tech Ltd. + // + { + shared_ptr er (new employer ("Simple Tech Ltd")); + + shared_ptr john ( + new employee ("John", + "Doe", + QDate (1974, 5, 23), + QByteArray ("\0xF1\0x00\0x34\0x45\0x00\0xDE", 6), + er)); + + shared_ptr jane ( + new employee ("Jane", + "Doe", + QDate (1983, 1, 18), + QByteArray ("\0xD7\0x00\0x14", 3), + er)); + + // Set the inverse side of the employee-employer relationship. + // + er->employees ().push_back (john); + er->employees ().push_back (jane); + + transaction t (db->begin ()); + + db->persist (er); - // Persist. + db->persist (john); + db->persist (jane); + + t.commit (); + } + + // Complex Systems Inc.dob + // + { + shared_ptr er (new employer ("Complex Systems Inc")); + + shared_ptr john ( + new employee ("John", + "Smith", + QDate (1954, 8, 1), + QByteArray ("\0x23\0xFD\0x8F\0x00", 4), + er)); + + shared_ptr jane ( + new employee ("Jane", + "Smith", + QDate (1976, 12, 31), + QByteArray ("0x00\0x32\0x00\0x01\0x00", 5), + er)); + + // Set the inverse side of the employee-employer relationship. + // + er->employees ().push_back (john); + er->employees ().push_back (jane); + + transaction t (db->begin ()); + + db->persist (er); + + db->persist (john); + db->persist (jane); + + t.commit (); + } + } + + + // Load Simple Tech Ltd and print its employees. // { + session s; transaction t (db->begin ()); - db->persist (p1); + + shared_ptr stl (db->load ("Simple Tech Ltd")); + + employees& es (stl->employees ()); + + for (employees::iterator i (es.begin ()); i != es.end (); ++i) + { + lazy_weak_ptr& lwp (*i); + + // Load and lock the employee and his employer. + // + shared_ptr p (lwp.load ()); + shared_ptr pe (p->employer ().load ()); + + cout << p->first () << " " << p->last () << endl + << " born: " << p->born ().toString () << endl + << " public key length: " << p->public_key ().size () << endl + << " employer: " + << pe->name ().toAscii ().data () << endl; + + cout << endl; + } + + t.commit (); + } + + // Search for Complex Systems Inc employees. + // + { + typedef odb::query query; + typedef odb::result result; + + session s; + transaction t (db->begin ()); + + result r (db->query ( + query::employer::name == "Complex Systems Inc")); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + cout << i->first () << " " << i->last () << endl; + t.commit (); } } diff --git a/qt/employee.hxx b/qt/employee.hxx new file mode 100644 index 0000000..1e39993 --- /dev/null +++ b/qt/employee.hxx @@ -0,0 +1,157 @@ +// file : qt/employee.hxx +// author : Constantin Michael +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include + +#include +#include +#include + +#include + +// Include TR1 header in a compiler-specific fashion. Fall back +// on the Boost implementation if the compiler does not support TR1. +// +#include + +#include + +using std::tr1::shared_ptr; + +using odb::tr1::lazy_shared_ptr; +using odb::tr1::lazy_weak_ptr; + +// Forward declarations. +// +class employer; +class employee; + +typedef std::vector > employees; + +#pragma db object +class employer +{ +public: + employer (const QString& name) + : name_ (name) + { + } + + const QString& + name () const + { + return name_; + } + + // Employees of this employer. + // + typedef ::employees employees_type; + + const employees_type& + employees () const + { + return employees_; + } + + employees_type& + employees () + { + return employees_; + } + +private: + friend class odb::access; + + employer () {} + + #pragma db id + QString name_; + + #pragma db not_null inverse(employer_) + employees_type employees_; +}; + +#pragma db object +class employee +{ +public: + typedef ::employer employer_type; + + employee (const QString& first, + const QString& last, + const QDate& born, + const QByteArray& public_key, + shared_ptr employer) + : first_ (first), last_ (last), born_(born), public_key_(public_key), + employer_ (employer) + { + } + + // Name. + // + const QString& + first () const + { + return first_; + } + + const QString& + last () const + { + return last_; + } + + // Date of birth. + // + const QDate& + born () const + { + return born_; + } + + // Public key. + // + const QByteArray& + public_key () const + { + return public_key_; + } + + // Employer. + // + lazy_shared_ptr + employer () const + { + return employer_; + } + + void + employer (shared_ptr employer) + { + employer_ = employer; + } + +private: + friend class odb::access; + + employee () {} + + #pragma db id auto + unsigned long id_; + + QString first_; + QString last_; + + QDate born_; + + QByteArray public_key_; + + #pragma db not_null + lazy_shared_ptr employer_; +}; + +#endif // EMPLOYEE_HXX diff --git a/qt/makefile b/qt/makefile index a47858d..22af2d1 100644 --- a/qt/makefile +++ b/qt/makefile @@ -6,7 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make cxx_tun := driver.cxx -odb_hdr := person.hxx +odb_hdr := employee.hxx cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) cxx_od := $(cxx_obj:.o=.o.d) @@ -29,11 +29,11 @@ $(call import,\ $(scf_root)/import/libodb-qt/stub.make,\ l: odb_qt.l,cpp-options: odb_qt.l.cpp-options) -ifdef db_id $(call import,\ $(scf_root)/import/libqt/core/stub.make,\ l: qt_core.l,cpp-options: qt_core.l.cpp-options) +ifdef db_id $(call import,\ $(scf_root)/import/libodb-$(db_id)/stub.make,\ l: odb_db.l,cpp-options: odb_db.l.cpp-options) @@ -45,14 +45,10 @@ endif # Build. # -$(driver): $(cxx_obj) $(odb_db.l) $(odb_qt.l) $(odb.l) -$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(driver): $(cxx_obj) $(odb_db.l) $(odb_qt.l) $(odb.l) $(qt_core.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) -D$(db_macro) $(cxx_obj) $(cxx_od): $(odb.l.cpp-options) $(odb_qt.l.cpp-options) \ -$(odb_db.l.cpp-options) - -ifeq ($(db_id),mysql) -$(cxx_obj) $(cxx_od): cpp_options += -DDATABASE_MYSQL -endif +$(odb_db.l.cpp-options) $(qt_core.l.cpp-options) genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) gen := $(addprefix $(out_base)/,$(genf)) @@ -60,9 +56,9 @@ gen := $(addprefix $(out_base)/,$(genf)) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): export odb_options += --database $(db_id) --profile qt \ ---generate-query --generate-schema -$(gen): cpp_options := -I$(out_base) -$(gen): $(odb.l.cpp-options) $(odb_qt.l.cpp-options) $(qt.l.cpp-options) +--generate-query --generate-schema --default-pointer std::tr1::shared_ptr +$(gen): cpp_options := -I$(src_base) +$(gen): $(odb.l.cpp-options) $(odb_qt.l.cpp-options) $(qt_core.l.cpp-options) $(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) @@ -91,9 +87,9 @@ $(dist): # Test. # -$(test): schema := $(src_base)/$(basename $(odb_hdr)).sql +$(test): header := $(odb_hdr) $(test): $(driver) - $(call message,sql $$1,$(dcf_root)/db-driver $$1,$(schema)) + $(call schema) $(call message,test $<,$< --options-file $(dcf_root)/db.options) # Clean. diff --git a/qt/person.hxx b/qt/person.hxx deleted file mode 100644 index 28460c4..0000000 --- a/qt/person.hxx +++ /dev/null @@ -1,41 +0,0 @@ -// file : qt/person.hxx -// author : Constantin Michael -// copyright : not copyrighted - public domain - -#ifndef EMPLOYEE_HXX -#define EMPLOYEE_HXX - -#include -#include -#include -#include - -#pragma db object -struct time_punchcard -{ - #pragma db id auto - unsigned long id; - - QDateTime date_time; -}; - -#pragma db object -struct person -{ - bool - operator== (const person& x) const - { - return name == x.name && - date_of_birth == x.date_of_birth && - time_of_birth == x.time_of_birth; - } - - #pragma db id - QString name; - - #pragma db type("DATE NOT NULL") - QDate date_of_birth; - QTime time_of_birth; -}; - -#endif // EMPLOYEE_HXX -- cgit v1.1