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) 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  loadGeometryColumnsCache(conn->getConn());
96 
97  m_isOpened = true;
98 }
99 
101 {
102  m_isOpened = false;
103 }
104 
106 {
107  return m_isOpened;
108 }
109 
111 {
112  return m_isOpened;
113 }
114 
116 {
119 }
120 
122 {
124 }
125 
126 const std::map<std::string, std::string>& te::ado::DataSource::getGeomColumns() const
127 {
128  return m_geomColumns;
129 }
130 
131 void te::ado::DataSource::registerGeometryColumn(const std::string& datasetName,
132  const std::string& geomColName)
133 {
134  boost::lock_guard<boost::mutex> lock(m_mtx);
135 
136  m_geomColumns[datasetName] = geomColName;
137 }
138 
139 bool te::ado::DataSource::isGeometryColumn(const std::string& datasetName,
140  const std::string& colName) const
141 {
142  boost::lock_guard<boost::mutex> lock(m_mtx);
143 
144  std::map<std::string, std::string>::const_iterator it = m_geomColumns.find(datasetName);
145 
146  if(it != m_geomColumns.end())
147  return it->second == colName;
148 
149  return false;
150 }
151 
152 void te::ado::DataSource::create(const std::map<std::string, std::string>& dsInfo)
153 {
154  m_connInfo = dsInfo;
155 
156  std::string connInfo = te::ado::MakeConnectionStr(dsInfo);
157 
158  // Create the new database
159  ADOX::_CatalogPtr pCatalog = 0;
160 
161  pCatalog.CreateInstance(__uuidof(ADOX::Catalog));
162 
163  try
164  {
165  pCatalog->Create(connInfo.c_str());
166  }
167  catch(_com_error& e)
168  {
169  throw te::common::Exception((LPCSTR)e.ErrorMessage());
170  }
171 
172  std::map<std::string, std::string>::const_iterator it = dsInfo.find("CREATE_OGC_METADATA_TABLES");
173  std::map<std::string, std::string>::const_iterator it_end = dsInfo.end();
174 
175  if(it != it_end && it->second == "TRUE")
176  {
177  // Create the geometry_columns dataset
178  te::da::DataSetType* geomColsDt = new te::da::DataSetType("geometry_columns");
179 
180  geomColsDt->add(new te::dt::StringProperty("f_table_catalog", te::dt::VAR_STRING, 256));
181  geomColsDt->add(new te::dt::StringProperty("f_table_schema", te::dt::VAR_STRING, 256));
182  geomColsDt->add(new te::dt::StringProperty("f_table_name", te::dt::VAR_STRING, 256));
183  geomColsDt->add(new te::dt::StringProperty("f_geometry_column", te::dt::VAR_STRING, 256));
184  geomColsDt->add(new te::dt::SimpleProperty("coord_dimension", te::dt::INT32_TYPE));
185  geomColsDt->add(new te::dt::SimpleProperty("srid", te::dt::INT32_TYPE));
186  geomColsDt->add(new te::dt::StringProperty("type", te::dt::VAR_STRING, 30));
187 
188  std::map<std::string, std::string> op;
189 
190  createDataSet(geomColsDt, op);
191  }
192 
193  close();
194 }
195 
196 void te::ado::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
197 {
198  if(!exists(dsInfo))
199  throw te::common::Exception(TE_TR("The data source doesn't exist!"));
200 
201  std::map<std::string, std::string> info = dsInfo;
202 
203  boost::filesystem::path path(info["DB_NAME"]);
204 
205  if(boost::filesystem::remove(path) == false)
206  throw te::common::Exception(TE_TR("The data source could not be dropped!"));
207 }
208 
209 bool te::ado::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
210 {
211  std::map<std::string, std::string> info = dsInfo;
212 
213  boost::filesystem::path path(info["DB_NAME"]);
214 
215  return boost::filesystem::exists(path);
216 }
217 
218 std::vector<std::string> te::ado::DataSource::getDataSourceNames(const std::map<std::string, std::string>& dsInfo)
219 {
220  return std::vector<std::string>(); // The DataSource is a File.
221 }
222 
223 std::vector<te::common::CharEncoding> te::ado::DataSource::getEncodings(const std::map<std::string, std::string>& dsInfo)
224 {
225  return std::vector<te::common::CharEncoding>(); //TODO how?
226 }
227 
229 {
230  boost::lock_guard<boost::mutex> lock(m_mtx);
231 
232  m_geomColumns.clear();
233 
234  _RecordsetPtr recordset;
235 
236  TESTHR(recordset.CreateInstance(__uuidof(Recordset)));
237 
238  std::string query = "SELECT * FROM geometry_columns";
239 
240  try
241  {
242  recordset->Open(query.c_str(), _variant_t((IDispatch *)adoConn), adOpenDynamic, adLockReadOnly, adCmdText);
243 
244  while(!recordset->EndOfFile)
245  {
246  std::string tablename = (LPCSTR)(_bstr_t)recordset->GetFields()->GetItem("f_table_name")->GetValue();
247 
248  std::string columnName = (LPCSTR)(_bstr_t)recordset->GetFields()->GetItem("f_geometry_column")->GetValue();
249 
250  m_geomColumns[tablename] = columnName;
251 
252  recordset->MoveNext();
253  }
254  }
255  catch(_com_error& e)
256  {
257  throw te::common::Exception((LPCSTR)e.ErrorMessage());
258  }
259 }
260 
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:91
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Definition: DataSource.cpp:115
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:196
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:110
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:100
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:209
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:105
std::string MakeConnectionStr(const std::map< std::string, std::string > &dsInfo)
Create a connection string based on a map.
Definition: Utils.cpp:82
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
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:223
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:121
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:131
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:126
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:152
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:139
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:228
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:218
An static class with global definitions.
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:86