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 "../datasource/DataSource.h"
35 
36 // STL Includes
37 #include <memory>
38 #include <string>
39 #include <vector>
40 
41 namespace te
42 {
43  namespace core
44  {
45  class URI;
46  }
47 }
48 
49 namespace te
50 {
51  namespace gm
52  {
53  class Envelope;
54  }
55 
56  namespace da
57  {
58  class DataSet;
59  class DataSetType;
60  class DataSetTypeConverter;
61  class Literal;
62  class ObjectIdSet;
63  class Select;
64 
65  /*!
66  \class DataAccess
67 
68  \brief Class used to centralize and control access to data in terralib.
69  It aims to create a high-level interface for simplifying the
70  concepts defined in the terralib DataAccess module.
71 
72  */
74  {
75  public:
76 
77  /*!
78  \brief constructor.
79 
80  \param uri The URI used to describe the datasource connection.
81  \param dataSourceDriver The data source type name (example: POSTGIS, ORACLE, SQLITE).
82  \param dataSetName The dataset name (not needed to inform if DataAccess will be used to create a new Dataset)
83 
84  \exception Exception An exception can be thrown, if the data source cannot be created or opened and dataset not found.
85 
86  \note A data source will be created without id information
87  */
88  DataAccess(const te::core::URI& uri, const std::string& dataSourceDriver, const std::string& dataSetName = "");
89 
90  /*!
91  \brief Constructor.
92 
93  \param dataSource The data source pointer.
94  \param dataSetName The dataset name (not needed to inform if DataAccess will be used to create a new Dataset)
95 
96  \exception Exception An exception can be thrown, if the data source cannot be opened or dataset not found.
97 
98  \note This class will take the ownership of data source pointer
99  */
100  DataAccess(te::da::DataSourcePtr dataSource, const std::string& dataSetName = "");
101 
102  /*!
103  \brief constructor.
104 
105  \param dataSource The data source pointer.
106  \param dataSetType The dataset type
107 
108  \exception Exception An exception can be thrown, if the data source cannot be opened or dataset not found.
109 
110  \note This class will take the ownership of data source pointer, but will clone the received dataSetType
111  */
112  DataAccess(te::da::DataSourcePtr dataSource, const te::da::DataSetType* dataSetType);
113 
114  /*! \brief Default destructor. */
115  ~DataAccess();
116 
117  //!< Check if the the dataSet exists
118  bool dataSetExists() const;
119 
120  /*!
121  \brief Get data source connection info
122 
123  \return String with data source connection info
124  */
125  std::string getDataSourceConnectionInfo();
126 
127  /*!
128  \brief Get data set name
129 
130  \return String with the new data set name
131  */
132  std::string getDataSetName() const;
133 
134  /*!
135  \brief Get data set type
136 
137  \return Pointer to the datasettype
138 
139  \note The caller of this method will NOT take the ownership of the given pointer
140  */
141  const te::da::DataSetType* getDataSetType() const;
142 
143  /*!
144  \brief Set data set converter type.
145 
146  \param converter The data set type converter pointer
147 
148  \note This class will take the ownership of converter pointer
149  */
150  void setConverter(te::da::DataSetTypeConverter* converter);
151 
152  /*!
153  \brief Get data set type
154 
155  \return Pointer to the datasettype
156 
157  \note The caller of this method will NOT take the ownership of the given pointer
158  */
159  te::da::DataSetTypeConverter* getConverter() const;
160 
161  /*!
162  \brief Set data set restriction.
163 
164  \param select The data set query pointer
165 
166  \note This class will take the ownership of query pointer
167  */
168  void setQuery(te::da::Select* select);
169 
170  /*!
171  \brief Get query object
172 
173  \return Pointer to the query object
174 
175  \note The caller of this method will NOT take the ownership of the given pointer
176  */
177  te::da::Select* getQuery() const;
178 
179  /*!
180  \brief Get query object as string
181 
182  \return String with the query restriction.
183  */
184  std::string getStrQuery() const;
185 
186  /*!
187  \brief Get dataset using internal information (data source, query, datasetTypeConverter)
188 
189  \return DataSet object
190  */
191  std::unique_ptr<te::da::DataSet> getDataSet() const;
192 
193  /*!
194  \brief Get the data source associated with this data access
195 
196  \return The data source associated with this data access
197  */
198  te::da::DataSourcePtr getDataSource() const;
199 
200  /*!
201  \brief Get dataset using internal information (data source, datasetTypeConverter), but using query parameter
202  for data restriction.
203 
204  \param select Select object that defines the data filter information.
205 
206  \return DataSet object
207  */
208  std::unique_ptr<te::da::DataSet> query(const te::da::Select& select);
209  std::unique_ptr<te::da::DataSet> query(const std::string& query);
210 
211  /*!
212  \brief Function used to create a new DataSet into internal datasource. This class will NOT take the ownership of the dataSetType pointer
213 
214  \param dataSet Pointer to a data set type to be created
215  */
216  void createDataSet(te::da::DataSetType* dataSetType);
217 
218  /*!
219  \brief Function used to save data into internal datasource (if a converter was defined, it will be used)
220 
221  \param dataSet Pointer to a data set to be saved
222 
223  \note DataAccess will take the ownership of dataset and erase the data at end
224  */
225  void saveDataSet(te::da::DataSet* dataSet, bool enableProgress = true);
226 
227  /*!
228  \brief Function used to get distinct values ir order, from a specific attribute
229 
230  \param attributeName String with attribute name
231 
232  \return A vector with all distinct values from a attribute in order.
233  */
234  std::vector<std::string> getDistinctValues(const std::string& attributeName);
235 
236  /*!
237  \brief It creates a Literal Expression based on a attribute type
238 
239  \param attributeName Attribute name from data set
240 
241  \param value Attribute value used to create the right Literal expression
242 
243  \return Valid pointer if the attribute type is supported and a null pointer in other case
244  */
245  te::da::Literal* getLiteral(const std::string& attributeName, const std::string& value);
246 
247  /*!
248  \brief Gets the MBR of the data access
249 
250  \return The MBR of the data access
251  */
252  te::gm::Envelope getMBR() const;
253 
254  /*!
255  \brief Gets the SRID of the data access
256 
257  \return The SRID of the data access
258  */
259  int getSRID() const;
260 
261  //!< The the objectId set to be used as a filter
262  void setObjectIdSetFilter(const te::da::ObjectIdSet* objectIdSetFilter);
263 
264  protected:
265 
267  std::unique_ptr<te::da::DataSetType> m_dataSetType; //!< Data set type.
268  std::unique_ptr<te::da::DataSetTypeConverter> m_dataSetTypeConverter; //!< DataSetType Converter
269  std::unique_ptr<te::da::Select> m_dataSetQuery; //!< Filter used to load data with restriction
270  std::string m_dataSetName; //!< Dataset name.
271  std::unique_ptr<te::da::ObjectIdSet> m_objectIdSetFilter; //!< A optional filter containg an objectId set
272  };
273 
274  class TEDATAACCESSEXPORT DataAccessPtr : public std::unique_ptr<te::da::DataAccess>
275  {
276  public:
277  DataAccessPtr(te::da::DataAccess* dataAccess = nullptr) : std::unique_ptr<te::da::DataAccess>(dataAccess) {};
278  };
279  class TEDATAACCESSEXPORT DataAccessSharedPtr : public std::shared_ptr<te::da::DataAccess>
280  {
281  public:
282  DataAccessSharedPtr(te::da::DataAccess* dataAccess = nullptr) : std::shared_ptr<te::da::DataAccess>(dataAccess) {};
283  };
284  }
285 }
286 
287 #endif // __TERRALIB_DATAACCESS_INTERNAL_DATAACCESS_H
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1449
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::string m_dataSetName
Dataset name.
Definition: DataAccess.h:270
std::unique_ptr< te::da::DataSetType > m_dataSetType
Data set type.
Definition: DataAccess.h:267
An converter for DataSetType.
std::unique_ptr< te::da::Select > m_dataSetQuery
Filter used to load data with restriction.
Definition: DataAccess.h:269
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
This class models a literal value.
Definition: Literal.h:53
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
TerraLib.
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
A class to store the proxy information that must be used to access data located in URIs...
Definition: URI.h:49
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:113
std::unique_ptr< te::da::ObjectIdSet > m_objectIdSetFilter
A optional filter containg an objectId set.
Definition: DataAccess.h:271
DataAccessPtr(te::da::DataAccess *dataAccess=nullptr)
Definition: DataAccess.h:277
DataAccessSharedPtr(te::da::DataAccess *dataAccess=nullptr)
Definition: DataAccess.h:282
te::da::DataSourcePtr m_dataSource
Data source.
Definition: DataAccess.h:266
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97
Class used to centralize and control access to data in terralib. It aims to create a high-level inter...
Definition: DataAccess.h:73
std::unique_ptr< te::da::DataSetTypeConverter > m_dataSetTypeConverter
DataSetType Converter.
Definition: DataAccess.h:268