27 #include "../common/StringUtils.h"
28 #include "../common/Translator.h"
29 #include "../dataaccess/dataset/DataSet.h"
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../dataaccess/query/Query.h"
32 #include "../datatype/ByteArray.h"
33 #include "../datatype/DateTime.h"
34 #include "../geometry/Geometry.h"
50 #include <boost/cstdint.hpp>
51 #include <boost/format.hpp>
52 #include <boost/lexical_cast.hpp>
68 switch(propertyDataType)
108 std::auto_ptr<te::dt::ByteArray> ba(d->
getByteArray(propertyPos));
115 std::auto_ptr<te::gm::Geometry> geom(d->
getGeometry(propertyPos));
122 std::auto_ptr<te::dt::DateTime> dt(d->
getDateTime(propertyPos));
128 throw Exception(
TE_TR(
"The TerraLib data type is not supported by the PostgreSQL driver!"));
175 prepare(sql, paramTypes);
180 throw Exception(
TE_TR(
"Not implemented yet!"));
187 m_result = PQexecPrepared(m_conn, m_qname.c_str(), m_nparams, m_paramValues, m_paramLengths, m_paramFormats, 1);
190 for(std::size_t i = 0; i < m_nparams; ++i)
192 delete [] (m_paramValues[i]);
193 m_paramValues[i] = 0;
194 m_paramLengths[i] = 0;
198 if((PQresultStatus(m_result) != PGRES_COMMAND_OK) &&
199 (PQresultStatus(m_result) != PGRES_TUPLES_OK))
201 boost::format errmsg(
TE_TR(
"Could not execute the prepared query due to the following error: %1%."));
203 errmsg = errmsg % PQerrorMessage(m_conn);
205 throw Exception(errmsg.str());
216 std::vector<int> ptypes;
228 m_paramLengths[i] =
sizeof(char);
229 m_paramFormats[i] = 1;
231 if(m_paramValues[i] == 0)
232 m_paramValues[i] =
new char[
sizeof(char)];
234 memcpy(m_paramValues[i], &value,
sizeof(
char));
239 throw Exception(
TE_TR(
"The TerraLib unsigned char data type is not supported by PostgreSQL data access driver!"));
244 m_paramLengths[i] =
sizeof(boost::int16_t);
245 m_paramFormats[i] = 1;
247 if(m_paramValues[i] == 0)
248 m_paramValues[i] =
new char[
sizeof(boost::int16_t)];
250 memcpy(m_paramValues[i], &value,
sizeof(boost::int16_t));
252 #if TE_MACHINE_BYTE_ORDER == TE_NDR
259 m_paramLengths[i] =
sizeof(boost::int32_t);
260 m_paramFormats[i] = 1;
262 if(m_paramValues[i] == 0)
263 m_paramValues[i] =
new char[
sizeof(boost::int32_t)];
265 memcpy(m_paramValues[i], &value,
sizeof(boost::int32_t));
267 #if TE_MACHINE_BYTE_ORDER == TE_NDR
274 m_paramLengths[i] =
sizeof(boost::int64_t);
275 m_paramFormats[i] = 1;
277 if(m_paramValues[i] == 0)
278 m_paramValues[i] =
new char[
sizeof(boost::int64_t)];
280 memcpy(m_paramValues[i], &value,
sizeof(boost::int64_t));
282 #if TE_MACHINE_BYTE_ORDER == TE_NDR
289 m_paramLengths[i] =
sizeof(char);
290 m_paramFormats[i] = 1;
292 if(m_paramValues[i] == 0)
293 m_paramValues[i] =
new char[
sizeof(char)];
295 char bvalue = value ? 1 : 0;
297 memcpy(m_paramValues[i], &bvalue,
sizeof(
char));
302 m_paramLengths[i] =
sizeof(float);
303 m_paramFormats[i] = 1;
305 if(m_paramValues[i] == 0)
306 m_paramValues[i] =
new char[
sizeof(float)];
308 memcpy(m_paramValues[i], &value,
sizeof(
float));
310 #if TE_MACHINE_BYTE_ORDER == TE_NDR
317 m_paramLengths[i] =
sizeof(double);
318 m_paramFormats[i] = 1;
320 if(m_paramValues[i] == 0)
321 m_paramValues[i] =
new char[
sizeof(double)];
323 memcpy(m_paramValues[i], &value,
sizeof(
double));
325 #if TE_MACHINE_BYTE_ORDER == TE_NDR
332 delete [] (m_paramValues[i]);
334 m_paramValues[i] =
new char[value.length() + 1];
336 memcpy(m_paramValues[i], value.c_str(), value.length() + 1);
339 m_paramFormats[i] = 0;
344 delete [] (m_paramValues[i]);
346 m_paramValues[i] =
new char[value.length() + 1];
348 memcpy(m_paramValues[i], value.c_str(), value.length() + 1);
351 m_paramFormats[i] = 0;
356 delete [] (m_paramValues[i]);
358 m_paramValues[i] =
new char[value.
bytesUsed()];
363 m_paramFormats[i] = 1;
368 delete [] (m_paramValues[i]);
370 m_paramValues[i] = 0;
372 m_paramFormats[i] = 1;
374 std::size_t ewkbsize = value.
getWkbSize() + 4;
376 m_paramValues[i] =
new char[ewkbsize];
380 m_paramLengths[i] =
static_cast<int>(ewkbsize);
385 throw Exception(
TE_TR(
"Not implemented yet!"));
407 delete [] (m_paramValues[i]);
409 std::string dvalue = value.
toString();
411 m_paramValues[i] =
new char[dvalue.length() + 1];
413 memcpy(m_paramValues[i], dvalue.c_str(), dvalue.length() + 1);
415 m_paramLengths[i] = dvalue.length() + 1;
420 throw Exception(
TE_TR(
"Not implemented yet!"));
425 throw Exception(
TE_TR(
"Not implemented yet!"));
439 m_nparams = paramTypes.size();
441 m_paramValues =
new char*[m_nparams];
443 memset(m_paramValues, 0, m_nparams *
sizeof(
char*));
445 m_paramLengths =
new int[m_nparams];
447 memset(m_paramLengths, 0, m_nparams *
sizeof(
int));
449 m_paramFormats =
new int[m_nparams];
451 memset(m_paramFormats, 0, m_nparams *
sizeof(
int));
454 m_result = PQprepare(m_conn, m_qname.c_str(), query.c_str(), m_nparams, 0);
457 if((PQresultStatus(m_result) != PGRES_COMMAND_OK) &&
458 (PQresultStatus(m_result) != PGRES_TUPLES_OK))
460 boost::format errmsg(
TE_TR(
"Could not create the prepared query due to the following error: %1%."));
462 errmsg = errmsg % PQerrorMessage(m_conn);
464 throw Exception(errmsg.str());
470 const std::size_t nparams = propertiesPos.size();
472 for(std::size_t i = 0; i < nparams; ++i)
473 BindValue(
this, d, i + offset, propertiesPos[i]);
478 const std::size_t nparams = propertiesPos.size();
480 for(std::size_t i = 0; i < nparams; ++i)
486 for(std::size_t i = 0; i < m_nparams; ++i)
496 m_t->execute(
"DEALLOCATE PREPARE " + m_qname);
504 for(std::size_t i = 0; i < m_nparams; ++i)
505 delete [] (m_paramValues[i]);
507 delete [] m_paramValues;
512 delete [] m_paramLengths;
517 delete [] m_paramFormats;
~PreparedQuery()
Virtual destructor.
virtual std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const =0
Method for retrieving a date and time attribute value.
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
std::size_t bytesUsed() const
It returns the number of used bytes in the internal buffer.
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
virtual char getChar(std::size_t i) const =0
Method for retrieving a signed character attribute value (1 byte long).
Connection * getConnection() const
It returns the underlying connection.
void bindNumeric(int i, const std::string &value)
Binds the i-th parameter of the query to a value.
virtual bool getBool(std::size_t i) const =0
Method for retrieving a boolean attribute value.
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
void write(const te::gm::Geometry *geom)
It serializes the geometry to an EWKB representation into the specified buffer.
void Swap8Bytes(char *v)
It swaps an array of eight bytes in local.
virtual std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const =0
Method for retrieving a byte array.
unsigned int getRasterTypeId() const
It returns the type id associated to the PostGIS Raster type.
A visitor for building an SQL statement using PostGIS dialect.
void bind(int i, char value)
Binds the i-th parameter of the query to a value.
virtual std::string toString() const =0
It returns the data value in a string notation.
te::da::DataSourceTransactor * getTransactor() const
It returns a pointer to the underlying data source transactor.
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
#define TE_TR(message)
It marks a string in order to get translated.
std::size_t getWkbSize() const
It returns the size required by a WKB representation for this geometric object.
te::da::DataSet * query(te::common::TraverseType travType=te::common::FORWARDONLY, te::common::AccessPolicy rwRole=te::common::RAccess)
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
void Swap2Bytes(T &v)
It swaps two bytes in local.
The transactor class for the PostGIS driver.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
PreparedQuery()
Constructor.
An utility class for writing a PostGIS EWKB.
PGconn * getConn() const
It gets the underlying PGconn object.
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
std::string getName() const
It returns the prepared query name.
void Swap4Bytes(T &v)
It swaps four bytes in local.
Implementation of a dataset for the PostGIS driver.
An abstract class for raster data strucutures.
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
char * getData() const
It returns the data array.
Utility functions for PostgreSQL.
A base class for values that can be retrieved from the data access module.
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A class that implements a connection to a PostgreSQL database.
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
Implementation of the data source for the PostGIS driver.
A Transactor can be viewed as a connection to the data source for reading/writing things into it...
A dataset is the unit of information manipulated by the data access module of TerraLib.
An exception class for the PostGIS driver.
A visitor for building an SQL statement using PostGIS dialect.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
te::common::CharEncoding getCharEncoding() const
It returns the datasource char encoding.
void prepare(const te::da::Query &query, const std::vector< te::dt::Property * > ¶mTypes)
It prepares the query that may be used for commands that are used mutiple times (select, insert, update and delete).
te::dt::Property * Convert2TerraLib(unsigned int attNum, const char *attName, unsigned int attType, bool attNotNull, const char *fmt, bool attHasDefault, const char *attDefValue, unsigned int pgisGeomTypeOid, unsigned int pgisRasterTypeOid)
It creates a PropertyType from a PostgreSQL attribute description.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
void BindValue(te::pgis::PreparedQuery *pq, te::da::DataSet *d, std::size_t i, std::size_t propertyPos)
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
A class that implements a prepared query for PostgreSQL data access driver.
A Query is independent from the data source language/dialect.
A class for representing binary data.
unsigned int getGeomTypeId() const
It returns the type id associated to the PostGIS Geometry type.
A class that implements a prepared query for PostgreSQL data access driver.