From dee5f10d1d75eee43596ad1dfb47ecec9e0b1307 Mon Sep 17 00:00:00 2001 From: Michael Shepanski Date: Wed, 22 Oct 2014 16:42:05 +0200 Subject: Allow lambdas & std::functions as query factories with C++-98 builds of libodb --- common/prepared/driver.cxx | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'common/prepared') diff --git a/common/prepared/driver.cxx b/common/prepared/driver.cxx index 5c622c0..dfbdf13 100644 --- a/common/prepared/driver.cxx +++ b/common/prepared/driver.cxx @@ -249,7 +249,7 @@ main (int argc, char* argv[]) } db->query_factory ("person-params-query", - database::query_factory_type ()); + database::query_factory_ptr ()); } // Cached query with wildcard factory. @@ -273,7 +273,7 @@ main (int argc, char* argv[]) t.commit (); } - db->query_factory ("", database::query_factory_type ()); + db->query_factory ("", database::query_factory_ptr ()); } // Cached query with lambda factory. @@ -312,7 +312,47 @@ main (int argc, char* argv[]) } db->query_factory ("person-params-query-2", - database::query_factory_type ()); + database::query_factory_ptr ()); + } + + // Cached query with lambda factory using closure. Forces nonoptimized + // representation of std::function. + // + { + const std::string person_name ("John First"); + + db->query_factory ( + "person-params-query-3", + [person_name] (const char* name, connection& c) + { + typedef odb::query query; + + prepared_query pq ( + c.prepare_query ( + name, + query::age > 50 && query::name != person_name)); + c.cache_query (pq); + }); + + { + transaction t (db->begin ()); + + prep_query pq (db->lookup_query ("person-params-query-3")); + assert (pq); + + result r (pq.execute ()); + assert (size (r) == 4); + + t.commit (); + } + + db->query_factory ("person-params-query-3", +#ifdef HAVE_CXX11_NULLPTR + nullptr +#else + database::query_factory_ptr () +#endif + ); } #endif -- cgit v1.1