2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2014-11-20 15:25:50 +01:00
|
|
|
* Copyright (C) 2014 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02: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 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02: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 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifndef POSTGIS_RESULTSET_HPP
|
|
|
|
#define POSTGIS_RESULTSET_HPP
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2011-10-23 21:23:04 +02:00
|
|
|
extern "C" {
|
2011-11-14 04:37:50 +01:00
|
|
|
#include "libpq-fe.h"
|
2011-05-10 16:22:47 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
class IResultSet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//virtual IResultSet& operator=(const IResultSet& rhs) = 0;
|
|
|
|
virtual ~IResultSet() {}
|
|
|
|
virtual void close() = 0;
|
|
|
|
virtual int getNumFields() const = 0;
|
|
|
|
virtual bool next() = 0;
|
|
|
|
virtual const char* getFieldName(int index) const = 0;
|
|
|
|
virtual int getFieldLength(int index) const = 0;
|
|
|
|
virtual int getFieldLength(const char* name) const = 0;
|
|
|
|
virtual int getTypeOID(int index) const = 0;
|
|
|
|
virtual int getTypeOID(const char* name) const = 0;
|
|
|
|
virtual bool isNull(int index) const = 0;
|
|
|
|
virtual const char* getValue(int index) const = 0;
|
|
|
|
virtual const char* getValue(const char* name) const = 0;
|
|
|
|
};
|
|
|
|
|
2013-05-16 17:08:42 +02:00
|
|
|
class ResultSet : public IResultSet, private mapnik::noncopyable
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ResultSet(PGresult *res)
|
2012-03-31 22:24:32 +02:00
|
|
|
: res_(res),
|
2013-05-16 17:08:42 +02:00
|
|
|
pos_(-1)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
numTuples_ = PQntuples(res_);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual void close()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
PQclear(res_);
|
|
|
|
res_ = 0;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual ~ResultSet()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2013-05-16 17:08:42 +02:00
|
|
|
PQclear(res_);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual int getNumFields() const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2010-11-19 00:42:33 +01:00
|
|
|
return PQnfields(res_);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
int pos() const
|
|
|
|
{
|
|
|
|
return pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int size() const
|
|
|
|
{
|
|
|
|
return numTuples_;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool next()
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
return (++pos_ < numTuples_);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual const char* getFieldName(int index) const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
return PQfname(res_, index);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual int getFieldLength(int index) const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
return PQgetlength(res_, pos_, index);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual int getFieldLength(const char* name) const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
int col = PQfnumber(res_, name);
|
|
|
|
if (col >= 0)
|
|
|
|
{
|
|
|
|
return PQgetlength(res_, pos_, col);
|
|
|
|
}
|
2010-11-19 00:42:33 +01:00
|
|
|
return 0;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual int getTypeOID(int index) const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
return PQftype(res_, index);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual int getTypeOID(const char* name) const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
int col = PQfnumber(res_, name);
|
|
|
|
if (col >= 0)
|
|
|
|
{
|
|
|
|
return PQftype(res_, col);
|
|
|
|
}
|
2010-11-19 00:42:33 +01:00
|
|
|
return 0;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual bool isNull(int index) const
|
2008-02-23 02:25:52 +01:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
return static_cast<bool>(PQgetisnull(res_, pos_, index));
|
2008-02-23 02:25:52 +01:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual const char* getValue(int index) const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
return PQgetvalue(res_, pos_, index);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-02-05 18:41:44 +01:00
|
|
|
virtual const char* getValue(const char* name) const
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2012-03-31 22:24:32 +02:00
|
|
|
int col = PQfnumber(res_, name);
|
|
|
|
if (col >= 0)
|
|
|
|
{
|
2010-11-19 00:42:33 +01:00
|
|
|
return getValue(col);
|
2012-03-31 22:24:32 +02:00
|
|
|
}
|
2010-11-19 00:42:33 +01:00
|
|
|
return 0;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2012-03-31 22:24:32 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
PGresult* res_;
|
|
|
|
int pos_;
|
|
|
|
int numTuples_;
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#endif // POSTGIS_RESULTSET_HPP
|