Changeset 129 for trunk/src/sql_Database.cpp
- Timestamp:
- 11/18/09 04:40:30 (3 years ago)
- Files:
-
- 1 modified
-
trunk/src/sql_Database.cpp (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sql_Database.cpp
r127 r129 1 /* 1 /** 2 2 ** Database.cpp 3 3 ** 4 ** Published / author: 200 5-08-12/ grymse@alhem.net4 ** Published / author: 2001-02-15 / grymse@alhem.net 5 5 **/ 6 6 7 7 /* 8 Copyright (C) 2001 -2006Anders Hedstrom8 Copyright (C) 2001 Anders Hedstrom 9 9 10 10 This program is made available under the terms of the GNU GPL. … … 37 37 #include <string> 38 38 #include <map> 39 #ifdef WIN32 40 #include <config-win.h> 41 #include <mysql.h> 42 #else 39 43 #include <stdio.h> 40 44 #include <stdlib.h> 41 45 #include <string.h> 42 #include < sqlite3.h>46 #include <mysql/mysql.h> 43 47 #include <stdarg.h> 48 #endif 44 49 45 50 #include "sql_IError.h" … … 47 52 48 53 49 #ifdef SQLITEW_NAMESPACE50 namespace SQLITEW_NAMESPACE {54 #ifdef MYSQLW_NAMESPACE 55 namespace MYSQLW_NAMESPACE { 51 56 #endif 52 57 … … 72 77 73 78 79 Database::Database(const std::string& h,const std::string& u,const std::string& p,const std::string& d,IError *e) 80 :host(h) 81 ,user(u) 82 ,password(p) 83 ,database(d) 84 ,m_errhandler(e) 85 ,m_embedded(false) 86 ,m_mutex(m_mutex) 87 ,m_b_use_mutex(false) 88 { 89 } 90 91 92 Database::Database(Mutex& m,const std::string& h,const std::string& u,const std::string& p,const std::string& d,IError *e) 93 :host(h) 94 ,user(u) 95 ,password(p) 96 ,database(d) 97 ,m_errhandler(e) 98 ,m_embedded(false) 99 ,m_mutex(m) 100 ,m_b_use_mutex(true) 101 { 102 } 103 104 74 105 Database::~Database() 75 106 { … … 77 108 { 78 109 OPENDB *p = *it; 79 sqlite3_close(p -> db);110 mysql_close(&p -> mysql); 80 111 } 81 112 while (m_opendbs.size()) … … 93 124 94 125 126 void Database::OnMyInit(OPENDB *odb) 127 { 128 // using embedded server (libmysqld) 129 if (m_embedded) 130 { 131 mysql_options(&odb -> mysql, MYSQL_READ_DEFAULT_GROUP, "test_libmysqld_CLIENT"); 132 } 133 } 134 135 95 136 void Database::RegErrHandler(IError *p) 96 137 { … … 124 165 return NULL; 125 166 } 126 int rc = sqlite3_open(database.c_str(), &odb -> db); 127 if (rc) 128 { 129 error("Can't open database: %s\n", sqlite3_errmsg(odb -> db)); 130 sqlite3_close(odb -> db); 167 if (!mysql_init(&odb -> mysql)) 168 { 169 error("mysql_init() failed - list size %d",m_opendbs.size()); 131 170 delete odb; 132 171 return NULL; 133 172 } 173 // use callback to set mysql_options() before connect, etc 174 this -> OnMyInit(odb); 175 if (m_embedded) 176 { 177 if (!mysql_real_connect(&odb -> mysql,NULL,NULL,NULL,database.c_str(),0,NULL,0) ) 178 { 179 error("mysql_real_connect(NULL,NULL,NULL,%s,0,NULL,0) failed - list size %d",database.c_str(),m_opendbs.size()); 180 delete odb; 181 return NULL; 182 } 183 } 184 else 185 { 186 if (!mysql_real_connect(&odb -> mysql,host.c_str(),user.c_str(),password.c_str(),database.c_str(),0,NULL,0) ) 187 { 188 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()); 189 delete odb; 190 return NULL; 191 } 192 } 134 193 odb -> busy = true; 135 194 m_opendbs.push_back(odb); … … 137 196 else 138 197 { 198 if (mysql_ping(&odb -> mysql)) 199 { 200 error("mysql_ping() failed when reusing an old connection from the connection pool"); 201 } 139 202 odb -> busy = true; 140 203 } … … 189 252 190 253 191 void Database::error(Query& q,const std::string& msg)192 {193 if (m_errhandler)194 {195 m_errhandler -> error(*this, q, msg);196 }197 }198 199 200 254 bool Database::Connected() 201 255 { … … 205 259 return false; 206 260 } 261 int ping_result = mysql_ping(&odb -> mysql); 262 if (ping_result) 263 { 264 error("mysql_ping() failed"); 265 } 207 266 freedb(odb); 208 return true;267 return ping_result ? false : true; 209 268 } 210 269 … … 279 338 case '\\': 280 339 case 34: 281 str2 += '\ '';340 str2 += '\\'; 282 341 default: 342 str2 += str[i]; 343 } 344 } 345 return str2; 346 } 347 348 349 std::string Database::unsafestr(const std::string& str) 350 { 351 std::string str2; 352 for (size_t i = 0; i < str.size(); i++) 353 { 354 if (str[i] == '\\') 355 { 356 i++; 357 } 358 if (i < str.size()) 359 { 283 360 str2 += str[i]; 284 361 } … … 346 423 } 347 424 348 349 #ifdef SQLITEW_NAMESPACE 350 } // namespace SQLITEW_NAMESPACE { 351 #endif 352 425 // Added Michael Griffin 11/15/09 426 // Custom for Inserting Clean Char * 427 char *Database::strip_escape(OPENDB *qodb, char* Oldstr) { 428 429 char* Newstr = NULL; 430 int ilen=0; 431 432 if(!Oldstr) 433 return NULL; 434 435 Newstr = new char[((2 * strlen(Oldstr))+1)]; 436 437 ilen=mysql_real_escape_string(&qodb -> mysql, 438 (char *)Newstr, 439 (char *)Oldstr, 440 (long unsigned int)strlen((char *)Oldstr)); 441 442 Newstr[ilen] = 0; 443 return (Newstr); 444 } 445 446 447 #ifdef MYSQLW_NAMESPACE 448 } // namespace MYSQLW_NAMESPACE { 449 #endif 450
Enthral BBS Software for *nix/bsd/osx