All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSource.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009-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 // TerraLib 5
21 #include "../common/Translator.h"
22 #include "Config.h"
23 #include "DataSource.h"
24 #include "Exception.h"
25 #include "ThemeInfo.h"
26 #include "Transactor.h"
27 #include "Utils.h"
28 
29 // TerraLib 4.x
30 #include <terralib4/kernel/TeDatabase.h>
31 #include <terralib4/kernel/TeDatabaseFactory.h>
32 #include <terralib4/kernel/TeDatabaseFactoryParams.h>
33 #include <terralib4/kernel/TeDBConnectionsPool.h>
34 #include <terralib4/kernel/TeDefines.h>
35 #include <terralib4/kernel/TeTheme.h>
36 #include <terralib4/utils/TeUpdateDBVersion.h>
37 
40 
42  : m_db(0)
43 {
44 }
45 
47 {
48 }
49 
50 std::string terralib4::DataSource::getType() const
51 {
53 }
54 
55 const std::map<std::string, std::string>& terralib4::DataSource::getConnectionInfo() const
56 {
57  return m_dbInfo;
58 }
59 
60 void terralib4::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
61 {
62  m_dbInfo = connInfo;
63 }
64 
65 std::auto_ptr<te::da::DataSourceTransactor> terralib4::DataSource::getTransactor()
66 {
67  std::auto_ptr<te::da::DataSourceTransactor> t(new Transactor(this, m_db));
68  return t;
69 }
70 
72 {
73  close();
74 
75  std::string dbInfo = m_dbInfo.at("T4_DRIVER");
76  std::string auxDbName = m_dbInfo.at("T4_DB_NAME");
77  std::string hostName = "";
78  std::string userName = "";
79  std::string password = "";
80  int portNumber = -1;
81 
82 
83  m_db = TeDBConnectionsPool::instance().getDatabase(dbInfo, auxDbName, hostName, userName,
84  password, portNumber);
85 
86  if(!m_db->isConnected())
87  {
88  if(!m_db->connect(hostName, userName, password, auxDbName, portNumber))
89  throw te::da::Exception(TE_TR("Could not connect to informed database!"));
90  }
91 
92  string DBver;
93  if(needUpdateDB(m_db, DBver))
94  {
95  std::string dbVersion = TeDBVERSION;
96 
97  if(isLowerVersion(dbVersion, DBver))
98  {
99  close();
100 
101  throw te::da::Exception(TE_TR("Cannot connect to database because the version of Terraview is lower than the version of the database!"));
102  }
103 
104  close();
105 
106  throw te::da::Exception(TE_TR("The database must be converted to the model ") + dbVersion + "! \n");
107  }
108 
109  m_db->loadLayerSet();
110  m_db->loadViewSet(m_db->user());
111 }
112 
114 {
115  delete m_db;
116  m_db = 0;
117 }
118 
120 {
121  return m_db != 0;
122 }
123 
125 {
126  return m_db != 0;
127 }
128 
130 {
131  return sm_capabilities;
132 }
133 
135 {
136  return sm_dialect;
137 }
138 
139 void terralib4::DataSource::create(const std::map<std::string, std::string>& dsInfo)
140 {
141  throw Exception(TE_TR("This driver is read-only!"));
142 }
143 
144 void terralib4::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
145 {
146  throw Exception(TE_TR("This driver is read-only!"));
147 }
148 
149 bool terralib4::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
150 {
151  std::vector<string> dbnames = getDataSourceNames(dsInfo);
152 
153  return std::find(dbnames.begin(), dbnames.end(), dsInfo.at("T4_DB_NAME")) != dbnames.end();
154 }
155 
156 std::vector<std::string> terralib4::DataSource::getDataSourceNames(const std::map<std::string, std::string>& dsInfo)
157 {
158  std::auto_ptr<TeDatabaseFactoryParams> params(terralib4::Convert2T4DatabaseParams(dsInfo));
159 
160  std::auto_ptr<TeDatabase> db(TeDatabaseFactory::make(*params.get()));
161 
162  std::vector<std::string> dbnames;
163 
164  db->showDatabases(params->host_, params->user_, params->password_, dbnames, params->port_);
165 
166  return dbnames;
167 }
168 
169 std::vector<te::common::CharEncoding> terralib4::DataSource::getEncodings(const std::map<std::string, std::string>& dsInfo)
170 {
171  /*std::auto_ptr<TeDatabaseFactoryParams> params(terralib4::Convert2T4DatabaseParams(dsInfo));
172 
173  std::auto_ptr<TeDatabase> db(TeDatabaseFactory::make(*params.get()));
174 
175  std::vector<std::string> encodings;
176 
177  db->getEncodingList(encodings);
178 
179  return encodings;*/
180 
181  return std::vector<te::common::CharEncoding>();
182 }
183 
185 {
186  return m_db;
187 }
188 
189 std::vector<std::string> terralib4::DataSource::getTL4Layers()
190 {
191  std::vector<std::string> layers;
192 
193  std::auto_ptr<te::da::DataSourceTransactor> t = getTransactor();
194 
195  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
196  layers = t4t->getTL4Layers();
197 
198  return layers;
199 }
200 
201 std::vector<std::string> terralib4::DataSource::getTL4Tables()
202 {
203  std::vector<std::string> tables;
204 
205  std::auto_ptr<te::da::DataSourceTransactor> t = getTransactor();
206 
207  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
208  tables = t4t->getTL4Tables();
209 
210  return tables;
211 }
212 
213 std::vector<::terralib4::ThemeInfo> terralib4::DataSource::getTL4Themes()
214 {
215  std::vector<::terralib4::ThemeInfo> themes;
216 
217  std::auto_ptr<te::da::DataSourceTransactor> t = getTransactor();
218 
219  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
220  themes = t4t->getTL4Themes();
221 
222  return themes;
223 }
224 
225 TeTheme* terralib4::DataSource::getTL4Theme(const ::terralib4::ThemeInfo theme)
226 {
227  TeTheme* tl4Theme = 0;
228 
229  std::auto_ptr<te::da::DataSourceTransactor> t = getTransactor();
230 
231  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
232  tl4Theme = t4t->getTL4Theme(theme);
233 
234  return tl4Theme;
235 }
236 
237 int terralib4::DataSource::getLayerSRID(const std::string & layerName)
238 {
239  std::auto_ptr<te::da::DataSourceTransactor> t = getTransactor();
240 
241  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
242  return t4t->getLayerSRID(layerName);
243 }
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:139
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:71
DataSourceTransactor implementation for TerraLib 4.x API.
void drop(const std::map< std::string, std::string > &dsInfo)
It removes the data source with the connection information from a driver.
Definition: DataSource.cpp:144
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
Definition: DataSource.cpp:50
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo theme)
Definition: DataSource.cpp:225
static te::da::DataSourceCapabilities sm_capabilities
Definition: DataSource.h:98
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
std::vector< std::string > getDataSourceNames(const std::map< std::string, std::string > &dsInfo)
It gets the data source names available in a driver.
Definition: DataSource.cpp:156
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
std::vector< std::string > getTL4Tables()
Definition: Transactor.cpp:806
Configuration flags for the TerraLib 4 driver.
bool isValid() const
It checks if the data source is valid (available for using).
Definition: DataSource.cpp:124
The basic information about a Terralib 4.x Theme.
std::auto_ptr< TeDatabaseFactoryParams > Convert2T4DatabaseParams(const std::map< std::string, std::string > &dsInfo)
It converts a data source information to a TerraLib 4.x database params.
Definition: Utils.cpp:127
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
Definition: DataSource.cpp:119
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
Definition: DataSource.cpp:134
TeDatabase * getTerralib4Db()
Definition: DataSource.cpp:184
std::auto_ptr< te::da::DataSourceTransactor > getTransactor()
It returns an object that can execute transactions in the context of a data source.
Definition: DataSource.cpp:65
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo theme)
Definition: Transactor.cpp:860
const std::map< std::string, std::string > & getConnectionInfo() const
It returns the set of parameters used to set up the access channel to the underlying repository...
Definition: DataSource.cpp:55
std::vector< std::string > getTL4Tables()
Definition: DataSource.cpp:201
std::vector< te::common::CharEncoding > getEncodings(const std::map< std::string, std::string > &dsInfo)
It gets the encodings for the data source.
Definition: DataSource.cpp:169
std::vector<::terralib4::ThemeInfo > getTL4Themes()
Definition: DataSource.cpp:213
static te::da::SQLDialect * sm_dialect
Definition: DataSource.h:99
int getLayerSRID(const std::string &layerName)
Definition: Transactor.cpp:887
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Definition: DataSource.cpp:129
int getLayerSRID(const std::string &layerName)
Definition: DataSource.cpp:237
void close()
It closes the data source and clears all the resources used by its internal communication channel...
Definition: DataSource.cpp:113
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
It sets the connection information to be used when connecting to the data source. ...
Definition: DataSource.cpp:60
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:149
std::vector< std::string > getTL4Layers()
Definition: Transactor.cpp:791
#define TERRALIB4_DRIVER_IDENTIFIER
The Terralib 4 driver identifier string.
Definition: Config.h:84
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
~DataSource()
Virtual destructor.
Definition: DataSource.cpp:46
std::vector<::terralib4::ThemeInfo > getTL4Themes()
Definition: Transactor.cpp:819
std::vector< std::string > getTL4Layers()
Definition: DataSource.cpp:189