From 8e29be9f2229a6c618f95b725502f7082f02d50a Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Tue, 22 Mar 2011 10:49:56 +0200 Subject: Add Qt profile example --- qt/README | 56 +++++++ qt/database.hxx | 46 ++++++ qt/driver.cxx | 45 ++++++ qt/employee.hxx | 15 ++ qt/makefile | 131 +++++++++++++++++ qt/qt-vc10.sln | 15 ++ qt/qt-vc10.vcxproj | 174 ++++++++++++++++++++++ qt/qt-vc10.vcxproj.filters | 25 ++++ qt/qt-vc9.sln | 15 ++ qt/qt-vc9.vcproj | 357 +++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 879 insertions(+) create mode 100644 qt/README create mode 100644 qt/database.hxx create mode 100644 qt/driver.cxx create mode 100644 qt/employee.hxx create mode 100644 qt/makefile create mode 100644 qt/qt-vc10.sln create mode 100644 qt/qt-vc10.vcxproj create mode 100644 qt/qt-vc10.vcxproj.filters create mode 100644 qt/qt-vc9.sln create mode 100644 qt/qt-vc9.vcproj (limited to 'qt') diff --git a/qt/README b/qt/README new file mode 100644 index 0000000..4e94255 --- /dev/null +++ b/qt/README @@ -0,0 +1,56 @@ +This example shows how to persist objects that use Qt smart pointers, +containers, and value types with the help of the Qt profile library +(libodb-qt). + +The example consists of the following files: + +employee.hxx + Describe contents. + +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. + + These files are generated by the ODB compiler from employee.hxx using the + following command line: + + odb -d -p qt --generate-schema --generate-query employee.hxx + + Where stands for the database system we are using, for example, + 'mysql'. + + The -p option is used to instruct the ODB compiler to load the Qt + profile. + +database.hxx + Contains the create_database() function which instantiates the concrete + database class corresponding to the database system we are using. + +driver.cxx + Driver for the example. It includes the employee.hxx and employee-odb.hxx + headers to gain access to the persistent classes and their database support + code. It also includes database.hxx for the create_database() function + declaration. + + In main() the driver first calls create_database() to obtain the database + 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. + +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: + +mysql --user=odb_test --database=odb_test < employee.sql + +Here we use 'odb_test' as the database login and also 'odb_test' as the +database name. + +Once the database schema is ready, we can run the example (using MySQL as +the database): + +./driver --user odb_test --database odb_test diff --git a/qt/database.hxx b/qt/database.hxx new file mode 100644 index 0000000..c7c6957 --- /dev/null +++ b/qt/database.hxx @@ -0,0 +1,46 @@ +// file : qt/database.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// +// Create concrete database instance based on the DATABASE_* macros. +// + +#ifndef DATABASE_HXX +#define DATABASE_HXX + +#include +#include // std::auto_ptr +#include // std::exit +#include + +#include + +#if defined(DATABASE_MYSQL) +# include +#endif + +inline std::auto_ptr +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb::core; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + odb::mysql::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + return auto_ptr (new odb::mysql::database (argc, argv)); +#endif +} + +#endif // DATABASE_HXX diff --git a/qt/driver.cxx b/qt/driver.cxx new file mode 100644 index 0000000..14f0c0e --- /dev/null +++ b/qt/driver.cxx @@ -0,0 +1,45 @@ +// file : qt/driver.cxx +// author : Constantin Michael +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include +#include + +#include "database.hxx" // create_database + +#include "employee.hxx" +#include "employee-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + employee e; + e.name = "John Doe"; + + QChar* c = e.name.data (); + + while (!c->isNull ()) + { + cout << c->toAscii (); + ++c; + } + + cout << endl; + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/qt/employee.hxx b/qt/employee.hxx new file mode 100644 index 0000000..2fc124d --- /dev/null +++ b/qt/employee.hxx @@ -0,0 +1,15 @@ +// file : qt/employee.hxx +// author : Constantin Michael +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include + +struct employee +{ + QString name; +}; + +#endif // EMPLOYEE_HXX diff --git a/qt/makefile b/qt/makefile new file mode 100644 index 0000000..17e9290 --- /dev/null +++ b/qt/makefile @@ -0,0 +1,131 @@ +# file : qt/makefile +# author : Constantin Michael +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make + +cxx_tun := driver.cxx +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) + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +$(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) + +$(call import,\ + $(scf_root)/import/libodb-$(db_id)/stub.make,\ + l: odb_db.l,cpp-options: odb_db.l.cpp-options) +endif + +ifeq ($(odb_db.l.cpp-options),) +odb_db.l.cpp-options := $(out_base)/.unbuildable +endif + +# Build. +# +$(driver): $(cxx_obj) $(odb_db.l) $(odb_qt.l) $(odb.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(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 + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) \ +--generate-query --generate-schema +$(gen): cpp_options := -I$(out_base) +$(gen): $(odb.l.cpp-options) $(odb_qt.l.cpp-options) $(qt.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): export name := $(name) +$(dist): export odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := README $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) $(call vc9slns,$(name)) $(call vc10slns,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) README database.hxx) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,$(name),$(name)) + $(call meta-vc10projs,$(name),$(name)) + $(call meta-vc9slns,$(name)) + $(call meta-vc10slns,$(name)) + +# Test. +# +$(test): schema := $(src_base)/$(basename $(odb_hdr)).sql +$(test): $(driver) + $(call message,sql $$1,$(dcf_root)/db-driver $$1,$(schema)) + $(call message,test $<,$< --options-file $(dcf_root)/db.options) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9sln.make) +$(call include,$(bld_root)/meta/vc10sln.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + diff --git a/qt/qt-vc10.sln b/qt/qt-vc10.sln new file mode 100644 index 0000000..9a5dc32 --- /dev/null +++ b/qt/qt-vc10.sln @@ -0,0 +1,15 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +__projects__ +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution +__solution_configurations__ + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution +__project_configurations__ + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/qt/qt-vc10.vcxproj b/qt/qt-vc10.vcxproj new file mode 100644 index 0000000..d252a8a --- /dev/null +++ b/qt/qt-vc10.vcxproj @@ -0,0 +1,174 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {__uuid__()} + Win32Proj + __value__(name) + + + + Application + true + Unicode + + + Application + true + Unicode + + + Application + false + true + Unicode + + + Application + false + true + Unicode + + + + + + + + + + + + + + + + + + + true + $(Configuration)\ + driver + + + true + $(Platform)\$(Configuration)\ + driver + + + false + $(Configuration)\ + driver + + + false + $(Platform)\$(Configuration)\ + driver + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database)-d.lib;odb-qt-d.lib;qt-core-d.lib;odb-d.lib;%(AdditionalDependencies) + Console + true + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database)-d.lib;odb-qt-d.lib;qt-core-d.lib;odb-d.lib;%(AdditionalDependencies) + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database).lib;odb-qt.lib;qt-core.lib;odb.lib;%(AdditionalDependencies) + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;__upcase__(database_)__upcase__(__value__(database));%(PreprocessorDefinitions) + 4068;4355;4800;4290;%(DisableSpecificWarnings) + + + odb-__value__(database).lib;odb-qt.lib;qt-core.lib;odb.lib;%(AdditionalDependencies) + Console + true + true + true + + + +__custom_build_entry__( +__path__(odb_header_stem).hxx, +odb __path__(odb_header_stem).hxx, +odb.exe __xml__(__shell_quotes__(m4_patsubst(__value__(odb_options), @database@, __value__(database)))) __path__(odb_header_stem).hxx, +__path__(odb_header_stem)-odb.hxx;__path__(odb_header_stem)-odb.ixx;__path__(odb_header_stem)-odb.cxx) + + +__header_entry__(__path__(odb_header_stem)-odb.hxx) +__header_entry__(__path__(odb_header_stem)-odb.ixx) +__header_entries__(database.hxx) +__header_entries__(extra_headers) + + +__source_entry__(driver.cxx) +__source_entry__(__path__(odb_header_stem)-odb.cxx) +__source_entries__(extra_sources) + + + + + diff --git a/qt/qt-vc10.vcxproj.filters b/qt/qt-vc10.vcxproj.filters new file mode 100644 index 0000000..f754d41 --- /dev/null +++ b/qt/qt-vc10.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + {__uuid__()} + cxx + + + {__uuid__()} + h;hxx;ixx;txx + + + +__header_filter_entry__(__path__(odb_header_stem).hxx) +__header_filter_entry__(__path__(odb_header_stem)-odb.hxx) +__header_filter_entry__(__path__(odb_header_stem)-odb.ixx) +__header_filter_entries__(database.hxx) +__header_filter_entries__(extra_headers) + + +__source_filter_entry__(driver.cxx) +__source_filter_entry__(__path__(odb_header_stem)-odb.cxx) +__source_filter_entries__(extra_sources) + + diff --git a/qt/qt-vc9.sln b/qt/qt-vc9.sln new file mode 100644 index 0000000..2ec9432 --- /dev/null +++ b/qt/qt-vc9.sln @@ -0,0 +1,15 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +__projects__ +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution +__solution_configurations__ + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution +__project_configurations__ + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/qt/qt-vc9.vcproj b/qt/qt-vc9.vcproj new file mode 100644 index 0000000..0e7ed9c --- /dev/null +++ b/qt/qt-vc9.vcproj @@ -0,0 +1,357 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +__source_entry__(driver.cxx) +__source_entry__(__path__(odb_header_stem)-odb.cxx) +__source_entries__(extra_sources) + + +__file_entry_custom_build__( +__path__(odb_header_stem).hxx, +odb __path__(odb_header_stem).hxx, +odb.exe __xml__(__shell_quotes__(m4_patsubst(__value__(odb_options), @database@, __value__(database)))) __path__(odb_header_stem).hxx, +__path__(odb_header_stem)-odb.hxx;__path__(odb_header_stem)-odb.ixx;__path__(odb_header_stem)-odb.cxx) +__file_entry__(__path__(odb_header_stem)-odb.hxx) +__file_entry__(__path__(odb_header_stem)-odb.ixx) +__file_entries__(database.hxx) +__file_entries__(extra_headers) + + + + + -- cgit v1.1