PrintCatalog.cpp
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 examples/ado/PrintCatalog.cpp
22 
23  \brief It displays the information about datasets stored in the data source catalog
24 */
25 
26 // Example
27 #include "ADOExamples.h"
28 
29 // TerraLib
30 #include <terralib/common.h>
34 
35 // STL
36 #include <iostream>
37 #include <memory>
38 
39 
41 {
42 
43  if(ds == 0 || !ds->isOpened())
44  {
45  std::cout << "The informed data source is NULL or is closed!" << std::endl;
46  return;
47  }
48 
49  // Get a transactor to interact to the data source
50  std::unique_ptr<te::da::DataSourceTransactor> t(ds->getTransactor());
51  //te::da::DataSourceTransactor* t = ds->getTransactor();
52 
53  std::vector<std::string> datasets = ds->getDataSetNames();
54 
55  // Iterate over the dataset names to retrieve its information
56  std::cout << "Printing information about datasets...\n\nNumber of datasets: " << datasets.size() << std::endl;
57 
58  for(std::size_t i = 0; i < datasets.size(); ++i)
59  {
60  // Get dataset information: the parameter true indicates that we want all the information about the dataset;
61  // you can set it to false and see the difference (no primary key and other stuffs are retrieved)
62 
63  std::unique_ptr<te::da::DataSetType> dt(ds->getDataSetType(datasets[i]));
64 
65  std::cout << "\n=============================================================================" << std::endl;
66  std::cout << "DataSet: " << dt->getName() << std::endl;
67  std::cout << ">>>>>Number of attributes: " << dt->size() << std::endl;
68 
69  // Get the primary key information taking the column(s) that compose(s) the primary key(if any)
70  std::cout << ">>>>>Primary Key Information" << std::endl;
71 
72  te::da::PrimaryKey* pk = dt->getPrimaryKey();
73  if(pk != 0)
74  {
75  const std::vector<te::dt::Property*>& pkCols = pk->getProperties();
76 
77  std::vector<std::string> pNames(pkCols.size());
78  for(std::size_t i = 0; i < pkCols.size(); ++i)
79  {
80  pNames[i]= pkCols[i]->getName();
81  std::cout << "\t" << "Column " << i << ": " << pNames[i] << std::endl;
82  }
83  }
84  else
85  std::cout << "\t" << "The data set has no primary key!" << std::endl;
86 
87  // Get the unique key(s) information taking the column(s) that compose(s) each unique key(if any)
88  std::cout << ">>>>>Unique Key Information" << std::endl;
89 
90  // Get the number of unique keys
91  std::size_t numUniqueKeys = dt->getNumberOfUniqueKeys();
92 
93  if(numUniqueKeys == 0)
94  std::cout << "\t" << "The data set has no unique keys!" << std::endl;
95 
96  for(std::size_t i = 0; i < numUniqueKeys; ++i)
97  {
98  te::da::UniqueKey* uk = dt->getUniqueKey(i);
99  const std::vector<te::dt::Property*>& ukCols = uk->getProperties();
100 
101  std::vector<std::string> pNames(ukCols.size());
102  for(std::size_t i = 0; i < ukCols.size(); ++i)
103  {
104  pNames[i]= ukCols[i]->getName();
105  std::cout << "\t" << "Column " << i << ": " << pNames[i] << std::endl;
106  }
107  }
108 
109  // Get the index(es) information taking the column(s) that compose(s) each index(if any)
110  std::cout << ">>>>>Index(es) Information" << std::endl;
111 
112  // Get the number of indexes
113  std::size_t numIndexes = dt->getNumberOfIndexes();
114 
115  if(numIndexes == 0)
116  std::cout << "\t" << "The data set has no index(es)!" << std::endl;
117  else
118  std::cout << "- Number of Indexes: " << numIndexes << std::endl;
119 
120  for(std::size_t i = 0; i < numIndexes; ++i)
121  {
122  te::da::Index* inx = dt->getIndex(i);
123 
124  std::cout << "- Index Name: " << inx->getName() << std::endl;
125 
126  const std::vector<te::dt::Property*>& inxCols = inx->getProperties();
127 
128  std::vector<std::string> pNames(inxCols.size());
129  for(std::size_t i = 0; i < inxCols.size(); ++i)
130  {
131  pNames[i]= inxCols[i]->getName();
132  std::cout << " Column " << i << ": " << pNames[i] << std::endl;
133  }
134  }
135 
136  std::size_t numCC = dt->getNumberOfCheckConstraints();
137 
138  std::cout << ">>>>>Check Contraint(s) Information" << std::endl;
139 
140  std::cout << " Number of Check-Constraints: " << numCC << std::endl;
141 
142  for(std::size_t i = 0; i < numCC; ++i)
143  {
144  te::da::CheckConstraint* cc = dt->getCheckConstraint(i);
145  std::cout << " Check Constraint Name: " << cc->getName();
146  std::cout << ", Expression = " << cc->getExpression() << std::endl;
147  }
148 
149  std::cout << std::endl;
150  }
151 }
virtual std::unique_ptr< DataSourceTransactor > getTransactor()=0
It returns the set of parameters used to set up the access channel to the underlying repository...
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
virtual bool isOpened() const =0
It returns true if the data source is opened, otherwise it returns false.
Examples that show how to access/manipulate an ADO data source.
static te::dt::Date ds(2010, 01, 01)
A class that describes a check constraint.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the primary key.
Definition: PrimaryKey.h:109
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
const std::string & getExpression() const
It returns the check constraint expression.
static te::dt::TimeDuration dt(20, 30, 50, 11)
virtual std::vector< std::string > getDataSetNames()
It gets the dataset names available in the data source.
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
virtual std::unique_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that form the unique key.
Definition: UniqueKey.h:110
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the index.
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
This file contains include headers for the TerraLib Common Runtime module.
void PrintCatalog(te::da::DataSource *ds)
This example will print to the standard output all the information about the datasets stored in a dat...
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
It describes an index associated to a DataSetType.
const std::string & getName() const
It returns the index name.