OCILIB (C and C++ Driver for Oracle)  4.7.6
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Exception.hpp
1 /*
2  * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI)
3  *
4  * Website: http://www.ocilib.net
5  *
6  * Copyright (c) 2007-2023 Vincent ROGIER <vince.rogier@ocilib.net>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 
23 #include "ocilibcpp/types.hpp"
24 
25 namespace ocilib
26 {
27 inline Exception::Exception() noexcept
28  : _what(nullptr),
29  _pStatement(nullptr),
30  _pConnnection(nullptr),
31  _row(0),
32  _type(static_cast<ExceptionType::Type>(0)),
33  _errLib(0),
34  _errOracle(0)
35 {
36 
37 }
38 
39 inline Exception::Exception(OCI_Error *err) noexcept
40  : _what(nullptr),
41  _pStatement(OCI_ErrorGetStatement(err)),
42  _pConnnection(OCI_ErrorGetConnection(err)),
43  _row(OCI_ErrorGetRow(err)),
44  _type(static_cast<ExceptionType::Type>(OCI_ErrorGetType(err))),
45  _errLib(OCI_ErrorGetInternalCode(err)),
46  _errOracle(OCI_ErrorGetOCICode(err))
47 {
48  SetWhat(OCI_ErrorGetString(err));
49 }
50 
51 inline Exception::Exception(const Exception& other) noexcept : Exception()
52 {
53  *this = other;
54 }
55 
56 inline Exception::~Exception() noexcept
57 {
58  delete [] _what;
59 }
60 
61 inline Exception& Exception::operator = (const Exception& other) noexcept
62 {
63  if (this != &other)
64  {
65  _pStatement = other._pStatement;
66  _pConnnection = other._pConnnection;
67  _row = other._row;
68  _type = other._type;
69  _errLib = other._errLib;
70  _errOracle = other._errOracle;
71 
72  CopyWhat(other._what);
73  }
74 
75  return *this;
76 }
77 inline void Exception::CopyWhat(const char* value) noexcept
78 {
79  if (_what)
80  {
81  delete[] _what;
82  _what = nullptr;
83  }
84 
85  if (!value)
86  {
87  return;
88  }
89 
90  const size_t len = strlen(value) + 1;
91 
92  _what = new (std::nothrow) char[len];
93  if (_what)
94  {
95  memcpy(_what, value, len);
96  }
97 }
98 
99 inline void Exception::SetWhat(const otext* value) noexcept
100 {
101 
102 #if defined(OCI_CHARSET_ANSI)
103 
104  CopyWhat(value);
105 
106 #else
107 
108  if (_what)
109  {
110  delete[] _what;
111  _what = nullptr;
112  }
113 
114  if (!value)
115  {
116  return;
117  }
118 
119  const size_t valueLenght = wcslen(value);
120 
121  _what = new (std::nothrow) char[valueLenght + 1];
122  if (_what)
123  {
124  const otext* ptr = value;
125 
126  mbstate_t mbs;
127  mbrlen(nullptr, 0, &mbs);
128 
129  #if defined(_MSC_VER)
130  __pragma(warning(disable : 4996))
131  #endif
132 
133  const size_t convLenght = wcsrtombs(_what, &ptr, valueLenght, &mbs);
134 
135  #if defined(_MSC_VER)
136  __pragma(warning(default: 4996))
137  #endif
138 
139  const size_t whatLenght = (static_cast<size_t>(-1) == convLenght) ? 0 : convLenght;
140 
141  _what[whatLenght] = 0;
142 
143 
144  }
145 
146 #endif
147 }
148 
149 inline const char * Exception::what() const noexcept
150 {
151  return _what;
152 }
153 
155 {
156  const char* str = what();
157 
158  if (!str)
159  {
160  return ostring{};
161  }
162 
163  ostring message;
164 
165 #if defined(OCI_CHARSET_ANSI)
166 
167  message = str;
168 
169 #else
170 
171  const size_t valueLenght = strlen(str);
172 
173  message.resize(valueLenght);
174 
175  const char* ptr = str;
176 
177  mbstate_t mbs;
178  mbrlen(nullptr, 0, &mbs);
179 
180 
181 #if defined(_MSC_VER)
182  __pragma(warning(disable : 4996))
183 #endif
184 
185  const size_t convLenght = mbsrtowcs(&message[0], &ptr, valueLenght, &mbs);
186 
187 #if defined(_MSC_VER)
188  __pragma(warning(default: 4996))
189 #endif
190 
191  const size_t messLenght = (static_cast<size_t>(-1) == convLenght) ? 0 : convLenght;
192 
193  message.resize(messLenght);
194 
195 #endif
196 
197  return message;
198 }
199 
201 {
202  return _type;
203 }
204 
206 {
207  return _errOracle;
208 }
209 
211 {
212  return _errLib;
213 }
214 
216 {
217  return Statement(_pStatement, nullptr);
218 }
219 
221 {
222  return Connection(_pConnnection, Environment::GetEnvironmentHandle());
223 }
224 
225 inline unsigned int Exception::GetRow() const
226 {
227  return _row;
228 }
229 
230 }
A connection or session with a specific database.
Definition: types.hpp:1580
Exception class handling all OCILIB errors.
Definition: types.hpp:358
ostring GetMessage() const
Retrieve the error message.
Definition: Exception.hpp:154
const char * what() const noexcept override
Override the std::exception::what() method.
Definition: Exception.hpp:149
int GetOracleErrorCode() const
Return the Oracle error code.
Definition: Exception.hpp:205
unsigned int GetRow() const
Return the row index which caused an error during statement execution.
Definition: Exception.hpp:225
ExceptionType GetType() const
Return the Exception type.
Definition: Exception.hpp:200
virtual ~Exception() noexcept
Virtual destructor required for deriving from std::exception.
Definition: Exception.hpp:56
Connection GetConnection() const
Return the connection within the error occurred.
Definition: Exception.hpp:220
int GetInternalErrorCode() const
Return the OCILIB error code.
Definition: Exception.hpp:210
Statement GetStatement() const
Return the statement within the error occurred.
Definition: Exception.hpp:215
Object used for executing SQL or PL/SQL statement and returning the produced results.
Definition: types.hpp:5559
struct OCI_Error OCI_Error
Encapsulates an Oracle or OCILIB exception.
Definition: types.h:390
OCI_SYM_PUBLIC int OCI_API OCI_ErrorGetInternalCode(OCI_Error *err)
Retrieve Internal Error code from error handle.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_ErrorGetType(OCI_Error *err)
Retrieve the type of error from error handle.
OCI_SYM_PUBLIC const otext *OCI_API OCI_ErrorGetString(OCI_Error *err)
Retrieve error message from error handle.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_ErrorGetRow(OCI_Error *err)
Return the row index which caused an error during statement execution.
OCI_SYM_PUBLIC OCI_Statement *OCI_API OCI_ErrorGetStatement(OCI_Error *err)
Retrieve statement handle within the error occurred.
OCI_SYM_PUBLIC int OCI_API OCI_ErrorGetOCICode(OCI_Error *err)
Retrieve Oracle Error code from error handle.
OCI_SYM_PUBLIC OCI_Connection *OCI_API OCI_ErrorGetConnection(OCI_Error *err)
Retrieve connection handle within the error occurred.
OCILIB ++ Namespace.
std::basic_string< otext, std::char_traits< otext >, std::allocator< otext > > ostring
string class wrapping the OCILIB otext * type and OTEXT() macros ( see Character sets )
Definition: config.hpp:120