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 /*!
21  \file terralib/ado/DataSource.cpp
22 
23  \brief Implementation of the data source class for the ADO driver.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/Translator.h"
29 #include "../dataaccess/datasource/DataSourceTransactor.h"
30 #include "../dataaccess/query/SQLDialect.h"
31 #include "../datatype/StringProperty.h"
32 #include "Connection.h"
33 #include "DataSource.h"
34 
35 #include "Globals.h"
36 #include "Transactor.h"
37 #include "Utils.h"
38 
39 // STL
40 #include <cassert>
41 #include <iostream>
42 #include <memory>
43 
44 // Boost
45 #include <boost/filesystem.hpp>
46 #include <boost/format.hpp>
47 #include <boost/lexical_cast.hpp>
48 
49 inline void TESTHR(HRESULT hr)
50 {
51  if(FAILED(hr))
52  _com_issue_error(hr);
53 }
54 
56  : m_isOpened(false)
57 {
58  //::CoInitialize(0);
59 }
60 
62 {
63  //::CoUninitialize();
64 }
65 
66 std::string te::ado::DataSource::getType() const
67 {
68  return ADO_DRIVER_IDENTIFIER;
69 }
70 
71 const std::map<std::string, std::string>& te::ado::DataSource::getConnectionInfo() const
72 {
73  return m_connInfo;
74 }
75 
76 void te::ado::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
77 {
78  m_connInfo = connInfo;
79 }
80 
81 std::auto_ptr<te::da::DataSourceTransactor> te::ado::DataSource::getTransactor()
82 {
83  return std::auto_ptr<te::da::DataSourceTransactor>(new te::ado::Transactor(this));
84 }
85 
87 {
88 // assure we are in a closed state
89  close();
90 
91  std::string connInfo = MakeConnectionStr(m_connInfo);
92 
93  std::auto_ptr<Connection> conn(new te::ado::Connection(connInfo));
94 
95  _ConnectionPtr connection = conn->getConn();
96 
97  loadGeometryColumnsCache(connection);
98 
99  m_isOpened = true;
100 }
101 
103 {
104  m_isOpened = false;
105 }
106 
108 {
109  return m_isOpened;
110 }
111 
113 {
114  return m_isOpened;
115 }
116 
118 {
121 }
122 
124 {
126 }
127 
128 const std::map<std::string, std::string>& te::ado::DataSource::getGeomColumns() const
129 {
130  return m_geomColumns;
131 }
132 
133 void te::ado::DataSource::registerGeometryColumn(const std::string& datasetName,
134  const std::string& geomColName)
135 {
136  boost::lock_guard<boost::mutex> lock(m_mtx);
137 
138  m_geomColumns[datasetName] = geomColName;
139 }
140 
141 bool te::ado::DataSource::isGeometryColumn(const std::string& datasetName,
142  const std::string& colName) const
143 {
144  boost::lock_guard<boost::mutex> lock(m_mtx);
145 
146  std::map<std::string, std::string>::const_iterator it = m_geomColumns.find(datasetName);
147 
148  if(it != m_geomColumns.end())
149  return it->second == colName;
150 
151  return false;
152 }
153 
154 void te::ado::DataSource::create(const std::map<std::string, std::string>& dsInfo)
155 {
156  m_connInfo = dsInfo;
157 
158  std::string connInfo = te::ado::MakeConnectionStr(dsInfo);
159 
160  // Create the new database
161  ADOX::_CatalogPtr pCatalog = 0;
162 
163  pCatalog.CreateInstance(__uuidof(ADOX::Catalog));
164 
165  try
166  {
167  pCatalog->Create(connInfo.c_str());
168  }
169  catch(_com_error& e)
170  {
171  throw te::common::Exception((LPCSTR)e.ErrorMessage());
172  }
173 
174  std::map<std::string, std::string>::const_iterator it = dsInfo.find("CREATE_OGC_METADATA_TABLES");
175  std::map<std::string, std::string>::const_iterator it_end = dsInfo.end();
176 
177  if(it != it_end && it->second == "TRUE")
178  {
179  // Create the geometry_columns dataset
180  te::da::DataSetType* geomColsDt = new te::da::DataSetType("geometry_columns");
181 
182  geomColsDt->add(new te::dt::StringProperty("f_table_catalog", te::dt::VAR_STRING, 256));
183  geomColsDt->add(new te::dt::StringProperty("f_table_schema", te::dt::VAR_STRING, 256));
184  geomColsDt->add(new te::dt::StringProperty("f_table_name", te::dt::VAR_STRING, 256));
185  geomColsDt->add(new te::dt::StringProperty("f_geometry_column", te::dt::VAR_STRING, 256));
186  geomColsDt->add(new te::dt::SimpleProperty("coord_dimension", te::dt::INT32_TYPE));
187  geomColsDt->add(new te::dt::SimpleProperty("srid", te::dt::INT32_TYPE));
188  geomColsDt->add(new te::dt::StringProperty("type", te::dt::VAR_STRING, 30));
189 
190  std::map<std::string, std::string> op;
191 
192  createDataSet(geomColsDt, op);
193  }
194 
195  close();
196 }
197 
198 void te::ado::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
199 {
200  if(!exists(dsInfo))
201  throw te::common::Exception(TE_TR("The data source doesn't exist!"));
202 
203  std::map<std::string, std::string> info = dsInfo;
204 
205  boost::filesystem::path path(info["DB_NAME"]);
206 
207  if(boost::filesystem::remove(path) == false)
208  throw te::common::Exception(TE_TR("The data source could not be dropped!"));
209 }
210 
211 bool te::ado::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
212 {
213  std::map<std::string, std::string> info = dsInfo;
214 
215  boost::filesystem::path path(info["DB_NAME"]);
216 
217  return boost::filesystem::exists(path);
218 }
219 
220 std::vector<std::string> te::ado::DataSource::getDataSourceNames(const std::map<std::string, std::string>&)
221 {
222  return std::vector<std::string>(); // The DataSource is a File.
223 }
224 
225 std::vector<te::common::CharEncoding> te::ado::DataSource::getEncodings(const std::map<std::string, std::string>&)
226 {
227  return std::vector<te::common::CharEncoding>(); //TODO how?
228 }
229 
231 {
232  boost::lock_guard<boost::mutex> lock(m_mtx);
233 
234  m_geomColumns.clear();
235 
236  _RecordsetPtr recordset;
237 
238  TESTHR(recordset.CreateInstance(__uuidof(Recordset)));
239 
240  std::string query = "SELECT * FROM geometry_columns";
241 
242  try
243  {
244  recordset->Open(query.c_str(), _variant_t((IDispatch *)adoConn), adOpenDynamic, adLockReadOnly, adCmdText);
245 
246  while(!recordset->EndOfFile)
247  {
248  std::string tablename = (LPCSTR)(_bstr_t)recordset->GetFields()->GetItem("f_table_name")->GetValue();
249 
250  std::string columnName = (LPCSTR)(_bstr_t)recordset->GetFields()->GetItem("f_geometry_column")->GetValue();
251 
252  m_geomColumns[tablename] = columnName;
253 
254  recordset->MoveNext();
255  }
256  }
257  catch(_com_error& e)
258  {
259  throw te::common::Exception((LPCSTR)e.ErrorMessage());
260  }
261 }
262 
The transactor class for the Microsoft Access driver.
Definition: Transactor.h:62
static te::da::DataSourceCapabilities * sm_capabilities
The query dialect supported by ADO driver.
Definition: Globals.h:59
#define ADO_DRIVER_IDENTIFIER
The ADO driver identifier string.
Definition: Config.h:88
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Definition: DataSource.cpp:117
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:198
An atomic property like an integer or double.
bool isValid() const
It checks if the data source is valid (available for using).
Definition: DataSource.cpp:112
void TESTHR(HRESULT hr)
Definition: DataSource.cpp:49
A class that models the description of a dataset.
Definition: DataSetType.h:72
void close()
It closes the data source and clears all the resources used by its internal communication channel...
Definition: DataSource.cpp:102
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:211
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
Definition: DataSource.cpp:107
std::string MakeConnectionStr(const std::map< std::string, std::string > &dsInfo)
Create a connection string based on a map.
Definition: Utils.cpp:75
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
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:225
A class that implements a connection to a ADO database.
Definition: Connection.h:60
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
Definition: DataSource.cpp:123
static te::da::SQLDialect * sm_queryDialect
The query dialect supported by ADO driver.
Definition: Globals.h:58
void registerGeometryColumn(const std::string &datasetName, const std::string &geomColName)
Definition: DataSource.cpp:133
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:81
The type for string types: FIXED_STRING, VAR_STRING or STRING.
const std::map< std::string, std::string > & getGeomColumns() const
Definition: DataSource.cpp:128
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:154
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
~DataSource()
Virtual destructor.
Definition: DataSource.cpp:61
Implementation of the data source class for the ADO driver.
DataSourceTransactor class implementation for Microsoft Access driver.
void add(Constraint *c)
It adds a new constraint.
bool isGeometryColumn(const std::string &datasetName, const std::string &colName) const
Definition: DataSource.cpp:141
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
Definition: DataSource.cpp:66
Utility functions for ADO.
A class that implements a connection to a ADO database.
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:76
void loadGeometryColumnsCache(_ConnectionPtr &adoConn)
Definition: DataSource.cpp:230
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:71
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:220
An static class with global definitions.
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:86