DataSetType.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 terralib/dataaccess/dataset/DataSetType.h
22 
23  \brief A class that models the description of a dataset.
24 */
25 
26 #ifndef __TERRALIB_DATAACCESS_INTERNAL_DATASETTYPE_H
27 #define __TERRALIB_DATAACCESS_INTERNAL_DATASETTYPE_H
28 
29 // TerraLib
30 #include "../../datatype/CompositeProperty.h"
31 #include "../Config.h"
32 
33 // STL
34 #include <string>
35 
36 // Boost
37 #include <boost/shared_ptr.hpp>
38 
39 namespace te
40 {
41 // Forward declarations
42  namespace da
43  {
44  class CheckConstraint;
45  class Constraint;
46  class DataSourceCatalog;
47  class ForeignKey;
48  class Index;
49  class PrimaryKey;
50  class Sequence;
51  class UniqueKey;
52 
53  /*!
54  \class DataSetType
55 
56  \brief A class that models the description of a dataset.
57 
58  A DataSetType may be used to describe the dataset schema.
59  It lists the attributes of the dataset, including their names and types.
60 
61  The information about the properties (or attributes) of datasets are stored in instances of the
62  Property class. To a property it is associated a name, the data type
63  and other metadata necessary
64  to fully specify the type, such as the attribute size.
65 
66  \note This class sometimes are referred to as dataset schemas.
67 
68  \ingroup dataaccess
69 
70  \sa DataSet, te::dt::Property, te::dt::CompositeProperty, DataSourceCatalog
71  */
73  {
74  public:
75 
76  using CompositeProperty::add;
77  using CompositeProperty::getPropertyPosition;
78 
79  /** @name DataSetType Basic Methods
80  * Basic methods for a DataSetType.
81  */
82  //@{
83 
84  /*!
85  \brief It creates a new DataSetType not associated to any DataSetType or Catalog.
86 
87  By default the DataSetType doesn't have a geometric property nor a default geometry property.
88 
89  \param name The DataSetType name.
90  \param id The DataSetType identifier.
91 
92  \warning The identifier value (id) may be used by data source implementations. So, don't rely on its value!
93  */
94  DataSetType(const std::string& name, unsigned int id = 0);
95 
96  /*!
97  \brief Copy constructor.
98 
99  \param rhs The right-hand-side copy that would be used to copy from.
100 
101  \note This method is used by clone().
102  */
103  DataSetType(const DataSetType& rhs);
104 
105  /*! Destructor */
106  ~DataSetType();
107 
108  /*!
109  \brief Assignment operator not allowed.
110 
111  \param rhs The right-hand-side copy that would be used to copy from.
112 
113  \return A reference to this object.
114  */
115  DataSetType& operator=(const DataSetType& rhs);
116 
117 
118  //@}
119 
120  /** @name General Descriptive Methods
121  * Methods for obtainning information about a given dataset.
122  */
123  //@{
124 
125  /*!
126  \brief A human descriptive title for the DataSetType.
127 
128  \return This value may be a concatenation of the DataSetType name or a more human readable text.
129  */
130  const std::string& getTitle() const { return m_title; }
131 
132  /*!
133  \brief It sets a human descriptive title for the DataSetType.
134 
135  \param title This value can be used to display a human descriptive title for the DataSetType.
136  */
137  void setTitle(const std::string& title) { m_title = title; }
138 
139  /*!
140  \brief It returns true if the DataSetType has at least one geometry property; otherwise, it returns false.
141 
142  \return True if the DataSetType has at least one geometry property; otherwise, it returns false.
143  */
144  bool hasGeom() const;
145 
146  /*!
147  \brief It returns true if the DataSetType has at least one raster property; otherwise, it returns false.
148 
149  \return True if the DataSetType has at least one raster property; otherwise, it returns false.
150  */
151  bool hasRaster() const;
152 
153  /*!
154  \brief It returns the catalog that owns the DataSetType.
155 
156  \return The catalog associated to the DataSetType.
157  */
158  DataSourceCatalog* getCatalog() const { return m_catalog; }
159 
160  /*!
161  \brief It sets the DataSetType associated catalog.
162 
163  \param catalog The catalog associated to the DataSetType.
164 
165  \warning Take care when calling this method. If the DataSetType belongs to a DataSourceCatalog
166  remember to detach it from the catalog before calling this method.
167  */
168  void setCatalog(DataSourceCatalog* catalog) { m_catalog = catalog; }
169 
170  /*!
171  \brief It returns the dataset category.
172 
173  Some drivers and applications can use this value to refine their functionalities.
174 
175  \return The dataset category.
176  */
177  int getCategory() const { return m_category; }
178 
179  /*!
180  \brief It sets the dataset category.
181 
182  \param c The dataset category.
183  */
184  void setCategory(int c) { m_category = c; }
185 
186  //@}
187 
188  /** @name Primary Key Methods
189  * Methods for managing Primary Key
190  */
191  //@{
192 
193  /*!
194  \brief It sets the primary key constraint.
195 
196  \param pk The primary key constraint.
197 
198  \pre The pk must not be associated to another DataSetType.
199 
200  \post The DataSetType will take the ownership of the primary key.
201  \post The primary key will be automatically attached to the DataSetType.
202 
203  \note If another primary key has been already set, it will destroy it and set the new one as the valid primary key.
204 
205  \note If you inform a NULL pointer, it ill just release the internal primary key.
206  */
207  void setPrimaryKey(PrimaryKey* pk);
208 
209  /*!
210  \brief It returns the primary key associated to the dataset type.
211 
212  \rerturn The primary key associated to the dataset type or NULL if none exists.
213  */
214  PrimaryKey* getPrimaryKey() const { return m_pk; }
215 
216  //@}
217 
218  /** @name Constraint Methods
219  * Methods for managing constraints
220  */
221  //@{
222 
223  /*!
224  \brief It adds a new constraint.
225 
226  A constraint can be a: foreign key, unique key, check constraint or primary key
227 
228  \param c The constraint to be added to the DataSetType.
229 
230  \pre Don't inform a NULL pointer.
231  \pre For foreign keys, if the DataSetType is associated to a DataSourceCatalog the DataSetType
232  referenced in the foreign key must already be in the catalog.
233 
234  \post The constraint will be attached to the DataSetType.
235  \post The DataSetType will take the ownership of the informed constraint.
236  \post For foreign keys, if the DataSetType is associated to a DataSourceCatalog, this method
237  will register in the catalog an association between the foreign key and its referenced DataSetType.
238 
239  \exception Exception For foreign keys, it throws an exception if the DataSetType is associated to a DataSourceCatalog
240  and the foreign key is referencing another DataSetType that is not.
241  */
242  void add(Constraint* c);
243 
244  /*!
245  \brief It removes the constraint.
246 
247  A constraint can be a: foreign key, unique key, check constraint or primary key
248 
249  \param c The constraint to be removed from the DataSetType.
250 
251  \pre Don't inform a NULL pointer or a unique key that doesn't belong to the DataSetType.
252 
253  \post The constraint pointer will be invalidated.
254  */
255  void remove(Constraint* c);
256 
257  //@}
258 
259  /** @name Unique Keys Methods
260  * Methods for managing Unique Keys
261  */
262  //@{
263 
264  /*!
265  \brief It returns the number of unique keys defined for the dataset type.
266 
267  \return The number of unique keys defined for the dataset type.
268  */
269  std::size_t getNumberOfUniqueKeys() const { return m_uniqueKeys.size(); }
270 
271  /*!
272  \brief It adds the unique key constraints.
273 
274  \param uks The unique key constraints to be added to the DataSetType.
275 
276  \pre Don't inform NULL pointers in the list.
277 
278  \post The unique key constraints will be attached to the DataSetType.
279  \post The DataSetType will take the ownership of the informed unique key constraints.
280  */
281  void add(const std::vector<UniqueKey*>& uks);
282 
283  /*!
284  \brief It returns the i-th unique key.
285 
286  \param i The unique key position.
287 
288  \return The i-th unique key.
289 
290  \pre i must be in the valid range [0, getNumberOfUniqueKeys()).
291 
292  \note It doesn't make range check!
293  */
294  UniqueKey* getUniqueKey(std::size_t i) const { return m_uniqueKeys[i]; }
295 
296  /*!
297  \brief It returns a unique key with the given name or NULL if none is found.
298 
299  \param name The unique key name.
300 
301  \return A unique key with the given name or NULL.
302  */
303  UniqueKey* getUniqueKey(const std::string& name) const;
304 
305  /*!
306  \brief It removes the unique key.
307 
308  \param uk The unique key to be removed from the DataSetType.
309 
310  \pre Don't inform a NULL pointer or a unique key that doesn't belong to the DataSetType.
311 
312  \post The unique key pointer will be invalidated.
313  */
314  void remove(UniqueKey* uk);
315 
316  /*! \brief It removes all unique keys from the dataset type. */
317  void clearUniqueKeys();
318 
319  //@}
320 
321  /** @name Check Constraint Methods
322  * Methods for managing Check Constraints
323  */
324  //@{
325 
326  /*!
327  \brief It returns the number of check-constraints defined over the dataset type.
328 
329  \return The number of check-constraints defined over the dataset type.
330  */
331  std::size_t getNumberOfCheckConstraints() const { return m_checkConstraints.size(); }
332 
333  /*!
334  \brief It adds the check constraints.
335 
336  \param ccs The check constraints to be added to the DataSetType.
337 
338  \pre Don't inform NULL pointers in the list.
339 
340  \post The check constraints will be attached to the DataSetType.
341  \post The DataSetType will take the ownership of the informed check constraints.
342  */
343  void add(const std::vector<CheckConstraint*>& ccs);
344 
345  /*!
346  \brief It returns the i-th check-constraint associated to the dataset type.
347 
348  \param i The check-constraint position.
349 
350  \return The i-th check-constraint associated to the dataset type.
351 
352  \pre i must be in the valid range [0, getNumberOfCheckConstraints()).
353  */
354  CheckConstraint* getCheckConstraint(std::size_t i) const { return m_checkConstraints[i]; }
355 
356  /*!
357  \brief It returns a check-constraint with the given name or NULL if none is found.
358 
359  \param name The check-constraint name.
360 
361  \return A check-constraint with the given name or NULL if none exist.
362  */
363  CheckConstraint* getCheckConstraint(const std::string& name) const;
364 
365  /*!
366  \brief It removes the check constraint.
367 
368  \param cc The check constraint to be removed from the DataSetType.
369 
370  \pre Don't inform a NULL pointer or a check constraint that doesn't belong to the DataSetType.
371 
372  \post The check constraint pointer will be invalidated.
373  */
374  void remove(CheckConstraint* cc);
375 
376  /*! \brief It removes all check constraints. */
377  void clearCheckConstraints();
378 
379  //@}
380 
381  /** @name Index Methods
382  * Methods for managing Indexes
383  */
384  //@{
385 
386  /*!
387  \brief It returns the number of indexes defined for the dataset type.
388 
389  \return The number of indexes defined for the dataset type.
390  */
391  std::size_t getNumberOfIndexes() const { return m_idxs.size(); }
392 
393  /*!
394  \brief It adds a new index.
395 
396  \param idx The index to be added to the DataSetType.
397 
398  \pre Don't inform a NULL pointer.
399 
400  \post The index will be attached to the DataSetType.
401  \post The DataSetType will take the ownership of the informed index.
402  */
403  void add(Index* idx);
404 
405  /*!
406  \brief It adds all the indexes.
407 
408  \param idxs A list of indexes to be added to the DataSetType.
409 
410  \pre Don't inform a NULL pointer in the list.
411 
412  \post The indexes will be attached to the DataSetType.
413  \post The DataSetType will take the ownership of the informed indexes.
414  */
415  void add(const std::vector<Index*>& idxs);
416 
417  /*!
418  \brief It returns the i-th index associated to the dataset type.
419 
420  \param i The index position.
421 
422  \return The i-th index associated to the dataset type.
423 
424  \pre i must be in the valid range [0, getNumberOfIndexes()).
425 
426  \note It doesn't make range check!
427  */
428  Index* getIndex(std::size_t i) const { return m_idxs[i]; }
429 
430  /*!
431  \brief It returns an index with the given name or NULL if none is found.
432 
433  \param name The index name.
434 
435  \return An index with the given name or NULL.
436  */
437  Index* getIndex(const std::string& name) const;
438 
439  /*!
440  \brief It removes the index.
441 
442  \param idx The index to be removed from the DataSetType.
443 
444  \pre Don't inform a NULL pointer or a index that doesn't belong to the DataSetType.
445 
446  \post If there is a primary key referencing the index it will be released.
447  \post If there is a unique key referencing the index it will be released.
448  \post The index pointer will be invalidated.
449  */
450  void remove(Index* idx);
451 
452  /*! \brief It removes all indexes from the dataset type. */
453  void clearIndexes();
454 
455  //@}
456 
457  /** @name Foreign Key Methods
458  * Methods for managing foreign Keys
459  */
460  //@{
461 
462  /*!
463  \brief It returns the number of foreign keys defined for the dataset type.
464 
465  \return The number of foreign keys defined for the dataset type.
466  */
467  std::size_t getNumberOfForeignKeys() const { return m_foreignKeys.size(); }
468 
469  /*!
470  \brief It returns the i-th foreign key associated to the dataset type.
471 
472  \param i The foreign key position.
473 
474  \return The i-th foreign key associated to the dataset type.
475 
476  \pre i must be in the valid range [0, getNumberOfForeignKeys()).
477 
478  \note It doesn't make range check!
479  */
480  ForeignKey* getForeignKey(std::size_t i) const { return m_foreignKeys[i]; }
481 
482  /*!
483  \brief It returns a foreign key with the given name or NULL if none is found.
484 
485  \param name The foreign key name.
486 
487  \return A foreign key with the given name or NULL if none exist.
488  */
489  ForeignKey* getForeignKey(const std::string& name) const;
490 
491  /*!
492  \brief It adds a new foreign key.
493 
494  \param fk The foreign key to be added to the DataSetType.
495 
496  \pre Don't inform a NULL pointer or a fk associated to another DataSetType.
497  \pre If the DataSetType is associated to a DataSourceCatalog the DataSetType referenced in the foreign key must already be in the catalog.
498 
499  \post The foreign key will be attached to the DataSetType.
500  \post The DataSetType will take the ownership of the informed foreign key.
501  \post If the DataSetType is associated to a DataSourceCatalog, this method
502  will register in the catalog an association between the fk and its referenced DataSetType.
503 
504  \exception Exception It throws an exception if the DataSetType is associated to a DataSourceCatalog
505  and the fk is referencing another DataSetType that is not.
506  */
507  void add(ForeignKey* fk);
508 
509  /*!
510  \brief It removes the foreign key.
511 
512  \param fk The foreign key to be removed from the DataSetType.
513 
514  \pre Don't pass a NULL pointer or a foreign key that doesn't belong to the DataSetType.
515 
516  \post The foreign key pointer will be invalidated.
517  \post If the DataSetType is associated to a DataSourceCatalog, this method
518  will unregister in the catalog the association between the fk and its referenced DataSetType.
519 
520  \exception Exception It throws an exception if the fk can not be removed.
521  */
522  void remove(ForeignKey* fk);
523 
524  //@}
525 
526  /** @name Re-implementation from CompositeProperty
527  * Re-implementation from CompositeProperty.
528  */
529  //@{
530 
531  /*!
532  \brief It removes the Property.
533 
534  This method may propagate changes to:
535  indexes, unique keys, primary key, foreign key,
536  check-constraints, sequences and catalog.
537 
538  If the property to be removed is a geometry
539  this method will take care of the default geometry property
540  finding the first onde available or setting
541  it to NULL if no other geometry property exists.
542 
543  \param p The Property to be removed.
544 
545  \post Any association with Primary Keys, and other stuffs will be automatically removed.
546  \post The Property pointer will be invalidated.
547 
548  \todo Update CheckConstraint entries!
549  */
550  void remove(Property* p);
551 
552  /*! \brief It clears the DataSetType definition. */
553  void clear();
554 
555  //@}
556 
557  /** @name Auxiliary Methods
558  * Auxiliary methods.
559  */
560  //@{
561 
562  /*!
563  \brief It replaces the property p by pp and adjust all pointers inclusive in the catalog.
564 
565  \param p The property to be replaced.
566  \param pp The property that will be added in the datasettype.
567 
568  \note If the property p is the owner of a sequence, the new property pp will take the ownership.
569 
570  \note The caller of this method will take the ownership of p pointer. So, it is the caller responsability to free it!
571 
572  \note The DataSetType will take the ownership of pp pointer.
573  */
574  void replace(Property* p, Property* pp);
575 
576  //@}
577 
578 
579  /** @name Implementation From Superclass Property
580  * Methods re-implemented from the superclass Property.
581  */
582  //@{
583 
584  /*!
585  \brief It returns a clone of the object.
586 
587  The new DataSetType will not be associated
588  to any DataSourceCatalog. All associated constraints names,
589  like indexes, unique keys, primary keys, foreign keys and
590  check constraints will preserve the name. This is also true
591  for the DataSetType name. So, be carefull
592  when trying to persist the DataSetType in the same data source
593  as the cloned dataset type.
594 
595  Foreign keys are copied and will reference the same objects as
596  the cloned object.
597 
598  \return A clone of the object.
599  */
600  Property* clone() const;
601 
602  //@}
603 
604  protected:
605 
606  /** @name Protected Helper Methods
607  * Methods for dealing with some regular tasks related to DataSetType constraints.
608  */
609  //@{
610 
611  /*!
612  \brief It removes all unique keys related to the given Property.
613 
614  \param p A Property that may take part of a unique key.
615  */
616  void removeUniqueKeys(Property* p);
617 
618  /*!
619  \brief It removes all indexes related to the given Property.
620 
621  \param p A Property that may take part of a index.
622  */
623  void removeIndexes(Property* p);
624 
625  /*!
626  \brief It removes all foreign keys that p takes part and all foreign keys that references p.
627 
628  This method may change this DataSetType and all other DataSetTypes in the DataSourceCatalog
629  that has a foreign key referencing this property.
630 
631  \param p A Property that may take part of a foreign key.
632 
633  \note If the DataSetType is associated to a DataSourceCatalog it
634  can propagate changes for other foreign keys. It is like a cascade deletion of
635  related foreign keys and attributes.
636  */
637  void removeForeignKeys(Property* p);
638 
639  //@}
640 
641  private:
642 
643  DataSourceCatalog* m_catalog; //!< The associated catalog.
644  PrimaryKey* m_pk; //!< The DataSetType primary key.
645  std::string m_title; //!< A brief description of the DataSetType.
646  std::vector<ForeignKey*> m_foreignKeys; //!< A vector of foreign key constraints.
647  std::vector<CheckConstraint*> m_checkConstraints; //!< A vector of check constraints.
648  std::vector<Index*> m_idxs; //!< A vector of indexes.
649  std::vector<UniqueKey*> m_uniqueKeys; //!< A vector of unique key constraints.
650  int m_category; //!< A category
651  };
652 
653  typedef boost::shared_ptr<DataSetType> DataSetTypePtr;
654 
655  inline bool te::da::DataSetType::hasGeom() const
656  {
657  return hasPropertyOfType(te::dt::GEOMETRY_TYPE);
658  }
659 
660  inline bool DataSetType::hasRaster() const
661  {
663  }
664 
665  } // end namespace da
666 } // end namespace te
667 
668 #endif // __TERRALIB_DATAACCESS_INTERNAL_DATASETTYPE_H
669 
670 
DataSourceCatalog * getCatalog() const
It returns the catalog that owns the DataSetType.
Definition: DataSetType.h:158
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
Definition: DataSetType.h:137
A base class for a compound property (non-atomic properties).
void setCatalog(DataSourceCatalog *catalog)
It sets the DataSetType associated catalog.
Definition: DataSetType.h:168
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
Definition: DataSetType.h:655
It represents the system catalog of a DataSource.
PrimaryKey * m_pk
The DataSetType primary key.
Definition: DataSetType.h:644
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
Definition: DataSetType.h:269
CheckConstraint * getCheckConstraint(std::size_t i) const
It returns the i-th check-constraint associated to the dataset type.
Definition: DataSetType.h:354
int m_category
A category.
Definition: DataSetType.h:650
DataSourceCatalog * m_catalog
The associated catalog.
Definition: DataSetType.h:643
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
A class that describes a check constraint.
std::vector< ForeignKey * > m_foreignKeys
A vector of foreign key constraints.
Definition: DataSetType.h:646
Index * getIndex(std::size_t i) const
It returns the i-th index associated to the dataset type.
Definition: DataSetType.h:428
std::string m_title
A brief description of the DataSetType.
Definition: DataSetType.h:645
std::size_t getNumberOfForeignKeys() const
It returns the number of foreign keys defined for the dataset type.
Definition: DataSetType.h:467
bool hasRaster() const
It returns true if the DataSetType has at least one raster property; otherwise, it returns false...
Definition: DataSetType.h:660
URI C++ Library.
std::size_t getNumberOfIndexes() const
It returns the number of indexes defined for the dataset type.
Definition: DataSetType.h:391
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
int getCategory() const
It returns the dataset category.
Definition: DataSetType.h:177
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
bool hasPropertyOfType(const int t) const
Tells if there is a property of the given data type.
ForeignKey * getForeignKey(std::size_t i) const
It returns the i-th foreign key associated to the dataset type.
Definition: DataSetType.h:480
std::size_t getNumberOfCheckConstraints() const
It returns the number of check-constraints defined over the dataset type.
Definition: DataSetType.h:331
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
std::vector< CheckConstraint * > m_checkConstraints
A vector of check constraints.
Definition: DataSetType.h:647
std::vector< UniqueKey * > m_uniqueKeys
A vector of unique key constraints.
Definition: DataSetType.h:649
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97
std::vector< Index * > m_idxs
A vector of indexes.
Definition: DataSetType.h:648
void setCategory(int c)
It sets the dataset category.
Definition: DataSetType.h:184
It describes an index associated to a DataSetType.
Definition: Index.h:54
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
Definition: DataSetType.h:294
const std::string & getTitle() const
A human descriptive title for the DataSetType.
Definition: DataSetType.h:130