Dependents: MySQLClientExample MySQLClientExampleMA
MySQLClient.h
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 /** \file 00025 MySQL Client header file 00026 */ 00027 00028 #ifndef MYSQL_CLIENT_H 00029 #define MYSQL_CLIENT_H 00030 00031 #include "core/net.h" 00032 #include "core/netservice.h" 00033 #include "api/TCPSocket.h" 00034 #include "api/DNSRequest.h" 00035 #include "mbed.h" 00036 00037 #include <string> 00038 using std::string; 00039 00040 #include <map> 00041 using std::map; 00042 00043 typedef unsigned char byte; 00044 00045 ///MySQL client results 00046 enum MySQLResult 00047 { 00048 MYSQL_OK, ///<Success 00049 MYSQL_PROCESSING, ///<Processing 00050 MYSQL_PRTCL, ///<Protocol error 00051 MYSQL_SETUP, ///<Not properly configured 00052 MYSQL_DNS, ///<Could not resolve name 00053 MYSQL_AUTHFAILED, ///<Auth failure 00054 MYSQL_READY, ///<Ready to send commands 00055 MYSQL_SQL, ///<SQL Error 00056 MYSQL_TIMEOUT, ///<Connection timeout 00057 MYSQL_CONN ///<Connection error 00058 }; 00059 00060 ///A MySQL Client 00061 /** 00062 This MySQL client implements a limited subset of the MySQL internal client/server protocol (including authentication), for server versions 4.1 and newer. 00063 */ 00064 class MySQLClient : protected NetService 00065 { 00066 public: 00067 ///Instantiates the MySQL client 00068 MySQLClient(); 00069 virtual ~MySQLClient(); 00070 00071 //High Level setup functions 00072 00073 ///Opens a connection to a server 00074 /** 00075 Opens a connection to the server host using the provided username, password passowrd and selecting database 00076 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter 00077 @param host : server 00078 @param user : username 00079 @param db : database to use 00080 @param pMethod : callback to call on each request completion 00081 */ 00082 MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking 00083 00084 ///Opens a connection to a server 00085 /** 00086 Opens a connection to the server host using the provided username, password passowrd and selecting database 00087 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter 00088 @param host : server 00089 @param user : username 00090 @param db : database to use 00091 @param pItem : callback's class instance 00092 @param pMethod : callback's method to call on each request completion 00093 */ 00094 template<class T> 00095 MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking 00096 { 00097 setOnResult(pItem, pMethod); 00098 setup(host, user, password, db); 00099 return MYSQL_PROCESSING; 00100 } 00101 00102 00103 ///Executes an SQL command 00104 /** 00105 Executes an SQL request on the SQL server 00106 This is a non-blocking function 00107 On completion, the callback set in the open function is fired with the result of the command in parameter 00108 @param sqlCommand SQL request to execute 00109 */ 00110 MySQLResult sql(string& sqlCommand); 00111 00112 ///Closes the connection to the server 00113 MySQLResult exit(); 00114 00115 void setOnResult( void (*pMethod)(MySQLResult) ); 00116 class CDummy; 00117 template<class T> 00118 void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) ) 00119 { 00120 m_pCb = NULL; 00121 m_pCbItem = (CDummy*) pItem; 00122 m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod; 00123 } 00124 00125 ///Setups timeout 00126 /** 00127 @param ms : time of connection inactivity in ms after which the request should timeout 00128 */ 00129 void setTimeout(int ms); 00130 00131 virtual void poll(); //Called by NetServices 00132 00133 protected: 00134 void resetTimeout(); 00135 00136 void init(); 00137 void close(); 00138 00139 void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary 00140 void connect(); //Start Connection 00141 00142 void handleHandshake(); 00143 void sendAuth(); 00144 00145 void handleAuthResult(); 00146 void sendAuth323(); 00147 00148 void sendCommand(byte command, byte* arg, int len); 00149 void handleCommandResult(); 00150 00151 void readData(); //Copy to buf 00152 void writeData(); //Copy from buf 00153 00154 void onTCPSocketEvent(TCPSocketEvent e); 00155 void onDNSReply(DNSReply r); 00156 void onResult(MySQLResult r); //Called when exchange completed or on failure 00157 void onTimeout(); //Connection has timed out 00158 00159 private: 00160 CDummy* m_pCbItem; 00161 void (CDummy::*m_pCbMeth)(MySQLResult); 00162 00163 void (*m_pCb)(MySQLResult); 00164 00165 TCPSocket* m_pTCPSocket; 00166 00167 Timer m_watchdog; 00168 int m_timeout; 00169 00170 DNSRequest* m_pDnsReq; 00171 00172 bool m_closed; 00173 00174 enum MySQLStep 00175 { 00176 // MYSQL_INIT, 00177 MYSQL_HANDSHAKE, 00178 MYSQL_AUTH, 00179 MYSQL_COMMANDS, 00180 MYSQL_CLOSED 00181 }; 00182 00183 //Parameters 00184 Host m_host; 00185 00186 string m_user; 00187 string m_password; 00188 string m_db; 00189 00190 //Low-level buffers & state-machine 00191 MySQLStep m_state; 00192 00193 byte* m_buf; 00194 byte* m_pPos; 00195 int m_len; 00196 int m_size; 00197 00198 int m_packetId; 00199 00200 }; 00201 00202 #endif
Generated on Tue Jul 12 2022 20:43:20 by 1.7.2