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_Query.cpp

    r127 r129  
    1 /* 
     1/** 
    22 **     Query.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. 
     
    3636#include <string> 
    3737#include <map> 
     38#ifdef WIN32 
     39#include <config-win.h> 
     40#include <mysql.h> 
     41#else 
    3842#include <stdio.h> 
    3943#include <stdlib.h> 
    4044#include <string.h> 
    41 #include <sqlite3.h> 
     45#include <mysql/mysql.h> 
     46#endif 
    4247 
    4348#include "sql_Database.h" 
     
    4550 
    4651 
    47 #ifdef SQLITEW_NAMESPACE 
    48 namespace SQLITEW_NAMESPACE { 
     52#ifdef MYSQLW_NAMESPACE 
     53namespace MYSQLW_NAMESPACE { 
    4954#endif 
    5055 
    5156 
    52 Query::Query(Database& dbin)  
    53 : m_db(dbin) 
    54 ,odb(dbin.grabdb()) 
     57Query::Query(Database *dbin) 
     58:m_db(*dbin) 
     59,odb(dbin ? dbin -> grabdb() : NULL) 
    5560,res(NULL) 
    56 ,row(false) 
    57 ,cache_rc(0) 
    58 ,cache_rc_valid(false) 
    59 ,m_row_count(0) 
     61,row(NULL) 
    6062,m_num_cols(0) 
    6163{ 
     
    6365 
    6466 
    65 Query::Query(Database& dbin,const std::string& sql)  
    66 : m_db(dbin) 
    67 ,odb(dbin.grabdb()) 
    68 ,res(NULL) 
    69 ,row(false) 
    70 ,cache_rc(0) 
    71 ,cache_rc_valid(false) 
    72 ,m_row_count(0) 
     67Query::Query(Database& dbin) : m_db(dbin),odb(dbin.grabdb()),res(NULL),row(NULL) 
    7368,m_num_cols(0) 
    7469{ 
     70} 
     71 
     72 
     73Query::Query(Database *dbin,const std::string& sql) : m_db(*dbin) 
     74,odb(dbin ? dbin -> grabdb() : NULL),res(NULL),row(NULL) 
     75,m_num_cols(0) 
     76{ 
    7577        execute(sql); 
    7678} 
    7779 
    7880 
     81Query::Query(Database& dbin,const std::string& sql) : m_db(dbin),odb(dbin.grabdb()),res(NULL),row(NULL) 
     82,m_num_cols(0) 
     83{ 
     84        execute(sql); // returns 0 if fail 
     85} 
     86 
     87 
    7988Query::~Query() 
    8089{ 
    8190        if (res) 
    8291        { 
    83                 GetDatabase().error(*this, "sqlite3_finalize in destructor"); 
    84                 sqlite3_finalize(res); 
     92                GetDatabase().error(*this, "mysql_free_result in destructor"); 
     93                mysql_free_result(res); 
    8594        } 
    8695        if (odb) 
     
    97106 
    98107 
    99 /* 
    100 The sqlite3_finalize() routine deallocates a prepared SQL statement.  
    101 All prepared statements must be finalized before the database can be closed. 
    102 */ 
    103108bool Query::execute(const std::string& sql) 
    104 { 
    105         // query, no result 
     109{               // query, no result 
    106110        m_last_query = sql; 
    107111        if (odb && res) 
     
    111115        if (odb && !res) 
    112116        { 
    113                 const char *s = NULL; 
    114                 int rc = sqlite3_prepare(odb -> db, sql.c_str(), sql.size(), &res, &s); 
    115                 if (rc != SQLITE_OK) 
    116                 { 
    117                         GetDatabase().error(*this, "execute: prepare query failed"); 
    118                         return false; 
    119                 } 
    120                 if (!res) 
    121                 { 
    122                         GetDatabase().error(*this, "execute: query failed"); 
    123                         return false; 
    124                 } 
    125                 rc = sqlite3_step(res); // execute 
    126                 sqlite3_finalize(res); // deallocate statement 
     117                if (mysql_query(&odb -> mysql,sql.c_str())) 
     118                { 
     119                        GetDatabase().error(*this,"query failed"); 
     120                } 
     121                else 
     122                { 
     123                        return true; 
     124                } 
     125        } 
     126        return false; 
     127} 
     128 
     129 
     130 
     131// methods using db specific api calls 
     132 
     133MYSQL_RES *Query::get_result(const std::string& sql) 
     134{       // query, result 
     135        if (odb && res) 
     136        { 
     137                GetDatabase().error(*this, "get_result: query busy"); 
     138        } 
     139        if (odb && !res) 
     140        { 
     141                if (execute(sql)) 
     142                { 
     143                        res = mysql_store_result(&odb -> mysql); 
     144                        if (res) 
     145                        { 
     146                                MYSQL_FIELD *f = mysql_fetch_field(res); 
     147                                int i = 1; 
     148                                while (f) 
     149                                { 
     150                                        if (f -> name) 
     151                                                m_nmap[f -> name] = i; 
     152                                        f = mysql_fetch_field(res); 
     153                                        i++; 
     154                                } 
     155                                m_num_cols = i - 1; 
     156                        } 
     157                } 
     158        } 
     159        return res; 
     160} 
     161 
     162 
     163void Query::free_result() 
     164{ 
     165        if (odb && res) 
     166        { 
     167                mysql_free_result(res); 
    127168                res = NULL; 
    128                 switch (rc) 
    129                 { 
    130                 case SQLITE_BUSY: 
    131                         GetDatabase().error(*this, "execute: database busy"); 
    132                         return false; 
    133                 case SQLITE_DONE: 
    134                 case SQLITE_ROW: 
    135                         return true; 
    136                 case SQLITE_ERROR: 
    137                         GetDatabase().error(*this, sqlite3_errmsg(odb -> db)); 
    138                         return false; 
    139                 case SQLITE_MISUSE: 
    140                         GetDatabase().error(*this, "execute: database misuse"); 
    141                         return false; 
    142                 } 
    143                 GetDatabase().error(*this, "execute: unknown result code"); 
    144         } 
    145         return false; 
    146 } 
    147  
    148  
    149  
    150 // methods using db specific api calls 
    151  
    152 sqlite3_stmt *Query::get_result(const std::string& sql) 
    153 { 
    154         // query, result 
    155         m_last_query = sql; 
    156         if (odb && res) 
    157         { 
    158                 GetDatabase().error(*this, "get_result: query busy"); 
    159         } 
    160         if (odb && !res) 
    161         { 
    162                 const char *s = NULL; 
    163                 int rc = sqlite3_prepare(odb -> db, sql.c_str(), sql.size(), &res, &s); 
    164                 if (rc != SQLITE_OK) 
    165                 { 
    166                         GetDatabase().error(*this, "get_result: prepare query failed"); 
    167                         return NULL; 
    168                 } 
    169                 if (!res) 
    170                 { 
    171                         GetDatabase().error(*this, "get_result: query failed"); 
    172                         return NULL; 
    173                 } 
    174                 // get column names from result 
    175                 { 
    176                         int i = 0; 
    177                         do 
    178                         { 
    179                                 const char *p = sqlite3_column_name(res, i); 
    180                                 if (!p) 
    181                                         break; 
    182                                 m_nmap[p] = ++i; 
    183                         } while (true); 
    184                         m_num_cols = i; 
    185                 } 
    186                 cache_rc = sqlite3_step(res); 
    187                 cache_rc_valid = true; 
    188                 m_row_count = (cache_rc == SQLITE_ROW) ? 1 : 0; 
    189         } 
    190         return res; 
    191 } 
    192  
    193  
    194 void Query::free_result() 
    195 { 
    196         if (odb && res) 
    197         { 
    198                 sqlite3_finalize(res); 
    199                 res = NULL; 
    200                 row = false; 
    201                 cache_rc_valid = false; 
    202         } 
    203         // clear column names 
     169                row = NULL; 
     170        } 
    204171        while (m_nmap.size()) 
    205172        { 
     
    207174                m_nmap.erase(it); 
    208175        } 
    209 } 
    210  
    211  
    212 bool Query::fetch_row() 
     176        m_num_cols = 0; 
     177} 
     178 
     179 
     180MYSQL_ROW Query::fetch_row() 
    213181{ 
    214182        rowcount = 0; 
    215         row = false; 
    216         if (odb && res) 
    217         { 
    218                 int rc = cache_rc_valid ? cache_rc : sqlite3_step(res); // execute 
    219                 cache_rc_valid = false; 
    220                 switch (rc) 
    221                 { 
    222                 case SQLITE_BUSY: 
    223                         GetDatabase().error(*this, "execute: database busy"); 
    224                         return false; 
    225                 case SQLITE_DONE: 
    226                         return false; 
    227                 case SQLITE_ROW: 
    228                         row = true; 
    229                         return true; 
    230                 case SQLITE_ERROR: 
    231                         GetDatabase().error(*this, sqlite3_errmsg(odb -> db)); 
    232                         return false; 
    233                 case SQLITE_MISUSE: 
    234                         GetDatabase().error(*this, "execute: database misuse"); 
    235                         return false; 
    236                 } 
    237                 GetDatabase().error(*this, "execute: unknown result code"); 
    238         } 
     183        return odb && res ? row = mysql_fetch_row(res) : NULL; 
     184} 
     185 
     186 
     187my_ulonglong Query::insert_id() 
     188{ 
     189        if (odb) 
     190        { 
     191                return mysql_insert_id(&odb -> mysql); 
     192        } 
     193        else 
     194        { 
     195                return 0; 
     196        } 
     197} 
     198 
     199 
     200long Query::num_rows() 
     201{ 
     202        return odb && res ? mysql_num_rows(res) : 0; 
     203} 
     204 
     205 
     206int Query::num_cols() 
     207{ 
     208        return m_num_cols; 
     209} 
     210 
     211 
     212bool Query::is_null(int x) 
     213{ 
     214        if (odb && res && row) 
     215        { 
     216                return row[x] ? false : true; 
     217        } 
     218        return false; // ... 
     219} 
     220 
     221 
     222bool Query::is_null(const std::string& x) 
     223{ 
     224        int index = m_nmap[x] - 1; 
     225        if (index >= 0) 
     226                return is_null(index); 
     227        error("Column name lookup failure: " + x); 
    239228        return false; 
    240229} 
    241230 
    242231 
    243 sqlite_int64 Query::insert_id() 
    244 { 
    245         if (odb) 
    246         { 
    247                 return sqlite3_last_insert_rowid(odb -> db); 
    248         } 
    249         else 
    250         { 
    251                 return 0; 
    252         } 
    253 } 
    254  
    255  
    256 long Query::num_rows() 
    257 { 
    258         return odb && res ? m_row_count : 0; 
    259 } 
    260  
    261  
    262 int Query::num_cols() 
    263 { 
    264         return m_num_cols; 
    265 } 
    266  
    267  
    268 bool Query::is_null(int x) 
     232bool Query::is_null() 
     233{ 
     234        return is_null(rowcount++); 
     235} 
     236 
     237 
     238const char *Query::getstr(const std::string& x) 
     239{ 
     240        int index = m_nmap[x] - 1; 
     241        if (index >= 0) 
     242                return getstr(index); 
     243        error("Column name lookup failure: " + x); 
     244        return NULL; 
     245} 
     246 
     247 
     248const char *Query::getstr(int x) 
    269249{ 
    270250        if (odb && res && row) 
    271251        { 
    272                 if (sqlite3_column_type(res, x) == SQLITE_NULL) 
    273                         return true; 
    274         } 
    275         return false; // ... 
    276 } 
    277  
    278  
    279 const char *Query::getstr(const std::string& x) 
    280 { 
    281         int index = m_nmap[x] - 1; 
    282         if (index >= 0) 
    283                 return getstr(index); 
    284         error("Column name lookup failure: " + x); 
    285         return ""; 
    286 } 
    287  
    288  
    289 const char *Query::getstr(int x) 
    290 { 
    291         if (odb && res && row && x < sqlite3_column_count(res) ) 
    292         { 
    293                 const unsigned char *tmp = sqlite3_column_text(res, x); 
    294                 return tmp ? (const char *)tmp : ""; 
    295         } 
    296         return ""; 
     252                return row[x] ? row[x] : ""; 
     253        } 
     254        return NULL; 
    297255} 
    298256 
     
    316274double Query::getnum(int x) 
    317275{ 
    318         if (odb && res && row) 
    319         { 
    320                 return sqlite3_column_double(res, x); 
    321         } 
     276        return odb && res && row && row[x] ? atof(row[x]) : 0; 
     277} 
     278 
     279 
     280long Query::getval(const std::string& x) 
     281{ 
     282        int index = m_nmap[x] - 1; 
     283        if (index >= 0) 
     284                return getval(index); 
     285        error("Column name lookup failure: " + x); 
    322286        return 0; 
    323287} 
    324288 
    325289 
    326 long Query::getval(const std::string& x) 
    327 { 
    328         int index = m_nmap[x] - 1; 
    329         if (index >= 0) 
    330                 return getval(index); 
     290long Query::getval(int x) 
     291{ 
     292        return odb && res && row && row[x] ? atol(row[x]) : 0; 
     293} 
     294 
     295 
     296double Query::getnum() 
     297{ 
     298        return getnum(rowcount++); 
     299} 
     300 
     301 
     302long Query::getval() 
     303{ 
     304        return getval(rowcount++); 
     305} 
     306 
     307 
     308unsigned long Query::getuval(const std::string& x) 
     309{ 
     310        int index = m_nmap[x] - 1; 
     311        if (index >= 0) 
     312                return getuval(index); 
    331313        error("Column name lookup failure: " + x); 
    332314        return 0; 
     
    334316 
    335317 
    336 long Query::getval(int x) 
    337 { 
    338         if (odb && res && row) 
    339         { 
    340                 return sqlite3_column_int(res, x); 
    341         } 
     318unsigned long Query::getuval(int x) 
     319{ 
     320        unsigned long l = 0; 
     321        if (odb && res && row && row[x]) 
     322        { 
     323                l = m_db.a2ubigint(row[x]); 
     324        } 
     325        return l; 
     326} 
     327 
     328 
     329unsigned long Query::getuval() 
     330{ 
     331        return getuval(rowcount++); 
     332} 
     333 
     334 
     335int64_t Query::getbigint(const std::string& x) 
     336{ 
     337        int index = m_nmap[x] - 1; 
     338        if (index >= 0) 
     339                return getbigint(index); 
     340        error("Column name lookup failure: " + x); 
    342341        return 0; 
    343342} 
    344343 
    345344 
    346 double Query::getnum() 
    347 { 
    348         return getnum(rowcount++); 
    349 } 
    350  
    351  
    352 long Query::getval() 
    353 { 
    354         return getval(rowcount++); 
    355 } 
    356  
    357  
    358 unsigned long Query::getuval(const std::string& x) 
    359 { 
    360         int index = m_nmap[x] - 1; 
    361         if (index >= 0) 
    362                 return getuval(index); 
     345int64_t Query::getbigint(int x) 
     346{ 
     347        return odb && res && row && row[x] ? m_db.a2bigint(row[x]) : 0; 
     348} 
     349 
     350 
     351int64_t Query::getbigint() 
     352{ 
     353        return getbigint(rowcount++); 
     354} 
     355 
     356 
     357uint64_t Query::getubigint(const std::string& x) 
     358{ 
     359        int index = m_nmap[x] - 1; 
     360        if (index >= 0) 
     361                return getubigint(index); 
    363362        error("Column name lookup failure: " + x); 
    364363        return 0; 
     
    366365 
    367366 
    368 unsigned long Query::getuval(int x) 
    369 { 
    370         unsigned long l = 0; 
    371         if (odb && res && row) 
    372         { 
    373                 l = sqlite3_column_int(res, x); 
    374         } 
    375         return l; 
    376 } 
    377  
    378  
    379 unsigned long Query::getuval() 
    380 { 
    381         return getuval(rowcount++); 
    382 } 
    383  
    384  
    385 int64_t Query::getbigint(const std::string& x) 
    386 { 
    387         int index = m_nmap[x] - 1; 
    388         if (index >= 0) 
    389                 return getbigint(index); 
    390         error("Column name lookup failure: " + x); 
    391         return 0; 
    392 } 
    393  
    394  
    395 int64_t Query::getbigint(int x) 
    396 { 
    397         if (odb && res && row) 
    398         { 
    399                 return sqlite3_column_int64(res, x); 
    400         } 
    401         return 0; 
    402 } 
    403  
    404  
    405 int64_t Query::getbigint() 
    406 { 
    407         return getbigint(rowcount++); 
    408 } 
    409  
    410  
    411 uint64_t Query::getubigint(const std::string& x) 
    412 { 
    413         int index = m_nmap[x] - 1; 
    414         if (index >= 0) 
    415                 return getubigint(index); 
    416         error("Column name lookup failure: " + x); 
    417         return 0; 
    418 } 
    419  
    420  
    421367uint64_t Query::getubigint(int x) 
    422368{ 
    423         uint64_t l = 0; 
    424         if (odb && res && row) 
    425         { 
    426                 l = sqlite3_column_int64(res, x); 
    427         } 
    428         return l; 
     369        return odb && res && row && row[x] ? m_db.a2ubigint(row[x]) : 0; 
    429370} 
    430371 
     
    489430std::string Query::GetError() 
    490431{ 
     432        return odb ? mysql_error(&odb -> mysql) : ""; 
     433} 
     434 
     435 
     436int Query::GetErrno() 
     437{ 
     438        return odb ? mysql_errno(&odb -> mysql) : 0; 
     439} 
     440 
     441 
     442bool Query::Connected() 
     443{ 
    491444        if (odb) 
    492                 return sqlite3_errmsg(odb -> db); 
    493         return ""; 
    494 } 
    495  
    496  
    497 int Query::GetErrno() 
    498 { 
    499         if (odb) 
    500                 return sqlite3_errcode(odb -> db); 
    501         return 0; 
    502 } 
    503  
    504  
    505 bool Query::Connected() 
    506 { 
     445        { 
     446                if (mysql_ping(&odb -> mysql)) 
     447                { 
     448                        GetDatabase().error(*this, "mysql_ping() failed"); 
     449                        return false; 
     450                } 
     451        } 
    507452        return odb ? true : false; 
    508453} 
    509454 
    510455 
    511  
    512  
    513 void Query::ViewRes() 
    514 { 
    515         if (!res) 
    516         { 
    517                 printf("no result stored\n"); 
    518                 return; 
    519         } 
    520         printf("result column count = %d\n", sqlite3_column_count(res)); 
    521         for (int i = 0; i < sqlite3_column_count(res); i++) 
    522         { 
    523                 printf(" %2d   type %d   name '%s'", i, sqlite3_column_type(res, i), sqlite3_column_name(res, i)); 
    524                 printf("  / '%s'", (char *)sqlite3_column_text(res, i)); 
    525                 printf("  / %d", sqlite3_column_int(res, i)); 
    526                 printf("  / %f", sqlite3_column_double(res, i)); 
    527                 printf("\n"); 
    528         } 
    529 } 
    530  
    531  
    532 void Query::error(const std::string& msg) 
    533 { 
    534         GetDatabase().error(*this, msg); 
    535 } 
    536  
    537  
    538 #ifdef SQLITEW_NAMESPACE 
    539 } // namespace SQLITEW_NAMESPACE { 
     456void Query::error(const std::string& x) 
     457{ 
     458        m_db.error(*this, x.c_str()); 
     459} 
     460 
     461 
     462std::string Query::safestr(const std::string& x) 
     463{ 
     464        return m_db.safestr(x); 
     465} 
     466 
     467// Added Michael Griffin 11/15/09 
     468// Custom 'mysql_real_escape_string()' 
     469char *Query::strip(char* Oldstr)  
     470{ 
     471    return m_db.strip_escape(odb,Oldstr); 
     472} 
     473 
     474 
     475#ifdef MYSQLW_NAMESPACE 
     476} // namespace MYSQLW_NAMESPACE { 
    540477#endif 
    541478