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