-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle.c
481 lines (376 loc) · 11.2 KB
/
handle.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "driver.h"
#include "list.h"
/**
* Declarations for internal comdb2_Free* functions.
*/
static SQLRETURN comdb2_SQLFreeEnv(env_t *phenv);
static SQLRETURN comdb2_SQLFreeConnect(dbc_t *phdbc);
static SQLRETURN comdb2_SQLFreeStmt(stmt_t *phstmt);
/*
COMDB2-ODBC internal.
Allocates an environment handle.
*/
static SQLRETURN comdb2_SQLAllocEnv(SQLHENV *phenv)
{
SQLRETURN ret;
env_t *env;
__debug("enters method.");
if((*phenv = my_calloc_typed(env_t, 1, SQLHENV)) == NULL)
ret = SQL_ERROR;
else {
env = (env_t *) *phenv;
zerofill(env, env_t, 1);
INIT_LIST_HEAD(&env->conns);
ret = SQL_SUCCESS;
}
__debug("leaves method.");
return ret;
}
/*
COMDB2-ODBC internal.
Frees an environment handle.
*/
static SQLRETURN comdb2_SQLFreeEnv(env_t *phenv)
{
SQLRETURN ret = SQL_SUCCESS;
dbc_t *dbc, *next;
__debug("enters method.");
if(!phenv)
return SQL_INVALID_HANDLE;
/* Free all associated connection handles. */
list_iterate_safe(dbc, next, &phenv->conns, list, dbc_t)
if(SQL_FAILED(ret = comdb2_SQLFreeConnect(dbc)))
return ret;
free(phenv->error);
free(phenv);
__debug("leaves method.");
return ret;
}
/*
COMDB2-ODBC internal.
Allocates a connection handle.
*/
static SQLRETURN comdb2_SQLAllocConnect(SQLHENV henv,
SQLHDBC *phdbc)
{
SQLRETURN ret;
dbc_t *dbc;
env_t *phenv = (env_t *)henv;
__debug("enters method.");
if(!henv)
return SQL_INVALID_HANDLE;
if((*phdbc = my_calloc_typed(dbc_t, 1, SQLHDBC)) == NULL)
return ENV_ODBC_ERR(ERROR_MEM_ALLOC_FAIL);
else {
dbc = (dbc_t *) *phdbc;
zerofill(*phdbc, dbc_t, 1);
dbc->env = phenv;
dbc->auto_commit = SQL_AUTOCOMMIT_DEFAULT;
dbc->brand_new = true;
list_append(&dbc->list, &phenv->conns);
INIT_LIST_HEAD(&dbc->stmts);
ret = SQL_SUCCESS;
}
__debug("leaves method.");
return ret;
}
/*
COMDB2-ODBC internal.
Frees a connection handle.
*/
static SQLRETURN comdb2_SQLFreeConnect(dbc_t *phdbc)
{
SQLRETURN ret = SQL_SUCCESS;
stmt_t *stmt, *next;
__debug("enters method.");
if(!phdbc)
return SQL_INVALID_HANDLE;
if(phdbc->in_txn) {
__warn("Transaction executing.");
return DBC_ODBC_ERR_MSG(ERROR_FUNCTION_SEQ_ERR, "A transaction is executing.");
}
/* Free all associated statement handles. */
list_iterate_safe(stmt, next, &phdbc->stmts, list, stmt_t)
if(SQL_SUCCEEDED(ret = comdb2_SQLFreeStmt(stmt)))
return ret;
/* If the connection is still connected, it should be closed. */
if(phdbc->connected && SQL_FAILED(ret = SQLDisconnect(phdbc)))
return ret;
/* Free error struct */
free(phdbc->error);
phdbc->error = NULL;
/* Remove myself from the environment. */
list_rm(&phdbc->list);
free(phdbc);
__debug("leaves method.");
return ret;
}
/*
COMDB2-ODBC internal.
Allocates a statement handle.
*/
static SQLRETURN comdb2_SQLAllocStmt(SQLHDBC hdbc, SQLHSTMT *phstmt)
{
stmt_t *stmt;
dbc_t *phdbc = (dbc_t *)hdbc;
__debug("enters method.");
if(SQL_NULL_HDBC == hdbc)
return SQL_INVALID_HANDLE;
if((*phstmt = my_calloc_typed(stmt_t, 1, SQLHSTMT)) == NULL) {
*phstmt = SQL_NULL_HSTMT;
return DBC_ODBC_ERR(ERROR_MEM_ALLOC_FAIL);
}
stmt = (stmt_t *) *phstmt;
zerofill(*phstmt, stmt_t, 1);
stmt->dbc = phdbc;
stmt->sqlh = phdbc->sqlh;
stmt->status = STMT_ALLOCATED;
stmt->changed = true;
list_append(&stmt->list, &phdbc->stmts);
__debug("leaves method.");
return SQL_SUCCESS;
}
/*
Recycles a statement handle.
Returns true/false to indicate success/failure.
*/
bool recycle_stmt(stmt_t *phstmt)
{
if(!phstmt)
return false;
__debug("enters method.");
switch(phstmt->status) {
case STMT_ALLOCATED:
__info("Allocated, nothing to do.");
return true;
case STMT_EXECUTING:
__warn("Statement executing.");
return false;
case STMT_READY:
case STMT_PREMATURE:
case STMT_FINISHED:
case STMT_EXTRACTED:
break;
default:
__fatal("Unknown status.");
return false;
}
free(phstmt->query);
phstmt->query = NULL;
free(phstmt->query_with_params);
phstmt->query_with_params = NULL;
free(phstmt->effects);
phstmt->effects = NULL;
free(phstmt->error);
phstmt->error = NULL;
/* Free bound parameters and clear cdb2 bindings. */
for(; phstmt->params_allocated > 0; --phstmt->params_allocated) {
/* Only internal_buffer needs to be freed. Other pointers will be freed by user programs. */
free(phstmt->params[phstmt->params_allocated - 1].internal_buffer);
phstmt->params[phstmt->params_allocated - 1].internal_buffer = NULL;
}
free(phstmt->params);
phstmt->params = NULL;
cdb2_clearbindings(phstmt->sqlh);
/* Free data buffers */
free(phstmt->buffers);
phstmt->buffers = NULL;
phstmt->sql_type = STMT_UNDEFINED;
phstmt->status = STMT_ALLOCATED;
phstmt->changed = true;
phstmt->col_count = 0;
__debug("leaves method.");
return true;
}
/*
COMDB2-ODBC internal.
After calling this function, the statement handle cannot bu used again.
*/
static SQLRETURN comdb2_SQLFreeStmt(stmt_t *phstmt)
{
__debug("enters method.");
if(!phstmt)
return SQL_INVALID_HANDLE;
if(phstmt->status & STMT_EXECUTING) {
/* If the statement is still executing (possibly a long query),
it cannot be freed until execution is done. */
__fatal("Statement executing.");
return STMT_ODBC_ERR_MSG(ERROR_FUNCTION_SEQ_ERR, "Statement is executing.");
}
free(phstmt->query);
free(phstmt->query_with_params);
free(phstmt->effects);
free(phstmt->error);
/* Free bound parameters and clear cdb2 bindings. */
for(; phstmt->params_allocated > 0; --phstmt->params_allocated) {
/* Only internal_buffer needs to be freed. Other pointers will be freed by user programs. */
free(phstmt->params[phstmt->params_allocated - 1].internal_buffer);
phstmt->params[phstmt->params_allocated - 1].internal_buffer = NULL;
}
free(phstmt->params);
cdb2_clearbindings(phstmt->sqlh);
/* Free data buffers */
free(phstmt->buffers);
list_rm(&phstmt->list);
free(phstmt);
__debug("leaves method.");
return SQL_SUCCESS;
}
/*
COMDB2-ODBC internal.
Unbinds a statement handle. It means all bound columns will be discarded.
*/
static SQLRETURN comdb2_unbind(stmt_t *phstmt)
{
__debug("enters method.");
if(!phstmt)
return SQL_INVALID_HANDLE;
free(phstmt->buffers);
phstmt->buffers = NULL;
phstmt->num_data_buffers = 0;
__debug("leaves method.");
return SQL_SUCCESS;
}
/*
COMDB2-ODBC internal.
Resets a statement handle. It means all bound parameters will be discarded.
*/
static SQLRETURN comdb2_reset_params(stmt_t *phstmt)
{
__debug("enters method.");
if(!phstmt)
return SQL_INVALID_HANDLE;
/* Free bound parameters and clear cdb2 bindings. */
for(; phstmt->params_allocated > 0; --phstmt->params_allocated) {
/* Only internal_buffer needs to be freed. Other pointers will be freed by user programs. */
free(phstmt->params[phstmt->params_allocated - 1].internal_buffer);
phstmt->params[phstmt->params_allocated - 1].internal_buffer = NULL;
}
free(phstmt->params);
phstmt->params = NULL;
phstmt->changed = true;
cdb2_clearbindings(phstmt->sqlh);
__debug("leaves method.");
return SQL_SUCCESS;
}
/*
COMDB2-ODBC internal.
Closes a statement handle.
*/
static SQLRETURN comdb2_SQLCloseCursor(stmt_t* phstmt)
{
int rc = CDB2_OK_DONE;
__debug("enters method.");
if(!phstmt)
return SQL_INVALID_HANDLE;
if(phstmt->status & STMT_FINISHED && SQLH_STATUS(phstmt) == SQLH_FINISHED) {
__info("Clear unextracted rows.");
while((rc = cdb2_next_record(phstmt->sqlh)) == CDB2_OK);
SET_SQLH_IDLE(phstmt);
}
phstmt->status = STMT_EXTRACTED;
/* Clear effects and error. */
free(phstmt->effects);
phstmt->effects = NULL;
free(phstmt->error);
phstmt->error = NULL;
phstmt->col_count = 0;
__debug("leaves method.");
return SQL_SUCCESS;
}
/******************
* ODBC API *
*****************/
SQLRETURN SQL_API SQLCloseCursor(SQLHSTMT hstmt)
{
return comdb2_SQLCloseCursor((stmt_t *)hstmt);
}
SQLRETURN SQL_API SQLAllocStmt(SQLHDBC hdbc, SQLHSTMT *phstmt)
{
return comdb2_SQLAllocStmt(hdbc, phstmt);
}
SQLRETURN SQL_API SQLFreeStmt(SQLHSTMT hstmt, SQLUSMALLINT option)
{
SQLRETURN ret = SQL_ERROR;
stmt_t *phstmt = (stmt_t *)hstmt;
if(!hstmt)
return SQL_INVALID_HANDLE;
switch(option) {
case SQL_CLOSE:
ret = comdb2_SQLCloseCursor(phstmt);
break;
case SQL_DROP:
ret = comdb2_SQLFreeStmt(phstmt);
break;
case SQL_UNBIND:
ret = comdb2_unbind(phstmt);
break;
case SQL_RESET_PARAMS:
ret = comdb2_reset_params(phstmt);
break;
default:
ret = STMT_ODBC_ERR(ERROR_OPTION_OUT_OF_RANGE);
break;
}
return ret;
}
SQLRETURN SQL_API SQLAllocEnv(SQLHENV *phenv)
{
return comdb2_SQLAllocEnv(phenv);
}
SQLRETURN SQL_API SQLFreeEnv(SQLHENV henv)
{
return comdb2_SQLFreeEnv(henv);
}
SQLRETURN SQL_API SQLAllocConnect(SQLHENV henv, SQLHDBC *phdbc)
{
return comdb2_SQLAllocConnect(henv, phdbc);
}
SQLRETURN SQL_API SQLFreeConnect(SQLHDBC hdbc)
{
return comdb2_SQLFreeConnect(hdbc);
}
SQLRETURN SQL_API SQLAllocHandle(
SQLSMALLINT HandleType,
SQLHANDLE InputHandle,
SQLHANDLE *OutputHandlePtr)
{
/* handle allocation is forwarded to an ODBC 2.x method according to the handle type.
In this way we can guarantee backward compatibility. */
SQLRETURN ret = SQL_ERROR;
switch(HandleType) {
case SQL_HANDLE_ENV:
ret = comdb2_SQLAllocEnv(OutputHandlePtr);
break;
case SQL_HANDLE_DBC:
ret = comdb2_SQLAllocConnect(InputHandle, OutputHandlePtr);
break;
case SQL_HANDLE_STMT:
ret = comdb2_SQLAllocStmt(InputHandle, OutputHandlePtr);
break;
case SQL_HANDLE_DESC:
default:
break;
}
return ret;
}
SQLRETURN SQL_API SQLFreeHandle(SQLSMALLINT HandleType,
SQLHANDLE Handle)
{
SQLRETURN ret = SQL_ERROR;
switch(HandleType) {
case SQL_HANDLE_ENV:
ret = comdb2_SQLFreeEnv((env_t *)Handle);
break;
case SQL_HANDLE_DBC:
ret = comdb2_SQLFreeConnect((dbc_t *)Handle);
break;
case SQL_HANDLE_STMT:
ret = comdb2_SQLFreeStmt((stmt_t *)Handle);
break;
case SQL_HANDLE_DESC:
default:
break;
}
return ret;
}