Author: wyoung
Date: Fri Feb 27 20:04:30 2009
New Revision: 2462

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2462&view=rev
Log:
Created RowCountInsertPolicy, and a test for it.

Added:
    trunk/test/insertpolicy.cpp
Modified:
    trunk/Wishlist
    trunk/lib/insertpolicy.h
    trunk/mysql++.bkl

Modified: trunk/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=2462&r1=2461&r2=2462&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Fri Feb 27 20:04:30 2009
@@ -9,8 +9,6 @@
 
     o Suppress DOS line-ending related diffs from examples/cgi_jpeg
       output when running dtest on Windows.  Check for -D?
-
-  o Write RowCountInsertPolicy
   
 
 v3.1 Tentative Plan

Modified: trunk/lib/insertpolicy.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/insertpolicy.h?rev=2462&r1=2461&r2=2462&view=diff
==============================================================================
--- trunk/lib/insertpolicy.h (original)
+++ trunk/lib/insertpolicy.h Fri Feb 27 20:04:30 2009
@@ -30,9 +30,10 @@
 /// Query::insertfrom().  You access it as Query::InsertPolicy<T>
 
 /***********************************************************************
- Copyright (c) 2008-2009 by AboveNet, Inc.  Others may also hold
- copyrights on code in this file.  See the CREDITS file in the top
- directory of the distribution for details.
+ Copyright (c) 2008-2009 by AboveNet, Inc., and (c) 2009 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.
 
  This file is part of MySQL++.
 
@@ -55,7 +56,52 @@
 #if !defined(MYSQLPP_INSERTPOLICY_H)
 #define MYSQLPP_INSERTPOLICY_H
 
-/// \brief A policy object that triggers a new INSERT statement
+/// \brief An insert policy object that triggers a new INSERT
+/// statement after a given number of rows have been inserted.
+///
+/// This policy is very lightweight, but is only reliable when you
+/// can predict the size of each INSERT in advance.  The others do
+/// more processing to reduce the risk of unpredictable row sizes.
+template <class AccessController = Transaction>
+class MYSQLPP_EXPORT RowCountInsertPolicy
+{
+public:
+       /// \brief Constructor
+       RowCountInsertPolicy(unsigned int rows) :
+       cur_rows_(0),
+       max_rows_(rows)
+       {
+       }
+
+       /// \brief Destructor
+       ~RowCountInsertPolicy() { }
+
+       /// \brief Can we add another object to the query?
+       ///
+       /// \retval true if the object is allowed to be added to the
+       /// INSERT statement
+       template <class RowT>
+       bool can_add(int size, const RowT& object)
+       {
+               if (++cur_rows_ > max_rows_) {
+                       cur_rows_ = 0;
+                       return false;
+               }
+               else {
+                       return true;
+               }
+       }
+
+       /// \brief Alias for our access controller type
+       typedef AccessController access_controller;
+
+private:
+       unsigned int cur_rows_;
+       unsigned const int max_rows_;
+};
+
+
+/// \brief An insert policy object that triggers a new INSERT statement
 /// after a size threshold for the length of the INSERT statement
 /// is exceeded.
 ///
@@ -96,7 +142,7 @@
 };
 
 
-/// \brief A policy object that triggers a new INSERT statement
+/// \brief An insert policy object that triggers a new INSERT statement
 /// if the object to be added would cause the statement to exceed
 /// a maximum size.
 ///

Modified: trunk/mysql++.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=2462&r1=2461&r2=2462&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Fri Feb 27 20:04:30 2009
@@ -208,6 +208,9 @@
     <exe id="test_inttypes" template="programs">
       <sources>test/inttypes.cpp</sources>
     </exe>
+    <exe id="test_insertpolicy" template="programs">
+      <sources>test/insertpolicy.cpp</sources>
+    </exe>
     <exe id="test_manip" template="programs">
       <sources>test/manip.cpp</sources>
     </exe>

Added: trunk/test/insertpolicy.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/test/insertpolicy.cpp?rev=2462&view=auto
==============================================================================
--- trunk/test/insertpolicy.cpp (added)
+++ trunk/test/insertpolicy.cpp Fri Feb 27 20:04:30 2009
@@ -1,0 +1,104 @@
+/***********************************************************************
+ test/insertpolicy.cpp - Checks that the *InsertPolicy objects work
+       as expected.
+
+ Copyright (c) 2009 by Educational Technology Resources, Inc.
+ Others may also hold copyrights on code in this file.  See the
+ CREDITS.txt 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 <mysql++.h>
+
+#include <iostream>
+
+static const unsigned char nonzero = 4;
+
+
+template <class InsertPolicy>
+static bool
+test_policy(InsertPolicy& ip, unsigned char max, bool expected_to_fail)
+{
+       unsigned char i;
+       mysqlpp::Row dummy;
+       for (i = 0; i < UCHAR_MAX; ++i) {
+               if (!ip.can_add(i, dummy)) {
+                       break;
+               }
+       }
+
+       if (expected_to_fail ? i != max : i == max) {
+               return true;
+       }
+       else {
+               std::cerr << typeid(ip).name() << '(' << int(max) << 
+                               ") allowed " << int(i) << " inserts!" << 
std::endl;
+               return false;
+       }
+}
+
+
+template <class InsertPolicy>
+static bool
+test_policy(InsertPolicy& ip, unsigned char expected_allow_count)
+{
+       return  test_policy(ip, expected_allow_count, false) &&
+                       test_policy(ip, expected_allow_count + 1, true) &&
+                       test_policy(ip, expected_allow_count - 1, true) &&
+                       test_policy(ip, expected_allow_count ? 0 : nonzero, 
true);
+}
+
+
+static bool
+test_row_count_nonzero()
+{
+       mysqlpp::Query::RowCountInsertPolicy<> ip_nonzero(nonzero);
+       return test_policy(ip_nonzero, nonzero);
+}
+
+
+static bool
+test_row_count_zero()
+{
+       mysqlpp::Query::RowCountInsertPolicy<> ip_zero(0);
+       return test_policy(ip_zero, 0);
+}
+
+
+static bool
+test_row_count()
+{
+       return  test_row_count_nonzero() &&
+                       test_row_count_zero();
+}
+
+
+int
+main()
+{
+       try {
+               return test_row_count() ? 0 : 1;
+       }
+       catch (...) {
+               std::cerr << "Unhandled exception caught by "
+                               "test/insertpolicy!" << std::endl;
+               return 2;
+       }
+}
+


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

Reply via email to