2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2005-06-14 15:06:59 +00:00
|
|
|
|
|
|
|
//$Id: connection.hpp 17 2005-03-08 23:58:43Z pavlenko $
|
|
|
|
|
|
|
|
#ifndef CONNECTION_HPP
|
|
|
|
#define CONNECTION_HPP
|
|
|
|
|
|
|
|
|
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "resultset.hpp"
|
|
|
|
|
|
|
|
using namespace mapnik;
|
|
|
|
|
|
|
|
class ResultSet;
|
|
|
|
|
|
|
|
class Connection
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
PGconn *conn_;
|
|
|
|
public:
|
|
|
|
Connection(const std::string& uri,const std::string& dbname,
|
|
|
|
const std::string& username,const std::string& password)
|
|
|
|
{
|
|
|
|
std::string connStr="host="+uri+" dbname="+dbname+" user="+username+" password="+password;
|
|
|
|
conn_=PQconnectdb(connStr.c_str());
|
|
|
|
if (PQstatus(conn_) == CONNECTION_BAD)
|
|
|
|
{
|
2006-03-19 21:53:47 +00:00
|
|
|
std::clog << "connection to "<< connStr << " failed\n"
|
2005-06-14 15:06:59 +00:00
|
|
|
<< PQerrorMessage(conn_)<< std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-19 21:53:47 +00:00
|
|
|
std::clog <<"connected ok "<<std::endl;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool execute(const std::string& sql) const
|
|
|
|
{
|
|
|
|
PGresult *result=PQexec(conn_,sql.c_str());
|
|
|
|
bool ok=(result && PQresultStatus(result)==PGRES_COMMAND_OK);
|
|
|
|
PQclear(result);
|
|
|
|
return ok;
|
|
|
|
}
|
2005-12-12 13:15:33 +00:00
|
|
|
boost::shared_ptr<ResultSet> executeQuery(const std::string& sql,int type=0) const
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
|
|
|
PGresult *result=0;
|
|
|
|
if (type==1)
|
|
|
|
{
|
|
|
|
result=PQexecParams(conn_,sql.c_str(),0,0,0,0,0,1);
|
2005-12-12 13:15:33 +00:00
|
|
|
return boost::shared_ptr<ResultSet>(new ResultSet(result));
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
result=PQexec(conn_,sql.c_str());
|
2005-12-12 13:15:33 +00:00
|
|
|
return boost::shared_ptr<ResultSet>(new ResultSet(result));
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
|
|
|
bool isOK() const
|
|
|
|
{
|
|
|
|
return (PQstatus(conn_)!=CONNECTION_BAD);
|
|
|
|
}
|
|
|
|
void close()
|
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
|
|
|
}
|
|
|
|
~Connection()
|
|
|
|
{
|
|
|
|
PQfinish(conn_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //CONNECTION_HPP
|