insertpolicy.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 /***********************************************************************
00033  Copyright (c) 2008-2009 by AboveNet, Inc., and (c) 2009 by Educational
00034  Technology Resources, Inc.  Others may also hold copyrights on code
00035  in this file.  See the CREDITS file in the top directory of the
00036  distribution for details.
00037 
00038  This file is part of MySQL++.
00039 
00040  MySQL++ is free software; you can redistribute it and/or modify it
00041  under the terms of the GNU Lesser General Public License as published
00042  by the Free Software Foundation; either version 2.1 of the License, or
00043  (at your option) any later version.
00044 
00045  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
00046  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00047  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00048  License for more details.
00049 
00050  You should have received a copy of the GNU Lesser General Public
00051  License along with MySQL++; if not, write to the Free Software
00052  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00053  USA
00054 ***********************************************************************/
00055 
00056 #if !defined(MYSQLPP_INSERTPOLICY_H)
00057 #define MYSQLPP_INSERTPOLICY_H
00058 
00065 template <class AccessController = Transaction>
00066 class MYSQLPP_EXPORT RowCountInsertPolicy
00067 {
00068 public:
00070         RowCountInsertPolicy(unsigned int rows) :
00071         cur_rows_(0),
00072         max_rows_(rows)
00073         {
00074         }
00075 
00077         ~RowCountInsertPolicy() { }
00078 
00083         template <class RowT>
00084         bool can_add(int, const RowT&)
00085         {
00086                 if (++cur_rows_ > max_rows_) {
00087                         cur_rows_ = 0;
00088                         return false;
00089                 }
00090                 else {
00091                         return true;
00092                 }
00093         }
00094 
00096         typedef AccessController access_controller;
00097 
00098 private:
00099         unsigned int cur_rows_;
00100         unsigned const int max_rows_;
00101 };
00102 
00103 
00111 template <class AccessController = Transaction>
00112 class MYSQLPP_EXPORT SizeThresholdInsertPolicy
00113 {
00114 public:
00116         SizeThresholdInsertPolicy(int size) :
00117         size_(size)
00118         {
00119         }
00120 
00122         ~SizeThresholdInsertPolicy() { }
00123 
00131         template <class RowT>
00132         bool can_add(int size, const RowT& object) const
00133         {
00134                 return (size < size_);
00135         }
00136 
00138         typedef AccessController access_controller;
00139 
00140 private:
00141         int size_;
00142 };
00143 
00144 
00152 template <class AccessController = Transaction>
00153 class MYSQLPP_EXPORT MaxPacketInsertPolicy
00154 {
00155 public:
00161         MaxPacketInsertPolicy(Connection* con, int size) :
00162         conn_(con), size_(size)
00163         {
00164         }
00165 
00174         MaxPacketInsertPolicy(int size) :
00175         conn_(0), size_(size)
00176         {
00177         }
00178 
00180         ~MaxPacketInsertPolicy() { }
00181 
00189         template <class RowT>
00190         bool can_add(int size, const RowT& object) const
00191         {
00192                 if (size < size_) {
00193                         // Haven't hit size threshold yet, so see if this next
00194                         // item pushes it over the line.
00195                         SQLStream s(conn_);
00196                         s << ",(" << object.value_list() << ")";
00197                         return (size_ - size) >= static_cast<int>(s.str().size());
00198                 }
00199                 else {
00200                         // Already too much in query buffer!
00201                         return false;
00202                 }
00203         }
00204 
00206         typedef AccessController access_controller;
00207 
00208 private:
00209         Connection* conn_;
00210         int size_;
00211 };
00212 
00213 #endif // !defined(MYSQLPP_INSERTPOLICY_H)
00214 

Generated on Thu Jun 3 11:59:12 2010 for MySQL++ by  doxygen 1.4.7