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> 
  104       if(p->
size() > 0 && p->
size() < 10485760)
 
  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);
 
  274           if(sp->
size() > 10485760)
 
  276             SetColumnDef(s, Globals::sm_stringTypeName, sp, justDataType);
 
  280             SetColumnDef(s, Globals::sm_varcharTypeName, sp, justDataType);
 
  284           SetColumnDef(s, Globals::sm_stringTypeName, sp, justDataType);
 
  289       SetColumnDef(s, Globals::sm_booleanTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
 
  293       SetColumnDef(s, Globals::sm_byteArrayTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
 
  297       SetColumnDef(s, Globals::sm_geometryTypeName, static_cast<const te::dt::SimpleProperty*>(p), justDataType);
 
  301       SetColumnDef(s, static_cast<const te::dt::ArrayProperty*>(p), justDataType);
 
  305       throw Exception(
TE_TR(
"The informed type could not be mapped to PostgreSQL type system!"));
 
  315   std::size_t tosize = 0;
 
  317   char* ewkb = 
new char[size];
 
  319   EWKBWriter::write(g, ewkb);
 
  320   unsigned char* byteArray = PQescapeByteaConn(conn, (
unsigned char*)ewkb, size, &tosize);
 
  323   output += 
"ST_GeomFromEWKB('";
 
  324   output += (
char*)byteArray;
 
  327   PQfreemem(byteArray);
 
  332   std::size_t length = s.length() ;
 
  333   char* to = 
new char[2 * length + 1];
 
  334   PQescapeStringConn(conn, to, s.c_str(), length, 0);
 
  340                                 unsigned int pgisGeomTypeOid,
 
  341                                 unsigned int pgisRasterTypeOid,
 
  342                                 std::vector<int>& teTypes)
 
  344   int ncols = PQnfields(result);
 
  346   for(
int i = 0; i < ncols; ++i)
 
  348     std::auto_ptr<te::dt::Property> p(
Convert2TerraLib(i, PQfname(result, i), PQftype(result, i),
 
  349                                                        false, 0, 
false, 0, -1, pgisGeomTypeOid, pgisRasterTypeOid));
 
  351     teTypes.push_back(p->getType());
 
  357   std::map<std::string, std::string>::const_iterator it = dsInfo.find(
"PG_HOST");
 
  358   std::map<std::string, std::string>::const_iterator it_end = dsInfo.end();
 
  359   std::string connInfo;
 
  362     connInfo += 
" host = " + it->second;
 
  364   it = dsInfo.find(
"PG_HOST_ADDR");
 
  367     connInfo += 
" hostaddr = " + it->second;
 
  369   it = dsInfo.find(
"PG_PORT");
 
  372     connInfo += 
" port = " + it->second;
 
  374   it = dsInfo.find(
"PG_DB_NAME");
 
  377     connInfo += 
" dbname = " + it->second;
 
  379   it = dsInfo.find(
"PG_USER");
 
  382     connInfo += 
" user = " + it->second;
 
  384   it = dsInfo.find(
"PG_PASSWORD");
 
  386   if((it != it_end) && (!it->second.empty()))
 
  387     connInfo += 
" password = " +  it->second;
 
  389   it = dsInfo.find(
"PG_CONNECT_TIMEOUT");
 
  392     connInfo += 
" connect_timeout = " + it->second;
 
  394   it = dsInfo.find(
"PG_OPTIONS");
 
  397     connInfo += 
" options = " + it->second;
 
  399   it = dsInfo.find(
"PG_SSL_MODE");
 
  402     connInfo += 
" sslmode = " + it->second;
 
  404   it = dsInfo.find(
"PG_KRBSRVNAME");
 
  407     connInfo += 
" krbsrvname = " + it->second;
 
  409   it = dsInfo.find(
"PG_GSSLIB");
 
  412     connInfo += 
" gsslib = " + it->second;
 
  417 void te::pgis::SplitTableName(
const std::string& fullName, 
const std::string* defaultSchema, std::string& schemaName, std::string& tableName)
 
  419   assert(defaultSchema);
 
  422   size_t pos = fullName.find(
".");
 
  424   if(pos == std::string::npos)
 
  426     tableName = fullName;
 
  427     schemaName = *defaultSchema;
 
  431     tableName = fullName.substr(pos + 1);
 
  432     schemaName = fullName.substr(0, pos);    
 
  438   std::string wheresql;
 
  440   const std::size_t size = properties.size();
 
  442   for(std::size_t i = 0; i < size; ++i)
 
  447     wheresql += properties[i]->getName();
 
  449     wheresql += 
" = $" + boost::lexical_cast<std::string>(i + 1 + offset);
 
  457   std::string valueNames(
"(");
 
  459   for(std::size_t i = 0; i < nproperties; ++i)
 
  464     valueNames += 
"$"  + boost::lexical_cast<std::string>(i + 1);
 
  476   const std::size_t size = properties.size();
 
  478   for(std::size_t i = 0; i < size; ++i)
 
  483     sql += properties[i]->getName();
 
  485     sql += 
" = $" + boost::lexical_cast<std::string>(i + 1);
 
  493   std::string values(
"(");
 
  495   const std::size_t np = dt->
size();
 
  497   for(std::size_t i = 0; i < np; ++i)
 
  517       value += d->
getChar(propertyPos);
 
  521       value += boost::lexical_cast<std::string>(d->
getInt16(propertyPos));
 
  525       value += boost::lexical_cast<std::string>(d->
getInt32(propertyPos));
 
  529       value += boost::lexical_cast<std::string>(d->
getInt64(propertyPos));
 
  533       value += d->
getBool(propertyPos) ? 
"1" : 
"0";
 
  537       value += boost::lexical_cast<std::string>(d->
getFloat(propertyPos));
 
  541       value += boost::lexical_cast<std::string>(d->
getDouble(propertyPos));
 
  550         std::string dvalue = d->
getString(propertyPos);
 
  552         char* valueto = 
new char[dvalue.length() * 2  + 1];
 
  556         PQescapeStringConn(conn, valueto, dvalue.c_str(), dvalue.length(), &pgerror);
 
  561           throw Exception(
TE_TR(
"Could not escape string!"));
 
  584         std::auto_ptr<te::dt::ByteArray> ba(d->
getByteArray(propertyPos));
 
  588         unsigned char* pgba = PQescapeByteaConn(conn, (
const unsigned char*)(ba->getData()), ba->bytesUsed(), &tolen);
 
  591         value += (
char*)pgba;
 
  600         std::auto_ptr<te::gm::Geometry> geom(d->
getGeometry(propertyPos));
 
  603         std::size_t ewkbsize = geom->getWkbSize() + 4;
 
  605         char* ewkb = 
new char[ewkbsize];
 
  607         EWKBWriter::write(geom.get(), ewkb);
 
  609         char* hewkb = 
new char[ewkbsize * 2  + 1];
 
  626         std::auto_ptr<te::dt::DateTime> dt(d->
getDateTime(propertyPos));
 
  628         value += dt->toString();
 
  634       throw Exception(
TE_TR(
"The TerraLib data type is not supported by the PostgreSQL driver!"));
 
  644   const std::size_t np = dt->
size();
 
  646   for(std::size_t i = 0; i < np; ++i)
 
  660 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)...