DataAccess.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21 \file dataaccess/utils/DataAccess.h
22 
23 \brief Class used to centralize and control access to data in terralib.
24  It aims to create a high-level interface for simplifying the
25  concepts defined in the terralib DataAccess module.
26 */
27 
28 #ifndef __TERRALIB_DATAACCESS_INTERNAL_DATAACCESS_H
29 #define __TERRALIB_DATAACCESS_INTERNAL_DATAACCESS_H
30 
31 #include "../Config.h"
32 
33 // TerraLib
34 #include "../../datatype/AbstractData.h"
35 #include "../datasource/DataSource.h"
36 #include "../datasource/DataSourceTransactor.h"
37 
38 // STL Includes
39 #include <memory>
40 #include <string>
41 #include <vector>
42 
43 namespace te
44 {
45  namespace core
46  {
47  class URI;
48  }
49 }
50 
51 namespace te
52 {
53  namespace gm
54  {
55  class Envelope;
56  }
57 
58  namespace da
59  {
60  class DataSet;
61  class DataSetType;
62  class DataSetTypeConverter;
63  class Expression;
64  class Literal;
65  class ObjectIdSet;
66  class Select;
67 
68  /*!
69  \class DataAccess
70 
71  \brief Class used to centralize and control access to data in terralib.
72  It aims to create a high-level interface for simplifying the
73  concepts defined in the terralib DataAccess module.
74 
75  */
77  {
78  public:
79 
80  /*!
81  \brief constructor.
82 
83  \param uri The URI used to describe the datasource connection.
84  \param dataSourceDriver The data source type name (example: POSTGIS, ORACLE, SQLITE).
85  \param dataSetName The dataset name (not needed to inform if DataAccess will be used to create a new Dataset)
86 
87  \exception Exception An exception can be thrown, if the data source cannot be created or opened and dataset not found.
88 
89  \note A data source will be created without id information
90  */
91  DataAccess(const te::core::URI& uri, const std::string& dataSourceDriver, const std::string& dataSetName = "");
92 
93  /*!
94  \brief Constructor.
95 
96  \param dataSource The data source pointer.
97  \param dataSetName The dataset name (not needed to inform if DataAccess will be used to create a new Dataset)
98 
99  \exception Exception An exception can be thrown, if the data source cannot be opened or dataset not found.
100 
101  \note This class will take the ownership of data source pointer
102  */
103  DataAccess(te::da::DataSourcePtr dataSource, const std::string& dataSetName = "");
104 
105  /*!
106  \brief constructor.
107 
108  \param dataSource The data source pointer.
109  \param dataSetType The dataset type
110 
111  \exception Exception An exception can be thrown, if the data source cannot be opened or dataset not found.
112 
113  \note This class will take the ownership of data source pointer, but will clone the received dataSetType
114  */
115  DataAccess(te::da::DataSourcePtr dataSource, const te::da::DataSetType* dataSetType);
116 
117  //!< Copy constructor
119 
120  /*! \brief Default destructor. */
121  virtual ~DataAccess();
122 
123  virtual te::dt::AbstractData* clone() const override;
124 
125  //!< Begins a transaction
127 
128  //!< Commits a transaction
130 
131  //!< Rollbacks the transaction
133 
134  //!< When handling with files, specially using OGR, the olnly way to ensure the data is writen tis to close dataSource. So this functions encapsulates it
135  void flush();
136 
137  virtual int getTypeCode() const override;
138 
139  virtual std::string toString() const override;
140 
141  //!< Check if the the dataSet exists
142  bool dataSetExists() const;
143 
144  /*!
145  \brief Get data source connection info
146 
147  \return String with data source connection info
148  */
149  std::string getDataSourceConnectionInfo() const;
150 
151  /*!
152  \brief Get data set name
153 
154  \return String with the new data set name
155  */
156  std::string getDataSetName() const;
157 
158  /*!
159  \brief Get data set type
160 
161  \return Pointer to the datasettype
162 
163  \note The caller of this method will NOT take the ownership of the given pointer
164  */
166 
167  /*!
168  \brief Set data set converter type.
169 
170  \param converter The data set type converter pointer
171 
172  \note This class will take the ownership of converter pointer
173  */
175 
176  /*!
177  \brief Get data set type
178 
179  \return Pointer to the datasettype
180 
181  \note The caller of this method will NOT take the ownership of the given pointer
182  */
184 
185  /*!
186  \brief Set data set restriction.
187 
188  \param select The data set query pointer
189 
190  \note This class will take the ownership of query pointer
191  */
192  void setQuery(te::da::Select* select);
193 
194  /*!
195  \brief Get query object
196 
197  \return Pointer to the query object
198 
199  \note The caller of this method will NOT take the ownership of the given pointer
200  */
202 
203  /*!
204  \brief Get query object as string
205 
206  \return String with the query restriction.
207  */
208  std::string getStrQuery() const;
209 
210  /*!
211  \brief Get dataset using internal information (data source, query, datasetTypeConverter)
212 
213  \return DataSet object
214  */
215  std::unique_ptr<te::da::DataSet> getDataSet() const;
216 
217  /*!
218  \brief Get dataset using internal information (data source, query, datasetTypeConverter), using the given filter
219 
220  \return DataSet object
221  */
222  std::unique_ptr<te::da::DataSet> getDataSet(const te::da::Expression* filter) const;
223 
224  /*!
225  \brief Get the data source associated with this data access
226 
227  \return The data source associated with this data access
228  */
230 
231  /*!
232  \brief Get dataset using internal information (data source, datasetTypeConverter), but using query parameter
233  for data restriction.
234 
235  \param select Select object that defines the data filter information.
236 
237  \return DataSet object
238  */
239  std::unique_ptr<te::da::DataSet> query(const te::da::Select& select);
240  std::unique_ptr<te::da::DataSet> query(const std::string& query);
241 
242  /*!
243  \brief Executes the given sql in the current transactor
244  */
245  void execute(const std::string& query);
246 
247  /*!
248  \brief Function used to create a new DataSet into internal datasource. This class will NOT take the ownership of the dataSetType pointer
249 
250  \param dataSet Pointer to a data set type to be created
251  */
252  void createDataSet(te::da::DataSetType* dataSetType);
253 
254  /*!
255  \brief Function used to save data into internal datasource (if a converter was defined, it will be used)
256 
257  \param dataSet Pointer to a data set to be saved
258 
259  \note DataAccess will take the ownership of dataset and erase the data at end
260  */
261  void saveDataSet(te::da::DataSet* dataSet, bool enableProgress = true);
262 
263  /*!
264  \brief Function used to get distinct values ir order, from a specific attribute
265 
266  \param attributeName String with attribute name
267 
268  \return A vector with all distinct values from a attribute in order.
269  */
270  std::vector<std::string> getDistinctValues(const std::string& attributeName);
271 
272  /*!
273  \brief It creates a Literal Expression based on a attribute type
274 
275  \param attributeName Attribute name from data set
276 
277  \param value Attribute value used to create the right Literal expression
278 
279  \return Valid pointer if the attribute type is supported and a null pointer in other case
280  */
281  te::da::Literal* getLiteral(const std::string& attributeName, const std::string& value);
282 
283  /*!
284  \brief Gets the MBR of the data access
285 
286  \return The MBR of the data access
287  */
289 
290  /*!
291  \brief Gets the SRID of the data access
292 
293  \return The SRID of the data access
294  */
295  int getSRID() const;
296 
297  //!< The the objectId set to be used as a filter
298  void setObjectIdSetFilter(const te::da::ObjectIdSet* objectIdSetFilter);
299 
300  //!< Gets the current transactor. If no transactor is defined, it creates one
302 
303  //!< Sets he transactor to be used
305 
306  protected:
307 
310  std::unique_ptr<te::da::DataSetType> m_dataSetType; //!< Data set type.
311  std::unique_ptr<te::da::DataSetTypeConverter> m_dataSetTypeConverter; //!< DataSetType Converter
312  std::unique_ptr<te::da::Select> m_dataSetQuery; //!< Filter used to load data with restriction
313  std::string m_dataSetName; //!< Dataset name.
314  std::unique_ptr<te::da::ObjectIdSet> m_objectIdSetFilter; //!< A optional filter containg an objectId set
315  };
316 
317  class TEDATAACCESSEXPORT DataAccessPtr : public std::unique_ptr<te::da::DataAccess>
318  {
319  public:
320  DataAccessPtr(te::da::DataAccess* dataAccess = nullptr) : std::unique_ptr<te::da::DataAccess>(dataAccess) {};
321  };
322  class TEDATAACCESSEXPORT DataAccessSharedPtr : public std::shared_ptr<te::da::DataAccess>
323  {
324  public:
325  DataAccessSharedPtr(te::da::DataAccess* dataAccess = nullptr) : std::shared_ptr<te::da::DataAccess>(dataAccess) {};
326  };
327  }
328 }
329 
330 #endif // __TERRALIB_DATAACCESS_INTERNAL_DATAACCESS_H
te::da::DataAccess::getStrQuery
std::string getStrQuery() const
Get query object as string.
te::gm::Envelope
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:52
te::da::DataAccess::getDataSetName
std::string getDataSetName() const
Get data set name.
te::da::DataAccess::toString
virtual std::string toString() const override
Check if the the dataSet exists.
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::da::DataAccess::m_objectIdSetFilter
std::unique_ptr< te::da::ObjectIdSet > m_objectIdSetFilter
A optional filter containg an objectId set.
Definition: DataAccess.h:314
te::da::DataAccess::getDataSource
te::da::DataSourcePtr getDataSource() const
Get the data source associated with this data access.
te::da::DataAccess::getConverter
te::da::DataSetTypeConverter * getConverter() const
Get data set type.
te::da::DataAccess::getQuery
te::da::Select * getQuery() const
Get query object.
te::da::DataAccess::rollbackTransaction
void rollbackTransaction()
When handling with files, specially using OGR, the olnly way to ensure the data is writen tis to clos...
te::da::DataAccess::m_dataSource
te::da::DataSourcePtr m_dataSource
Data source.
Definition: DataAccess.h:308
te::da::DataAccess::dataSetExists
bool dataSetExists() const
te::da::DataAccess::query
std::unique_ptr< te::da::DataSet > query(const te::da::Select &select)
Get dataset using internal information (data source, datasetTypeConverter), but using query parameter...
te::da::DataAccess::getMBR
te::gm::Envelope getMBR() const
Gets the MBR of the data access.
te::da::DataAccessPtr
Definition: DataAccess.h:318
te::da::DataAccess::commitTransaction
void commitTransaction()
Rollbacks the transaction.
te::da::DataAccess::~DataAccess
virtual ~DataAccess()
Default destructor.
TEDATAACCESSEXPORT
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97
te::da::DataAccess::getDataSetType
const te::da::DataSetType * getDataSetType() const
Get data set type.
te::da::DataAccess::execute
void execute(const std::string &query)
Executes the given sql in the current transactor.
te::da::DataAccess::DataAccess
DataAccess(const te::core::URI &uri, const std::string &dataSourceDriver, const std::string &dataSetName="")
constructor.
te::da::DataAccess::flush
void flush()
te::da::DataAccess::m_dataSetTypeConverter
std::unique_ptr< te::da::DataSetTypeConverter > m_dataSetTypeConverter
DataSetType Converter.
Definition: DataAccess.h:311
te::da::DataAccess
Class used to centralize and control access to data in terralib. It aims to create a high-level inter...
Definition: DataAccess.h:77
te::da::DataAccess::beginTransaction
void beginTransaction()
Commits a transaction.
te::da::DataAccess::getSRID
int getSRID() const
Gets the SRID of the data access.
te::da::DataAccess::saveDataSet
void saveDataSet(te::da::DataSet *dataSet, bool enableProgress=true)
Function used to save data into internal datasource (if a converter was defined, it will be used)
te::da::Expression
This is an abstract class that models a query expression.
Definition: Expression.h:48
te::da::DataAccess::getTransactor
te::da::DataSourceTransactorPtr getTransactor() const
Sets he transactor to be used.
te::da::DataAccess::query
std::unique_ptr< te::da::DataSet > query(const std::string &query)
te::da::Literal
This class models a literal value.
Definition: Literal.h:54
te::core::URI
A class to store the proxy information that must be used to access data located in URIs.
Definition: URI.h:50
te::da::DataAccess::setObjectIdSetFilter
void setObjectIdSetFilter(const te::da::ObjectIdSet *objectIdSetFilter)
Gets the current transactor. If no transactor is defined, it creates one.
te::da::DataAccess::DataAccess
DataAccess(const te::da::DataAccess &rhs)
te::da::DataAccess::setTransactor
void setTransactor(te::da::DataSourceTransactorPtr transactor)
te::da::DataAccessSharedPtr::DataAccessSharedPtr
DataAccessSharedPtr(te::da::DataAccess *dataAccess=nullptr)
Definition: DataAccess.h:325
te::da::DataAccess::getDataSourceConnectionInfo
std::string getDataSourceConnectionInfo() const
Get data source connection info.
te::da::DataAccess::setQuery
void setQuery(te::da::Select *select)
Set data set restriction.
te::da::DataAccess::getDataSet
std::unique_ptr< te::da::DataSet > getDataSet(const te::da::Expression *filter) const
Get dataset using internal information (data source, query, datasetTypeConverter),...
te::da::DataAccessSharedPtr
Definition: DataAccess.h:323
te::da::DataAccess::getLiteral
te::da::Literal * getLiteral(const std::string &attributeName, const std::string &value)
It creates a Literal Expression based on a attribute type.
te::da::DataAccess::getDistinctValues
std::vector< std::string > getDistinctValues(const std::string &attributeName)
Function used to get distinct values ir order, from a specific attribute.
te::da::DataAccessPtr::DataAccessPtr
DataAccessPtr(te::da::DataAccess *dataAccess=nullptr)
Definition: DataAccess.h:320
te::dt::AbstractData
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:56
te::da::Select
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:67
te::da::DataAccess::getDataSet
std::unique_ptr< te::da::DataSet > getDataSet() const
Get dataset using internal information (data source, query, datasetTypeConverter)
te::da::DataSourcePtr
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1449
te::da::DataSet
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:114
te::da::DataAccess::m_dataSetType
std::unique_ptr< te::da::DataSetType > m_dataSetType
Data set type.
Definition: DataAccess.h:310
te::da::DataAccess::m_transactor
te::da::DataSourceTransactorPtr m_transactor
Transactor.
Definition: DataAccess.h:309
te::da::DataAccess::m_dataSetName
std::string m_dataSetName
Dataset name.
Definition: DataAccess.h:313
te::da::DataSetType
A class that models the description of a dataset.
Definition: DataSetType.h:73
te::da::DataAccess::getTypeCode
virtual int getTypeCode() const override
It returns the data type code associated to the data value.
te::da::DataAccess::clone
virtual te::dt::AbstractData * clone() const override
Begins a transaction.
te::da::DataAccess::setConverter
void setConverter(te::da::DataSetTypeConverter *converter)
Set data set converter type.
te::da::DataAccess::DataAccess
DataAccess(te::da::DataSourcePtr dataSource, const std::string &dataSetName="")
Constructor.
te::da::DataSetTypeConverter
An converter for DataSetType.
Definition: DataSetTypeConverter.h:60
te::da::DataAccess::DataAccess
DataAccess(te::da::DataSourcePtr dataSource, const te::da::DataSetType *dataSetType)
constructor.
te::da::DataSourceTransactorPtr
boost::shared_ptr< DataSourceTransactor > DataSourceTransactorPtr
Definition: DataSourceTransactor.h:1251
te::da::ObjectIdSet
This class represents a set of unique ids created in the same context. i.e. from the same data set.
Definition: ObjectIdSet.h:56
te::da::DataAccess::m_dataSetQuery
std::unique_ptr< te::da::Select > m_dataSetQuery
Filter used to load data with restriction.
Definition: DataAccess.h:312
te::da::DataAccess::createDataSet
void createDataSet(te::da::DataSetType *dataSetType)
Function used to create a new DataSet into internal datasource. This class will NOT take the ownershi...