All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Connection.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/Connection.cpp
22 
23  \brief A class that implements a connection to a ADO database.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/Translator.h"
29 #include "Connection.h"
30 
31 // STL
32 #include <cassert>
33 #include <string>
34 
35 // Boost
36 #include <boost/format.hpp>
37 
38 inline void TESTHR(HRESULT hr)
39 {
40  if(FAILED(hr))
41  _com_issue_error(hr);
42 }
43 
44 te::ado::Connection::Connection(const std::string& conninfo)
45  : m_conn(0)
46 {
47  if(conninfo.empty())
48  return;
49 
50  _bstr_t connStr = conninfo.c_str();
51 
52  try
53  {
54  TESTHR(::CoInitialize(0));
55 
56  TESTHR(m_conn.CreateInstance(__uuidof(::Connection)));
57  TESTHR(m_conn->Open(connStr, "", "", -1));
58  }
59  catch(_com_error& e)
60  {
61  std::string err = TE_TR("ADO Connection: ");
62  err += (LPCSTR)e.ErrorMessage();
63  throw te::common::Exception(err);
64  }
65 
66  long status = m_conn->GetState();
67 
68  if(status != adStateOpen)
69  {
70  boost::format errmsg(TE_TR("It was not possible to create a connection to the given data source due to the following error: %1%."));
71 
72  errmsg = errmsg % m_conn->GetErrors()->GetItem(0)->GetDescription();
73 
74  m_conn->Close();
75 
76  m_conn = 0;
77 
78  throw te::common::Exception(errmsg.str());
79  }
80 }
81 
83 {
84  if (m_conn)
85  if (m_conn->GetState() == ::adStateOpen)
86  m_conn->Close();
87 
88  ::CoUninitialize();
89 }
90 
91 _RecordsetPtr te::ado::Connection::query(const std::string& query, bool connected)
92 {
93  _RecordsetPtr recordset;
94 
95  TESTHR(recordset.CreateInstance(__uuidof(Recordset)));
96 
97  try
98  {
99  //if(connected)
100  //recordset->Open(query.c_str(), _variant_t((IDispatch *)m_conn), adOpenDynamic, adLockReadOnly, adCmdText);
101  //else
102  recordset->Open(query.c_str(), _variant_t((IDispatch *)m_conn), adOpenStatic, adLockReadOnly, adCmdText);
103  }
104  catch(_com_error& e)
105  {
106  std::string err = TE_TR("ADO Connection: ");
107  err += (LPCSTR)e.ErrorMessage();
108  throw te::common::Exception(err);
109  }
110 
111  return recordset;
112 }
113 
114 void te::ado::Connection::execute(const std::string& command)
115 {
116  try
117  {
118  m_conn->Execute(_bstr_t(command.c_str()),0, adCmdText);
119  }
120  catch(_com_error& e)
121  {
122  std::string err = TE_TR("ADO Connection: ");
123  err += (LPCSTR)e.ErrorMessage();
124  throw te::common::Exception(err);
125  }
126 }
127 
129 {
130  return m_conn != 0;
131 }
~Connection()
Destructor.
Definition: Connection.cpp:82
Connection(const std::string &conninfo)
Constructor.
Definition: Connection.cpp:44
void TESTHR(HRESULT hr)
Definition: Connection.cpp:38
_RecordsetPtr query(const std::string &query, bool connected=false)
It queries the database.
Definition: Connection.cpp:91
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
A class that implements a connection to a ADO database.
Definition: Connection.h:60
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
_ConnectionPtr m_conn
The ADO real connection handle.
Definition: Connection.h:109
void execute(const std::string &command)
It executes the given SQL command and throws away the result.
Definition: Connection.cpp:114
A class that implements a connection to a ADO database.