insertpolicy.h
Go to the documentation of this file.
1 
32 /***********************************************************************
33  Copyright © 2008-2009 by AboveNet, Inc., and © 2009 by Educational
34  Technology Resources, Inc. Others may also hold copyrights on code
35  in this file. See the CREDITS file in the top directory of the
36  distribution for details.
37 
38  This file is part of MySQL++.
39 
40  MySQL++ is free software; you can redistribute it and/or modify it
41  under the terms of the GNU Lesser General Public License as published
42  by the Free Software Foundation; either version 2.1 of the License, or
43  (at your option) any later version.
44 
45  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
46  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
48  License for more details.
49 
50  You should have received a copy of the GNU Lesser General Public
51  License along with MySQL++; if not, write to the Free Software
52  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
53  USA
54 ***********************************************************************/
55 
56 #if !defined(MYSQLPP_INSERTPOLICY_H)
57 #define MYSQLPP_INSERTPOLICY_H
58 
59 // Only allow these templates to be defined if they're being bodily
60 // included into class Query's definition. They're in this separate
61 // file only for claity, so they don't get lost among the other
62 // definitions in class Query. Without this, Doxygen will pick them
63 // up and treat them as a) outside namespace mysqlpp; and b) not member
64 // templates of class Query. We also don't want end-users of the
65 // library to #include this file in their own code; these templates
66 // should only be used as mysqlpp::Query::*.
67 #if defined(MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES)
68 
69 
76 template <class AccessController = Transaction>
77 class MYSQLPP_EXPORT RowCountInsertPolicy
78 {
79 public:
81  RowCountInsertPolicy(unsigned int rows) :
82  cur_rows_(0),
83  max_rows_(rows)
84  {
85  }
86 
88  ~RowCountInsertPolicy() { }
89 
94  template <class RowT>
95  bool can_add(int, const RowT&)
96  {
97  if (++cur_rows_ > max_rows_) {
98  cur_rows_ = 0;
99  return false;
100  }
101  else {
102  return true;
103  }
104  }
105 
107  typedef AccessController access_controller;
108 
109 private:
110  unsigned int cur_rows_;
111  unsigned const int max_rows_;
112 };
113 
114 
122 template <class AccessController = Transaction>
123 class MYSQLPP_EXPORT SizeThresholdInsertPolicy
124 {
125 public:
127  SizeThresholdInsertPolicy(int size) :
128  size_(size)
129  {
130  }
131 
133  ~SizeThresholdInsertPolicy() { }
134 
142  template <class RowT>
143  bool can_add(int size, const RowT& object) const
144  {
145  return (size < size_);
146  }
147 
149  typedef AccessController access_controller;
150 
151 private:
152  int size_;
153 };
154 
155 
163 template <class AccessController = Transaction>
164 class MYSQLPP_EXPORT MaxPacketInsertPolicy
165 {
166 public:
172  MaxPacketInsertPolicy(Connection* con, int size) :
173  conn_(con), size_(size)
174  {
175  }
176 
185  MaxPacketInsertPolicy(int size) :
186  conn_(0), size_(size)
187  {
188  }
189 
191  ~MaxPacketInsertPolicy() { }
192 
200  template <class RowT>
201  bool can_add(int size, const RowT& object) const
202  {
203  if (size < size_) {
204  // Haven't hit size threshold yet, so see if this next
205  // item pushes it over the line.
206  SQLStream s(conn_);
207  s << ",(" << object.value_list() << ")";
208  return (size_ - size) >= static_cast<int>(s.str().size());
209  }
210  else {
211  // Already too much in query buffer!
212  return false;
213  }
214  }
215 
217  typedef AccessController access_controller;
218 
219 private:
220  Connection* conn_;
221  int size_;
222 };
223 
224 #endif // defined(MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES)
225 
226 #endif // !defined(MYSQLPP_INSERTPOLICY_H)
227