Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

connection.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 /***********************************************************************
00010  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
00011  MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
00012  Others may also hold copyrights on code in this file.  See the CREDITS
00013  file in the top directory of the distribution for details.
00014 
00015  This file is part of MySQL++.
00016 
00017  MySQL++ is free software; you can redistribute it and/or modify it
00018  under the terms of the GNU Lesser General Public License as published
00019  by the Free Software Foundation; either version 2.1 of the License, or
00020  (at your option) any later version.
00021 
00022  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
00023  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00025  License for more details.
00026 
00027  You should have received a copy of the GNU Lesser General Public
00028  License along with MySQL++; if not, write to the Free Software
00029  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00030  USA
00031 ***********************************************************************/
00032 
00033 #ifndef MYSQLPP_CONNECTION_H
00034 #define MYSQLPP_CONNECTION_H
00035 
00036 #include "platform.h"
00037 
00038 #include "exceptions.h"
00039 #include "result.h"
00040 
00041 #include <mysql.h>
00042 
00043 #include <vector>
00044 #include <deque>
00045 #include <list>
00046 #include <set>
00047 #include <map>
00048 
00049 #ifdef HAVE_EXT_SLIST
00050 #  include <ext/slist>
00051 #else
00052 #  ifdef HAVE_STD_SLIST
00053 #      include <slist>
00054 #  endif
00055 #endif
00056 
00057 namespace mysqlpp {
00058 
00059 class Query;
00060 
00062 
00063 class Connection
00064 {
00065 private:
00066         friend class ResNSel;
00067         friend class ResUse;
00068         friend class Query;
00069 
00070         bool throw_exceptions;
00071         MYSQL mysql;
00072         bool is_connected;
00073         bool locked;
00074         bool Success;
00075 
00076 public:
00080         Connection();
00081 
00087         Connection(bool te);
00088 
00103         Connection(const char* db, const char* host = "",
00104                         const char* user = "", const char* passwd = "",
00105                         bool te = true);
00106 
00132         Connection(const char* db, const char* host, const char* user,
00133                         const char* passwd, uint port, my_bool compress = 0,
00134                         unsigned int connect_timeout = 60, bool te = true,
00135                         cchar* socket_name = 0, unsigned int client_flag = 0);
00136 
00137         ~Connection();
00138 
00146         bool connect(cchar* db = "", cchar* host = "", cchar* user = "",
00147                         cchar* passwd = "");
00148 
00159         bool real_connect(cchar* db = "", cchar* host = "",
00160                         cchar* user = "", cchar* passwd = "", uint port = 0,
00161                         my_bool compress = 0, unsigned int connect_timeout = 60,
00162                         cchar* socket_name = 0, unsigned int client_flag = 0);
00163 
00167         void close()
00168         {
00169                 mysql_close(&mysql);
00170                 is_connected = false;
00171         }
00172         
00175         std::string info();
00176 
00180         bool connected() const
00181         {
00182                 return is_connected;
00183         }
00184 
00186         bool success() const
00187         {
00188                 return Success;
00189         }
00190 
00197         bool lock()
00198         {
00199                 if (locked) {
00200                         return true;
00201                 }
00202                 locked = true;
00203                 return false;
00204         }
00205 
00209         void unlock() { locked = false; }
00210 
00212         void purge() { close(); }
00213 
00221         Query query();
00222 
00238         operator bool() { return success(); }
00239 
00244         const char *error() { return mysql_error(&mysql); }
00245 
00250         int errnum() { return mysql_errno(&mysql); }
00251 
00260         int refresh(unsigned int refresh_options)
00261         {
00262                 return mysql_refresh(&mysql, refresh_options);
00263         }
00264 
00275         int ping() { return mysql_ping(&mysql); }
00276 
00282         int kill(unsigned long pid) { return mysql_kill(&mysql, pid); }
00283 
00287         std::string client_info()
00288         {
00289                 return std::string(mysql_get_client_info());
00290         }
00291 
00298         std::string host_info()
00299         {
00300                 return std::string(mysql_get_host_info(&mysql));
00301         }
00302 
00307         int proto_info() 
00308         {
00309                 return mysql_get_proto_info(&mysql);
00310         }
00311 
00315         std::string server_info()
00316         {
00317                 return std::string(mysql_get_server_info(&mysql));
00318         }
00319 
00326         std::string stat() { return std::string(mysql_stat(&mysql)); }
00327 
00347         Result store(const std::string& str)
00348         {
00349                 return store(str, throw_exceptions);
00350         }
00351 
00373         ResUse use(const std::string& str)
00374         {
00375                 return use(str, throw_exceptions);
00376         }
00377 
00391         ResNSel execute(const std::string& str)
00392         {
00393                 return execute(str, throw_exceptions);
00394         }
00395 
00407         bool exec(const std::string& str);
00408 
00414         Result store(const std::string& str, bool te);
00415 
00421         ResUse use(const std::string& str, bool te);
00422 
00428         ResNSel execute(const std::string& str, bool te);
00429 
00436         bool create_db(std::string db)
00437         {
00438                 return !execute("CREATE DATABASE " + db);
00439         }
00440 
00447         bool drop_db(std::string db)
00448         {
00449                 return !execute("DROP DATABASE " + db);
00450         }
00451 
00453         bool select_db(std::string db) { return select_db(db.c_str()); }
00454 
00456         bool select_db(const char* db);
00457 
00465         bool reload();
00466         
00472         bool shutdown();
00473 
00479         std::string infoo() { return info(); }
00480 
00482         st_mysql_options get_options() const { return mysql.options; }
00483 
00491         int read_options(enum mysql_option option, const char* arg)
00492         {
00493                 return mysql_options(&mysql, option, arg);
00494         }
00495 
00499         my_ulonglong affected_rows() { return mysql_affected_rows(&mysql); }
00500 
00507         my_ulonglong insert_id() { return mysql_insert_id(&mysql); }
00508 
00521         template <class Sequence>
00522         void storein_sequence(Sequence& con, const std::string& s);
00523 
00534         template <class Set>
00535         void storein_set(Set& con, const std::string& s);
00536 
00538         template <class T>
00539         void storein(std::vector<T>& con, const std::string& s)
00540         {
00541                 storein_sequence(con, s);
00542         }
00543 
00545         template <class T>
00546         void storein(std::deque<T>& con, const std::string& s)
00547         {
00548                 storein_sequence(con, s);
00549         }
00550 
00552         template <class T>
00553         void storein(std::list<T>& con, const std::string& s)
00554         {
00555                 storein_sequence(con, s);
00556         }
00557 
00558 #if defined(HAVE_EXT_SLIST)
00561         template <class T>
00562         void storein(__gnu_cxx::slist<T>& con, const std::string& s)
00563         {
00564                 storein_sequence(con, s);
00565         }
00566 #elif defined(HAVE_STD_SLIST)
00573         template <class T>
00574         void storein(slist<T>& con, const std::string& s)
00575         {
00576                 storein_sequence(con, s);
00577         }
00578 #endif
00579 
00581         template <class T>
00582         void storein(std::set<T>& con, const std::string& s)
00583         {
00584                 storein_set(con, s);
00585         }
00586 
00588         template <class T>
00589         void storein(std::multiset<T>& con, const std::string& s)
00590         {
00591                 storein_set(con, s);
00592         }
00593 };
00594 
00595 
00596 template <class Sequence>
00597 void Connection::storein_sequence(Sequence& con, const std::string& s)
00598 {
00599         ResUse result = use(s);
00600         while (1) {
00601                 MYSQL_ROW d = mysql_fetch_row(result.mysql_result());
00602                 if (!d)
00603                         break;
00604                 Row row(d, &result, mysql_fetch_lengths(result.mysql_result()),
00605                                 true);
00606                 if (!row)
00607                         break;
00608                 con.push_back(typename Sequence::value_type(row));
00609         }
00610 }
00611 
00612 template <class Set>
00613 void Connection::storein_set(Set& con, const std::string& s)
00614 {
00615         ResUse result = use(s);
00616         while (1) {
00617                 MYSQL_ROW d = mysql_fetch_row(result.mysql_result());
00618                 if (!d)
00619                         return;
00620                 Row row(d, &result, mysql_fetch_lengths(result.mysql_result()),
00621                                 true);
00622                 if (!row)
00623                         break;
00624                 con.insert(typename Set::value_type(row));
00625         }
00626 }
00627 
00628 } // end namespace mysqlpp
00629 
00630 #endif
00631 

Generated on Thu May 26 09:39:58 2005 for MySQL++ by doxygen1.2.18