-
Notifications
You must be signed in to change notification settings - Fork 2
/
descriptor.c
657 lines (613 loc) · 16.8 KB
/
descriptor.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
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*-------
* Module: descriptor.c
*
* Description: This module contains functions related to creating
* and manipulating a statement.
*
* Classes: DescriptorClass (Functions prefix: "DC_")
*
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
#include "environ.h"
#include "connection.h"
#include "descriptor.h"
#include "statement.h"
#include "qresult.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "pgapifunc.h"
void TI_Constructor(TABLE_INFO *self, const ConnectionClass *conn)
{
memset(self, 0, sizeof(TABLE_INFO));
TI_set_updatable(self);
}
void TI_Destructor(TABLE_INFO **ti, int count)
{
int i;
inolog("TI_Destructor count=%d\n", count);
if (ti)
{
for (i = 0; i < count; i++)
{
if (ti[i])
{
COL_INFO *coli = ti[i]->col_info;
if (coli)
{
mylog("!!!refcnt %p:%d -> %d\n", coli, coli->refcnt, coli->refcnt - 1);
coli->refcnt--;
if (coli->refcnt <= 0 && 0 == coli->acc_time) /* acc_time == 0 means the table is dropped */
free_col_info_contents(coli);
}
NULL_THE_NAME(ti[i]->schema_name);
NULL_THE_NAME(ti[i]->table_name);
NULL_THE_NAME(ti[i]->table_alias);
NULL_THE_NAME(ti[i]->bestitem);
NULL_THE_NAME(ti[i]->bestqual);
free(ti[i]);
ti[i] = NULL;
}
}
}
}
void FI_Constructor(FIELD_INFO *self, BOOL reuse)
{
inolog("FI_Constructor reuse=%d\n", reuse);
if (reuse)
FI_Destructor(&self, 1, FALSE);
memset(self, 0, sizeof(FIELD_INFO));
self->nullable = TRUE;
self->columnkey = -1;
self->typmod = -1;
}
void FI_Destructor(FIELD_INFO **fi, int count, BOOL freeFI)
{
int i;
inolog("FI_Destructor count=%d\n", count);
if (fi)
{
for (i = 0; i < count; i++)
{
if (fi[i])
{
NULL_THE_NAME(fi[i]->column_name);
NULL_THE_NAME(fi[i]->column_alias);
NULL_THE_NAME(fi[i]->schema_name);
NULL_THE_NAME(fi[i]->before_dot);
if (freeFI)
{
free(fi[i]);
fi[i] = NULL;
}
}
}
if (freeFI)
free(fi);
}
}
void DC_Constructor(DescriptorClass *self, BOOL embedded, StatementClass *stmt)
{
memset(self, 0, sizeof(DescriptorClass));
self->deschd.embedded = embedded;
}
static void ARDFields_free(ARDFields * self)
{
inolog("ARDFields_free %p bookmark=%p", self, self->bookmark);
if (self->bookmark)
{
free(self->bookmark);
self->bookmark = NULL;
}
inolog(" hey");
/*
* the memory pointed to by the bindings is not deallocated by the
* driver but by the application that uses that driver, so we don't
* have to care
*/
ARD_unbind_cols(self, TRUE);
}
static void APDFields_free(APDFields * self)
{
if (self->bookmark)
{
free(self->bookmark);
self->bookmark = NULL;
}
/* param bindings */
APD_free_params(self, STMT_FREE_PARAMS_ALL);
}
static void IRDFields_free(IRDFields * self)
{
/* Free the parsed field information */
if (self->fi)
{
FI_Destructor(self->fi, self->allocated, TRUE);
self->fi = NULL;
}
self->allocated = 0;
self->nfields = 0;
}
static void IPDFields_free(IPDFields * self)
{
/* param bindings */
IPD_free_params(self, STMT_FREE_PARAMS_ALL);
}
void DC_Destructor(DescriptorClass *self)
{
DescriptorHeader *deschd = &(self->deschd);
if (deschd->__error_message)
{
free(deschd->__error_message);
deschd->__error_message = NULL;
}
if (deschd->pgerror)
{
ER_Destructor(deschd->pgerror);
deschd->pgerror = NULL;
}
if (deschd->type_defined)
{
switch (deschd->desc_type)
{
case SQL_ATTR_APP_ROW_DESC:
ARDFields_free(&(self->ardf));
break;
case SQL_ATTR_APP_PARAM_DESC:
APDFields_free(&(self->apdf));
break;
case SQL_ATTR_IMP_ROW_DESC:
IRDFields_free(&(self->irdf));
break;
case SQL_ATTR_IMP_PARAM_DESC:
IPDFields_free(&(self->ipdf));
break;
}
}
}
void InitializeEmbeddedDescriptor(DescriptorClass *self, StatementClass *stmt,
UInt4 desc_type)
{
DescriptorHeader *deschd = &(self->deschd);
DC_Constructor(self, TRUE, stmt);
DC_get_conn(self) = SC_get_conn(stmt);
deschd->type_defined = TRUE;
deschd->desc_type = desc_type;
switch (desc_type)
{
case SQL_ATTR_APP_ROW_DESC:
memset(&(self->ardf), 0, sizeof(ARDFields));
stmt->ard = self;
break;
case SQL_ATTR_APP_PARAM_DESC:
memset(&(self->apdf), 0, sizeof(APDFields));
stmt->apd = self;
break;
case SQL_ATTR_IMP_ROW_DESC:
memset(&(self->irdf), 0, sizeof(IRDFields));
stmt->ird = self;
stmt->ird->irdf.stmt = stmt;
break;
case SQL_ATTR_IMP_PARAM_DESC:
memset(&(self->ipdf), 0, sizeof(IPDFields));
stmt->ipd = self;
break;
}
}
/*
* ARDFields initialize
*/
void
InitializeARDFields(ARDFields *opt)
{
memset(opt, 0, sizeof(ARDFields));
opt->size_of_rowset = 1;
opt->bind_size = 0; /* default is to bind by column */
opt->size_of_rowset_odbc2 = 1;
}
/*
* APDFields initialize
*/
void
InitializeAPDFields(APDFields *opt)
{
memset(opt, 0, sizeof(APDFields));
opt->paramset_size = 1;
opt->param_bind_type = 0; /* default is to bind by column */
opt->paramset_size_dummy = 1; /* dummy setting */
}
BindInfoClass *ARD_AllocBookmark(ARDFields *ardopts)
{
if (!ardopts->bookmark)
{
ardopts->bookmark = (BindInfoClass *) malloc(sizeof(BindInfoClass));
memset(ardopts->bookmark, 0, sizeof(BindInfoClass));
}
return ardopts->bookmark;
}
#define DESC_INCREMENT 10
char CC_add_descriptor(ConnectionClass *self, DescriptorClass *desc)
{
int i;
int new_num_descs;
DescriptorClass **descs;
mylog("CC_add_descriptor: self=%p, desc=%p\n", self, desc);
for (i = 0; i < self->num_descs; i++)
{
if (!self->descs[i])
{
DC_get_conn(desc) = self;
self->descs[i] = desc;
return TRUE;
}
}
/* no more room -- allocate more memory */
new_num_descs = DESC_INCREMENT + self->num_descs;
descs = (DescriptorClass **) realloc(self->descs, sizeof(DescriptorClass *) * new_num_descs);
if (!descs)
return FALSE;
self->descs = descs;
memset(&self->descs[self->num_descs], 0, sizeof(DescriptorClass *) *
DESC_INCREMENT);
DC_get_conn(desc) = self;
self->descs[self->num_descs] = desc;
self->num_descs = new_num_descs;
return TRUE;
}
/*
* This API allocates a Application descriptor.
*/
RETCODE SQL_API
PGAPI_AllocDesc(HDBC ConnectionHandle,
SQLHDESC *DescriptorHandle)
{
CSTR func = "PGAPI_AllocDesc";
ConnectionClass *conn = (ConnectionClass *) ConnectionHandle;
RETCODE ret = SQL_SUCCESS;
DescriptorClass *desc;
mylog("%s: entering...\n", func);
desc = (DescriptorClass *) malloc(sizeof(DescriptorClass));
if (desc)
{
memset(desc, 0, sizeof(DescriptorClass));
DC_get_conn(desc) = conn;
if (CC_add_descriptor(conn, desc))
*DescriptorHandle = desc;
else
{
free(desc);
CC_set_error(conn, CONN_STMT_ALLOC_ERROR, "Maximum number of descriptors exceeded", func);
ret = SQL_ERROR;
}
}
else
{
CC_set_error(conn, CONN_STMT_ALLOC_ERROR, "No more memory ti allocate a further descriptor", func);
ret = SQL_ERROR;
}
return ret;
}
RETCODE SQL_API
PGAPI_FreeDesc(SQLHDESC DescriptorHandle)
{
CSTR func = "PGAPI_FreeDesc";
DescriptorClass *desc = (DescriptorClass *) DescriptorHandle;
RETCODE ret = SQL_SUCCESS;
mylog("%s: entering...\n", func);
DC_Destructor(desc);
if (!desc->deschd.embedded)
{
int i;
ConnectionClass *conn = DC_get_conn(desc);
for (i = 0; i < conn->num_descs; i++)
{
if (conn->descs[i] == desc)
{
conn->descs[i] = NULL;
break;
}
}
free(desc);
}
return ret;
}
static void BindInfoClass_copy(const BindInfoClass *src, BindInfoClass *target)
{
memcpy(target, src, sizeof(BindInfoClass));
}
static void ARDFields_copy(const ARDFields *src, ARDFields *target)
{
memcpy(target, src, sizeof(ARDFields));
target->bookmark = NULL;
if (src->bookmark)
{
BindInfoClass *bookmark = ARD_AllocBookmark(target);
BindInfoClass_copy(src->bookmark, bookmark);
}
if (src->allocated <= 0)
{
target->allocated = 0;
target->bindings = NULL;
}
else
{
int i;
target->bindings = malloc(target->allocated * sizeof(BindInfoClass));
for (i = 0; i < target->allocated; i++)
BindInfoClass_copy(&src->bindings[i], &target->bindings[i]);
}
}
static void ParameterInfoClass_copy(const ParameterInfoClass *src, ParameterInfoClass *target)
{
memcpy(target, src, sizeof(ParameterInfoClass));
}
static void APDFields_copy(const APDFields *src, APDFields *target)
{
memcpy(target, src, sizeof(APDFields));
if (src->bookmark)
{
target->bookmark = malloc(sizeof(ParameterInfoClass));
ParameterInfoClass_copy(src->bookmark, target->bookmark);
}
if (src->allocated <= 0)
{
target->allocated = 0;
target->parameters = NULL;
}
else
{
int i;
target->parameters = malloc(target->allocated * sizeof(ParameterInfoClass));
for (i = 0; i < target->allocated; i++)
ParameterInfoClass_copy(&src->parameters[i], &target->parameters[i]);
}
}
static void ParameterImplClass_copy(const ParameterImplClass *src, ParameterImplClass *target)
{
memcpy(target, src, sizeof(ParameterImplClass));
}
static void IPDFields_copy(const IPDFields *src, IPDFields *target)
{
memcpy(target, src, sizeof(IPDFields));
if (src->allocated <= 0)
{
target->allocated = 0;
target->parameters = NULL;
}
else
{
int i;
target->parameters = (ParameterImplClass *) malloc(target->allocated * sizeof(ParameterImplClass));
for (i = 0; i < target->allocated; i++)
ParameterImplClass_copy(&src->parameters[i], &target->parameters[i]);
}
}
RETCODE SQL_API
PGAPI_CopyDesc(SQLHDESC SourceDescHandle,
SQLHDESC TargetDescHandle)
{
CSTR func = "PGAPI_CopyDesc";
RETCODE ret = SQL_ERROR;
DescriptorClass *src, *target;
DescriptorHeader *srchd, *targethd;
ARDFields *ard_src, *ard_tgt;
APDFields *apd_src, *apd_tgt;
IPDFields *ipd_src, *ipd_tgt;
mylog("%s: entering...\n", func);
src = (DescriptorClass *) SourceDescHandle;
target = (DescriptorClass *) TargetDescHandle;
srchd = &(src->deschd);
targethd = &(target->deschd);
if (!srchd->type_defined)
{
mylog("source type undefined\n");
DC_set_error(target, DESC_EXEC_ERROR, "source handle type undefined");
return ret;
}
if (targethd->type_defined)
{
inolog("source type=%d -> target type=%d\n", srchd->desc_type, targethd->desc_type);
if (SQL_ATTR_IMP_ROW_DESC == targethd->desc_type)
{
mylog("can't modify IRD\n");
DC_set_error(target, DESC_EXEC_ERROR, "can't copy to IRD");
return ret;
}
else if (targethd->desc_type != srchd->desc_type)
{
if (targethd->embedded)
{
mylog("src type != target type\n");
DC_set_error(target, DESC_EXEC_ERROR, "copying different type descriptor to embedded one");
return ret;
}
}
DC_Destructor(target);
}
ret = SQL_SUCCESS;
switch (srchd->desc_type)
{
case SQL_ATTR_APP_ROW_DESC:
inolog("src=%p target=%p type=%d", src, target, srchd->desc_type);
if (!targethd->type_defined)
{
targethd->desc_type = srchd->desc_type;
}
ard_src = &(src->ardf);
inolog(" rowset_size=%d bind_size=%d ope_ptr=%p off_ptr=%p\n",
ard_src->size_of_rowset, ard_src->bind_size,
ard_src->row_operation_ptr, ard_src->row_offset_ptr);
ard_tgt = &(target->ardf);
inolog(" target=%p", ard_tgt);
ARDFields_copy(ard_src, ard_tgt);
inolog(" offset_ptr=%p\n", ard_tgt->row_offset_ptr);
break;
case SQL_ATTR_APP_PARAM_DESC:
if (!targethd->type_defined)
{
targethd->desc_type = srchd->desc_type;
}
apd_src = &(src->apdf);
apd_tgt = &(target->apdf);
APDFields_copy(apd_src, apd_tgt);
break;
case SQL_ATTR_IMP_PARAM_DESC:
if (!targethd->type_defined)
{
targethd->desc_type = srchd->desc_type;
}
ipd_src = &(src->ipdf);
ipd_tgt = &(target->ipdf);
IPDFields_copy(ipd_src, ipd_tgt);
break;
default:
mylog("invalid descriptor handle type=%d\n", srchd->desc_type);
DC_set_error(target, DESC_EXEC_ERROR, "invalid descriptor type");
ret = SQL_ERROR;
}
if (SQL_SUCCESS == ret)
targethd->type_defined = TRUE;
return ret;
}
void DC_clear_error(DescriptorClass *self)
{
DescriptorHeader *deschd = &(self->deschd);
if (deschd->__error_message)
{
free(deschd->__error_message);
deschd->__error_message = NULL;
}
if (deschd->pgerror)
{
ER_Destructor(deschd->pgerror);
deschd->pgerror = NULL;
}
deschd->__error_number = 0;
deschd->error_row = 0;
deschd->error_index = 0;
}
void DC_set_error(DescriptorClass *self, int errornumber, const char *errormsg)
{
DescriptorHeader *deschd = &(self->deschd);
if (deschd->__error_message)
free(deschd->__error_message);
deschd->__error_number = errornumber;
deschd->__error_message = errormsg ? strdup(errormsg) : NULL;
}
void DC_set_errormsg(DescriptorClass *self, const char *errormsg)
{
DescriptorHeader *deschd = &(self->deschd);
if (deschd->__error_message)
free(deschd->__error_message);
deschd->__error_message = errormsg ? strdup(errormsg) : NULL;
}
const char *DC_get_errormsg(const DescriptorClass *desc)
{
return desc->deschd.__error_message;
}
int DC_get_errornumber(const DescriptorClass *desc)
{
return desc->deschd.__error_number;
}
/* Map sql commands to statement types */
static struct
{
int number;
const char * ver3str;
const char * ver2str;
} Descriptor_sqlstate[] =
{
{ DESC_ERROR_IN_ROW, "01S01", "01S01" },
{ DESC_OPTION_VALUE_CHANGED, "01S02", "01S02" },
{ DESC_OK, "00000", "00000" }, /* OK */
{ DESC_EXEC_ERROR, "HY000", "S1000" }, /* also a general error */
{ DESC_STATUS_ERROR, "HY010", "S1010" },
{ DESC_SEQUENCE_ERROR, "HY010", "S1010" }, /* Function sequence error */
{ DESC_NO_MEMORY_ERROR, "HY001", "S1001" }, /* memory allocation failure */
{ DESC_COLNUM_ERROR, "07009", "S1002" }, /* invalid column number */
{ DESC_NO_STMTSTRING, "HY001", "S1001" }, /* having no stmtstring is also a malloc problem */
{ DESC_ERROR_TAKEN_FROM_BACKEND, "HY000", "S1000" }, /* general error */
{ DESC_INTERNAL_ERROR, "HY000", "S1000" }, /* general error */
{ DESC_STILL_EXECUTING, "HY010", "S1010" },
{ DESC_NOT_IMPLEMENTED_ERROR, "HYC00", "S1C00" }, /* == 'driver not
* capable' */
{ DESC_BAD_PARAMETER_NUMBER_ERROR, "07009", "S1093" },
{ DESC_OPTION_OUT_OF_RANGE_ERROR, "HY092", "S1092" },
{ DESC_INVALID_COLUMN_NUMBER_ERROR, "07009", "S1002" },
{ DESC_RESTRICTED_DATA_TYPE_ERROR, "07006", "07006" },
{ DESC_INVALID_CURSOR_STATE_ERROR, "07005", "24000" },
{ DESC_CREATE_TABLE_ERROR, "42S01", "S0001" }, /* table already exists */
{ DESC_NO_CURSOR_NAME, "S1015", "S1015" },
{ DESC_INVALID_CURSOR_NAME, "34000", "34000" },
{ DESC_INVALID_ARGUMENT_NO, "HY024", "S1009" }, /* invalid argument value */
{ DESC_ROW_OUT_OF_RANGE, "HY107", "S1107" },
{ DESC_OPERATION_CANCELLED, "HY008", "S1008" },
{ DESC_INVALID_CURSOR_POSITION, "HY109", "S1109" },
{ DESC_VALUE_OUT_OF_RANGE, "HY019", "22003" },
{ DESC_OPERATION_INVALID, "HY011", "S1011" },
{ DESC_PROGRAM_TYPE_OUT_OF_RANGE, "?????", "?????" },
{ DESC_BAD_ERROR, "08S01", "08S01" }, /* communication link failure */
{ DESC_INVALID_OPTION_IDENTIFIER, "HY092", "HY092" },
{ DESC_RETURN_NULL_WITHOUT_INDICATOR, "22002", "22002" },
{ DESC_INVALID_DESCRIPTOR_IDENTIFIER, "HY091", "HY091" },
{ DESC_OPTION_NOT_FOR_THE_DRIVER, "HYC00", "HYC00" },
{ DESC_FETCH_OUT_OF_RANGE, "HY106", "S1106" },
{ DESC_COUNT_FIELD_INCORRECT, "07002", "07002" },
};
static PG_ErrorInfo *DC_create_errorinfo(const DescriptorClass *self)
{
const DescriptorHeader *deschd = &(self->deschd);
PG_ErrorInfo *error;
ConnectionClass *conn;
EnvironmentClass *env;
Int4 errornum;
BOOL env_is_odbc3 = TRUE;
if (deschd->pgerror)
return deschd->pgerror;
errornum = deschd->__error_number;
error = ER_Constructor(errornum, deschd->__error_message);
if (!error)
return error;
conn = DC_get_conn(self);
if (conn && (env = (EnvironmentClass *) conn->henv))
env_is_odbc3 = EN_is_odbc3(env);
errornum -= LOWEST_DESC_ERROR;
if (errornum < 0 ||
errornum >= sizeof(Descriptor_sqlstate) / sizeof(Descriptor_sqlstate[0]))
errornum = 1 - LOWEST_DESC_ERROR;
strcpy(error->sqlstate, env_is_odbc3 ? Descriptor_sqlstate[errornum].ver3str : Descriptor_sqlstate[errornum].ver2str);
return error;
}
void
DC_log_error(const char *func, const char *desc, const DescriptorClass *self)
{
#define nullcheck(a) (a ? a : "(NULL)")
if (self)
{
qlog("DESCRIPTOR ERROR: func=%s, desc='%s', errnum=%d, errmsg='%s'\n", func, desc, self->deschd.__error_number, nullcheck(self->deschd.__error_message));
mylog("DESCRIPTOR ERROR: func=%s, desc='%s', errnum=%d, errmsg='%s'\n", func, desc, self->deschd.__error_number, nullcheck(self->deschd.__error_message));
}
}
/* Returns the next SQL error information. */
RETCODE SQL_API
PGAPI_DescError(SQLHDESC hdesc,
SQLSMALLINT RecNumber,
SQLCHAR FAR * szSqlState,
SQLINTEGER FAR * pfNativeError,
SQLCHAR FAR * szErrorMsg,
SQLSMALLINT cbErrorMsgMax,
SQLSMALLINT FAR * pcbErrorMsg,
UWORD flag)
{
CSTR func = "PGAPI_DescError";
/* CC: return an error of a hdesc */
DescriptorClass *desc = (DescriptorClass *) hdesc;
DescriptorHeader *deschd = &(desc->deschd);
mylog("%s RecN=%d\n", func);
deschd->pgerror = DC_create_errorinfo(desc);
return ER_ReturnError(&(deschd->pgerror), RecNumber, szSqlState,
pfNativeError, szErrorMsg, cbErrorMsgMax,
pcbErrorMsg, flag);
}