From 0d49ea1fe08cf1eab41a00149393a291c65a59d7 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 25 Jan 2024 20:32:06 +0300 Subject: Turn odb-tests repository into package for muti-package repository --- odb-tests/common/callback/driver.cxx | 184 +++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 odb-tests/common/callback/driver.cxx (limited to 'odb-tests/common/callback/driver.cxx') 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 // std::unique_ptr +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include + +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 (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 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 o1 (db->load (1)); + object o2; + db->load (2, o2); + t.commit (); + } + cout << "***" << endl; + + // Query. + // + cout << "query" << endl; + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + + result r (db->query ((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 o1 (db->load (1)); + unique_ptr o2 (db->load (2)); + o1->data++; + o2->data++; + db->update (o1.get ()); + db->update (static_cast (*o2)); + t.commit (); + } + cout << "***" << endl; + + // Erase. + // + cout << "erase" << endl; + { + transaction t (db->begin ()); + unique_ptr o1 (db->load (1)); + unique_ptr o2 (db->load (2)); + db->erase (static_cast (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 o1 (db->load (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; + } +} -- cgit v1.1