OCILIB (C and C++ Driver for Oracle)  4.7.3
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Connection.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-2021 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 CppClangTidyHicppUseEqualsDefault
26 // ReSharper disable CppClangTidyModernizeUseEqualsDefault
27 // ReSharper disable CppClangTidyPerformanceUnnecessaryValueParam
28 // ReSharper disable CppClangTidyHicppUseEmplace
29 
30 namespace ocilib
31 {
32 
34 {
35 
36 }
37 
38 inline Connection::Connection(const ostring& db, const ostring& user, const ostring& pwd, Environment::SessionFlags sessionFlags)
39 {
40  Open(db, user, pwd, sessionFlags);
41 }
42 
44 {
45  Acquire(con, reinterpret_cast<HandleFreeFunc>(parent ? OCI_ConnectionFree : nullptr), nullptr, parent);
46 }
47 
48 inline void Connection::Open(const ostring& db, const ostring& user, const ostring& pwd, Environment::SessionFlags sessionFlags)
49 {
50  Acquire(core::Check(OCI_ConnectionCreate(db.c_str(), user.c_str(), pwd.c_str(), sessionFlags.GetValues())),
51  reinterpret_cast<HandleFreeFunc>(OCI_ConnectionFree), nullptr, Environment::GetEnvironmentHandle());
52 }
53 
54 inline void Connection::Close()
55 {
56  Release();
57 }
58 
59 inline void Connection::Commit()
60 {
61  core::Check(OCI_Commit(*this));
62 }
63 
64 inline void Connection::Rollback()
65 {
66  core::Check(OCI_Rollback(*this));
67 }
68 
69 inline void Connection::Break()
70 {
71  core::Check(OCI_Break(*this));
72 }
73 
74 inline void Connection::SetAutoCommit(bool enabled)
75 {
76  core::Check(OCI_SetAutoCommit(*this, enabled));
77 }
78 
79 inline bool Connection::GetAutoCommit() const
80 {
81  return (core::Check(OCI_GetAutoCommit(*this)) == TRUE);
82 }
83 
84 inline bool Connection::IsServerAlive() const
85 {
86  return (core::Check(OCI_IsConnected(*this)) == TRUE);
87 }
88 
89 inline bool Connection::PingServer() const
90 {
91  return( core::Check(OCI_Ping(*this)) == TRUE);
92 }
93 
95 {
97 }
98 
100 {
102 }
103 
105 {
107 }
108 
110 {
111  return OracleVersion(static_cast<OracleVersion::Type>(core::Check(OCI_GetVersionConnection(*this))));
112 }
113 
115 {
117 }
118 
119 inline unsigned int Connection::GetServerMajorVersion() const
120 {
121  return core::Check(OCI_GetServerMajorVersion(*this));
122 }
123 
124 inline unsigned int Connection::GetServerMinorVersion() const
125 {
126  return core::Check(OCI_GetServerMinorVersion(*this));
127 }
128 
129 inline unsigned int Connection::GetServerRevisionVersion() const
130 {
132 }
133 
134 inline void Connection::ChangePassword(const ostring& newPwd)
135 {
136  core::Check(OCI_SetPassword(*this, newPwd.c_str()));
137 }
138 
140 {
142 }
143 
144 inline void Connection::SetSessionTag(const ostring& tag)
145 {
146  core::Check(OCI_SetSessionTag(*this, tag.c_str()));
147 }
148 
150 {
152 }
153 
154 inline void Connection::SetTransaction(const Transaction &transaction)
155 {
156  core::Check(OCI_SetTransaction(*this, transaction));
157 }
158 
159 inline bool Connection::SetFormat(FormatType formatType, const ostring& format)
160 {
161  return core::Check(OCI_SetFormat(*this, formatType, format.c_str()) == TRUE);
162 }
163 
165 {
166  return core::MakeString(core::Check(OCI_GetFormat(*this, formatType)));
167 }
168 
169 inline void Connection::EnableServerOutput(unsigned int bufsize, unsigned int arrsize, unsigned int lnsize)
170 {
171  core::Check(OCI_ServerEnableOutput(*this, bufsize, arrsize, lnsize));
172 }
173 
175 {
177 }
178 
179 inline bool Connection::GetServerOutput(ostring &line) const
180 {
181  const otext * str = core::Check(OCI_ServerGetOutput(*this));
182 
183  line = core::MakeString(str);
184 
185  return (str != nullptr);
186 }
187 
188 inline void Connection::GetServerOutput(std::vector<ostring> &lines) const
189 {
190  const otext * str = core::Check(OCI_ServerGetOutput(*this));
191 
192  while (str)
193  {
194  lines.push_back(str);
195  str = core::Check(OCI_ServerGetOutput(*this));
196  }
197 }
198 
199 inline void Connection::SetTrace(SessionTrace trace, const ostring& value)
200 {
201  core::Check(OCI_SetTrace(*this, trace, value.c_str()));
202 }
203 
205 {
206  return core::MakeString(core::Check(OCI_GetTrace(*this, trace)));
207 }
208 
210 {
212 }
213 
215 {
216  return core::Check(OCI_GetInstanceName(*this));
217 }
218 
220 {
222 }
223 
225 {
226  return core::Check(OCI_GetServerName(*this));
227 }
228 
230 {
232 }
233 
235 {
236  return Timestamp(core::Check(OCI_GetInstanceStartTime(*this)), GetHandle());
237 }
238 
239 inline unsigned int Connection::GetStatementCacheSize() const
240 {
241  return core::Check(OCI_GetStatementCacheSize(*this));
242 }
243 
244 inline void Connection::SetStatementCacheSize(unsigned int value)
245 {
246  core::Check(OCI_SetStatementCacheSize(*this, value));
247 }
248 
249 inline unsigned int Connection::GetDefaultLobPrefetchSize() const
250 {
252 }
253 
254 inline void Connection::SetDefaultLobPrefetchSize(unsigned int value)
255 {
257 }
258 
259 inline unsigned int Connection::GetMaxCursors() const
260 {
261  return core::Check(OCI_GetMaxCursors(*this));
262 }
263 
264 inline bool Connection::IsTAFCapable() const
265 {
266  return (core::Check(OCI_IsTAFCapable(*this)) == TRUE);
267 }
268 
270 {
271  core::Check(OCI_SetTAFHandler(*this, static_cast<POCI_TAF_HANDLER>(handler != nullptr ? Environment::TAFHandler : nullptr)));
272 
273  Environment::SetUserCallback<Connection::TAFHandlerProc>(static_cast<OCI_Connection*>(*this), handler);
274 }
275 
277 {
278  return core::Check(OCI_GetUserData(*this));
279 }
280 
282 {
283  core::Check(OCI_SetUserData(*this, value));
284 }
285 
286 inline unsigned int Connection::GetTimeout(TimeoutType timeout)
287 {
288  return core::Check(OCI_GetTimeout(*this, timeout));
289 }
290 
291 inline void Connection::SetTimeout(TimeoutType timeout, unsigned int value)
292 {
293  core::Check(OCI_SetTimeout(*this, timeout, value));
294 }
295 
296 }
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetTrace(OCI_Connection *con, unsigned int trace)
Get the current trace for the trace type from the given connection.
OCI_SYM_PUBLIC boolean OCI_API OCI_SetAutoCommit(OCI_Connection *con, boolean enable)
Enable / disable auto commit mode.
Internal usage. Interface for handling ownership and relationship of a C API handle.
Definition: core.hpp:312
void SetTAFHandler(TAFHandlerProc handler)
Set the Transparent Application Failover (TAF) user handler.
Definition: Connection.hpp:269
ostring GetPassword() const
Return the current logged user password.
Definition: Connection.hpp:104
struct OCI_Connection OCI_Connection
Oracle physical connection.
Definition: types.h:112
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetStatementCacheSize(OCI_Connection *con)
Return the maximum number of statements to keep in the statement cache.
unsigned int GetServerRevisionVersion() const
Return the revision version number of the connected database server.
Definition: Connection.hpp:129
ostring GetServer() const
Return the Oracle server Hos name of the connected database/service name.
Definition: Connection.hpp:224
void SetTrace(SessionTrace trace, const ostring &value)
Set tracing information for the session.
Definition: Connection.hpp:199
ostring GetUserName() const
Return the current logged user name.
Definition: Connection.hpp:99
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetUserName(OCI_Connection *con)
Return the current logged user name.
OCILIB ++ Namespace.
void SetDefaultLobPrefetchSize(unsigned int value)
Enable or disable pre-fetching for all LOBs fetched in the connection.
Definition: Connection.hpp:254
void ChangePassword(const ostring &newPwd)
Change the password of the logged user.
Definition: Connection.hpp:134
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetMaxCursors(OCI_Connection *con)
Return the maximum number of SQL statements that can be opened in one session.
void SetAutoCommit(bool enabled)
Enable or disable auto commit mode (implicit commits after every SQL execution)
Definition: Connection.hpp:74
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetDefaultLobPrefetchSize(OCI_Connection *con)
Return the default LOB prefetch buffer size for the connection.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetTimeout(OCI_Connection *con, unsigned int type)
Returns the requested timeout value for OCI calls that require server round-trips to the given databa...
OCI_SYM_PUBLIC boolean OCI_API OCI_SetStatementCacheSize(OCI_Connection *con, unsigned int value)
Set the maximum number of statements to keep in the statement cache.
OCI_SYM_PUBLIC boolean OCI_API OCI_ConnectionFree(OCI_Connection *con)
Close a physical connection to an Oracle database server.
OCI_SYM_PUBLIC boolean OCI_API OCI_SetTimeout(OCI_Connection *con, unsigned int type, unsigned int value)
Set a given timeout for OCI calls that require server round-trips to the given database.
void Close()
Close the physical connection to the DB server.
Definition: Connection.hpp:54
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so, it raises a C++ exception using the retrieved error handle.
Definition: Utils.hpp:53
Oracle Transaction object.
Definition: types.hpp:2358
OCI_SYM_PUBLIC boolean OCI_API OCI_SetTrace(OCI_Connection *con, unsigned int trace, const otext *value)
Set tracing information to the session of the given connection.
OCI_SYM_PUBLIC boolean OCI_API OCI_GetAutoCommit(OCI_Connection *con)
Get current auto commit mode status.
Transaction GetTransaction() const
Return the current transaction of the connection.
Definition: Connection.hpp:149
OCI_SYM_PUBLIC boolean OCI_API OCI_IsTAFCapable(OCI_Connection *con)
Verify if the given connection support TAF events.
Template Flags template class providing some type safety to some extends for manipulating flags set v...
Definition: core.hpp:147
ostring GetDatabase() const
Return the Oracle server database name of the connected database/service name.
Definition: Connection.hpp:209
void SetTimeout(TimeoutType timeout, unsigned int value)
Set a given timeout for OCI calls that require server round-trips to the given database.
Definition: Connection.hpp:291
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetServerRevisionVersion(OCI_Connection *con)
Return the revision version number of the connected database server.
OCI_SYM_PUBLIC boolean OCI_API OCI_ServerDisableOutput(OCI_Connection *con)
Disable the server output.
OCI_SYM_PUBLIC OCI_Transaction *OCI_API OCI_GetTransaction(OCI_Connection *con)
Return the current transaction of the connection.
ostring GetTrace(SessionTrace trace) const
Get the current trace for the trace type from the given connection.
Definition: Connection.hpp:204
ostring GetFormat(FormatType formatType)
Return the format string for implicit string conversions of the given type.
Definition: Connection.hpp:164
OCI_SYM_PUBLIC boolean OCI_API OCI_Break(OCI_Connection *con)
Perform an immediate abort of any currently Oracle OCI call.
unsigned int GetServerMinorVersion() const
Return the minor version number of the connected database server.
Definition: Connection.hpp:124
OCI_SYM_PUBLIC boolean OCI_API OCI_SetDefaultLobPrefetchSize(OCI_Connection *con, unsigned int value)
Enable or disable prefetching for all LOBs fetched in the connection.
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetDomainName(OCI_Connection *con)
Return the Oracle server domain name of the connected database/service name.
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_IsConnected(OCI_Connection *con)
Returns TRUE is the given connection is still connected otherwise FALSE.
void SetUserData(AnyPointer value)
Associate a pointer to user data to the given connection.
Definition: Connection.hpp:281
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
OCI_SYM_PUBLIC OCI_Connection *OCI_API OCI_ConnectionCreate(const otext *db, const otext *user, const otext *pwd, unsigned int mode)
Create a physical connection to an Oracle database server.
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetInstanceName(OCI_Connection *con)
Return the Oracle server Instance name of the connected database/service name.
void SetTransaction(const Transaction &transaction)
Set a transaction to a connection.
Definition: Connection.hpp:154
void * AnyPointer
Alias for the generic void pointer.
Definition: config.hpp:129
bool IsServerAlive() const
Indicate if the connection is still connected to the server.
Definition: Connection.hpp:84
Template Enumeration template class providing some type safety to some extends for manipulating enume...
Definition: core.hpp:117
OracleVersion GetVersion() const
Return the Oracle version supported by the connection.
Definition: Connection.hpp:109
core::Enum< OracleVersionValues > OracleVersion
Oracle Version.
Definition: types.hpp:71
OCI_SYM_PUBLIC boolean OCI_API OCI_Ping(OCI_Connection *con)
Makes a round trip call to the server to confirm that the connection and the server are active...
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetServiceName(OCI_Connection *con)
Return the Oracle server service name of the connected database/service name.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetServerMajorVersion(OCI_Connection *con)
Return the major version number of the connected database server.
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetDBName(OCI_Connection *con)
Return the Oracle server database name of the connected database/service name.
OCI_SYM_PUBLIC boolean OCI_API OCI_Rollback(OCI_Connection *con)
Cancel current pending changes.
OCI_SYM_PUBLIC boolean OCI_API OCI_SetSessionTag(OCI_Connection *con, const otext *tag)
Associate a tag to the given connection/session.
void SetStatementCacheSize(unsigned int value)
Set the maximum number of statements to keep in the statement cache.
Definition: Connection.hpp:244
unsigned int GetMaxCursors() const
Return the maximum number of SQL statements that can be opened in one session.
Definition: Connection.hpp:259
OCI_SYM_PUBLIC boolean OCI_API OCI_SetTransaction(OCI_Connection *con, OCI_Transaction *trans)
Set a transaction to a connection.
unsigned int GetStatementCacheSize() const
Return the maximum number of statements to keep in the statement cache.
Definition: Connection.hpp:239
void DisableServerOutput()
Disable the server output.
Definition: Connection.hpp:174
unsigned int GetTimeout(TimeoutType timeout)
Returns the requested timeout value for OCI calls that require server round-trips to the given databa...
Definition: Connection.hpp:286
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 const otext *OCI_API OCI_GetSessionTag(OCI_Connection *con)
Return the tag associated the given connection.
bool IsTAFCapable() const
Verify if the connection support TAF events.
Definition: Connection.hpp:264
OCI_SYM_PUBLIC const otext *OCI_API OCI_ServerGetOutput(OCI_Connection *con)
Retrieve one line of the server buffer.
ostring GetSessionTag() const
Return the tag associated with the given connection.
Definition: Connection.hpp:139
void Open(const ostring &db, const ostring &user, const ostring &pwd, Environment::SessionFlags sessionFlags=Environment::SessionDefault)
Create a physical connection to an Oracle database server.
Definition: Connection.hpp:48
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetVersionConnection(OCI_Connection *con)
Return the highest Oracle version is supported by the connection.
void Commit()
Commit current pending changes.
Definition: Connection.hpp:59
OCI_SYM_PUBLIC boolean OCI_API OCI_SetUserData(OCI_Connection *con, void *data)
Associate a pointer to user data to the given connection.
bool SetFormat(FormatType formatType, const ostring &format)
Set the format string for implicit string conversions of the given type.
Definition: Connection.hpp:159
void Break()
Perform an immediate abort of any currently Oracle OCI call on the given connection.
Definition: Connection.hpp:69
ostring GetDomain() const
Return the Oracle server Domain name of the connected database/service name.
Definition: Connection.hpp:229
OCI_SYM_PUBLIC boolean OCI_API OCI_SetTAFHandler(OCI_Connection *con, POCI_TAF_HANDLER handler)
Set the Transparent Application Failover (TAF) user handler.
void Rollback()
Cancel current pending changes.
Definition: Connection.hpp:64
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetVersionServer(OCI_Connection *con)
Return the connected database server version.
unsigned int GetDefaultLobPrefetchSize() const
Return the default LOB prefetch buffer size for the connection.
Definition: Connection.hpp:249
ostring GetServerVersion() const
Return the connected database server string version.
Definition: Connection.hpp:114
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetServerMinorVersion(OCI_Connection *con)
Return the minor version number of the connected database server.
ostring GetInstance() const
Return the Oracle server Instance name of the connected database/service name.
Definition: Connection.hpp:214
void EnableServerOutput(unsigned int bufsize, unsigned int arrsize, unsigned int lnsize)
Enable the server output.
Definition: Connection.hpp:169
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetServerName(OCI_Connection *con)
Return the Oracle server machine name of the connected database/service name.
ostring GetService() const
Return the Oracle server Service name of the connected database/service name.
Definition: Connection.hpp:219
ostring GetConnectionString() const
Return the name of the connected database/service name.
Definition: Connection.hpp:94
unsigned int GetServerMajorVersion() const
Return the major version number of the connected database server.
Definition: Connection.hpp:119
OCI_SYM_PUBLIC OCI_Timestamp *OCI_API OCI_GetInstanceStartTime(OCI_Connection *con)
Return the date and time (Timestamp) server instance start of the connected database/service name...
AnyPointer GetUserData()
Return the pointer to user data previously associated with the connection.
Definition: Connection.hpp:276
OCI_SYM_PUBLIC boolean OCI_API OCI_ServerEnableOutput(OCI_Connection *con, unsigned int bufsize, unsigned int arrsize, unsigned int lnsize)
Enable the server output.
void SetSessionTag(const ostring &tag)
Associate a tag to the given connection/session.
Definition: Connection.hpp:144
OCI_SYM_PUBLIC boolean OCI_API OCI_SetPassword(OCI_Connection *con, const otext *password)
Change the password of the logged user.
bool PingServer() const
Performs a round trip call to the server to confirm that the connection to the server is still valid...
Definition: Connection.hpp:89
Timestamp GetInstanceStartTime() const
Return the date and time (Timestamp) server instance start of the.
Definition: Connection.hpp:234
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
OCI_SYM_PUBLIC boolean OCI_API OCI_Commit(OCI_Connection *con)
Commit current pending changes.
bool GetAutoCommit() const
Indicates if auto commit is currently activated.
Definition: Connection.hpp:79
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetDatabase(OCI_Connection *con)
Return the name of the connected database/service name.
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.
bool GetServerOutput(ostring &line) const
Retrieve one line of the server buffer.
Definition: Connection.hpp:179
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetPassword(OCI_Connection *con)
Return the current logged user password.
Object identifying the SQL data type TIMESTAMP.
Definition: types.hpp:3490
FailoverResult(* TAFHandlerProc)(Connection &con, FailoverRequest failoverRequest, FailoverEvent failoverEvent)
User callback for TAF event notifications.
Definition: types.hpp:1721
Connection()
Default constructor.
Definition: Connection.hpp:33