OCILIB (C and C++ Driver for Oracle)  4.7.6
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Environment.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 // ReSharper disable CppClangTidyPerformanceUnnecessaryValueParam
26 // ReSharper disable CppClangTidyHicppUseAuto
27 // ReSharper disable CppClangTidyModernizeUseAuto
28 
29 namespace ocilib
30 {
31 
32 inline void Environment::Initialize(EnvironmentFlags mode, const ostring& libpath)
33 {
34  GetInstance().SelfInitialize(mode, libpath);
35 }
36 
37 inline void Environment::Cleanup()
38 {
39  GetInstance().SelfCleanup();
40 
41  Environment* handle = static_cast<Environment*>(OCI_GetUserData(nullptr));
42  OCI_SetUserData(nullptr, handle);
43 
44 #ifdef OCILIBPP_DEBUG_MEMORY_ENABLED
45 
46  core::GetMemoryDebugInfo().PrintAllocations();
47 
48 #endif
49 
50 }
51 
53 {
54  return GetInstance()._mode;
55 }
56 
58 {
59  return ImportMode(static_cast<ImportMode::Type>(core::Check(OCI_GetImportMode())));
60 }
61 
63 {
64  return CharsetMode(static_cast<CharsetMode::Type>(core::Check(OCI_GetCharset())));
65 }
66 
67 inline unsigned int Environment::GetCharMaxSize()
68 {
69  return GetInstance()._charMaxSize;
70 }
71 
73 {
74  return core::Check(OCI_GetAllocatedBytes(type.GetValues()));
75 }
76 
78 {
79  return GetInstance()._initialized;
80 }
81 
83 {
84  return OracleVersion(static_cast<OracleVersion::Type>(core::Check(OCI_GetOCICompileVersion())));
85 }
86 
88 {
89  return OracleVersion(static_cast<OracleVersion::Type>(core::Check(OCI_GetOCIRuntimeVersion())));
90 }
91 
93 {
94  return OCI_VER_MAJ(core::Check(OCI_GetOCICompileVersion()));
95 }
96 
98 {
99  return OCI_VER_MIN(core::Check(OCI_GetOCICompileVersion()));
100 }
101 
103 {
104  return OCI_VER_REV(core::Check(OCI_GetOCICompileVersion()));
105 }
106 
108 {
109  return OCI_VER_MAJ(core::Check(OCI_GetOCIRuntimeVersion()));
110 }
111 
113 {
114  return OCI_VER_MIN(core::Check(OCI_GetOCIRuntimeVersion()));
115 }
116 
118 {
119  return OCI_VER_REV(core::Check(OCI_GetOCIRuntimeVersion()));
120 }
121 
122 inline void Environment::EnableWarnings(bool value)
123 {
124  OCI_EnableWarnings(static_cast<boolean>(value));
125 }
126 
127 inline bool Environment::SetFormat(FormatType formatType, const ostring& format)
128 {
129  return core::Check(OCI_SetFormat(nullptr, formatType, format.c_str()) == TRUE);
130 }
131 
133 {
134  return core::MakeString(core::Check(OCI_GetFormat(nullptr, formatType)));
135 }
136 
137 inline void Environment::StartDatabase(const ostring& db, const ostring& user, const ostring &pwd, Environment::StartFlags startFlags,
138  Environment::StartMode startMode, Environment::SessionFlags sessionFlags, const ostring& spfile)
139 {
140  core::Check(OCI_DatabaseStartup(db.c_str(), user.c_str(), pwd.c_str(), sessionFlags.GetValues(),
141  startMode.GetValues(), startFlags.GetValues(), spfile.c_str() ));
142 }
143 
144 inline void Environment::ShutdownDatabase(const ostring& db, const ostring& user, const ostring &pwd, Environment::ShutdownFlags shutdownFlags,
145  Environment::ShutdownMode shutdownMode, Environment::SessionFlags sessionFlags)
146 {
147  core::Check(OCI_DatabaseShutdown(db.c_str(), user.c_str(), pwd.c_str(), sessionFlags.GetValues(),
148  shutdownMode.GetValues(), shutdownFlags.GetValues() ));
149 }
150 
151 inline void Environment::ChangeUserPassword(const ostring& db, const ostring& user, const ostring& pwd, const ostring& newPwd)
152 {
153  core::Check(OCI_SetUserPassword(db.c_str(), user.c_str(), pwd.c_str(), newPwd.c_str()));
154 }
155 
156 inline void Environment::SetHAHandler(HAHandlerProc handler)
157 {
158  core::Check(OCI_SetHAHandler(static_cast<POCI_HA_HANDLER>(handler != nullptr ? Environment::HAHandler : nullptr)));
159 
160  SetUserCallback<HAHandlerProc>(GetEnvironmentHandle(), handler);
161 }
162 
163 inline void Environment::HAHandler(OCI_Connection *pConnection, unsigned int source, unsigned int event, OCI_Timestamp *pTimestamp)
164 {
165  const HAHandlerProc handler = GetUserCallback<HAHandlerProc>(GetEnvironmentHandle());
166 
167  if (handler)
168  {
169  Connection connection(pConnection, nullptr);
170  Timestamp timestamp(pTimestamp, connection.GetHandle());
171 
172  handler(connection,
173  HAEventSource(static_cast<HAEventSource::Type>(source)),
174  HAEventType (static_cast<HAEventType::Type> (event)),
175  timestamp);
176  }
177 }
178 
179 inline unsigned int Environment::TAFHandler(OCI_Connection *pConnection, unsigned int type, unsigned int event)
180 {
181  unsigned int res = OCI_FOC_OK;
182 
183  const Connection::TAFHandlerProc handler = GetUserCallback<Connection::TAFHandlerProc>(core::Check(pConnection));
184 
185  if (handler)
186  {
187  Connection connection(pConnection, nullptr);
188 
189  res = handler(connection,
190  Connection::FailoverRequest( static_cast<Connection::FailoverRequest::Type> (type)),
191  Connection::FailoverEvent ( static_cast<Connection::FailoverEvent::Type> (event)));
192  }
193 
194  return res;
195 }
196 
197 inline void Environment::NotifyHandler(OCI_Event *pEvent)
198 {
199  const Subscription::NotifyHandlerProc handler = GetUserCallback<Subscription::NotifyHandlerProc>((core::Check(OCI_EventGetSubscription(pEvent))));
200 
201  if (handler)
202  {
203  Event evt(pEvent);
204  handler(evt);
205  }
206 }
207 
208 inline void Environment::NotifyHandlerAQ(OCI_Dequeue *pDequeue)
209 {
210  const Dequeue::NotifyAQHandlerProc handler = GetUserCallback<Dequeue::NotifyAQHandlerProc>(core::Check(pDequeue));
211 
212  if (handler)
213  {
214  Dequeue dequeue(pDequeue, Environment::GetEnvironmentHandle());
215  handler(dequeue);
216  }
217 }
218 
219 template<class T>
220 T Environment::GetUserCallback(AnyPointer ptr)
221 {
222  return reinterpret_cast<T>(GetInstance()._callbacks.Get(ptr));
223 }
224 
225 template<class T>
226 void Environment::SetUserCallback(AnyPointer ptr, T callback)
227 {
228  if (callback)
229  {
230  GetInstance()._callbacks.Set(ptr, reinterpret_cast<CallbackPointer>(callback));
231  }
232  else
233  {
234  GetInstance()._callbacks.Remove(ptr);
235  }
236 }
237 
238 inline core::Handle * Environment::GetEnvironmentHandle()
239 {
240  return GetInstance()._handle.GetHandle();
241 }
242 
243 inline Environment& Environment::GetInstance()
244 {
245  Environment* handle = static_cast<Environment*>(OCI_GetUserData(nullptr));
246  if (handle != nullptr)
247  {
248  return *handle;
249  }
250 
251  static Environment environment;
252 
253  OCI_SetUserData(nullptr,&environment);
254 
255  return environment;
256 }
257 
258 inline Environment::Environment() : _charMaxSize(0), _initialized(false), _guard(core::SynchronizationMode::Unsafe)
259 {
260 
261 }
262 
263 inline void Environment::SelfInitialize(EnvironmentFlags mode, const ostring& libpath)
264 {
265  _mode = mode;
266 
267  core::Check(OCI_Initialize(nullptr, libpath.c_str(), _mode.GetValues() | OCI_ENV_CONTEXT));
268 
269  _initialized = true;
270 
271  _guard.SetMode((_mode & Environment::Threaded) == Environment::Threaded ? core::SynchronizationMode::Safe : core::SynchronizationMode::Unsafe);
272 
273  _callbacks.SetGuard(&_guard);
274 
275  _handle.AcquireTransient
276  (
277  /* returned value IS NOT an OCI_Environment* but OCIEnv* direct handle
278  to be changed when C API we have public methods for OCI_Environment */
279  static_cast<OCI_Environment*>(const_cast<AnyPointer>(core::Check(OCI_HandleGetEnvironment()))),
280  nullptr
281  );
282 
283  _charMaxSize = ComputeCharMaxSize(GetCharset());
284 }
285 
286 inline void Environment::SelfCleanup()
287 {
288  _guard.SetMode(core::SynchronizationMode::Unsafe);
289 
290  _callbacks.SetGuard(nullptr);
291 
292  _handle.Release();
293 
294  if (_initialized)
295  {
297  }
298 
299  _initialized = false;
300 }
301 
302 }
A connection or session with a specific database.
Definition: types.hpp:1580
FailoverResult(* TAFHandlerProc)(Connection &con, FailoverRequest failoverRequest, FailoverEvent failoverEvent)
User callback for TAF event notifications.
Definition: types.hpp:1743
core::Enum< FailoverRequestValues > FailoverRequest
Failover requests.
Definition: types.hpp:1658
core::Enum< FailoverEventValues > FailoverEvent
Failover events.
Definition: types.hpp:1686
void(* NotifyAQHandlerProc)(Dequeue &dequeue)
User callback for dequeue event notifications.
Definition: types.hpp:7950
Static class in charge of library initialization / cleanup.
Definition: types.hpp:490
static bool SetFormat(FormatType formatType, const ostring &format)
Set the format string for implicit string conversions of the given type.
static unsigned int GetRuntimeMajorVersion()
Return the major version number of OCI used at runtime.
static void ShutdownDatabase(const ostring &db, const ostring &user, const ostring &pwd, Environment::ShutdownFlags shutdownFlags, Environment::ShutdownMode shutdownMode, Environment::SessionFlags sessionFlags=SessionSysDba)
Shutdown a database instance.
static unsigned int GetRuntimeRevisionVersion()
Return the revision version number of OCI used at runtime.
void(* HAHandlerProc)(Connection &con, HAEventSource eventSource, HAEventType eventType, Timestamp &time)
User callback for HA event notifications.
Definition: types.hpp:822
static OracleVersion GetCompileVersion()
Return the version of OCI used for compiling OCILIB.
Definition: Environment.hpp:82
static Environment::EnvironmentFlags GetMode()
Return the Environment mode flags.
Definition: Environment.hpp:52
static unsigned int GetCompileRevisionVersion()
Return the revision version number of OCI used for compiling OCILIB.
core::Enum< CharsetModeValues > CharsetMode
Environment charset mode.
Definition: types.hpp:638
core::Enum< ImportModeValues > ImportMode
OCI libraries import mode.
Definition: types.hpp:616
static void EnableWarnings(bool value)
Enable or disable Oracle warning notifications.
static big_uint GetAllocatedBytes(AllocatedBytesFlags type)
Return the current number of bytes allocated internally in the library.
Definition: Environment.hpp:72
static void SetHAHandler(HAHandlerProc handler)
Set the High availability (HA) user handler.
static OracleVersion GetRuntimeVersion()
Return the version of OCI used at runtime.
Definition: Environment.hpp:87
static void StartDatabase(const ostring &db, const ostring &user, const ostring &pwd, Environment::StartFlags startFlags, Environment::StartMode startMode, Environment::SessionFlags sessionFlags=SessionSysDba, const ostring &spfile=OTEXT(""))
Start a database instance.
static void Initialize(EnvironmentFlags mode=Environment::Default, const ostring &libpath=OTEXT(""))
Initialize the OCILIB environment.
Definition: Environment.hpp:32
static void Cleanup()
Clean up all resources allocated by the environment.
Definition: Environment.hpp:37
static void ChangeUserPassword(const ostring &db, const ostring &user, const ostring &pwd, const ostring &newPwd)
Change the password of the given user on the given database.
core::Enum< HAEventTypeValues > HAEventType
Type of HA events.
Definition: types.hpp:570
static unsigned int GetCompileMinorVersion()
Return the minor version number of OCI used for compiling OCILIB.
Definition: Environment.hpp:97
static bool Initialized()
Return true if the environment has been successfully initialized.
Definition: Environment.hpp:77
static Environment::CharsetMode GetCharset()
Return the OCILIB charset type.
Definition: Environment.hpp:62
static Environment::ImportMode GetImportMode()
Return the Oracle shared library import mode.
Definition: Environment.hpp:57
static unsigned int GetRuntimeMinorVersion()
Return the minor version number of OCI used at runtime.
static unsigned int GetCompileMajorVersion()
Return the major version number of OCI used for compiling OCILIB.
Definition: Environment.hpp:92
static ostring GetFormat(FormatType formatType)
Return the format string for implicit string conversions of the given type.
static unsigned int GetCharMaxSize()
Return maximum size for a character.
Definition: Environment.hpp:67
core::Enum< HAEventSourceValues > HAEventSource
Source of HA events.
Definition: types.hpp:548
void(* NotifyHandlerProc)(Event &evt)
User callback for subscriptions event notifications.
Definition: types.hpp:7083
Object identifying the SQL data type TIMESTAMP.
Definition: types.hpp:3513
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:118
OCI_SYM_PUBLIC boolean OCI_API OCI_SetFormat(OCI_Connection *con, unsigned int type, const otext *format)
Set the format string for implicit string conversions of the given type.
OCI_SYM_PUBLIC void *OCI_API OCI_GetUserData(OCI_Connection *con)
Return the pointer to user data previously associated with the connection.
OCI_SYM_PUBLIC boolean OCI_API OCI_SetUserData(OCI_Connection *con, void *data)
Associate a pointer to user data to the given connection.
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetFormat(OCI_Connection *con, unsigned int type)
Return the format string for implicit string conversions of the given type.
OCI_SYM_PUBLIC boolean OCI_API OCI_SetUserPassword(const otext *db, const otext *user, const otext *pwd, const otext *new_pwd)
Change the password of the given user on the given database.
struct OCI_Dequeue OCI_Dequeue
OCILIB encapsulation of A/Q dequeuing operations.
Definition: types.h:470
struct OCI_Connection OCI_Connection
Oracle physical connection.
Definition: types.h:124
struct OCI_Timestamp OCI_Timestamp
Oracle internal timestamp representation.
Definition: types.h:289
struct OCI_Environment OCI_Environment
Environment object.
Definition: types.h:95
void(* POCI_HA_HANDLER)(OCI_Connection *con, unsigned int source, unsigned int event, OCI_Timestamp *time)
HA (High Availability) events Notification User callback prototype.
Definition: types.h:629
struct OCI_Event OCI_Event
OCILIB encapsulation of Oracle DCN event.
Definition: types.h:440
OCI_SYM_PUBLIC boolean OCI_API OCI_Cleanup(void)
Clean up all resources allocated by the library.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetOCICompileVersion(void)
Return the version of OCI used for compilation.
OCI_SYM_PUBLIC boolean OCI_API OCI_SetHAHandler(POCI_HA_HANDLER handler)
Set the High availability (HA) user handler.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetCharset(void)
Return the OCILIB charset type.
OCI_SYM_PUBLIC boolean OCI_API OCI_EnableWarnings(boolean value)
Enable or disable Oracle warning notifications.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetOCIRuntimeVersion(void)
Return the version of OCI used at runtime.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetImportMode(void)
Return the Oracle shared library import mode.
OCI_SYM_PUBLIC boolean OCI_API OCI_Initialize(POCI_ERROR err_handler, const otext *lib_path, unsigned int mode)
Initialize the library.
OCI_SYM_PUBLIC big_uint OCI_API OCI_GetAllocatedBytes(unsigned int mem_type)
Return the current number of bytes allocated internally in the library.
OCI_SYM_PUBLIC boolean OCI_API OCI_DatabaseShutdown(const otext *db, const otext *user, const otext *pwd, unsigned int sess_mode, unsigned int shut_mode, unsigned int shut_flag)
Shutdown a database instance.
OCI_SYM_PUBLIC boolean OCI_API OCI_DatabaseStartup(const otext *db, const otext *user, const otext *pwd, unsigned int sess_mode, unsigned int start_mode, unsigned int start_flag, const otext *spfile)
Start a database instance.
OCI_SYM_PUBLIC const void *OCI_API OCI_HandleGetEnvironment(void)
Return the OCI Environment Handle (OCIEnv *) of OCILIB library.
OCI_SYM_PUBLIC OCI_Subscription *OCI_API OCI_EventGetSubscription(OCI_Event *event)
Return the subscription handle that generated this event.
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
SynchronizationMode
Internal usage. Synchronization mode enumeration.
Definition: core.hpp:217
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
void * CallbackPointer
Alias used for storing user callback method pointers.
Definition: config.hpp:174
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129
core::Enum< OracleVersionValues > OracleVersion
Oracle Version.
Definition: types.hpp:78