forked from postgresql-interfaces/psqlodbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
2196 lines (2022 loc) · 50.7 KB
/
parse.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------
* Module: parse.c
*
* Description: This module contains routines related to parsing SQL
* statements. This can be useful for two reasons:
*
* 1. So the query does not actually have to be executed
* to return data about it
*
* 2. To be able to return information about precision,
* nullability, aliases, etc. in the functions
* SQLDescribeCol and SQLColAttributes. Currently,
* Postgres doesn't return any information about
* these things in a query.
*
* Classes: none
*
* API functions: none
*
* Comments: See "readme.txt" for copyright and license information.
*--------
*/
/* Multibyte support Eiji Tokuya 2001-03-15 */
#include "psqlodbc.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "statement.h"
#include "connection.h"
#include "qresult.h"
#include "pgtypes.h"
#include "pgapifunc.h"
#include "catfunc.h"
#include "multibyte.h"
#include "misc.h"
#define FLD_INCR 32
#define TAB_INCR 8
#define COLI_INCR 16
#define COLI_RECYCLE 128
static const char *getNextToken(int ccsc, char escape_in_literal, const char *s, char *token, int smax, char *delim, char *quote, char *dquote, char *numeric);
static void getColInfo(COL_INFO *col_info, FIELD_INFO *fi, int k);
static char searchColInfo(COL_INFO *col_info, FIELD_INFO *fi);
static BOOL getColumnsInfo(ConnectionClass *, TABLE_INFO *, OID, StatementClass *);
Int4 FI_precision(const FIELD_INFO *fi)
{
OID ftype;
if (!fi) return -1;
ftype = FI_type(fi);
switch (ftype)
{
case PG_TYPE_NUMERIC:
return fi->column_size;
case PG_TYPE_DATETIME:
case PG_TYPE_TIMESTAMP_NO_TMZONE:
return fi->decimal_digits;
}
return 0;
}
Int4 FI_scale(const FIELD_INFO *fi)
{
OID ftype;
if (!fi) return -1;
ftype = FI_type(fi);
switch (ftype)
{
case PG_TYPE_NUMERIC:
return fi->decimal_digits;
}
return 0;
}
static const char *
getNextToken(
int ccsc, /* client encoding */
char escape_ch,
const char *s, char *token, int smax, char *delim, char *quote, char *dquote, char *numeric)
{
size_t out = 0;
size_t taglen;
char in_quote, in_dollar_quote, in_escape;
const UCHAR *tag, *tagend;
encoded_str encstr;
char escape_in_literal;
const UCHAR *tstr = (const UCHAR *) s;
UCHAR tchar, qc;
if (smax <= 1)
return NULL;
smax--;
/* skip leading delimiters */
while (isspace(*tstr) || *tstr == ',')
{
/* MYLOG(0, "skipping '%c'\n", *tstr); */
tstr++;
}
if (*tstr == '\0')
{
token[0] = '\0';
return NULL;
}
if (quote)
*quote = FALSE;
if (dquote)
*dquote = FALSE;
if (numeric)
*numeric = FALSE;
encoded_str_constr(&encstr, ccsc, (const char *) tstr);
/* get the next token */
for (tchar = encoded_nextchar(&encstr); tchar && out < smax; tstr++, tchar = encoded_nextchar(&encstr))
{
if (MBCS_NON_ASCII(encstr))
{
token[out++] = tchar;
continue;
}
if (isspace(tchar) || tchar == ',')
break;
/* Handle quoted stuff */
in_quote = in_dollar_quote = FALSE;
taglen = 0;
tag = NULL;
escape_in_literal = '\0';
if (out == 0)
{
qc = tchar;
if (qc == DOLLAR_QUOTE)
{
in_quote = in_dollar_quote = TRUE;
tag = tstr;
taglen = 1;
if (tagend = (const UCHAR *) strchr((const char *) tstr + 1, DOLLAR_QUOTE), NULL != tagend)
taglen = tagend - tstr + 1;
tstr += (taglen - 1);
encoded_position_shift(&encstr, taglen - 1);
if (quote)
*quote = TRUE;
}
else if (qc == LITERAL_QUOTE)
{
in_quote = TRUE;
if (quote)
*quote = TRUE;
escape_in_literal = escape_ch;
if (!escape_in_literal)
{
if (LITERAL_EXT == tstr[-1])
escape_in_literal = ESCAPE_IN_LITERAL;
}
}
else if (qc == IDENTIFIER_QUOTE)
{
in_quote = TRUE;
if (dquote)
*dquote = TRUE;
}
} /* out == 0 */
if (in_quote) /* dquote, dollar_quote */
{
in_escape = FALSE;
for (tstr++, tchar = encoded_nextchar(&encstr); tchar != '\0' && out != smax; tstr++, tchar = encoded_nextchar(&encstr))
{
if (MBCS_NON_ASCII(encstr))
{
token[out++] = tchar;
continue;
}
if (in_escape)
in_escape = FALSE;
else if (tchar == qc)
{
if (!in_dollar_quote)
{
/*
* Peek at the next byte to see if this is a '' or
* "", i.e a quote character that has been escaped
* by doubling it.
*/
if (tstr[1] == qc)
{
tstr++;
tchar = encoded_nextchar(&encstr);
}
else
break;
}
else if (strncmp((const char *) tstr, (const char *) tag, taglen) == 0)
{
tstr += (taglen - 1);
tchar = encoded_position_shift(&encstr, taglen - 1);
break;
}
token[out++] = tchar;
}
else if (LITERAL_QUOTE == qc && tchar == escape_in_literal)
{
in_escape = TRUE;
}
else
{
token[out++] = tchar;
}
} /* for */
if (tchar == qc)
tstr++;
break;
} /* in_quote */
/* Check for numeric literals */
if (out == 0 && isdigit(tchar))
{
if (numeric)
*numeric = TRUE;
token[out++] = tchar;
tstr++;
while ((isalnum(*tstr) || *tstr == '.') && out < smax)
{
token[out++] = *tstr;
tstr++;
}
break;
}
if (ispunct(tchar) && tchar != '_')
{
MYLOG(0, "got ispunct: s[] = '%c'\n", tchar);
if (out == 0)
{
token[out++] = tchar;
tstr++;
}
break;
}
if (out < smax)
token[out++] = tchar;
} /* for */
/* MYLOG(0, "done -- s[] = '%c'\n", *tstr); */
token[out] = '\0';
/* find the delimiter */
while (isspace(*tstr))
tstr++;
/* return the most priority delimiter */
if (*tstr == ',')
{
if (delim)
*delim = *tstr;
}
else if (*tstr == '\0')
{
if (delim)
*delim = '\0';
}
else
{
if (delim)
*delim = ' ';
}
/* skip trailing blanks */
while (isspace(*tstr))
tstr++;
return (const char *) tstr;
}
static void
getColInfo(COL_INFO *col_info, FIELD_INFO *fi, int k)
{
char *str;
MYLOG(DETAIL_LOG_LEVEL, "entering non-manual result\n");
fi->dquote = TRUE;
STR_TO_NAME(fi->column_name, QR_get_value_backend_text(col_info->result, k, COLUMNS_COLUMN_NAME));
fi->columntype = (OID) QR_get_value_backend_int(col_info->result, k, COLUMNS_FIELD_TYPE, NULL);
fi->column_size = QR_get_value_backend_int(col_info->result, k, COLUMNS_PRECISION, NULL);
fi->length = QR_get_value_backend_int(col_info->result, k, COLUMNS_LENGTH, NULL);
if (str = QR_get_value_backend_text(col_info->result, k, COLUMNS_SCALE), str)
fi->decimal_digits = atoi(str);
else
fi->decimal_digits = -1;
fi->nullable = QR_get_value_backend_int(col_info->result, k, COLUMNS_NULLABLE, NULL);
fi->display_size = QR_get_value_backend_int(col_info->result, k, COLUMNS_DISPLAY_SIZE, NULL);
fi->auto_increment = QR_get_value_backend_int(col_info->result, k, COLUMNS_AUTO_INCREMENT, NULL);
}
static char
searchColInfo(COL_INFO *col_info, FIELD_INFO *fi)
{
int k,
cmp, attnum, atttypmod;
OID basetype;
const char *col;
MYLOG(DETAIL_LOG_LEVEL, "entering num_cols=" FORMAT_ULEN " col=%s\n", QR_get_num_cached_tuples(col_info->result), PRINT_NAME(fi->column_name));
if (fi->attnum < 0)
return FALSE;
for (k = 0; k < QR_get_num_cached_tuples(col_info->result); k++)
{
if (fi->attnum > 0)
{
attnum = QR_get_value_backend_int(col_info->result, k, COLUMNS_PHYSICAL_NUMBER, NULL);
if (basetype = (OID) strtoul(QR_get_value_backend_text(col_info->result, k, COLUMNS_BASE_TYPEID), NULL, 10), 0 == basetype)
basetype = (OID) strtoul(QR_get_value_backend_text(col_info->result, k, COLUMNS_FIELD_TYPE), NULL, 10);
atttypmod = QR_get_value_backend_int(col_info->result, k, COLUMNS_ATTTYPMOD, NULL);
MYLOG(DETAIL_LOG_LEVEL, "%d attnum=%d\n", k, attnum);
if (attnum == fi->attnum &&
basetype == fi->basetype &&
atttypmod == fi->typmod)
{
getColInfo(col_info, fi, k);
MYLOG(0, "PARSE: searchColInfo by attnum=%d\n", attnum);
return TRUE;
}
}
else if (NAME_IS_VALID(fi->column_name))
{
col = QR_get_value_backend_text(col_info->result, k, COLUMNS_COLUMN_NAME);
MYLOG(DETAIL_LOG_LEVEL, "%d col=%s\n", k, col);
if (fi->dquote)
cmp = strcmp(col, GET_NAME(fi->column_name));
else
cmp = stricmp(col, GET_NAME(fi->column_name));
if (!cmp)
{
if (!fi->dquote)
STR_TO_NAME(fi->column_name, col);
getColInfo(col_info, fi, k);
MYLOG(0, "PARSE: \n");
return TRUE;
}
}
}
return FALSE;
}
/*
* lower the unquoted name
*/
static
void lower_the_name(char *name, ConnectionClass *conn, BOOL dquote)
{
if (!dquote)
{
char *ptr;
encoded_str encstr;
make_encoded_str(&encstr, conn, name);
/* lower case table name */
for (ptr = name; *ptr; ptr++)
{
encoded_nextchar(&encstr);
if (!MBCS_NON_ASCII(encstr))
*ptr = tolower((UCHAR) *ptr);
}
}
}
/*
* Check relhasoids(before PG12), relhssubclass and get some relevant information.
*/
BOOL CheckPgClassInfo(StatementClass *stmt)
{
const COL_INFO *coli;
int table_info;
TABLE_INFO *ti;
BOOL hasoids = FALSE, hassubclass =FALSE, keyFound = FALSE;
MYLOG(0, "Entering\n");
if (0 != SC_checked_hasoids(stmt))
return TRUE;
if (!stmt->ti || !stmt->ti[0])
return FALSE;
ti = stmt->ti[0];
MYLOG(DETAIL_LOG_LEVEL, "ti->col_info=%p\n", ti->col_info);
if (TI_checked_hasoids(ti))
;
else if (coli = ti->col_info, NULL != coli)
{
table_info = coli->table_info;
if (0 == (table_info & TBINFO_HASSUBCLASS))
{
TI_set_has_no_subclass(ti);
}
else
{
hassubclass = TRUE;
TI_set_hassubclass(ti);
STR_TO_NAME(ti->bestitem, TABLEOID_NAME);
STRX_TO_NAME(ti->bestqual, "\"" TABLEOID_NAME "\" = %u");
}
if (!hassubclass)
{
if (0 == (table_info & TBINFO_HASOIDS))
{
TI_set_has_no_oids(ti);
}
else
{
hasoids = TRUE;
TI_set_hasoids(ti);
STR_TO_NAME(ti->bestitem, OID_NAME);
STRX_TO_NAME(ti->bestqual, "\"" OID_NAME "\" = %u");
}
}
ti->table_oid = coli->table_oid;
if (!hasoids && !hassubclass)
{
QResultClass *res = coli->result;
int num_tuples = res ? QR_get_num_cached_tuples(res) : -1;
if (num_tuples > 0)
{
int i;
for (i = 0; i < num_tuples; i++)
{
if (QR_get_value_backend_int(res, i, COLUMNS_AUTO_INCREMENT, NULL) != 0&&
QR_get_value_backend_int(res, i, COLUMNS_FIELD_TYPE, NULL) == PG_TYPE_INT4)
{
char query[512];
STR_TO_NAME(ti->bestitem, QR_get_value_backend_text(res, i, COLUMNS_COLUMN_NAME));
SPRINTF_FIXED(query, "\"%s\" = %%d", SAFE_NAME(ti->bestitem));
STRX_TO_NAME(ti->bestqual, query);
break;
}
}
}
}
TI_set_hasoids_checked(ti);
}
else
return FALSE;
stmt->num_key_fields = PG_NUM_NORMAL_KEYS;
if (TI_has_subclass(ti))
keyFound = FALSE;
else if (TI_has_oids(ti))
keyFound = TRUE;
else if (NAME_IS_NULL(ti->bestqual))
{
keyFound = TRUE;
stmt->num_key_fields--;
}
else
keyFound = TRUE;
MYLOG(DETAIL_LOG_LEVEL, "subclass=%d oids=%d bestqual=%s keyFound=%d num_key_fields=%d\n", TI_has_subclass(ti), TI_has_oids(ti), PRINT_NAME(ti->bestqual), keyFound, stmt->num_key_fields);
SC_set_checked_hasoids(stmt, keyFound);
return TRUE;
}
static BOOL increaseNtab(StatementClass *stmt, const char *func)
{
TABLE_INFO **ti = stmt->ti, *wti;
if (!(stmt->ntab % TAB_INCR))
{
SC_REALLOC_return_with_error(ti, TABLE_INFO *, (stmt->ntab + TAB_INCR) * sizeof(TABLE_INFO *), stmt, "PGAPI_AllocStmt failed in parse_statement for TABLE_INFO", FALSE);
stmt->ti = ti;
}
wti = ti[stmt->ntab] = (TABLE_INFO *) malloc(sizeof(TABLE_INFO));
if (wti == NULL)
{
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "PGAPI_AllocStmt failed in parse_statement for TABLE_INFO(2).", func);
return FALSE;
}
TI_Constructor(wti, SC_get_conn(stmt));
stmt->ntab++;
return TRUE;
}
static void setNumFields(IRDFields *irdflds, size_t numFields)
{
FIELD_INFO **fi = irdflds->fi;
size_t nfields = irdflds->nfields;
if (numFields < nfields)
{
int i;
for (i = (int) numFields; i < (int) nfields; i++)
{
if (fi[i])
fi[i]->flag = 0;
}
}
irdflds->nfields = (UInt4) numFields;
}
void SC_initialize_cols_info(StatementClass *stmt, BOOL DCdestroy, BOOL parseReset)
{
IRDFields *irdflds = SC_get_IRDF(stmt);
/* Free the parsed table information */
if (stmt->ti)
{
TI_Destructor(stmt->ti, stmt->ntab);
free(stmt->ti);
stmt->ti = NULL;
}
stmt->ntab = 0;
if (DCdestroy) /* Free the parsed field information */
DC_Destructor((DescriptorClass *) SC_get_IRD(stmt));
else
setNumFields(irdflds, 0);
if (parseReset)
{
stmt->parse_status = STMT_PARSE_NONE;
SC_reset_updatable(stmt);
}
}
static BOOL allocateFields(IRDFields *irdflds, size_t sizeRequested)
{
FIELD_INFO **fi = irdflds->fi;
size_t alloc_size, incr_size;
if (sizeRequested <= irdflds->allocated)
return TRUE;
alloc_size = (0 != irdflds->allocated ? irdflds->allocated : FLD_INCR);
for (; alloc_size < sizeRequested; alloc_size *= 2)
;
incr_size = sizeof(FIELD_INFO *) * (alloc_size - irdflds->allocated);
fi = (FIELD_INFO **) realloc(fi, alloc_size * sizeof(FIELD_INFO *));
if (!fi)
{
irdflds->fi = NULL;
irdflds->allocated = irdflds->nfields = 0;
return FALSE;
}
memset(&fi[irdflds->allocated], 0, incr_size);
irdflds->fi = fi;
irdflds->allocated = (SQLSMALLINT) alloc_size;
return TRUE;
}
/*
* This function may not be called but when it is called ...
*/
static void xxxxx(StatementClass *stmt, FIELD_INFO *fi, QResultClass *res, int i)
{
STR_TO_NAME(fi->column_alias, QR_get_fieldname(res, i));
fi->basetype = QR_get_field_type(res, i);
if (0 == fi->columntype)
fi->columntype = fi->basetype;
if (fi->attnum < 0)
{
fi->nullable = FALSE;
fi->updatable = FALSE;
}
else if (fi->attnum > 0)
{
int unknowns_as = 0;
int type = pg_true_type(SC_get_conn(stmt), fi->columntype, fi->basetype);
fi->nullable = TRUE; /* probably ? */
fi->column_size = pgtype_column_size(stmt, type, i, unknowns_as);
fi->length = pgtype_buffer_length(stmt, type, i, unknowns_as);
fi->decimal_digits = pgtype_decimal_digits(stmt, type, i);
fi->display_size = pgtype_display_size(stmt, type, i, unknowns_as);
}
if (NAME_IS_NULL(fi->column_name))
{
switch (fi->attnum)
{
case CTID_ATTNUM:
STR_TO_NAME(fi->column_name, "ctid");
break;
case OID_ATTNUM:
STR_TO_NAME(fi->column_name, OID_NAME);
break;
case XMIN_ATTNUM:
STR_TO_NAME(fi->column_name, XMIN_NAME);
break;
}
}
}
/*
* SQLColAttribute tries to set the FIELD_INFO (using protocol 3).
*/
static BOOL
ColAttSet(StatementClass *stmt, TABLE_INFO *rti)
{
CSTR func = "ColAttSet";
QResultClass *res = SC_get_ExecdOrParsed(stmt);
IRDFields *irdflds = SC_get_IRDF(stmt);
COL_INFO *col_info = NULL;
FIELD_INFO **fi, *wfi;
OID reloid = 0;
Int2 attid;
int i, num_fields;
BOOL fi_reuse, updatable, call_xxxxx;
MYLOG(0, "entering\n");
if (reloid = rti->table_oid, 0 == reloid)
return FALSE;
if (0 != (rti->flags & TI_COLATTRIBUTE))
return TRUE;
col_info = rti->col_info;
if (!QR_command_maybe_successful(res))
return FALSE;
if (num_fields = QR_NumPublicResultCols(res), num_fields <= 0)
return FALSE;
fi = irdflds->fi;
if (num_fields > (int) irdflds->allocated)
{
if (!allocateFields(irdflds, num_fields))
return FALSE;
fi = irdflds->fi;
}
setNumFields(irdflds, num_fields);
updatable = TI_is_updatable(rti);
MYLOG(0, "updatable=%d tab=%d fields=%d", updatable, stmt->ntab, num_fields);
if (updatable)
{
if (1 > stmt->ntab)
updatable = FALSE;
}
MYPRINTF(0, "->%d\n", updatable);
for (i = 0; i < num_fields; i++)
{
if (reloid == (OID) QR_get_relid(res, i))
{
if (wfi = fi[i], NULL == wfi)
{
wfi = (FIELD_INFO *) malloc(sizeof(FIELD_INFO));
if (wfi == NULL)
{
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "Couldn't allocate memory for field info.", func);
return FALSE;
}
fi_reuse = FALSE;
fi[i] = wfi;
}
else if (FI_is_applicable(wfi))
continue;
else
fi_reuse = TRUE;
FI_Constructor(wfi, fi_reuse);
attid = (Int2) QR_get_attid(res, i);
wfi->attnum = attid;
wfi->basetype = QR_get_field_type(res, i);
wfi->typmod = QR_get_atttypmod(res, i);
call_xxxxx = TRUE;
if (searchColInfo(col_info, wfi))
{
STR_TO_NAME(wfi->column_alias, QR_get_fieldname(res, i));
wfi->basetype = QR_get_field_type(res, i);
wfi->updatable = updatable;
call_xxxxx = FALSE;
}
else
{
if (attid > 0)
{
if (getColumnsInfo(NULL, rti, reloid, stmt) &&
searchColInfo(col_info, wfi))
{
STR_TO_NAME(wfi->column_alias, QR_get_fieldname(res, i));
wfi->basetype = QR_get_field_type(res, i);
wfi->updatable = updatable;
call_xxxxx= FALSE;
}
}
}
if (call_xxxxx)
xxxxx(stmt, wfi, res, i);
wfi->ti = rti;
wfi->flag |= FIELD_COL_ATTRIBUTE;
}
}
if (stmt->updatable < 0)
{
if (stmt->ntab > 1)
updatable = FALSE;
SC_set_updatable(stmt, updatable);
}
rti->flags |= TI_COLATTRIBUTE;
return TRUE;
}
static BOOL
getCOLIfromTable(ConnectionClass *conn, pgNAME *schema_name, pgNAME table_name,
COL_INFO **coli)
{
int colidx;
BOOL found = FALSE;
*coli = NULL;
if (NAME_IS_NULL(table_name))
return TRUE;
if (NAME_IS_NULL(*schema_name))
{
const char *curschema = CC_get_current_schema(conn);
/*
* Though current_schema() doesn't have
* much sense in PostgreSQL, we first
* check the current_schema() when no
* explicit schema name is specified.
*/
if (curschema)
{
for (colidx = 0; colidx < conn->ntables; colidx++)
{
if (!NAMEICMP(conn->col_info[colidx]->table_name, table_name) &&
!stricmp(SAFE_NAME(conn->col_info[colidx]->schema_name), curschema))
{
MYLOG(0, "FOUND col_info table='%s' current schema='%s'\n", PRINT_NAME(table_name), curschema);
found = TRUE;
STR_TO_NAME(*schema_name, curschema);
break;
}
}
}
if (!found)
{
QResultClass *res;
char token[256], relcnv[128];
BOOL tblFound = FALSE;
/*
* We also have to check as follows.
*/
SPRINTF_FIXED(token,
"select nspname from pg_namespace n, pg_class c"
" where c.relnamespace=n.oid and c.oid='%s'::regclass",
identifierEscape((const SQLCHAR *) SAFE_NAME(table_name), SQL_NTS, conn, relcnv, sizeof(relcnv), TRUE));
res = CC_send_query(conn, token, NULL, READ_ONLY_QUERY, NULL);
if (QR_command_maybe_successful(res))
{
if (QR_get_num_total_tuples(res) == 1)
{
tblFound = TRUE;
STR_TO_NAME(*schema_name, QR_get_value_backend_text(res, 0, 0));
}
}
QR_Destructor(res);
if (!tblFound)
return FALSE;
}
}
if (!found && NAME_IS_VALID(*schema_name))
{
for (colidx = 0; colidx < conn->ntables; colidx++)
{
if (!NAMEICMP(conn->col_info[colidx]->table_name, table_name) &&
!NAMEICMP(conn->col_info[colidx]->schema_name, *schema_name))
{
MYLOG(0, "FOUND col_info table='%s' schema='%s'\n", PRINT_NAME(table_name), PRINT_NAME(*schema_name));
found = TRUE;
break;
}
}
}
*coli = found ? conn->col_info[colidx] : NULL;
return TRUE; /* success */
}
static BOOL
getColumnsInfo(ConnectionClass *conn, TABLE_INFO *wti, OID greloid, StatementClass *stmt)
{
BOOL found = FALSE;
RETCODE result;
HSTMT hcol_stmt = NULL;
StatementClass *col_stmt;
QResultClass *res;
MYLOG(0, "entering Getting PG_Columns for table %u(%s)\n", greloid, PRINT_NAME(wti->table_name));
if (NULL == conn)
conn = SC_get_conn(stmt);
result = PGAPI_AllocStmt(conn, &hcol_stmt, 0);
if (!SQL_SUCCEEDED(result))
{
if (stmt)
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "PGAPI_AllocStmt failed in parse_statement for columns.", __FUNCTION__);
goto cleanup;
}
col_stmt = (StatementClass *) hcol_stmt;
if (greloid)
result = PGAPI_Columns(hcol_stmt, NULL, 0,
NULL, 0, NULL, 0, NULL, 0,
PODBC_SEARCH_BY_IDS, greloid, 0);
else
result = PGAPI_Columns(hcol_stmt, NULL, 0,
(SQLCHAR *) SAFE_NAME(wti->schema_name), SQL_NTS,
(SQLCHAR *) SAFE_NAME(wti->table_name), SQL_NTS,
NULL, 0,
PODBC_NOT_SEARCH_PATTERN, 0, 0);
MYLOG(0, " Past PG_Columns\n");
res = SC_get_ExecdOrParsed(col_stmt);
if (SQL_SUCCEEDED(result)
&& res != NULL && QR_get_num_cached_tuples(res) > 0)
{
BOOL coli_exist = FALSE;
COL_INFO *coli = NULL, *ccoli = NULL, *tcoli;
int k;
time_t acctime = 0;
MYLOG(0, " Success\n");
if (greloid != 0)
{
for (k = 0; k < conn->ntables; k++)
{
tcoli = conn->col_info[k];
if (tcoli->table_oid == greloid)
{
coli = tcoli;
coli_exist = TRUE;
break;
}
}
}
if (!coli_exist)
{
for (k = 0; k < conn->ntables; k++)
{
tcoli = conn->col_info[k];
if (0 < tcoli->refcnt)
continue;
if ((0 == tcoli->table_oid &&
NAME_IS_NULL(tcoli->table_name)) ||
strnicmp(SAFE_NAME(tcoli->schema_name), "pg_temp_", 8) == 0)
{
coli = tcoli;
coli_exist = TRUE;
break;
}
if (NULL == ccoli ||
tcoli->acc_time < acctime)
{
ccoli = tcoli;
acctime = tcoli->acc_time;
}
}
if (!coli_exist &&
NULL != ccoli &&
conn->ntables >= COLI_RECYCLE)
{
coli_exist = TRUE;
coli = ccoli;
}
}
if (coli_exist)
{
free_col_info_contents(coli);
}
else
{
if (conn->ntables >= conn->coli_allocated)
{
Int2 new_alloc;
COL_INFO **col_info;
new_alloc = conn->coli_allocated * 2;
if (new_alloc <= conn->ntables)
new_alloc = COLI_INCR;
MYLOG(0, "PARSE: Allocating col_info at ntables=%d\n", conn->ntables);
col_info = (COL_INFO **) realloc(conn->col_info, new_alloc * sizeof(COL_INFO *));
if (!col_info)
{
if (stmt)
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "PGAPI_AllocStmt failed in parse_statement for col_info.", __FUNCTION__);
goto cleanup;
}
conn->col_info = col_info;
conn->coli_allocated = new_alloc;
}
MYLOG(0, "PARSE: malloc at conn->col_info[%d]\n", conn->ntables);
coli = conn->col_info[conn->ntables] = (COL_INFO *) malloc(sizeof(COL_INFO));
}
if (!coli)
{
if (stmt)
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "PGAPI_AllocStmt failed in parse_statement for col_info(2).", __FUNCTION__);
goto cleanup;
}
col_info_initialize(coli);
coli->result = res;
if (res && QR_get_num_cached_tuples(res) > 0)
{
int num_tuples = QR_get_num_cached_tuples(res);
int i;
if (!greloid)
greloid = (OID) strtoul(QR_get_value_backend_text(res, 0, COLUMNS_TABLE_OID), NULL, 10);
if (!wti->table_oid)
wti->table_oid = greloid;
if (NAME_IS_NULL(wti->schema_name))
STR_TO_NAME(wti->schema_name,
QR_get_value_backend_text(res, 0, COLUMNS_SCHEMA_NAME));
if (NAME_IS_NULL(wti->table_name))
STR_TO_NAME(wti->table_name,
QR_get_value_backend_text(res, 0, COLUMNS_TABLE_NAME));
for (i = 0; i < num_tuples; i++)
{
if (NULL != QR_get_value_backend_text(res, 0, COLUMNS_TABLE_INFO))
{
coli->table_info = QR_get_value_backend_int(res, 0, COLUMNS_TABLE_INFO, NULL);
break;
}
}
}
MYLOG(DETAIL_LOG_LEVEL, "#2 %p->table_name=%s(%u)\n", wti, PRINT_NAME(wti->table_name), wti->table_oid);
/*
* Store the table name and the SQLColumns result
* structure
*/
if (NAME_IS_VALID(wti->schema_name))
{
NAME_TO_NAME(coli->schema_name, wti->schema_name);
}
else
NULL_THE_NAME(coli->schema_name);
NAME_TO_NAME(coli->table_name, wti->table_name);
coli->table_oid = wti->table_oid;
/*
* The connection will now free the result structures, so
* make sure that the statement doesn't free it
*/
SC_init_Result(col_stmt);
if (!coli_exist)
conn->ntables++;
if (res && QR_get_num_cached_tuples(res) > 0)
MYLOG(DETAIL_LOG_LEVEL, "oid item == %s\n", (const char *) QR_get_value_backend_text(res, 0, 3));
MYLOG(0, "Created col_info table='%s', ntables=%d\n", PRINT_NAME(wti->table_name), conn->ntables);
/* Associate a table from the statement with a SQLColumn info */
found = TRUE;
coli->refcnt++;
wti->col_info = coli;
}
cleanup:
if (hcol_stmt)
PGAPI_FreeStmt(hcol_stmt, SQL_DROP);
return found;
}
BOOL getCOLIfromTI(const char *func, ConnectionClass *conn, StatementClass *stmt, const OID reloid, TABLE_INFO **pti)
{
BOOL colatt = FALSE, found = FALSE;
OID greloid = reloid;
TABLE_INFO *wti = *pti;
COL_INFO *coli;
MYLOG(DETAIL_LOG_LEVEL, "entering reloid=%u ti=%p\n", reloid, wti);
if (!conn)
conn = SC_get_conn(stmt);
if (!wti) /* SQLColAttribute case */
{
int i;
if (0 == greloid)
return FALSE;
if (!stmt)
return FALSE;
colatt = TRUE;
for (i = 0; i < stmt->ntab; i++)