Author: wyoung
Date: Thu Feb  7 04:03:01 2008
New Revision: 2163

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2163&view=rev
Log:
Added Connection::count_rows() utility method, and calling it from
resetdb after populating the stock table to test it.  Just wraps "SELECT
COUNT(*) FROM tablename", in the tradition of other simple wrappers
defined in Connection.

Modified:
    trunk/bmark.txt
    trunk/examples/resetdb.cpp
    trunk/lib/connection.cpp
    trunk/lib/connection.h

Modified: trunk/bmark.txt
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/bmark.txt?rev=2163&r1=2162&r2=2163&view=diff
==============================================================================
--- trunk/bmark.txt (original)
+++ trunk/bmark.txt Thu Feb  7 04:03:01 2008
@@ -3,7 +3,7 @@
 Connecting to database server...
 Dropping existing sample data tables...
 Creating stock table...
-Populating stock table...
+Populating stock table...inserted 4 rows.
 Creating empty images table...
 Creating deadlock testing tables...
 Reinitialized sample database successfully.
@@ -92,7 +92,7 @@
 Connecting to database server...
 Dropping existing sample data tables...
 Creating stock table...
-Populating stock table...
+Populating stock table...inserted 4 rows.
 Creating empty images table...
 Creating deadlock testing tables...
 Reinitialized sample database successfully.
@@ -170,7 +170,7 @@
 Connecting to database server...
 Dropping existing sample data tables...
 Creating stock table...
-Populating stock table...
+Populating stock table...inserted 4 rows.
 Creating empty images table...
 Creating deadlock testing tables...
 Reinitialized sample database successfully.

Modified: trunk/examples/resetdb.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/examples/resetdb.cpp?rev=2163&r1=2162&r2=2163&view=diff
==============================================================================
--- trunk/examples/resetdb.cpp (original)
+++ trunk/examples/resetdb.cpp Thu Feb  7 04:03:01 2008
@@ -5,7 +5,7 @@
        the examples modify the table in this database.
 
  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
- MySQL AB, and (c) 2004-2007 by Educational Technology Resources, Inc.
+ MySQL AB, and (c) 2004-2008 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.
 
@@ -159,12 +159,15 @@
                // the first row is a UTF-8 encoded Unicode string!  All you
                // have to do to store Unicode data in recent versions of MySQL
                // is use UTF-8 encoding.
-               cout << "Populating stock table..." << endl;
+               cout << "Populating stock table..." << flush;
                query.execute("Nürnberger Brats", 97, 1.5, 8.79, "2005-03-10");
                query.execute("Pickle Relish", 87, 1.5, 1.75, "1998-09-04");
                query.execute("Hot Mustard", 73, .95, .97, "1998-05-25",
                 "good American yellow mustard, not that European stuff");
                query.execute("Hotdog Buns", 65, 1.1, 1.1, "1998-04-23");
+
+               // Test that above did what we wanted.
+               cout << "inserted " << con.count_rows("stock") << " rows." << 
endl;
 
                // Now create empty images table, for testing BLOB and auto-
                // increment column features.
@@ -191,19 +194,19 @@
        }
        catch (const mysqlpp::BadQuery& er) {
                // Handle any query errors
-               cerr << "Query error: " << er.what() << endl;
+               cerr << endl << "Query error: " << er.what() << endl;
                return 1;
        }
        catch (const mysqlpp::BadConversion& er) {
                // Handle bad conversions
-               cerr << "Conversion error: " << er.what() << endl <<
+               cerr << endl << "Conversion error: " << er.what() << endl <<
                                "\tretrieved data size: " << er.retrieved <<
                                ", actual size: " << er.actual_size << endl;
                return 1;
        }
        catch (const mysqlpp::Exception& er) {
                // Catch-all for any other MySQL++ exceptions
-               cerr << "Error: " << er.what() << endl;
+               cerr << endl << "Error: " << er.what() << endl;
                return 1;
        }
 

Modified: trunk/lib/connection.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.cpp?rev=2163&r1=2162&r2=2163&view=diff
==============================================================================
--- trunk/lib/connection.cpp (original)
+++ trunk/lib/connection.cpp Thu Feb  7 04:03:01 2008
@@ -120,6 +120,17 @@
        error_message_.clear();
        set_exceptions(other.throw_exceptions());
        driver_->copy(*other.driver_);
+}
+
+
+ulonglong
+Connection::count_rows(const std::string& table)
+{
+       error_message_.clear();
+       Query q(this, throw_exceptions());
+       q << "SELECT COUNT(*) FROM " << table;
+       StoreQueryResult res = q.store();
+       return res[0][0];
 }
 
 

Modified: trunk/lib/connection.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.h?rev=2163&r1=2162&r2=2163&view=diff
==============================================================================
--- trunk/lib/connection.h (original)
+++ trunk/lib/connection.h Thu Feb  7 04:03:01 2008
@@ -132,10 +132,18 @@
                        const char* user = 0, const char* password = 0,
                        unsigned int port = 0);
 
-       /// \brief return true if connection was established successfully
+       /// \brief Returns true if connection was established successfully
        ///
        /// \return true if connection was established successfully
        bool connected() const;
+
+       /// \brief Returns the number of rows in a table
+       ///
+       /// \param table name of table whose rows you want counted
+       ///
+       /// This is syntactic sugar for a \c SELECT \c COUNT(*)
+       /// \c FROM \c table SQL query.
+       ulonglong count_rows(const std::string& table);
 
        /// \brief Ask the database server to create a database
        ///


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

Reply via email to