All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Connection.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/Connection.cpp
22 
23  \brief A class that implements a connection to a ADO database.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "Connection.h"
29 #include "Exception.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  m_conn.CreateInstance(__uuidof(::Connection));
55  TESTHR(m_conn->Open(connStr, "", "", -1));
56  }
57  catch(_com_error& e)
58  {
59  throw Exception(TR_ADO(e.Description()));
60  }
61 
62  long status = m_conn->GetState();
63 
64  if(status != adStateOpen)
65  {
66  boost::format errmsg(TR_ADO("It was not possible to create a connection to the given data source due to the following error: %1%."));
67 
68  errmsg = errmsg % m_conn->GetErrors()->GetItem(0)->GetDescription();
69 
70  m_conn->Close();
71 
72  m_conn = 0;
73 
74  throw Exception(errmsg.str());
75  }
76 }
77 
79 {
80  if (m_conn)
81  if (m_conn->GetState() == ::adStateOpen)
82  m_conn->Close();
83 }
84 
85 _RecordsetPtr te::ado::Connection::query(const std::string& query, bool connected)
86 {
87  _RecordsetPtr recordset;
88 
89  TESTHR(recordset.CreateInstance(__uuidof(Recordset)));
90 
91  try
92  {
93  //if(connected)
94  //recordset->Open(query.c_str(), _variant_t((IDispatch *)m_conn), adOpenDynamic, adLockReadOnly, adCmdText);
95  //else
96  recordset->Open(query.c_str(), _variant_t((IDispatch *)m_conn), adOpenStatic, adLockReadOnly, adCmdText);
97  }
98  catch(_com_error& e)
99  {
100  throw Exception(TR_ADO(e.Description()));
101  }
102 
103  return recordset;
104 }
105 
106 void te::ado::Connection::execute(const std::string& command)
107 {
108  try
109  {
110  m_conn->Execute(_bstr_t(command.c_str()),0, adCmdText);
111  }
112  catch(_com_error& e)
113  {
114  throw Exception(TR_ADO(e.Description()));
115  }
116 }
117 
119 {
120  return m_conn != 0;
121 }
_ConnectionPtr m_conn
The ADO real connection handle.
Definition: Connection.h:109
A class that implements a connection to a ADO database.
~Connection()
Destructor.
Definition: Connection.cpp:78
void execute(const std::string &command)
It executes the given SQL command and throws away the result.
Definition: Connection.cpp:106
void TESTHR(HRESULT hr)
Definition: Connection.cpp:38
Connection(const std::string &conninfo)
Constructor.
Definition: Connection.cpp:44
An exception class for ADO.
_RecordsetPtr query(const std::string &query, bool connected=false)
It queries the database.
Definition: Connection.cpp:85
A class that implements a connection to a ADO database.
Definition: Connection.h:60
#define TR_ADO(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:126