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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/// \file row.h
/// \brief Declares the classes for holding row data from a result set.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2008 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
***********************************************************************/
#if !defined(MYSQLPP_ROW_H)
#define MYSQLPP_ROW_H
#include "common.h"
#include "mystring.h"
#include "noexceptions.h"
#include "refcounted.h"
#include "vallist.h"
#include <vector>
#include <string>
namespace mysqlpp {
#if !defined(DOXYGEN_IGNORE)
// Make Doxygen ignore this
class FieldNames;
class MYSQLPP_EXPORT ResultBase;
#endif
/// \brief Manages rows from a result set.
///
/// This class is like an extended version of a \c const \c std::vector
/// of mysqlpp::String. It adds stuff for populating the vector. As
/// for why it's \c const, what would it mean to modify a Row? If we
/// ever did support such semantics, it should probably actually modify
/// the database. We can't do that if we just derive from std::vector.
///
/// Not that we could derive from std::vector even if we wanted to:
/// \c vector::operator[](size_type) would interfere with our
/// \c operator[](const \c char*). We can avoid this only by
/// maintaining our own public inteface independent of that of
/// \c vector.
class MYSQLPP_EXPORT Row : public OptionalExceptions
{
private:
/// \brief Pointer to bool data member, for use by safe bool
/// conversion operator.
///
/// \see http://www.artima.com/cppsource/safebool.html
typedef bool Row::*private_bool_type;
public:
/// \brief type of our internal data list
///
/// This is public because all other typedefs we have for
/// mirroring std::vector's public interface depend on it.
typedef std::vector<String> list_type;
/// \brief constant iterator type
typedef list_type::const_iterator const_iterator;
/// \brief constant reference type
typedef list_type::const_reference const_reference;
/// \brief const reverse iterator type
typedef list_type::const_reverse_iterator const_reverse_iterator;
/// \brief type for index differences
typedef list_type::difference_type difference_type;
/// \brief iterator type
///
/// Note that this is just an alias for the const iterator. Row
/// is immutable, but people are in the habit of saying 'iterator'
/// even when they don't intend to use the iterator to modify the
/// container, so we provide this as a convenience.
typedef const_iterator iterator;
/// \brief reference type
///
/// \sa iterator for justification for this const_reference alias
typedef const_reference reference;
/// \brief mutable reverse iterator type
///
/// \sa iterator for justification for this const_reverse_iterator
/// alias
typedef const_reverse_iterator reverse_iterator;
/// \brief type of returned sizes
typedef list_type::size_type size_type;
/// \brief type of data in container
typedef list_type::value_type value_type;
/// \brief Default constructor
Row() :
initialized_(false)
{
}
/// \brief Copy constructor
Row(const Row& r) :
OptionalExceptions(),
data_(r.data_.begin(), r.data_.end()),
field_names_(r.field_names_),
initialized_(r.initialized_)
{
}
/// \brief Create a row object
///
/// \param row MySQL C API row data
/// \param res result set that the row comes from
/// \param lengths length of each item in row
/// \param te if true, throw exceptions on errors
Row(MYSQL_ROW row, const ResultBase* res,
const unsigned long* lengths, bool te = true);
/// \brief Destroy object
~Row() { }
/// \brief Get a const reference to the field given its index
///
/// \throw mysqlpp::BadIndex if the row is not initialized or there
/// are less than \c i fields in the row.
const_reference at(size_type i) const;
/// \brief Get a reference to the last element of the vector
const_reference back() const { return data_.back(); }
/// \brief Return a const iterator pointing to first element in the
/// container
const_iterator begin() const { return data_.begin(); }
/// \brief Returns true if container is empty
bool empty() const { return data_.empty(); }
/// \brief Return a const iterator pointing to one past the last
/// element in the container
const_iterator end() const { return data_.end(); }
/// \brief Get an "equal list" of the fields and values in this row
///
/// When inserted into a C++ stream, the delimiter 'd' will be used
/// between the items, " = " is the relationship operator, and items
/// will be quoted and escaped.
equal_list_ba<FieldNames, Row, quote_type0>
equal_list(const char* d = ",", const char* e = " = ") const;
/// \brief Get an "equal list" of the fields and values in this row
///
/// This method's parameters govern how the returned list will
/// behave when you insert it into a C++ stream:
///
/// \param d delimiter to use between items
/// \param e the operator to use between elements
/// \param m the manipulator to use for each element
///
/// For example, if d is ",", e is " = ", and m is the quote
/// manipulator, then the field and value lists (a, b) (c, d'e)
/// will yield an equal list that gives the following when inserted
/// into a C++ stream:
///
/// \code
/// 'a' = 'c', 'b' = 'd''e'
/// \endcode
///
/// Notice how the single quote was 'escaped' in the SQL way to
/// avoid a syntax error.
template <class Manip>
equal_list_ba<FieldNames, Row, Manip> equal_list(const char* d,
const char* e, Manip m) const;
/// \brief Get a list of the field names in this row
///
/// When inserted into a C++ stream, the delimiter 'd' will be used
/// between the items, and no manipulator will be used on the items.
value_list_ba<FieldNames, do_nothing_type0>
field_list(const char* d = ",") const;
/// \brief Get a list of the field names in this row
///
/// \param d delimiter to place between the items when the list is
/// inserted into a C++ stream
/// \param m manipulator to use before each item when the list is
/// inserted into a C++ stream
template <class Manip>
value_list_ba<FieldNames, Manip> field_list(const char* d,
Manip m) const;
/// \brief Get a list of the field names in this row
///
/// \param d delimiter to place between the items when the list is
/// inserted into a C++ stream
/// \param m manipulator to use before each item when the list is
/// inserted into a C++ stream
/// \param vb for each true item in this list, add that field name
/// to the returned list; ignore the others
template <class Manip>
value_list_b<FieldNames, Manip> field_list(const char* d, Manip m,
const std::vector<bool>& vb) const;
/// \brief Get a list of the field names in this row
///
/// \param d delimiter to place between the items when the list is
/// inserted into a C++ stream
/// \param vb for each true item in this list, add that field name
/// to the returned list; ignore the others
///
/// Field names will be quoted and escaped when inserted into a C++
/// stream.
value_list_b<FieldNames, quote_type0> field_list(
const char* d, const std::vector<bool>& vb) const;
/// \brief Get a list of the field names in this row
///
/// \param vb for each true item in this list, add that field name
/// to the returned list; ignore the others
///
/// Field names will be quoted and escaped when inserted into a C++
/// stream, and a comma will be placed between them as a delimiter.
value_list_b<FieldNames, quote_type0> field_list(
const std::vector<bool>& vb) const;
/// \brief Get a list of the field names in this row
///
/// For each true parameter, the field name in that position within
/// the row is added to the returned list. When the list is
/// inserted into a C++ stream, the delimiter 'd' will be placed
/// between the items as a delimiter, and the manipulator 'm' used
/// before each item.
template <class Manip>
value_list_b<FieldNames, Manip> field_list(const char *d, Manip m,
bool t0,
bool t1 = false, bool t2 = false, bool t3 = false,
bool t4 = false, bool t5 = false, bool t6 = false,
bool t7 = false, bool t8 = false, bool t9 = false,
bool ta = false, bool tb = false, bool tc = false) const;
/// \brief Get a list of the field names in this row
///
/// For each true parameter, the field name in that position within
/// the row is added to the returned list. When the list is
/// inserted into a C++ stream, the delimiter 'd' will be placed
/// between the items as a delimiter, and the items will be quoted
/// and escaped.
value_list_b<FieldNames, quote_type0> field_list(
const char *d, bool t0,
bool t1 = false, bool t2 = false, bool t3 = false,
bool t4 = false, bool t5 = false, bool t6 = false,
bool t7 = false, bool t8 = false, bool t9 = false,
bool ta = false, bool tb = false, bool tc = false) const;
/// \brief Get a list of the field names in this row
///
/// For each true parameter, the field name in that position within
/// the row is added to the returned list. When the list is
/// inserted into a C++ stream, a comma will be placed between the
/// items as a delimiter, and the items will be quoted and escaped.
value_list_b<FieldNames, quote_type0> field_list(
bool t0,
bool t1 = false, bool t2 = false, bool t3 = false,
bool t4 = false, bool t5 = false, bool t6 = false,
bool t7 = false, bool t8 = false, bool t9 = false,
bool ta = false, bool tb = false, bool tc = false) const;
/// \brief Returns a field's index given its name
size_type field_num(const char* name) const;
/// \brief Get a reference to the first element of the vector
const_reference front() const { return data_.front(); }
/// \brief Return maximum number of elements that can be stored
/// in container without resizing.
size_type max_size() const { return data_.max_size(); }
/// \brief Assignment operator
Row& operator =(const Row& rhs)
{
data_.assign(rhs.data_.begin(), rhs.data_.end());
field_names_.assign(rhs.field_names_);
initialized_ = rhs.initialized_;
return *this;
}
/// \brief Get the value of a field given its name.
///
/// If the field does not exist in this row, we throw a BadFieldName
/// exception if exceptions are enabled, or an empty row if not.
/// An empty row tests as false in bool context.
///
/// This operator is fairly inefficient. operator[](int) is faster.
const_reference operator [](const char* field) const;
/// \brief Get the value of a field given its index.
///
/// This function is just syntactic sugar, wrapping the at() method.
///
/// It's \b critical that the parameter type be \c int, not
/// \c size_type, because it will interfere with the \c const
/// \c char* overload otherwise. row[0] is ambiguous when there
/// isn't an int overload.
///
/// \throw mysqlpp::BadIndex if the row is not initialized or there
/// are less than \c i fields in the row.
const_reference operator [](int i) const
{ return at(static_cast<size_type>(i)); }
/// \brief Returns true if row object was fully initialized and
/// has data.
///
/// This operator lets you use Row in bool context, which lets you
/// do things like tell when you've run off the end of a "use"
/// query's result set:
///
/// \code
/// Query q("....");
/// if (UseQueryResult res = q.use()) {
/// // Can use 'res', query succeeded
/// while (Row row = res.fetch_row()) {
/// // Retreived another row in the result set, can use 'row'
/// }
/// }
/// \endcode
///
operator private_bool_type() const
{
return data_.size() && initialized_ ? &Row::initialized_ : 0;
}
/// \brief Return reverse iterator pointing to first element in the
/// container
const_reverse_iterator rbegin() const { return data_.rbegin(); }
/// \brief Return reverse iterator pointing to one past the last
/// element in the container
const_reverse_iterator rend() const { return data_.rend(); }
/// \brief Get the number of fields in the row.
size_type size() const { return data_.size(); }
/// \brief Get a list of the values in this row
///
/// When inserted into a C++ stream, the delimiter 'd' will be used
/// between the items, and the quoting and escaping rules will be
/// set by the manipulator 'm' you choose.
///
/// \param d delimiter to use between values
/// \param m manipulator to use when inserting values into a stream
template <class Manip>
value_list_ba<Row, Manip> value_list(const char* d = ",",
Manip m = quote) const
{
return value_list_ba<Row, Manip>(*this, d, m);
}
/// \brief Get a list of the values in this row
///
/// \param d delimiter to use between values
/// \param vb for each true item in this list, add that value to the
/// returned list; ignore the others
/// \param m manipulator to use when inserting values into a stream
template <class Manip>
value_list_b<Row, Manip> value_list(const char *d,
const std::vector<bool>& vb, Manip m = quote) const
{
return value_list_b<Row, Manip>(*this, vb, d, m);
}
/// \brief Get a list of the values in this row
///
/// \param vb for each true item in this list, add that value to the
/// returned list; ignore the others
///
/// Items will be quoted and escaped when inserted into a C++ stream,
/// and a comma will be used as a delimiter between the items.
value_list_b<Row, quote_type0> value_list(
const std::vector<bool> &vb) const
{
return value_list_b<Row, quote_type0>(*this, vb, ",", quote);
}
/// \brief Get a list of the values in this row
///
/// For each true parameter, the value in that position within the
/// row is added to the returned list. When the list is inserted
/// into a C++ stream, the delimiter 'd' will be placed between the
/// items, and the manipulator 'm' used before each item.
template <class Manip>
value_list_b<Row, Manip> value_list(const char *d, Manip m,
bool t0, bool t1 = false, bool t2 = false, bool t3 = false,
bool t4 = false, bool t5 = false, bool t6 = false,
bool t7 = false, bool t8 = false, bool t9 = false,
bool ta = false, bool tb = false, bool tc = false) const
{
std::vector<bool> vb;
create_vector(size(), vb, t0, t1, t2, t3, t4, t5, t6,
t7, t8, t9, ta, tb, tc);
return value_list_b<Row, Manip>(*this, vb, d, m);
}
/// \brief Get a list of the values in this row
///
/// For each true parameter, the value in that position within the
/// row is added to the returned list. When the list is inserted
/// into a C++ stream, the delimiter 'd' will be placed between the
/// items, and items will be quoted and escaped.
value_list_b <Row, quote_type0>
value_list(const char *d, bool t0, bool t1 = false, bool t2 = false,
bool t3 = false, bool t4 = false, bool t5 = false,
bool t6 = false, bool t7 = false, bool t8 = false,
bool t9 = false, bool ta = false, bool tb = false,
bool tc = false) const
{
std::vector<bool> vb;
create_vector(size(), vb, t0, t1, t2, t3, t4, t5, t6,
t7, t8, t9, ta, tb, tc);
return value_list_b<Row, quote_type0>(*this, vb, d, quote);
}
/// \brief Get a list of the values in this row
///
/// For each true parameter, the value in that position within the
/// row is added to the returned list. When the list is inserted
/// into a C++ stream, the a comma will be placed between the items,
/// as a delimiter, and items will be quoted and escaped.
value_list_b<Row, quote_type0> value_list(bool t0,
bool t1 = false, bool t2 = false, bool t3 = false,
bool t4 = false, bool t5 = false, bool t6 = false,
bool t7 = false, bool t8 = false, bool t9 = false,
bool ta = false, bool tb = false, bool tc = false) const
{
std::vector<bool> vb;
create_vector(size(), vb, t0, t1, t2, t3, t4, t5, t6,
t7, t8, t9, ta, tb, tc);
return value_list_b<Row, quote_type0>(*this, vb, ",", quote);
}
/// \brief Get a list of the values in this row
///
/// The 's' parameters name the fields that will be added to the
/// returned list. When inserted into a C++ stream, the delimiter
/// 'd' will be placed between the items, and the manipulator 'm'
/// will be inserted before each item.
template <class Manip>
value_list_b<Row, Manip> value_list(const char *d, Manip m,
std::string s0, std::string s1 = "", std::string s2 = "",
std::string s3 = "", std::string s4 = "",
std::string s5 = "", std::string s6 = "",
std::string s7 = "", std::string s8 = "",
std::string s9 = "", std::string sa = "",
std::string sb = "", std::string sc = "") const
{
std::vector<bool> vb;
create_vector(*this, vb, s0, s1, s2, s3, s4, s5, s6, s7, s8,
s9, sa, sb, sc);
return value_list_b<Row, Manip>(*this, vb, d, m);
}
/// \brief Get a list of the values in this row
///
/// The 's' parameters name the fields that will be added to the
/// returned list. When inserted into a C++ stream, the delimiter
/// 'd' will be placed between the items, and items will be quoted
/// and escaped.
value_list_b<Row, quote_type0> value_list(
const char *d,
std::string s0, std::string s1 = "", std::string s2 = "",
std::string s3 = "", std::string s4 = "",
std::string s5 = "", std::string s6 = "",
std::string s7 = "", std::string s8 = "",
std::string s9 = "", std::string sa = "",
std::string sb = "", std::string sc = "") const
{
std::vector<bool> vb;
create_vector(*this, vb, s0, s1, s2, s3, s4, s5, s6, s7, s8,
s9, sa, sb, sc);
return value_list_b<Row, quote_type0>(*this, vb, d, quote);
}
/// \brief Get a list of the values in this row
///
/// The 's' parameters name the fields that will be added to the
/// returned list. When inserted into a C++ stream, a comma will be
/// placed between the items as a delimiter, and items will be
/// quoted and escaped.
value_list_b<Row, quote_type0> value_list(
std::string s0,
std::string s1 = "", std::string s2 = "",
std::string s3 = "", std::string s4 = "",
std::string s5 = "", std::string s6 = "",
std::string s7 = "", std::string s8 = "",
std::string s9 = "", std::string sa = "",
std::string sb = "", std::string sc = "") const
{
std::vector<bool> vb;
create_vector(*this, vb, s0, s1, s2, s3, s4, s5, s6, s7, s8,
s9, sa, sb, sc);
return value_list_b<Row, quote_type0>(*this, vb, ",", quote);
}
private:
list_type data_;
RefCountedPointer<FieldNames> field_names_;
bool initialized_;
};
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_ROW_H)
|