27 #include "../Defines.h" 
   28 #include "../common/ByteSwapUtils.h" 
   29 #include "../common/Globals.h" 
   30 #include "../common/StringUtils.h" 
   31 #include "../common/Translator.h" 
   32 #include "../dataaccess/dataset/DataSetType.h" 
   33 #include "../datatype/Array.h" 
   34 #include "../datatype/ByteArray.h" 
   35 #include "../datatype/DateTime.h" 
   36 #include "../datatype/SimpleData.h" 
   37 #include "../geometry/Geometry.h" 
   52 #include <boost/dynamic_bitset.hpp> 
   77     template<
class T, 
class ALIGN> 
inline void GetArray(
int i, 
int j, 
PGresult* result, std::vector<T>& a)
 
   79       char* value = PQgetvalue(result, i, j);
 
   81       int ndim = *((
int*)value);
 
   84       int dataoffset = *((
int*)value);
 
   87       unsigned int elemtype = *((
unsigned int*)value);
 
   88       value += 
sizeof(
unsigned int);
 
   90       int dimension = *((
int*)value);
 
   93       int lowerbnds = *((
int*)value);
 
   94       value += (2 * 
sizeof(int)); 
 
  104         a.reserve(dimension);
 
  106         for(
int k = 0; k < dimension; ++k)
 
  111           value += 
sizeof(ALIGN);
 
  116         a.reserve(dimension);
 
  118         for(
int k = 0; k < dimension; ++k)
 
  122           value += 
sizeof(ALIGN);
 
  131                            const std::vector<int>& ptypes,
 
  138     m_timeIsInteger(timeIsInteger),
 
  161   return PQnfields(m_result);
 
  171   return PQfname(m_result, i);
 
  176   assert(i < m_ptypes.size());
 
  184   throw Exception(
TE_TR(
"Not implemented yet!"));
 
  189   return (m_size == 0);
 
  207       throw Exception(
TE_TR(
"This driver only supports the getExtent method over a geometry column!"));
 
  214       std::auto_ptr<te::gm::Geometry> geom(getGeometry(i));
 
  215       m_mbr->Union(*(geom->getMBR()));
 
  223   return std::auto_ptr<te::gm::Envelope>(mbr);
 
  229   return (m_i < m_size);
 
  247   return (m_i < m_size);
 
  258   m_i = 
static_cast<int>(i);
 
  259   return (m_i < m_size);
 
  274   return m_i == (m_size - 1);
 
  279   return m_i >= m_size;
 
  284   char cval = *(PQgetvalue(m_result, m_i, i));
 
  290   unsigned char cval = *(PQgetvalue(m_result, m_i, i));
 
  296   short int ival = *((
short int*)(PQgetvalue(m_result, m_i, i)));
 
  298 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  307   int ival = *((
int*)(PQgetvalue(m_result, m_i, i)));
 
  309 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  318   long long int ival = *((
long long int*)(PQgetvalue(m_result, m_i, i)));
 
  320 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  329   char bval = *(PQgetvalue(m_result, m_i, i));
 
  335   float fval = *((
float*)(PQgetvalue(m_result, m_i, i)));
 
  337 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  346   double dval = *((
double*)(PQgetvalue(m_result, m_i, i)));
 
  348 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  357   char* val = PQgetvalue(m_result, m_i, i);
 
  361   unsigned short totalGroups; 
 
  363   totalGroups = *((
unsigned short*)val);
 
  365 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  373   unsigned short decimalGroups; 
 
  375   decimalGroups = *((
unsigned short*)(val+2));
 
  377 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  383   decimalGroups = totalGroups - decimalGroups;
 
  388   sign = *((
unsigned short*)(val+4));
 
  390 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  394   if(!(sign == 0 || sign == 0x4000))
 
  407   while(ii < ((totalGroups - decimalGroups) * 2))
 
  409     n = *((
unsigned short*)(val + ii + 8));
 
  411 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  426   if(decimalGroups == 0)
 
  430     while(ii < (totalGroups * 2))
 
  432       n = *((
unsigned short*)(val + ii + 8));
 
  434 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  440       while (newVal.length() < 4) 
 
  441         newVal = 
"0" + newVal;
 
  448     while((decVal.empty() == 
false) && (decVal[decVal.length() - 1] == 
'0'))
 
  449       decVal.erase(decVal.length() - 1);
 
  452   return intVal + 
"." + decVal;
 
  457   std::string value(PQgetvalue(m_result, m_i, i));
 
  463   int size = PQgetlength(m_result, m_i, i);
 
  466   b->
