Database Class ReferenceConnection information and pooling. More...
Collaboration diagram for Database:
Detailed DescriptionConnection information and pooling.
Definition at line 60 of file Database.h. Member Typedef Documentation
Definition at line 95 of file Database.h.
Constructor & Destructor Documentation
Use embedded libmysqld. Published / author: 2001-02-15 / grymse@alhem.net Definition at line 59 of file Database.cpp. 00060 :database(d) 00061 ,m_errhandler(e) 00062 ,m_embedded(true) 00063 ,m_mutex(m_mutex) 00064 ,m_b_use_mutex(false) 00065 { 00066 }
Use embedded libmysqld + thread safe.
Definition at line 69 of file Database.cpp. 00070 :database(d) 00071 ,m_errhandler(e) 00072 ,m_embedded(true) 00073 ,m_mutex(m) 00074 ,m_b_use_mutex(true) 00075 { 00076 }
Connect to a MySQL server.
Definition at line 79 of file Database.cpp. 00080 :host(h) 00081 ,user(u) 00082 ,password(p) 00083 ,database(d) 00084 ,m_errhandler(e) 00085 ,m_embedded(false) 00086 ,m_mutex(m_mutex) 00087 ,m_b_use_mutex(false) 00088 { 00089 }
Connect to a MySQL server + thread safe.
Definition at line 92 of file Database.cpp. 00093 :host(h) 00094 ,user(u) 00095 ,password(p) 00096 ,database(d) 00097 ,m_errhandler(e) 00098 ,m_embedded(false) 00099 ,m_mutex(m) 00100 ,m_b_use_mutex(true) 00101 { 00102 }
Definition at line 105 of file Database.cpp. References error(), and m_opendbs. 00106 { 00107 for (opendb_v::iterator it = m_opendbs.begin(); it != m_opendbs.end(); it++) 00108 { 00109 OPENDB *p = *it; 00110 mysql_close(&p -> mysql); 00111 } 00112 while (m_opendbs.size()) 00113 { 00114 opendb_v::iterator it = m_opendbs.begin(); 00115 OPENDB *p = *it; 00116 if (p -> busy) 00117 { 00118 error("destroying Database object before Query object"); 00119 } 00120 delete p; 00121 m_opendbs.erase(it); 00122 } 00123 }
Member Function Documentation
Callback after mysql_init.
Definition at line 126 of file Database.cpp. References m_embedded. Referenced by grabdb(). 00127 { 00128 // using embedded server (libmysqld) 00129 if (m_embedded) 00130 { 00131 mysql_options(&odb -> mysql, MYSQL_READ_DEFAULT_GROUP, "test_libmysqld_CLIENT"); 00132 } 00133 }
try to establish connection with given host
Definition at line 254 of file Database.cpp. References error(), freedb(), and grabdb(). 00255 { 00256 OPENDB *odb = grabdb(); 00257 if (!odb) 00258 { 00259 return false; 00260 } 00261 int ping_result = mysql_ping(&odb -> mysql); 00262 if (ping_result) 00263 { 00264 error("mysql_ping() failed"); 00265 } 00266 freedb(odb); 00267 return ping_result ? false : true; 00268 }
Definition at line 136 of file Database.cpp. References m_errhandler. 00137 { 00138 m_errhandler = p; 00139 }
Definition at line 236 of file Database.cpp. References m_errhandler. Referenced by Query::Connected(), Connected(), Query::error(), error(), Query::execute(), Query::get_result(), grabdb(), ~Database(), and Query::~Query(). 00237 { 00238 if (m_errhandler) 00239 { 00240 va_list ap; 00241 char errstr[5000]; 00242 va_start(ap, format); 00243 #ifdef WIN32 00244 vsprintf(errstr, format, ap); 00245 #else 00246 vsnprintf(errstr, 5000, format, ap); 00247 #endif 00248 va_end(ap); 00249 m_errhandler -> error(*this, q, errstr); 00250 } 00251 }
Request a database connection. The "grabdb" method is used by the Query class, so that each object instance of Query gets a unique database connection. I will reimplement your connection check logic in the Query class, as that's where the database connection is really used. It should be used something like this. { Query q(db); if (!q.Connected()) return false; q.execute("delete * from user"); // well maybe not } When the Query object is deleted, then "freedb" is called - the database connection stays open in the m_opendbs vector. New Query objects can then reuse old connections. Definition at line 142 of file Database.cpp. References database, error(), host, m_b_use_mutex, m_embedded, m_mutex, m_opendbs, OnMyInit(), password, and user. Referenced by Connected(). 00143 { 00144 Lock lck(m_mutex, m_b_use_mutex); 00145 OPENDB *odb = NULL; 00146 00147 for (opendb_v::iterator it = m_opendbs.begin(); it != m_opendbs.end(); it++) 00148 { 00149 odb = *it; 00150 if (!odb -> busy) 00151 { 00152 break; 00153 } 00154 else 00155 { 00156 odb = NULL; 00157 } 00158 } 00159 if (!odb) 00160 { 00161 odb = new OPENDB; 00162 if (!odb) 00163 { 00164 error("grabdb: OPENDB struct couldn't be created"); 00165 return NULL; 00166 } 00167 if (!mysql_init(&odb -> mysql)) 00168 { 00169 error("mysql_init() failed - list size %d",m_opendbs.size()); 00170 delete odb; 00171 return NULL; 00172 } 00173 // use callback to set mysql_options() before connect, etc 00174 this -> OnMyInit(odb); 00175 if (m_embedded) 00176 { 00177 if (!mysql_real_connect(&odb -> mysql,NULL,NULL,NULL,database.c_str(),0,NULL,0) ) 00178 { 00179 error("mysql_real_connect(NULL,NULL,NULL,%s,0,NULL,0) failed - list size %d",database.c_str(),m_opendbs.size()); 00180 delete odb; 00181 return NULL; 00182 } 00183 } 00184 else 00185 { 00186 if (!mysql_real_connect(&odb -> mysql,host.c_str(),user.c_str(),password.c_str(),database.c_str(),0,NULL,0) ) 00187 { 00188 error("mysql_real_connect(%s,%s,***,%s,0,NULL,0) failed - list size %d",host.c_str(),user.c_str(),database.c_str(),m_opendbs.size()); 00189 delete odb; 00190 return NULL; 00191 } 00192 } 00193 odb -> busy = true; 00194 m_opendbs.push_back(odb); 00195 } 00196 else 00197 { 00198 if (mysql_ping(&odb -> mysql)) 00199 { 00200 error("mysql_ping() failed when reusing an old connection from the connection pool"); 00201 } 00202 odb -> busy = true; 00203 } 00204 return odb; 00205 }
Definition at line 208 of file Database.cpp. References m_b_use_mutex, and m_mutex. Referenced by Connected(), and Query::~Query(). 00209 { 00210 Lock lck(m_mutex, m_b_use_mutex); 00211 if (odb) 00212 { 00213 odb -> busy = false; 00214 } 00215 }
Definition at line 330 of file Database.cpp. Referenced by Query::safestr(). 00331 { 00332 std::string str2; 00333 for (size_t i = 0; i < str.size(); i++) 00334 { 00335 switch (str[i]) 00336 { 00337 case '\'': 00338 case '\\': 00339 case 34: 00340 str2 += '\\'; 00341 default: 00342 str2 += str[i]; 00343 } 00344 } 00345 return str2; 00346 }
Definition at line 349 of file Database.cpp. 00350 { 00351 std::string str2; 00352 for (size_t i = 0; i < str.size(); i++) 00353 { 00354 if (str[i] == '\\') 00355 { 00356 i++; 00357 } 00358 if (i < str.size()) 00359 { 00360 str2 += str[i]; 00361 } 00362 } 00363 return str2; 00364 }
Definition at line 367 of file Database.cpp. 00368 { 00369 std::string str2; 00370 for (size_t i = 0; i < str.size(); i++) 00371 { 00372 switch (str[i]) 00373 { 00374 case '&': 00375 str2 += "&"; 00376 break; 00377 case '<': 00378 str2 += "<"; 00379 break; 00380 case '>': 00381 str2 += ">"; 00382 break; 00383 case '"': 00384 str2 += """; 00385 break; 00386 case '\'': 00387 str2 += "'"; 00388 break; 00389 default: 00390 str2 += str[i]; 00391 } 00392 } 00393 return str2; 00394 }
Definition at line 397 of file Database.cpp. Referenced by Query::getbigint(). 00398 { 00399 int64_t val = 0; 00400 bool sign = false; 00401 size_t i = 0; 00402 if (str[i] == '-') 00403 { 00404 sign = true; 00405 i++; 00406 } 00407 for (; i < str.size(); i++) 00408 { 00409 val = val * 10 + (str[i] - 48); 00410 } 00411 return sign ? -val : val; 00412 }
Definition at line 415 of file Database.cpp. Referenced by Query::getubigint(), and Query::getuval(). 00416 { 00417 uint64_t val = 0; 00418 for (size_t i = 0; i < str.size(); i++) 00419 { 00420 val = val * 10 + (str[i] - 48); 00421 } 00422 return val; 00423 }
Definition at line 218 of file Database.cpp. References error(), and m_errhandler. 00219 { 00220 if (m_errhandler) 00221 { 00222 va_list ap; 00223 char errstr[5000]; 00224 va_start(ap, format); 00225 #ifdef WIN32 00226 vsprintf(errstr, format, ap); 00227 #else 00228 vsnprintf(errstr, 5000, format, ap); 00229 #endif 00230 va_end(ap); 00231 m_errhandler -> error(*this, errstr); 00232 } 00233 }
Member Data Documentation
The documentation for this class was generated from the following files: |