summaryrefslogtreecommitdiff
path: root/odb-tests/common/id/nested
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/id/nested')
-rw-r--r--odb-tests/common/id/nested/buildfile41
-rw-r--r--odb-tests/common/id/nested/driver.cxx266
-rw-r--r--odb-tests/common/id/nested/test.hxx217
-rw-r--r--odb-tests/common/id/nested/testscript33
4 files changed, 557 insertions, 0 deletions
diff --git a/odb-tests/common/id/nested/buildfile b/odb-tests/common/id/nested/buildfile
new file mode 100644
index 0000000..777cb65
--- /dev/null
+++ b/odb-tests/common/id/nested/buildfile
@@ -0,0 +1,41 @@
+# file : common/nested/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libodb = libodb%lib{odb}
+
+libs =
+
+for db: $databases
+ import libs += libodb-$db%lib{odb-$db}
+
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{* -*-odb -*-odb-*} {hxx ixx cxx}{test-odb} testscript
+
+# Introduce the metadata library target to make sure the libodb library is
+# resolved for the odb_compile ad hoc rule (see build/root.build for details).
+#
+libue{test-meta}: $libodb
+
+<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta}
+
+for db: $databases
+{
+ exe{driver}: {hxx ixx cxx}{test-odb-$db}: include = $multi
+ <{hxx ixx cxx}{test-odb-$db}>: hxx{test} libue{test-meta}
+}
+
+exe{driver}: libue{test-meta} $libs
+
+# Specify the ODB custom options to be used by the odb_compile ad hoc rule
+# (see build/root.build for details).
+#
+odb_options = --table-prefix t_id_nested_ \
+ --generate-schema \
+ --generate-query
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Testscript's run-time prerequisites.
+#
+exe{driver}: ../../../alias{database-client}: include = adhoc
diff --git a/odb-tests/common/id/nested/driver.cxx b/odb-tests/common/id/nested/driver.cxx
new file mode 100644
index 0000000..92a80f6
--- /dev/null
+++ b/odb-tests/common/id/nested/driver.cxx
@@ -0,0 +1,266 @@
+// file : common/id/nested/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test nested ids.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/session.hxx>
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <libcommon/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+using namespace odb::core;
+
+struct failed {};
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+
+ // Simple nested id.
+ //
+ {
+ using namespace test1;
+
+ object o1 (1, "a", 3);
+ o1.v.push_back (123);
+
+ object o2 (4, "b", 6);
+ o2.v.push_back (234);
+
+ object1 o (new object (10, "abc", 11));
+
+ {
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o.p);
+ db->persist (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> p1 (db->load<object> (o1.id.y));
+ unique_ptr<object> p2 (db->load<object> (o2.id.y));
+ unique_ptr<object1> p (db->load<object1> (o.id));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ assert (*p == o);
+ }
+
+ o1.z++;
+ o1.v.pop_back ();
+ o1.v.push_back (234);
+
+ o2.z--;
+ o2.v.back ()++;
+ o2.v.push_back (123);
+
+ delete o.p;
+ o.p = new object (20, "xyz", 11);
+
+ {
+ transaction t (db->begin ());
+ db->update (o1);
+ db->update (o2);
+ db->persist (o.p);
+ db->update (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> p1 (db->load<object> (o1.id.y));
+ unique_ptr<object> p2 (db->load<object> (o2.id.y));
+ unique_ptr<object1> p (db->load<object1> (o.id));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ assert (*p == o);
+ }
+ }
+
+ // Composite nested id.
+ //
+ {
+ using namespace test2;
+
+ object o1 (1, 2, "a", 123);
+ o1.v.push_back (123);
+
+ object o2 (1, 3, "b", 234);
+ o2.v.push_back (234);
+
+ object1 o (new object (2, 2, "abc", 123));
+ o.p->v.push_back (345);
+
+ {
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o.p);
+ db->persist (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> p1 (db->load<object> (o1.id.c));
+ unique_ptr<object> p2 (db->load<object> (o2.id.c));
+ unique_ptr<object1> p (db->load<object1> (o.id));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ assert (*p == o);
+ }
+
+ o1.z++;
+ o1.v.pop_back ();
+ o1.v.push_back (234);
+
+ o2.z--;
+ o2.v.modify_back ()++;
+ o2.v.push_back (123);
+
+ delete o.p;
+ o.p = new object (2, 3, "xyz", 234);
+
+ {
+ transaction t (db->begin ());
+ db->update (o1);
+ db->update (o2);
+ db->persist (o.p);
+ db->update (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> p1 (db->load<object> (o1.id.c));
+ unique_ptr<object> p2 (db->load<object> (o2.id.c));
+ unique_ptr<object1> p (db->load<object1> (o.id));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ assert (*p == o);
+ }
+ }
+
+ // Custom/by-value access.
+ //
+ {
+ using namespace test3;
+
+ object o1 (1, "a", 3);
+ object o2 (4, "b", 6);
+
+ {
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> p1 (db->load<object> (o1.id.y));
+ unique_ptr<object> p2 (db->load<object> (o2.id.y));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ }
+
+ o1.z++;
+ o2.z--;
+
+ {
+ transaction t (db->begin ());
+ db->update (o1);
+ db->update (o2);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> p1 (db->load<object> (o1.id.y));
+ unique_ptr<object> p2 (db->load<object> (o2.id.y));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ }
+ }
+
+ // Polymorphic.
+ //
+ {
+ using namespace test4;
+
+ base o1 (1, "a");
+ object o2 (2, "b", 1);
+
+ {
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<base> p1 (db->load<base> (o1.id.y));
+ unique_ptr<object> p2 (db->load<object> (o2.id.y));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ }
+
+ o2.z--;
+
+ {
+ transaction t (db->begin ());
+ db->update (o1);
+ db->update (o2);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<base> p1 (db->load<base> (o1.id.y));
+ unique_ptr<object> p2 (db->load<object> (o2.id.y));
+ t.commit ();
+
+ assert (*p1 == o1);
+ assert (*p2 == o2);
+ }
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/id/nested/test.hxx b/odb-tests/common/id/nested/test.hxx
new file mode 100644
index 0000000..06ee6b8
--- /dev/null
+++ b/odb-tests/common/id/nested/test.hxx
@@ -0,0 +1,217 @@
+// file : common/id/nested/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <string>
+#include <vector>
+
+#include <odb/core.hxx>
+#include <odb/vector.hxx>
+
+// Simple nested id.
+//
+#pragma db namespace table("t1_")
+namespace test1
+{
+ #pragma db value
+ struct comp
+ {
+ int x;
+ std::string y;
+ };
+
+ #pragma db object
+ struct object
+ {
+ #pragma db id(y)
+ comp id;
+
+ int z;
+ std::vector<int> v;
+
+ object () {}
+ object (int x, std::string y, int z_): z (z_) {id.x = x; id.y = y;}
+ };
+
+ inline bool
+ operator== (object a, object b)
+ {
+ return a.id.x == b.id.x && a.id.y == b.id.y && a.z == b.z && a.v == b.v;
+ }
+
+ #pragma db object
+ struct object1
+ {
+ #pragma db id auto
+ int id;
+
+ object* p;
+
+ object1 (object* p_ = 0): p (p_) {}
+ ~object1 () {delete p;}
+ };
+
+ inline bool
+ operator== (const object1& a, const object1& b)
+ {
+ return a.id == b.id && *a.p == *b.p;
+ }
+}
+
+// Composite nested id.
+//
+#pragma db namespace table("t2_")
+namespace test2
+{
+ #pragma db value
+ struct comp1
+ {
+ int x;
+ int y;
+ };
+
+ #pragma db value
+ struct comp2
+ {
+ comp1 c;
+ std::string s;
+ };
+
+ #pragma db object
+ struct object
+ {
+ #pragma db id(c)
+ comp2 id;
+
+ int z;
+ odb::vector<int> v;
+
+ object () {}
+ object (int x, int y, std::string s, int z_)
+ : z (z_) {id.c.x = x; id.c.y = y; id.s = s;}
+ };
+
+ inline bool
+ operator== (object a, object b)
+ {
+ return a.id.c.x == b.id.c.x && a.id.c.y == b.id.c.y &&
+ a.id.s == b.id.s && a.z == b.z && a.v == b.v;
+ }
+
+ #pragma db object
+ struct object1
+ {
+ #pragma db id auto
+ int id;
+
+ object* p;
+
+ object1 (object* p_ = 0): p (p_) {}
+ ~object1 () {delete p;}
+ };
+
+ inline bool
+ operator== (const object1& a, const object1& b)
+ {
+ return a.id == b.id && *a.p == *b.p;
+ }
+
+ // Multiple levels of nesting, just a compile test.
+ //
+ #pragma db object
+ struct object2
+ {
+ #pragma db id(c.x)
+ comp2 id;
+
+ int z;
+ };
+}
+
+// Custom/by-value access.
+//
+#pragma db namespace table("t3_")
+namespace test3
+{
+ #pragma db value
+ struct comp
+ {
+ int x;
+
+ std::string get_y () const {return y;}
+ void set_y (std::string v) {y = v;}
+
+ #pragma db get(get_y) set(set_y)
+ std::string y;
+ };
+
+ #pragma db object
+ struct object
+ {
+ comp get_id () const {return id;}
+ void set_id (comp v) {id = v;}
+
+ #pragma db id(y) get(get_id) set(set_id)
+ comp id;
+
+ int z;
+
+ object () {}
+ object (int x, std::string y, int z_): z (z_) {id.x = x; id.y = y;}
+ };
+
+ inline bool
+ operator== (object a, object b)
+ {
+ return a.id.x == b.id.x && a.id.y == b.id.y && a.z == b.z;
+ }
+}
+
+// Polymorphic.
+//
+#pragma db namespace table("t4_")
+namespace test4
+{
+ #pragma db value
+ struct comp
+ {
+ int x;
+ std::string y;
+ };
+
+ #pragma db object polymorphic
+ struct base
+ {
+ #pragma db id(y)
+ comp id;
+
+ virtual ~base () {}
+ base () {}
+ base (int x, std::string y) {id.x = x; id.y = y;}
+ };
+
+ inline bool
+ operator== (const base& a, const base& b)
+ {
+ return a.id.x == b.id.x && a.id.y == b.id.y;
+ }
+
+ #pragma db object
+ struct object: base
+ {
+ int z;
+
+ object () {}
+ object (int x, std::string y, int z_): base (x, y), z (z_) {}
+ };
+
+ inline bool
+ operator== (const object& a, const object& b)
+ {
+ return a.id.x == b.id.x && a.id.y == b.id.y && a.z == b.z;
+ }
+}
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/id/nested/testscript b/odb-tests/common/id/nested/testscript
new file mode 100644
index 0000000..89e8d7a
--- /dev/null
+++ b/odb-tests/common/id/nested/testscript
@@ -0,0 +1,33 @@
+# file : common/nested/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../../database-options.testscript
+
+: mysql
+:
+if $mysql
+{
+ .include ../../../mysql.testscript
+
+ $create_schema;
+ $*
+}
+
+: sqlite
+:
+if $sqlite
+{
+ .include ../../../sqlite.testscript
+
+ $*
+}
+
+: pgsql
+:
+if $pgsql
+{
+ .include ../../../pgsql.testscript
+
+ $create_schema;
+ $*
+}