Show
Ignore:
Timestamp:
11/18/09 04:40:30 (3 years ago)
Author:
mercyful
Message:

$ Build 438/Sql Alpha
+ Removed all SQLite Code and References.
+ Updated the SQL Wrapper, and all Automake files to now link in and also Detect MySql? in the configure scripts.
+ Added detection for OpenSSL, not in use yet, but will add encryption for passwords.

and anything else of intertest once everything is moved over to the SQL Backend.

This is still an interm develop update release, the MySql? backend
is still a WIP, and the Husky SMAPI Library will be removed shortly.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/sql_Database.cpp

    r127 r129  
    1 /* 
     1/** 
    22 **     Database.cpp 
    33 ** 
    4  **     Published / author: 2005-08-12 / grymse@alhem.net 
     4 **     Published / author: 2001-02-15 / grymse@alhem.net 
    55 **/ 
    66 
    77/* 
    8 Copyright (C) 2001-2006  Anders Hedstrom 
     8Copyright (C) 2001  Anders Hedstrom 
    99 
    1010This program is made available under the terms of the GNU GPL. 
     
    3737#include <string> 
    3838#include <map> 
     39#ifdef WIN32 
     40#include <config-win.h> 
     41#include <mysql.h> 
     42#else 
    3943#include <stdio.h> 
    4044#include <stdlib.h> 
    4145#include <string.h> 
    42 #include <sqlite3.h> 
     46#include <mysql/mysql.h> 
    4347#include <stdarg.h> 
     48#endif 
    4449 
    4550#include "sql_IError.h" 
     
    4752 
    4853 
    49 #ifdef SQLITEW_NAMESPACE 
    50 namespace SQLITEW_NAMESPACE { 
     54#ifdef MYSQLW_NAMESPACE 
     55namespace MYSQLW_NAMESPACE { 
    5156#endif 
    5257 
     
    7277 
    7378 
     79Database::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 
     92Database::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 
    74105Database::~Database() 
    75106{ 
     
    77108        { 
    78109                OPENDB *p = *it; 
    79                 sqlite3_close(p -> db); 
     110                mysql_close(&p -> mysql); 
    80111        } 
    81112        while (m_opendbs.size()) 
     
    93124 
    94125 
     126void 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 
    95136void Database::RegErrHandler(IError *p) 
    96137{ 
     
    124165                        return NULL; 
    125166                } 
    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()); 
    131170                        delete odb; 
    132171                        return NULL; 
    133172                } 
     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                } 
    134193                odb -> busy = true; 
    135194                m_opendbs.push_back(odb); 
     
    137196        else 
    138197        { 
     198                if (mysql_ping(&odb -> mysql)) 
     199                { 
     200                        error("mysql_ping() failed when reusing an old connection from the connection pool"); 
     201                } 
    139202                odb -> busy = true; 
    140203        } 
     
    189252 
    190253 
    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  
    200254bool Database::Connected() 
    201255{ 
     
    205259                return false; 
    206260        } 
     261        int ping_result = mysql_ping(&odb -> mysql); 
     262        if (ping_result) 
     263        { 
     264                error("mysql_ping() failed"); 
     265        } 
    207266        freedb(odb); 
    208         return true; 
     267        return ping_result ? false : true; 
    209268} 
    210269 
     
    279338                case '\\': 
    280339                case 34: 
    281                         str2 += '\''; 
     340                        str2 += '\\'; 
    282341                default: 
     342                        str2 += str[i]; 
     343                } 
     344        } 
     345        return str2; 
     346} 
     347 
     348 
     349std::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                { 
    283360                        str2 += str[i]; 
    284361                } 
     
    346423} 
    347424 
    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 * 
     427char *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