conn.h

Go to the documentation of this file.
00001 /* MySQL connection class.
00002  *
00003  * MySQL client module (c) 2006 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program (see the file COPYING); if not, write to the
00018  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00024 #ifndef MYSQL_CLI_CONN_H
00025 #define MYSQL_CLI_CONN_H
00026 
00027 /*************************************************************************/
00028 
00029 #include <deque>
00030 #include <string>
00031 #include <mysql.h>
00032 
00033 #include <misc/string.h>
00034 
00035 /*************************************************************************/
00036 
00038 
00039 namespace mysql_cli
00040 {
00041 
00042 /*************************************************************************/
00043 
00044 class Conn;
00045 class Result;
00046 
00047 /*************************************************************************/
00048 
00050 
00056 class Events
00057 {
00058     public:
00060         enum
00061         {
00063             evConnected = 1,
00065             evPing = 2,
00067             evClosed = 3,
00069             evError = 4,
00071             evQuery = 5,
00073             evWarningNote = 6,
00075             evWarning = 7,
00077             evWarningError = 8
00078         };
00079 
00081         virtual ~Events() { }
00082         
00084 
00088         virtual Events *copy() const = 0;
00089         
00091 
00104         virtual void log_event(Conn &conn, int event, unsigned int code,
00105                 char const *msg) const = 0;
00106 };
00107 
00108 /*************************************************************************/
00109 
00111 
00117 class Conn
00118 {
00119     public:
00121         static char const *client_info();
00123         static unsigned long client_version();
00124     
00126         Conn() : m_handle(0), m_connected(false), m_gone(false), m_queue(),
00127                 m_queries(0), m_since(0), m_exectime(0), m_ctimeout(0),
00128                 m_reconnect(true), m_host(0), m_port(0), m_unixsocket(0),
00129                 m_user(0), m_password(0), m_db(0), m_charset(0), m_es(0),
00130                 m_essize(0), m_e(0), m_logqueries(false), m_logwarnings(false)
00131                 { }
00133         Conn(char const *host, char const *user, char const *password,
00134                     char const *db, unsigned int port, char const *us) :
00135                 m_handle(0), m_connected(false), m_gone(false), m_queue(),
00136                 m_queries(0), m_since(0), m_exectime(0), m_ctimeout(0), 
00137                 m_reconnect(true), m_host(host), m_port(port), 
00138                 m_unixsocket(us), m_user(user), m_password(password), m_db(db),
00139                 m_charset(0), m_es(0), m_essize(0), m_e(0), 
00140                 m_logqueries(false), m_logwarnings(false) { }
00142 
00147         Conn(Conn const &c) : m_handle(0), m_connected(false), m_gone(false),
00148                 m_queries(0), m_since(0), m_exectime(0),
00149                 m_ctimeout(c.m_ctimeout), m_reconnect(c.m_reconnect),
00150                 m_host(c.m_host), m_port(c.m_port),
00151                 m_unixsocket(c.m_unixsocket), m_user(c.m_user),
00152                 m_password(c.m_password), m_db(c.m_db),
00153                 m_charset(c.m_charset), m_es(0), m_essize(0),
00154                 m_e(c.m_e ? c.m_e->copy() : 0), m_logqueries(c.m_logqueries),
00155                 m_logwarnings(c.m_logwarnings) { }
00157 
00163         Conn &operator=(Conn const &right);
00165         ~Conn();
00166         
00168 
00173         bool connect();
00174         
00176 
00185         bool change_user(char const *user, char const *password,
00186                 char const *db);
00187         
00189 
00195         bool select_db(char const *db);
00196         
00198 
00203         bool reconnect() { close(false); return connect(); }
00204         
00206 
00211         bool ping();
00212         
00214 
00221         void close(bool clear_queue);
00222         
00224 
00232         unsigned long escape_string(misc::cstring &dest, char const *src,
00233                 long len = -1);
00234         
00236 
00246         char const *escape_string(char const *str, long len = -1);
00247         
00249 
00262         bool query(char const *query, unsigned long length,
00263                 Result *result, bool use = false);
00265         bool query(char const *query, Result *result, bool use = false);
00267         bool queryf(Result *result, char const *fmt, ...) FORMAT(printf,3,4);
00268         
00270 
00281         bool query_cache(std::string const &query, bool cache);
00282         
00284         bool connected() const { return m_connected; }
00286 
00291         bool gone() const { return m_gone; }
00292         
00294         unsigned int queries() const { return m_queries; }
00296         time_t since() const { return m_since; }
00298         double exectime() const { return m_exectime; }
00299         
00301         unsigned long thread_id();
00302         
00304         unsigned int errnum();
00306         char const *error();
00307         
00309         char const *host_info();
00311         unsigned int proto_info();
00313         char const *server_info();
00315         unsigned long server_version();
00317         char const *stat();
00318         
00320         my_ulonglong insert_id();
00321         
00323         my_ulonglong affected_rows();
00325         unsigned int warning_count();
00327 
00331         char const *info();
00332         
00334         unsigned int field_count();
00335         
00337 
00341         unsigned int connect_timeout() const { return m_ctimeout; }
00343 
00348         unsigned int connect_timeout(unsigned int timeout)
00349                 { return (m_ctimeout = timeout); }
00350         
00352 
00357         my_bool reconnect() const { return m_reconnect; }
00358         
00360 
00366         my_bool reconnect(my_bool reco) { return (m_reconnect = reco); }
00367         
00369         char const *host() const { return m_host; }
00371         char const *host(char const *host) { return (m_host = host); }
00372         
00374         unsigned int port() const { return m_port; }
00376         unsigned int port(unsigned int port) { return (m_port = port); }
00377         
00379         char const *unix_socket() const { return m_unixsocket; }
00381         char const *unix_socket(char const *us) { return (m_unixsocket = us); }
00382         
00384         char const *user() const { return m_user; }
00386         char const *user(char const *user) { return (m_user = user); }
00387         
00389         char const *password() const { return m_password; }
00391         char const *password(char const *password)
00392                 { return (m_password = password); }
00393         
00395         char const *db() const { return m_db; }
00397         char const *db(char const *db) { return (m_db = db); }
00398         
00400         char const *charset();
00402         char const *charset(char const *charset);
00403         
00405         Events const *events() const { return m_e; }
00407 
00413         Events *events(Events *events);
00414         
00416         bool log_queries() const { return m_logqueries; }
00418         bool log_queries(bool value)
00419                 { return (m_logqueries = value); }
00420         
00422         bool log_warnings() const { return m_logwarnings; }
00424 
00429         bool log_warnings(bool value)
00430                 { return (m_logwarnings = value); }
00431     protected:
00433         typedef std::deque<std::string> queue_type;
00434         
00436         MYSQL *m_handle;
00438         bool m_connected;
00440         bool m_gone;
00441         
00443         std::deque<std::string> m_queue;
00444         
00446         unsigned int m_queries;
00448         time_t m_since;
00450         double m_exectime;
00451         
00453         unsigned int m_ctimeout;
00455         my_bool m_reconnect;
00456         
00458         misc::cstring m_host;
00460         unsigned int m_port;
00462         misc::cstring m_unixsocket;
00463         
00465         misc::cstring m_user;
00467         misc::cstring m_password;
00469         misc::cstring m_db;
00470         
00472         misc::cstring m_charset;
00473         
00475         misc::cstring m_es;
00477         size_t m_essize;
00478         
00480         Events *m_e;
00481         
00483         bool m_logqueries;
00485         bool m_logwarnings;
00486     
00487     private:
00488         /* Processes the queue, with provision for cases where the server goes away again.
00489          * Returns false if the server is gone again. */
00490         bool process_queue();
00491 };
00492 
00493 /*************************************************************************/
00494 
00495 } /* namespace mysql_cli */
00496 
00497 /*************************************************************************/
00498 
00499 #endif /* MYSQL_CLI_CONN_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3