From 510038e4743e7e5983fe1e552f066582449293d0 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 1 Jul 2015 19:20:25 +0200 Subject: C++ type mapping support for data members --- common/as/driver.cxx | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++ common/as/makefile | 118 ++++++++++++++++++++++++++++++ common/as/test.hxx | 159 +++++++++++++++++++++++++++++++++++++++++ common/as/test.std | 0 common/makefile | 1 + 5 files changed, 476 insertions(+) create mode 100644 common/as/driver.cxx create mode 100644 common/as/makefile create mode 100644 common/as/test.hxx create mode 100644 common/as/test.std (limited to 'common') diff --git a/common/as/driver.cxx b/common/as/driver.cxx new file mode 100644 index 0000000..ff65344 --- /dev/null +++ b/common/as/driver.cxx @@ -0,0 +1,198 @@ +// file : common/as/driver.cxx +// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test C++ type mapping (#pragma map type as ...). +// + +#include // std::auto_ptr +#include +#include + +#include +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Test basic type mapping functionality. + // + { + using namespace test1; + + object o1 (true, 123, 234); + o1.m[false] = 123; + o1.v.push_back (o1.ip); + + object o2 (false, 234, 456); + o2.m[true] = 234; + o2.v.push_back (o2.ip); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (o1.id)); + auto_ptr p2 (db->load (o2.id)); + t.commit (); + + assert (*p1 == o1); + assert (*p2 == o2); + } + + o1.b = false; + o1.ip.first++; + o1.ip.second--; + o1.m[false]++; + o1.m[true] = 234; + o1.v.back () = o1.ip; + + o2.b = true; + o2.ip.first--; + o2.ip.second++; + o2.m[true]--; + o2.m[false] = 345; + o2.v.push_back (o2.ip); + + { + transaction t (db->begin ()); + db->update (o1); + db->update (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (o1.id)); + auto_ptr p2 (db->load (o2.id)); + t.commit (); + + assert (*p1 == o1); + assert (*p2 == o2); + } + } + + // Test wrapped simple type mapping. + // + { + using namespace test2; + + object o1; + object o2; + o2.b = true; + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (o1.id)); + auto_ptr p2 (db->load (o2.id)); + t.commit (); + + assert (p1->b == o1.b); + assert (p2->b == o2.b); + } + + o1.b = false; + o2.b.reset (); + + { + transaction t (db->begin ()); + db->update (o1); + db->update (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (o1.id)); + auto_ptr p2 (db->load (o2.id)); + t.commit (); + + assert (p1->b == o1.b); + assert (p2->b == o2.b); + } + } + + // Test wrapped composite type mapping. + // + { + using namespace test3; + + object o1; + o1.ip = intp (0, 0); // NULL + + object o2; + o2.np = intp (123, 234); + o1.ip = intp (234, 123); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (o1.id)); + auto_ptr p2 (db->load (o2.id)); + t.commit (); + + assert (p1->np == o1.np && p1->ip == o1.ip); + assert (p2->np == o2.np && p2->ip == o2.ip); + } + + o1.np = intp (234, 456); + o1.ip = intp (456, 234); + + o2.np.reset (); + o2.ip = intp (0, 0); // NULL + + { + transaction t (db->begin ()); + db->update (o1); + db->update (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p1 (db->load (o1.id)); + auto_ptr p2 (db->load (o2.id)); + t.commit (); + + assert (p1->np == o1.np && p1->ip == o1.ip); + assert (p2->np == o2.np && p2->ip == o2.ip); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/as/makefile b/common/as/makefile new file mode 100644 index 0000000..6a1b135 --- /dev/null +++ b/common/as/makefile @@ -0,0 +1,118 @@ +# file : common/as/makefile +# copyright : Copyright (c) 2009-2015 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 := test.hxx +genf := $(call odb-gen,$(odb_hdr)) +gen := $(addprefix $(out_base)/,$(genf)) +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --generate-schema --generate-query \ +--generate-session --table-prefix as_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +ifneq ($(db_id),common) +$(gen): odb_options += --database $(db_id) +else +$(gen): odb_options += --multi-database dynamic +endif + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) + +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc8projs,$(name)) \ +$(call vc9projs,$(name)) $(call vc10projs,$(name)) $(call vc11projs,$(name)) \ +$(call vc12projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc8projs,../template/template,$(name)) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + $(call meta-vc11projs,../template/template,$(name)) + $(call meta-vc12projs,../template/template,$(name)) + +# Test. +# +ifneq ($(db_id),common) +$(eval $(call test-rule)) +else +$(foreach d,$(databases),$(eval $(call test-rule,$d))) +endif + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addsuffix .hxx.clean,$(filter %.cxx,$(gen))) + $(call message,,rm -f $(out_base)/test.out) + +# 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/vc8proj.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.make) +$(call include,$(bld_root)/meta/vc12proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/common/as/test.hxx b/common/as/test.hxx new file mode 100644 index 0000000..4839f18 --- /dev/null +++ b/common/as/test.hxx @@ -0,0 +1,159 @@ +// file : common/as/test.hxx +// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include +#include +#include // pair + +#include +#include + +// Test basic type mapping functionality. +// +#pragma db namespace table("t1_") +namespace test1 +{ + typedef std::pair intp; + + #pragma db value + struct comp + { + comp () {} + comp (int n1_, int n2_): n1 (n1_), n2 (n2_) {} + + int n1; + int n2; + }; + + #pragma db map type(intp) as(comp) \ + to(test1::comp ((?).first, (?).second)) \ + from(test1::intp ((?).n1, (?).n2)) + + #pragma db object + struct object + { + // Class-scope mapping. + // + #pragma db map type(bool) as(std::string) \ + to((?) ? "true" : "false") \ + from((?) == "true") + + #pragma db id auto + unsigned long id; + + bool b; + intp ip; + + #pragma db transient + std::map m; + + #pragma db transient + std::vector v; + + object () {} + object (bool b_, int n1, int n2): b (b_), ip (n1, n2) {} + }; + + inline bool + operator== (const object& x, const object y) + { + return x.b == y.b && x.ip == y.ip /*&& x.m == y.m && x.v == y.v*/; + } +} + +// Test wrapped simple type mapping. +// +#pragma db namespace table("t2_") +namespace test2 +{ + #pragma db map type(bool) as(std::string) \ + to((?) ? "true" : "false") \ + from((?) == "true") + + typedef odb::nullable null_bool; + typedef odb::nullable null_string; + + /* + #pragma db map type(null_bool) as(null_string) \ + to((?) \ + ? test2::null_string (*(?) ? "true" : "false") \ + : test2::null_string ()) \ + from((?) \ + ? test2::null_bool (*(?) == "true") \ + : test2::null_bool ()) + */ + + #pragma db map type(null_bool) as(std::string) \ + to((?) ? (*(?) ? "true" : "false") : "null") \ + from((?) != "null" \ + ? test2::null_bool ((?) == "true") \ + : test2::null_bool ()) + + #pragma db object + struct object + { + #pragma db id auto + unsigned long id; + + odb::nullable b; + }; +} + +// Test wrapped simple type mapping. +// +#pragma db namespace table("t3_") +namespace test3 +{ + typedef std::pair intp; + + #pragma db value + struct comp + { + comp () {} + comp (int n1_, int n2_): n1 (n1_), n2 (n2_) {} + + int n1; + int n2; + }; + + typedef odb::nullable null_intp; + typedef odb::nullable null_comp; + + #pragma db map type(null_intp) as(null_comp) \ + to((?) \ + ? test3::null_comp (test3::comp ((?)->first, (?)->second)) \ + : test3::null_comp ()) \ + from((?) \ + ? test3::null_intp (test3::intp ((?)->n1, (?)->n2)) \ + : test3::null_intp ()) + + // Map int pair with both members equal 0 to NULL comp. + // + #pragma db map type(intp) as(null_comp) \ + to((?).first != 0 || (?).second != 0 \ + ? test3::null_comp (test3::comp ((?).first, (?).second)) \ + : test3::null_comp ()) \ + from((?) \ + ? test3::intp (test3::intp ((?)->n1, (?)->n2)) \ + : test3::intp (0, 0)) + + #pragma db object + struct object + { + #pragma db id auto + unsigned long id; + + odb::nullable np; + intp ip; + }; +} + +//@@ Test wrapped id and version. +//@@ Containers. + +#endif // TEST_HXX diff --git a/common/as/test.std b/common/as/test.std new file mode 100644 index 0000000..e69de29 diff --git a/common/makefile b/common/makefile index d7d134e..1e57ed4 100644 --- a/common/makefile +++ b/common/makefile @@ -6,6 +6,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make tests := \ access \ +as \ auto \ blob \ bulk \ -- cgit v1.1