copy(PQgetvalue(m_result, m_i, i), size);
 
  467   return std::auto_ptr<te::dt::ByteArray>(b);
 
  472   return std::auto_ptr<te::gm::Geometry>(
EWKBReader::read(PQgetvalue(m_result, m_i, i)));
 
  477   return std::auto_ptr<te::rst::Raster>(0);
 
  482   Oid tid = PQftype(m_result, i);
 
  483   boost::int64_t ival = 0;
 
  499           ival = (boost::int64_t)(dval * 1000000);
 
  507         char* val = PQgetvalue(m_result, m_i, i);
 
  508         lval = *((
long int*)val);
 
  510 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  524           ival = (boost::int64_t)(dval * 1000000);      
 
  536           char* c = (
char*)PQgetvalue(m_result, m_i, i);
 
  537           dval = *((
double*)c);
 
  539           #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  543           ival = (boost::int64_t)(dval * 1000000);
 
  556           char* c = (
char*)PQgetvalue(m_result, m_i, i);
 
  557           dval = *((
double*)c);
 
  561           #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  566           ival = (boost::int64_t)(dval * 1000000);
 
  577       throw Exception(
TE_TR(
"This type is not supported by TerraLib!"));
 
  583   char* value = PQgetvalue(m_result, m_i, i);
 
  585   int ndim = *((
int*)value);
 
  586   value += 
sizeof(int);
 
  588 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  592   int dataoffset = *((
int*)value);
 
  593   value += 
sizeof(int);
 
  595 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  599   unsigned int elemtype = *((
unsigned int*)value);
 
  600   value += 
sizeof(
unsigned int);
 
  602 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  606   int* dimensions = (
int*)value;
 
  607   value += ndim * 
sizeof(int);
 
  609   int* lowerbounds = (
int*)value;
 
  610   value += ndim * 
sizeof(int);
 
  612   uint32_t* null_bitmap = (
unsigned int*)value;
 
  614   boost::dynamic_bitset<> mask;
 
  618     int nmasks = (dataoffset + 3) / 4;
 
  620     mask.resize(nmasks * 8 * 
sizeof(uint32_t));
 
  624     for(
int i = 0; i != nmasks; ++i)
 
  626       value += 
sizeof(uint32_t);
 
  628       uint32_t umask = null_bitmap[i];
 
  630 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  634       for(
int j = 0; j != 32; ++j)
 
  636         mask[pos] = ((umask >> j) & 1) == 0;
 
  643     value += 
sizeof(uint32_t);
 
  654         std::vector<std::size_t> pos(ndim, 0);
 
  656         for(
int d = 0; d != ndim; ++d)
 
  658           int d_size = dimensions[d];
 
  660 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  663           int d_lower_boundary = lowerbounds[d];
 
  665 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  669           for(i = 0; i != d_size; ++i)
 
  671             if((dataoffset != 0) && (mask[i] == 
false))
 
  679             int text_size = *(
int*)value;
 
  681 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  684             value += 
sizeof(int);
 
  692         return std::auto_ptr<te::dt::Array>(arr.release());
 
  700         std::vector<std::size_t> pos(ndim, 0);
 
  702         for(
int d = 0; d != ndim; ++d)
 
  704           int d_size = dimensions[d];
 
  706 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  709           int d_lower_boundary = lowerbounds[d];
 
  711 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  715           for(i = 0; i != d_size; ++i)
 
  717             if((dataoffset != 0) && (mask[i] == 
false))
 
  725             double val = *(
double*)value;
 
  727 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  733             value += 
sizeof(double);
 
  737         return std::auto_ptr<te::dt::Array>(arr.release());
 
  745         std::vector<std::size_t> pos(ndim, 0);
 
  747         for(
int d = 0; d != ndim; ++d)
 
  749           int d_size = dimensions[d];
 
  751 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  754           int d_lower_boundary = lowerbounds[d];
 
  756 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  760           for(i = 0; i != d_size; ++i)
 
  762             if((dataoffset != 0) && (mask[i] == 
false))
 
  770             boost::uint16_t val = *(boost::uint16_t*)value;
 
  772 #if TE_MACHINE_BYTE_ORDER == TE_NDR 
  778             value += 
sizeof(uint32_t);
 
  782         return std::auto_ptr<te::dt::Array>(arr.release());
 
  787       throw Exception(
TE_TR(
"The array element type is not supported yet!"));
 
  822   return PQgetisnull(m_result, m_i, i) == 1;
 
An utility class for reading a PostGIS EWKB. 
 
static te::gm::Geometry * read(const char *ewkb)
It returns a valid geometry from a given EWKB. 
 
CharEncoding
Supported charsets (character encoding). 
 
te::common::CharEncoding getPropertyCharEncoding(std::size_t i) const 
It returns the property character encoding at position pos. 
 
bool move(std::size_t i)
It moves the dataset internal pointer to a given position. 
 
bool moveFirst()
It moves the internal pointer to the first item in the collection. 
 
te::dt::DateTime * Internal2TimeTZ(boost::int64_t tval, int z)
 
te::dt::DateTime * Internal2Date(const long dDate)
It returns a DateTime type from a date loaded by PostgreSQL. 
 
int m_size
The number of datasets in the collection. 
 
DataSet()
Default constructor. 
 
