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