MySQL++ Reference Manual

- Main Page
- Classes
- Files

- File List
- File Members

# query.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004 /***********************************************************************
    00005  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
    00006  (c) 2004-2008 by Educational Technology Resources, Inc.  Others may
    00007  also hold copyrights on code in this file.  See the CREDITS.txt file
    00008  in the top directory of the distribution for details.
    00009
    00010  This file is part of MySQL++.
    00011
    00012  MySQL++ is free software; you can redistribute it and/or modify it
    00013  under the terms of the GNU Lesser General Public License as published
    00014  by the Free Software Foundation; either version 2.1 of the License, or
    00015  (at your option) any later version.
    00016
    00017  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00018  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00019  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00020  License for more details.
    00021
    00022  You should have received a copy of the GNU Lesser General Public
    00023  License along with MySQL++; if not, write to the Free Software
    00024  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00025  USA
    00026 ***********************************************************************/
    00027
    00028 #if !defined(MYSQLPP_QUERY_H)
    00029 #define MYSQLPP_QUERY_H
    00030
    00031 #include "common.h"
    00032
    00033 #include "noexceptions.h"
    00034 #include "qparms.h"
    00035 #include "querydef.h"
    00036 #include "result.h"
    00037 #include "row.h"
    00038 #include "stadapter.h"
    00039
    00040 #include <deque>
    00041 #include <iomanip>
    00042 #include <list>
    00043 #include <map>
    00044 #include <set>
    00045 #include <vector>
    00046
    00047 #ifdef HAVE_EXT_SLIST
    00048 #  include <ext/slist>
    00049 #else
    00050 #  if defined(HAVE_STD_SLIST) || defined(HAVE_GLOBAL_SLIST)
    00051 #      include <slist>
    00052 #  endif
    00053 #endif
    00054
    00055 namespace mysqlpp {
    00056
    00057 #if !defined(DOXYGEN_IGNORE)
    00058 // Make Doxygen ignore this
    00059 class MYSQLPP_EXPORT Connection;
    00060 #endif
    00061
    00116
    00117 class MYSQLPP_EXPORT Query :
    00118                 public std::ostream,
    00119                 public OptionalExceptions
    00120 {
    00121 public:
    00129         Query(Connection* c, bool te = true, const char* qstr = 0);
    00130
    00138         Query(const Query& q);
    00139
    00141         ulonglong affected_rows();
    00142
    00180         size_t escape_string(std::string* ps, const char* original = 0,
    00181                         size_t length = 0) const;
    00182
    00199         size_t escape_string(char* escaped, const char* original,
    00200                         size_t length) const;
    00201
    00206         int errnum() const;
    00207
    00212         const char* error() const;
    00213
    00216         std::string info();
    00217
    00228         ulonglong insert_id();
    00229
    00234         Query& operator=(const Query& rhs);
    00235
    00254         operator void*() const;
    00255
    00263         void parse();
    00264
    00274         void reset();
    00275
    00277         std::string str() { return str(template_defaults); }
    00278
    00292         std::string str(const SQLTypeAdapter& arg0)
    00293                         { return str(SQLQueryParms() << arg0); }
    00294
    00299         std::string str(SQLQueryParms& p);
    00300
    00311         bool exec() { return exec(str(template_defaults)); }
    00312
    00324         bool exec(const std::string& str);
    00325
    00342         SimpleResult execute() { return execute(str(template_defaults)); }
    00343
    00352         SimpleResult execute(SQLQueryParms& p);
    00353
    00370         SimpleResult execute(const SQLTypeAdapter& str);
    00371
    00376         SimpleResult execute(const char* str, size_t len);
    00377
    00403         UseQueryResult use() { return use(str(template_defaults)); }
    00404
    00414         UseQueryResult use(SQLQueryParms& p);
    00415
    00433         UseQueryResult use(const SQLTypeAdapter& str);
    00434
    00444         UseQueryResult use(const char* str, size_t len);
    00445
    00467         StoreQueryResult store() { return store(str(template_defaults)); }
    00468
    00477         StoreQueryResult store(SQLQueryParms& p);
    00478
    00496         StoreQueryResult store(const SQLTypeAdapter& str);
    00497
    00507         StoreQueryResult store(const char* str, size_t len);
    00508
    00519         template <typename Function>
    00520         Function for_each(const SQLTypeAdapter& query, Function fn)
    00521         {
    00522                 mysqlpp::UseQueryResult res = use(query);
    00523                 if (res) {
    00524                         mysqlpp::NoExceptions ne(res);
    00525                         while (mysqlpp::Row row = res.fetch_row()) {
    00526                                 fn(row);
    00527                         }
    00528                 }
    00529
    00530                 return fn;
    00531         }
    00532
    00540         template <typename Function>
    00541         Function for_each(Function fn)
    00542         {
    00543                 mysqlpp::UseQueryResult res = use();
    00544                 if (res) {
    00545                         mysqlpp::NoExceptions ne(res);
    00546                         while (mysqlpp::Row row = res.fetch_row()) {
    00547                                 fn(row);
    00548                         }
    00549                 }
    00550
    00551                 return fn;
    00552         }
    00553
    00564         template <class SSQLS, typename Function>
    00565         Function for_each(const SSQLS& ssqls, Function fn)
    00566         {
    00567                 std::string query("select * from ");
    00568                 query += ssqls.table();
    00569                 mysqlpp::UseQueryResult res = use(query);
    00570                 if (res) {
    00571                         mysqlpp::NoExceptions ne(res);
    00572                         while (mysqlpp::Row row = res.fetch_row()) {
    00573                                 fn(row);
    00574                         }
    00575                 }
    00576
    00577                 return fn;
    00578         }
    00579
    00599         template <class Sequence, typename Function>
    00600         Function store_if(Sequence& con, const SQLTypeAdapter& query, Function fn)
    00601         {
    00602                 mysqlpp::UseQueryResult res = use(query);
    00603                 if (res) {
    00604                         mysqlpp::NoExceptions ne(res);
    00605                         while (mysqlpp::Row row = res.fetch_row()) {
    00606                                 if (fn(row)) {
    00607                                         con.push_back(row);
    00608                                 }
    00609                         }
    00610                 }
    00611
    00612                 return fn;
    00613         }
    00614
    00626         template <class Sequence, class SSQLS, typename Function>
    00627         Function store_if(Sequence& con, const SSQLS& ssqls, Function fn)
    00628         {
    00629                 std::string query("select * from ");
    00630                 query += ssqls.table();
    00631                 mysqlpp::UseQueryResult res = use(query);
    00632                 if (res) {
    00633                         mysqlpp::NoExceptions ne(res);
    00634                         while (mysqlpp::Row row = res.fetch_row()) {
    00635                                 if (fn(row)) {
    00636                                         con.push_back(row);
    00637                                 }
    00638                         }
    00639                 }
    00640
    00641                 return fn;
    00642         }
    00643
    00653         template <class Sequence, typename Function>
    00654         Function store_if(Sequence& con, Function fn)
    00655         {
    00656                 mysqlpp::UseQueryResult res = use();
    00657                 if (res) {
    00658                         mysqlpp::NoExceptions ne(res);
    00659                         while (mysqlpp::Row row = res.fetch_row()) {
    00660                                 if (fn(row)) {
    00661                                         con.push_back(row);
    00662                                 }
    00663                         }
    00664                 }
    00665
    00666                 return fn;
    00667         }
    00668
    00695         StoreQueryResult store_next();
    00696
    00708         bool more_results();
    00709
    00726         template <class Sequence>
    00727         void storein_sequence(Sequence& con)
    00728         {
    00729                 storein_sequence(con, str(template_defaults));
    00730         }
    00731
    00745         template <class Sequence>
    00746         void storein_sequence(Sequence& con, const SQLTypeAdapter& s)
    00747         {
    00748                 UseQueryResult result = use(s);
    00749                 while (1) {
    00750                         MYSQL_ROW d = result.fetch_raw_row();
    00751                         if (!d)
    00752                                 break;
    00753                         Row row(d, &result, result.fetch_lengths(),
    00754                                         throw_exceptions());
    00755                         if (!row)
    00756                                 break;
    00757                         con.push_back(typename Sequence::value_type(row));
    00758                 }
    00759         }
    00760
    00771         template <class Seq>
    00772         void storein_sequence(Seq& con, SQLQueryParms& p)
    00773         {
    00774                 storein_sequence(con, str(p));
    00775         }
    00776
    00784         template <class Set>
    00785         void storein_set(Set& con)
    00786         {
    00787                 storein_set(con, str(template_defaults));
    00788         }
    00789
    00803         template <class Set>
    00804         void storein_set(Set& con, const SQLTypeAdapter& s)
    00805         {
    00806                 UseQueryResult result = use(s);
    00807                 while (1) {
    00808                         MYSQL_ROW d = result.fetch_raw_row();
    00809                         if (!d)
    00810                                 return;
    00811                         Row row(d, &result, result.fetch_lengths(),
    00812                                         throw_exceptions());
    00813                         if (!row)
    00814                                 break;
    00815                         con.insert(typename Set::value_type(row));
    00816                 }
    00817         }
    00818
    00829         template <class Set>
    00830         void storein_set(Set& con, SQLQueryParms& p)
    00831         {
    00832                 storein_set(con, str(p));
    00833         }
    00834
    00853         template <class Container>
    00854         void storein(Container& con)
    00855         {
    00856                 storein(con, str(template_defaults));
    00857         }
    00858
    00865         template <class T>
    00866         void storein(T& con, SQLQueryParms& p)
    00867         {
    00868                 storein(con, str(p));
    00869         }
    00870
    00872         template <class T>
    00873         void storein(std::vector<T>& con, const SQLTypeAdapter& s)
    00874         {
    00875                 storein_sequence(con, s);
    00876         }
    00877
    00879         template <class T>
    00880         void storein(std::deque<T>& con, const SQLTypeAdapter& s)
    00881         {
    00882                 storein_sequence(con, s);
    00883         }
    00884
    00886         template <class T>
    00887         void storein(std::list<T>& con, const SQLTypeAdapter& s)
    00888         {
    00889                 storein_sequence(con, s);
    00890         }
    00891
    00892 #if defined(HAVE_EXT_SLIST)
    00895         template <class T>
    00896         void storein(__gnu_cxx::slist<T>& con, const SQLTypeAdapter& s)
    00897         {
    00898                 storein_sequence(con, s);
    00899         }
    00900 #elif defined(HAVE_GLOBAL_SLIST)
    00907         template <class T>
    00908         void storein(slist<T>& con, const SQLTypeAdapter& s)
    00909         {
    00910                 storein_sequence(con, s);
    00911         }
    00912 #elif defined(HAVE_STD_SLIST)
    00918         template <class T>
    00919         void storein(std::slist<T>& con, const SQLTypeAdapter& s)
    00920         {
    00921                 storein_sequence(con, s);
    00922         }
    00923 #endif
    00924
    00926         template <class T>
    00927         void storein(std::set<T>& con, const SQLTypeAdapter& s)
    00928         {
    00929                 storein_set(con, s);
    00930         }
    00931
    00933         template <class T>
    00934         void storein(std::multiset<T>& con, const SQLTypeAdapter& s)
    00935         {
    00936                 storein_set(con, s);
    00937         }
    00938
    00949         template <class T>
    00950         Query& update(const T& o, const T& n)
    00951         {
    00952                 reset();
    00953
    00954                 // Cast required for VC++ 2003 due to error in overloaded operator
    00955                 // lookup logic.  For an explanation of the problem, see:
    00956                 // http://groups-beta.google.com/group/microsoft.public.vc.stl/browse_thread/thread/9a68d84644e64f15
    00957                 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
    00958                                 "UPDATE " << o.table() << " SET " << n.equal_list() <<
    00959                                 " WHERE " << o.equal_list(" AND ", sql_use_compare);
    00960                 return *this;
    00961         }
    00962
    00971         template <class T>
    00972         Query& insert(const T& v)
    00973         {
    00974                 reset();
    00975
    00976                 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
    00977                                 "INSERT INTO " << v.table() << " (" <<
    00978                                 v.field_list() << ") VALUES (" <<
    00979                                 v.value_list() << ')';
    00980                 return *this;
    00981         }
    00982
    00996         template <class Iter>
    00997         Query& insert(Iter first, Iter last)
    00998         {
    00999                 reset();
    01000                 if (first == last) {
    01001                         return *this;   // empty set!
    01002                 }
    01003
    01004                 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
    01005                                 "INSERT INTO " << first->table() << " (" <<
    01006                                 first->field_list() << ") VALUES (" <<
    01007                                 first->value_list() << ')';
    01008
    01009                 Iter it = first + 1;
    01010                 while (it != last) {
    01011                         MYSQLPP_QUERY_THISPTR << ",(" << it->value_list() << ')';
    01012                         ++it;
    01013                 }
    01014
    01015                 return *this;
    01016         }
    01017
    01027         template <class T>
    01028         Query& replace(const T& v)
    01029         {
    01030                 reset();
    01031
    01032                 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
    01033                                 "REPLACE INTO " << v.table() << " (" <<
    01034                                 v.field_list() << ") VALUES (" << v.value_list() << ')';
    01035                 return *this;
    01036         }
    01037
    01038 #if !defined(DOXYGEN_IGNORE)
    01039         // Declare the remaining overloads.  These are hidden down here partly
    01040         // to keep the above code clear, but also so that we may hide them
    01041         // from Doxygen, which gets confused by macro instantiations that look
    01042         // like method declarations.
    01043         mysql_query_define0(std::string, str)
    01044         mysql_query_define0(SimpleResult, execute)
    01045         mysql_query_define0(StoreQueryResult, store)
    01046         mysql_query_define0(UseQueryResult, use)
    01047         mysql_query_define1(storein_sequence)
    01048         mysql_query_define1(storein_set)
    01049         mysql_query_define1(storein)
    01050 #endif // !defined(DOXYGEN_IGNORE)
    01051
    01055         SQLQueryParms template_defaults;
    01056
    01057 private:
    01058         friend class SQLQueryParms;
    01059
    01061         Connection* conn_;
    01062
    01064         bool copacetic_;
    01065
    01067         std::vector<SQLParseElement> parse_elems_;
    01068
    01071         std::vector<std::string> parsed_names_;
    01072
    01074         std::map<std::string, short int> parsed_nums_;
    01075
    01077         std::stringbuf sbuffer_;
    01078
    01080         void proc(SQLQueryParms& p);
    01081
    01082         SQLTypeAdapter* pprepare(char option, SQLTypeAdapter& S, bool replace = true);
    01083 };
    01084
    01085
    01089 inline std::ostream& operator <<(std::ostream& os, Query& q)
    01090 {
    01091         return os << q.str();
    01092 }
    01093
    01094
    01095 } // end namespace mysqlpp
    01096
    01097 #endif // !defined(MYSQLPP_QUERY_H)
    01098