forked from bersler/OpenLogReplicator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseConnection.cpp
More file actions
111 lines (90 loc) · 4.58 KB
/
Copy pathDatabaseConnection.cpp
File metadata and controls
111 lines (90 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Class to handle database connection
Copyright (C) 2018-2024 Adam Leszczynski ([email protected])
This file is part of OpenLogReplicator.
OpenLogReplicator is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your option)
any later version.
OpenLogReplicator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenLogReplicator; see the file LICENSE; If not see
<http://www.gnu.org/licenses/>. */
#include "DatabaseConnection.h"
#include "DatabaseEnvironment.h"
namespace OpenLogReplicator {
DatabaseConnection::DatabaseConnection(DatabaseEnvironment* newEnv, const char* newUser, const char* newPassword, const char* newConnectString,
bool newSysAsm) :
user(newUser),
password(newPassword),
connectString(newConnectString),
sysAsm(newSysAsm),
connected(false),
env(newEnv),
errhp(nullptr),
srvhp(nullptr),
svchp(nullptr),
authp(nullptr) {
}
void DatabaseConnection::connect() {
disconnect();
OCIHandleAlloc(reinterpret_cast<dvoid*>(env->envhp), reinterpret_cast<dvoid**>(&errhp), OCI_HTYPE_ERROR,
0, nullptr);
OCIHandleAlloc(reinterpret_cast<dvoid*>(env->envhp), reinterpret_cast<dvoid**>(&srvhp), OCI_HTYPE_SERVER,
0, nullptr);
OCIHandleAlloc(reinterpret_cast<dvoid*>(env->envhp), reinterpret_cast<dvoid**>(&svchp), OCI_HTYPE_SVCCTX,
0, nullptr);
OCIHandleAlloc(reinterpret_cast<dvoid*>(env->envhp), reinterpret_cast<dvoid**>(&authp), OCI_HTYPE_SESSION,
0, nullptr);
env->checkErr(errhp, OCIServerAttach(srvhp, errhp, reinterpret_cast<const OraText*>(connectString.c_str()),
connectString.length(), OCI_DEFAULT));
env->checkErr(errhp, OCIAttrSet(reinterpret_cast<dvoid*>(svchp), OCI_HTYPE_SVCCTX, srvhp, 0,
OCI_ATTR_SERVER, reinterpret_cast<OCIError*>(errhp)));
env->checkErr(errhp, OCIAttrSet(reinterpret_cast<dvoid*>(authp), OCI_HTYPE_SESSION,
const_cast<dvoid*>(reinterpret_cast<const dvoid*>(user.c_str())), user.length(),
OCI_ATTR_USERNAME, reinterpret_cast<OCIError*>(errhp)));
env->checkErr(errhp, OCIAttrSet(reinterpret_cast<dvoid*>(authp), OCI_HTYPE_SESSION,
const_cast<dvoid*>(reinterpret_cast<const dvoid*>(password.c_str())), password.length(),
OCI_ATTR_PASSWORD, reinterpret_cast<OCIError*>(errhp)));
if (sysAsm)
env->checkErr(errhp, OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, OCI_SYSASM));
else
env->checkErr(errhp, OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, OCI_DEFAULT));
env->checkErr(errhp, OCIAttrSet(reinterpret_cast<dvoid*>(svchp), OCI_HTYPE_SVCCTX,
reinterpret_cast<dvoid*>(authp), 0, OCI_ATTR_SESSION, errhp));
connected = true;
}
void DatabaseConnection::disconnect() {
if (svchp != nullptr && errhp != nullptr)
OCISessionEnd(svchp, errhp, nullptr, OCI_DEFAULT);
if (svchp != nullptr && errhp != nullptr)
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
if (authp != nullptr) {
OCIHandleFree(reinterpret_cast<dvoid*>(authp), OCI_HTYPE_SESSION);
authp = nullptr;
}
if (errhp != nullptr) {
OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
errhp = nullptr;
}
if (svchp != nullptr) {
OCIHandleFree(reinterpret_cast<dvoid*>(svchp), OCI_HTYPE_SVCCTX);
svchp = nullptr;
}
if (authp != nullptr) {
OCIHandleFree(reinterpret_cast<dvoid*>(authp), OCI_HTYPE_SERVER);
authp = nullptr;
}
if (errhp != nullptr) {
OCIHandleFree(reinterpret_cast<dvoid*>(errhp), OCI_HTYPE_ERROR);
errhp = nullptr;
}
connected = false;
}
DatabaseConnection::~DatabaseConnection() {
if (connected)
disconnect();
}
}