27 #include "../common/HexUtils.h"
28 #include "../common/Translator.h"
29 #include "../dataaccess/dataset/DataSet.h"
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../datatype/ByteArray.h"
38 #include <boost/lexical_cast.hpp>
156 return Globals::sm_pointTypeName;
159 return Globals::sm_pointMTypeName;
164 return Globals::sm_lineStringTypeName;
167 return Globals::sm_lineStringMTypeName;
172 return Globals::sm_polygonTypeName;
175 return Globals::sm_polygonMTypeName;
180 return Globals::sm_geometryCollectionTypeName;
183 return Globals::sm_geometryCollectionMTypeName;
188 return Globals::sm_multiPointTypeName;
191 return Globals::sm_multiPointMTypeName;
196 return Globals::sm_multiLineStringTypeName;
199 return Globals::sm_multiLineStringMTypeName;
204 return Globals::sm_multiPolygonTypeName;
207 return Globals::sm_multiPolygonMTypeName;
210 return Globals::sm_geometryTypeName;
223 ret =
SetColumnDef(s, Globals::sm_charTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
227 ret =
SetColumnDef(s, Globals::sm_int2TypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
231 ret =
SetColumnDef(s, Globals::sm_intTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
235 ret =
SetColumnDef(s, Globals::sm_int8TypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
240 SetColumnDef(s, static_cast<const te::dt::NumericProperty*>(p), justDataType);
248 SetColumnDef(s, Globals::sm_dateTypeName, dtp, justDataType);
250 SetColumnDef(s, Globals::sm_timeTypeName, dtp, justDataType);
252 SetColumnDef(s, Globals::sm_timeStampTypeName, dtp, justDataType);
254 SetColumnDef(s, Globals::sm_timeStampTZTypeName, dtp, justDataType);
259 SetColumnDef(s, Globals::sm_floatTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
263 SetColumnDef(s, Globals::sm_doubleTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
271 SetColumnDef(s, Globals::sm_fixedcharTypeName, sp, justDataType);
273 SetColumnDef(s, Globals::sm_varcharTypeName, sp, justDataType);
275 SetColumnDef(s, Globals::sm_stringTypeName, sp, justDataType);
280 SetColumnDef(s, Globals::sm_booleanTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
284 SetColumnDef(s, Globals::sm_byteArrayTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
288 SetColumnDef(s, Globals::sm_geometryTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
292 SetColumnDef(s, static_cast<const te::dt::ArrayProperty*>(p), justDataType);
296 throw Exception(
TE_TR(
"The informed type could not be mapped to PostgreSQL type system!"));
306 std::size_t tosize = 0;
308 char* ewkb =
new char[size];
310 EWKBWriter::write(g, ewkb);
311 unsigned char* byteArray = PQescapeByteaConn(conn, (
unsigned char*)ewkb, size, &tosize);
314 output +=
"ST_GeomFromEWKB('";
315 output += (
char*)byteArray;
318 PQfreemem(byteArray);
323 std::size_t length = s.length() ;
324 char* to =
new char[2 * length + 1];
325 PQescapeStringConn(conn, to, s.c_str(), length, 0);
331 unsigned int pgisGeomTypeOid,
332 unsigned int pgisRasterTypeOid,
333 std::vector<int>& teTypes)
335 int ncols = PQnfields(result);
337 for(
int i = 0; i < ncols; ++i)
339 std::auto_ptr<te::dt::Property> p(
Convert2TerraLib(i, PQfname(result, i), PQftype(result, i),
340 false, 0,
false, 0, -1, pgisGeomTypeOid, pgisRasterTypeOid));
342 teTypes.push_back(p->getType());
348 std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"PG_HOST");
349 std::map<std::string, std::string>::const_iterator it_end = dsInfo.end();
350 std::string connInfo;
353 connInfo +=
" host = " + it->second;
355 it = dsInfo.find(
"PG_HOST_ADDR");
358 connInfo +=
" hostaddr = " + it->second;
360 it = dsInfo.find(
"PG_PORT");
363 connInfo +=
" port = " + it->second;
365 it = dsInfo.find(
"PG_DB_NAME");
368 connInfo +=
" dbname = " + it->second;
370 it = dsInfo.find(
"PG_USER");
373 connInfo +=
" user = " + it->second;
375 it = dsInfo.find(
"PG_PASSWORD");
377 if((it != it_end) && (!it->second.empty()))
378 connInfo +=
" password = " + it->second;
380 it = dsInfo.find(
"PG_CONNECT_TIMEOUT");
383 connInfo +=
" connect_timeout = " + it->second;
385 it = dsInfo.find(
"PG_OPTIONS");
388 connInfo +=
" options = " + it->second;
390 it = dsInfo.find(
"PG_SSL_MODE");
393 connInfo +=
" sslmode = " + it->second;
395 it = dsInfo.find(
"PG_KRBSRVNAME");
398 connInfo +=
" krbsrvname = " + it->second;
400 it = dsInfo.find(
"PG_GSSLIB");
403 connInfo +=
" gsslib = " + it->second;
408 void te::pgis::SplitTableName(
const std::string& fullName,
const std::string* defaultSchema, std::string& schemaName, std::string& tableName)
410 assert(defaultSchema);
413 size_t pos = fullName.find(
".");
415 if(pos == std::string::npos)
417 tableName = fullName;
418 schemaName = *defaultSchema;
422 tableName = fullName.substr(pos + 1);
423 schemaName = fullName.substr(0, pos);
429 std::string wheresql;
431 const std::size_t size = properties.size();
433 for(std::size_t i = 0; i < size; ++i)
438 wheresql += properties[i]->getName();
440 wheresql +=
" = $" + boost::lexical_cast<std::string>(i + 1 + offset);
448 std::string valueNames(
"(");
450 for(std::size_t i = 0; i < nproperties; ++i)
455 valueNames +=
"$" + boost::lexical_cast<std::string>(i + 1);
467 const std::size_t size = properties.size();
469 for(std::size_t i = 0; i < size; ++i)
474 sql += properties[i]->getName();
476 sql +=
" = $" + boost::lexical_cast<std::string>(i + 1);
484 std::string values(
"(");
486 const std::size_t np = dt->
size();
488 for(std::size_t i = 0; i < np; ++i)
508 value += d->
getChar(propertyPos);
512 value += boost::lexical_cast<std::string>(d->
getInt16(propertyPos));
516 value += boost::lexical_cast<std::string>(d->
getInt32(propertyPos));
520 value += boost::lexical_cast<std::string>(d->
getInt64(propertyPos));
524 value += d->
getBool(propertyPos) ?
"1" :
"0";
528 value += boost::lexical_cast<std::string>(d->
getFloat(propertyPos));
532 value += boost::lexical_cast<std::string>(d->
getDouble(propertyPos));
541 std::string dvalue = d->
getString(propertyPos);
543 char* valueto =
new char[dvalue.length() * 2 + 1];
547 PQescapeStringConn(conn, valueto, dvalue.c_str(), dvalue.length(), &pgerror);
552 throw Exception(
TE_TR(
"Could not escape string!"));
575 std::auto_ptr<te::dt::ByteArray> ba(d->
getByteArray(propertyPos));
579 unsigned char* pgba = PQescapeByteaConn(conn, (
const unsigned char*)(ba->getData()), ba->bytesUsed(), &tolen);
582 value += (
char*)pgba;
591 std::auto_ptr<te::gm::Geometry> geom(d->
getGeometry(propertyPos));
594 std::size_t ewkbsize = geom->getWkbSize() + 4;
596 char* ewkb =
new char[ewkbsize];
598 EWKBWriter::write(geom.get(), ewkb);
600 char* hewkb =
new char[ewkbsize * 2 + 1];
617 std::auto_ptr<te::dt::DateTime> dt(d->
getDateTime(propertyPos));
619 value += dt->toString();
625 throw Exception(
TE_TR(
"The TerraLib data type is not supported by the PostgreSQL driver!"));
635 const std::size_t np = dt->
size();
637 for(std::size_t i = 0; i < np; ++i)
651 static const char*
sg_pg_encoding [] = {
"UTF8",
"WIN1250",
"WIN1251",
"WIN1252",
"WIN1253",
"WIN1254",
"WIN1257",
"LATIN1"};
Property * getProperty(std::size_t i) const
It returns the i-th property.
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).
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
std::string MakeConnectionStr(const std::map< std::string, std::string > &dsInfo)
void SplitTableName(const std::string &fullName, const std::string *defaultSchema, std::string &schemaName, std::string &tableName)
void ScapeString(PGconn *conn, const std::string &s, std::string &output)
It escapes a string for use within an SQL command.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
An atomic property like an integer or double.
std::string GetBindableUpdateSQL(const std::vector< te::dt::Property * > &properties)
Given a list of properties it constructs a string with bindable parameters that can be used inside an...
CharEncoding
Supported charsets (character encoding).
StringType getSubType() const
It returns the string property sub type.
virtual char getChar(std::size_t i) const =0
Method for retrieving a signed character attribute value (1 byte long).
virtual bool getBool(std::size_t i) const =0
Method for retrieving a boolean attribute value.
A class that models the description of a dataset.
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
const std::string & GetGeometryName(te::gm::GeomType t)
It returns the geometry names as usual for PostGIS.
struct pg_result PGresult
An static class with global definitions.
virtual std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const =0
Method for retrieving a byte array.
std::string GetLoadDataRow(const te::da::DataSetType *dt, te::da::DataSet *d, PGconn *conn)
const char * GetPGEncoding(te::common::CharEncoding encoding)
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.
std::size_t size() const
It returns the maximum number of characters for a varying string, or the number of characters for a f...
It models a property definition.
char * Binary2Hex(const char *s, std::size_t size)
Each char from the array of characters in binary format is converted into a pair of hex characters...
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
std::string GetSQLValues(const te::da::DataSetType *dt, te::da::DataSet *d, PGconn *conn)
The type for arbitrary precison numbers, like numeric(p, q).
An utility class for writing a PostGIS EWKB.
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
Utility functions for PostgreSQL.
void Convert2PostGIS(const te::gm::Envelope *e, int srid, std::string &output)
It converts the envelope into a PostGIS BOX3D.
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
static const char * sg_pg_encoding[]
static const std::string sm_numericTypeName
The string literal representation for the numeric type.
bool isRequired() const
It returns true if the attribute is required, otherwise it returns false.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
std::size_t size() const
It returns the number of properties of the CompositeProperty.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
int getType() const
It returns the property data type.
void SetColumnDef(std::string &s, const te::dt::ArrayProperty *p, bool justDataType=false)
A dataset is the unit of information manipulated by the data access module of TerraLib.
te::common::CharEncoding GetTeEncoding(const char *const encoding)
Property * getElementType() const
It returns the type of array elements.
The type for variable-length multidimensional arrays.
static std::size_t sg_n_encoding
An exception class for the PostGIS driver.
unsigned int getPrecision() const
It returns the total count of significant digits in the whole number, that is, the number of digits t...
The type for date and time types: date, date period, date duration, time duration, time instant, time period, time instant with time zone or time period with time zone.
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.
std::string GetSQLValue(const te::dt::Property *p, std::size_t propertyPos, te::da::DataSet *d, PGconn *conn)
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
std::string * getDefaultValue() const
It returns the default value associated to the property, or NULL if none is associated.
DateTimeType getSubType() const
It returns the date time property sub type.
bool isAutoNumber() const
It returns true if the attribute is an autonumber, otherwise it returns false.
unsigned int getScale() const
It returns the count of decimal digits in the fractional part, to the right of the decimal point...
std::string GetSQLBindValues(std::size_t nproperties)
bool SetColumnDef(std::string &s, const std::string &tname, const te::dt::SimpleProperty *p, bool justDataType=false)
std::string GetBindableWhereSQL(const std::vector< te::dt::Property * > &properties, const std::size_t offset=0)
Given a list of properties it creates an AND connected expression with bindable parameters ($n)...