27 #include "../common/StringUtils.h"
28 #include "../common/Translator.h"
29 #include "../dataaccess/dataset/CheckConstraint.h"
30 #include "../dataaccess/dataset/DataSet.h"
31 #include "../dataaccess/dataset/ForeignKey.h"
32 #include "../dataaccess/dataset/Index.h"
33 #include "../dataaccess/dataset/ObjectIdSet.h"
34 #include "../dataaccess/dataset/PrimaryKey.h"
35 #include "../dataaccess/dataset/Sequence.h"
36 #include "../dataaccess/dataset/UniqueKey.h"
37 #include "../dataaccess/datasource/DataSourceCatalog.h"
38 #include "../dataaccess/datasource/ScopedTransaction.h"
39 #include "../dataaccess/query/Select.h"
40 #include "../dataaccess/query/SQLDialect.h"
41 #include "../dataaccess/utils/Utils.h"
42 #include "../datatype/Array.h"
43 #include "../datatype/Date.h"
44 #include "../datatype/DateTimeProperty.h"
45 #include "../datatype/Property.h"
46 #include "../datatype/SimpleData.h"
47 #include "../datatype/StringProperty.h"
48 #include "../geometry/Envelope.h"
49 #include "../geometry/GeometryProperty.h"
50 #include "../geometry/Utils.h"
51 #include "../geometry/Geometry.h"
52 #include "../memory/DataSet.h"
53 #include "../raster/RasterProperty.h"
63 #include <terralib/kernel/TeDatabase.h>
64 #include <terralib/kernel/TeLayer.h>
74 #include <boost/format.hpp>
75 #include <boost/lexical_cast.hpp>
81 m_isInTransaction(false)
96 m_db->beginTransaction();
97 m_isInTransaction =
true;
102 m_db->commitTransaction();
103 m_isInTransaction =
false;
108 m_db->rollbackTransaction();
109 m_isInTransaction =
false;
114 return m_isInTransaction;
122 TeLayerMap map = m_db->layerMap();
124 std::map<int, TeLayer*>::iterator it = map.begin();
128 while(it != map.end())
130 if(it->second->name() == name)
138 if(layer->hasGeometry(TeRASTER))
140 return std::auto_ptr<te::da::DataSet>(
new RasterDataSet(layer->raster()));
143 return std::auto_ptr<te::da::DataSet>(
new VectorDataSet(layer));
147 const std::string& propertyName,
154 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
158 const std::string& propertyName,
165 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
169 const ObjectIdSet* oids,
174 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
184 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver doesn't support queries!"));
192 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver doesn't support queries!"));
197 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver doesn't support command execution!"));
202 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver doesn't support command execution!"));
207 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver doesn't support prepared queries!"));
212 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver doesn't support prepared batch executors!"));
221 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver is read-only!"));
226 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
231 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver is read-only!"));
236 throw Exception(
TR_TERRALIB4(
"TerraLib 4.x driver is read-only!"));
241 m_db->loadLayerSet(
true);
243 TeLayerMap map = m_db->layerMap();
245 std::map<int, TeLayer*>::iterator it = map.begin();
247 std::vector<std::string> layers;
251 while(it != map.end())
253 layers.push_back(it->second->name());
263 return m_db->layerMap().size();
268 if(!m_db->layerExist(name))
269 return std::auto_ptr<te::da::DataSetType>(0);
271 TeLayerMap map = m_db->layerMap();
273 std::map<int, TeLayer*>::iterator it = map.begin();
277 while(it != map.end())
279 if(it->second->name() == name)
287 if(layer->hasGeometry(TeRASTER))
297 TeAttrTableVector tables;
298 layer->getAttrTables(tables);
300 TeTable table = tables[0];
303 mainDst->setTitle(layer->name());
305 std::vector<std::string> pkey;
306 table.primaryKeys(pkey);
314 std::vector<te::dt::Property*> pkProps;
315 for(std::size_t i = 0; i < pkey.size(); ++i)
318 pkProps.push_back(p);
324 if(tables.size() > 1)
326 for(std::size_t i = 1; i < tables.size(); ++i)
328 TeTable table = tables[i];
332 std::vector<te::dt::Property*> props = dst->getProperties();
334 for(std::size_t j = 0; j < props.size(); ++j)
344 TeRepresPointerVector vec = layer->vectRepres();
348 TeGeomRep geomRep = vec[0]->geomRep_;
352 mainDst->add(geomProp);
360 std::auto_ptr<te::da::DataSetType> dt = getDataSetType(datasetName);
362 std::vector<te::dt::Property*> dtProperties = dt->getProperties();
364 boost::ptr_vector<te::dt::Property> properties;
366 for(std::size_t i = 0; i < dtProperties.size(); ++i)
367 properties.push_back(dtProperties[i]->clone());
374 if(!propertyExists(datasetName, name))
375 throw Exception((boost::format(
TR_TERRALIB4(
"The dataset \"%1%\" has no property with this name \"%2%\"!")) % datasetName % name).str());
377 std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
379 return std::auto_ptr<te::dt::Property>(dt->getProperty(name)->clone());
384 std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
386 assert(propertyPos < dt->size());
388 return std::auto_ptr<te::dt::Property>(dt->getProperty(propertyPos)->clone());
393 std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
395 std::vector<std::string> pNames;
397 std::size_t numProperties = dt->size();
399 for(std::size_t i = 0; i < numProperties; ++i)
400 pNames.push_back(dt->getProperty(i)->getName());
407 std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
414 std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
416 std::vector<std::string> pNames = getPropertyNames(datasetName);
418 if(std::find(pNames.begin(), pNames.end(), name) != pNames.end())
426 std::string name = p->
getName();
427 if(propertyExists(datasetName, name))
428 throw Exception((boost::format(
TR_TERRALIB4(
"The dataset already \"%1%\" has a property with this name \"%2%\"!")) % datasetName % name).str());
432 TeAttributeRep newProperty;
434 newProperty.name_ = name;
444 TeLayerMap map = m_db->layerMap();
446 std::map<int, TeLayer*>::iterator it = map.begin();
450 while(it != map.end())
452 if(it->second->name() == datasetName)
460 TeAttrTableVector tables;
461 layer->getAttrTables(tables);
462 std::string tableName = tables[0].name();
464 m_db->addColumn(tableName, newProperty);
473 const std::string& propertyName,
474 const std::string& newPropertyName)
476 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
481 std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
483 return std::auto_ptr<te::da::PrimaryKey>(
static_cast<te::da::PrimaryKey*
>(dt->getPrimaryKey()->clone()));
488 std::auto_ptr<te::da::DataSetType> dt(getDataSetType(datasetName));
490 if(dt->getPrimaryKey()->getName() == name)
498 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
503 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
508 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
513 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
518 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
523 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
528 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
533 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
538 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
543 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
548 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
553 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
558 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
563 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
568 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
573 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
578 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
583 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
588 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
593 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
597 const std::map<std::string, std::string>& options)
599 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
604 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
609 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
614 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
619 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
624 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
629 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
635 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
641 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
646 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
651 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
656 std::vector<std::string> names = getDataSetNames();
658 for(std::size_t i = 0; i < names.size(); ++i)
667 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
671 const std::string& cloneName,
672 const std::map<std::string, std::string>& options)
674 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
679 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
684 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
689 const std::map<std::string, std::string>& options,
692 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
697 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
702 const std::vector<std::size_t>& properties,
704 const std::map<std::string, std::string>& options,
707 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
712 throw Exception(
TR_TERRALIB4(
"This method is not supported by TerraLib 4.x driver!"));
Implementation of a dataset for the TerraLib 4 driver.
std::auto_ptr< te::da::PrimaryKey > getPrimaryKey(const std::string &datasetName)
It retrieves the primary key of the dataset.
std::auto_ptr< te::da::UniqueKey > getUniqueKey(const std::string &datasetName, const std::string &name)
It gets the unique key in the dataset with the given name.
void dropCheckConstraint(const std::string &datasetName, const std::string &name)
It removes the check constraint from the dataset.
It describes an index associated to a DataSetType.
void dropPrimaryKey(const std::string &datasetName)
It removes the primary key constraint from the dataset schema.
void rollBack()
It aborts the transaction. Any changes will be rolled-back.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
DataSourceTransactor implementation for TerraLib 4.x API.
std::auto_ptr< te::da::BatchExecutor > getBatchExecutor()
It creates a batch command executor.
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.
bool foreignKeyExists(const std::string &datasetName, const std::string &name)
It checks if a foreign key with the given name exists in the data source.
bool indexExists(const std::string &datasetName, const std::string &name)
It checks if an index with the given name exists in the dataset.
void dropProperty(const std::string &datasetName, const std::string &name)
It removes a property from the given dataset.
std::size_t getNumberOfItems(const std::string &datasetName)
It retrieves the number of items of the given dataset.
A Select models a query to be used when retrieving data from a DataSource.
std::auto_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
#define TR_TERRALIB4(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
void addSequence(te::da::Sequence *sequence)
It creates a new sequence in the data source.
std::auto_ptr< te::da::ForeignKey > getForeignKey(const std::string &datasetName, const std::string &name)
It retrieves the foreign key from the given dataset.
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
bool isPropertyNameValid(const std::string &propertyName)
It checks if the given property name is valid.
std::auto_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
std::vector< std::string > getSequenceNames()
It gets the sequence names available in the data source.
TeAttrDataType Convert2T4GeomType(te::gm::GeomType type)
It describes a primary key (pk) constraint.
te::gm::GeomType Convert2T5GeomType(TeAttrDataType type)
const std::string & getName() const
It returns the property name.
bool hasDataSets()
It checks if the data source has any dataset.
It describes a sequence (a number generator).
void addPrimaryKey(const std::string &datasetName, te::da::PrimaryKey *pk)
It adds a primary key constraint to the dataset schema.
SpatialRelation
Spatial relations between geometric objects.
It models a foreign key constraint for a DataSetType.
void renameProperty(const std::string &datasetName, const std::string &propertyName, const std::string &newPropertyName)
It renames a property of the given dataset.
std::auto_ptr< te::da::PreparedQuery > getPrepared(const std::string &qName=std::string(""))
It creates a prepared query object that may be used for query commands (select, insert, update and delete) that are used repeatedly.
std::size_t getNumberOfDataSets()
It retrieves the number of data sets available in the data source.
void cloneDataSet(const std::string &name, const std::string &cloneName, const std::map< std::string, std::string > &options)
It clones the dataset in the data source.
void setName(const std::string &name)
It sets the property name.
AccessPolicy
Supported data access policies (can be used as bitfield).
void dropForeignKey(const std::string &datasetName, const std::string &fkName)
It removes the foreign key constraint from the dataset schema.
bool propertyExists(const std::string &datasetName, const std::string &name)
It checks if a property with the given name exists in the dataset.
Utility functions for the data access module.
A Query is independent from the data source language/dialect.
void createDataSet(te::da::DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
void optimize(const std::map< std::string, std::string > &opInfo)
For some data access drivers, this method will perform some operations to optimize the data storage...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
bool isDataSetNameValid(const std::string &datasetName)
It returns true if the given string is a valid dataset name.
bool isInTransaction() const
It returns true if a transaction is in progress, otherwise, it returns false.
void addCheckConstraint(const std::string &datasetName, te::da::CheckConstraint *cc)
It adds a check constraint to the dataset.
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
void dropSequence(const std::string &name)
It removes the sequence from the data source.
std::auto_ptr< te::da::DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It gets the dataset identified by the given name. A dataset can be connected or disconnected. A connected dataset, after its creation through the data source transactor, continues to depend on the connection given by its associated data source. Differently, a disconnected dataset, after its creation, no more depends of the connection given by the data source, and it continues to live after the connection has been released to the data source.
It describes a unique key (uk) constraint.
An static class with global definitions.
void update(const std::string &datasetName, te::da::DataSet *dataset, const std::vector< std::size_t > &properties, const te::da::ObjectIdSet *oids, const std::map< std::string, std::string > &options, std::size_t limit=0)
It updates the contents of a dataset for the set of data items.
Transactor(DataSource *ds, TeDatabase *db)
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
void cancel()
It requests that the data source stop the processing of the current command.
void remove(const std::string &datasetName, const te::da::ObjectIdSet *oids=0)
It removes all the informed items from the dataset.
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
TraverseType
A dataset can be traversed in two ways:
void begin()
It starts a new transaction.
void addForeignKey(const std::string &datasetName, te::da::ForeignKey *fk)
It adds a foreign key constraint to a dataset.
std::vector< std::string > getUniqueKeyNames(const std::string &datasetName)
It gets the unique key names of the given dataset.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
std::vector< std::string > getIndexNames(const std::string &datasetName)
It gets the index names of the given dataset.
A class that models the description of a dataset.
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
TeAttrDataType Convert2T4(int type)
It converts a Terralib 5 data type to Terralib 4.x data type.
It models a property definition.
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
std::vector< std::string > getForeignKeyNames(const std::string &datasetName)
It gets the foreign key names of the given dataset.
void add(const std::string &datasetName, te::da::DataSet *d, const std::map< std::string, std::string > &options, std::size_t limit=0)
It adds data items to the dataset in the data source.
std::auto_ptr< te::da::Sequence > getSequence(const std::string &name)
It gets the sequence with the given name in the data source.
int getType() const
It returns the property data type.
void execute(const te::da::Query &command)
It executes the specified command using a generic query representation.
bool primaryKeyExists(const std::string &datasetName, const std::string &name)
It checks if a primary key exists in the dataset.
bool sequenceExists(const std::string &name)
It checks if a sequence with the given name exists in the data source.
void dropUniqueKey(const std::string &datasetName, const std::string &name)
It removes the unique key constraint from the dataset.
std::auto_ptr< te::da::Index > getIndex(const std::string &datasetName, const std::string &name)
It gets the index with the given name from the dataset.
bool uniqueKeyExists(const std::string &datasetName, const std::string &name)
It checks if a unique key with the given name exists in the dataset.
A class that describes a check constraint.
An Envelope defines a 2D rectangular region.
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
boost::int64_t getLastGeneratedId()
It returns the last id generated by an insertion command.
void addProperty(const std::string &datasetName, te::dt::Property *p)
It adds a new property to the dataset schema.
void setProperties(const std::vector< te::dt::Property * > &properties)
It sets the properties that form the primary key.
std::auto_ptr< te::dt::Property > getProperty(const std::string &datasetName, const std::string &name)
It retrieves the property with the given name from the dataset.
void commit()
It commits the transaction.
std::auto_ptr< te::gm::Envelope > getExtent(const std::string &datasetName, const std::string &propertyName)
It retrieves the bounding rectangle of the spatial property for the given dataset.
std::auto_ptr< te::da::DataSet > query(const te::da::Select &q, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It executes a query that may return some data using a generic query. A dataset can be connected or di...
virtual Property * clone() const =0
It returns a clone of the object.
void addIndex(const std::string &datasetName, te::da::Index *idx, const std::map< std::string, std::string > &options)
It adds an index to the dataset.
bool checkConstraintExists(const std::string &datasetName, const std::string &name)
It checks if a check-constraint with the given name exists in the data source.
te::da::DataSource * getDataSource() const
It returns the parent data source of the transactor.
std::auto_ptr< te::da::CheckConstraint > getCheckConstraint(const std::string &datasetName, const std::string &name)
It gets the check constraint of the dataset with the given name.
A dataset is the unit of information manipulated by the data access module of TerraLib.
std::vector< std::string > getCheckConstraintNames(const std::string &datasetName)
It gets the check constraint names of the given dataset.
void dropIndex(const std::string &datasetName, const std::string &idxName)
It removes the index from the dataset schema.
std::string escape(const std::string &value)
It escapes a string for using in commands and queries.
void addUniqueKey(const std::string &datasetName, te::da::UniqueKey *uk)
It adds a unique key constraint to the dataset.