All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 "Transactor.h"
26 #include "Utils.h"
27 
28 // TerraLib 4.x
29 #include <terralib/kernel/TeDatabase.h>
30 #include <terralib/kernel/TeDatabaseFactory.h>
31 #include <terralib/kernel/TeDatabaseFactoryParams.h>
32 #include <terralib/kernel/TeDBConnectionsPool.h>
33 #include <terralib/kernel/TeDefines.h>
34 #include <terralib/utils/TeUpdateDBVersion.h>
35 
36 
39 
41  : m_db(0)
42 {
43 }
44 
46 {
47 }
48 
49 std::string terralib4::DataSource::getType() const
50 {
52 }
53 
54 const std::map<std::string, std::string>& terralib4::DataSource::getConnectionInfo() const
55 {
56  return m_dbInfo;
57 }
58 
59 void terralib4::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
60 {
61  m_dbInfo = connInfo;
62 }
63 
64 std::auto_ptr<te::da::DataSourceTransactor> terralib4::DataSource::getTransactor()
65 {
66  std::auto_ptr<te::da::DataSourceTransactor> t(new Transactor(this, m_db));
67  return t;
68 }
69 
71 {
72  close();
73 
74  std::string dbInfo = m_dbInfo.at("T4_DRIVER");
75  std::string auxDbName = m_dbInfo.at("T4_DB_NAME");
76  std::string hostName = "";
77  std::string userName = "";
78  std::string password = "";
79  int portNumber = -1;
80 
81 
82  m_db = TeDBConnectionsPool::instance().getDatabase(dbInfo, auxDbName, hostName, userName,
83  password, portNumber);
84 
85  if(!m_db->isConnected())
86  {
87  if(!m_db->connect(hostName, userName, password, auxDbName, portNumber))
88  throw te::da::Exception(TR_TERRALIB4("Could not connect to informed database!"));
89  }
90 
91  string DBver;
92  if(needUpdateDB(m_db, DBver))
93  {
94  std::string dbVersion = TeDBVERSION;
95 
96  if(isLowerVersion(dbVersion, DBver))
97  {
98  close();
99 
100  throw te::da::Exception(TR_TERRALIB4("Cannot connect to database because the version of Terraview is lower than the version of the database!"));
101  }
102 
103  close();
104 
105  throw te::da::Exception(TR_TERRALIB4("The database must be converted to the model ") + dbVersion + "! \n");
106  }
107 
108 }
109 
111 {
112  delete m_db;
113  m_db = 0;
114 }
115 
117 {
118  return m_db != 0;
119 }
120 
122 {
123  return m_db != 0;
124 }
125 
127 {
128  return sm_capabilities;
129 }
130 
132 {
133  return sm_dialect;
134 }
135 
136 void terralib4::DataSource::create(const std::map<std::string, std::string>& dsInfo)
137 {
138  throw Exception(TR_TERRALIB4("This driver is read-only!"));
139 }
140 
141 void terralib4::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
142 {
143  throw Exception(TR_TERRALIB4("This driver is read-only!"));
144 }
145 
146 bool terralib4::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
147 {
148  std::vector<string> dbnames = getDataSourceNames(dsInfo);
149 
150  return std::find(dbnames.begin(), dbnames.end(), dsInfo.at("T4_DB_NAME")) != dbnames.end();
151 }
152 
153 std::vector<std::string> terralib4::DataSource::getDataSourceNames(const std::map<std::string, std::string>& dsInfo)
154 {
155  std::auto_ptr<TeDatabaseFactoryParams> params(terralib4::Convert2T4DatabaseParams(dsInfo));
156 
157  std::auto_ptr<TeDatabase> db(TeDatabaseFactory::make(*params.get()));
158 
159  std::vector<std::string> dbnames;
160 
161  db->showDatabases(params->host_, params->user_, params->password_, dbnames, params->port_);
162 
163  return dbnames;
164 }
165 
166 std::vector<std::string> terralib4::DataSource::getEncodings(const std::map<std::string, std::string>& dsInfo)
167 {
168  std::auto_ptr<TeDatabaseFactoryParams> params(terralib4::Convert2T4DatabaseParams(dsInfo));
169 
170  std::auto_ptr<TeDatabase> db(TeDatabaseFactory::make(*params.get()));
171 
172  std::vector<std::string> encodings;
173 
174  db->getEncodingList(encodings);
175 
176  return encodings;
177 }
178 
180 {
181  return m_db;
182 }
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:64
bool isValid() const
It checks if the data source is valid (available for using).
Definition: DataSource.cpp:121
DataSourceTransactor implementation for TerraLib 4.x API.
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:136
#define TR_TERRALIB4(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:119
~DataSource()
Virtual destructor.
Definition: DataSource.cpp:45
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
Definition: DataSource.cpp:131
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
Definition: DataSource.cpp:49
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:54
TeDatabase * getTerralib4Db()
Definition: DataSource.cpp:179
static te::da::SQLDialect * sm_dialect
Definition: DataSource.h:85
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
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:146
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:141
std::vector< std::string > getEncodings(const std::map< std::string, std::string > &dsInfo)
It gets the encodings for the data source.
Definition: DataSource.cpp:166
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:59
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
static te::da::DataSourceCapabilities sm_capabilities
Definition: DataSource.h:84
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
Definition: DataSource.cpp:116
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:70
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
#define TERRALIB4_DRIVER_IDENTIFIER
The Terralib 4 driver identifier string.
Definition: Config.h:84
Configuration flags for the TerraLib 4 driver.
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:153
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Definition: DataSource.cpp:126
void close()
It closes the data source and clears all the resources used by its internal communication channel...
Definition: DataSource.cpp:110