src/terralib/terralib4/DataSource.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 // TerraLib 5
21 #include "../core/translator/Translator.h"
22 #include "../core/uri/URI.h"
23 #include "../core/uri/Utils.h"
24 #include "Config.h"
25 #include "DataSource.h"
26 #include "Exception.h"
27 #include "ThemeInfo.h"
28 #include "Transactor.h"
29 #include "Utils.h"
30 
31 // TerraLib 4.x
32 #include <terralib4/kernel/TeDatabase.h>
33 #include <terralib4/kernel/TeDatabaseFactory.h>
34 #include <terralib4/kernel/TeDatabaseFactoryParams.h>
35 #include <terralib4/kernel/TeDBConnectionsPool.h>
36 #include <terralib4/kernel/TeDefines.h>
37 #include <terralib4/kernel/TeTheme.h>
38 #include <terralib4/utils/TeUpdateDBVersion.h>
39 
40 // Boost
41 #include <boost/algorithm/string/replace.hpp>
42 
45 
46 terralib4::DataSource::DataSource(const std::string& connInfo)
47  : te::da::DataSource(connInfo),
48  m_db(0)
49 {
50 }
51 
53  : te::da::DataSource(uri),
54  m_db(0)
55 {
56 }
57 
58 
60 {
61 }
62 
63 std::string terralib4::DataSource::getType() const
64 {
66 }
67 
68 std::unique_ptr<te::da::DataSourceTransactor> terralib4::DataSource::getTransactor()
69 {
70  std::unique_ptr<te::da::DataSourceTransactor> t(new Transactor(this, m_db));
71  return t;
72 }
73 
75 {
76  close();
77 
78  if (!m_uri.isValid())
79  throw te::da::Exception(TE_TR("There is no valid information about the data source"));
80 
81  std::map<std::string, std::string> kvp = te::core::Expand(m_uri.query());
82  std::map<std::string, std::string>::const_iterator it = kvp.begin();
83  std::map<std::string, std::string>::const_iterator itend = kvp.end();
84  std::string dbInfo, auxDbName;
85 
86  it = kvp.find("T4_DRIVER");
87  if (it != itend && !it->second.empty())
88  dbInfo = terralib4::Convert2Latin1(it->second);
89  else
90  throw te::da::Exception(TE_TR("The connection information is invalid. Missing T4_DRIVER parameter!"));
91 
92  if(dbInfo == "Ado")
93  {
94  auxDbName = te::core::URIDecode(m_uri.uri());
95  boost::replace_all(auxDbName, "file://", "");
96  }
97  else
98  auxDbName = te::core::URIDecode(m_uri.path());
99 
100  if (auxDbName.empty())
101  throw te::da::Exception(TE_TR("The connection information is invalid. Missing the database name!"));
102 
103  std::string hostName = "";
104  std::string userName = "";
105  std::string password = "";
106  int portNumber = -1;
107 
108 
109  m_db = TeDBConnectionsPool::instance().getDatabase(dbInfo, auxDbName, hostName, userName,
110  password, portNumber);
111 
112  if(!m_db->isConnected())
113  {
114  if(!m_db->connect(hostName, userName, password, auxDbName, portNumber))
115  throw te::da::Exception(TE_TR("Could not connect to informed database!"));
116  }
117 
118  string DBver;
119  if(needUpdateDB(m_db, DBver))
120  {
121  std::string dbVersion = TeDBVERSION;
122 
123  if(isLowerVersion(dbVersion, DBver))
124  {
125  close();
126 
127  throw te::da::Exception(TE_TR("Cannot connect to database because the version of Terraview is lower than the version of the database!"));
128  }
129 
130  close();
131 
132  throw te::da::Exception(TE_TR("The database must be converted to the model ") + dbVersion + "! \n");
133  }
134 
135  m_db->loadLayerSet();
136  m_db->loadViewSet(m_db->user());
137 }
138 
140 {
141  delete m_db;
142  m_db = 0;
143 }
144 
146 {
147  return m_db != 0;
148 }
149 
151 {
152  return m_db != 0;
153 }
154 
156 {
157  return sm_capabilities;
158 }
159 
161 {
162  return sm_dialect;
163 }
164 
165 void terralib4::DataSource::create(const std::string& dsInfo)
166 {
167  throw Exception(TE_TR("This driver is read-only!"));
168 }
169 
170 void terralib4::DataSource::drop(const std::string& dsInfo)
171 {
172  throw Exception(TE_TR("This driver is read-only!"));
173 }
174 
175 bool terralib4::DataSource::exists(const std::string& dsInfo)
176 {
177  std::vector<string> dbnames = getDataSourceNames(dsInfo);
178  std::map<std::string, std::string> kvp = te::core::Expand(m_uri.query());
179 
180  return std::find(dbnames.begin(), dbnames.end(), kvp["T4_DB_NAME"]) != dbnames.end();
181 }
182 
183 std::vector<std::string> terralib4::DataSource::getDataSourceNames(const std::string& dsInfo)
184 {
185  std::unique_ptr<TeDatabaseFactoryParams> params(terralib4::Convert2T4DatabaseParams(dsInfo));
186 
187  std::unique_ptr<TeDatabase> db(TeDatabaseFactory::make(*params.get()));
188 
189  std::vector<std::string> dbnames;
190 
191  db->showDatabases(params->host_, params->user_, params->password_, dbnames, params->port_);
192 
193  return dbnames;
194 }
195 
197 {
198  return m_db;
199 }
200 
201 std::vector<std::string> terralib4::DataSource::getTL4Layers()
202 {
203  std::vector<std::string> layers;
204 
205  std::unique_ptr<te::da::DataSourceTransactor> t = getTransactor();
206 
207  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
208  layers = t4t->getTL4Layers();
209 
210  return layers;
211 }
212 
213 std::vector<std::string> terralib4::DataSource::getTL4Tables()
214 {
215  std::vector<std::string> tables;
216 
217  std::unique_ptr<te::da::DataSourceTransactor> t = getTransactor();
218 
219  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
220  tables = t4t->getTL4Tables();
221 
222  return tables;
223 }
224 
225 std::vector<std::string> terralib4::DataSource::getTL4Rasters()
226 {
227  std::vector<std::string> rasters;
228 
229  std::unique_ptr<te::da::DataSourceTransactor> t = getTransactor();
230 
231  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
232  rasters = t4t->getTL4Rasters();
233 
234  return rasters;
235 }
236 
237 std::vector<std::pair<std::string, std::string> > terralib4::DataSource::getTL4RasterFiles()
238 {
239  std::vector<std::pair<std::string, std::string> > rasters;
240 
241  std::unique_ptr<te::da::DataSourceTransactor> t = getTransactor();
242 
243  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
244  rasters = t4t->getTL4RasterFiles();
245 
246  return rasters;
247 }
248 
249 std::vector<::terralib4::ThemeInfo> terralib4::DataSource::getTL4Themes()
250 {
251  std::vector<::terralib4::ThemeInfo> themes;
252 
253  std::unique_ptr<te::da::DataSourceTransactor> t = getTransactor();
254 
255  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
256  themes = t4t->getTL4Themes();
257 
258  return themes;
259 }
260 
261 TeTheme* terralib4::DataSource::getTL4Theme(const ::terralib4::ThemeInfo& theme)
262 {
263  TeTheme* tl4Theme = 0;
264 
265  std::unique_ptr<te::da::DataSourceTransactor> t = getTransactor();
266 
267  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
268  tl4Theme = t4t->getTL4Theme(theme);
269 
270  return tl4Theme;
271 }
272 
273 int terralib4::DataSource::getLayerSRID(const std::string & layerName)
274 {
275  std::unique_ptr<te::da::DataSourceTransactor> t = getTransactor();
276 
277  terralib4::Transactor* t4t = dynamic_cast<terralib4::Transactor*>(t.get());
278  return t4t->getLayerSRID(layerName);
279 }
std::unique_ptr< TeDatabaseFactoryParams > Convert2T4DatabaseParams(const std::string &dsInfo)
It converts a data source information to a TerraLib 4.x database params.
std::vector< std::string > getTL4Rasters()
void create(const std::string &dsInfo)
It creates a new data source.
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
#define TERRALIB4_DRIVER_IDENTIFIER
The Terralib 4 driver identifier string.
void open()
It opens the data source and makes it ready for using.
std::vector< std::pair< std::string, std::string > > getTL4RasterFiles()
std::vector< std::string > getDataSourceNames(const std::string &dsInfo)
It gets the data source names available in a driver.
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
std::vector< std::string > getTL4Rasters()
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
static te::da::DataSourceCapabilities sm_capabilities
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
bool isValid() const
Return if the given URI is valid or not.
Definition: URI.cpp:133
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::vector< std::string > getTL4Tables()
bool isValid() const
It checks if the data source is valid (available for using).
std::string query() const
Retrieving the query.
Definition: URI.cpp:123
The basic information about a Terralib 4.x Theme.
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo &theme)
URI C++ Library.
Definition: Attributes.h:37
DataSource(const std::string &connInfo)
std::vector< std::pair< std::string, std::string > > getTL4RasterFiles()
std::vector< std::string > getTL4Tables()
const std::string & uri() const
Retrieving the full URI.
Definition: URI.cpp:88
void drop(const std::string &dsInfo)
It removes the data source with the connection information from a driver.
TETERRALIB4EXPORT std::string Convert2Latin1(const std::string &str)
bool exists(const std::string &dsInfo)
Check the existence of a data source in a driver.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
std::vector<::terralib4::ThemeInfo > getTL4Themes()
static te::da::SQLDialect * sm_dialect
int getLayerSRID(const std::string &layerName)
This file contains utility functions used to manipulate data from a URI.
DataSourceTransactor implementation for TerraLib 4.x API.
te::core::URI m_uri
The URI used to describe the datasource connection;.
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
int getLayerSRID(const std::string &layerName)
TeTheme * getTL4Theme(const ::terralib4::ThemeInfo &theme)
void close()
It closes the data source and clears all the resources used by its internal communication channel...
TECOREEXPORT std::string URIDecode(const std::string &srcUri)
Decodes an encoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
list rasters
Definition: compose.py:3
std::vector< std::string > getTL4Layers()
std::unique_ptr< te::da::DataSourceTransactor > getTransactor()
It returns the set of parameters used to set up the access channel to the underlying repository...
std::vector<::terralib4::ThemeInfo > getTL4Themes()
std::vector< std::string > getTL4Layers()
Configuration flags for the TerraLib 4 driver.