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-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 /*!
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/Translator.h"
28 //#include "../dataaccess/dataset/CheckConstraint.h"
29 //#include "../dataaccess/dataset/DataSet.h"
30 //#include "../dataaccess/dataset/ForeignKey.h"
31 //#include "../dataaccess/dataset/Index.h"
32 //#include "../dataaccess/dataset/PrimaryKey.h"
33 //#include "../dataaccess/dataset/Sequence.h"
34 //#include "../dataaccess/dataset/UniqueKey.h"
35 #include "../dataaccess/datasource/DataSourceTransactor.h"
36 //#include "../dataaccess/query/Select.h"
37 #include "../dataaccess/query/SQLDialect.h"
38 //#include "../dataaccess/utils/Utils.h"
39 //#include "../datatype/DateTimeProperty.h"
40 #include "../datatype/StringProperty.h"
41 //#include "../geometry/GeometryProperty.h"
42 //#include "../geometry/Envelope.h"
43 //#include "../geometry/Geometry.h"
44 #include "Connection.h"
45 #include "DataSource.h"
46 //#include "DataSet.h"
47 #include "Exception.h"
48 #include "Globals.h"
49 #include "Transactor.h"
50 #include "Utils.h"
51 
52 // STL
53 #include <cassert>
54 #include <iostream>
55 #include <memory>
56 
57 // Boost
58 #include <boost/filesystem.hpp>
59 #include <boost/format.hpp>
60 #include <boost/lexical_cast.hpp>
61 
62 inline void TESTHR(HRESULT hr)
63 {
64  if(FAILED(hr))
65  _com_issue_error(hr);
66 }
67 
69  : m_isOpened(false)
70 {
71  ::CoInitialize(0);
72 }
73 
75 {
76  ::CoUninitialize();
77 }
78 
79 std::string te::ado::DataSource::getType() const
80 {
81  return ADO_DRIVER_IDENTIFIER;
82 }
83 
84 const std::map<std::string, std::string>& te::ado::DataSource::getConnectionInfo() const
85 {
86  return m_connInfo;
87 }
88 
89 void te::ado::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
90 {
91  m_connInfo = connInfo;
92 }
93 
94 std::auto_ptr<te::da::DataSourceTransactor> te::ado::DataSource::getTransactor()
95 {
96  return std::auto_ptr<te::da::DataSourceTransactor>(new te::ado::Transactor(this));
97 }
98 
100 {
101 // assure we are in a closed state
102  close();
103 
104  std::string connInfo = MakeConnectionStr(m_connInfo);
105 
106  std::auto_ptr<Connection> conn(new te::ado::Connection(connInfo));
107 
108  loadGeometryColumnsCache(conn->getConn());
109 
110  m_isOpened = true;
111 }
112 
114 {
115  m_isOpened = false;
116 }
117 
119 {
120  return m_isOpened;
121 }
122 
124 {
125  return m_isOpened;
126 }
127 
129 {
132 }
133 
135 {
137 }
138 
139 const std::map<std::string, std::string>& te::ado::DataSource::getGeomColumns() const
140 {
141  return m_geomColumns;
142 }
143 
144 void te::ado::DataSource::registerGeometryColumn(const std::string& datasetName,
145  const std::string& geomColName)
146 {
147  boost::lock_guard<boost::mutex> lock(m_mtx);
148 
149  m_geomColumns[datasetName] = geomColName;
150 }
151 
152 bool te::ado::DataSource::isGeometryColumn(const std::string& datasetName,
153  const std::string& colName) const
154 {
155  boost::lock_guard<boost::mutex> lock(m_mtx);
156 
157  std::map<std::string, std::string>::const_iterator it = m_geomColumns.find(datasetName);
158 
159  if(it != m_geomColumns.end())
160  return it->second == colName;
161 
162  return false;
163 }
164 
165 void te::ado::DataSource::create(const std::map<std::string, std::string>& dsInfo)
166 {
167  m_connInfo = dsInfo;
168 
169  std::string connInfo = te::ado::MakeConnectionStr(dsInfo);
170 
171  // Create the new database
172  ADOX::_CatalogPtr pCatalog = 0;
173 
174  pCatalog.CreateInstance(__uuidof(ADOX::Catalog));
175 
176  try
177  {
178  pCatalog->Create(connInfo.c_str());
179  }
180  catch(_com_error& e)
181  {
182  throw Exception(TE_TR(e.Description()));
183  }
184 
185  std::map<std::string, std::string>::const_iterator it = dsInfo.find("CREATE_OGC_METADATA_TABLES");
186  std::map<std::string, std::string>::const_iterator it_end = dsInfo.end();
187 
188  if(it != it_end && it->second == "TRUE")
189  {
190  // Create the geometry_columns dataset
191  te::da::DataSetType* geomColsDt = new te::da::DataSetType("geometry_columns");
192 
193  geomColsDt->add(new te::dt::StringProperty("f_table_catalog", te::dt::VAR_STRING, 256));
194  geomColsDt->add(new te::dt::StringProperty("f_table_schema", te::dt::VAR_STRING, 256));
195  geomColsDt->add(new te::dt::StringProperty("f_table_name", te::dt::VAR_STRING, 256));
196  geomColsDt->add(new te::dt::StringProperty("f_geometry_column", te::dt::VAR_STRING, 256));
197  geomColsDt->add(new te::dt::SimpleProperty("coord_dimension", te::dt::INT32_TYPE));
198  geomColsDt->add(new te::dt::SimpleProperty("srid", te::dt::INT32_TYPE));
199  geomColsDt->add(new te::dt::StringProperty("type", te::dt::VAR_STRING, 30));
200 
201  std::map<std::string, std::string> op;
202 
203  createDataSet(geomColsDt, op);
204  }
205 
206  close();
207 }
208 
209 void te::ado::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
210 {
211  if(!exists(dsInfo))
212  throw Exception(TE_TR("The data source doesn't exist!"));
213 
214  std::map<std::string, std::string> info = dsInfo;
215 
216  boost::filesystem::path path(info["DB_NAME"]);
217 
218  if(boost::filesystem::remove(path) == false)
219  throw Exception(TE_TR("The data source could not be dropped!"));
220 }
221 
222 bool te::ado::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
223 {
224  std::map<std::string, std::string> info = dsInfo;
225 
226  boost::filesystem::path path(info["DB_NAME"]);
227 
228  return boost::filesystem::exists(path);
229 }
230 
231 std::vector<std::string> te::ado::DataSource::getDataSourceNames(const std::map<std::string, std::string>& dsInfo)
232 {
233  return std::vector<std::string>(); // The DataSource is a File.
234 }
235 
236 std::vector<te::common::CharEncoding> te::ado::DataSource::getEncodings(const std::map<std::string, std::string>& dsInfo)
237 {
238  return std::vector<te::common::CharEncoding>(); //TODO how?
239 }
240 
242 {
243  boost::lock_guard<boost::mutex> lock(m_mtx);
244 
245  m_geomColumns.clear();
246 
247  _RecordsetPtr recordset;
248 
249  TESTHR(recordset.CreateInstance(__uuidof(Recordset)));
250 
251  std::string query = "SELECT * FROM geometry_columns";
252 
253  try
254  {
255  recordset->Open(query.c_str(), _variant_t((IDispatch *)adoConn), adOpenDynamic, adLockReadOnly, adCmdText);
256 
257  while(!recordset->EndOfFile)
258  {
259  std::string tablename = (LPCSTR)(_bstr_t)recordset->GetFields()->GetItem("f_table_name")->GetValue();
260 
261  std::string columnName = (LPCSTR)(_bstr_t)recordset->GetFields()->GetItem("f_geometry_column")->GetValue();
262 
263  m_geomColumns[tablename] = columnName;
264 
265  recordset->MoveNext();
266  }
267  }
268  catch(_com_error& e)
269  {
270  throw Exception(TE_TR(e.Description()));
271  }
272 }
273 
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:128
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:209
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:123
void TESTHR(HRESULT hr)
Definition: DataSource.cpp:62
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:113
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:222
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:118
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:345
An exception class for ADO.
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:236
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:134
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:144
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:94
The type for string types: FIXED_STRING, VAR_STRING or STRING.
const std::map< std::string, std::string > & getGeomColumns() const
Definition: DataSource.cpp:139
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:165
~DataSource()
Virtual destructor.
Definition: DataSource.cpp:74
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:152
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
Definition: DataSource.cpp:79
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:89
void loadGeometryColumnsCache(_ConnectionPtr &adoConn)
Definition: DataSource.cpp:241
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:84
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:231
An static class with global definitions.
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:99