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
|
/* Copyright 2003 Netherlabs BV, bert.hubert@netherlabs.nl. See LICENSE
for more information.
$Id: spgsql.cc,v 1.4 2004/01/17 13:18:22 ahu Exp $ */
#include <string>
#include "spgsql.hh"
#include <iostream>
#include "pdns/logger.hh"
#include "pdns/dns.hh"
using namespace std;
bool SPgSQL::s_dolog;
SPgSQL::SPgSQL(const string &database, const string &host, const string &msocket, const string &user,
const string &password)
{
d_db=0;
string connectstr;
connectstr="dbname=";
connectstr+=database;
connectstr+=" user=";
connectstr+=user;
if(!host.empty())
connectstr+=" host="+host;
if(!password.empty())
connectstr+=" password="+password;
d_db=PQconnectdb(connectstr.c_str());
if (!d_db || PQstatus(d_db)==CONNECTION_BAD) {
try {
throw sPerrorException("Unable to connect to database, connect string: "+connectstr);
}
catch(...) {
if(d_db)
PQfinish(d_db);
throw;
}
}
}
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"));
}
int SPgSQL::doCommand(const string &query)
{
if(s_dolog)
L<<Logger::Warning<<"Command: "<<query<<endl;
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);
}
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;
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;
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;
}
|