backends/oracle/session.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2007 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #define SOCI_ORACLE_SOURCE
9 #include "soci-oracle.h"
10 #include "error.h"
11 #include <cctype>
12 #include <cstdio>
13 #include <cstring>
14 #include <ctime>
15 #include <sstream>
16 
17 #ifdef _MSC_VER
18 #pragma warning(disable:4355)
19 #endif
20 
21 using namespace soci;
22 using namespace soci::details;
23 using namespace soci::details::oracle;
24 
25 oracle_session_backend::oracle_session_backend(std::string const & serviceName,
26  std::string const & userName, std::string const & password, int mode,
27  bool decimals_as_strings)
28  : envhp_(NULL), srvhp_(NULL), errhp_(NULL), svchp_(NULL), usrhp_(NULL)
29  , decimals_as_strings_(decimals_as_strings)
30 {
31  sword res;
32 
33  // create the environment
34  res = OCIEnvCreate(&envhp_, OCI_THREADED | OCI_ENV_NO_MUTEX,
35  0, 0, 0, 0, 0, 0);
36  if (res != OCI_SUCCESS)
37  {
38  throw soci_error("Cannot create environment");
39  }
40 
41  // create the server handle
42  res = OCIHandleAlloc(envhp_, reinterpret_cast<dvoid**>(&srvhp_),
43  OCI_HTYPE_SERVER, 0, 0);
44  if (res != OCI_SUCCESS)
45  {
46  clean_up();
47  throw soci_error("Cannot create server handle");
48  }
49 
50  // create the error handle
51  res = OCIHandleAlloc(envhp_, reinterpret_cast<dvoid**>(&errhp_),
52  OCI_HTYPE_ERROR, 0, 0);
53  if (res != OCI_SUCCESS)
54  {
55  clean_up();
56  throw soci_error("Cannot create error handle");
57  }
58 
59  // create the server context
60  sb4 serviceNameLen = static_cast<sb4>(serviceName.size());
61  res = OCIServerAttach(srvhp_, errhp_,
62  reinterpret_cast<text*>(const_cast<char*>(serviceName.c_str())),
63  serviceNameLen, OCI_DEFAULT);
64  if (res != OCI_SUCCESS)
65  {
66  std::string msg;
67  int errNum;
68  get_error_details(res, errhp_, msg, errNum);
69  clean_up();
70  throw oracle_soci_error(msg, errNum);
71  }
72 
73  // create service context handle
74  res = OCIHandleAlloc(envhp_, reinterpret_cast<dvoid**>(&svchp_),
75  OCI_HTYPE_SVCCTX, 0, 0);
76  if (res != OCI_SUCCESS)
77  {
78  clean_up();
79  throw soci_error("Cannot create service context");
80  }
81 
82  // set the server attribute in the context handle
83  res = OCIAttrSet(svchp_, OCI_HTYPE_SVCCTX, srvhp_, 0,
84  OCI_ATTR_SERVER, errhp_);
85  if (res != OCI_SUCCESS)
86  {
87  std::string msg;
88  int errNum;
89  get_error_details(res, errhp_, msg, errNum);
90  clean_up();
91  throw oracle_soci_error(msg, errNum);
92  }
93 
94  // allocate user session handle
95  res = OCIHandleAlloc(envhp_, reinterpret_cast<dvoid**>(&usrhp_),
96  OCI_HTYPE_SESSION, 0, 0);
97  if (res != OCI_SUCCESS)
98  {
99  clean_up();
100  throw soci_error("Cannot allocate user session handle");
101  }
102 
103  // set username attribute in the user session handle
104  sb4 userNameLen = static_cast<sb4>(userName.size());
105  res = OCIAttrSet(usrhp_, OCI_HTYPE_SESSION,
106  reinterpret_cast<dvoid*>(const_cast<char*>(userName.c_str())),
107  userNameLen, OCI_ATTR_USERNAME, errhp_);
108  if (res != OCI_SUCCESS)
109  {
110  clean_up();
111  throw soci_error("Cannot set username");
112  }
113 
114  // set password attribute
115  sb4 passwordLen = static_cast<sb4>(password.size());
116  res = OCIAttrSet(usrhp_, OCI_HTYPE_SESSION,
117  reinterpret_cast<dvoid*>(const_cast<char*>(password.c_str())),
118  passwordLen, OCI_ATTR_PASSWORD, errhp_);
119  if (res != OCI_SUCCESS)
120  {
121  clean_up();
122  throw soci_error("Cannot set password");
123  }
124 
125  // begin the session
126  res = OCISessionBegin(svchp_, errhp_, usrhp_,
127  OCI_CRED_RDBMS, mode);
128  if (res != OCI_SUCCESS)
129  {
130  std::string msg;
131  int errNum;
132  get_error_details(res, errhp_, msg, errNum);
133  clean_up();
134  throw oracle_soci_error(msg, errNum);
135  }
136 
137  // set the session in the context handle
138  res = OCIAttrSet(svchp_, OCI_HTYPE_SVCCTX, usrhp_,
139  0, OCI_ATTR_SESSION, errhp_);
140  if (res != OCI_SUCCESS)
141  {
142  std::string msg;
143  int errNum;
144  get_error_details(res, errhp_, msg, errNum);
145  clean_up();
146  throw oracle_soci_error(msg, errNum);
147  }
148 }
149 
151 {
152  clean_up();
153 }
154 
156 {
157  // This code is commented out because it causes one of the transaction
158  // tests in common_tests::test10() to fail with error 'Invalid handle'
159  // With the code commented out, all tests pass.
160  // sword res = OCITransStart(svchp_, errhp_, 0, OCI_TRANS_NEW);
161  // if (res != OCI_SUCCESS)
162  // {
163  // throworacle_soci_error(res, errhp_);
164  // }
165 }
166 
168 {
169  sword res = OCITransCommit(svchp_, errhp_, OCI_DEFAULT);
170  if (res != OCI_SUCCESS)
171  {
173  }
174 }
175 
177 {
178  sword res = OCITransRollback(svchp_, errhp_, OCI_DEFAULT);
179  if (res != OCI_SUCCESS)
180  {
182  }
183 }
184 
186 {
187  if (svchp_ != NULL && errhp_ != NULL && usrhp_ != NULL)
188  {
189  OCISessionEnd(svchp_, errhp_, usrhp_, OCI_DEFAULT);
190  }
191 
192  if (usrhp_) { OCIHandleFree(usrhp_, OCI_HTYPE_SESSION); }
193  if (svchp_) { OCIHandleFree(svchp_, OCI_HTYPE_SVCCTX); }
194  if (srvhp_)
195  {
196  OCIServerDetach(srvhp_, errhp_, OCI_DEFAULT);
197  OCIHandleFree(srvhp_, OCI_HTYPE_SERVER);
198  }
199  if (errhp_) { OCIHandleFree(errhp_, OCI_HTYPE_ERROR); }
200  if (envhp_) { OCIHandleFree(envhp_, OCI_HTYPE_ENV); }
201 }
202 
204 {
205  return new oracle_statement_backend(*this);
206 }
207 
209 {
210  return new oracle_rowid_backend(*this);
211 }
212 
214 {
215  return new oracle_blob_backend(*this);
216 }
oracle_session_backend(std::string const &serviceName, std::string const &userName, std::string const &password, int mode, bool decimals_as_strings=false)
virtual oracle_rowid_backend * make_rowid_backend()
void get_error_details(sword res, OCIError *errhp, std::string &msg, int &errNum)
void throw_oracle_soci_error(sword res, OCIError *errhp)
virtual oracle_statement_backend * make_statement_backend()
virtual oracle_blob_backend * make_blob_backend()


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