OCILIB (C and C++ Driver for Oracle)  4.7.6
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Utils.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/core.hpp"
24 
25 // ReSharper disable CppClangTidyHicppUseAuto
26 // ReSharper disable CppClangTidyModernizeUseAuto
27 
28 namespace ocilib
29 {
30  namespace core
31  {
32  template<class T>
33  static T* OnAllocate(T* address, size_t count = 1)
34  {
35 #ifdef OCILIBPP_DEBUG_MEMORY_ENABLED
36  GetMemoryDebugInfo().OnAllocate(address, count);
37 #endif
38 
39  return address;
40  }
41 
42  template<class T>
43  static T* OnDeallocate(T* address)
44  {
45 #ifdef OCILIBPP_DEBUG_MEMORY_ENABLED
46  GetMemoryDebugInfo().OnDeallocate(address);
47 #endif
48 
49  return address;
50  }
51 
52  template<class T>
53  T Check(T result)
54  {
55  OCI_Error* err = OCI_GetLastError();
56 
57  if (err)
58  {
59  throw Exception(err);
60  }
61 
62  return result;
63  }
64 
65  inline ostring MakeString(const otext* result, int size)
66  {
67  return result ? (size >= 0 ? ostring(result, result + size) : ostring(result)) : ostring();
68  }
69 
70  inline Raw MakeRaw(AnyPointer result, unsigned int size)
71  {
72  unsigned char* ptr = static_cast<unsigned char*>(result);
73 
74  return (ptr && size > 0 ? Raw(ptr, ptr + size) : Raw());
75  }
76 
77  inline unsigned int ComputeCharMaxSize(Environment::CharsetMode charsetMode)
78  {
79  const int UTF8_BytesPerChar = 4;
80 
81  unsigned int res = sizeof(ostring::value_type);
82 
83  if (charsetMode == Environment::CharsetAnsi)
84  {
85 #ifdef _MSC_VER
86 #pragma warning(push)
87 #pragma warning(disable: 4996)
88 #endif
89  char* str = getenv("NLS_LANG");
90 
91 #ifdef _MSC_VER
92 #pragma warning(pop)
93 #endif
94 
95  if (str)
96  {
97  std::string nlsLang = str;
98 
99  for (char &i :nlsLang)
100  {
101  i = static_cast<std::string::value_type>(toupper(i));
102  }
103 
104  if (ostring::npos != nlsLang.find("UTF8"))
105  {
106  res = UTF8_BytesPerChar;
107  }
108  }
109  }
110 
111  return res;
112  }
113  }
114 }
Exception class handling all OCILIB errors.
Definition: types.hpp:358
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:118
struct OCI_Error OCI_Error
Encapsulates an Oracle or OCILIB exception.
Definition: types.h:390
OCI_SYM_PUBLIC OCI_Error *OCI_API OCI_GetLastError(void)
Retrieve the last error or warning occurred within the last OCILIB call.
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so,...
Definition: Utils.hpp:53
ostring MakeString(const otext *result, int size=-1)
Internal usage. Constructs a C++ string object from the given OCILIB string pointer.
Definition: Utils.hpp:65
Raw MakeRaw(AnyPointer result, unsigned int size)
Internal usage. Constructs a C++ Raw object from the given OCILIB raw buffer.
Definition: Utils.hpp:70
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
std::vector< unsigned char > Raw
C++ counterpart of SQL RAW data type.
Definition: config.hpp:138
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129