From 43fa55c1b8e389838c83be933bb30a2caaf7468d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 5 Feb 2013 15:50:08 +0200 Subject: Add support for change-tracking containers ODB now supports "smart" ordered containers. Such containers get extra functions for updating and deleting individual elements. Based on this functionality implement two change-tracking containers: odb::vector (equivalent to std::vector) and QOdbList (equivalent to QList). New tests: common/container/change-tracking and qt/common/container/change- tracking. --- common/container/basics/driver.cxx | 516 +++++++++++++++++++++ common/container/basics/makefile | 115 +++++ common/container/basics/test.hxx | 268 +++++++++++ common/container/basics/test.std | 0 common/container/change-tracking/driver.cxx | 694 ++++++++++++++++++++++++++++ common/container/change-tracking/makefile | 115 +++++ common/container/change-tracking/test.hxx | 81 ++++ common/container/change-tracking/test.std | 0 common/container/driver.cxx | 516 --------------------- common/container/makefile | 115 ----- common/container/test.hxx | 268 ----------- common/container/test.std | 0 common/makefile | 85 ++-- 13 files changed, 1832 insertions(+), 941 deletions(-) create mode 100644 common/container/basics/driver.cxx create mode 100644 common/container/basics/makefile create mode 100644 common/container/basics/test.hxx create mode 100644 common/container/basics/test.std create mode 100644 common/container/change-tracking/driver.cxx create mode 100644 common/container/change-tracking/makefile create mode 100644 common/container/change-tracking/test.hxx create mode 100644 common/container/change-tracking/test.std delete mode 100644 common/container/driver.cxx delete mode 100644 common/container/makefile delete mode 100644 common/container/test.hxx delete mode 100644 common/container/test.std (limited to 'common') diff --git a/common/container/basics/driver.cxx b/common/container/basics/driver.cxx new file mode 100644 index 0000000..b50ab5a --- /dev/null +++ b/common/container/basics/driver.cxx @@ -0,0 +1,516 @@ +// file : common/container/basics/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test basic container persistence. +// + +#include // std::auto_ptr +#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)); + + for (unsigned short i (0); i < 2; ++i) + { + object empty ("empty"), med ("medium"), full ("full"); + + // + // empty + // + + empty.num = 0; + empty.str = ""; + +#ifdef HAVE_CXX11 + // array + // + empty.na[0] = 123; + empty.na[1] = 234; + empty.na[2] = 345; + + empty.sa[0] = "aaa"; + empty.sa[1] = "bbbb"; + empty.sa[2] = "ccccc"; + + empty.ca[0] = comp (123, "aaa"); + empty.ca[1] = comp (234, "bbbb"); + empty.ca[2] = comp (345, "ccccc"); +#endif + + + // + // med + // + + med.num = 999; + med.str = "xxx"; + + // vector + // + med.nv.push_back (123); + med.nv.push_back (234); + + med.sv.push_back ("aaa"); + med.sv.push_back ("bbbb"); + + med.cv.push_back (comp (123, "aaa")); + med.cv.push_back (comp (234, "bbbb")); + + med.uv.push_back (123); + med.uv.push_back (234); + + // list + // + med.sl.push_back ("aaa"); + med.sl.push_back ("bbbb"); + + // set + // + med.ns.insert (123); + med.ns.insert (234); + + med.ss.insert ("aaa"); + med.ss.insert ("bbbb"); + + med.cs.insert (comp (123, "aaa")); + med.cs.insert (comp (234, "bbbb")); + + // map + // + med.nsm[123] = "aaa"; + med.nsm[234] = "bbbb"; + + med.snm["aaa"] = 123; + med.snm["bbbb"] = 234; + + med.ncm[123] = comp (123, "aaa"); + med.ncm[234] = comp (234, "bbbb"); + + med.csm[comp (123, "aaa")] = "aaa"; + med.csm[comp (234, "bbbb")] = "bbbb"; + +#ifdef HAVE_CXX11 + // array + // + med.na[0] = 123; + med.na[1] = 234; + med.na[2] = 345; + + med.sa[0] = "aaa"; + med.sa[1] = "bbbb"; + med.sa[2] = "ccccc"; + + med.ca[0] = comp (123, "aaa"); + med.ca[1] = comp (234, "bbbb"); + med.ca[2] = comp (345, "ccccc"); + + // forward_list + // + med.nfl.push_front (234); + med.nfl.push_front (123); + + med.sfl.push_front ("bbbb"); + med.sfl.push_front ("aaa"); + + med.cfl.push_front (comp (234, "bbbb")); + med.cfl.push_front (comp (123, "aaa")); + + // unordered_set + // + med.nus.insert (123); + med.nus.insert (234); + + med.sus.insert ("aaa"); + med.sus.insert ("bbbb"); + + med.cus.insert (comp (123, "aaa")); + med.cus.insert (comp (234, "bbbb")); + + // unordered_map + // + med.nsum[123] = "aaa"; + med.nsum[234] = "bbbb"; + + med.snum["aaa"] = 123; + med.snum["bbbb"] = 234; + + med.ncum[123] = comp (123, "aaa"); + med.ncum[234] = comp (234, "bbbb"); + + med.csum[comp (123, "aaa")] = "aaa"; + med.csum[comp (234, "bbbb")] = "bbbb"; +#endif + + // + // full + // + + full.num = 9999; + full.str = "xxxx"; + + // vector + // + full.nv.push_back (1234); + full.nv.push_back (2345); + full.nv.push_back (3456); + + full.sv.push_back ("aaaa"); + full.sv.push_back ("bbbbb"); + full.sv.push_back ("cccccc"); + + full.cv.push_back (comp (1234, "aaaa")); + full.cv.push_back (comp (2345, "bbbbb")); + full.cv.push_back (comp (3456, "cccccc")); + + full.uv.push_back (1234); + full.uv.push_back (2345); + full.uv.push_back (3456); + + // list + // + full.sl.push_back ("aaaa"); + full.sl.push_back ("bbbbb"); + full.sl.push_back ("cccccc"); + + // set + // + full.ns.insert (1234); + full.ns.insert (2345); + full.ns.insert (3456); + + full.ss.insert ("aaaa"); + full.ss.insert ("bbbbb"); + full.ss.insert ("cccccc"); + + full.cs.insert (comp (1234, "aaaa")); + full.cs.insert (comp (2345, "bbbbb")); + full.cs.insert (comp (3456, "cccccc")); + + // map + // + full.nsm[1234] = "aaaa"; + full.nsm[2345] = "bbbbb"; + full.nsm[3456] = "cccccc"; + + full.snm["aaaa"] = 1234; + full.snm["bbbbb"] = 2345; + full.snm["cccccc"] = 3456; + + full.ncm[1234] = comp (1234, "aaaa"); + full.ncm[2345] = comp (2345, "bbbbb"); + full.ncm[3456] = comp (3456, "cccccc"); + + full.csm[comp (1234, "aaaa")] = "aaaa"; + full.csm[comp (2345, "bbbbb")] = "bbbbb"; + full.csm[comp (3456, "cccccc")] = "cccccc"; + +#ifdef HAVE_CXX11 + // array + // + full.na[0] = 123; + full.na[1] = 234; + full.na[2] = 345; + + full.sa[0] = "aaa"; + full.sa[1] = "bbbb"; + full.sa[2] = "ccccc"; + + full.ca[0] = comp (123, "aaa"); + full.ca[1] = comp (234, "bbbb"); + full.ca[2] = comp (345, "ccccc"); + + // forward_list + // + full.nfl.push_front (345); + full.nfl.push_front (234); + full.nfl.push_front (123); + + full.sfl.push_front ("ccccc"); + full.sfl.push_front ("bbbb"); + full.sfl.push_front ("aaa"); + + full.cfl.push_front (comp (345, "ccccc")); + full.cfl.push_front (comp (234, "bbbb")); + full.cfl.push_front (comp (123, "aaa")); + + // unordered_set + // + full.nus.insert (1234); + full.nus.insert (2345); + full.nus.insert (3456); + + full.sus.insert ("aaaa"); + full.sus.insert ("bbbbb"); + full.sus.insert ("cccccc"); + + full.cus.insert (comp (1234, "aaaa")); + full.cus.insert (comp (2345, "bbbbb")); + full.cus.insert (comp (3456, "cccccc")); + + // unordered_map + // + full.nsum[1234] = "aaaa"; + full.nsum[2345] = "bbbbb"; + full.nsum[3456] = "cccccc"; + + full.snum["aaaa"] = 1234; + full.snum["bbbbb"] = 2345; + full.snum["cccccc"] = 3456; + + full.ncum[1234] = comp (1234, "aaaa"); + full.ncum[2345] = comp (2345, "bbbbb"); + full.ncum[3456] = comp (3456, "cccccc"); + + full.csum[comp (1234, "aaaa")] = "aaaa"; + full.csum[comp (2345, "bbbbb")] = "bbbbb"; + full.csum[comp (3456, "cccccc")] = "cccccc"; +#endif + + // persist + // + { + transaction t (db->begin ()); + db->persist (empty); + db->persist (med); + db->persist (full); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + auto_ptr e (db->load ("empty")); + auto_ptr m (db->load ("medium")); + auto_ptr f (db->load ("full")); + t.commit (); + + assert (empty == *e); + assert (med == *m); + assert (full == *f); + } + + // + // empty + // + + empty.num = 99; + empty.str = "xx"; + + empty.nv.push_back (12); + empty.sv.push_back ("aa"); + empty.cv.push_back (comp (12, "aa")); + empty.uv.push_back (12); + empty.sl.push_back ("aa"); + + empty.ns.insert (12); + empty.ss.insert ("aa"); + empty.cs.insert (comp (12, "aa")); + + empty.nsm[12] = "aa"; + empty.snm["aa"] = 12; + empty.ncm[12] = comp (12, "aa"); + empty.csm[comp (12, "aa")] = "aa"; + +#ifdef HAVE_CXX11 + empty.nfl.push_front (12); + empty.sfl.push_front ("aa"); + empty.cfl.push_front (comp (12, "aa")); + + empty.nus.insert (12); + empty.sus.insert ("aa"); + empty.cus.insert (comp (12, "aa")); + + empty.nsum[12] = "aa"; + empty.snum["aa"] = 12; + empty.ncum[12] = comp (12, "aa"); + empty.csum[comp (12, "aa")] = "aa"; +#endif + + // + // med + // + + med.num = 0; + med.str = ""; + + med.nv.clear (); + med.sv.clear (); + med.cv.clear (); + med.uv.clear (); + + med.sl.clear (); + + med.ns.clear (); + med.ss.clear (); + med.cs.clear (); + + med.nsm.clear (); + med.snm.clear (); + med.ncm.clear (); + med.csm.clear (); + +#ifdef HAVE_CXX11 + med.nfl.clear (); + med.sfl.clear (); + med.cfl.clear (); + + med.nus.clear (); + med.sus.clear (); + med.cus.clear (); + + med.nsum.clear (); + med.snum.clear (); + med.ncum.clear (); + med.csum.clear (); +#endif + + // + // full + // + + full.num++; + full.str += "x"; + + // vector + // + full.nv.back ()++; + full.nv.push_back (4567); + + full.sv.back () += "c"; + full.sv.push_back ("ddddddd"); + + full.cv.back ().num++; + full.cv.back ().str += "c"; + full.cv.push_back (comp (4567, "ddddddd")); + + full.uv.back ()++; + full.uv.push_back (4567); + + // list + // + full.sl.back () += "c"; + full.sl.push_back ("ddddddd"); + + // set + // + full.ns.insert (4567); + full.ss.insert ("ddddddd"); + full.cs.insert (comp (4567, "ddddddd")); + + // map + // + full.nsm[3456] += 'c'; + full.nsm[4567] = "ddddddd"; + + full.snm["cccccc"]++; + full.snm["ddddddd"] = 4567; + + full.ncm[3456].num++; + full.ncm[3456].str += 'c'; + full.ncm[4567] = comp (4567, "ddddddd"); + + full.csm[comp (3456, "cccccc")] += "c"; + full.csm[comp (4567, "ddddddd")] = "ddddddd"; + +#ifdef HAVE_CXX11 + // array + // + full.na[0]++; + full.sa[0] += 'a'; + full.ca[0].num++; + full.ca[0].str += 'a'; + + // forward_list + // + full.nfl.front ()++; + full.nfl.push_front (4567); + + full.sfl.front () += 'a'; + full.sfl.push_front ("ddddddd"); + + full.cfl.front ().num++; + full.cfl.front ().str += 'a'; + full.cfl.push_front (comp (4567, "ddddddd")); + + // unordered_set + // + full.nus.insert (4567); + full.sus.insert ("ddddddd1"); // 1 is to preserve order in VC++ 10. + full.cus.insert (comp (4567, "ddddddd1")); + + // unordered_map + // + full.nsum[3456] += 'c'; + full.nsum[4567] = "ddddddd"; + + full.snum["cccccc"]++; + full.snum["ddddddd1"] = 4567; + + full.ncum[3456].num++; + full.ncum[3456].str += 'c'; + full.ncum[4567] = comp (4567, "ddddddd"); + + full.csum[comp (3456, "cccccc")] += "c"; + full.csum[comp (4567, "ddddddd1")] = "ddddddd"; +#endif + + // update + // + { + transaction t (db->begin ()); + db->update (empty); + db->update (med); + db->update (full); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + auto_ptr e (db->load ("empty")); + auto_ptr m (db->load ("medium")); + auto_ptr f (db->load ("full")); + t.commit (); + + assert (empty == *e); + assert (med == *m); + assert (full == *f); + } + + // erase + // + if (i == 0) + { + transaction t (db->begin ()); + db->erase ("empty"); + db->erase ("medium"); + db->erase ("full"); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/container/basics/makefile b/common/container/basics/makefile new file mode 100644 index 0000000..58feb3b --- /dev/null +++ b/common/container/basics/makefile @@ -0,0 +1,115 @@ +# file : common/container/basics/makefile +# copyright : Copyright (c) 2009-2012 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 +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.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 + +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) + +# 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) + +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 \ +--table-prefix t_cont_bs_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(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): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) $(call vc11projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + $(call meta-vc11projs,../../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# 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)) + $(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/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.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/container/basics/test.hxx b/common/container/basics/test.hxx new file mode 100644 index 0000000..e88edd9 --- /dev/null +++ b/common/container/basics/test.hxx @@ -0,0 +1,268 @@ +// file : common/container/basics/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include // HAVE_CXX11 + +#include +#include +#include +#include +#include + +#ifdef HAVE_CXX11 +# include +# include +# include +# include +#endif + +#include + +#pragma db value +struct comp +{ + comp () {} + comp (int n, const std::string& s) : num (n), str (s) {} + + #pragma db column("number") + int num; + std::string str; +}; + +inline bool +operator== (const comp& x, const comp& y) +{ + return x.num == y.num && x.str == y.str; +} + +inline bool +operator!= (const comp& x, const comp& y) +{ + return !(x == y); +} + +inline bool +operator< (const comp& x, const comp& y) +{ + return x.num != y.num ? x.num < y.num : x.str < y.str; +} + +typedef std::list str_list; + +typedef std::vector num_vector; +typedef std::vector str_vector; + +typedef std::set num_set; +typedef std::set str_set; +typedef std::set comp_set; + +typedef std::map num_str_map; +typedef std::map str_num_map; +typedef std::map num_comp_map; +typedef std::map comp_str_map; + +#ifdef HAVE_CXX11 +struct comp_hash +{ + std::size_t + operator() (comp const& x) const {return nh (x.num) + sh (x.str);} + + std::hash nh; + std::hash sh; +}; + +typedef std::array num_array; +typedef std::array str_array; +typedef std::array comp_array; + +typedef std::forward_list num_flist; +typedef std::forward_list str_flist; +typedef std::forward_list comp_flist; + +typedef std::unordered_set num_uset; +typedef std::unordered_set str_uset; +typedef std::unordered_set comp_uset; + +typedef std::unordered_map num_str_umap; +typedef std::unordered_map str_num_umap; +typedef std::unordered_map num_comp_umap; +typedef std::unordered_map comp_str_umap; +#endif + +#pragma db value +struct cont_comp1 +{ + // This composite value does not have any columns. + // + num_vector sv; // Have the name "conflic" with the one in the object. +}; + +#pragma db value +struct cont_comp2 +{ + cont_comp2 (): num (777), str ("ggg") {} + + int num; + str_list sl; + std::string str; +}; + +#pragma db object +struct object +{ + object (): nv (comp1_.sv), sl (comp2_.sl) {} + object (const std::string& id) : id_ (id), nv (comp1_.sv), sl (comp2_.sl) {} + + #pragma db id + std::string id_; + + int num; + + cont_comp1 comp1_; + cont_comp2 comp2_; + + // vector + // + #pragma db transient + num_vector& nv; + + #pragma db table("object_strings") id_column ("obj_id") + str_vector sv; + + #pragma db value_column("") + std::vector cv; + + #pragma db unordered + num_vector uv; + + // list + // + #pragma db transient + str_list& sl; + + // set + // + num_set ns; + str_set ss; + comp_set cs; + + // map + // + num_str_map nsm; + str_num_map snm; + num_comp_map ncm; + comp_str_map csm; + +#ifdef HAVE_CXX11 + // array + // + num_array na; + str_array sa; + comp_array ca; + + // forward_list + // + num_flist nfl; + str_flist sfl; + comp_flist cfl; + + // unordered_set + // + num_uset nus; + str_uset sus; + comp_uset cus; + + // unordered_map + // + num_str_umap nsum; + str_num_umap snum; + num_comp_umap ncum; + comp_str_umap csum; +#else + // Dummy containers to get the equivalent DROP TABLE statements. + // + num_vector na; + num_vector sa; + num_vector ca; + + num_vector nfl; + num_vector sfl; + num_vector cfl; + + num_set nus; + str_set sus; + comp_set cus; + + num_str_map nsum; + str_num_map snum; + num_comp_map ncum; + comp_str_map csum; +#endif + + std::string str; +}; + +inline bool +operator== (const object& x, const object& y) +{ + if (x.uv.size () != y.uv.size ()) + return false; + + int xs (0), ys (0); + + for (num_vector::size_type i (0); i < x.uv.size (); ++i) + { + xs += x.uv[i]; + ys += y.uv[i]; + } + + return + x.id_ == y.id_ && + x.num == y.num && + + x.comp2_.num == y.comp2_.num && + x.comp2_.str == y.comp2_.str && + + x.nv == y.nv && + x.sv == y.sv && + x.cv == y.cv && + xs == ys && + + x.sl == y.sl && + + x.ns == y.ns && + x.ss == y.ss && + x.cs == y.cs && + + x.nsm == y.nsm && + x.snm == y.snm && + x.ncm == y.ncm && + x.csm == y.csm && + +#ifdef HAVE_CXX11 + x.na == y.na && + x.sa == y.sa && + x.ca == y.ca && + + x.nfl == y.nfl && + x.sfl == y.sfl && + x.cfl == y.cfl && + + x.nus == y.nus && + x.sus == y.sus && + x.cus == y.cus && + + x.nsum == y.nsum && + x.snum == y.snum && + x.ncum == y.ncum && + x.csum == y.csum && +#endif + + x.str == y.str; +} + +#endif // TEST_HXX diff --git a/common/container/basics/test.std b/common/container/basics/test.std new file mode 100644 index 0000000..e69de29 diff --git a/common/container/change-tracking/driver.cxx b/common/container/change-tracking/driver.cxx new file mode 100644 index 0000000..98d711c --- /dev/null +++ b/common/container/change-tracking/driver.cxx @@ -0,0 +1,694 @@ +// file : common/container/change-tracking/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test change-tracking containers. +// + +#include // HAVE_CXX11 + +#include // std::auto_ptr +#include +#include + +#ifdef HAVE_CXX11 +# include // std::move +#endif + +#include +#include +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +struct counting_tracer: odb::tracer +{ + void + reset (transaction& tr) {u = i = d = s = t = 0; tr.tracer (*this);} + + virtual void + execute (odb::connection&, const char* stmt) + { + string p (stmt, 6); + if (p == "UPDATE") + u++; + else if (p == "INSERT") + i++; + else if (p == "DELETE") + d++; + else if (p == "SELECT") + s++; + t++; + } + + size_t u, i, d, s, t; +}; + +static counting_tracer tr; + +// Compilation test: instantiate all the functions. In C++11 mode only +// do this if we have a fairly conforming compiler that implements the +// complete std::vector interface. +// + +#if defined (HAVE_CXX11) +#if defined (__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 7 +struct item {}; +template class odb::vector; +template class odb::vector_iterator, + std::vector::iterator>; +template class odb::vector_iterator, + std::vector::reverse_iterator>; +#endif +#endif + +void +f (const std::vector&) {} + +int +main (int argc, char* argv[]) +{ + try + { + // Test extended interface. + // + { + typedef odb::vector vector; + + vector ov; + std::vector sv; + f (ov); // Implicit conversion to std::vector. + vector ov1 (sv); // Initialization from std::vector. + ov = sv; // Assignement from std::vector. + + // Container comparison. + // + if (ov != ov1 || + ov != sv || + sv != ov1) + ov.clear (); + + // Iterator comparison/conversion. + // + vector::const_iterator i (ov.begin ()); + if (i != ov.end ()) + i = ov.end (); + + vector::const_reverse_iterator j (ov.rbegin ()); + if (j != ov.rend ()) + j = ov.rend (); + } + + auto_ptr db (create_database (argc, argv)); + + // Test traits logic. + // + { + object o ("1"); + o.i = 123; + o.s.push_back ("a"); + + assert (!o.s._tracking ()); + + // persist + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + assert (o.s._tracking ()); + + // load + // + { + transaction t (db->begin ()); + auto_ptr p (db->load ("1")); + assert (p->s._tracking ()); + t.commit (); + } + + // update + // + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + assert (o.s._tracking ()); + + // erase + // + { + transaction t (db->begin ()); + db->erase (o); + t.commit (); + } + + assert (!o.s._tracking ()); + } + + // Test change tracking. + // + object o ("1"); + o.i = 123; + o.s.push_back ("a"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // push_back/pop_back + // + { + o.s.push_back ("b"); // insert + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.i == 1 && tr.t == 2); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.pop_back (); + o.s.push_back ("c"); // update + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 2 && tr.t == 2); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.pop_back (); + for (int i (0); i != 1024; ++i) + o.s.push_back ("x"); // realloc + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 2 && tr.i == 1023 && tr.t == 1025); + assert (*db->load ("1") == o); + t.commit (); + } + + { + for (int i (0); i != 1024; ++i) + o.s.pop_back (); // delete + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.d == 1 && tr.t == 2); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.push_back ("b"); + o.s.pop_back (); // no-op + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.t == 1); + assert (*db->load ("1") == o); + t.commit (); + } + + // insert + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.insert (o.s.begin (), "a1"); // insert front + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.i == 1 && tr.t == 4); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.insert (o.s.begin () + 1, "a2"); // insert middle + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.i == 1 && tr.t == 4); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.insert (o.s.end (), "b1"); // insert back + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.i == 1 && tr.t == 2); + assert (*db->load ("1") == o); + t.commit (); + } + + // erase + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + o.s.push_back ("d"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.erase (o.s.begin ()); // erase front + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 4 && tr.d == 1 && tr.t == 5); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.erase (o.s.begin () + 1); // erase middle + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 2 && tr.d == 1 && tr.t == 3); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.erase (o.s.end () - 1); // erase back + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.d == 1 && tr.t == 2); + assert (*db->load ("1") == o); + t.commit (); + } + + // modify + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + o.s.push_back ("d"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.modify (1) += 'b'; + o.s.modify_at (2) += 'c'; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.t == 3); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.modify_front () += 'a'; + o.s.modify_back () += 'd'; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.t == 3); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.begin ().modify () += 'a'; + o.s.rbegin ().modify () += 'c'; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.t == 3); + assert (*db->load ("1") == o); + t.commit (); + } + + { + (o.s.rbegin () + 1).modify (1) += 'a'; + (o.s.rbegin () + 1).modify (-1) += 'c'; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.t == 3); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.mbegin (); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 5 && tr.t == 5); + assert (*db->load ("1") == o); + t.commit (); + } + + // clear + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.clear (); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.d == 1 && tr.t == 2); + assert (*db->load ("1") == o); + t.commit (); + } + + // assign + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.assign (4, "x"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 4 && tr.i == 1 && tr.t == 5); + assert (*db->load ("1") == o); + t.commit (); + } + + // resize + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.pop_back (); + o.s.resize (4, "x"); // expand + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 2 && tr.i == 1 && tr.t == 3); + assert (*db->load ("1") == o); + t.commit (); + } + + { + o.s.push_back ("y"); + o.s.resize (3); // shrink + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.d == 1 && tr.t == 2); + assert (*db->load ("1") == o); + t.commit (); + } + + // Transaction rollback. + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load ("1") == o); + t.commit (); + } + + { + { + o.s.push_back ("d"); + + transaction t (db->begin ()); + db->update (o); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.i == 4 && tr.d == 1 && tr.t == 6); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } + + // Armed copy. + // + { + auto_ptr c; + + { + o.s.pop_back (); + + transaction t (db->begin ()); + db->update (o); + c.reset (new object (o)); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.i == 3 && tr.d == 1 && tr.t == 5); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } + + // Armed swap. + // + { + object c (o); + + { + o.s.push_back ("d"); + + transaction t (db->begin ()); + db->update (o); + assert (o.s._tracking () && !c.s._tracking ()); + c.s.swap (o.s); + assert (!o.s._tracking () && c.s._tracking ()); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.i == 4 && tr.d == 1 && tr.t == 6); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } + + // Armed move. + // +#ifdef HAVE_CXX11 + { + auto_ptr c; + + { + o.s.pop_back (); + + transaction t (db->begin ()); + db->update (o); + assert (o.s._tracking ()); + c.reset (new object (std::move (o))); + assert (!o.s._tracking () && c->s._tracking ()); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.i == 2 && tr.d == 1 && tr.t == 4); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } +#endif + + // Test mixing "smart" and "dumb" container (specifically, erase(obj)). + // + { + mix_object o (1); + o.ov.assign (3, 123); + o.sv.assign (3, 123); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + db->erase (o); + t.commit (); + } + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + } + + // Test using change tracking container as inverse member. + // + { + inv_object1 o1; + inv_object2 o2; + o1.o2 = &o2; + + { + transaction t (db->begin ()); + db->persist (o2); + db->persist (o1); + t.commit (); + } + + assert (!o2.o1._tracking ()); + + { + session s; + transaction t (db->begin ()); + auto_ptr p1 (db->load (o1.id_)); + auto_ptr p2 (db->load (o2.id_)); + assert (p2->o1[0] == p1.get ()); + assert (!p2->o1._tracking ()); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/container/change-tracking/makefile b/common/container/change-tracking/makefile new file mode 100644 index 0000000..ffd149c --- /dev/null +++ b/common/container/change-tracking/makefile @@ -0,0 +1,115 @@ +# file : common/container/change-tracking/makefile +# copyright : Copyright (c) 2009-2012 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 +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.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 + +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) + +# 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) + +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 \ +--table-prefix t_cont_changet_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(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): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) $(call vc11projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../../template/Makefile.am) + $(call meta-vc9projs,../../template/template,$(name)) + $(call meta-vc10projs,../../template/template,$(name)) + $(call meta-vc11projs,../../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# 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)) + $(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/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.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/container/change-tracking/test.hxx b/common/container/change-tracking/test.hxx new file mode 100644 index 0000000..0387faf --- /dev/null +++ b/common/container/change-tracking/test.hxx @@ -0,0 +1,81 @@ +// file : common/container/change-tracking/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include // HAVE_CXX11 + +#include +#include +#include + +#ifdef HAVE_CXX11 +# include // std::move +#endif + +#include +#include + +#pragma db object pointer(std::auto_ptr) +struct object +{ + object () {} + object (const std::string& id): id_ (id) {} + +#ifdef HAVE_CXX11 + object (const object& x): id_ (x.id_), i (x.i), s (x.s) {} + object (object&& x): id_ (std::move (x.id_)), i (x.i), s (std::move (x.s)) {} +#endif + + #pragma db id + std::string id_; + + unsigned int i; + + odb::vector s; + + inline bool + operator== (const object& o) {return id_ == o.id_ && i == o.i && s == o.s;} +}; + +// Test mixing "smart" and "dumb" container (specifically, erase(obj)). +// +#pragma db object +struct mix_object +{ + mix_object () {} + mix_object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + odb::vector ov; + std::vector sv; +}; + +// Test using change tracking container as inverse member. +// +struct inv_object2; + +#pragma db object session +struct inv_object1 +{ + #pragma db id auto + unsigned long id_; + + inv_object2* o2; +}; + +#pragma db object session +struct inv_object2 +{ + #pragma db id auto + unsigned long id_; + + #pragma db inverse(o2) + odb::vector o1; +}; + +#endif // TEST_HXX diff --git a/common/container/change-tracking/test.std b/common/container/change-tracking/test.std new file mode 100644 index 0000000..e69de29 diff --git a/common/container/driver.cxx b/common/container/driver.cxx deleted file mode 100644 index c4e1179..0000000 --- a/common/container/driver.cxx +++ /dev/null @@ -1,516 +0,0 @@ -// file : common/container/driver.cxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -// Test container persistence. -// - -#include // std::auto_ptr -#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)); - - for (unsigned short i (0); i < 2; ++i) - { - object empty ("empty"), med ("medium"), full ("full"); - - // - // empty - // - - empty.num = 0; - empty.str = ""; - -#ifdef HAVE_CXX11 - // array - // - empty.na[0] = 123; - empty.na[1] = 234; - empty.na[2] = 345; - - empty.sa[0] = "aaa"; - empty.sa[1] = "bbbb"; - empty.sa[2] = "ccccc"; - - empty.ca[0] = comp (123, "aaa"); - empty.ca[1] = comp (234, "bbbb"); - empty.ca[2] = comp (345, "ccccc"); -#endif - - - // - // med - // - - med.num = 999; - med.str = "xxx"; - - // vector - // - med.nv.push_back (123); - med.nv.push_back (234); - - med.sv.push_back ("aaa"); - med.sv.push_back ("bbbb"); - - med.cv.push_back (comp (123, "aaa")); - med.cv.push_back (comp (234, "bbbb")); - - med.uv.push_back (123); - med.uv.push_back (234); - - // list - // - med.sl.push_back ("aaa"); - med.sl.push_back ("bbbb"); - - // set - // - med.ns.insert (123); - med.ns.insert (234); - - med.ss.insert ("aaa"); - med.ss.insert ("bbbb"); - - med.cs.insert (comp (123, "aaa")); - med.cs.insert (comp (234, "bbbb")); - - // map - // - med.nsm[123] = "aaa"; - med.nsm[234] = "bbbb"; - - med.snm["aaa"] = 123; - med.snm["bbbb"] = 234; - - med.ncm[123] = comp (123, "aaa"); - med.ncm[234] = comp (234, "bbbb"); - - med.csm[comp (123, "aaa")] = "aaa"; - med.csm[comp (234, "bbbb")] = "bbbb"; - -#ifdef HAVE_CXX11 - // array - // - med.na[0] = 123; - med.na[1] = 234; - med.na[2] = 345; - - med.sa[0] = "aaa"; - med.sa[1] = "bbbb"; - med.sa[2] = "ccccc"; - - med.ca[0] = comp (123, "aaa"); - med.ca[1] = comp (234, "bbbb"); - med.ca[2] = comp (345, "ccccc"); - - // forward_list - // - med.nfl.push_front (234); - med.nfl.push_front (123); - - med.sfl.push_front ("bbbb"); - med.sfl.push_front ("aaa"); - - med.cfl.push_front (comp (234, "bbbb")); - med.cfl.push_front (comp (123, "aaa")); - - // unordered_set - // - med.nus.insert (123); - med.nus.insert (234); - - med.sus.insert ("aaa"); - med.sus.insert ("bbbb"); - - med.cus.insert (comp (123, "aaa")); - med.cus.insert (comp (234, "bbbb")); - - // unordered_map - // - med.nsum[123] = "aaa"; - med.nsum[234] = "bbbb"; - - med.snum["aaa"] = 123; - med.snum["bbbb"] = 234; - - med.ncum[123] = comp (123, "aaa"); - med.ncum[234] = comp (234, "bbbb"); - - med.csum[comp (123, "aaa")] = "aaa"; - med.csum[comp (234, "bbbb")] = "bbbb"; -#endif - - // - // full - // - - full.num = 9999; - full.str = "xxxx"; - - // vector - // - full.nv.push_back (1234); - full.nv.push_back (2345); - full.nv.push_back (3456); - - full.sv.push_back ("aaaa"); - full.sv.push_back ("bbbbb"); - full.sv.push_back ("cccccc"); - - full.cv.push_back (comp (1234, "aaaa")); - full.cv.push_back (comp (2345, "bbbbb")); - full.cv.push_back (comp (3456, "cccccc")); - - full.uv.push_back (1234); - full.uv.push_back (2345); - full.uv.push_back (3456); - - // list - // - full.sl.push_back ("aaaa"); - full.sl.push_back ("bbbbb"); - full.sl.push_back ("cccccc"); - - // set - // - full.ns.insert (1234); - full.ns.insert (2345); - full.ns.insert (3456); - - full.ss.insert ("aaaa"); - full.ss.insert ("bbbbb"); - full.ss.insert ("cccccc"); - - full.cs.insert (comp (1234, "aaaa")); - full.cs.insert (comp (2345, "bbbbb")); - full.cs.insert (comp (3456, "cccccc")); - - // map - // - full.nsm[1234] = "aaaa"; - full.nsm[2345] = "bbbbb"; - full.nsm[3456] = "cccccc"; - - full.snm["aaaa"] = 1234; - full.snm["bbbbb"] = 2345; - full.snm["cccccc"] = 3456; - - full.ncm[1234] = comp (1234, "aaaa"); - full.ncm[2345] = comp (2345, "bbbbb"); - full.ncm[3456] = comp (3456, "cccccc"); - - full.csm[comp (1234, "aaaa")] = "aaaa"; - full.csm[comp (2345, "bbbbb")] = "bbbbb"; - full.csm[comp (3456, "cccccc")] = "cccccc"; - -#ifdef HAVE_CXX11 - // array - // - full.na[0] = 123; - full.na[1] = 234; - full.na[2] = 345; - - full.sa[0] = "aaa"; - full.sa[1] = "bbbb"; - full.sa[2] = "ccccc"; - - full.ca[0] = comp (123, "aaa"); - full.ca[1] = comp (234, "bbbb"); - full.ca[2] = comp (345, "ccccc"); - - // forward_list - // - full.nfl.push_front (345); - full.nfl.push_front (234); - full.nfl.push_front (123); - - full.sfl.push_front ("ccccc"); - full.sfl.push_front ("bbbb"); - full.sfl.push_front ("aaa"); - - full.cfl.push_front (comp (345, "ccccc")); - full.cfl.push_front (comp (234, "bbbb")); - full.cfl.push_front (comp (123, "aaa")); - - // unordered_set - // - full.nus.insert (1234); - full.nus.insert (2345); - full.nus.insert (3456); - - full.sus.insert ("aaaa"); - full.sus.insert ("bbbbb"); - full.sus.insert ("cccccc"); - - full.cus.insert (comp (1234, "aaaa")); - full.cus.insert (comp (2345, "bbbbb")); - full.cus.insert (comp (3456, "cccccc")); - - // unordered_map - // - full.nsum[1234] = "aaaa"; - full.nsum[2345] = "bbbbb"; - full.nsum[3456] = "cccccc"; - - full.snum["aaaa"] = 1234; - full.snum["bbbbb"] = 2345; - full.snum["cccccc"] = 3456; - - full.ncum[1234] = comp (1234, "aaaa"); - full.ncum[2345] = comp (2345, "bbbbb"); - full.ncum[3456] = comp (3456, "cccccc"); - - full.csum[comp (1234, "aaaa")] = "aaaa"; - full.csum[comp (2345, "bbbbb")] = "bbbbb"; - full.csum[comp (3456, "cccccc")] = "cccccc"; -#endif - - // persist - // - { - transaction t (db->begin ()); - db->persist (empty); - db->persist (med); - db->persist (full); - t.commit (); - } - - // load & check - // - { - transaction t (db->begin ()); - auto_ptr e (db->load ("empty")); - auto_ptr m (db->load ("medium")); - auto_ptr f (db->load ("full")); - t.commit (); - - assert (empty == *e); - assert (med == *m); - assert (full == *f); - } - - // - // empty - // - - empty.num = 99; - empty.str = "xx"; - - empty.nv.push_back (12); - empty.sv.push_back ("aa"); - empty.cv.push_back (comp (12, "aa")); - empty.uv.push_back (12); - empty.sl.push_back ("aa"); - - empty.ns.insert (12); - empty.ss.insert ("aa"); - empty.cs.insert (comp (12, "aa")); - - empty.nsm[12] = "aa"; - empty.snm["aa"] = 12; - empty.ncm[12] = comp (12, "aa"); - empty.csm[comp (12, "aa")] = "aa"; - -#ifdef HAVE_CXX11 - empty.nfl.push_front (12); - empty.sfl.push_front ("aa"); - empty.cfl.push_front (comp (12, "aa")); - - empty.nus.insert (12); - empty.sus.insert ("aa"); - empty.cus.insert (comp (12, "aa")); - - empty.nsum[12] = "aa"; - empty.snum["aa"] = 12; - empty.ncum[12] = comp (12, "aa"); - empty.csum[comp (12, "aa")] = "aa"; -#endif - - // - // med - // - - med.num = 0; - med.str = ""; - - med.nv.clear (); - med.sv.clear (); - med.cv.clear (); - med.uv.clear (); - - med.sl.clear (); - - med.ns.clear (); - med.ss.clear (); - med.cs.clear (); - - med.nsm.clear (); - med.snm.clear (); - med.ncm.clear (); - med.csm.clear (); - -#ifdef HAVE_CXX11 - med.nfl.clear (); - med.sfl.clear (); - med.cfl.clear (); - - med.nus.clear (); - med.sus.clear (); - med.cus.clear (); - - med.nsum.clear (); - med.snum.clear (); - med.ncum.clear (); - med.csum.clear (); -#endif - - // - // full - // - - full.num++; - full.str += "x"; - - // vector - // - full.nv.back ()++; - full.nv.push_back (4567); - - full.sv.back () += "c"; - full.sv.push_back ("ddddddd"); - - full.cv.back ().num++; - full.cv.back ().str += "c"; - full.cv.push_back (comp (4567, "ddddddd")); - - full.uv.back ()++; - full.uv.push_back (4567); - - // list - // - full.sl.back () += "c"; - full.sl.push_back ("ddddddd"); - - // set - // - full.ns.insert (4567); - full.ss.insert ("ddddddd"); - full.cs.insert (comp (4567, "ddddddd")); - - // map - // - full.nsm[3456] += 'c'; - full.nsm[4567] = "ddddddd"; - - full.snm["cccccc"]++; - full.snm["ddddddd"] = 4567; - - full.ncm[3456].num++; - full.ncm[3456].str += 'c'; - full.ncm[4567] = comp (4567, "ddddddd"); - - full.csm[comp (3456, "cccccc")] += "c"; - full.csm[comp (4567, "ddddddd")] = "ddddddd"; - -#ifdef HAVE_CXX11 - // array - // - full.na[0]++; - full.sa[0] += 'a'; - full.ca[0].num++; - full.ca[0].str += 'a'; - - // forward_list - // - full.nfl.front ()++; - full.nfl.push_front (4567); - - full.sfl.front () += 'a'; - full.sfl.push_front ("ddddddd"); - - full.cfl.front ().num++; - full.cfl.front ().str += 'a'; - full.cfl.push_front (comp (4567, "ddddddd")); - - // unordered_set - // - full.nus.insert (4567); - full.sus.insert ("ddddddd1"); // 1 is to preserve order in VC++ 10. - full.cus.insert (comp (4567, "ddddddd1")); - - // unordered_map - // - full.nsum[3456] += 'c'; - full.nsum[4567] = "ddddddd"; - - full.snum["cccccc"]++; - full.snum["ddddddd1"] = 4567; - - full.ncum[3456].num++; - full.ncum[3456].str += 'c'; - full.ncum[4567] = comp (4567, "ddddddd"); - - full.csum[comp (3456, "cccccc")] += "c"; - full.csum[comp (4567, "ddddddd1")] = "ddddddd"; -#endif - - // update - // - { - transaction t (db->begin ()); - db->update (empty); - db->update (med); - db->update (full); - t.commit (); - } - - // load & check - // - { - transaction t (db->begin ()); - auto_ptr e (db->load ("empty")); - auto_ptr m (db->load ("medium")); - auto_ptr f (db->load ("full")); - t.commit (); - - assert (empty == *e); - assert (med == *m); - assert (full == *f); - } - - // erase - // - if (i == 0) - { - transaction t (db->begin ()); - db->erase ("empty"); - db->erase ("medium"); - db->erase ("full"); - t.commit (); - } - } - } - catch (const odb::exception& e) - { - cerr << e.what () << endl; - return 1; - } -} diff --git a/common/container/makefile b/common/container/makefile deleted file mode 100644 index 7173c57..0000000 --- a/common/container/makefile +++ /dev/null @@ -1,115 +0,0 @@ -# file : common/container/makefile -# copyright : Copyright (c) 2009-2012 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 -cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.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 - -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) - -# 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) - -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 \ ---table-prefix t_container_ -$(gen): cpp_options := -I$(src_base) -$(gen): $(common.l.cpp-options) - -$(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): db_id := @database@ -$(dist): sources := $(cxx_tun) -$(dist): headers := $(odb_hdr) -$(dist): data_dist := test.std -$(dist): export name := $(name) -$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ -$(call vc10projs,$(name)) $(call vc11projs,$(name)) -$(dist): - $(call dist-data,$(sources) $(headers) $(data_dist)) - $(call meta-automake,../template/Makefile.am) - $(call meta-vc9projs,../template/template,$(name)) - $(call meta-vc10projs,../template/template,$(name)) - $(call meta-vc11projs,../template/template,$(name)) - -# Test. -# -$(test): $(driver) $(src_base)/test.std - $(call schema) - $(call message,test $<,$< --options-file $(dcf_root)/db.options \ ->$(out_base)/test.out) - $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) - $(call message,,rm -f $(out_base)/test.out) - -# 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)) - $(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/vc9proj.make) -$(call include,$(bld_root)/meta/vc10proj.make) -$(call include,$(bld_root)/meta/vc11proj.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/container/test.hxx b/common/container/test.hxx deleted file mode 100644 index c531efb..0000000 --- a/common/container/test.hxx +++ /dev/null @@ -1,268 +0,0 @@ -// file : common/container/test.hxx -// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC -// license : GNU GPL v2; see accompanying LICENSE file - -#ifndef TEST_HXX -#define TEST_HXX - -#include // HAVE_CXX11 - -#include -#include -#include -#include -#include - -#ifdef HAVE_CXX11 -# include -# include -# include -# include -#endif - -#include - -#pragma db value -struct comp -{ - comp () {} - comp (int n, const std::string& s) : num (n), str (s) {} - - #pragma db column("number") - int num; - std::string str; -}; - -inline bool -operator== (const comp& x, const comp& y) -{ - return x.num == y.num && x.str == y.str; -} - -inline bool -operator!= (const comp& x, const comp& y) -{ - return !(x == y); -} - -inline bool -operator< (const comp& x, const comp& y) -{ - return x.num != y.num ? x.num < y.num : x.str < y.str; -} - -typedef std::list str_list; - -typedef std::vector num_vector; -typedef std::vector str_vector; - -typedef std::set num_set; -typedef std::set str_set; -typedef std::set comp_set; - -typedef std::map num_str_map; -typedef std::map str_num_map; -typedef std::map num_comp_map; -typedef std::map comp_str_map; - -#ifdef HAVE_CXX11 -struct comp_hash -{ - std::size_t - operator() (comp const& x) const {return nh (x.num) + sh (x.str);} - - std::hash nh; - std::hash sh; -}; - -typedef std::array num_array; -typedef std::array str_array; -typedef std::array comp_array; - -typedef std::forward_list num_flist; -typedef std::forward_list str_flist; -typedef std::forward_list comp_flist; - -typedef std::unordered_set num_uset; -typedef std::unordered_set str_uset; -typedef std::unordered_set comp_uset; - -typedef std::unordered_map num_str_umap; -typedef std::unordered_map str_num_umap; -typedef std::unordered_map num_comp_umap; -typedef std::unordered_map comp_str_umap; -#endif - -#pragma db value -struct cont_comp1 -{ - // This composite value does not have any columns. - // - num_vector sv; // Have the name "conflic" with the one in the object. -}; - -#pragma db value -struct cont_comp2 -{ - cont_comp2 (): num (777), str ("ggg") {} - - int num; - str_list sl; - std::string str; -}; - -#pragma db object -struct object -{ - object (): nv (comp1_.sv), sl (comp2_.sl) {} - object (const std::string& id) : id_ (id), nv (comp1_.sv), sl (comp2_.sl) {} - - #pragma db id - std::string id_; - - int num; - - cont_comp1 comp1_; - cont_comp2 comp2_; - - // vector - // - #pragma db transient - num_vector& nv; - - #pragma db table("object_strings") id_column ("obj_id") - str_vector sv; - - #pragma db value_column("") - std::vector cv; - - #pragma db unordered - num_vector uv; - - // list - // - #pragma db transient - str_list& sl; - - // set - // - num_set ns; - str_set ss; - comp_set cs; - - // map - // - num_str_map nsm; - str_num_map snm; - num_comp_map ncm; - comp_str_map csm; - -#ifdef HAVE_CXX11 - // array - // - num_array na; - str_array sa; - comp_array ca; - - // forward_list - // - num_flist nfl; - str_flist sfl; - comp_flist cfl; - - // unordered_set - // - num_uset nus; - str_uset sus; - comp_uset cus; - - // unordered_map - // - num_str_umap nsum; - str_num_umap snum; - num_comp_umap ncum; - comp_str_umap csum; -#else - // Dummy containers to get the equivalent DROP TABLE statements. - // - num_vector na; - num_vector sa; - num_vector ca; - - num_vector nfl; - num_vector sfl; - num_vector cfl; - - num_set nus; - str_set sus; - comp_set cus; - - num_str_map nsum; - str_num_map snum; - num_comp_map ncum; - comp_str_map csum; -#endif - - std::string str; -}; - -inline bool -operator== (const object& x, const object& y) -{ - if (x.uv.size () != y.uv.size ()) - return false; - - int xs (0), ys (0); - - for (num_vector::size_type i (0); i < x.uv.size (); ++i) - { - xs += x.uv[i]; - ys += y.uv[i]; - } - - return - x.id_ == y.id_ && - x.num == y.num && - - x.comp2_.num == y.comp2_.num && - x.comp2_.str == y.comp2_.str && - - x.nv == y.nv && - x.sv == y.sv && - x.cv == y.cv && - xs == ys && - - x.sl == y.sl && - - x.ns == y.ns && - x.ss == y.ss && - x.cs == y.cs && - - x.nsm == y.nsm && - x.snm == y.snm && - x.ncm == y.ncm && - x.csm == y.csm && - -#ifdef HAVE_CXX11 - x.na == y.na && - x.sa == y.sa && - x.ca == y.ca && - - x.nfl == y.nfl && - x.sfl == y.sfl && - x.cfl == y.cfl && - - x.nus == y.nus && - x.sus == y.sus && - x.cus == y.cus && - - x.nsum == y.nsum && - x.snum == y.snum && - x.ncum == y.ncum && - x.csum == y.csum && -#endif - - x.str == y.str; -} - -#endif // TEST_HXX diff --git a/common/container/test.std b/common/container/test.std deleted file mode 100644 index e69de29..0000000 diff --git a/common/makefile b/common/makefile index eae7f7e..883fe3f 100644 --- a/common/makefile +++ b/common/makefile @@ -4,48 +4,49 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make -tests := \ -access \ -auto \ -blob \ -callback \ -circular/single \ -circular/multiple \ -composite \ -composite-id \ -const-object \ -const-member \ -container \ -ctor \ -default \ -definition \ -enum \ -erase-query \ -include \ -index \ -inheritance/polymorphism \ -inheritance/reuse \ -inheritance/transient \ -inverse \ -lazy-ptr \ -lifecycle \ -no-id \ -optimistic \ -pragma \ -prepared \ -query/basics \ -query/array \ -readonly \ -relationship \ -relationship-query \ -schema \ -session/cache \ -template \ -transaction/basics \ -transaction/callback \ -types \ -view \ -virtual \ +tests := \ +access \ +auto \ +blob \ +callback \ +circular/single \ +circular/multiple \ +composite \ +composite-id \ +const-object \ +const-member \ +container/basics \ +container/change-tracking \ +ctor \ +default \ +definition \ +enum \ +erase-query \ +include \ +index \ +inheritance/polymorphism \ +inheritance/reuse \ +inheritance/transient \ +inverse \ +lazy-ptr \ +lifecycle \ +no-id \ +optimistic \ +pragma \ +prepared \ +query/basics \ +query/array \ +readonly \ +relationship \ +relationship-query \ +schema \ +session/cache \ +template \ +transaction/basics \ +transaction/callback \ +types \ +view \ +virtual \ wrapper thread_tests := threads -- cgit v1.1