From 87991ad75cf0a33b2503dbb8725c3c6609254961 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 14 Sep 2010 15:44:06 +0200 Subject: Add hello example --- hello/database.hxx | 41 +++++++++++++++++++ hello/driver.cxx | 65 +++++++++++++++++++++++++++++ hello/makefile | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hello/person.hxx | 60 +++++++++++++++++++++++++++ 4 files changed, 283 insertions(+) create mode 100644 hello/database.hxx create mode 100644 hello/driver.cxx create mode 100644 hello/makefile create mode 100644 hello/person.hxx (limited to 'hello') 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 +// copyright : not copyrighted - public domain + +// +// Create concrete database based on the DATABASE_* macros. +// + +#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; + + 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 (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 +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include +#include + +#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 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 query; + typedef odb::result result; + + transaction t (db->begin_transaction ()); + + result r (db->query (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 +# 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 +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include + +#include + +#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 -- cgit v1.1