All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DSInfo.cpp
Go to the documentation of this file.
1 // Terralib Data Source Info tool
2 #include <DSInfo.h>
3 
5 {
6  m_dataSourceType = "";
7  m_connStr = "";
8  m_dataSource = 0;
9 }
10 
11 bool DSInfo::init(std::string dstype, std::string connstr)
12 {
13 
14  if(dstype.empty())
15  {
16  std::cout << "Command Line Error: Data Source Type (ds-type) missing!" << std::endl;
17  return false;
18  }
19  else if(connstr.empty())
20  {
21  std::cout << "Command Line Error: Data Source Connection String (conn-str) missing!" << std::endl;
22  return false;
23  }
24 
26  m_connStr = connstr;
27 
28  std::string errorMessage;
29  Utils::loadModules(errorMessage);
30 
31  Utils::getDataSource(dstype, connstr, m_dataSource, errorMessage);
32 
33  if(m_dataSource == 0)
34  {
35  std::cout << std::endl << "Could not get a connection with the DataSource." << std::endl;
36  return false;
37  }
38 
39  return true;
40 }
41 
42 PKInfo DSInfo::getPrimaryKeyInfo(std::string dataSetName)
43 {
45  te::da::DataSourceCatalogLoader* catalog = transactor->getCatalogLoader();
46  te::da::DataSetType* dataSetType = catalog->getDataSetType(dataSetName, true);
47 
48  te::da::PrimaryKey* pk = 0;
49  pk = dataSetType->getPrimaryKey();
50 
51  PKInfo pkInfo;
52 
53  if(pk)
54  {
55  pkInfo.name = pk->getName();
57  }
58  else
59  {
60  pkInfo = PKInfo();
61  }
62 
63  delete transactor;
64  delete catalog;
65  delete dataSetType;
66 
67  return pkInfo;
68 }
69 
70 std::vector<FKInfo> DSInfo::getForeignKeyInfo(std::string dataSetName)
71 {
73  te::da::DataSourceCatalogLoader* catalog = transactor->getCatalogLoader();
74  te::da::DataSetType* dataSetType = catalog->getDataSetType(dataSetName, true);
75 
76  std::vector<FKInfo> vecFkInfo;
77 
78  unsigned int fkNumber = dataSetType->getNumberOfForeignKeys();
79 
80  for(unsigned int i = 0; i < fkNumber; i++)
81  {
82  te::da::ForeignKey* fk = dataSetType->getForeignKey(i);
83 
84  FKInfo fkInfo;
85  fkInfo.name = fk->getName();
87 
88  vecFkInfo.push_back(fkInfo);
89  }
90 
91  delete transactor;
92  delete catalog;
93  delete dataSetType;
94 
95  return vecFkInfo;
96 }
97 
98 std::vector<UKInfo> DSInfo::getUniqueKeyInfo(std::string dataSetName)
99 {
101  te::da::DataSourceCatalogLoader* catalog = transactor->getCatalogLoader();
102  te::da::DataSetType* dataSetType = catalog->getDataSetType(dataSetName, true);
103 
104  std::vector<UKInfo> vecUkInfo;
105 
106  unsigned int ukNumber = dataSetType->getNumberOfUniqueKeys();
107 
108  for(unsigned int i = 0; i < ukNumber; i++)
109  {
110  te::da::UniqueKey* uk = dataSetType->getUniqueKey(i);
111 
112  UKInfo ukInfo;
113  ukInfo.name = uk->getName();
115 
116  vecUkInfo.push_back(ukInfo);
117  }
118 
119  delete transactor;
120  delete catalog;
121  delete dataSetType;
122 
123  return vecUkInfo;
124 }
125 
126 std::vector<IdxInfo> DSInfo::getIndexesKeyInfo(std::string dataSetName)
127 {
129  te::da::DataSourceCatalogLoader* catalog = transactor->getCatalogLoader();
130  te::da::DataSetType* dataSetType = catalog->getDataSetType(dataSetName, true);
131 
132  std::vector<IdxInfo> vecIdxInfo;
133 
134  unsigned int idxNumber = dataSetType->getNumberOfIndexes();
135 
136  for(unsigned int i = 0; i < idxNumber; i++)
137  {
138  te::da::Index* idx = dataSetType->getIndex(i);
139 
140  IdxInfo idxInfo;
141  idxInfo.name = idx->getName();
142  idxInfo.type = Utils::getIdxTypeName(idx->getIndexType());
144 
145  vecIdxInfo.push_back(idxInfo);
146  }
147 
148  delete transactor;
149  delete catalog;
150  delete dataSetType;
151 
152  return vecIdxInfo;
153 }
154 
155 std::vector<CCInfo> DSInfo::getCheckConstraintInfo(std::string dataSetName)
156 {
158  te::da::DataSourceCatalogLoader* catalog = transactor->getCatalogLoader();
159  te::da::DataSetType* dataSetType = catalog->getDataSetType(dataSetName, true);
160 
161  std::vector<CCInfo> vecCcInfo;
162 
163  unsigned int ccNumber = dataSetType->getNumberOfCheckConstraints();
164 
165  for(unsigned int i = 0; i < ccNumber; i++)
166  {
167  te::da::CheckConstraint* cc = dataSetType->getCheckConstraint(i);
168 
169  CCInfo ccInfo;
170  ccInfo.name = cc->getName();
171  ccInfo.expression = cc->getExpression();
172 
173  vecCcInfo.push_back(ccInfo);
174  }
175 
176  delete transactor;
177  delete catalog;
178  delete dataSetType;
179 
180  return vecCcInfo;
181 }
182 
183 DataSetStruct DSInfo::getDataSetInfo(std::string dataSetName)
184 {
185  DataSetStruct ds;
186 
187  te::da::DataSet* dataset = 0;
188  dataset = Utils::getDataSet(m_dataSource, dataSetName);
189 
190  if(dataset)
191  {
192  ds.name = dataSetName;
193  ds.pkInfo = getPrimaryKeyInfo(dataSetName);
194  ds.hasPk = (!ds.pkInfo.name.empty() ? true : false);
195  ds.vecFkInfo = getForeignKeyInfo(dataSetName);
196  ds.vecUkInfo = getUniqueKeyInfo(dataSetName);
197  ds.vecIdxInfo = getIndexesKeyInfo(dataSetName);
198  ds.vecCcInfo = getCheckConstraintInfo(dataSetName);
199  }
200  else
201  {
202  ds = DataSetStruct();
203  }
204 
205  return ds;
206 
207 }
208 
209 std::vector<std::string*> DSInfo::getDataSetNameList()
210 {
211  std::vector<std::string*> dataSetsNames;
212 
214 
215  te::da::DataSourceCatalogLoader* catalogLoader = transactor->getCatalogLoader();
216 
217  catalogLoader->getDataSets(dataSetsNames);
218 
219  delete transactor;
220  delete catalogLoader;
221 
222  return dataSetsNames;
223 }
224 
225 std::vector<DataSetStruct> DSInfo::getDataSetsInfo()
226 {
227  std::vector<DataSetStruct> vecDataSetStruct;
228 
229  std::map<std::string, te::da::DataSet*> dataSets;
230 
231  std::vector<std::string*> dataSetsNames;
232 
234 
235  te::da::DataSourceCatalogLoader* catalogLoader = transactor->getCatalogLoader();
236 
237  catalogLoader->getDataSets(dataSetsNames);
238 
239  for(std::vector<std::string*>::const_iterator it = dataSetsNames.begin(); it < dataSetsNames.end(); ++it)
240  {
241  const std::string* datasetName = *it;
242 
243  vecDataSetStruct.push_back(getDataSetInfo(*datasetName));
244  }
245 
246  delete transactor;
247  delete catalogLoader;
248 
249  return vecDataSetStruct;
250 }
251 
253 {
254  DataSourceHeader dsh;
255 
256  std::string dsType = m_dataSource->getType();
257 
258  std::map<std::string, std::string> connInfo = m_dataSource->getConnectionInfo();
259 
260  dsh.name = (!connInfo["dbname"].empty() ? connInfo["dbname"] : std::string());
261  dsh.type = dsType;
262 
263  return dsh;
264 }
265 
266 std::map<std::string, std::string> DSInfo::getCapabilities()
267 {
268  std::map<std::string, std::string> capMap;
269 
270  m_dataSource->getCapabilities(capMap);
271 
272  if(capMap.empty())
273  return std::map<std::string, std::string>();
274 
275  return capMap;
276 }
277 
278 std::map<std::string, std::vector<std::string>> DSInfo::getDataSourceParameters(std::string dsType)
279 {
280  std::map<std::string, std::vector<std::string>> mapParams;
281 
282  std::string errorMessage;
283  Utils::loadModules(errorMessage);
284 
285  if(dsType.empty())
286  {
287  te::da::DataSourceFactory::dictionary_type::const_iterator it = te::da::DataSourceFactory::getDictionary().begin();
288  te::da::DataSourceFactory::dictionary_type::const_iterator itend = te::da::DataSourceFactory::getDictionary().end();
289 
290  while(it != itend)
291  {
292  std::string datasourceType = it->first;
293 
294  const te::da::DataSourceFactory* factory = dynamic_cast<te::da::DataSourceFactory*>(it->second);
295 
296  std::vector<std::string> datasourceParams;
297 
298  factory->getConnectionParameters(datasourceParams);
299 
300  mapParams[datasourceType] = datasourceParams;
301 
302  ++it;
303  }
304  }
305  else
306  {
307  dsType = te::common::Convert2UCase(dsType);
308 
309  const te::da::DataSourceFactory* factory = 0;
310 
311  factory = dynamic_cast<te::da::DataSourceFactory*>(te::da::DataSourceFactory::getDictionary().find(dsType));
312 
313  if(factory)
314  {
315  std::vector<std::string> datasourceParams;
316 
317  factory->getConnectionParameters(datasourceParams);
318 
319  mapParams[dsType] = datasourceParams;
320  }
321  else
322  {
323  std::cout << std::endl << "Parameters Unknown!" << std::endl;
324  }
325  }
326 
327  return mapParams;
328 }
Struct that stores information about a index using the most common data types.
Definition: DSInfo.h:207
Struct that stores information about a data source header.
Definition: DSInfo.h:240
bool init(std::string dstype, std::string connstr)
Init the application.
Definition: DSInfo.cpp:11
std::vector< std::string > porperties
Foreign Key Properties in a string vector.
Definition: DSInfo.h:192
std::vector< UKInfo > vecUkInfo
Unique Key struct vector.
Definition: DSInfo.h:232
std::string m_connStr
The Data Source Connection String.
Definition: DSInfo.h:172
std::vector< std::string > porperties
Unique Key Properties in a string vector.
Definition: DSInfo.h:201
static std::vector< std::string > getFKPropertiesNames(te::da::ForeignKey *fk)
Getting Foreign Key Properties Names.
Definition: Utils.cpp:55
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::string type
Data Source Type.
Definition: DSInfo.h:243
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
DataSetStruct getDataSetInfo(std::string dataSetName)
Get specific Data Set information.
Definition: DSInfo.cpp:183
virtual std::auto_ptr< DataSourceTransactor > getTransactor()=0
It returns an object that can execute transactions in the context of a data source.
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
std::vector< std::string > porperties
Primary Key Properties in a string vector.
Definition: DSInfo.h:183
Struct that stores information about a check constraint using the most common data types...
Definition: DSInfo.h:217
static bool loadModules(std::string &errorMessage)
Load Terralib modules.
Definition: Utils.cpp:110
std::vector< IdxInfo > getIndexesKeyInfo(std::string dataSetName)
Get indexes information.
Definition: DSInfo.cpp:126
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
std::vector< std::string > porperties
Index Properties in a string vector.
Definition: DSInfo.h:211
std::vector< IdxInfo > vecIdxInfo
Index struct vector.
Definition: DSInfo.h:233
A class that describes a check constraint.
static std::vector< std::string > getIdxPropertiesNames(te::da::Index *idx)
Getting Index Properties Names.
Definition: Utils.cpp:83
std::string type
Index Type.
Definition: DSInfo.h:210
std::string name
Index Name.
Definition: DSInfo.h:209
std::vector< FKInfo > getForeignKeyInfo(std::string dataSetName)
Get foreign keys information.
Definition: DSInfo.cpp:70
Index * getIndex(std::size_t i) const
It returns the i-th index associated to the dataset type.
Definition: DataSetType.h:428
Struct that stores information about a primary key using the most common data types.
Definition: DSInfo.h:180
std::string name
Primary Key Name.
Definition: DSInfo.h:182
std::size_t getNumberOfForeignKeys() const
It returns the number of foreign keys defined for the dataset type.
Definition: DataSetType.h:467
const std::string & getExpression() const
It returns the check constraint expression.
std::string m_dataSourceType
The Data Source Type.
Definition: DSInfo.h:171
DataSourceHeader getDataSourceHeader()
Get the Data Source Header.
Definition: DSInfo.cpp:252
std::string expression
Check Constraint Expression.
Definition: DSInfo.h:220
virtual std::string getType() const =0
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
std::size_t getNumberOfIndexes() const
It returns the number of indexes defined for the dataset type.
Definition: DataSetType.h:391
virtual const DataSourceCapabilities & getCapabilities() const =0
It returns the known capabilities of the data source.
static bool getDataSource(std::string dsType, std::string connStr, te::da::DataSource *&dataSource, std::string &errorMessage)
Method to connect with a Data Source.
Definition: Utils.cpp:4
std::map< std::string, std::string > getCapabilities()
Get the Data Source Capabilities.
Definition: DSInfo.cpp:266
It models a foreign key constraint for a DataSetType.
Definition: ForeignKey.h:50
std::vector< CCInfo > vecCcInfo
Check Constraint struct vector.
Definition: DSInfo.h:234
Struct that stores information about a data set using the most common data types. ...
Definition: DSInfo.h:226
std::string name
Check Constraint Name.
Definition: DSInfo.h:219
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
static std::vector< std::string > getPKPropertiesNames(te::da::PrimaryKey *pk)
Getting Primary Key Properties Names.
Definition: Utils.cpp:41
A DataSourceTransactor can be viewed as a connection to the data source for reading/writing things in...
static std::vector< std::string > getUKPropertiesNames(te::da::UniqueKey *uk)
Getting Unique Key Properties Names.
Definition: Utils.cpp:69
static te::da::DataSet * getDataSet(te::da::DataSource *dataSource, std::string dataSetName)
Definition: Utils.cpp:97
Struct that stores information about a unique key using the most common data types.
Definition: DSInfo.h:198
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
std::map< std::string, std::vector< std::string > > getDataSourceParameters(std::string dataSourceType)
Get Data Source Types connection parameters.
Definition: DSInfo.cpp:278
std::string name
Foreign Key Name.
Definition: DSInfo.h:191
std::vector< CCInfo > getCheckConstraintInfo(std::string dataSetName)
Get check constraint information.
Definition: DSInfo.cpp:155
ForeignKey * getForeignKey(std::size_t i) const
It returns the i-th foreign key associated to the dataset type.
Definition: DataSetType.h:480
std::vector< std::string * > getDataSetNameList()
Get all Data Sets names.
Definition: DSInfo.cpp:209
std::size_t getNumberOfCheckConstraints() const
It returns the number of check-constraints defined over the dataset type.
Definition: DataSetType.h:331
PKInfo getPrimaryKeyInfo(std::string dataSetName)
Get primary key information.
Definition: DSInfo.cpp:42
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
std::string name
Data Source Name.
Definition: DSInfo.h:242
te::da::DataSource * m_dataSource
The Data Source.
Definition: DSInfo.h:173
std::vector< FKInfo > vecFkInfo
Foreign Key struct vector.
Definition: DSInfo.h:231
static std::string getIdxTypeName(int id)
Definition: Utils.cpp:27
DSInfo()
Class Constructor.
Definition: DSInfo.cpp:4
std::vector< DataSetStruct > getDataSetsInfo()
Get all Data Sets informations.
Definition: DSInfo.cpp:225
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
Struct that stores information about a foreign key using the most common data types.
Definition: DSInfo.h:189
bool hasPk
If has Primary key.
Definition: DSInfo.h:229
IndexType getIndexType() const
It gets the index type.
Definition: Index.h:169
std::string name
Unique Key Name.
Definition: DSInfo.h:200
It describes an index associated to a DataSetType.
Definition: Index.h:54
Data Source Informations tool.
virtual const std::map< std::string, std::string > & getConnectionInfo() const =0
It returns the set of parameters used to set up the access channel to the underlying repository...
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
Definition: DataSetType.h:294
std::vector< UKInfo > getUniqueKeyInfo(std::string dataSetName)
Get unique keys information.
Definition: DSInfo.cpp:98
A factory for data sources.
std::string name
Data Set Name.
Definition: DSInfo.h:228
PKInfo pkInfo
Primary Key struct.
Definition: DSInfo.h:230
const std::string & getName() const
It returns the index name.
Definition: Index.h:155