forked from apache/kylin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKO_ALLOC.CPP
626 lines (501 loc) · 22 KB
/
KO_ALLOC.CPP
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ----------------------------------------------------------------------------
//
// File: KO_ALLOC.CPP
//
// Notes: Contains function for managing allocation and release
// of the three structures/handles - Environment,
// Connection and Statement. Statements r
// encapsulated inside connection while all connections
// inside environment
//
// Exported functions:
// SQLAllocEnv
// SQLAllocConnect
// SQLAllocConnect
// SQLAllocStmt
// SQLAllocHandle
// SQLFreeStmt
// SQLDisconnect
// SQLFreeConnect
// SQLFreeHandle
// SQLFreeEnv
// ----------------------------------------------------------------------------
#include "stdafx.h"
// --------------------------- local functions --------------------------------
static eGoodBad _SQLAttachStmt ( pODBCConn pConn, pODBCStmt pStmt );
static eGoodBad _SQLDetachStmt ( pODBCConn pConn, pODBCStmt pStmt );
static RETCODE SQL_API _SQLFreeStmtResult ( pODBCStmt pHandle );
static RETCODE SQL_API _SQLFreeStmtCols ( pODBCStmt pHandle );
static RETCODE SQL_API _SQLFreeStmtParams ( pODBCStmt pHandle );
static RETCODE SQL_API _SQLFreeStmtAll ( pODBCStmt pHandle );
static RETCODE SQL_API _SQLFreeStmts ( pODBCStmt pHandle );
static eGoodBad _SQLAttachConn ( pODBCEnv pEnv, pODBCConn pConn );
static eGoodBad _SQLDetachConn ( pODBCEnv pEnv, pODBCConn pConn );
static RETCODE SQL_API _SQLFreeConnect ( pODBCConn pHandle );
static RETCODE SQL_API _SQLFreeConnects ( pODBCConn pHandle );
static RETCODE SQL_API _SQLFreeEnv ( pODBCEnv pHandle );
// -----------------------------------------------------------------------
// to allocate a environment
// -----------------------------------------------------------------------
RETCODE SQL_API SQLAllocEnv ( HENV* pOutputHandlePtr ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLAllocEnv called" ) );
pODBCEnv env = NULL;
// precaution
if ( pOutputHandlePtr == 0 ) {
__ODBCPOPMSG ( _ODBCPopMsg ( "SQLAllocEnv - bad params" ) );
return SQL_ERROR;
}
// initializations
*pOutputHandlePtr = 0;
// allocate an env
env = new ODBCEnv;
// clear the env attributes
memset ( env, 0, sizeof ( ODBCEnv ) );
// set the handle signature
( ( pODBCEnv ) env )->Sign = SQL_HANDLE_ENV;
// default values
( ( pODBCEnv ) env )->AttrODBCVersion =
SQL_OV_ODBC3; // SQL_ATTR_ODBC_VERSION 200
( ( pODBCEnv ) env )->AttrConnPooling =
SQL_CP_OFF; // SQL_ATTR_CONNECTION_POOLING 201
( ( pODBCEnv ) env )->AttrCPMatch =
SQL_CP_STRICT_MATCH; // SQL_ATTR_CP_MATCH 202
( ( pODBCEnv ) env )->AttrOutputNTS = SQL_TRUE; // SQL_ATTR_OUTPUT_NTS
// pass back to caller
*pOutputHandlePtr = ( HENV ) env;
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to allocate a connection
// -----------------------------------------------------------------------
RETCODE SQL_API SQLAllocConnect ( HENV pEnvHandle, HDBC* pOutputHandlePtr ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLAllocConnect called" ) );
pODBCConn conn;
__CHK_HANDLE ( pEnvHandle, SQL_HANDLE_ENV, SQL_ERROR );
_SQLFreeDiag ( _DIAGENV ( pEnvHandle ) );
// precaution
if ( pOutputHandlePtr == 0 ) {
__ODBCPOPMSG ( _ODBCPopMsg ( "SQLAllocConnect - bad params" ) );
return SQL_ERROR;
}
// initializations
*pOutputHandlePtr = 0;
// allocate a conn
conn = new ODBCConn;
// clear the conn attributes
memset ( conn, 0, sizeof ( ODBCConn ) );
// set the handle signature
( ( pODBCConn ) conn )->Sign = SQL_HANDLE_DBC;
// default values
( ( pODBCConn ) conn )->AccessMode = SQL_MODE_READ_ONLY;
( ( pODBCConn ) conn )->AutoIPD = SQL_FALSE;
( ( pODBCConn ) conn )->AsyncEnable = SQL_ASYNC_ENABLE_OFF;
( ( pODBCConn ) conn )->AutoCommit = SQL_AUTOCOMMIT_ON;
( ( pODBCConn ) conn )->TimeOut = 0;
( ( pODBCConn ) conn )->LoginTimeOut = 0;
( ( pODBCConn ) conn )->MetaDataID = SQL_FALSE;
( ( pODBCConn ) conn )->ODBCCursors = SQL_CUR_USE_DRIVER;
( ( pODBCConn ) conn )->Window = NULL;
( ( pODBCConn ) conn )->TxnIsolation = 0;
( ( pODBCConn ) conn )->MaxRows = 0;
( ( pODBCConn ) conn )->QueryTimeout = 0;
// attach it to link list
_SQLAttachConn ( ( pODBCEnv ) pEnvHandle, conn );
// pass back conn to caller
*pOutputHandlePtr = ( HDBC ) conn;
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to allocate a statement
// -----------------------------------------------------------------------
RETCODE SQL_API SQLAllocStmt ( HDBC pConnHandle, HSTMT* pOutputHandlePtr ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLAllocStmt called" ) );
pODBCStmt stmt = NULL;
__CHK_HANDLE ( pConnHandle, SQL_HANDLE_DBC, SQL_ERROR );
_SQLFreeDiag ( _DIAGCONN ( pConnHandle ) );
// precaution
if ( pOutputHandlePtr == 0 ) {
__ODBCPOPMSG ( _ODBCPopMsg ( "SQLAllocStmt - bad params" ) );
return SQL_ERROR;
}
// initializations
*pOutputHandlePtr = 0;
// allocate a stmt
stmt = new ODBCStmt;
// clear the stmt attributes
memset ( stmt, 0, sizeof ( ODBCStmt ) );
// set the handle signature
( ( pODBCStmt ) stmt )->Sign = SQL_HANDLE_STMT;
// default values
( ( pODBCStmt ) stmt )->AsyncEnable = SQL_ASYNC_ENABLE_OFF;
( ( pODBCStmt ) stmt )->Concurrency = SQL_CONCUR_READ_ONLY;
( ( pODBCStmt ) stmt )->CursorScroll = SQL_NONSCROLLABLE;
( ( pODBCStmt ) stmt )->CursorSensitivity = SQL_UNSPECIFIED;
( ( pODBCStmt ) stmt )->CursorType = SQL_CURSOR_FORWARD_ONLY;
( ( pODBCStmt ) stmt )->AutoIPD = SQL_FALSE;
( ( pODBCStmt ) stmt )->KeysetSize = 0;
( ( pODBCStmt ) stmt )->MetaDataID = SQL_FALSE;
( ( pODBCStmt ) stmt )->NoScan = SQL_NOSCAN_OFF;
( ( pODBCStmt ) stmt )->QryTimeout = 0;
( ( pODBCStmt ) stmt )->RetrieveData = SQL_RD_DEFAULT;
// set descriptor defaults
_SQLSetARDFieldsDefault ( & ( ( ( pODBCStmt ) stmt )->ARD ), ( pODBCStmt ) stmt );
_SQLSetAPDFieldsDefault ( & ( ( ( pODBCStmt ) stmt )->APD ), ( pODBCStmt ) stmt );
_SQLSetIRDFieldsDefault ( & ( ( ( pODBCStmt ) stmt )->IRD ), ( pODBCStmt ) stmt );
_SQLSetIPDFieldsDefault ( & ( ( ( pODBCStmt ) stmt )->IPD ), ( pODBCStmt ) stmt );
// attach it to link list
_SQLAttachStmt ( ( pODBCConn ) pConnHandle, stmt );
// pass back to caller
*pOutputHandlePtr = ( HSTMT ) stmt;
// ???? debug log message
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "New stmt: %d", stmt ) );
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to allocate a handle for env, conn or stmt
// -----------------------------------------------------------------------
RETCODE SQL_API SQLAllocHandle ( SQLSMALLINT pHandleType, SQLHANDLE pInputHandle, SQLHANDLE* pOutputHandlePtr ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLAllocHandle called" ) );
switch ( pHandleType ) {
case SQL_HANDLE_ENV:
return SQLAllocEnv ( pOutputHandlePtr ); // allocate environment
case SQL_HANDLE_DBC:
return SQLAllocConnect ( pInputHandle, pOutputHandlePtr ); // allocate connection
case SQL_HANDLE_STMT:
return SQLAllocStmt ( pInputHandle, pOutputHandlePtr ); // allocate statement
case SQL_HANDLE_DESC:
__ODBCPOPMSG ( _ODBCPopMsg ( "SQLAllocHandle - Explicit descriptor requested - not supported" ) );
return SQL_ERROR; // allocate a descriptor
}
return SQL_ERROR;
}
// -----------------------------------------------------------------------
// to free an existing statement handle
// -----------------------------------------------------------------------
RETCODE SQL_API SQLFreeStmt ( SQLHSTMT pStmt, SQLUSMALLINT pOption ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, "SQLFreeStmt called, %d with option %d", pStmt, pOption ) );
__CHK_HANDLE ( pStmt, SQL_HANDLE_STMT, SQL_ERROR );
_SQLFreeDiag ( _DIAGSTMT ( pStmt ) );
// check the operation to perform
switch ( pOption ) {
case SQL_UNBIND:
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLFreeStmt called for Col bindings" ) );
return _SQLFreeStmtCols ( ( pODBCStmt ) pStmt );
case SQL_RESET_PARAMS:
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLFreeStmt called for params" ) );
return _SQLFreeStmtParams ( ( pODBCStmt ) pStmt );
default: // include SQL_CLOSE:
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLFreeStmt called for Result" ) );
return _SQLFreeStmtResult ( ( pODBCStmt ) pStmt );
}
}
// -----------------------------------------------------------------------
// to disconnect from sever is a state is maintained on a connection
// -----------------------------------------------------------------------
RETCODE SQL_API SQLDisconnect ( HDBC pHandle ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, "SQLDisconnect called" ) );
__CHK_HANDLE ( pHandle, SQL_HANDLE_DBC, SQL_ERROR );
_SQLFreeDiag ( _DIAGCONN ( pHandle ) );
// free the connection
RETCODE code = _SQLDisconnect ( ( pODBCConn ) pHandle );
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLDisconnect exited" ) );
return code;
}
// -----------------------------------------------------------------------
// to free an existing connection handle
// -----------------------------------------------------------------------
RETCODE SQL_API SQLFreeConnect ( HDBC pHandle ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, "SQLFreeConnect called" ) );
__CHK_HANDLE ( pHandle, SQL_HANDLE_DBC, SQL_ERROR );
// free the connection
RETCODE code = _SQLFreeConnect ( ( pODBCConn ) pHandle );
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "SQLFreeConnect Exited" ) );
return code;
}
// -----------------------------------------------------------------------
// to free an existing environment handle
// -----------------------------------------------------------------------
RETCODE SQL_API SQLFreeEnv ( HENV pHandle ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, "SQLFreeEnv called" ) );
__CHK_HANDLE ( pHandle, SQL_HANDLE_ENV, SQL_ERROR );
// free the env
return _SQLFreeEnv ( ( pODBCEnv ) pHandle );
}
// -----------------------------------------------------------------------
// to free an environment, connection or stmt
// -----------------------------------------------------------------------
RETCODE SQL_API SQLFreeHandle ( SQLSMALLINT pHandleType, SQLHANDLE pHandle ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, "SQLFreeHandle called, Handle Type: %d, Handle: %d", pHandleType,
( Long ) pHandle ) );
__CHK_HANDLE ( pHandle, pHandleType, SQL_ERROR );
// as per the handle type
switch ( pHandleType ) {
case SQL_HANDLE_ENV:
return _SQLFreeEnv ( ( pODBCEnv ) pHandle ); // free environment
case SQL_HANDLE_DBC:
return _SQLFreeConnect ( ( pODBCConn ) pHandle ); // free conenction
case SQL_HANDLE_STMT:
return _SQLFreeStmtAll ( ( pODBCStmt ) pHandle ); // free statement
case SQL_HANDLE_DESC:
__ODBCPOPMSG ( _ODBCPopMsg ( "SQLFreeHandle called for descriptor" ) );
return SQL_ERROR; // free descriptor
}
return SQL_ERROR;
}
// -----------------------------------------------------------------------
// to attach a new stmt to STMT link list in CONN
// -----------------------------------------------------------------------
static eGoodBad _SQLAttachStmt ( pODBCConn pConn, pODBCStmt pStmt ) {
pODBCStmt l;
// precaution
if ( pConn == NULL || pStmt == NULL )
{ return BAD; }
// set conn as stmt container
pStmt->Conn = pConn;
// check if this is the first item
if ( pConn->Stmts == NULL ) {
// set as first and only item
pConn->Stmts = pStmt;
pStmt->Prev = NULL;
pStmt->Next = NULL;
return GOOD;
}
// move to tail item
for ( l = pConn->Stmts; l->Next != NULL; l = l->Next );
// attach to tail
l->Next = pStmt;
pStmt->Prev = l;
pStmt->Next = NULL;
return GOOD;
}
// -----------------------------------------------------------------------
// to detach an existing stmt from link list
// -----------------------------------------------------------------------
static eGoodBad _SQLDetachStmt ( pODBCConn pConn, pODBCStmt pStmt ) {
// precaution
if ( pConn == NULL || pStmt == NULL )
{ return BAD; }
if ( pStmt->Prev )
{ ( pStmt->Prev )->Next = pStmt->Next; } // attach prev to next
else
{ pConn->Stmts = pStmt->Next; } // set head to next if any
if ( pStmt->Next )
{ ( pStmt->Next )->Prev = pStmt->Prev; } // set next to prev if any
return GOOD;
}
// -----------------------------------------------------------------------
// free the cursor/results associated with the specified stmt
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeStmtResult ( pODBCStmt pHandle ) {
// note
// assumes handle already checked
// basic stmt
if ( pHandle->Stmt ) {
delete[] pHandle->Stmt;
pHandle->Stmt = NULL;
}
// basic stmt
pHandle->StmtLen = 0;
pHandle->Prepared = FALSE;
pHandle->CurRowsetStartRow = NULL; // start of current rowset
pHandle->CurRowsetStartRowPos = 0; // absolute position
pHandle->CurRowsetEndRow = NULL; // end of current rowset
pHandle->CurRowsetEndRowPos = 0; // absolute position
pHandle->RowCount = 0;
_SQLFreeIRDContent ( & ( pHandle->IRD ) );
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// free all the cols bound to a specified stmt
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeStmtCols ( pODBCStmt pHandle ) {
// note
// assumes handle already checked
return _SQLFreeARDContent ( & ( pHandle->ARD ) );
}
// -----------------------------------------------------------------------
// free all the params bound to a particular stmt
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeStmtParams ( pODBCStmt pHandle ) {
// note always called as a sub-function so no diag reset
// to be implemented along with
// other details of parms
return _SQLFreeAPDContent ( & ( pHandle->APD ) );
}
// -----------------------------------------------------------------------
// to free the stmt itself & others associated items like ARD, APD etc
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeStmtAll ( pODBCStmt pHandle ) {
_SQLFreeDiag ( _DIAGSTMT ( pHandle ) );
// note always called as a sub-function so no diag reset
pODBCConn conn;
// extract the container connection
conn = pHandle->Conn;
// check if valid
if ( conn == NULL )
{ return SQL_ERROR; }
// pluck from link-list
_SQLDetachStmt ( conn, pHandle );
// clear/free results
_SQLFreeStmtResult ( pHandle );
// clear/free col bindings
_SQLFreeStmtCols ( pHandle );
// clear/free parms bindings
_SQLFreeStmtParams ( pHandle );
// now free the structure itself
delete pHandle;
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to all the stmts starting from the specified stmt
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeStmts ( pODBCStmt pHandle ) {
pODBCStmt t, n;
// loop to iterate the list
for ( n = pHandle; n != NULL; ) {
t = n;
n = n->Next;
_SQLFreeStmtAll ( t );
}
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to attach a new conn to CONN link list in ENV
// -----------------------------------------------------------------------
static eGoodBad _SQLAttachConn ( pODBCEnv pEnv, pODBCConn pConn ) {
pODBCConn l;
// precaution
if ( pEnv == NULL || pConn == NULL )
{ return BAD; }
// set env as container for conn
pConn->Env = pEnv;
// check if this is the first item
if ( pEnv->Conns == NULL ) {
// set as first and only item
pEnv->Conns = pConn;
pConn->Prev = NULL;
pConn->Next = NULL;
return GOOD;
}
// move to tail item
for ( l = pEnv->Conns; l->Next != NULL; l = l->Next );
// attach to tail
l->Next = pConn;
pConn->Prev = l;
pConn->Next = NULL;
return GOOD;
}
// -----------------------------------------------------------------------
// to detach an existing item from ARD link-list
// -----------------------------------------------------------------------
static eGoodBad _SQLDetachConn ( pODBCEnv pEnv, pODBCConn pConn ) {
// precaution
if ( pEnv == NULL || pConn == NULL )
{ return BAD; }
if ( pConn->Prev )
{ ( pConn->Prev )->Next = pConn->Next; } // attach prev to next
else
{ pEnv->Conns = pConn->Next; } // set head to next if any
if ( pConn->Next )
{ ( pConn->Next )->Prev = pConn->Prev; } // set next to prev if any
return GOOD;
}
// -----------------------------------------------------------------------
// to free all info associated with a connection but not the conenction itself
// -----------------------------------------------------------------------
RETCODE SQL_API _SQLDisconnect ( pODBCConn pHandle ) {
pHandle->IsConnected = FALSE;
if ( pHandle->ConnectStr ) {
delete[] pHandle->ConnectStr;
pHandle->ConnectStr = NULL;
}
if ( pHandle->Server ) {
delete[] pHandle->Server;
pHandle->Server = NULL;
}
if ( pHandle->Project ) {
delete[] pHandle->Project;
pHandle->Project = NULL;
}
if ( pHandle->UserName ) {
delete[] pHandle->UserName;
pHandle->UserName = NULL;
}
if ( pHandle->Password ) {
delete[] pHandle->Password;
pHandle->Password = NULL;
}
// free all associated statements
if ( pHandle->Stmts ) { // all stmts within connection
_SQLFreeStmts ( pHandle->Stmts );
pHandle->Stmts = NULL;
}
//free meta
if ( pHandle->meta ) {
pHandle->meta = NULL;
}
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to free a specifed connection, plucks from parent/list AND FREEs it
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeConnect ( pODBCConn pHandle ) {
_SQLFreeDiag ( _DIAGCONN ( pHandle ) );
pODBCEnv env;
// disconnect
_SQLDisconnect ( pHandle );
// extract the container environment
env = pHandle->Env;
// check if valid
if ( env == NULL ) { return SQL_ERROR; }
// detach from link list
_SQLDetachConn ( env, pHandle );
// now free the structure itself
delete pHandle;
// reset
pHandle = NULL;
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to all the connections starting from the specified connection
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeConnects ( pODBCConn pHandle ) {
pODBCConn t, n;
// loop to iterate the list of connections
for ( n = pHandle; n != NULL; ) {
t = n; // save
n = n->Next; // get next
_SQLFreeConnect ( t ); // free saved
}
return SQL_SUCCESS;
}
// -----------------------------------------------------------------------
// to free the specified env
// -----------------------------------------------------------------------
static RETCODE SQL_API _SQLFreeEnv ( pODBCEnv pHandle ) {
// free diags if any
_SQLFreeDiag ( _DIAGENV ( pHandle ) );
// check if any connections are allocated
if ( pHandle->Conns ) {
//_SQLFreeConnects ( pHandle->Conns ); // relase all connections
}
delete pHandle;
return SQL_SUCCESS;
}