From 382d0972301fb575f36eb2e5d442d735d7da3da4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 7 Mar 2012 10:21:09 +0200 Subject: Use RAII to free select statement results --- common/query/driver.cxx | 19 +++++++++++++++++++ common/query/test.std | 1 + common/threads/driver.cxx | 31 ++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 9 deletions(-) (limited to 'common') diff --git a/common/query/driver.cxx b/common/query/driver.cxx index 6afd82d..c207955 100644 --- a/common/query/driver.cxx +++ b/common/query/driver.cxx @@ -513,6 +513,25 @@ main (int argc, char* argv[]) t.commit (); } + + // Test empty result set. + // + cout << "test 018" << endl; + { + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "None")); + assert (r.empty ()); + t.commit (); + } + + { + transaction t (db->begin ()); + result r (db->query (query::last_name == "None")); + assert (r.begin () == r.end ()); + t.commit (); + } + } } catch (const odb::exception& e) { diff --git a/common/query/test.std b/common/query/test.std index 58a2aa0..e4ed7a0 100644 --- a/common/query/test.std +++ b/common/query/test.std @@ -98,3 +98,4 @@ Jane Doe 29 married test 016 test 017 +test 018 diff --git a/common/threads/driver.cxx b/common/threads/driver.cxx index 710b162..cbfb283 100644 --- a/common/threads/driver.cxx +++ b/common/threads/driver.cxx @@ -42,7 +42,7 @@ struct task { } - void + void* execute () { try @@ -121,24 +121,31 @@ struct task } } } + catch (int) + { + } + /* catch (const odb::exception& e) { cerr << e.what () << endl; + return reinterpret_cast (1); } + */ + + return 0; } static void* execute (void* arg) { - static_cast (arg)->execute (); - return 0; + return static_cast (arg)->execute (); } database& db_; unsigned long n_; }; -void +bool test (int argc, char* argv[], size_t max_connections) { auto_ptr db (create_database (argc, argv, true, max_connections)); @@ -156,8 +163,11 @@ test (int argc, char* argv[], size_t max_connections) new (details::shared) details::thread (&task::execute, t.get ()))); } + bool r (true); + for (unsigned long i (0); i < thread_count; ++i) - threads[i]->join (); + if (threads[i]->join () != 0) + r = false; { typedef odb::result result; @@ -170,6 +180,8 @@ test (int argc, char* argv[], size_t max_connections) t.commit (); } + + return r; } int @@ -177,10 +189,11 @@ main (int argc, char* argv[]) { try { - test (argc, argv, 0); - test (argc, argv, thread_count - 1); - test (argc, argv, thread_count / 2); - test (argc, argv, thread_count / 4); + if (!(test (argc, argv, 0) && + test (argc, argv, thread_count - 1) && + test (argc, argv, thread_count / 2) && + test (argc, argv, thread_count / 4))) + return 1; } catch (const odb::exception& e) { -- cgit v1.1