1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/* Copyright 2003 - 2005 Netherlabs BV, bert.hubert@netherlabs.nl. See LICENSE
for more information. */
#include <string>
#include "spgsql.hh"
#include <iostream>
#include "pdns/logger.hh"
#include "pdns/dns.hh"
#include "pdns/namespaces.hh"
bool SPgSQL::s_dolog;
SPgSQL::SPgSQL(const string &database, const string &host, const string& port, const string &user,
const string &password)
{
d_db=0;
d_connectstr="";
if (!database.empty())
d_connectstr+="dbname="+database;
if (!user.empty())
d_connectstr+=" user="+user;
if(!host.empty())
d_connectstr+=" host="+host;
if(!port.empty())
d_connectstr+=" port="+port;
d_connectlogstr=d_connectstr;
if(!password.empty()) {
d_connectlogstr+=" password=<HIDDEN>";
d_connectstr+=" password="+password;
}
ensureConnect();
}
void SPgSQL::setLog(bool state)
{
s_dolog=state;
}
SPgSQL::~SPgSQL()
{
PQfinish(d_db);
}
SSqlException SPgSQL::sPerrorException(const string &reason)
{
return SSqlException(reason+string(": ")+(d_db ? PQerrorMessage(d_db) : "no connection"));
}
void SPgSQL::ensureConnect()
{
if(d_db)
PQfinish(d_db);
d_db=PQconnectdb(d_connectstr.c_str());
if (!d_db || PQstatus(d_db)==CONNECTION_BAD) {
try {
throw sPerrorException("Unable to connect to database, connect string: "+d_connectlogstr);
}
catch(...) {
if(d_db)
PQfinish(d_db);
d_db = 0;
throw;
}
}
}
int SPgSQL::doCommand(const string &query)
{
if(s_dolog)
L<<Logger::Warning<<"Command: "<<query<<endl;
bool first = true;
retry:
if(!(d_result=PQexec(d_db,query.c_str())) || PQresultStatus(d_result)!=PGRES_COMMAND_OK) {
string error("unknown reason");
if(d_result) {
error=PQresultErrorMessage(d_result);
PQclear(d_result);
}
if(PQstatus(d_db)==CONNECTION_BAD) {
ensureConnect();
if(first) {
first = false;
goto retry;
}
}
throw SSqlException("PostgreSQL failed to execute command: "+error);
}
if(d_result)
PQclear(d_result);
d_count=0;
return 0;
}
int SPgSQL::doQuery(const string &query)
{
if(s_dolog)
L<<Logger::Warning<<"Query: "<<query<<endl;
bool first = true;
retry:
if(!(d_result=PQexec(d_db,query.c_str())) || PQresultStatus(d_result)!=PGRES_TUPLES_OK) {
string error("unknown reason");
if(d_result) {
error=PQresultErrorMessage(d_result);
PQclear(d_result);
}
if(PQstatus(d_db)==CONNECTION_BAD) {
ensureConnect();
if(first) {
first = false;
goto retry;
}
}
throw SSqlException("PostgreSQL failed to execute command: "+error);
}
d_count=0;
return 0;
}
int SPgSQL::doQuery(const string &query, result_t &result)
{
result.clear();
if(s_dolog)
L<<Logger::Warning<<"Query: "<<query<<endl;
if(!(d_result=PQexec(d_db,query.c_str())) || PQresultStatus(d_result)!=PGRES_TUPLES_OK) {
string error("unknown reason");
if(d_result) {
error=PQresultErrorMessage(d_result);
PQclear(d_result);
}
throw SSqlException("PostgreSQL failed to execute command: "+error);
}
d_count=0;
row_t row;
while(getRow(row))
result.push_back(row);
return result.size();
}
bool SPgSQL::getRow(row_t &row)
{
row.clear();
if(d_count >= PQntuples(d_result)) {
PQclear(d_result);
return false;
}
for(int i=0;i<PQnfields(d_result);i++)
row.push_back(PQgetvalue(d_result,d_count,i) ?: "");
d_count++;
return true;
}
string SPgSQL::escape(const string &name)
{
string a;
for(string::const_iterator i=name.begin();i!=name.end();++i) {
if(*i=='\'' || *i=='\\')
a+='\\';
a+=*i;
}
return a;
}
|