root/trunk/pdns/modules/gmysqlbackend/smysql.cc @ 477

Revision 477, 2.7 KB (checked in by ahu, 9 years ago)

Move from u_intxx_t to uintxx_t typedefs, which are preferred:
 http://lists.freedesktop.org/pipermail/release-wranglers/2004-August/000926.html
 http://www.oreillynet.com/pub/a/network/2003/10/07/michael_barr.html

massive removal of windows style line-endings

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