MySQL++: insertpolicy.h Source File

MySQL++ 3.2.5

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 (void)object; // we don't use this, but other policies do
146 return (size < size_);
147 }
148
150 typedef AccessController access_controller;
151
152 private:
153 int size_;
154 };
155
156
164 template <class AccessController = Transaction>
165 class MYSQLPP_EXPORT MaxPacketInsertPolicy
166 {
167 public:
173 MaxPacketInsertPolicy(Connection* con, int size) :
174 conn_(con), size_(size)
175 {
176 }
177
186 MaxPacketInsertPolicy(int size) :
187 conn_(0), size_(size)
188 {
189 }
190
192 ~MaxPacketInsertPolicy() { }
193
201 template <class RowT>
202 bool can_add(int size, const RowT& object) const
203 {
204 if (size < size_) {
205 // Haven't hit size threshold yet, so see if this next
206 // item pushes it over the line.
207 SQLStream s(conn_);
208 s << ",(" << object.value_list() << ")";
209 return (size_ - size) >= static_cast<int>(s.str().size());
210 }
211 else {
212 // Already too much in query buffer!
213 return false;
214 }
215 }
216
218 typedef AccessController access_controller;
219
220 private:
221 Connection* conn_;
222 int size_;
223 };
224
225 #endif // defined(MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES)
226
227 #endif // !defined(MYSQLPP_INSERTPOLICY_H)
228