Author: wyoung
Date: Thu Jun 21 12:53:34 2007
New Revision: 1574

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1574&view=rev
Log:
Added another Query::for_each() overload which builds a "select * from
TABLE" query using the _table member of an SSQLS instance.  Modified the
example program to use this version, as it seems likely to be the most
popular overload.

Modified:
    trunk/examples/for_each.cpp
    trunk/lib/query.h

Modified: trunk/examples/for_each.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/for_each.cpp?rev=1574&r1=1573&r2=1574&view=diff
==============================================================================
--- trunk/examples/for_each.cpp (original)
+++ trunk/examples/for_each.cpp Thu Jun 21 12:53:34 2007
@@ -35,7 +35,7 @@
 #include <math.h>
 
 
-// Define a functor to maintain statistics about the stock table
+// Define a functor to collect statistics about the stock table
 class gather_stock_stats
 {
 public:
@@ -84,11 +84,10 @@
                        return 1;
                }
 
-               // Gather and display the stats for the entire table
+               // Gather and display the stats for the entire stock table
                mysqlpp::Query query = con.query();
-               std::cout << "There are " << query.for_each(
-                               "select * from stock", gather_stock_stats()) <<
-                               '.' << std::endl;
+               std::cout << "There are " << query.for_each(stock(),
+                               gather_stock_stats()) << '.' << std::endl;
        }
        catch (const mysqlpp::BadQuery& e) {
                // Something went wrong with the SQL query.

Modified: trunk/lib/query.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/query.h?rev=1574&r1=1573&r2=1574&view=diff
==============================================================================
--- trunk/lib/query.h (original)
+++ trunk/lib/query.h Thu Jun 21 12:53:34 2007
@@ -415,6 +415,32 @@
                return fn;
        }
 
+       /// \brief Run a functor for every row in a table
+       ///
+       /// Just like for_each(Function), except that it builds a
+       /// "select * from TABLE" query using the SQL table name from
+       /// the SSQLS instance you pass.
+       ///
+       /// \param ssqls the SSQLS instance to get a table name from
+       /// \param fn the functor called for each row
+       ///
+       /// \return a copy of the passed functor
+       template <class SSQLS, typename Function>
+       Function for_each(const SSQLS& ssqls, Function fn)
+       {       
+               SQLString query("select * from ");
+               query += ssqls._table;
+               mysqlpp::ResUse res = use(query);
+               if (res) {
+                       mysqlpp::NoExceptions ne(res);
+                       while (mysqlpp::Row row = res.fetch_row()) {
+                               fn(row);
+                       }
+               }
+
+               return fn;
+       }
+
        /// \brief Execute a query, conditionally storing each row in a
        /// container
        ///


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to