backends/db2/session.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2011-2013 Denis Chapligin
3 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #define SOCI_DB2_SOURCE
10 #include "soci-db2.h"
11 #include <connection-parameters.h>
12 
13 #ifdef _MSC_VER
14 #pragma warning(disable:4355)
15 #endif
16 
17 using namespace soci;
18 using namespace soci::details;
19 
20 const std::string db2_soci_error::sqlState(std::string const & msg,const SQLSMALLINT htype,const SQLHANDLE hndl) {
21  std::ostringstream ss(msg, std::ostringstream::app);
22 
23 
24  SQLCHAR message[SQL_MAX_MESSAGE_LENGTH + 1];
25  SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
26  SQLINTEGER sqlcode;
27  SQLSMALLINT length;
28 
29  if ( SQLGetDiagRec(htype,
30  hndl,
31  1,
32  sqlstate,
33  &sqlcode,
34  message,
35  SQL_MAX_MESSAGE_LENGTH + 1,
36  &length) == SQL_SUCCESS ) {
37  ss<<" SQLMESSAGE: ";
38  ss<<message;
39  }
40  return ss.str();
41 }
42 
43 void db2_session_backend::parseKeyVal(std::string const & keyVal) {
44  size_t delimiter=keyVal.find_first_of("=");
45  std::string key=keyVal.substr(0,delimiter);
46  std::string value=keyVal.substr(delimiter+1,keyVal.length());
47 
48  if (!key.compare("DSN")) {
49  this->dsn=value;
50  }
51  if (!key.compare("Uid")) {
52  this->username=value;
53  }
54  if (!key.compare("Pwd")) {
55  this->password=value;
56  }
57  this->autocommit=true; //Default value
58  if (!key.compare("autocommit")) {
59  if (!value.compare("off")) {
60  this->autocommit=false;
61  }
62  }
63 }
64 
65 /* DSN=SAMPLE;Uid=db2inst1;Pwd=db2inst1;AutoCommit=off */
67  std::string processingString(connectString);
68  size_t delimiter=processingString.find_first_of(";");
69  while(delimiter!=std::string::npos) {
70  std::string keyVal=processingString.substr(0,delimiter);
71  parseKeyVal(keyVal);
72  processingString=processingString.erase(0,delimiter+1);
73  delimiter=processingString.find_first_of(";");
74  }
75  if (!processingString.empty()) {
76  parseKeyVal(processingString);
77  }
78 }
79 
82  in_transaction(false)
83 {
85  SQLRETURN cliRC = SQL_SUCCESS;
86 
87  /* Prepare handles */
88  cliRC = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&hEnv);
89  if (cliRC != SQL_SUCCESS) {
90  throw db2_soci_error("Error while allocating the enironment handle",cliRC);
91  }
92 
93  cliRC = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
94  if (cliRC != SQL_SUCCESS) {
95  std::string msg=db2_soci_error::sqlState("Error while allocating the connection handle",SQL_HANDLE_ENV,hEnv);
96  SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
97  throw db2_soci_error(msg,cliRC);
98  }
99 
100  /* Set autocommit */
101  if(this->autocommit) {
102  cliRC = SQLSetConnectAttr(hDbc,SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, SQL_NTS);
103  } else {
104  cliRC = SQLSetConnectAttr(hDbc,SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, SQL_NTS);
105  }
106  if (cliRC != SQL_SUCCESS) {
107  std::string msg=db2_soci_error::sqlState("Error while setting autocommit attribute",SQL_HANDLE_DBC,hDbc);
108  SQLFreeHandle(SQL_HANDLE_DBC,hDbc);
109  SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
110  throw db2_soci_error(msg,cliRC);
111  }
112 
113  /* Connect to database */
114  cliRC = SQLConnect(hDbc, const_cast<SQLCHAR *>((const SQLCHAR *) dsn.c_str()), SQL_NTS,
115  const_cast<SQLCHAR *>((const SQLCHAR *) username.c_str()), SQL_NTS,
116  const_cast<SQLCHAR *>((const SQLCHAR *) password.c_str()), SQL_NTS);
117  if (cliRC != SQL_SUCCESS) {
118  std::string msg=db2_soci_error::sqlState("Error connecting to database",SQL_HANDLE_DBC,hDbc);
119  SQLFreeHandle(SQL_HANDLE_DBC,hDbc);
120  SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
121  throw db2_soci_error(msg,cliRC);
122  }
123 }
124 
126 {
127  clean_up();
128 }
129 
131 {
132  // In DB2, transations begin implicitly; however, autocommit must be disabled for the duration of the transaction
133  if(autocommit)
134  {
135  SQLRETURN cliRC = SQLSetConnectAttr(hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_OFF, SQL_NTS);
136  if (cliRC != SQL_SUCCESS && cliRC != SQL_SUCCESS_WITH_INFO)
137  {
138  std::string msg=db2_soci_error::sqlState("Clearing the autocommit attribute failed", SQL_HANDLE_DBC, hDbc);
139  SQLFreeHandle(SQL_HANDLE_DBC,hDbc);
140  SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
141  throw db2_soci_error(msg,cliRC);
142  }
143  }
144 
145  in_transaction = true;
146 }
147 
149 {
150  if (!autocommit || in_transaction) {
151  in_transaction = false;
152  SQLRETURN cliRC = SQLEndTran(SQL_HANDLE_DBC,hDbc,SQL_COMMIT);
153  if(autocommit)
154  {
155  SQLRETURN cliRC2 = SQLSetConnectAttr(hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_ON, SQL_NTS);
156  if ((cliRC == SQL_SUCCESS || cliRC == SQL_SUCCESS_WITH_INFO) &&
157  cliRC2 != SQL_SUCCESS && cliRC2 != SQL_SUCCESS_WITH_INFO)
158  {
159  std::string msg=db2_soci_error::sqlState("Setting the autocommit attribute failed", SQL_HANDLE_DBC, hDbc);
160  SQLFreeHandle(SQL_HANDLE_DBC,hDbc);
161  SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
162  throw db2_soci_error(msg,cliRC);
163  }
164  }
165  if (cliRC != SQL_SUCCESS && cliRC != SQL_SUCCESS_WITH_INFO) {
166  throw db2_soci_error("Commit failed",cliRC);
167  }
168  }
169 }
170 
172 {
173  if (!autocommit || in_transaction) {
174  in_transaction = false;
175  SQLRETURN cliRC = SQLEndTran(SQL_HANDLE_DBC,hDbc,SQL_ROLLBACK);
176  if(autocommit)
177  {
178  SQLRETURN cliRC2 = SQLSetConnectAttr(hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_ON, SQL_NTS);
179  if ((cliRC == SQL_SUCCESS || cliRC == SQL_SUCCESS_WITH_INFO) &&
180  cliRC2 != SQL_SUCCESS && cliRC2 != SQL_SUCCESS_WITH_INFO)
181  {
182  std::string msg=db2_soci_error::sqlState("Setting the autocommit attribute failed", SQL_HANDLE_DBC, hDbc);
183  SQLFreeHandle(SQL_HANDLE_DBC,hDbc);
184  SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
185  throw db2_soci_error(msg,cliRC);
186  }
187  }
188  if (cliRC != SQL_SUCCESS && cliRC != SQL_SUCCESS_WITH_INFO) {
189  throw db2_soci_error("Rollback failed",cliRC);
190  }
191  }
192 }
193 
195 {
196  // if a transaction is in progress, it will automatically be rolled back upon when the connection is disconnected/freed
197  in_transaction = false;
198 
199  SQLDisconnect(hDbc);
200  SQLFreeHandle(SQL_HANDLE_DBC,hDbc);
201  SQLFreeHandle(SQL_HANDLE_ENV,hEnv);
202 }
203 
205 {
206  return new db2_statement_backend(*this);
207 }
208 
210 {
211  return new db2_rowid_backend(*this);
212 }
213 
215 {
216  return new db2_blob_backend(*this);
217 }
static const std::string sqlState(std::string const &msg, const SQLSMALLINT htype, const SQLHANDLE hndl)
db2_rowid_backend * make_rowid_backend()
std::string const & get_connect_string() const
void parseConnectString(std::string const &)
db2_statement_backend * make_statement_backend()
db2_blob_backend * make_blob_backend()
std::string connectString
Definition: test-db2.cpp:21
db2_session_backend(connection_parameters const &parameters)
void parseKeyVal(std::string const &)
std::vector< ISM::CombinatorialTrainerParameters > parameters


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41