diff options
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | hello/database.hxx | 41 | ||||
-rw-r--r-- | hello/driver.cxx | 65 | ||||
-rw-r--r-- | hello/makefile | 117 | ||||
-rw-r--r-- | hello/person.hxx | 60 | ||||
-rw-r--r-- | makefile | 5 |
6 files changed, 286 insertions, 4 deletions
diff --git a/configure.ac b/configure.ac index a6d58d8..8f66d99 100644 --- a/configure.ac +++ b/configure.ac @@ -7,7 +7,7 @@ AC_PREREQ(2.60) AC_INIT([odb-examples], [__value__(version)], [odb-users@codesynthesis.com]) AC_CONFIG_AUX_DIR([config]) AC_CONFIG_MACRO_DIR([m4]) -AC_CONFIG_SRCDIR([template/driver.cxx]) +AC_CONFIG_SRCDIR([hello/driver.cxx]) AM_INIT_AUTOMAKE([-Wall -Werror foreign nostdinc subdir-objects dist-bzip2 dist-zip tar-ustar]) diff --git a/hello/database.hxx b/hello/database.hxx new file mode 100644 index 0000000..624bf1b --- /dev/null +++ b/hello/database.hxx @@ -0,0 +1,41 @@ +// file : hello/database.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +// +// Create concrete database based on the DATABASE_* macros. +// + +#include <string> +#include <memory> // std::auto_ptr +#include <cstdlib> // std::exit +#include <iostream> + +#include <odb/database.hxx> + +#if defined(DATABASE_MYSQL) +# include <odb/mysql/database.hxx> +#endif + +inline std::auto_ptr<odb::database> +create_database (int& argc, char* argv[]) +{ + using namespace std; + using namespace odb; + + if (argc > 1 && argv[1] == string ("--help")) + { + cerr << "Usage: " << argv[0] << " [options]" << endl + << "Options:" << endl; + +#if defined(DATABASE_MYSQL) + mysql::database::print_usage (cerr); +#endif + + exit (0); + } + +#if defined(DATABASE_MYSQL) + return auto_ptr<database> (new mysql::database (argc, argv)); +#endif +} diff --git a/hello/driver.cxx b/hello/driver.cxx new file mode 100644 index 0000000..2c3edff --- /dev/null +++ b/hello/driver.cxx @@ -0,0 +1,65 @@ +// file : hello/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include "database.hxx" // create_database + +#include "person.hxx" +#include "person-odb.hxx" + +using namespace std; +using namespace odb; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // Create a few persistent person objects. + // + { + person john_doe ("John", "Doe", 29); + person jane_doe ("Jane", "Doe", 28); + person joe_dirt ("Joe", "Dirt", 31); + + transaction t (db->begin_transaction ()); + + db->persist (john_doe); + db->persist (jane_doe); + db->persist (joe_dirt); + + t.commit (); + } + + // Say hello to those under 30. + // + { + typedef odb::query<person> query; + typedef odb::result<person> result; + + transaction t (db->begin_transaction ()); + + result r (db->query<person> (query::age < 30)); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << "Hello, " << i->first () << " " << i->last () << "!" << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/hello/makefile b/hello/makefile new file mode 100644 index 0000000..8192aae --- /dev/null +++ b/hello/makefile @@ -0,0 +1,117 @@ +# file : hello/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2009-2010 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 := person.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) + +ifdef db_id +$(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.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) +$(cxx_obj) $(cxx_od): $(odb.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) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(notdir $(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 := $(call vc9projs,$(name)) $(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) database.hxx) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(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/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/hello/person.hxx b/hello/person.hxx new file mode 100644 index 0000000..4730ff0 --- /dev/null +++ b/hello/person.hxx @@ -0,0 +1,60 @@ +// file : hello/person.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object +struct person +{ + person (const std::string& first, + const std::string& last, + unsigned short age) + : first_ (first), last_ (last), age_ (age) + { + } + + const std::string& + first () const + { + return first_; + } + + const std::string& + last () const + { + return last_; + } + + unsigned short + age () const + { + return age_; + } + +public: + unsigned long + id () const + { + return id_; + } + +private: + friend class odb::access; + + person () {} + + #pragma db id auto + unsigned long id_; + + std::string first_; + std::string last_; + unsigned short age_; +}; + +#endif // PERSON_HXX @@ -5,9 +5,8 @@ include $(dir $(lastword $(MAKEFILE_LIST)))build/bootstrap.make -dirs := template -dist_dirs := $(dirs) -#dist_dirs := $(filterout $(dirs),template) +dirs := hello template +dist_dirs := $(filter-out template,$(dirs)) default := $(out_base)/ dist := $(out_base)/.dist |