27 #include "../core/utils/HexUtils.h" 28 #include "../core/translator/Translator.h" 29 #include "../core/uri/URI.h" 30 #include "../core/uri/Utils.h" 31 #include "../dataaccess/dataset/DataSet.h" 32 #include "../dataaccess/dataset/DataSetType.h" 33 #include "../datatype/ByteArray.h" 40 #include <boost/lexical_cast.hpp> 106 if(p->
size() > 0 && p->
size() < 10485760)
269 SetColumnDef(s, static_cast<const te::dt::NumericProperty*>(p), justDataType);
303 if(sp->
size() > 10485760)
330 SetColumnDef(s, static_cast<const te::dt::ArrayProperty*>(p), justDataType);
334 throw Exception(
TE_TR(
"The informed type could not be mapped to PostgreSQL type system!"));
344 std::size_t tosize = 0;
346 char* ewkb =
new char[size];
349 unsigned char* byteArray = PQescapeByteaConn(conn, (
unsigned char*)ewkb, size, &tosize);
352 output +=
"ST_GeomFromEWKB('";
353 output += (
char*)byteArray;
356 PQfreemem(byteArray);
361 std::size_t length = s.length() ;
362 char* to =
new char[2 * length + 1];
363 PQescapeStringConn(conn, to, s.c_str(), length,
nullptr);
369 unsigned int pgisGeomTypeOid,
370 unsigned int pgisRasterTypeOid,
371 std::vector<int>& teTypes)
373 int ncols = PQnfields(result);
375 for(
int i = 0; i < ncols; ++i)
377 std::unique_ptr<te::dt::Property>
p(
Convert2TerraLib(i, PQfname(result, i), PQftype(result, i),
378 false,
nullptr,
false,
nullptr, -1, pgisGeomTypeOid, pgisRasterTypeOid));
380 teTypes.push_back(p->getType());
386 std::string connStr, aux;
388 std::map<std::string, std::string>::const_iterator it = kvp.begin();
389 std::map<std::string, std::string>::const_iterator itend = kvp.end();
391 aux = connInfo.
host();
393 connStr +=
" host = " + aux;
395 it = kvp.find(
"PG_HOST_ADDR");
396 if (it != itend && !it->second.empty())
397 connStr +=
" hostaddr = " + it->second;
399 aux = connInfo.
port();
401 connStr +=
" port = " + aux;
405 connStr +=
" dbname = " + aux;
407 aux = connInfo.
user();
409 connStr +=
" user = " + aux;
413 connStr +=
" password = " + aux;
415 it = kvp.find(
"PG_CONNECT_TIMEOUT");
416 if (it != itend && !it->second.empty())
417 connStr +=
" connect_timeout = " + it->second;
419 it = kvp.find(
"PG_OPTIONS");
420 if (it != itend && !it->second.empty())
421 connStr +=
" options = " + it->second;
423 it = kvp.find(
"PG_SSL_MODE");
424 if (it != itend && !it->second.empty())
425 connStr +=
" sslmode = " + it->second;
427 it = kvp.find(
"PG_KRBSRVNAME");
428 if (it != itend && !it->second.empty())
429 connStr +=
" krbsrvname = " + it->second;
431 it = kvp.find(
"PG_GSSLIB");
432 if (it != itend && !it->second.empty())
433 connStr +=
" gsslib = " + it->second;
435 it = kvp.find(
"PG_APPLICATION_NAME");
436 if (it != itend && !it->second.empty())
437 connStr +=
" application_name = " + it->second;
442 void te::pgis::SplitTableName(
const std::string& fullName,
const std::string* defaultSchema, std::string& schemaName, std::string& tableName)
444 assert(defaultSchema);
447 size_t pos = fullName.find(
".");
449 if(pos == std::string::npos)
451 tableName = fullName;
452 schemaName = *defaultSchema;
456 tableName = fullName.substr(pos + 1);
457 schemaName = fullName.substr(0, pos);
463 std::string wheresql;
465 const std::size_t size = properties.size();
467 for(std::size_t i = 0; i < size; ++i)
472 wheresql += properties[i]->getName();
474 wheresql +=
" = $" + boost::lexical_cast<std::string>(i + 1 + offset);
482 std::string valueNames(
"(");
484 for(std::size_t i = 0; i < nproperties; ++i)
489 valueNames +=
"$" + boost::lexical_cast<std::string>(i + 1);
501 const std::size_t size = properties.size();
503 for(std::size_t i = 0; i < size; ++i)
508 sql += properties[i]->getName();
510 sql +=
" = $" + boost::lexical_cast<std::string>(i + 1);
518 std::string values(
"(");
520 const std::size_t np = dt->
size();
522 for(std::size_t i = 0; i < np; ++i)
542 value += d->
getChar(propertyPos);
546 value += boost::lexical_cast<std::string>(d->
getInt16(propertyPos));
550 value += boost::lexical_cast<std::string>(d->
getInt32(propertyPos));
554 value += boost::lexical_cast<std::string>(d->
getInt64(propertyPos));
558 value += d->
getBool(propertyPos) ?
"1" :
"0";
562 value += boost::lexical_cast<std::string>(d->
getFloat(propertyPos));
566 value += boost::lexical_cast<std::string>(d->
getDouble(propertyPos));
575 std::string dvalue = d->
getString(propertyPos);
577 char* valueto =
new char[dvalue.length() * 2 + 1];
581 PQescapeStringConn(conn, valueto, dvalue.c_str(), dvalue.length(), &pgerror);
609 std::unique_ptr<te::dt::ByteArray> ba(d->
getByteArray(propertyPos));
613 unsigned char* pgba = PQescapeByteaConn(conn, (
const unsigned char*)(ba->getData()), ba->bytesUsed(), &tolen);
616 value += (
char*)pgba;
625 std::unique_ptr<te::gm::Geometry> geom(d->
getGeometry(propertyPos));
628 std::size_t ewkbsize = geom->getWkbSize() + 4;
630 char* ewkb =
new char[ewkbsize];
634 char* hewkb =
new char[ewkbsize * 2 + 1];
651 std::unique_ptr<te::dt::DateTime>
dt(d->
getDateTime(propertyPos));
659 throw Exception(
TE_TR(
"The TerraLib data type is not supported by the PostgreSQL driver!"));
669 const std::size_t np = dt->
size();
671 for(std::size_t i = 0; i < np; ++i)
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
Property * getProperty(std::size_t i) const
It returns the i-th property.
std::string path() const
Retrieving the path.
std::string MakeConnectionStr(const te::core::URI &connInfo)
static const std::string sm_intTypeName
The string literal representation for the int type.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
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.
static const std::string sm_doubleTypeName
The string literal representation for the double type.
static const std::string sm_pointTypeName
The string literal representation for PostGIS point geometry type.
virtual std::unique_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const =0
Method for retrieving a byte array.
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...
StringType getSubType() const
It returns the string property sub type.
Base exception class for plugin module.
A class that models the description of a dataset.
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
static const std::string sm_byteArrayTypeName
The string literal representation for the byte array type.
const std::string & GetGeometryName(te::gm::GeomType t)
It returns the geometry names as usual for PostGIS.
void write(const te::gm::Geometry *geom)
It serializes the geometry to an EWKB representation into the specified buffer.
std::string toString() const
It returns the time duration in the ISO textual format (hh:mm:ss,ss).
struct pg_result PGresult
An utility class for writing a PostGIS EWKB.
static const std::string sm_dateTypeName
The string literal representation for the date type.
static const std::string sm_timeTypeName
The string literal representation for the time type.
static const std::string sm_pointMTypeName
The string literal representation for PostGIS point geometry type.
std::string password() const
Retrieving the password information.
std::string GetLoadDataRow(const te::da::DataSetType *dt, te::da::DataSet *d, PGconn *conn)
#define TE_TR(message)
It marks a string in order to get translated.
static const std::string sm_lineStringTypeName
The string literal representation for PostGIS line string geometry type.
std::size_t getWkbSize() const _NOEXCEPT_OP(true)
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.
std::string query() const
Retrieving the query.
std::string GetSQLValues(const te::da::DataSetType *dt, te::da::DataSet *d, PGconn *conn)
static const std::string sm_fixedcharTypeName
The string literal representation for the char type.
static const std::string sm_lineStringMTypeName
The string literal representation for PostGIS line string with m geometry type.
The type for arbitrary precison numbers, like numeric(p, q).
static const std::string sm_stringTypeName
The string literal representation for the string type.
An exception class for the PostGIS driver.
static const std::string sm_int2TypeName
The string literal representation for the int2 type.
static const std::string sm_geometryTypeName
The string literal representation for PostGIS base geometry type.
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
static const std::string sm_geometryCollectionMTypeName
The string literal representation for PostGIS geometry collection with m geometry type...
unsigned int getPrecision() const
It returns the count of decimal digits in the fractional part, to the right of the decimal point...
static const std::string sm_floatTypeName
The string literal representation for the float type.
static const std::string sm_multiLineStringTypeName
The string literal representation for PostGIS multi-linestring geometry type.
void Convert2PostGIS(const te::gm::Envelope *e, int srid, std::string &output)
It converts the envelope into a PostGIS BOX3D.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
static const std::string sm_multiPointTypeName
The string literal representation for PostGIS multi-point geometry type.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
static const std::string sm_multiLineStringMTypeName
The string literal representation for PostGIS multi-linestring with m geometry type.
static te::dt::TimeDuration dt(20, 30, 50, 11)
static const std::string sm_numericTypeName
The string literal representation for the numeric type.
std::string port() const
Retrieving the port.
static const std::string sm_charTypeName
The string literal representation for the 1-byte character type.
std::string host() const
Retrieving the host.
static const std::string sm_timeStampTZTypeName
The string literal representation for the time stamp with time zone 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.
static const std::string sm_geometryCollectionTypeName
The string literal representation for PostGIS geometry collection geometry type.
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.
A class for representing an Uniform Resource Identifier (URI).
int getType() const
It returns the property data type.
virtual std::unique_ptr< te::dt::DateTime > getDateTime(std::size_t i) const =0
Method for retrieving a date and time attribute value.
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual char getChar(std::size_t i) const =0
Method for retrieving a signed character attribute value (1 byte long).
Property * getElementType() const
It returns the type of array elements.
The type for variable-length multidimensional arrays.
This file contains utility functions used to manipulate data from a URI.
unsigned int getPrecision() const
It returns the total count of significant digits in the whole number, that is, the number of digits t...
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
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.
static const std::string sm_multiPolygonTypeName
The string literal representation for PostGIS multi-polygon geometry type.
static const std::string sm_multiPointMTypeName
The string literal representation for PostGIS multi-point with m geometry type.
An static class with global definitions.
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 bool getBool(std::size_t i) const =0
Method for retrieving a boolean attribute value.
static const std::string sm_polygonTypeName
The string literal representation for PostGIS polygon geometry type.
std::string GetSQLValue(const te::dt::Property *p, std::size_t propertyPos, te::da::DataSet *d, PGconn *conn)
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
TECOREEXPORT std::string URIDecode(const std::string &srcUri)
Decodes an encoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
std::string * getDefaultValue() const
It returns the default value associated to the property, or NULL if none is associated.
static const std::string sm_polygonMTypeName
The string literal representation for PostGIS polygon with m geometry type.
static const std::string sm_multiPolygonMTypeName
The string literal representation for PostGIS multi-polygon with m geometry type. ...
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.
static const std::string sm_varcharTypeName
The string literal representation for the varchar type.
static const std::string sm_timeStampTypeName
The string literal representation for the time stamp type.
unsigned int getScale() const
It returns the count of decimal digits in the fractional part, to the right of the decimal point...
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...
std::string user() const
Retrieving the user information.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
std::string GetSQLBindValues(std::size_t nproperties)
static const std::string sm_booleanTypeName
The string literal representation for the boolean type.
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)...
static const std::string sm_int8TypeName
The string literal representation for the int8 type.