1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/// \file qparms.h
/// \brief Declares the template query parameter-related stuff.
///
/// The classes defined in this file are used by class Query when it
/// parses a template query: they hold information that it finds in the
/// template, so it can assemble a SQL statement later on demand.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2007 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
***********************************************************************/
#ifndef MYSQLPP_QPARMS_H
#define MYSQLPP_QPARMS_H
#include "stadapter.h"
#include <vector>
namespace mysqlpp {
#if !defined(DOXYGEN_IGNORE)
// Make Doxygen ignore this
class MYSQLPP_EXPORT Query;
#endif
/// \brief This class holds the parameter values for filling
/// template queries.
class MYSQLPP_EXPORT SQLQueryParms : public std::vector<SQLTypeAdapter>
{
public:
/// \brief Abbreviation so some of the declarations below don't
/// span many lines.
typedef const SQLTypeAdapter& sta;
/// \brief Default constructor
SQLQueryParms() :
parent_(0),
processing_(false)
{
}
/// \brief Create object
///
/// \param p pointer to the query object these parameters are tied
/// to
SQLQueryParms(Query* p) :
parent_(p),
processing_(false)
{
}
/// \brief Returns true if we are bound to a query object.
///
/// Basically, this tells you which of the two ctors were called.
bool bound() { return parent_ != 0; }
/// \brief Clears the list
void clear() { erase(begin(), end()); }
/// \brief Indirect access to Query::escape_string()
///
/// \internal Needed by \c operator<<(Manip&, \c const \c T&) where
/// \c Manip is used on a SQLQueryParms object. We'd have to make
/// all these operators friends to give access to our internal Query
/// object otherwise.
///
/// \see Query::escape_string(std::string*, const char*, size_t)
size_t escape_string(std::string* ps, const char* original = 0,
size_t length = 0) const;
/// \brief Indirect access to Query::escape_string()
///
/// \see escape_string(std::string*, const char*, size_t)
/// \see Query::escape_string(const char*, const char*, size_t)
size_t escape_string(char* escaped, const char* original,
size_t length) const;
/// \brief Access element number n
SQLTypeAdapter& operator [](size_type n)
{
if (n >= size()) {
insert(end(), (n + 1) - size(), "");
}
return std::vector<SQLTypeAdapter>::operator [](n);
}
/// \brief Access element number n
const SQLTypeAdapter& operator [](size_type n) const
{ return std::vector<SQLTypeAdapter>::operator [](n); }
/// \brief Access the value of the element with a key of str.
SQLTypeAdapter& operator [](const char *str);
/// \brief Access the value of the element with a key of str.
const SQLTypeAdapter& operator [](const char *str) const;
/// \brief Adds an element to the list
SQLQueryParms& operator <<(const SQLTypeAdapter& str)
{
push_back(str);
return *this;
}
/// \brief Adds an element to the list
SQLQueryParms& operator +=(const SQLTypeAdapter& str)
{
push_back(str);
return *this;
}
/// \brief Build a composite of two parameter lists
///
/// If this list is (a, b) and \c other is (c, d, e, f, g), then
/// the returned list will be (a, b, e, f, g). That is, all of this
/// list's parameters are in the returned list, plus any from the
/// other list that are in positions beyond what exist in this list.
///
/// If the two lists are the same length or this list is longer than
/// the \c other list, a copy of this list is returned.
SQLQueryParms operator +(
const SQLQueryParms& other) const;
#if !defined(DOXYGEN_IGNORE)
// Doxygen will not generate documentation for this section.
void set(sta a)
{
clear();
*this << a;
}
void set(sta a, sta b)
{
clear();
*this << a << b;
}
void set(sta a, sta b, sta c)
{
clear();
*this << a << b << c;
}
void set(sta a, sta b, sta c, sta d)
{
clear();
*this << a << b << c << d;
}
void set(sta a, sta b, sta c, sta d, sta e)
{
clear();
*this << a << b << c << d << e;
}
void set(sta a, sta b, sta c, sta d, sta e, sta f)
{
clear();
*this << a << b << c << d << e << f;
}
void set(sta a, sta b, sta c, sta d, sta e, sta f, sta g)
{
clear();
*this << a << b << c << d << e << f << g;
}
void set(sta a, sta b, sta c, sta d, sta e, sta f, sta g, sta h)
{
clear();
*this << a << b << c << d << e << f << g << h;
}
void set(sta a, sta b, sta c, sta d, sta e, sta f, sta g, sta h, sta i)
{
clear();
*this << a << b << c << d << e << f << g << h << i;
}
void set(sta a, sta b, sta c, sta d, sta e, sta f, sta g, sta h, sta i, sta j)
{
clear();
*this << a << b << c << d << e << f << g << h << i << j;
}
void set(sta a, sta b, sta c, sta d, sta e, sta f, sta g, sta h, sta i, sta j, sta k)
{
clear();
*this << a << b << c << d << e << f << g << h << i << j << k;
}
#endif // !defined(DOXYGEN_IGNORE)
/// \brief Set the template query parameters.
///
/// Sets parameter 0 to a, parameter 1 to b, etc. There are
/// overloaded versions of this function that take anywhere from
/// one to a dozen parameters.
void set(sta a, sta b, sta c, sta d, sta e, sta f, sta g,
sta h, sta i, sta j, sta k, sta l)
{
clear();
*this << a << b << c << d << e << f << g << h << i << j << k << l;
}
private:
friend class Query;
Query* parent_;
bool processing_; ///< true if we're building a query string
};
/// \brief Used within Query to hold elements for parameterized
/// queries.
///
/// Each element has three parts:
///
/// The concept behind the \c before variable needs a little explaining.
/// When a template query is parsed, each parameter is parsed into one
/// of these SQLParseElement objects, but the non-parameter parts of the
/// template also have to be stored somewhere. MySQL++ chooses to
/// attach the text leading up to a parameter to that parameter. So,
/// the \c before string is simply the text copied literally into the
/// finished query before we insert a value for the parameter.
///
/// The \c option character is currently one of 'q', 'Q', 'r', 'R' or
/// ' '. See the "Template Queries" chapter in the user manual for
/// details.
///
/// The position value (\c num) allows a template query to have its
/// parameters in a different order than in the Query method call.
/// An example of how this can be helpful is in the "Template Queries"
/// chapter of the user manual.
struct SQLParseElement
{
/// \brief Create object
///
/// \param b the 'before' value
/// \param o the 'option' value
/// \param n the 'num' value
SQLParseElement(std::string b, char o, signed char n) :
before(b),
option(o),
num(n)
{
}
std::string before; ///< string inserted before the parameter
char option; ///< the parameter option, or blank if none
signed char num; ///< the parameter position to use
};
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_QPARMS_H)
|