Author: wyoung
Date: Tue Jun 19 06:30:31 2007
New Revision: 1563

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1563&view=rev
Log:
- Added examples/store_if.cpp to test Query::store_if().
- Added Query::for_each() and Query::store_if(), suggested by Joel 
  Fielder <[EMAIL PROTECTED]>.  (Code's all mine.)
- Added examples/store_if.cpp to test Query::store_if().
- Added Joel to the credits list.

Added:
    trunk/examples/store_if.cpp
      - copied, changed from r1559, trunk/examples/usequery.cpp
Modified:
    trunk/   (props changed)
    trunk/CREDITS
    trunk/lib/query.h
    trunk/mysql++.bkl

Propchange: trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Jun 19 06:30:31 2007
@@ -44,6 +44,7 @@
 multiquery
 resetdb
 simple[0-9]
+store_if
 tquery
 updel
 usequery

Modified: trunk/CREDITS
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/CREDITS?rev=1563&r1=1562&r2=1563&view=diff
==============================================================================
--- trunk/CREDITS (original)
+++ trunk/CREDITS Tue Jun 19 06:30:31 2007
@@ -36,6 +36,10 @@
        Remi Collet <[EMAIL PROTECTED]> is maintaining offical
        RPMs for Fedora, with other systems on the way.  His work has
        improved the RPM spec file we distribute greatly.
+
+       Joel Fielder <[EMAIL PROTECTED]> came up with the
+       original idea for Query::for_each() and Query::store_in(), and
+       provided a fix for exception flag propagation in Query.
 
 
 Here are the personal credits from the old 1.7.9 documentation,

Copied: trunk/examples/store_if.cpp (from r1559, trunk/examples/usequery.cpp)
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/store_if.cpp?p2=trunk/examples/store_if.cpp&p1=trunk/examples/usequery.cpp&r1=1559&r2=1563&rev=1563&view=diff
==============================================================================
--- trunk/examples/usequery.cpp (original)
+++ trunk/examples/store_if.cpp Tue Jun 19 06:30:31 2007
@@ -1,8 +1,10 @@
 /***********************************************************************
- usequery.cpp - Same as simple2 example, only with exceptions enabled.
-       The end of the result set is signalled differently in this case.
+ store_if.cpp - Demonstrates Query::store_if(), showing only the rows
+       from the sample table with prime quantities.  This isn't intended
+       to be useful, only to show how you can do result set filtering that
+       outstrips the power of SQL.
 
- Copyright (c) 2005 by Educational Technology Resources, Inc.
+ Copyright (c) 2005-2007 by Educational Technology Resources, Inc.
  Others may also hold copyrights on code in this file.  See the CREDITS
  file in the top directory of the distribution for details.
 
@@ -25,10 +27,37 @@
 ***********************************************************************/
 
 #include "util.h"
+#include "stock.h"
 
 #include <mysql++.h>
 
 #include <iostream>
+
+
+// Define a functor for testing primality.
+struct is_prime
+{
+       bool operator()(const stock& s)
+       {
+               if ((s.num == 2) || (s.num == 3)) {
+                       return true;    // 2 and 3 are trivial cases
+               }
+               else if ((s.num < 2) || ((s.num % 2) == 0)) {
+                       return false;   // can't be prime if < 2 or even
+               }
+               else {
+                       // The only possibility left is that it's divisible by 
an
+                       // odd number that's less or equal to its square root.
+                       for (int i = 3; i <= sqrt(s.num); i += 2) {
+                               if ((s.num % i) == 0) {
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+       }
+};
+
 
 int
 main(int argc, char *argv[])
@@ -40,21 +69,17 @@
                        return 1;
                }
 
-               // Build query to retrieve the entire stock table
+               // Pull only the stock items with prime quantities
+               std::vector<stock> results;
                mysqlpp::Query query = con.query();
-               query << "select * from stock";
+               query.store_if(results, "select * from stock", is_prime());
 
-               // Execute the query, but don't save results in memory
-               mysqlpp::ResUse res = query.use();
-               if (!res) {
-                       std::cerr << "Result set is empty!" << std::endl;
-                       return 1;
-               }
-
-               // Iterate through result set, printing each row.
-               mysqlpp::Row r;
-               while (r = res.fetch_row()) {
-                       print_stock_row(r);
+               // Show the results
+               print_stock_header(results.size());
+               std::vector<stock>::const_iterator it;
+               for (it = results.begin(); it != results.end(); ++it) {
+                       print_stock_row(it->item.c_str(), it->num, it->weight,
+                                       it->price, it->sdate);
                }
        }
        catch (const mysqlpp::BadQuery& e) {
@@ -62,18 +87,11 @@
                std::cerr << "Query failed: " << e.what() << std::endl;
                return 1;
        }
-       catch (const mysqlpp::EndOfResults&) {
-               // Last query result received.  Exit normally.
-               return 0;
-       }
        catch (const mysqlpp::Exception& er) {
                // Catch-all for any other MySQL++ exceptions
                std::cerr << "Error: " << er.what() << std::endl;
                return 1;
        }
 
-       // Shouldn't happen!  Program should either error out through one of
-       // the "return 1" cases above, or successfully walk off the end of
-       // the result set and go through the EndOfResults path above.
-       return 2;
+       return 0;
 }

Modified: trunk/lib/query.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/query.h?rev=1563&r1=1562&r2=1563&view=diff
==============================================================================
--- trunk/lib/query.h (original)
+++ trunk/lib/query.h Tue Jun 19 06:30:31 2007
@@ -370,6 +370,65 @@
        /// than use(), but it lets you have random access to the results.
        Result store(const char* str, size_t len);
 
+       /// \brief Execute a query, and call a functor for each returned row
+       ///
+       /// This method wraps a use() query, calling the given functor for
+       /// every returned row.  It is analogous to STL's for_each()
+       /// algorithm, but instead of iterating over some range within a
+       /// container, it iterates over a result set produced by a query.
+       ///
+       /// \param query the query string
+       /// \param fn the functor called for each row
+       /// \return a copy of the passed functor
+       template <typename Function>
+       Function for_each(const SQLString& query, Function fn)
+       {       
+               mysqlpp::ResUse res = use(query);
+               if (res) {
+                       mysqlpp::NoExceptions ne(*this);
+                       while (mysqlpp::Row row = res.fetch_row()) {
+                               fn(row);
+                       }
+               }
+
+               return fn;
+       }
+
+       /// \brief Execute a query, conditionally storing each row in a
+       /// container
+       ///
+       /// This method wraps a use() query, calling the given functor for
+       /// every returned row, and storing the results in the given
+       /// sequence container if the functor returns true.
+       ///
+       /// This is analogous to the STL copy_if() algorithm, except that
+       /// the source rows come from a database query instead of another
+       /// container.  (copy_if() isn't a standard STL algorithm, but only
+       /// due to an oversight by the standardization committee.)  This
+       /// fact may help you to remember the order of the parameters: the
+       /// container is the destination, the query is the source, and the
+       /// functor is the predicate; it's just like an STL algorithm.
+       ///
+       /// \param seq the destination container; needs a push_back() method
+       /// \param query the query string
+       /// \param fn the functor called for each row
+       /// \return a copy of the passed functor
+       template <class Sequence, typename Function>
+       Function store_if(Sequence& seq, const SQLString& query, Function fn)
+       {       
+               mysqlpp::NoExceptions ne(*this);
+               mysqlpp::ResUse res = use(query);
+               if (res) {
+                       while (mysqlpp::Row row = res.fetch_row()) {
+                               if (fn(row)) {
+                                       seq.push_back(row);
+                               }
+                       }
+               }
+
+               return fn;
+       }
+
        /// \brief Return next result set, when processing a multi-query
        ///
        /// There are two cases where you'd use this function instead of

Modified: trunk/mysql++.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=1563&r1=1562&r2=1563&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Tue Jun 19 06:30:31 2007
@@ -240,6 +240,9 @@
                <exe id="xaction" template="util-example,common-example">
                        <sources>examples/xaction.cpp</sources>
                </exe>
+               <exe id="store_if" template="util-example,common-example">
+                       <sources>examples/store_if.cpp</sources>
+               </exe>
 
                <!-- The few examples that don't use the util module -->
                <exe id="cgi_jpeg" template="common-example">


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

Reply via email to