insertpolicy.h
Go to the documentation of this file.
/***********************************************************************
Copyright © 2008-2009 by AboveNet, Inc., and © 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++.
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
***********************************************************************/
#if !defined(MYSQLPP_INSERTPOLICY_H)
#define MYSQLPP_INSERTPOLICY_H
// Only allow these templates to be defined if they're being bodily
// included into class Query's definition. They're in this separate
// file only for claity, so they don't get lost among the other
// definitions in class Query. Without this, Doxygen will pick them
// up and treat them as a) outside namespace mysqlpp; and b) not member
// templates of class Query. We also don't want end-users of the
// library to #include this file in their own code; these templates
// should only be used as mysqlpp::Query::*.
#if defined(MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES)
template <class AccessController = Transaction>
class MYSQLPP_EXPORT RowCountInsertPolicy
{
public:
RowCountInsertPolicy(unsigned int rows) :
cur_rows_(0),
max_rows_(rows)
{
}
~RowCountInsertPolicy() { }
template <class RowT>
bool can_add(int, const RowT&)
{
if (++cur_rows_ > max_rows_) {
cur_rows_ = 0;
return false;
}
else {
return true;
}
}
typedef AccessController access_controller;
private:
unsigned int cur_rows_;
unsigned const int max_rows_;
};
template <class AccessController = Transaction>
class MYSQLPP_EXPORT SizeThresholdInsertPolicy
{
public:
SizeThresholdInsertPolicy(int size) :
size_(size)
{
}
~SizeThresholdInsertPolicy() { }
template <class RowT>
bool can_add(int size, const RowT& object) const
{
(void)object; // we don't use this, but other policies do
return (size < size_);
}
typedef AccessController access_controller;
private:
int size_;
};
template <class AccessController = Transaction>
class MYSQLPP_EXPORT MaxPacketInsertPolicy
{
public:
MaxPacketInsertPolicy(Connection* con, int size) :
conn_(con), size_(size)
{
}
MaxPacketInsertPolicy(int size) :
conn_(0), size_(size)
{
}
~MaxPacketInsertPolicy() { }
template <class RowT>
bool can_add(int size, const RowT& object) const
{
if (size < size_) {
// Haven't hit size threshold yet, so see if this next
// item pushes it over the line.
SQLStream s(conn_);
s << ",(" << object.value_list() << ")";
return (size_ - size) >= static_cast<int>(s.str().size());
}
else {
// Already too much in query buffer!
return false;
}
}
typedef AccessController access_controller;
private:
Connection* conn_;
int size_;
};
#endif // defined(MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES)
#endif // !defined(MYSQLPP_INSERTPOLICY_H)