summaryrefslogtreecommitdiff
path: root/odb-tests/common/callback
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/callback')
-rw-r--r--odb-tests/common/callback/buildfile41
-rw-r--r--odb-tests/common/callback/driver.cxx184
-rw-r--r--odb-tests/common/callback/test.hxx43
-rw-r--r--odb-tests/common/callback/testscript100
4 files changed, 368 insertions, 0 deletions
diff --git a/odb-tests/common/callback/buildfile b/odb-tests/common/callback/buildfile
new file mode 100644
index 0000000..2ecc3b8
--- /dev/null
+++ b/odb-tests/common/callback/buildfile
@@ -0,0 +1,41 @@
+# file : common/callback/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 callback_ \
+ --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/callback/driver.cxx b/odb-tests/common/callback/driver.cxx
new file mode 100644
index 0000000..80513c6
--- /dev/null
+++ b/odb-tests/common/callback/driver.cxx
@@ -0,0 +1,184 @@
+// file : common/callback/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test database operation callbacks.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#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;
+
+const char* events[] =
+{
+ "pre_persist",
+ "post_persist",
+ "pre_load",
+ "post_load",
+ "pre_update",
+ "post_update",
+ "pre_erase",
+ "post_erase"
+};
+
+void object::
+db_callback (callback_event e, database& db)
+{
+ cout << " " << events[e] << " " << id_ << endl;
+
+ // Test custom recursive loading.
+ //
+ if (e == callback_event::post_load && ref != 0)
+ {
+ robj = db.load<object> (ref);
+ cout << " " << id_ << ' ' << ref << ' ' << robj->id_ << endl;
+ }
+}
+
+void object::
+db_callback (callback_event e, database&) const
+{
+ cout << " " << events[e] << " " << id_ << " const" << endl;
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ // Persist.
+ //
+ cout << "persist" << endl;
+ {
+ object o1 (1, 1);
+ object const o2 (2, 2);
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (&o2);
+ t.commit ();
+ }
+ cout << "***" << endl;
+
+ // Load.
+ //
+ cout << "load" << endl;
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (1));
+ object o2;
+ db->load<object> (2, o2);
+ t.commit ();
+ }
+ cout << "***" << endl;
+
+ // Query.
+ //
+ cout << "query" << endl;
+ {
+ typedef odb::query<object> query;
+ typedef odb::result<object> result;
+
+ transaction t (db->begin ());
+
+ result r (db->query<object> ((query::id < 3) + "ORDER BY" + query::id));
+
+ for (result::iterator i (r.begin ()); i != r.end (); ++i)
+ {
+ if (i->id_ > 3) // Load.
+ break;
+ }
+
+ t.commit ();
+ }
+ cout << "***" << endl;
+
+ // Update.
+ //
+ cout << "update" << endl;
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (1));
+ unique_ptr<object> o2 (db->load<object> (2));
+ o1->data++;
+ o2->data++;
+ db->update (o1.get ());
+ db->update (static_cast<const object&> (*o2));
+ t.commit ();
+ }
+ cout << "***" << endl;
+
+ // Erase.
+ //
+ cout << "erase" << endl;
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (1));
+ unique_ptr<object> o2 (db->load<object> (2));
+ db->erase (static_cast<const object*> (o1.get ()));
+ db->erase (*o2);
+ t.commit ();
+ }
+ cout << "***" << endl;
+
+ // Delayed (recursive) load.
+ //
+ cout << "delayed load" << endl;
+ {
+ {
+ object o1 (1, 1);
+ object o2 (2, 2);
+ object o3 (3, 3);
+ object o4 (4, 4);
+
+ o1.pobj = &o2;
+ o1.ref = 4;
+
+ o2.pobj = &o3;
+ o2.ref = 4;
+
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o3);
+ db->persist (o4);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (1));
+ object* o2 (o1->pobj);
+
+ cout << o1->id_ << ' ' << o1->ref << ' ' << o1->robj->id_ << endl;
+ cout << o2->id_ << ' ' << o2->ref << ' ' << o2->robj->id_ << endl;
+
+ delete o1->robj;
+ delete o2->robj;
+
+ delete o2->pobj;
+ delete o2;
+ t.commit ();
+ }
+ }
+ cout << "***" << endl;
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/callback/test.hxx b/odb-tests/common/callback/test.hxx
new file mode 100644
index 0000000..bd30907
--- /dev/null
+++ b/odb-tests/common/callback/test.hxx
@@ -0,0 +1,43 @@
+// file : common/callback/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <odb/core.hxx>
+#include <odb/callback.hxx>
+
+#pragma db object callback(db_callback)
+struct object
+{
+ object (unsigned long id, unsigned long d)
+ : id_ (id), data (d), pobj (0), robj (0), ref (0)
+ {
+ }
+
+ object ()
+ : id_ (0), pobj (0), robj (0)
+ {
+ }
+
+ #pragma db id
+ unsigned long id_;
+
+ unsigned long data;
+
+ object* pobj;
+
+ // Test custom recursive loading.
+ //
+ #pragma db transient
+ object* robj;
+ unsigned long ref; // Unless 0, reference to another object.
+
+ void
+ db_callback (odb::callback_event, odb::database&);
+
+ void
+ db_callback (odb::callback_event, odb::database&) const;
+};
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/callback/testscript b/odb-tests/common/callback/testscript
new file mode 100644
index 0000000..c7d03ee
--- /dev/null
+++ b/odb-tests/common/callback/testscript
@@ -0,0 +1,100 @@
+# file : common/callback/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+
++cat <<EOI >=output
+ persist
+ pre_persist 1 const
+ post_persist 1 const
+ pre_persist 2 const
+ post_persist 2 const
+ ***
+ load
+ pre_load 0
+ post_load 1
+ pre_load 0
+ post_load 2
+ ***
+ query
+ pre_load 0
+ post_load 1
+ pre_load 0
+ post_load 2
+ ***
+ update
+ pre_load 0
+ post_load 1
+ pre_load 0
+ post_load 2
+ pre_update 1 const
+ post_update 1 const
+ pre_update 2 const
+ post_update 2 const
+ ***
+ erase
+ pre_load 0
+ post_load 1
+ pre_load 0
+ post_load 2
+ pre_erase 1 const
+ post_erase 1 const
+ pre_erase 2 const
+ post_erase 2 const
+ ***
+ delayed load
+ pre_persist 1 const
+ post_persist 1 const
+ pre_persist 2 const
+ post_persist 2 const
+ pre_persist 3 const
+ post_persist 3 const
+ pre_persist 4 const
+ post_persist 4 const
+ pre_load 0
+ pre_load 0
+ pre_load 0
+ post_load 3
+ post_load 2
+ pre_load 0
+ post_load 4
+ 2 4 4
+ post_load 1
+ pre_load 0
+ post_load 4
+ 1 4 4
+ 1 4 4
+ 2 4 4
+ ***
+ EOI
+
+test.redirects += >>>../output
+
+: mysql
+:
+if $mysql
+{
+ .include ../../mysql.testscript
+
+ $create_schema;
+ $*
+}
+
+: sqlite
+:
+if $sqlite
+{
+ .include ../../sqlite.testscript
+
+ $*
+}
+
+: pgsql
+:
+if $pgsql
+{
+ .include ../../pgsql.testscript
+
+ $create_schema;
+ $*
+}