All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSource.h
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 terralib/dataaccess/datasource/DataSource.h
22 
23  \brief An abstract class for data providers like a DBMS, Web Services or a regular file.
24 */
25 
26 #ifndef __TERRALIB_DATAACCESS_DATASOURCE_INTERNAL_DATASOURCE_H
27 #define __TERRALIB_DATAACCESS_DATASOURCE_INTERNAL_DATASOURCE_H
28 
29 // TerraLib
30 #include "../../common/Enums.h"
31 #include "../../geometry/Enums.h"
32 #include "../dataset/CheckConstraint.h"
33 #include "../dataset/DataSet.h"
34 #include "../dataset/DataSetType.h"
35 #include "../dataset/ForeignKey.h"
36 #include "../dataset/Index.h"
37 #include "../dataset/PrimaryKey.h"
38 #include "../dataset/Sequence.h"
39 #include "../dataset/UniqueKey.h"
40 #include "../Config.h"
41 
42 // STL
43 #include <map>
44 #include <memory>
45 #include <string>
46 #include <vector>
47 
48 // Boost
49 #include <boost/ptr_container/ptr_vector.hpp>
50 #include <boost/cstdint.hpp>
51 #include <boost/noncopyable.hpp>
52 #include <boost/shared_ptr.hpp>
53 
54 namespace te
55 {
56  namespace dt
57  {
58  class Property;
59  }
60 
61  namespace gm
62  {
63  class Envelope;
64  class Geometry;
65  }
66 
67  namespace da
68  {
69  class DataSourceCapabilities;
70  class DataSourceTransactor;
71  class ObjectIdSet;
72  class Query;
73  class Select;
74  class SQLDialect;
75  class CheckConstraint;
76  class ForeignKey;
77  class Index;
78  class PrimaryKey;
79  class Sequence;
80  class UniqueKey;
81 
82  /*!
83  \class DataSource
84 
85  \brief An abstract class for data providers like a DBMS, Web Services or a regular file.
86 
87  A data source is the fundamental class of the data access module and
88  it may represent a data repository. Among other things, a repository can be:
89  <ul>
90  <li>a PostgreSQL database</li>
91  <li>an Oracle database</li>
92  <li>an OGC Web Feature Service</li>
93  <li>a directory of ESRI shape-files</li>
94  <li>a data stream</li>
95  <li>
96 
97  A data source can be used to access data in a repository
98  and it requires a driver implementation that must register a
99  concrete factory for building data source objects
100  of the type of the driver (DataSourceFactory).
101 
102  Each data source is characterized by a set of parameters that can be used
103  to set up an access channel to its underlying repository. This information
104  is referred as the data source connection information.
105 
106  A data source exposes the data contained in it as a collection of datasets.
107  The information about the data stored in a data source may be retrieved.
108 
109  Besides the descriptive information about the underlying data repository,
110  each data source also provides information about their capabilities.
111 
112  \ingroup dataaccess
113 
114  \sa DataSourceManager, DataSourceFactory, DataSet, DataSetType
115  */
116  class TEDATAACCESSEXPORT DataSource : public boost::noncopyable
117  {
118  public:
119 
120  /*! \brief Default constructor that can be called by subclasses. */
121  DataSource();
122 
123  /*! \brief Virtual destructor. */
124  virtual ~DataSource();
125 
126  /*!
127  \brief An identification value for the data source.
128 
129  \return The data source identification.
130  */
131  const std::string& getId() const;
132 
133  /*!
134  \brief It sets the data source identification.
135 
136  \param id An identification value.
137  */
138  void setId(const std::string& id);
139 
140  /** @name Data Source Basic Methods
141  * Basic Methods for operating a data source.
142  */
143  //@{
144 
145  /*!
146  \brief It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
147 
148  \return The data source type name. Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
149 
150  \note Each data source driver must have a unique name.
151 
152  \note Thread-safe!
153  */
154  virtual std::string getType() const = 0;
155 
156  /*!
157  \brief It returns the set of parameters used to set up the access channel to the underlying repository.
158 
159  This is the connection information used by a data source in order to enter in an operational mode,
160  when the open method is called.
161 
162  The key-value-pairs (kvp) may contain information about: maximum number of accepted connections,
163  user name and password required for establishing a connection, the url of a service, or any other
164  information needed by the data source to operate. This information is dependent on the data source
165  driver, so check the driver documentation for any additional information about the kvp.
166 
167  \return An associative container (key-value-pair) with information about the data source.
168 
169  \note Thread-safe!
170  */
171  virtual const std::map<std::string, std::string>& getConnectionInfo() const = 0;
172 
173  /*!
174  \brief It sets the connection information to be used when connecting to the data source.
175 
176  \param connInfo Key-value-pairs (kvp) with the connection information.
177  */
178  virtual void setConnectionInfo(const std::map<std::string, std::string>& connInfo) = 0;
179 
180  /*!
181  \brief It returns an object that can execute transactions in the context of a data source.
182 
183  Use this method to get an object that allows to retrieve a dataset, to insert or update data,
184  or to modify dataset types(schemas). You don't need to cache this kind of object because each
185  driver in TerraLib already keeps a connection pooling. So, as soon as you finish using
186  the transactor, destroy it.
187 
188  \return A pointer to an object that can execute transactions in the context of a data source.
189 
190  \exception Exception An exception can be thrown, if it is not possible to get a transactor;
191  for example, if there is no available connection.
192 
193  \note Thread-safe!
194  */
195  virtual std::auto_ptr<DataSourceTransactor> getTransactor() = 0;
196 
197  /*!
198  \brief It opens the data source and makes it ready for using.
199 
200  If the subclass needs to open a connection to a database server,
201  to open a file, or to get information from a Web Service, this
202  method can do this kind of job to prepare the data source to be
203  in an operational mode. It will use the connection information
204  provided by the setConnectionInfo methods.
205 
206  \exception Exception An exception can be thrown, if the data source cannot be opened.
207 
208  \note Not thread-safe!
209  */
210  virtual void open() = 0;
211 
212  /*!
213  \brief It closes the data source and clears all the resources used by its internal communication channel.
214 
215  This method closes any connection, any opened file, and releases any other resources.
216 
217  \exception Exception An exception can be thrown, if the data source cannot be closed.
218 
219  \note Not thread-safe!
220  */
221  virtual void close() = 0;
222 
223  /*!
224  \brief It returns true if the data source is opened, otherwise it returns false.
225 
226  This method will not check if the data source is available for using;
227  it will just answer if the data source has already been opened.
228  If you want to know if the data source is available for using,
229  check the isValid() method.
230 
231  \return It returns true if the data source is opened; otherwise, it returns false.
232 
233  \note Not thread-safe!
234  */
235  virtual bool isOpened() const = 0;
236 
237  /*!
238  \brief It checks if the data source is valid (available for using).
239 
240  For a DBMS, it will check the opened connections.
241  For a WFS client, it will check if the server is reachable.
242  For a file, it will check if it can be read.
243 
244  \return It returns true if the data source is available; otherwise, it returns false.
245 
246  \note Not thread-safe!
247  */
248  virtual bool isValid() const = 0;
249 
250  /*!
251  \brief It returns the known capabilities of the data source.
252 
253  The returned object has all the information about the operations the data source can perform.
254  Here you will find if the data source implementation supports primary keys,
255  foreign keys, if it can be used in a thread environment and much more information.
256 
257  \param capabilities The known capabilities of the data source.
258 
259  \note Thread-safe!
260  */
261  virtual const DataSourceCapabilities& getCapabilities() const = 0;
262 
263  /*!
264  \brief It returns the data source SQL dialect, if there is one.
265 
266  \return The data source SQL dialect.
267 
268  \note Thread-safe!
269  */
270  virtual const SQLDialect* getDialect() const = 0;
271  //@}
272 
273  /** @name Data Retrieval
274  * Methods for retrieving data from the data source.
275  */
276  //@{
277 
278  /*!
279  \brief It gets the dataset identified by the given name.
280  This method always returns a disconnected dataset, that is, a dataset that no more depends
281  of the data source that provided the connection for its creation. Therefore, the disconnected
282  dataset continues to live after the connection given by the data source has been released.
283 
284  \param name The dataset name.
285  \param accessPolicy Access policy.
286  \param travType The traverse type associated to the returned dataset.
287 
288  \exception Exception It can throw an exception if:
289  <ul>
290  <li>something goes wrong during the data retrieval</li>
291  <li>if the data source driver doesn't support the traversal type</li>
292  </ul>
293 
294  \note Thread-safe!
295  */
296  virtual std::auto_ptr<DataSet> getDataSet(const std::string& name,
298  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
299 
300  /*!
301  \brief It gets the dataset identified by the given name using a spatial filter over the specified property.
302  This method always returns a disconnected dataset, that is, a dataset that no more depends
303  of the data source that provided the connection for its creation. Therefore, the disconnected
304  dataset continues to live after the connection given by the data source has been released.
305 
306  \param name The dataset name.
307  \param propertyName The name of the spatial property used to apply the spatial filter.
308  \param e A rectangle used as a spatial filter when retrieving the dataset.
309  \param r The spatial relation used during the filtering.
310  \param accessPolicy Access policy.
311  \param travType The traversal type associated to the returned dataset.
312 
313  \exception Exception It can throw an exception if:
314  <ul>
315  <li>something goes wrong during the data retrieval</li>
316  <li>if the data source driver doesn't support the traversal type</li>
317  </ul>
318 
319  \note The envelope coordinates should be in the same coordinate system as the dataset.
320 
321  \note Thread-safe!
322  */
323  virtual std::auto_ptr<DataSet> getDataSet(const std::string& name,
324  const std::string& propertyName,
325  const te::gm::Envelope* e,
328  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
329 
330  /*!
331  \brief It gets the dataset identified by the given name using a spatial filter over the given geometric property.
332  This method always returns a disconnected dataset, that is, a dataset that no more depends
333  of the data source that provided the connection for its creation. Therefore, the disconnected
334  dataset continues to live after the connection given by the data source has been released.
335 
336  \param name The dataset name.
337  \param propertyName The name of the spatial property used to apply the spatial filter.
338  \param g The geometry used as a spatial filter when retrieving the dataset.
339  \param r The spatial relation used during the filtering.
340  \param accessPolicy Access policy.
341  \param travType The traverse type associated to the returned dataset.
342 
343  \note The geometry coordinates should be in the same coordinate system as the dataset.
344 
345  \note Thread-safe!
346  */
347  virtual std::auto_ptr<DataSet> getDataSet(const std::string& name,
348  const std::string& propertyName,
349  const te::gm::Geometry* g,
352  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
353 
354  /*!
355  \brief It gets the dataset identified by the given name using the identification of the objects.
356  This method always returns a disconnected dataset, that is, a dataset that no more depends
357  of the data source that provided the connection for its creation. Therefore, the disconnected
358  dataset continues to live after the connection given by the data source has been released.
359 
360  \param name The dataset name.
361  \param oids A pointer to the set of objects. Do not pass a null pointer nor an empty set.
362  \param accessPolicy Access policy.
363  \param travType The traverse type associated to the returned dataset.
364 
365  \exception Exception It can throw an exception if:
366  <ul>
367  <li>something goes wrong during data retrieval</li>
368  <li>if the data source driver doesn't support the traversal type</li>
369  </ul>
370 
371  \note Thread-safe!
372  */
373  std::auto_ptr<te::da::DataSet> getDataSet(const std::string& name,
374  const te::da::ObjectIdSet* oids,
376  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
377 
378  /*!
379  \brief It executes a query that may return some data using a generic query.
380  This method always returns a disconnected dataset, that is, a dataset that no more depends
381  of the data source that provided the connection for its creation. Therefore, the disconnected
382  dataset continues to live after the connection given by the data source has been released.
383 
384  This method is different of the method that accepts a dataset name and
385  a spatial filter; this method allows the retrieving of only a
386  subset of the attributes, since a query can include a property list.
387 
388  \param q A valid query object.
389  \param travType The traverse type associated to the returned dataset.
390  \param accessPolicy Access policy.
391 
392  \exception Exception It can throw an exception if:
393  <ul>
394  <li>something goes wrong during data retrieval</li>
395  <li>if the data source driver doesn't support the traversal type</li>
396  </ul>
397 
398  \note Thread-safe!
399  */
400  virtual std::auto_ptr<DataSet> query(const Select& q,
402  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
403 
404  /*!
405  \brief It executes a query that may return some data using the data source native language.
406  This method always returns a disconnected dataset, that is, a dataset that is no more dependent
407  of the data source that provided the connection for its creation. Therefore, the disconnected
408  dataset continues to live after the connection given by the data source has been released.
409 
410  \param query A query string in the data source native language.
411  \param travType The traverse type associated to the returned dataset.
412  \param accessPolicy Access policy.
413 
414  \exception Exception It can throw an exception if:
415  <ul>
416  <li>something goes wrong during data retrieval</li>
417  <li>if the data source driver doesn't support the traversal type</li>
418  </ul>
419 
420  \note Don't use this method, if you want portability for your application.
421  \note Thread-safe!
422  */
423  virtual std::auto_ptr<DataSet> query(const std::string& query,
425  const te::common::AccessPolicy accessPolicy = te::common::RAccess);
426  //@}
427 
428  /** @name Command Execution Methods
429  * Methods for executing commands against the data source.
430  */
431  //@{
432  /*!
433  \brief It executes the specified command using a generic query representation.
434 
435  \param command A query like: CREATE, DROP, ALTER, INSERT, UPDATE, DELETE.
436 
437  \exception Exception An exception can be thrown if the query cannot be performed.
438 
439  \note Thread-safe!
440  */
441  virtual void execute(const Query& command);
442 
443  /*!
444  \brief It executes the specified command in the data source native language.
445 
446  \param command A query string in the data source native language (like: CREATE, DROP, ALTER, INSERT, UPDATE, DELETE).
447 
448  \exception Exception An exception can be thrown if the query can not be performed.
449 
450  \note Thread-safe!
451  */
452  virtual void execute(const std::string& command);
453  //@}
454 
455  /** @name Auxiliary Methods for Commands and Queries
456  * Auxiliary methods for commands and queries.
457  */
458  //@{
459  /*!
460  \brief It escapes a string for using in commands and queries.
461 
462  \param value Any string.
463 
464  \return A valid escaped string.
465 
466  \note Thread-safe!
467  */
468  virtual std::string escape(const std::string& value);
469 
470  /*!
471  \brief It checks if the given dataset name is valid.
472 
473  \param datasetName A dataset name whose validity will be checked.
474 
475  \return True, if the name is valid according to the data source rules.
476 
477  \note Thread-safe!
478  */
479  virtual bool isDataSetNameValid(const std::string& datasetName);
480 
481  /*!
482  \brief It checks if the given property name is valid.
483 
484  \param propertyName A property name whose validity will be checked.
485 
486  \return True, if the name is valid according to the data source rules.
487 
488  \note Thread-safe!
489  */
490  virtual bool isPropertyNameValid(const std::string& propertyName);
491  //@}
492 
493  /** @name Dataset Metadata Retrieval
494  * Methods for retrieving metadata about the datasets of the data source.
495  */
496  //@{
497  /*!
498  \brief It gets the dataset names available in the data source.
499 
500  \return The dataset names available in the data source.
501 
502  \exception Exception An exception can be thrown, if the dataset names could not be retrieved.
503 
504  \note Each dataset in the data source must have a unique name. For example, in a DBMS the name
505  may contain the schema name before the table name separated by a dot notation (".").
506 
507  \note Thread-safe!
508  */
509  virtual std::vector<std::string> getDataSetNames();
510 
511  /*!
512  \brief It retrieves the number of data sets available in the data source.
513 
514  \exception Exception An exception can be thrown, if the number of datasets could not be retrieved.
515 
516  \return The number of data sets available in the data source.
517 
518  \note Thread-safe!
519  */
520  virtual std::size_t getNumberOfDataSets();
521 
522  /*!
523  \brief It gets information about the given dataset.
524 
525  This method can provide the following information about a dataset:
526  <ul>
527  <li>the list of properties, including: name, data type, size, if the value is required or not, if it is an autoincrement</li>
528  <li>primary key</li>
529  <li>foreign keys</li>
530  <li>unique keys</li>
531  <li>check constraints</li>
532  <li>indexes</li>
533  </ul>
534 
535  \param name The name of the dataset we are looking information for.
536 
537  \exception Exception An exception can be thrown, if the information about the dataset could not be retrieved.
538 
539  \return The dataset schema.
540 
541  \note Thread-safe!
542  */
543  virtual std::auto_ptr<te::da::DataSetType> getDataSetType(const std::string& name);
544 
545  /*!
546  \brief It retrieves the properties of the dataset.
547 
548  \param datasetName The dataset name.
549 
550  \exception Exception An exception can be thrown, if the dataset properties could not be retrieved.
551 
552  \return The dataset properties.
553 
554  \note Thread-safe!
555  */
556  virtual boost::ptr_vector<te::dt::Property> getProperties(const std::string& datasetName);
557 
558  /*!
559  \brief It retrieves the property with the given name from the dataset.
560 
561  \param datasetName The dataset name.
562  \param propertyName The property name.
563 
564  \exception Exception An exception can be thrown, if the dataset property could not be retrieved.
565 
566  \return The property with the given name from the dataset.
567 
568  \note Thread-safe!
569  */
570  virtual std::auto_ptr<te::dt::Property> getProperty(const std::string& datasetName, const std::string& name);
571 
572  /*!
573  \brief It retrieves the property lying in the given position from the dataset.
574 
575  \param datasetName The dataset name.
576  \param propertyPos The property position.
577 
578  \exception Exception An exception can be thrown, if the property lying in the given position could not be retrieved.
579 
580  \return The property in the given position.
581 
582  \note Thread-safe!
583  */
584  virtual std::auto_ptr<te::dt::Property> getProperty(const std::string& datasetName, std::size_t propertyPos);
585 
586  /*!
587  \brief It gets the property names of the given dataset.
588 
589  \param datasetName The dataset name.
590 
591  \exception Exception An exception can be thrown, if the property names of the dataset could not be retrieved.
592 
593  \return The property names of the dataset.
594 
595  \note Each dataset in the data source must have a unique name. For example, in a DBMS the name
596  may contain the schema name before the table name separated by a dot notation (".").
597 
598  \note Thread-safe!
599  */
600  virtual std::vector<std::string> getPropertyNames(const std::string& datasetName);
601 
602  /*!
603  \brief It gets the number of properties of the given dataset.
604 
605  \param datasetName The dataset name.
606 
607  \exception Exception An exception can be thrown, if the number of dataset properties could not be retrieved.
608 
609  \return The number of dataset properties.
610 
611  \note Thread-safe!
612  */
613  virtual std::size_t getNumberOfProperties(const std::string& datasetName);
614 
615  /*!
616  \brief It checks if a property with the given name exists in the dataset.
617 
618  \param datasetName The dataset name.
619  \param name The property name.
620 
621  \exception Exception An exception can be thrown, if the existence of the dataset property could not be obtained.
622 
623  \return True, if the property exists in the dataset; otherwise, it returns false.
624 
625  \note Thread-safe!
626  */
627  virtual bool propertyExists(const std::string& datasetName, const std::string& name);
628 
629  /*!
630  \brief It adds a new property to the dataset schema.
631 
632  \param datasetName The dataset where the property will be added.
633  \param p The new property to be added.
634 
635  \exception Exception An exception can be thrown, if the property could not be added to the dataset schema.
636 
637  \note Don't delete the given property, because the schema will take the ownership of it.
638  \note Thread-safe!
639  */
640  virtual void addProperty(const std::string& datasetName, te::dt::Property* p);
641 
642  /*!
643  \brief It removes a property from the given dataset.
644 
645  \param datasetName The dataset from where the given property will be removed.
646  \param name The property to be removed from the dataset.
647 
648  \exception Exception An exception can be thrown, if the dataset property could not be removed.
649 
650  \note Thread-safe!
651  */
652  virtual void dropProperty(const std::string& datasetName, const std::string& name);
653 
654  /*!
655  \brief It renames a property of the given dataset.
656 
657  \param datasetName The dataset containing the property to be renamed.
658  \param propertyName The property to be renamed from the dataset.
659  \param newPropertyName The new property name.
660 
661  \exception Exception An exception can be thrown, if the dataset property could not be renamed.
662 
663  \note Thread-safe!
664  */
665  virtual void renameProperty(const std::string& datasetName,
666  const std::string& propertyName,
667  const std::string& newPropertyName);
668 
669  /*!
670  \brief It retrieves the primary key of the dataset.
671 
672  \param datasetName The dataset name.
673 
674  \exception Exception An exception can be thrown, if the primary key could not be retrieved.
675 
676  \return If a primary key exists in the dataset, it is returned; otherwise, a NULL is returned.
677 
678  \note Thread-safe!
679  */
680  virtual std::auto_ptr<te::da::PrimaryKey> getPrimaryKey(const std::string& datasetName);
681 
682  /*!
683  \brief It checks if a primary key exists in the dataset.
684 
685  \param datasetName The dataset name.
686  \param name The primary key name.
687 
688  \exception Exception An exception can be thrown, if the existence of the primary key could not be determined.
689 
690  \return True, if a primary key exists in the dataset; otherwise, it returns false.
691 
692  \note Thread-safe!
693  */
694  virtual bool primaryKeyExists(const std::string& datasetName, const std::string& name);
695 
696  /*!
697  \brief It adds a primary key constraint to the dataset schema.
698 
699  \param datasetName The name of the dataset where the primary key will be added.
700  \param pk The primary key constraint.
701 
702  \exception Exception An exception can be thrown, if the primary key could not be added to the dataset schema.
703 
704  \note Don't delete the given primary key, because the schema will take the ownership of it.
705  \note Thread-safe!
706  */
707  virtual void addPrimaryKey(const std::string& datasetName, PrimaryKey* pk);
708 
709  /*!
710  \brief It removes the primary key constraint from the dataset schema.
711 
712  \param datasetName The dataset from where the primary key will be removed.
713 
714  \exception Exception An exception can be thrown, if the primary key could not be dropped from the dataset schema.
715 
716  \note Thread-safe!
717  */
718  virtual void dropPrimaryKey(const std::string& datasetName);
719 
720  /*!
721  \brief It retrieves the foreign key from the given dataset.
722 
723  \param datasetName The dataset name.
724  \param name The foreign key name.
725 
726  \exception Exception An exception can be thrown, if the foreign key could not be retrieved.
727 
728  \return If the foreign key exists in the dataset, it is returned; otherwise, a NULL is returned.
729 
730  \note Thread-safe!
731  */
732  virtual std::auto_ptr<ForeignKey> getForeignKey(const std::string& datasetName, const std::string& name);
733 
734  /*!
735  \brief It gets the foreign key names of the given dataset.
736 
737  \param datasetName The dataset name.
738 
739  \exception Exception An exception can be thrown, if the foreign key names could not be retrieved.
740 
741  \return The foreign key names of the dataset.
742 
743  \note Thread-safe!
744  */
745  virtual std::vector<std::string> getForeignKeyNames(const std::string& datasetName);
746 
747  /*!
748  \brief It checks if a foreign key with the given name exists in the data source.
749 
750  \param datasetName The dataset name.
751  \param name The foreign key name.
752 
753  \exception Exception An exception can be thrown, if the existence of the foreign key could not be obtained.
754 
755  \return True, if the foreign key exists in the dataset; otherwise, it returns false.
756  */
757  virtual bool foreignKeyExists(const std::string& datasetName, const std::string& name);
758 
759  /*!
760  \brief It adds a foreign key constraint to a dataset.
761 
762  \param datasetName The dataset where the foreign key constraint will be added.
763  \param fk The foreign key constraint.
764 
765  \exception Exception An exception can be thrown, if the foreign key could not be added to the dataset schema.
766 
767  \note Don't delete the given foreign key, because the schema will take the ownership of it.
768  \note Thread-safe!
769  */
770  virtual void addForeignKey(const std::string& datasetName, ForeignKey* fk);
771 
772  /*!
773  \brief It removes the foreign key constraint from the dataset schema.
774 
775  \param datasetName The dataset where the foreign key will be removed.
776  \param fkName The foreign key to be removed.
777 
778  \exception Exception An exception can be thrown, if the foreign key could not be removed from the dataset schema.
779 
780  \note Thread-safe!
781  */
782  virtual void dropForeignKey(const std::string& datasetName, const std::string& fkName);
783 
784  /*!
785  \brief It gets the unique key in the dataset with the given name.
786 
787  \param datasetName The dataset name.
788  \param name The unique key name.
789 
790  \exception Exception An exception can be thrown, if the unique key could not be retrieved.
791 
792  \return The unique key with the given name in the dataset.
793 
794  \note Thread-safe!
795  */
796  virtual std::auto_ptr<te::da::UniqueKey> getUniqueKey(const std::string& datasetName, const std::string& name);
797 
798  /*!
799  \brief It gets the unique key names of the given dataset.
800 
801  \param datasetName The dataset name.
802 
803  \exception Exception An exception can be thrown, if the unique key names could not be obtained.
804 
805  \return The unique key names of the dataset.
806 
807  \note Thread-safe!
808  */
809  virtual std::vector<std::string> getUniqueKeyNames(const std::string& datasetName);
810 
811  /*!
812  \brief It checks if a unique key with the given name exists in the dataset.
813 
814  \param datasetName The dataset name.
815  \param name The unique key name.
816 
817  \exception Exception An exception can be thrown, if the existence of the unique key could not be determined.
818 
819  \return True, if the unique key exists in the dataset; otherwise, it returns false.
820 
821  \note Thread-safe!
822  */
823  virtual bool uniqueKeyExists(const std::string& datasetName, const std::string& name);
824 
825  /*!
826  \brief It adds a unique key constraint to the dataset.
827 
828  \param datasetName The dataset where the unique key will be added.
829  \param uk The unique key constraint.
830 
831  \exception Exception An exception can be thrown, if the unique key could not be added to the dataset schema.
832 
833  \note Don't delete the given unique key, because the schema will take the ownership of it.
834  \note Thread-safe!
835  */
836  virtual void addUniqueKey(const std::string& datasetName, UniqueKey* uk);
837 
838  /*!
839  \brief It removes the unique key constraint from the dataset.
840 
841  \param datasetName The dataset from where the unique key will be removed.
842  \param name The unique key constraint name.
843 
844  \exception Exception An exception can be thrown, if the unique key could not be removed from the dataset schema.
845 
846  \note Thread-safe!
847  */
848  virtual void dropUniqueKey(const std::string& datasetName, const std::string& name);
849 
850  /*!
851  \brief It gets the check constraint of the dataset with the given name.
852 
853  \param datasetName The dataset name.
854  \param name The check constraint name.
855 
856  \exception Exception An exception can be thrown, if the check constraint could not be retrieved.
857 
858  \return The check constraint with the given name.
859 
860  \note Thread-safe!
861  */
862  virtual std::auto_ptr<te::da::CheckConstraint> getCheckConstraint(const std::string& datasetName, const std::string& name);
863 
864  /*!
865  \brief It gets the check constraint names of the given dataset.
866 
867  \param datasetName The dataset name.
868 
869  \exception Exception An exception can be thrown, if the check constraint names could not be retrieved.
870 
871  \return The check constraint names of the dataset.
872 
873  \note Thread-safe!
874  */
875  virtual std::vector<std::string> getCheckConstraintNames(const std::string& datasetName);
876 
877  /*!
878  \brief It checks if a check-constraint with the given name exists in the data source.
879 
880  \param datasetName The dataset name.
881  \param name The check-constraint name.
882 
883  \exception Exception An exception can be thrown, if the existence of the check constraint could not be determined.
884 
885  \return True, if the check-constraint exists in the dataset; otherwise, it returns false.
886 
887  \note Thread-safe!
888  */
889  virtual bool checkConstraintExists(const std::string& datasetName, const std::string& name);
890 
891  /*!
892  \brief It adds a check constraint to the dataset.
893 
894  \param datasetName The dataset where the constraint will be added.
895  \param cc The check constraint to be added.
896 
897  \exception Exception An exception can be thrown, if the check constraint could not be added to the dataset schema.
898 
899  \note Don't delete the given check constraint, because the schema will take the ownership of it.
900  \note Thread-safe!
901  */
902  virtual void addCheckConstraint(const std::string& datasetName, CheckConstraint* cc);
903 
904  /*!
905  \brief It removes the check constraint from the dataset.
906 
907  \param datasetName The dataset from where the check constraint will be removed.
908  \param name The check constraint to be removed.
909 
910  \exception Exception An exception can be thrown, if the check constraint could not be removed from the dataset schema.
911 
912  \note Thread-safe!
913  */
914  virtual void dropCheckConstraint(const std::string& datasetName, const std::string& name);
915 
916  /*!
917  \brief It gets the index with the given name from the dataset.
918 
919  \param datasetName The dataset name.
920  \param name The index name.
921 
922  \exception Exception An exception can be thrown, if the index could not be retrieved.
923 
924  \return The index from the given dataset.
925 
926  \note Thread-safe!
927  */
928  virtual std::auto_ptr<te::da::Index> getIndex(const std::string& datasetName, const std::string& name);
929 
930  /*!
931  \brief It gets the index names of the given dataset.
932 
933  \param datasetName The dataset name.
934 
935  \exception Exception An exception can be thrown, if the index names could not be retrieved.
936 
937  \return The index names of the given dataset.
938 
939  \note Thread-safe!
940  */
941  virtual std::vector<std::string> getIndexNames(const std::string& datasetName);
942 
943  /*!
944  \brief It checks if an index with the given name exists in the dataset.
945 
946  \param datasetName The dataset name.
947  \param name The index name.
948 
949  \exception Exception An exception can be thrown, if the index existence could not be determined.
950 
951  \return True, if the index exists in the dataset; otherwise, it returns false.
952 
953  \note Thread-safe!
954  */
955  virtual bool indexExists(const std::string& datasetName, const std::string& name);
956 
957  /*!
958  \brief It adds an index to the dataset.
959 
960  \param datasetName The dataset where the index will be added.
961  \param idx The index to be added.
962  \param options A list of optional modifiers (driver specific).
963 
964  \exception Exception An exception can be thrown, if the index could not be added to the dataset schema.
965 
966  \note Don't delete the given index, because the schema will take the ownership of it.
967  \note Thread-safe!
968  */
969  virtual void addIndex(const std::string& datasetName, Index* idx,
970  const std::map<std::string, std::string>& options);
971 
972  /*!
973  \brief It removes the index from the given dataset.
974 
975  \param datasetName The dataset where the index will be removed.
976  \param idxName The index to be removed.
977 
978  \exception Exception An exception can be thrown, if the index could not be removed from the dataset schema.
979 
980  \note Thread-safe!
981  */
982  virtual void dropIndex(const std::string& datasetName, const std::string& idxName);
983 
984  /*!
985  \brief It gets the sequence with the given name in the data source.
986 
987  \param name The sequence name.
988 
989  \exception Exception An exception can be thrown, if the sequence could not be retrieved from the data source.
990 
991  \return The sequence with the given name.
992 
993  \note Thread-safe!
994  */
995  virtual std::auto_ptr<Sequence> getSequence(const std::string& name);
996 
997  /*!
998  \brief It gets the sequence names available in the data source.
999 
1000  \note Each sequence in the data source must have a unique name. For example, in a DBMS the name
1001  may contain the schema name before the sequence name separated by a dot notation (".").
1002 
1003  \exception Exception An exception can be thrown, if the sequence names could not be retrieved.
1004 
1005  \return The sequence names of the data source.
1006 
1007  \note Thread-safe!
1008  */
1009  virtual std::vector<std::string> getSequenceNames();
1010 
1011  /*!
1012  \brief It checks if a sequence with the given name exists in the data source.
1013 
1014  \param name The sequence name.
1015 
1016  \exception Exception An exception can be thrown, if the index existence could not be determined.
1017 
1018  \return True, if the sequence exists in the data source; otherwise, it returns false.
1019 
1020  \note Thread-safe!
1021  */
1022  virtual bool sequenceExists(const std::string& name);
1023 
1024  /*!
1025  \brief It adds a new sequence in the data source.
1026 
1027  \exception Exception An exception can be thrown, if the sequence could not be added to the data source.
1028 
1029  \note Don't delete the given sequence, because the data source will take the ownership of it.
1030  \note Thread-safe!
1031  */
1032  virtual void addSequence(Sequence* sequence);
1033 
1034  /*!
1035  \brief It removes the sequence from the data source.
1036 
1037  \param name The sequence that will be removed.
1038 
1039  \exception Exception An exception can be thrown, if the sequence could not be removed from the data source.
1040 
1041  \note Thread-safe!
1042  */
1043  virtual void dropSequence(const std::string& name);
1044 
1045  /*!
1046  \brief It retrieves the bounding rectangle of the spatial property for the given dataset.
1047 
1048  \param datasetName The dataset name.
1049  \param propertyName The spatial property name.
1050 
1051  \exception Exception An exception can be thrown, if the extent of the geometry property could not be retrieved.
1052 
1053  \return The spatial property bounding rectangle, or NULL, if none can be retrieved.
1054 
1055  \note Thread-safe!
1056  */
1057  virtual std::auto_ptr<te::gm::Envelope> getExtent(const std::string& datasetName,
1058  const std::string& propertyName);
1059 
1060  /*!
1061  \brief It retrieves the bounding rectangle for the spatial property lying in the given position in the dataset.
1062 
1063  \param datasetName The dataset name.
1064  \param propertyPos The spatial property position.
1065 
1066  \exception Exception An exception can be thrown, if the extent of the geometry property lying in the given position could not be retrieved.
1067 
1068  \return The spatial property bounding rectangle, or NULL, if none can be retrieved.
1069 
1070  \note Thread-safe!
1071  */
1072  virtual std::auto_ptr<te::gm::Envelope> getExtent(const std::string& datasetName, std::size_t propertyPos);
1073 
1074  /*!
1075  \brief It retrieves the number of items of the given dataset.
1076 
1077  \param datasetName The dataset name.
1078 
1079  \exception Exception An exception can be thrown, if the number of items of the dataset could not be retrieved.
1080 
1081  \return The number of items of the given dataset.
1082 
1083  \note Thread-safe!
1084  */
1085  virtual std::size_t getNumberOfItems(const std::string& datasetName);
1086 
1087  /*!
1088  \brief It checks if the data source has any dataset.
1089 
1090  \exception Exception An exception can be thrown, if it is not possible to check if the data source has datasets .
1091 
1092  \return True, if the data source has datasets; otherwise, it returns false.
1093 
1094  \note Thread-safe!
1095  */
1096  virtual bool hasDataSets();
1097 
1098  /*!
1099  \brief It checks if a dataset with the given name exists in the data source.
1100 
1101  \param name The dataset name.
1102 
1103  \exception Exception An exception can be thrown, if the existence of a dataset in the data source could not be determined.
1104 
1105  \return True, if the dataset exists in the data source; otherwise, it returns false.
1106 
1107  \note Thread-safe!
1108  */
1109  virtual bool dataSetExists(const std::string& name);
1110  //@}
1111 
1112  /** @name Dataset Schema Persistence Methods
1113  * Methods for dealing with datasource and dataset schema changes.
1114  */
1115  //@{
1116 
1117  /*!
1118  \brief It creates the dataset schema definition in the target data source.
1119 
1120  If a dataset schema with the same name already exists in the target data source,
1121  this may throw an exception.
1122 
1123  After calling this method, the dataset schema may be updated.
1124 
1125  \param dt The dataset schema to be created. It may be changed during the operation.
1126  \param options A list of optional modifiers (driver specific).
1127 
1128  \pre The schema of a related dataset in a foreign key must be already in the data source.
1129 
1130  \post In some data sources, this method may output implicit indexes, sequences or constraints.
1131  The method, if necessary, will create and adjust the dataset schema.
1132 
1133  \post The caller of this method will take the ownership of the given schema.
1134 
1135  \note If you want to create a new schema based on an already existing one,
1136  you must create a fresh copy of the DataSetType with the clone() method.
1137 
1138  \note Thread-safe!
1139  */
1140  virtual void createDataSet(DataSetType* dt, const std::map<std::string, std::string>& options);
1141 
1142  /*!
1143  \brief It clones the dataset in the data source.
1144 
1145  \param name The dataset to be cloned.
1146  \param cloneName The name of the cloned dataset.
1147  \param options A list of optional modifiers. It is driver specific.
1148 
1149  \exception Exception An exception can be thrown, if the dataset schema could not be cloned.
1150 
1151  \note Thread-safe!
1152  */
1153  virtual void cloneDataSet(const std::string& name,
1154  const std::string& cloneName,
1155  const std::map<std::string, std::string>& options);
1156 
1157  /*!
1158  \brief It removes the dataset schema from the data source.
1159 
1160  \param name The dataset name whose schema will be removed from the data source.
1161 
1162  \exception Exception An exception can be thrown, if the dataset could not be removed from the data source.
1163 
1164  \note Thread-safe!
1165  */
1166  virtual void dropDataSet(const std::string& name);
1167 
1168  /*!
1169  \brief It renames a dataset.
1170 
1171  \param name The name of the dataset to be renamed.
1172  \param newName The new dataset name.
1173 
1174  \exception Exception An exception can be thrown, if the dataset could not be renamed.
1175 
1176  \note Thread-safe!
1177  */
1178  virtual void renameDataSet(const std::string& name, const std::string& newName);
1179  //@}
1180 
1181  /** @name Dataset Persistence Methods
1182  * Methods for dealing with the persistence of data in a data source.
1183  */
1184  //@{
1185 
1186  /*!
1187  \brief It adds data items to the dataset in the data source.
1188 
1189  \param datasetName The target dataset name.
1190  \param d The data items to be added to the dataset.
1191  \param options A list of optional modifiers (driver specific).
1192  \param limit The number of items to be used from the input dataset. If set to 0 (default), all items are used.
1193 
1194  \exception Exception An exception can be thrown, if the input dataset items could not be added to the given dataset.
1195 
1196  \note The dataset reading will start in the current position.
1197  So, keep in mind that it is the caller responsability
1198  to inform the right position(and a valid one) in the dataset 'd', to start the inserting process.
1199 
1200  \note Thread-safe!
1201  */
1202  virtual void add(const std::string& datasetName,
1203  DataSet* d,
1204  const std::map<std::string, std::string>& options,
1205  std::size_t limit = 0);
1206 
1207  /*!
1208  \brief It removes all the informed items from the dataset.
1209 
1210  It removes all the data items from a dataset which are identified by
1211  a set of object identifiers. If this set is not informed, all items will be removed.
1212 
1213  \param datasetName The dataset name.
1214  \param oids A set of object identifiers used used to remove data from the dataset, or NULL, for removing all.
1215 
1216  \exception Exception An exception can be thrown, if the data items could not be removed.
1217 
1218  \note Thread-safe!
1219  */
1220  virtual void remove(const std::string& datasetName, const te::da::ObjectIdSet* oids = 0);
1221 
1222  /*!
1223  \brief It updates the contents of a dataset for the set of data items.
1224 
1225  \param datasetName The target dataset name.
1226  \param dataset The list of data items to be updated.
1227  \param properties The list of properties of the dataset to be updated.
1228  \param oids The list of objects to be updated.
1229  \param options A list of optional modifiers. It is driver specific.
1230  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
1231 
1232  \exception Exception An exception can be thrown, if the dataset could not be updated.
1233 
1234  \note The dataset reading will start in the
1235  current position. So, keep in mind that it is the caller responsability
1236  to inform the right position(and a valid one) for the dataset 'd', to start the updating process.
1237 
1238  \note Thread-safe!
1239  */
1240  virtual void update(const std::string& datasetName,
1241  DataSet* dataset,
1242  const std::vector<std::size_t>& properties,
1243  const te::da::ObjectIdSet* oids,
1244  const std::map<std::string, std::string>& options,
1245  std::size_t limit = 0);
1246  //@}
1247 
1248  /** @name Data Source static methods
1249  * Data Source static methods
1250  */
1251  //@{
1252 
1253  /*!
1254  \brief It creates a new repository for a data source.
1255 
1256  \param dsType The type of data source to be created (example: POSTGIS, ORACLE, SQLITE).
1257  \param dsInfo The information for creating a new data source.
1258 
1259  \exception Exception An exception can be thrown, if the data source could not be created.
1260 
1261  \return A data source for the new data repository.
1262 
1263  \note Thread-safe!
1264  */
1265  static std::auto_ptr<DataSource> create(const std::string& dsType, const std::map<std::string, std::string>& dsInfo);
1266 
1267  /*!
1268  \brief It removes a data source identified by its connection information and the driver type.
1269 
1270  \param dsType The data source type name (example: POSTGIS, ORACLE, SQLITE).
1271  \param dsInfo The connection information for removing the data source.
1272 
1273  \exception Exception An exception can be thrown, if the data source could not be removed.
1274 
1275  \note No other instance of the data source to be removed can be opened, when calling this method.
1276 
1277  \note Thread-safe!
1278  */
1279  static void drop(const std::string& dsType, const std::map<std::string, std::string>& dsInfo);
1280 
1281  /*!
1282  \brief It checks if the data source exists with the connection information and the driver type.
1283 
1284  \param dsType The data source type name (example: POSTGIS, ORACLE, SQLITE).
1285  \param dsInfo The data source information.
1286 
1287  \exception Exception An exception can be thrown, if the data source exists in the driver.
1288 
1289  \return True, if the data source exists; otherwise, it returns false.
1290 
1291  \note Thread-safe!
1292  */
1293  static bool exists(const std::string& dsType, const std::map<std::string, std::string>& dsInfo);
1294 
1295  /*!
1296  \brief It returns the data source names available in the driver.
1297 
1298  \param dsType The type name of the data source(example: PostGIS, Oracle, WFS).
1299  \param dsInfo The information about the data sources.
1300 
1301  \exception Exception An exception can be thrown, if the data source names could not be retrieved.
1302 
1303  \return The data source names available.
1304 
1305  \exception Exception An exception can be thrown, if the list of data source names could not be retrieved.
1306  */
1307  static std::vector<std::string> getDataSourceNames(const std::string& dsType, const std::map<std::string, std::string>& info);
1308 
1309  /*!
1310  \brief It gets the encoding names of the data source.
1311 
1312  \param dsType The data source type name (example: PostGIS, Oracle, WFS).
1313  \param dsInfo The data source information.
1314 
1315  \exception Exception An exception can be thrown, if the encoding names could not be retrieved.
1316 
1317  \return The encoding names of the data source.
1318  */
1319  static std::vector<std::string> getEncodings(const std::string& dsType, const std::map<std::string, std::string>& info);
1320  //@}
1321 
1322  protected:
1323 
1324  /** @name Protected Data Source Methods????
1325  * The protected methods of the data source
1326  */
1327  //@{
1328 
1329  /*!
1330  \brief It creates a new data source.
1331 
1332  \param dsInfo The information for creating a new data source.
1333 
1334  \exception Exception An exception can be thrown, if the data source could not be created.
1335 
1336  \note Not thread-safe!
1337  */
1338  virtual void create(const std::map<std::string, std::string>& dsInfo) = 0;
1339 
1340  /*!
1341  \brief It removes the data source with the connection information from a driver.
1342 
1343  \param dsInfo The information for removing a data source from a driver.
1344 
1345  \exception Exception An exception can be thrown, if the data source could not be removed.
1346 
1347  \note Not thread-safe!
1348  */
1349  virtual void drop(const std::map<std::string, std::string>& dsInfo) = 0;
1350 
1351  /*!
1352  \brief Check the existence of a data source in a driver.
1353 
1354  \param dsInfo The data source information.
1355 
1356  \exception Exception An exception can be thrown, if the existence of a data source in a driver could not be determined.
1357 
1358  \return True, if the data source exists, or false, otherwise.
1359 
1360  \note Thread-safe!
1361  */
1362  virtual bool exists(const std::map<std::string, std::string>& dsInfo) = 0;
1363 
1364  /*!
1365  \brief It gets the data source names available in a driver.
1366 
1367  \param dsInfo The data source information.
1368 
1369  \exception Exception An exception can be thrown, if the data source names could not be retrieved.
1370 
1371  \return The data source names available in the driver.
1372 
1373  \note Not thread-safe!
1374  */
1375  virtual std::vector<std::string> getDataSourceNames(const std::map<std::string, std::string>& dsInfo) = 0;
1376 
1377  /*!
1378  \brief It gets the encodings for the data source.
1379 
1380  \param dsInfo The data source information.
1381 
1382  \exception Exception An exception can be thrown, if the encoding names could not be retrieved.
1383 
1384  \return The encoding names for the data source.
1385 
1386  */
1387  virtual std::vector<std::string> getEncodings(const std::map<std::string, std::string>& dsInfo) = 0;
1388  //@}
1389 
1390  protected:
1391 
1392  std::string m_id; //!< The data source identification.
1393  };
1394 
1395  typedef boost::shared_ptr<DataSource> DataSourcePtr;
1396 
1397  } // end namespace da
1398 } // end namespace te
1399 
1400 #endif // __TERRALIB_DATAACCESS_DATASOURCE_INTERNAL_DATASOURCE_H
It describes an index associated to a DataSetType.
Definition: Index.h:54
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
It describes a sequence (a number generator).
Definition: Sequence.h:56
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:135
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
A Query is independent from the data source language/dialect.
Definition: Query.h:46
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
TraverseType
A dataset can be traversed in two ways:
Definition: Enums.h:53
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:116
A class that models the description of a dataset.
Definition: DataSetType.h:72
It models a property definition.
Definition: Property.h:59
std::string m_id
The data source identification.
Definition: DataSource.h:1392
A class that describes a check constraint.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:111