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 <mysql.h>
00030 
00031 #include <misc/string.h>
00032 
00033 /*************************************************************************/
00034 
00036 
00037 namespace mysql_cli
00038 {
00039 
00040 /*************************************************************************/
00041 
00042 class Conn;
00043 class Result;
00044 
00045 /*************************************************************************/
00046 
00048 
00054 class Events
00055 {
00056     public:
00058         enum
00059         {
00061             evConnected = 1,
00063             evPing = 2,
00065             evClosed = 3,
00067             evError = 4,
00069             evQuery = 5,
00071             evWarningNote = 6,
00073             evWarning = 7,
00075             evWarningError = 8
00076         };
00077 
00079         virtual ~Events() { }
00080         
00082 
00086         virtual Events *copy() const = 0;
00087         
00089 
00102         virtual void log_event(Conn &conn, int event, unsigned int code,
00103                 char const *msg) const = 0;
00104 };
00105 
00106 /*************************************************************************/
00107 
00109 
00115 class Conn
00116 {
00117     public:
00119         static char const *client_info();
00121         static unsigned long client_version();
00122     
00124         Conn() : m_handle(0), m_connected(false), m_gone(false), m_queries(0),
00125                 m_since(0), m_exectime(0), m_ctimeout(0), m_reconnect(true),
00126                 m_host(0), m_port(0), m_unixsocket(0), m_user(0),
00127                 m_password(0), m_db(0), m_charset(0), m_es(0), m_essize(0),
00128                 m_e(0), m_logqueries(false), m_logwarnings(false) { }
00130         Conn(char const *host, char const *user, char const *password,
00131                     char const *db, unsigned int port, char const *us) :
00132                 m_handle(0), m_connected(false), m_gone(false), m_queries(0),
00133                 m_since(0), m_exectime(0), m_ctimeout(0), m_reconnect(true),
00134                 m_host(host), m_port(port), m_unixsocket(us), m_user(user),
00135                 m_password(password), m_db(db), m_charset(0), m_es(0),
00136                 m_essize(0), m_e(0), m_logqueries(false),
00137                 m_logwarnings(false) { }
00139 
00144         Conn(Conn const &c) : m_handle(0), m_connected(false), m_gone(false),
00145                 m_queries(0), m_since(0), m_exectime(0),
00146                 m_ctimeout(c.m_ctimeout), m_reconnect(c.m_reconnect),
00147                 m_host(c.m_host), m_port(c.m_port),
00148                 m_unixsocket(c.m_unixsocket), m_user(c.m_user),
00149                 m_password(c.m_password), m_db(c.m_db),
00150                 m_charset(c.m_charset), m_es(0), m_essize(0),
00151                 m_e(c.m_e ? c.m_e->copy() : 0), m_logqueries(c.m_logqueries),
00152                 m_logwarnings(c.m_logwarnings) { }
00154 
00160         Conn &operator=(Conn const &right);
00162         ~Conn();
00163         
00165 
00170         bool connect();
00171         
00173 
00182         bool change_user(char const *user, char const *password,
00183                 char const *db);
00184         
00186 
00192         bool select_db(char const *db);
00193         
00195 
00199         bool reconnect() { close(); return connect(); }
00200         
00202 
00207         bool ping();
00208         
00210 
00214         void close();
00215         
00217 
00225         unsigned long escape_string(misc::cstring &dest, char const *src,
00226                 long len = -1);
00227         
00229 
00239         char const *escape_string(char const *str, long len = -1);
00240         
00242 
00255         bool query(char const *query, unsigned long length,
00256                 Result *result, bool use = false);
00258         bool query(char const *query, Result *result, bool use = false);
00260         bool queryf(Result *result, char const *fmt, ...) FORMAT(printf,3,4);
00261         
00263         bool connected() const { return m_connected; }
00265 
00270         bool gone() const { return m_gone; }
00271         
00273         unsigned int queries() const { return m_queries; }
00275         time_t since() const { return m_since; }
00277         double exectime() const { return m_exectime; }
00278         
00280         unsigned long thread_id();
00281         
00283         unsigned int errnum();
00285         char const *error();
00286         
00288         char const *host_info();
00290         unsigned int proto_info();
00292         char const *server_info();
00294         unsigned long server_version();
00296         char const *stat();
00297         
00299         my_ulonglong insert_id();
00300         
00302         my_ulonglong affected_rows();
00304         unsigned int warning_count();
00306 
00310         char const *info();
00311         
00313         unsigned int field_count();
00314         
00316 
00320         unsigned int connect_timeout() const { return m_ctimeout; }
00322 
00327         unsigned int connect_timeout(unsigned int timeout)
00328                 { return (m_ctimeout = timeout); }
00329         
00331 
00336         my_bool reconnect() const { return m_reconnect; }
00337         
00339 
00345         my_bool reconnect(my_bool reco) { return (m_reconnect = reco); }
00346         
00348         char const *host() const { return m_host; }
00350         char const *host(char const *host) { return (m_host = host); }
00351         
00353         unsigned int port() const { return m_port; }
00355         unsigned int port(unsigned int port) { return (m_port = port); }
00356         
00358         char const *unix_socket() const { return m_unixsocket; }
00360         char const *unix_socket(char const *us) { return (m_unixsocket = us); }
00361         
00363         char const *user() const { return m_user; }
00365         char const *user(char const *user) { return (m_user = user); }
00366         
00368         char const *password() const { return m_password; }
00370         char const *password(char const *password)
00371                 { return (m_password = password); }
00372         
00374         char const *db() const { return m_db; }
00376         char const *db(char const *db) { return (m_db = db); }
00377         
00379         char const *charset();
00381         char const *charset(char const *charset);
00382         
00384         Events const *events() const { return m_e; }
00386 
00392         Events *events(Events *events);
00393         
00395         bool log_queries() const { return m_logqueries; }
00397         bool log_queries(bool value)
00398                 { return (m_logqueries = value); }
00399         
00401         bool log_warnings() const { return m_logwarnings; }
00403 
00408         bool log_warnings(bool value)
00409                 { return (m_logwarnings = value); }
00410     protected:
00412         MYSQL *m_handle;
00414         bool m_connected;
00416         bool m_gone;
00417         
00419         unsigned int m_queries;
00421         time_t m_since;
00423         double m_exectime;
00424         
00426         unsigned int m_ctimeout;
00428         my_bool m_reconnect;
00429         
00431         misc::cstring m_host;
00433         unsigned int m_port;
00435         misc::cstring m_unixsocket;
00436         
00438         misc::cstring m_user;
00440         misc::cstring m_password;
00442         misc::cstring m_db;
00443         
00445         misc::cstring m_charset;
00446         
00448         misc::cstring m_es;
00450         size_t m_essize;
00451         
00453         Events *m_e;
00454         
00456         bool m_logqueries;
00458         bool m_logwarnings;
00459 };
00460 
00461 /*************************************************************************/
00462 
00463 } /* namespace mysql_cli */
00464 
00465 /*************************************************************************/
00466 
00467 #endif /* MYSQL_CLI_CONN_H */

Generated on Wed Aug 15 00:37:21 2007 for Epona API by  doxygen 1.5.2