aboutsummaryrefslogtreecommitdiff
path: root/qt/common/containers/change-tracking
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-02-05 15:50:08 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-02-05 15:50:08 +0200
commit43fa55c1b8e389838c83be933bb30a2caaf7468d (patch)
tree310bc0ecc43ea38276a7e8ff2a541f2cba395333 /qt/common/containers/change-tracking
parent3eee63801cbe833f6557d6f85c5778b6209140be (diff)
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.
Diffstat (limited to 'qt/common/containers/change-tracking')
-rw-r--r--qt/common/containers/change-tracking/driver.cxx632
-rw-r--r--qt/common/containers/change-tracking/makefile126
-rw-r--r--qt/common/containers/change-tracking/test.hxx43
-rw-r--r--qt/common/containers/change-tracking/test.std0
4 files changed, 801 insertions, 0 deletions
diff --git a/qt/common/containers/change-tracking/driver.cxx b/qt/common/containers/change-tracking/driver.cxx
new file mode 100644
index 0000000..15dd825
--- /dev/null
+++ b/qt/common/containers/change-tracking/driver.cxx
@@ -0,0 +1,632 @@
+// file : qt/common/containers/change-tracking/driver.cxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test change-tracking Qt containers.
+//
+
+#include <common/config.hxx> // HAVE_CXX11
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#ifdef HAVE_CXX11
+# include <utility> // std::move
+#endif
+
+#include <QtCore/QtGlobal> // QT_VERSION, Q_FOREACH
+#include <QtCore/QCoreApplication>
+
+#include <odb/tracer.hxx>
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <odb/qt/list-iterator.hxx>
+#include <odb/qt/mutable-list-iterator.hxx>
+
+#include <common/common.hxx>
+
+#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. Only do this if we
+// have a fairly recent version of Qt, otherwise some underlying
+// functions will be missing.
+//
+#if QT_VERSION >= 0x050000
+template class QOdbList<short>;
+template class QOdbListIteratorImpl<QOdbList<short> >;
+template class QOdbListIterator<short>;
+template class QMutableOdbListIterator<short>;
+#endif
+
+void
+f (const QList<int>&) {}
+
+int
+main (int argc, char* argv[])
+{
+ QCoreApplication app (argc, argv);
+
+ try
+ {
+ // Test extended interface.
+ //
+ {
+ typedef QOdbList<int> list;
+
+ list ol;
+ QList<int> ql;
+ f (ol); // Implicit conversion to QList.
+ list ol1 (ql); // Initialization from QList.
+ ol = ql; // Assignement from QList.
+
+ // Container comparison.
+ //
+ if (ol != ol1 ||
+ ol != ql ||
+ ql != ol1)
+ ol.clear ();
+
+ // Container operators.
+ //
+ ol += ol1;
+ ol += ql;
+ ol = ol1 + ql;
+ ol = ql + ol1;
+
+ // Iterator comparison/conversion.
+ //
+#ifndef QT_STRICT_ITERATORS
+ list::const_iterator i (ol.begin ());
+ if (i != ol.end ())
+ i = ol.end ();
+#endif
+
+ Q_FOREACH (const int& i, ol)
+ cerr << i;
+ }
+
+ auto_ptr<database> 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<object> p (db->load<object> ("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<object> ("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<object> ("1") == o);
+ t.commit ();
+ }
+
+ {
+ 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<object> ("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<object> ("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<object> ("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<object> ("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<object> ("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<object> ("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<object> ("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<object> ("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<object> ("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<object> ("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<object> ("1") == o);
+ t.commit ();
+ }
+
+ {
+ o.s.modify (1) += 'b';
+ 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<object> ("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<object> ("1") == o);
+ t.commit ();
+ }
+
+ {
+ o.s.begin ().modify () += 'a';
+
+ transaction t (db->begin ());
+ tr.reset (t);
+ db->update (o);
+ assert (tr.u == 2 && tr.t == 2);
+ assert (*db->load<object> ("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<object> ("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<object> ("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<object> ("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<object> ("1") == o);
+ t.commit ();
+ }
+
+ {
+ QList<QString> v;
+ v.push_back ("1");
+ v.push_back ("2");
+ v.push_back ("3");
+ v.push_back ("4");
+ o.s = v;
+
+ transaction t (db->begin ());
+ tr.reset (t);
+ db->update (o);
+ assert (tr.u == 4 && tr.i == 1 && tr.t == 5);
+ assert (*db->load<object> ("1") == o);
+ t.commit ();
+ }
+
+ // removeOne/removeAll
+ //
+ {
+ o.s.clear ();
+ o.s.push_back ("a");
+ o.s.push_back ("a");
+ o.s.push_back ("b");
+ o.s.push_back ("c");
+ o.s.push_back ("a");
+ o.s.push_back ("d");
+
+ transaction t (db->begin ());
+ tr.reset (t);
+ db->update (o);
+ assert (*db->load<object> ("1") == o);
+ t.commit ();
+ }
+
+ {
+ o.s.removeOne ("c");
+ assert (o.s.size () == 5 && o.s[3] == "a");
+
+ transaction t (db->begin ());
+ tr.reset (t);
+ db->update (o);
+ assert (tr.u == 3 && tr.d == 1 && tr.t == 4);
+ assert (*db->load<object> ("1") == o);
+ t.commit ();
+ }
+
+ {
+ o.s.removeAll ("a");
+ assert (o.s.size () == 2 && o.s[0] == "b" && o.s[1] == "d");
+
+ transaction t (db->begin ());
+ tr.reset (t);
+ db->update (o);
+ assert (tr.u == 3 && tr.d == 1 && tr.t == 4);
+ assert (*db->load<object> ("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<object> ("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<object> 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<object> 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
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/qt/common/containers/change-tracking/makefile b/qt/common/containers/change-tracking/makefile
new file mode 100644
index 0000000..7a25503
--- /dev/null
+++ b/qt/common/containers/change-tracking/makefile
@@ -0,0 +1,126 @@
+# file : qt/common/containers/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)
+
+$(call import,\
+ $(scf_root)/import/libodb-qt/stub.make,\
+ l: odb_qt.l,cpp-options: odb_qt.l.cpp-options)
+
+$(call import,\
+ $(scf_root)/import/libqt/core/stub.make,\
+ l: qt_core.l,cpp-options: qt.l.cpp-options)
+
+# Build.
+#
+$(driver): $(cxx_obj) $(odb_qt.l) $(common.l) $(qt_core.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base)
+$(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(odb_qt.l.cpp-options) \
+$(qt_core.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) \
+--profile qt/containers --profile qt/basic --generate-schema \
+--table-prefix qt_cont_ct_
+$(gen): cpp_options := -I$(src_base)
+$(gen): $(common.l.cpp-options) $(odb_qt.l.cpp-options) \
+$(qt_core.l.cpp-options)
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Dist
+#
+name := $(subst /,-,$(subst $(src_root)/qt/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/qt/common/containers/change-tracking/test.hxx b/qt/common/containers/change-tracking/test.hxx
new file mode 100644
index 0000000..7ad230e
--- /dev/null
+++ b/qt/common/containers/change-tracking/test.hxx
@@ -0,0 +1,43 @@
+// file : qt/common/containers/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 <common/config.hxx> // HAVE_CXX11
+
+#include <memory>
+
+#ifdef HAVE_CXX11
+# include <utility> // std::move
+#endif
+
+#include <QtCore/QString>
+
+#include <odb/core.hxx>
+#include <odb/qt/list.hxx>
+
+#pragma db object pointer(std::auto_ptr)
+struct object
+{
+ object () {}
+ object (const QString& 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
+ QString id_;
+
+ unsigned int i;
+
+ QOdbList<QString> s;
+
+ inline bool
+ operator== (const object& o) {return id_ == o.id_ && i == o.i && s == o.s;}
+};
+
+#endif // TEST_HXX
diff --git a/qt/common/containers/change-tracking/test.std b/qt/common/containers/change-tracking/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/qt/common/containers/change-tracking/test.std