bool movePrevious()
It moves the internal pointer to the previous item of the collection. 
 
struct pg_result PGresult
 
static const MachineByteOrder sm_machineByteOrder
A flag that indicates the machine byte order (Big Endian or Little Endian). 
 
te::dt::DateTime * Internal2TimeStamp(boost::int64_t ival)
It returns a DateTime type from a timestamp loaded by PostgreSQL. 
 
#define TE_TR(message)
It marks a string in order to get translated. 
 
te::common::AccessPolicy getAccessPolicy() const 
It returns the read and write permission associated to the dataset. 
 
std::string getPropertyName(std::size_t i) const 
It returns the property name at position pos. 
 
int getPropertyDataType(std::size_t i) const 
It returns the underlying data type of the property at position pos. 
 
~DataSet()
The destructor will clear the internal PGresult. 
 
#define PG_TIMESTAMP_TYPE
 
boost::int16_t getInt16(std::size_t i) const 
Method for retrieving a 16-bit integer attribute value (2 bytes long). 
 
bool isAfterEnd() const 
It tells if the dataset internal pointer is on the sentinel position after the last element of the co...
 
AccessPolicy
Supported data access policies (can be used as bitfield). 
 
TraverseType
A dataset can be traversed in two ways: 
 
bool isAtEnd() const 
It tells if the dataset internal pointer is on the last element of the collection. 
 
bool isBeforeBegin() const 
It tells if the dataset internal pointer is in a position before the first element of the collection ...
 
std::string getDatasetNameOfProperty(std::size_t i) const 
It returns the underlying dataset name of the property at position pos. 
 
The type for variable-length multidimensional arrays. 
 
An Envelope defines a 2D rectangular region. 
 
bool isAtBegin() const 
It tells if the dataset internal pointer is on the first element of the collection or not...
 
Utility functions for PostgreSQL. 
 
double getDouble(std::size_t i) const 
Method for retrieving a double attribute value. 
 
std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const 
Method for retrieving a byte array. 
 
Implementation of a dataset for the PostGIS driver. 
 
PGresult * m_result
The internal buffer with the result query. 
 
std::string getNumeric(std::size_t i) const 
Method for retrieving a numeric attribute value. 
 
te::common::TraverseType getTraverseType() const 
It returns the traverse type associated to the dataset. 
 
bool isConnected() const 
It returns true if the dataset is connected and false if it is disconnected. A dataset can be connect...
 
float getFloat(std::size_t i) const 
Method for retrieving a float attribute value. 
 
A class that implements a connection to a PostgreSQL database. 
 
void GetArray(int i, int j, PGresult *result, std::vector< T > &a)
It retrieves information from an unidimensional array. 
 
std::size_t size() const 
It returns the collection size, if it is known. 
 
bool isEmpty() const 
It returns true if the collection is empty. 
 
bool moveLast()
It sets the dataset internal pointer to the last item in the collection. 
 
Implementation of the data source for the PostGIS driver. 
 
bool isNull(std::size_t i) const 
It checks if the attribute value is NULL. 
 
std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const 
Method for retrieving a geometric attribute value. 
 
boost::int32_t getInt32(std::size_t i) const 
Method for retrieving a 32-bit integer attribute value (4 bytes long). 
 
boost::int64_t getInt64(std::size_t i) const 
Method for retrieving a 64-bit integer attribute value (8 bytes long). 
 
An exception class for the PostGIS driver. 
 
std::auto_ptr< te::rst::Raster > getRaster(std::size_t i) const 
Method for retrieving a raster attribute value. 
 
std::auto_ptr< te::dt::Array > getArray(std::size_t i) const 
Method for retrieving an array. 
 
char getChar(std::size_t i) const 
Method for retrieving a signed character attribute value (1 byte long). 
 
std::string getString(std::size_t i) const 
Method for retrieving a string value attribute. 
 
std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const 
Method for retrieving a date and time attribute value. 
 
te::dt::DateTime * Internal2Time(boost::int64_t tval)
It returns a DateTime type from a time loaded by PostgreSQL. 
 
bool moveNext()
It moves the internal pointer to the next item of the collection. 
 
A template for atomic data types (integers, floats, strings and others). 
 
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string. 
 
std::auto_ptr< te::gm::Envelope > getExtent(std::size_t i)
It computes the bounding rectangle for a spatial property of the dataset. 
 
std::size_t getNumProperties() const 
It returns the number of properties that composes an item of the dataset. 
 
void copy(char *data, std::size_t size)
It copies the data from the given pointer to the byte array. 
 
A class that implements a connection pool for PostGIS. 
 
unsigned char getUChar(std::size_t i) const 
Method for retrieving an unsigned character attribute value (1 byte long). 
 
void SwapBytes(T &v)
It swaps the bytes in local. 
 
#define PG_TIMESTAMPTZ_TYPE
 
A class for representing binary data. 
 
bool getBool(std::size_t i) const 
Method for retrieving a boolean attribute value. 
 
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.