-
Notifications
You must be signed in to change notification settings - Fork 9
/
odbc_env.c
356 lines (309 loc) · 9.03 KB
/
odbc_env.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
*/
#include "odbc_portable.h"
#include "odbc_env.h"
#include "sqlext.h"
#include "odbc_diag_record.h"
#include "odbc_connection.h"
#include "odbc_util.h"
#include "cas_cci.h"
PRIVATE RETCODE connection_end_tran (ODBC_CONNECTION * conn,
short completion_type);
/* odbc_environments :
* global enviroment handle list head
*/
static ODBC_ENV *odbc_environments = NULL;
/************************************************************************
* name: odbc_alloc_env
* arguments:
* ODBC_ENV **envptr
* returns/side-effects:
* RETCODE - odbc api return code
* description:
* NOTE:
************************************************************************/
PUBLIC RETCODE
odbc_alloc_env (ODBC_ENV ** envptr)
{
ODBC_ENV *env;
env = (ODBC_ENV *) UT_ALLOC (sizeof (ODBC_ENV));
if (env == NULL)
{
*envptr = (ODBC_ENV *) SQL_NULL_HENV;
return ODBC_ERROR;
}
else
{
memset (env, 0, sizeof (ODBC_ENV));
env->handle_type = SQL_HANDLE_ENV;
env->diag = odbc_alloc_diag ();
env->program_name = UT_MAKE_STRING ("ODBC", -1);
/* Default Attribute value */
env->attr_odbc_version = 0;
env->attr_connection_pooling = SQL_CP_DEFAULT;
env->attr_cp_match = SQL_CP_MATCH_DEFAULT;
env->attr_output_nts = SQL_TRUE;
env->next = odbc_environments;
odbc_environments = env;
*envptr = env;
}
return ODBC_SUCCESS;
}
/************************************************************************
* name: odbc_free_env
* arguments:
* ODBC_ENV *env
* returns/side-effects:
* RETCODE - odbc api return code
* description:
* NOTE:
************************************************************************/
PUBLIC RETCODE
odbc_free_env (ODBC_ENV * env)
{
ODBC_ENV *e, *prev;
if (env->conn != NULL)
{
/* HY010 - DM */
odbc_set_diag (env->diag, "HY010", 0, NULL);
return ODBC_ERROR;
}
/* remove from list */
for (e = odbc_environments, prev = NULL; e != NULL && e != env; e = e->next)
{
prev = e;
}
if (e == env)
{
if (prev != NULL)
{
prev->next = env->next;
}
else
{
odbc_environments = env->next;
}
}
odbc_free_diag (env->diag, FREE_ALL);
env->diag = NULL;
NC_FREE (env->program_name);
UT_FREE (env);
return ODBC_SUCCESS;
}
/************************************************************************
* name: odbc_set_env_attr
* arguments:
* ODBC_ENV *env - environment handle
* attribute - attribute type
* valueptr - generic value pointer
* stringlength - SQL_IS_INTEGER(-6) or string length
* returns/side-effects:
* RETCODE
* description:
*
* NOTE:
* diagnostic에 대해서 아직 structure가 설정이 되지 않아서 SQLSTATE를
* 설정하지 못한다. structure에 반영한 후 각 state 값을 설정하도록
* 한다.
************************************************************************/
PUBLIC RETCODE
odbc_set_env_attr (ODBC_ENV * env,
long attribute, void *valueptr, long stringlength)
{
if (valueptr == NULL)
{
odbc_set_diag (env->diag, "HY009", 0, NULL);
return ODBC_ERROR;
}
/* valueptr will be a 32-bit integer value or
* point to a null-terminated character string
*/
switch (attribute)
{
case SQL_ATTR_CONNECTION_POOLING:
odbc_set_diag (env->diag, "HYC00", 0, NULL);
return ODBC_ERROR;
break;
case SQL_ATTR_CP_MATCH:
odbc_set_diag (env->diag, "HYC00", 0, NULL);
return ODBC_ERROR;
break;
case SQL_ATTR_ODBC_VERSION:
switch ((long) valueptr)
{
case SQL_OV_ODBC3:
case SQL_OV_ODBC2:
env->attr_odbc_version = (long) valueptr;
break;
default:
odbc_set_diag (env->diag, "HY024", 0, NULL);
return ODBC_ERROR;
break;
}
break;
case SQL_ATTR_OUTPUT_NTS:
switch ((long) valueptr)
{
case SQL_TRUE:
env->attr_output_nts = (unsigned long) valueptr;
break;
case SQL_FALSE:
odbc_set_diag (env->diag, "HYC00", 0, NULL);
return ODBC_ERROR;
break;
default:
odbc_set_diag (env->diag, "HY024", 0, NULL);
return ODBC_ERROR;
break;
}
break;
default:
odbc_set_diag (env->diag, "HY092", 0, NULL);
return ODBC_ERROR;
break;
}
return ODBC_SUCCESS;
}
/************************************************************************
* name: odbc_get_env_attr
* arguments:
* ODBC_ENV *env
* returns/side-effects:
* RETCODE - odbc api return code
* description:
* NOTE:
************************************************************************/
PUBLIC
odbc_get_env_attr (ODBC_ENV * env,
long attribute,
void *value_ptr,
long buffer_length, long *string_length_ptr)
{
switch (attribute)
{
case SQL_ATTR_ODBC_VERSION:
if (value_ptr != NULL)
*((unsigned long *) value_ptr) = env->attr_odbc_version;
if (string_length_ptr != NULL)
*string_length_ptr = sizeof (long);
break;
case SQL_ATTR_OUTPUT_NTS:
if (value_ptr != NULL)
*((unsigned long *) value_ptr) = env->attr_output_nts;
if (string_length_ptr != NULL)
*string_length_ptr = sizeof (long);
break;
default:
odbc_set_diag (env->diag, "HY092", 0, NULL);
return ODBC_ERROR;
}
return ODBC_SUCCESS;
}
/************************************************************************
* name: odbc_end_tran
* arguments:
* returns/side-effects:
* description:
* NOTE:
************************************************************************/
PUBLIC RETCODE
odbc_end_tran (short handle_type, void *handle, short completion_type)
{
ODBC_CONNECTION *conn;
RETCODE rc;
if ((handle_type != SQL_HANDLE_ENV && handle_type != SQL_HANDLE_DBC) ||
(handle_type == SQL_HANDLE_ENV
&& ((ODBC_ENV *) handle)->handle_type != SQL_HANDLE_ENV)
|| (handle_type == SQL_HANDLE_DBC
&& ((ODBC_CONNECTION *) handle)->handle_type != SQL_HANDLE_DBC))
{
return ODBC_INVALID_HANDLE;
}
if (handle_type == SQL_HANDLE_ENV)
{
for (conn = ((ODBC_ENV *) handle)->conn; conn;
conn = ((ODBC_CONNECTION *) conn)->next)
{
rc =
connection_end_tran ((ODBC_CONNECTION *) conn, completion_type);
if (rc < 0)
{
return ODBC_ERROR;
}
}
}
else
{
rc = connection_end_tran ((ODBC_CONNECTION *) handle, completion_type);
if (rc < 0)
{
return ODBC_ERROR;
}
}
return ODBC_SUCCESS;
}
/************************************************************************
* name: connection_end_tran
* arguments:
* returns/side-effects:
* description:
* NOTE:
************************************************************************/
PRIVATE RETCODE
connection_end_tran (ODBC_CONNECTION * conn, short completion_type)
{
ODBC_STATEMENT *stmt;
int cci_rc;
T_CCI_ERROR cci_err_buf;
char type;
if (completion_type == SQL_COMMIT)
{
type = CCI_TRAN_COMMIT;
}
else
{
type = CCI_TRAN_ROLLBACK;
}
if (conn->connhd > 0)
{
cci_rc = cci_end_tran (conn->connhd, type, &cci_err_buf);
if (cci_rc < 0)
{
odbc_set_diag_by_cci (conn->diag, cci_rc, &cci_err_buf);
return ODBC_ERROR;
}
}
// delete all open cursor
for (stmt = conn->statements; stmt; stmt = stmt->next)
{
odbc_close_cursor (stmt);
}
return ODBC_SUCCESS;
}