-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoci_util.c
133 lines (111 loc) · 3.44 KB
/
oci_util.c
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* {{{
This file is part of libtraceproc - a library for tracing Pro*C/OCI calls
Copyright (C) 2013 Georg Sauthoff <[email protected]>
This program 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 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
}}} */
#include <stdio.h>
#include <stdbool.h>
#include <ociap.h>
#include "ret_check.h"
#include "ora_util.h"
#include "oci_util.h"
#include "ocierr.c"
void oci_err(int ret, void *handle, unsigned handle_type,
const char *func, const char *filename, int line
)
{
const Oci_Err *e = oci_errs;
for (; e->str; ++e) {
if (e->val == ret) {
fprintf(stderr, "%s() %s:%d: %s", func, filename, line, e->str);
if (e->call_error_get) {
// call multiple times (with increasing no) - until OCI_NO_DATA is returned
int no = 1;
int code = 0;
char buffer[1024] = {0};
int r = OCIErrorGet(handle, no, 0, &code,
(OraText*) buffer, 1023, handle_type);
fprintf(stderr, ": code %d, %s\n", code, buffer);
} else
fprintf(stderr, "\n");
return;
}
}
fprintf(stderr, "%s() %s:%d: Unknown OCI return code: %d",
func, filename, line, ret);
}
int oci_connect(
OCIEnv **envh,
OCISvcCtx **sh,
OCIError **eh
)
{
OCIEnv *oenv = 0;
int ret = OCIEnvNlsCreate(
&oenv,
OCI_DEFAULT, // also bit-orable
0, // memory callback context
0, // memory alloc fn
0, // memory realloc fn
0, // memory free fn
0, // user memory to be allocated
0, // user memory ptr
0, // use NLS_LANG
0 // use NLS_CHAR
);
IFOCIENVRET(ret, oenv, -1);
*envh = oenv;
OCIError *error_handle = 0;
ret = OCIHandleAlloc(oenv, (void**) &error_handle, OCI_HTYPE_ERROR, 0, 0);
IFOCIRET(ret, error_handle, -1);
*eh = error_handle;
Ora_Connect c = {0};
ret = ora_init_vars(&c);
IFTRUERET(ret, 0, -1);
OCISvcCtx *service_handle = 0;
// done by Logon2
//ret = OCIHandleAlloc(&oenv, &service_handle, OCI_HTYPE_SVCCTX, 0, 0);
//IFOCIRET(ret, error_handle, -1);
ret = OCILogon2(
oenv,
error_handle,
&service_handle,
(OraText*) c.username,
strlen(c.username),
(OraText*) c.password,
strlen(c.password),
(OraText*) c.dbspec,
strlen(c.dbspec),
OCI_DEFAULT // flags are also bit-orable
);
IFOCIRET(ret, error_handle, -1);
*sh = service_handle;
return 0;
}
int oci_disconnect(
OCIEnv *env_handle,
OCISvcCtx *service_handle,
OCIError *error_handle
)
{
// not necessary because free of env should be hierachical
//ret = OCIHandleFree(service_handle, OCI_HTYPE_SVCCTX);
int ret = OCILogoff(service_handle, error_handle);
IFOCIRET(ret, error_handle, -1);
ret = OCIHandleFree(error_handle, OCI_HTYPE_ERROR);
IFTRUERET(ret, 0, -1);
ret = OCIHandleFree(env_handle, OCI_HTYPE_ENV);
IFTRUERET(ret, 0, -1);
ret = OCITerminate(OCI_DEFAULT);
IFTRUERET(ret, 0, -1);
return 0;
}