Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

MysqlDb.h

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * 00003 * $Id: MysqlDb.h,v 1.25 2006/08/17 02:58:56 deph Exp $ 00004 * 00005 * Author: Laurent Conin 00006 *************************************************************************** 00007 * 00008 * Description: Mysql - SQL Query handler 00009 * 00010 *************************************************************************** 00011 * 00012 * $Log: MysqlDb.h,v $ 00013 * Revision 1.25 2006/08/17 02:58:56 deph 00014 * updated load balancer - removing hard-coded nodes from API to xml 00015 * 00016 * Revision 1.24 2006/06/02 18:23:23 deph 00017 * Added an extra machine (db01) for analysis between 11pm and 7am 00018 * 00019 * Revision 1.22 2005/12/15 03:14:27 jeromel 00020 * Mem Leak fixes / Missing delete in new and stream context. 00021 * 00022 * Revision 1.21 2004/01/15 00:02:24 fisyak 00023 * Replace ostringstream => StString, add option for alpha 00024 * 00025 * Revision 1.20 2003/09/16 22:44:17 porter 00026 * got rid of all ostrstream objects; replaced with StString+string. 00027 * modified rules.make and added file stdb_streams.h for standalone compilation 00028 * 00029 * Revision 1.19 2003/09/02 17:57:49 perev 00030 * gcc 3.2 updates + WarnOff 00031 * 00032 * Revision 1.18 2003/07/02 18:39:24 porter 00033 * added server version to connection notification and host:port to connection failure notification 00034 * 00035 * Revision 1.17 2003/04/11 22:47:35 porter 00036 * Added a fast multi-row write model specifically needed by the daqEventTag 00037 * writer. Speed increased from about 100Hz to ~3000Hz. It is only invoked if 00038 * the table is marked as Non-Indexed (daqTags & scalers). For non-indexed tables 00039 * which include binary stored data (we don't have any yet), the fast writer has 00040 * to invoke a slower buffer so that the rates are a bit slower (~500Hz at 50 rows/insert). 00041 * 00042 * Revision 1.16 2002/03/22 19:05:38 porter 00043 * #-of-retries on server connect increased to 7 with timeout period doubled per 00044 * retry starting at 1 sec. DOES NOT work (is ignored) on STAR's Redhat 6.2 00045 * version of mysqlclient but does on Redhat 7.2. Needed for maintainable 00046 * multiple mirror servers using dns for round-robin load balancing. 00047 * 00048 * Revision 1.15 2002/01/30 15:40:47 porter 00049 * changed limits on flavor tag & made defaults retrieving more readable 00050 * 00051 * Revision 1.14 2001/10/24 04:05:08 porter 00052 * added long long type to I/O and got rid of obsolete dataIndex table 00053 * 00054 * Revision 1.13 2001/04/25 17:17:33 perev 00055 * HPcorrs 00056 * 00057 * Revision 1.12 2001/03/30 18:48:26 porter 00058 * modified code to keep Insure from wigging-out on ostrstream functions. 00059 * moved some messaging into a StDbSql method. 00060 * 00061 * Revision 1.11 2001/02/09 23:06:24 porter 00062 * replaced ostrstream into a buffer with ostrstream creating the 00063 * buffer. The former somehow clashed on Solaris with CC5 iostream (current .dev) 00064 * 00065 * Revision 1.10 2001/01/22 18:37:50 porter 00066 * Update of code needed in next year running. This update has little 00067 * effect on the interface (only 1 method has been changed in the interface). 00068 * Code also preserves backwards compatibility so that old versions of 00069 * StDbLib can read new table structures. 00070 * -Important features: 00071 * a. more efficient low-level table structure (see StDbSql.cc) 00072 * b. more flexible indexing for new systems (see StDbElememtIndex.cc) 00073 * c. environment variable override KEYS for each database 00074 * d. StMessage support & clock-time logging diagnostics 00075 * -Cosmetic features 00076 * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access 00077 * f. removed codes that have been obsolete for awhile (e.g. db factories) 00078 * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI 00079 * and mysqlAccessor became StDbSql) 00080 * 00081 * Revision 1.9 2000/03/01 20:56:15 porter 00082 * 3 items: 00083 * 1. activated reConnect for server timeouts 00084 * 2. activated connection sharing; better resource utilization but poorer 00085 * logging 00086 * 3. made rollback method in mysqlAccessor more robust (affects writes only) 00087 * 00088 * Revision 1.8 2000/02/18 16:58:09 porter 00089 * optimization of table-query, + whereClause gets timeStamp if indexed 00090 * + fix to write multiple rows algorithm 00091 * 00092 * Revision 1.7 2000/02/15 20:27:43 porter 00093 * Some updates to writing to the database(s) via an ensemble (should 00094 * not affect read methods & haven't in my tests. 00095 * - closeAllConnections(node) & closeConnection(table) method to mgr. 00096 * - 'NullEntry' version to write, with setStoreMode in table; 00097 * - updated both StDbTable's & StDbTableDescriptor's copy-constructor 00098 * 00099 * Revision 1.6 2000/01/27 05:54:32 porter 00100 * Updated for compiling on CC5 + HPUX-aCC + KCC (when flags are reset) 00101 * Fixed reConnect()+transaction model mismatch 00102 * added some in-code comments 00103 * 00104 * Revision 1.5 2000/01/10 20:37:53 porter 00105 * expanded functionality based on planned additions or feedback from Online work. 00106 * update includes: 00107 * 1. basis for real transaction model with roll-back 00108 * 2. limited SQL access via the manager for run-log & tagDb 00109 * 3. balance obtained between enumerated & string access to databases 00110 * 4. 3-levels of diagnostic output: Quiet, Normal, Verbose 00111 * 5. restructured Node model for better XML support 00112 * 00113 * Revision 1.4 1999/10/19 14:30:37 porter 00114 * modifications relevant to use with StDbBroker and future merging with 00115 * "params" database structure + some docs + suppressing diagnostics messages 00116 * 00117 * Revision 1.3 1999/09/30 02:06:00 porter 00118 * add StDbTime to better handle timestamps, modify SQL content (mysqlAccessor) 00119 * allow multiple rows (StDbTable), & Added the comment sections at top of 00120 * each header and src file 00121 * 00122 **************************************************************************/ 00123 #ifndef MYSQDB_HH 00124 #define MYSQDB_HH 00125 #ifndef __CINT__ 00126 #include "mysql.h" 00127 #else 00128 typedef char MYSQL_RES; 00129 typedef char MYSQL_ROW; 00130 typedef char MYSQL; 00131 typedef int MYSQL_FIELD; 00132 #endif 00133 00134 #include "stdb_streams.h" 00135 #include "StDbBuffer.h" 00136 #include "StDbLogger.hh" 00137 #include "parseXmlString.hh" 00138 #include "StDbManagerImpl.hh" 00139 #include <vector.h> 00140 #include <string.h> 00141 00142 #ifdef HPUX 00143 #define freeze(i) str() 00144 #endif 00145 00146 #define endsql ";" 00147 00148 00149 class StDbBuffer; 00150 00151 class MysqlResult { 00152 00153 private: 00154 MYSQL_RES * mRes; 00155 MYSQL_ROW mRow; 00156 char mSep; 00157 00158 public: 00159 MysqlResult() {mRes=0;}; 00160 // virtual char float* NextRowBin(); 00161 //virtual void Set(const MYSQL_RES *aPnt) {mRes=aPnt;}; 00162 virtual unsigned NbRows () {return mysql_num_rows(mRes);}; 00163 virtual unsigned NbFields () {return mysql_num_fields(mRes);}; 00164 virtual void Release () {if(mRes)mysql_free_result(mRes);mRes=0;}; 00165 MysqlResult &operator++() {mRow=mysql_fetch_row(mRes);return *this;}; 00166 // MysqlResult &operator >>(char *aString); 00167 00168 protected: 00169 00170 friend class MysqlDb; 00171 }; 00172 00173 class MysqlBin{ 00174 00175 private: 00176 char *mBinData; 00177 unsigned long int mLen; 00178 00179 public: 00180 MysqlBin() {mBinData=0;mLen=0;}; 00181 virtual void Input(const unsigned long int aLen,char *aBin) 00182 {mBinData=aBin;mLen=aLen;}; 00183 00184 friend class MysqlDb; 00185 }; 00186 00187 00188 class MysqlDb{ 00189 00190 private: 00191 00192 MYSQL mData; 00193 char* mQuery; 00194 unsigned long int mQueryLen; 00195 char* mQueryLast; // for printing query after execution 00196 MysqlResult* mRes; 00197 bool mqueryState; 00198 bool mhasConnected; 00199 bool mhasBinaryQuery; 00200 00201 char* mdbhost; 00202 char* mdbName; 00203 char* mdbuser; 00204 char* mdbpw; 00205 int mdbPort; 00206 char* mdbServerVersion; 00207 00208 unsigned int mtimeout; // wait time between connection tries 00209 00210 00211 // these are specific for fast multi-row writes of non-indexed tables 00212 int jfields; 00213 char* cnames[200]; 00214 bool isBlob[200]; 00215 bool isBinary[200]; 00216 bool isSpecialType[200]; 00217 00218 std::vector<std::string>::iterator RecommendedServer(std::vector<std::string>* ListToUse, char* socket, int port); 00219 00220 public: 00221 00222 // for logging times 00223 bool mlogTime; 00224 StDbLogger mqueryLog; 00225 StDbLogger msocketLog; 00226 StDbLogger mconnectLog; 00227 00228 MysqlDb(); 00229 virtual ~MysqlDb(); 00230 virtual bool Connect(const char *aHost, const char *aUser, 00231 const char *aPasswd, const char *aDb, const int aPort=0); 00232 virtual bool reConnect(); 00233 00234 virtual unsigned NbRows (){if(mqueryState)return mRes->NbRows(); return 0;}; 00235 virtual unsigned NbFields(){if(mqueryState)return mRes->NbFields(); return 0;}; 00236 virtual void Release() {mRes->Release();}; 00237 00238 virtual char* printQuery(); 00239 // virtual char* LastQuery(); 00240 00241 // these 3 meths are specific for fast multi-row writes of non-index tables 00242 virtual bool InputStart(const char *aName,StDbBuffer *aBuff, const char* colList, int nRows, bool& hasBinary); 00243 virtual bool InputRow(StDbBuffer* aBuff, int row); 00244 virtual bool InputEnd(); 00245 00246 00247 virtual bool Input(const char *aName,StDbBuffer *aBuff); 00248 //virtual bool InitBuff(StDbBuffer *aBuff); 00249 virtual bool Output(StDbBuffer *aBuff); 00250 MysqlDb &operator<<(const char *c); 00251 MysqlDb &operator<<(const string s); 00252 MysqlDb &operator<<(const MysqlBin *aBin); 00253 00254 MysqlDb &operator<<(const short aq); 00255 MysqlDb &operator<<(const unsigned short aq); 00256 MysqlDb &operator<<(const int aq); 00257 MysqlDb &operator<<(const unsigned int aq); 00258 MysqlDb &operator<<(const long long aq); 00259 MysqlDb &operator<<(const float aq); 00260 MysqlDb &operator<<(const double aq); 00261 00262 char **DecodeStrArray(char* strinput , int &aLen) ; 00263 char* CodeStrArray(char** strarr , int aLen); 00264 virtual int GetLastInsertID(){ return (int)mysql_insert_id(&mData);} 00265 virtual bool QueryStatus() { return mqueryState; } 00266 virtual bool checkForTable(const char* tableName); 00267 virtual void Close(); 00268 virtual bool IsConnected() { return mhasConnected; } 00269 virtual bool setDefaultDb(const char* dbName); 00270 00271 StDbManagerImpl* my_manager; // need access to the list of servers and their properties for load balancing 00272 00273 protected: 00274 virtual void RazQuery() ; 00275 virtual bool ExecQuery(); 00276 //virtual column* NextRow(); 00277 //virtual column* PrepareWrite(); 00278 //virtual void Print() {cout << mQueryMess << "|" <<mQueryLast <<endl;}; // debug 00279 //virtual void Out(); //debug 00280 //virtual MysqlResult* Result() { return mRes;}; //debug 00281 00282 //ClassDef(MysqlDb,0) 00283 00284 }; 00285 00286 inline void MysqlDb::Close(){ 00287 if(mhasConnected)mysql_close(&mData); 00288 mhasConnected=false; 00289 } 00290 00291 inline MysqlDb &MysqlDb::operator<<( const string s){ 00292 return *this<<s.c_str(); 00293 } 00294 00295 inline MysqlDb &MysqlDb::operator<<( const short aq){ 00296 StString ts; ts<<aq; 00297 return *this<<ts.str(); 00298 } 00299 00300 inline MysqlDb &MysqlDb::operator<<( const unsigned short aq){ 00301 StString ts; ts<<aq; 00302 return *this<<ts.str(); 00303 } 00304 00305 inline MysqlDb &MysqlDb::operator<<( const int aq){ 00306 StString ts; ts<<aq; 00307 return *this<<ts.str(); 00308 } 00309 00310 inline MysqlDb &MysqlDb::operator<<( const unsigned int aq){ 00311 StString ts; ts<<aq; 00312 return *this<<ts.str(); 00313 } 00314 00315 inline MysqlDb &MysqlDb::operator<<( const long long aq){ 00316 StString ts; ts<<aq; 00317 return *this<<ts.str(); 00318 } 00319 00320 inline MysqlDb &MysqlDb::operator<<( const float aq){ 00321 StString ts; ts<<aq; 00322 return *this<<ts.str(); 00323 } 00324 00325 inline MysqlDb &MysqlDb::operator<<( const double aq){ 00326 StString ts; ts<<aq; 00327 return *this<<ts.str(); 00328 } 00329 00330 00331 extern const MysqlBin *Binary(const unsigned long int aLen,const float *aBin); 00332 //extern MysqlBin *Binary(const unsigned long int aLen,const char *aBin); 00333 #endif 00334 00335 00336 00337 00338 00339 00340

Generated on Thu Aug 24 14:45:25 2006 for Doxygen by doxygen 1.3.7