From 2f17222fd6b2f3cfa251e57daf67ee58d25d6e43 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 23 Sep 2010 09:32:31 +0200 Subject: Add the mapping example It shows how to map between C++ value types and SQL types --- mapping/database.hxx | 41 ++++++++++++++++++ mapping/driver.cxx | 62 +++++++++++++++++++++++++++ mapping/makefile | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ mapping/person.hxx | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ mapping/traits.hxx | 87 +++++++++++++++++++++++++++++++++++++ 5 files changed, 422 insertions(+) create mode 100644 mapping/database.hxx create mode 100644 mapping/driver.cxx create mode 100644 mapping/makefile create mode 100644 mapping/person.hxx create mode 100644 mapping/traits.hxx (limited to 'mapping') diff --git a/mapping/database.hxx b/mapping/database.hxx new file mode 100644 index 0000000..d668b89 --- /dev/null +++ b/mapping/database.hxx @@ -0,0 +1,41 @@ +// file : mapping/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/mapping/driver.cxx b/mapping/driver.cxx new file mode 100644 index 0000000..fd878dc --- /dev/null +++ b/mapping/driver.cxx @@ -0,0 +1,62 @@ +// file : mapping/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)); + + { + person john ("John", "Doe", date (1978, 10, 13), true); + person jane ("Jane", "Doe", date (1975, 11, 23), false); + person joe ("Joe", "Dirt", date (1973, 12, 28), true); + + transaction t (db->begin_transaction ()); + + db->persist (john); + db->persist (jane); + db->persist (joe); + + t.commit (); + } + + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin_transaction ()); + + result r (db->query (query::married && + query::born == date (1978, 10, 13))); + + for (result::iterator i (r.begin ()); i != r.end (); ++i) + { + cout << i->first () << " " << i->last () << endl; + } + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/mapping/makefile b/mapping/makefile new file mode 100644 index 0000000..7b9c72e --- /dev/null +++ b/mapping/makefile @@ -0,0 +1,118 @@ +# file : mapping/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-schema \ +--generate-query --hxx-prologue '\#include "traits.hxx"' +$(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 extra_headers := traits.hxx +$(dist): export odb_header_stem := $(basename $(odb_hdr)) +$(dist): export extra_dist := $(call vc9projs,$(name)) $(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(extra_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/mapping/person.hxx b/mapping/person.hxx new file mode 100644 index 0000000..b091944 --- /dev/null +++ b/mapping/person.hxx @@ -0,0 +1,114 @@ +// file : mapping/person.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef PERSON_HXX +#define PERSON_HXX + +#include +#include + +#include + +// In our database boolean values are stored as strings with valid +// values being "true" and "false". +// +#pragma db value(bool) type("VARCHAR(5) NOT NULL") + +#pragma db value type("DATE NOT NULL") +class date +{ +public: + date (unsigned int year, unsigned int month, unsigned int day) + : year_ (year), month_ (month), day_ (day) + { + } + + unsigned int + year () const + { + return year_; + } + + unsigned int + month () const + { + return month_; + } + + unsigned int + day () const + { + return day_; + } + +private: + unsigned int year_; + unsigned int month_; + unsigned int day_; +}; + +inline std::ostream& +operator<< (std::ostream& os, const date& d) +{ + return os << d.year () << '-' << d.month () << '-' << d.day (); +} + +#pragma db object +class person +{ +public: + person (const std::string& first, + const std::string& last, + const date& born, + bool married) + : first_ (first), last_ (last), born_ (born), married_ (married) + { + } + + const std::string& + first () const + { + return first_; + } + + const std::string& + last () const + { + return last_; + } + + const date& + born () const + { + return born_; + } + + bool + married () const + { + return married_; + } + +public: + unsigned long + id () const + { + return id_; + } + +private: + friend class odb::access; + + person (): born_ (0, 0, 0) {} + + #pragma db id auto + unsigned long id_; + + std::string first_; + std::string last_; + date born_; + bool married_; +}; + +#endif // PERSON_HXX diff --git a/mapping/traits.hxx b/mapping/traits.hxx new file mode 100644 index 0000000..0533360 --- /dev/null +++ b/mapping/traits.hxx @@ -0,0 +1,87 @@ +// file : mapping/traits.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef TRAITS_HXX +#define TRAITS_HXX + +#include + +#include "person.hxx" // date + +// MySQL implementation. +// +#if defined(DATABASE_MYSQL) + +#include + +namespace odb +{ + namespace mysql + { + template <> + class value_traits + { + public: + typedef bool value_type; + typedef bool query_type; + typedef details::buffer image_type; + + static void + set_value (bool& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + v = (!is_null && n == 4 && std::strncmp ("true", b.data (), n) == 0); + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + bool v) + { + is_null = false; + n = v ? 4 : 5; + + if (n > b.capacity ()) + b.capacity (n); + + std::memcpy (b.data (), (v ? "true" : "false"), n); + } + }; + + template <> + class value_traits + { + public: + typedef date value_type; + typedef date query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (date& v, const MYSQL_TIME& i, bool is_null) + { + if (!is_null) + v = date (i.year, i.month, i.day); + else + v = date (0, 0, 0); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const date& v) + { + is_null = false; + i.neg = false; + i.year = v.year (); + i.month = v.month (); + i.day = v.day (); + } + }; + } +} + +#endif // DATABASE_MYSQL + +#endif // TRAITS_HXX -- cgit v1.1