Author: wyoung
Date: Wed Jun 20 15:18:09 2007
New Revision: 1572

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1572&view=rev
Log:
Added examples/for_each.cpp, based on code provided by Joel Fielder.

Added:
    trunk/examples/for_each.cpp   (with props)
Modified:
    trunk/CREDITS
    trunk/mysql++.bkl

Modified: trunk/CREDITS
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/CREDITS?rev=1572&r1=1571&r2=1572&view=diff
==============================================================================
--- trunk/CREDITS (original)
+++ trunk/CREDITS Wed Jun 20 15:18:09 2007
@@ -38,8 +38,9 @@
        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.
+       original idea for Query::for_each() and Query::store_in(),
+       provided the basis for examples/for_each.cpp, and provided
+       a fix for exception flag propagation in Query.
 
 
 Here are the personal credits from the old 1.7.9 documentation,

Added: trunk/examples/for_each.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/for_each.cpp?rev=1572&view=auto
==============================================================================
--- trunk/examples/for_each.cpp (added)
+++ trunk/examples/for_each.cpp Wed Jun 20 15:18:09 2007
@@ -1,0 +1,105 @@
+/***********************************************************************
+ for_each.cpp - Demonstrates Query::for_each(), showing how to perform
+       an arbitrary action on each row in a result set.
+
+ Copyright (c) 2005-2007 by Joel Fielder and 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.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#include "util.h"
+#include "stock.h"
+
+#include <mysql++.h>
+
+#include <iostream>
+
+#include <math.h>
+
+
+// Define a functor to maintain statistics about the stock table
+class gather_stock_stats
+{
+public:
+       gather_stock_stats() :
+       items_(0),
+       weight_(0),
+       cost_(0)
+       {
+       }
+
+       void operator()(const stock& s)
+       {
+               items_  += s.num;
+               weight_ += (s.num * s.weight);
+               cost_   += (s.num * s.price);
+       }
+       
+private:
+       mysqlpp::sql_bigint items_;
+       mysqlpp::sql_double weight_, cost_;
+
+       friend std::ostream& operator<<(std::ostream& os,
+                       const gather_stock_stats& ss);
+};
+
+
+// Dump the contents of gather_stock_stats to a stream in human-readable
+// form.
+std::ostream&
+operator<<(std::ostream& os, const gather_stock_stats& ss)
+{
+       os << ss.items_ << " items " <<
+                       "weighing " << ss.weight_ << " stone and " <<
+                       "costing " << ss.cost_ << " cowrie shells";
+       return os;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+       try {
+               // Connect to the sample database
+               mysqlpp::Connection con;
+               if (!connect_to_db(argc, argv, con)) {
+                       return 1;
+               }
+
+               // Gather and display the stats for the entire table
+               mysqlpp::Query query = con.query();
+               std::cout << "There are " << query.for_each(
+                               "select * from stock", gather_stock_stats()) <<
+                               '.' << std::endl;
+       }
+       catch (const mysqlpp::BadQuery& e) {
+               // Something went wrong with the SQL query.
+               std::cerr << "Query failed: " << e.what() << std::endl;
+               return 1;
+       }
+       catch (const mysqlpp::Exception& er) {
+               // Catch-all for any other MySQL++ exceptions
+               std::cerr << "Error: " << er.what() << std::endl;
+               return 1;
+       }
+
+       return 0;
+}

Propchange: trunk/examples/for_each.cpp
------------------------------------------------------------------------------
    svn:executable = *

Modified: trunk/mysql++.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=1572&r1=1571&r2=1572&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Wed Jun 20 15:18:09 2007
@@ -243,6 +243,9 @@
                <exe id="store_if" template="util-example,common-example">
                        <sources>examples/store_if.cpp</sources>
                </exe>
+               <exe id="for_each" template="util-example,common-example">
+                       <sources>examples/for_each.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