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
416 if(intVal.empty() ==
false)
436 if(decimalGroups == 0)
440 while(ii < (totalGroups * 2))
442 n = *((
unsigned short*)(val + ii + 8));
444 #if TE_MACHINE_BYTE_ORDER == TE_NDR
450 while (newVal.length() < 4)
451 newVal =
"0" + newVal;
458 while((decVal.empty() ==
false) && (decVal[decVal.length() - 1] ==
'0'))
459 decVal.erase(decVal.length() - 1);
462 return intVal +
"." + decVal;
467 std::string value(PQgetvalue(m_result, m_i, i));
473 int size = PQgetlength(m_result, m_i, i);
476 b->
copy(PQgetvalue(m_result, m_i, i), size);
477 return std::auto_ptr<te::dt::ByteArray>(b);
482 return std::auto_ptr<te::gm::Geometry>(
EWKBReader::read(PQgetvalue(m_result, m_i, i)));
487 return std::auto_ptr<te::rst::Raster>(0);
492 Oid tid = PQftype(m_result, i);
493 boost::int64_t ival = 0;
509 ival = (boost::int64_t)(dval * 1000000);
517 char* val = PQgetvalue(m_result, m_i, i);
518 lval = *((
long int*)val);
520 #if TE_MACHINE_BYTE_ORDER == TE_NDR
534 ival = (boost::int64_t)(dval * 1000000);
546 char* c = (
char*)PQgetvalue(m_result, m_i, i);
547 dval = *((
double*)c);
549 #if TE_MACHINE_BYTE_ORDER == TE_NDR
553 ival = (boost::int64_t)(dval * 1000000);
566 char* c = (
char*)PQgetvalue(m_result, m_i, i);
567 dval = *((
double*)c);
571 #if TE_MACHINE_BYTE_ORDER == TE_NDR
576 ival = (boost::int64_t)(dval * 1000000);
587 throw Exception(
TE_TR(
"This type is not supported by TerraLib!"));
593 char* value = PQgetvalue(m_result, m_i, i);
595 int ndim = *((
int*)value);
596 value +=
sizeof(int);
598 #if TE_MACHINE_BYTE_ORDER == TE_NDR
602 int dataoffset = *((
int*)value);
603 value +=
sizeof(int);
605 #if TE_MACHINE_BYTE_ORDER == TE_NDR
609 unsigned int elemtype = *((
unsigned int*)value);
610 value +=
sizeof(
unsigned int);
612 #if TE_MACHINE_BYTE_ORDER == TE_NDR
616 int* dimensions = (
int*)value;
617 value += ndim *
sizeof(int);
619 int* lowerbounds = (
int*)value;
620 value += ndim *
sizeof(int);
622 uint32_t* null_bitmap = (
unsigned int*)value;
624 boost::dynamic_bitset<> mask;
628 int nmasks = (dataoffset + 3) / 4;
630 mask.resize(nmasks * 8 *
sizeof(uint32_t));
634 for(
int i = 0; i != nmasks; ++i)
636 value +=
sizeof(uint32_t);
638 uint32_t umask = null_bitmap[i];
640 #if TE_MACHINE_BYTE_ORDER == TE_NDR
644 for(
int j = 0; j != 32; ++j)
646 mask[pos] = ((umask >> j) & 1) == 0;
653 value +=
sizeof(uint32_t);
664 std::vector<std::size_t> pos(ndim, 0);
666 for(
int d = 0; d != ndim; ++d)
668 int d_size = dimensions[d];
670 #if TE_MACHINE_BYTE_ORDER == TE_NDR
673 int d_lower_boundary = lowerbounds[d];
675 #if TE_MACHINE_BYTE_ORDER == TE_NDR
679 for(i = 0; i != d_size; ++i)
681 if((dataoffset != 0) && (mask[i] ==
false))
689 int text_size = *(
int*)value;
691 #if TE_MACHINE_BYTE_ORDER == TE_NDR
694 value +=
sizeof(int);
702 return std::auto_ptr<te::dt::Array>(arr.release());
710 std::vector<std::size_t> pos(ndim, 0);
712 for(
int d = 0; d != ndim; ++d)
714 int d_size = dimensions[d];
716 #if TE_MACHINE_BYTE_ORDER == TE_NDR
719 int d_lower_boundary = lowerbounds[d];
721 #if TE_MACHINE_BYTE_ORDER == TE_NDR
725 for(i = 0; i != d_size; ++i)
727 if((dataoffset != 0) && (mask[i] ==
false))
735 double val = *(
double*)value;
737 #if TE_MACHINE_BYTE_ORDER == TE_NDR
743 value +=
sizeof(double);
747 return std::auto_ptr<te::dt::Array>(arr.release());
755 std::vector<std::size_t> pos(ndim, 0);
757 for(
int d = 0; d != ndim; ++d)
759 int d_size = dimensions[d];
761 #if TE_MACHINE_BYTE_ORDER == TE_NDR
764 int d_lower_boundary = lowerbounds[d];
766 #if TE_MACHINE_BYTE_ORDER == TE_NDR
770 for(i = 0; i != d_size; ++i)
772 if((dataoffset != 0) && (mask[i] ==
false))
780 boost::uint16_t val = *(boost::uint16_t*)value;
782 #if TE_MACHINE_BYTE_ORDER == TE_NDR
788 value +=
sizeof(uint32_t);
792 return std::auto_ptr<te::dt::Array>(arr.release());
797 throw Exception(
TE_TR(
"The array element type is not supported yet!"));
832 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.