Safir SDK Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Connection.h
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * Copyright Saab AB, 2005-2013 (http://safir.sourceforge.net)
4 *
5 * Created by: Jörgen Johansson / stjrjo
6 *
7 *******************************************************************************
8 *
9 * This file is part of Safir SDK Core.
10 *
11 * Safir SDK Core is free software: you can redistribute it and/or modify
12 * it under the terms of version 3 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * Safir SDK Core is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Safir SDK Core. If not, see <http://www.gnu.org/licenses/>.
22 *
23 ******************************************************************************/
24 #ifndef Safir_Databases_Connection_h
25 #define Safir_Databases_Connection_h
26 
28 #include "Safir/Databases/Odbc/Internal/InternalDefs.h"
30 #include <vector>
31 #include <boost/noncopyable.hpp>
32 
33 namespace Safir
34 {
35 namespace Databases
36 {
37 namespace Odbc
38 {
39 class Environment;
40 class Statement;
41 
42 #ifdef _MSC_VER
43 #pragma warning(push)
44 #pragma warning(disable: 4275)
45 #pragma warning(disable: 4251)
46 #endif
47 
53 class OLIB_API Connection : private boost::noncopyable
54 {
55 public:
59  m_hConnection(SQL_NULL_HDBC),
60  m_bIsConnected(false)
61  {
62  // This bit of code will make sure that the client is using the
63  // same size for SQLWCHAR as the library.
64  CheckSQLWCHARSize(sizeof(SQLWCHAR));
65  }
66 
69  ~Connection();
70 
78  void Alloc(const Environment & environment);
79 
84  void Free();
85 
94  void Connect(const std::wstring & wszConnectionString);
95 
104  void Connect(char * cszConnectionString);
105 
116  void Disconnect();
117 
126  void UseAutoTransactions();
127 
136  void UseManualTransactions();
137 
146  void Commit();
147 
156  void Rollback();
157 
166  void SetConnectAttr(long lAttribute, long lValue);
167 
176  void SetConnectAttr(long lAttribute, const std::wstring & wszValue);
177 
186  void GetConnectAttr(long lAttribute, long & lValue) const;
187 
197  void GetConnectAttr(long lAttribute, wchar_t * wszValue, unsigned long ulLength) const;
198 
209  bool GetDiagRec(short sRecNumber,
210  std::wstring & SqlState,
211  boost::int32_t & NativeError,
212  std::wstring & MessageText,
213  bool & bDataRead) const;
214 
219  bool IsValid() const;
220 
225  bool IsConnected() const;
226 
231  SQLHDBC Handle() const;
232 private:
233  SQLHDBC m_hConnection;
234  bool m_bIsConnected;
235 
236  typedef std::pair<std::wstring,std::wstring> StateMessagePair;
237  //returns pair of SQLState and MessageText
238  const StateMessagePair GetDiagRec() const;
239 
240  void AddStatement(Statement * pStatement);
241  void RemoveStatement(Statement * pStatement);
242  void EndTran(short sCompletionType);
243  void ThrowReconnectException( const std::wstring & fileName,
244  const Safir::Dob::Typesystem::Int64 lineNumber) const;
245  void ThrowReconnectException( SQLSMALLINT HandleType,
246  SQLHANDLE Handle,
247  const std::wstring & fileName,
248  const Safir::Dob::Typesystem::Int64 lineNumber) const;
249 
250  std::vector<Statement *> m_statements;
251 
253 
254  static void CheckSQLWCHARSize(const size_t size);
255 };
256 
257 #ifdef _MSC_VER
258 #pragma warning(pop)
259 #endif
260 
261 inline
263 {
264  SetConnectAttr(SQL_ATTR_AUTOCOMMIT, static_cast<long>(SQL_AUTOCOMMIT_ON));
265 }
266 
267 inline
269 {
270  SetConnectAttr(SQL_ATTR_AUTOCOMMIT, static_cast<long>(SQL_AUTOCOMMIT_OFF));
271 }
272 
273 inline
275 {
276  EndTran(SQL_COMMIT);
277 }
278 
279 inline
281 {
282  EndTran(SQL_ROLLBACK);
283 }
284 
285 inline
287 {
288  return m_hConnection != SQL_NULL_HDBC;
289 }
290 
291 inline
293 {
294  return m_bIsConnected;
295 }
296 
297 inline
298 SQLHDBC Connection::Handle() const
299 {
300  return m_hConnection;
301 }
302 
303 } // End namespace Odbc
304 
305 } // End namespace Databases
306 
307 } // End namespace Safir
308 
309 #endif //Safir_Databases_Connection_h
void Rollback()
Remove all changes made in this transaction.
Definition: Connection.h:280
void UseAutoTransactions()
Set odbc to automatically commit each transaction after a successful query.
Definition: Connection.h:262
DotsC_Int64 Int64
64 bit integer type.
Definition: Defs.h:70
void Commit()
Commits the changes permanently made in this transaction.
Definition: Connection.h:274
bool IsValid() const
Checks if this connection is a valid allocated connection.
Definition: Connection.h:286
Connection()
Constructor.
Definition: Connection.h:58
void SetConnectAttr(long lAttribute, long lValue)
Sets a value for a ODBC connection attribute.
The Statement class models a statement made to the database engine and one statement object should be...
Definition: Statement.h:59
bool IsConnected() const
Checks if a connection has been established.
Definition: Connection.h:292
The Environment class models the necessary setup each application neeeds in order to access the datab...
Definition: Environment.h:49
SQLHDBC Handle() const
Returns the ODBC Handle of the environment.
Definition: Connection.h:298
void UseManualTransactions()
Set odbc to require manual commits to end transactions.
Definition: Connection.h:268
The Connection class models a connection made to the database engine.
Definition: Connection.h:53