| [6] | 1 | /* Copyright 2001 Netherlabs BV, bert.hubert@netherlabs.nl. See LICENSE |
|---|
| 2 | for more information. |
|---|
| [477] | 3 | $Id$ */ |
|---|
| [6] | 4 | #include "smysql.hh" |
|---|
| 5 | #include <string> |
|---|
| 6 | #include <iostream> |
|---|
| [191] | 7 | #include "pdns/misc.hh" |
|---|
| [79] | 8 | #include "pdns/logger.hh" |
|---|
| 9 | #include "pdns/dns.hh" |
|---|
| [6] | 10 | using namespace std; |
|---|
| 11 | |
|---|
| 12 | bool SMySQL::s_dolog; |
|---|
| 13 | |
|---|
| [477] | 14 | SMySQL::SMySQL(const string &database, const string &host, uint16_t port, const string &msocket, const string &user, |
|---|
| [6] | 15 | const string &password) |
|---|
| 16 | { |
|---|
| 17 | mysql_init(&d_db); |
|---|
| [1459] | 18 | mysql_options(&d_db, MYSQL_READ_DEFAULT_GROUP, "client"); |
|---|
| [1461] | 19 | my_bool reconnect = 1; |
|---|
| 20 | mysql_options(&d_db, MYSQL_OPT_RECONNECT, &reconnect); |
|---|
| 21 | |
|---|
| [6] | 22 | if (!mysql_real_connect(&d_db, host.empty() ? 0 : host.c_str(), |
|---|
| 23 | user.empty() ? 0 : user.c_str(), |
|---|
| 24 | password.empty() ? 0 : password.c_str(), |
|---|
| [228] | 25 | database.c_str(), port, |
|---|
| [6] | 26 | msocket.empty() ? 0 : msocket.c_str(), |
|---|
| 27 | 0)) { |
|---|
| [228] | 28 | |
|---|
| [6] | 29 | throw sPerrorException("Unable to connect to database"); |
|---|
| 30 | } |
|---|
| [228] | 31 | |
|---|
| [6] | 32 | d_rres=0; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | void SMySQL::setLog(bool state) |
|---|
| 36 | { |
|---|
| 37 | s_dolog=state; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | SMySQL::~SMySQL() |
|---|
| 41 | { |
|---|
| 42 | mysql_close(&d_db); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | SSqlException SMySQL::sPerrorException(const string &reason) |
|---|
| 46 | { |
|---|
| 47 | return SSqlException(reason+string(": ")+mysql_error(&d_db)); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| [194] | 50 | int SMySQL::doCommand(const string &query) |
|---|
| 51 | { |
|---|
| 52 | return doQuery(query); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| [6] | 55 | int SMySQL::doQuery(const string &query) |
|---|
| 56 | { |
|---|
| 57 | if(d_rres) |
|---|
| 58 | throw SSqlException("Attempt to start new MySQL query while old one still in progress"); |
|---|
| 59 | |
|---|
| 60 | if(s_dolog) |
|---|
| 61 | L<<Logger::Warning<<"Query: "<<query<<endl; |
|---|
| 62 | |
|---|
| [191] | 63 | int err; |
|---|
| 64 | if((err=mysql_query(&d_db,query.c_str()))) |
|---|
| [207] | 65 | throw sPerrorException("Failed to execute mysql_query, perhaps connection died? Err="+itoa(err)); |
|---|
| [6] | 66 | |
|---|
| 67 | |
|---|
| 68 | return 0; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | int SMySQL::doQuery(const string &query, result_t &result) |
|---|
| 72 | { |
|---|
| 73 | result.clear(); |
|---|
| 74 | doQuery(query); |
|---|
| 75 | |
|---|
| 76 | row_t row; |
|---|
| 77 | while(getRow(row)) |
|---|
| 78 | result.push_back(row); |
|---|
| 79 | |
|---|
| 80 | return result.size(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | bool SMySQL::getRow(row_t &row) |
|---|
| 84 | { |
|---|
| 85 | row.clear(); |
|---|
| 86 | if(!d_rres) |
|---|
| 87 | if(!(d_rres = mysql_use_result(&d_db))) |
|---|
| 88 | throw sPerrorException("Failed on mysql_use_result"); |
|---|
| 89 | |
|---|
| 90 | MYSQL_ROW rrow; |
|---|
| 91 | |
|---|
| 92 | if((rrow = mysql_fetch_row(d_rres))) { |
|---|
| 93 | for(unsigned int i=0;i<mysql_num_fields(d_rres);i++) |
|---|
| 94 | row.push_back(rrow[i] ?: ""); |
|---|
| 95 | return true; |
|---|
| 96 | } |
|---|
| 97 | mysql_free_result(d_rres); |
|---|
| 98 | d_rres=0; |
|---|
| 99 | return false; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | string SMySQL::escape(const string &name) |
|---|
| 103 | { |
|---|
| 104 | string a; |
|---|
| 105 | |
|---|
| 106 | for(string::const_iterator i=name.begin();i!=name.end();++i) { |
|---|
| 107 | if(*i=='\'' || *i=='\\') |
|---|
| 108 | a+='\\'; |
|---|
| 109 | a+=*i; |
|---|
| 110 | } |
|---|
| 111 | return a; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | #if 0 |
|---|
| 116 | int main() |
|---|
| 117 | { |
|---|
| 118 | try { |
|---|
| 119 | SMySQL s("kkfnetmail","127.0.0.1","readonly"); |
|---|
| 120 | SSql::result_t juh; |
|---|
| 121 | |
|---|
| 122 | int num=s.doQuery("select *, from mboxes", juh); |
|---|
| 123 | cout<<num<<" responses"<<endl; |
|---|
| 124 | |
|---|
| 125 | for(int i=0;i<num;i++) { |
|---|
| 126 | const SSql::row_t &row=juh[i]; |
|---|
| 127 | |
|---|
| 128 | for(SSql::row_t::const_iterator j=row.begin();j!=row.end();++j) |
|---|
| 129 | cout <<"'"<< *j<<"', "; |
|---|
| 130 | cout<<endl; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | catch(SSqlException &e) { |
|---|
| 134 | cerr<<e.txtReason()<<endl; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | #endif |
|---|