forked from postgresql-interfaces/psqlodbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qresult.c
1494 lines (1333 loc) · 37.5 KB
/
qresult.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: qresult.c
*
* Description: This module contains functions related to
* managing result information (i.e, fetching rows
* from the backend, managing the tuple cache, etc.)
* and retrieving it. Depending on the situation, a
* QResultClass will hold either data from the backend
* or a manually built result.
*
* Classes: QResultClass (Functions prefix: "QR_")
*
* API functions: none
*
* Comments: See "readme.txt" for copyright and license information.
*---------
*/
#include "qresult.h"
#include "statement.h"
#include <libpq-fe.h>
#include "misc.h"
#include <stdio.h>
#include <string.h>
#include <limits.h>
static BOOL QR_prepare_for_tupledata(QResultClass *self);
static BOOL QR_read_tuples_from_pgres(QResultClass *, PGresult **pgres);
/*
* Used for building a Manual Result only
* All info functions call this function to create the manual result set.
*/
void
QR_set_num_fields(QResultClass *self, int new_num_fields)
{
if (!self) return;
MYLOG(0, "entering\n");
CI_set_num_fields(QR_get_fields(self), new_num_fields);
MYLOG(0, "leaving\n");
}
void
QR_set_position(QResultClass *self, SQLLEN pos)
{
self->tupleField = self->backend_tuples + ((QR_get_rowstart_in_cache(self) + pos) * self->num_fields);
}
void
QR_set_cache_size(QResultClass *self, SQLLEN cache_size)
{
self->cache_size = cache_size;
}
void
QR_set_reqsize(QResultClass *self, Int4 reqsize)
{
self->rowset_size_include_ommitted = reqsize;
}
void
QR_set_cursor(QResultClass *self, const char *name)
{
ConnectionClass *conn = QR_get_conn(self);
if (self->cursor_name)
{
if (name &&
0 == strcmp(name, self->cursor_name))
return;
free(self->cursor_name);
if (conn)
{
CONNLOCK_ACQUIRE(conn);
conn->ncursors--;
CONNLOCK_RELEASE(conn);
}
self->cursTuple = -1;
QR_set_no_cursor(self);
}
else if (NULL == name)
return;
if (name)
{
self->cursor_name = strdup(name);
if (conn)
{
CONNLOCK_ACQUIRE(conn);
conn->ncursors++;
CONNLOCK_RELEASE(conn);
}
}
else
{
self->cursor_name = NULL;
/*
* The isSqlServr() check below was added because the code was freeing
* cursors prematurely when other results with open cursors exist. The
* code was originally added for a scenario using SQL Server linked
* servers in commit c07342d22d82ea6293d27057840babfc2ff6d750.
*/
if (isSqlServr())
{
QResultClass *res;
for (res = QR_nextr(self); NULL != res; res = QR_nextr(res))
{
if (NULL != res->cursor_name)
free(res->cursor_name);
res->cursor_name = NULL;
}
}
}
}
void
QR_set_num_cached_rows(QResultClass *self, SQLLEN num_rows)
{
self->num_cached_rows = num_rows;
if (QR_synchronize_keys(self))
self->num_cached_keys = self->num_cached_rows;
}
void
QR_set_rowstart_in_cache(QResultClass *self, SQLLEN start)
{
if (QR_synchronize_keys(self))
self->key_base = start;
self->base = start;
}
void
QR_inc_rowstart_in_cache(QResultClass *self, SQLLEN base_inc)
{
if (!QR_has_valid_base(self))
MYLOG(0, " called while the cache is not ready\n");
self->base += base_inc;
if (QR_synchronize_keys(self))
self->key_base = self->base;
}
void
QR_set_fields(QResultClass *self, ColumnInfoClass *fields)
{
ColumnInfoClass *curfields = QR_get_fields(self);
if (curfields == fields)
return;
/*
* Unlink the old columninfo from this result set, freeing it if this
* was the last reference.
*/
if (NULL != curfields)
{
if (curfields->refcount > 1)
curfields->refcount--;
else
CI_Destructor(curfields);
}
self->fields = fields;
if (NULL != fields)
fields->refcount++;
}
/*
* CLASS QResult
*/
QResultClass *
QR_Constructor(void)
{
QResultClass *rv;
MYLOG(0, "entering\n");
rv = (QResultClass *) malloc(sizeof(QResultClass));
if (rv != NULL)
{
ColumnInfoClass *fields;
rv->rstatus = PORES_EMPTY_QUERY;
rv->pstatus = 0;
/* construct the column info */
rv->fields = NULL;
if (fields = CI_Constructor(), NULL == fields)
{
free(rv);
return NULL;
}
QR_set_fields(rv, fields);
rv->backend_tuples = NULL;
rv->sqlstate[0] = '\0';
rv->message = NULL;
rv->messageref = NULL;
rv->command = NULL;
rv->notice = NULL;
rv->conn = NULL;
QR_nextr(rv) = NULL;
rv->count_backend_allocated = 0;
rv->count_keyset_allocated = 0;
rv->num_total_read = 0;
rv->num_cached_rows = 0;
rv->num_cached_keys = 0;
rv->fetch_number = 0;
rv->flags = 0; /* must be cleared before calling QR_set_rowstart_in_cache() */
QR_set_rowstart_in_cache(rv, -1);
rv->key_base = -1;
rv->recent_processed_row_count = -1;
rv->cursTuple = -1;
rv->move_offset = 0;
rv->num_fields = 0;
rv->num_key_fields = PG_NUM_NORMAL_KEYS; /* CTID + OID */
rv->tupleField = NULL;
rv->cursor_name = NULL;
rv->aborted = FALSE;
rv->cache_size = 0;
rv->cmd_fetch_size = 0;
rv->rowset_size_include_ommitted = 1;
rv->move_direction = 0;
rv->keyset = NULL;
rv->reload_count = 0;
rv->rb_alloc = 0;
rv->rb_count = 0;
rv->dataFilled = FALSE;
rv->rollback = NULL;
rv->ad_alloc = 0;
rv->ad_count = 0;
rv->added_keyset = NULL;
rv->added_tuples = NULL;
rv->up_alloc = 0;
rv->up_count = 0;
rv->updated = NULL;
rv->updated_keyset = NULL;
rv->updated_tuples = NULL;
rv->dl_alloc = 0;
rv->dl_count = 0;
rv->deleted = NULL;
rv->deleted_keyset = NULL;
}
MYLOG(0, "leaving %p\n", rv);
return rv;
}
void
QR_close_result(QResultClass *self, BOOL destroy)
{
ConnectionClass *conn;
QResultClass *next;
BOOL top = TRUE;
if (!self) return;
MYLOG(0, "entering\n");
while(self)
{
/*
* If conn is defined, then we may have used "backend_tuples", so in
* case we need to, free it up. Also, close the cursor.
*/
if ((conn = QR_get_conn(self)) && conn->pqconn)
{
if (CC_is_in_trans(conn) || QR_is_withhold(self))
{
if (!QR_close(self)) /* close the cursor if there is one */
{
}
}
}
QR_free_memory(self); /* safe to call anyway */
/*
* Should have been freed in the close() but just in case...
* QR_set_cursor clears the cursor name of all the chained results too,
* so we only need to do this for the first result in the chain.
*/
if (top)
QR_set_cursor(self, NULL);
/* Free up column info */
if (destroy)
QR_set_fields(self, NULL);
/* Free command info (this is from strdup()) */
if (self->command)
{
free(self->command);
self->command = NULL;
}
/* Free message info (this is from strdup()) */
if (self->message)
{
free(self->message);
self->message = NULL;
}
/* Free notice info (this is from strdup()) */
if (self->notice)
{
free(self->notice);
self->notice = NULL;
}
/* Destruct the result object in the chain */
next = QR_nextr(self);
QR_detach(self);
if (destroy)
free(self);
/* Repeat for the next result in the chain */
self = next;
destroy = TRUE; /* always destroy chained results */
top = FALSE;
}
MYLOG(0, "leaving\n");
}
void
QR_reset_for_re_execute(QResultClass *self)
{
MYLOG(0, "entering for %p\n", self);
if (!self) return;
QR_close_result(self, FALSE);
/* reset flags etc */
self->flags = 0;
QR_set_rowstart_in_cache(self, -1);
self->recent_processed_row_count = -1;
/* clear error info etc */
self->rstatus = PORES_EMPTY_QUERY;
self->aborted = FALSE;
self->sqlstate[0] = '\0';
self->messageref = NULL;
MYLOG(0, "leaving\n");
}
void
QR_Destructor(QResultClass *self)
{
MYLOG(0, "entering\n");
if (!self) return;
QR_close_result(self, TRUE);
MYLOG(0, "leaving\n");
}
void
QR_set_command(QResultClass *self, const char *msg)
{
if (self->command)
free(self->command);
self->command = msg ? strdup(msg) : NULL;
}
void
QR_set_message(QResultClass *self, const char *msg)
{
if (self->message)
free(self->message);
self->messageref = NULL;
self->message = msg ? strdup(msg) : NULL;
}
void
QR_add_message(QResultClass *self, const char *msg)
{
char *message = self->message;
size_t alsize, pos, addlen;
if (!msg || !msg[0])
return;
addlen = strlen(msg);
if (message)
{
pos = strlen(message) + 1;
alsize = pos + addlen + 1;
}
else
{
pos = 0;
alsize = addlen + 1;
}
if (message = realloc(message, alsize), NULL == message)
return;
if (pos > 0)
message[pos - 1] = ';';
strncpy_null(message + pos, msg, addlen + 1);
self->message = message;
}
void
QR_set_notice(QResultClass *self, const char *msg)
{
if (self->notice)
free(self->notice);
self->notice = msg ? strdup(msg) : NULL;
}
void
QR_add_notice(QResultClass *self, const char *msg)
{
char *message = self->notice;
size_t alsize, pos, addlen;
if (!msg || !msg[0])
return;
addlen = strlen(msg);
if (message)
{
pos = strlen(message) + 1;
alsize = pos + addlen + 1;
}
else
{
pos = 0;
alsize = addlen + 1;
}
if (message = realloc(message, alsize), NULL == message)
return;
if (pos > 0)
message[pos - 1] = ';';
strncpy_null(message + pos, msg, addlen + 1);
self->notice = message;
}
TupleField *QR_AddNew(QResultClass *self)
{
size_t alloc;
UInt4 num_fields;
if (!self) return NULL;
MYLOG(DETAIL_LOG_LEVEL, FORMAT_ULEN "th row(%d fields) alloc=" FORMAT_LEN "\n", self->num_cached_rows, QR_NumResultCols(self), self->count_backend_allocated);
if (num_fields = QR_NumResultCols(self), !num_fields) return NULL;
if (self->num_fields <= 0)
{
self->num_fields = num_fields;
QR_set_reached_eof(self);
}
alloc = self->count_backend_allocated;
if (!self->backend_tuples)
{
self->num_cached_rows = 0;
alloc = TUPLE_MALLOC_INC;
QR_MALLOC_return_with_error(self->backend_tuples, TupleField, alloc * sizeof(TupleField) * num_fields, self, "Out of memory in QR_AddNew.", NULL);
}
else if (self->num_cached_rows >= self->count_backend_allocated)
{
alloc = self->count_backend_allocated * 2;
QR_REALLOC_return_with_error(self->backend_tuples, TupleField, alloc * sizeof(TupleField) * num_fields, self, "Out of memory in QR_AddNew.", NULL);
}
self->count_backend_allocated = alloc;
if (self->backend_tuples)
{
memset(self->backend_tuples + num_fields * self->num_cached_rows, 0, num_fields * sizeof(TupleField));
self->num_cached_rows++;
self->ad_count++;
}
return self->backend_tuples + num_fields * (self->num_cached_rows - 1);
}
void
QR_free_memory(QResultClass *self)
{
SQLLEN num_backend_rows = self->num_cached_rows;
int num_fields = self->num_fields;
MYLOG(0, "entering fcount=" FORMAT_LEN "\n", num_backend_rows);
if (self->backend_tuples)
{
ClearCachedRows(self->backend_tuples, num_fields, num_backend_rows);
free(self->backend_tuples);
self->count_backend_allocated = 0;
self->backend_tuples = NULL;
self->dataFilled = FALSE;
self->tupleField = NULL;
}
if (self->keyset)
{
ConnectionClass *conn = QR_get_conn(self);
free(self->keyset);
self->keyset = NULL;
self->count_keyset_allocated = 0;
if (self->reload_count > 0 && conn && conn->pqconn)
{
char plannm[32];
SPRINTF_FIXED(plannm, "_KEYSET_%p", self);
if (CC_is_in_error_trans(conn))
{
CC_mark_a_object_to_discard(conn, 's',plannm);
}
else
{
QResultClass *res;
char cmd[64];
SPRINTF_FIXED(cmd, "DEALLOCATE \"%s\"", plannm);
res = CC_send_query(conn, cmd, NULL, IGNORE_ABORT_ON_CONN | ROLLBACK_ON_ERROR, NULL);
QR_Destructor(res);
}
}
self->reload_count = 0;
}
if (self->rollback)
{
free(self->rollback);
self->rb_alloc = 0;
self->rb_count = 0;
self->rollback = NULL;
}
if (self->deleted)
{
free(self->deleted);
self->deleted = NULL;
}
if (self->deleted_keyset)
{
free(self->deleted_keyset);
self->deleted_keyset = NULL;
}
self->dl_alloc = 0;
self->dl_count = 0;
/* clear added info */
if (self->added_keyset)
{
free(self->added_keyset);
self->added_keyset = NULL;
}
if (self->added_tuples)
{
ClearCachedRows(self->added_tuples, num_fields, self->ad_count);
free(self->added_tuples);
self->added_tuples = NULL;
}
self->ad_alloc = 0;
self->ad_count = 0;
/* clear updated info */
if (self->updated)
{
free(self->updated);
self->updated = NULL;
}
if (self->updated_keyset)
{
free(self->updated_keyset);
self->updated_keyset = NULL;
}
if (self->updated_tuples)
{
ClearCachedRows(self->updated_tuples, num_fields, self->up_count);
free(self->updated_tuples);
self->updated_tuples = NULL;
}
self->up_alloc = 0;
self->up_count = 0;
self->num_total_read = 0;
self->num_cached_rows = 0;
self->num_cached_keys = 0;
self->cursTuple = -1;
self->pstatus = 0;
MYLOG(0, "leaving\n");
}
BOOL
QR_from_PGresult(QResultClass *self, StatementClass *stmt, ConnectionClass *conn, const char *cursor, PGresult **pgres)
{
int num_io_params, num_cached_rows;
int i;
Int2 paramType;
IPDFields *ipdopts;
Int2 lf;
int new_num_fields;
OID new_adtid, new_relid = 0, new_attid = 0;
Int2 new_adtsize;
Int4 new_atttypmod = -1;
char *new_field_name;
Int2 dummy1, dummy2;
int cidx;
BOOL reached_eof_now = FALSE;
if (NULL != conn)
/* First, get column information */
QR_set_conn(self, conn);
/* at first read in the number of fields that are in the query */
new_num_fields = PQnfields(*pgres);
QLOG(0, "\tnFields: %d\n", new_num_fields);
/* according to that allocate memory */
QR_set_num_fields(self, new_num_fields);
if (NULL == QR_get_fields(self)->coli_array)
return FALSE;
/* now read in the descriptions */
for (lf = 0; lf < new_num_fields; lf++)
{
new_field_name = PQfname(*pgres, lf);
new_relid = PQftable(*pgres, lf);
new_attid = PQftablecol(*pgres, lf);
new_adtid = (OID) PQftype(*pgres, lf);
new_adtsize = (Int2) PQfsize(*pgres, lf);
new_atttypmod = (Int4) PQfmod(*pgres, lf);
/* Subtract the header length */
switch (new_adtid)
{
case PG_TYPE_DATETIME:
case PG_TYPE_TIMESTAMP_NO_TMZONE:
case PG_TYPE_TIME:
case PG_TYPE_TIME_WITH_TMZONE:
break;
default:
new_atttypmod -= 4;
}
if (new_atttypmod < 0)
new_atttypmod = -1;
QLOG(0, "\tfieldname='%s', adtid=%d, adtsize=%d, atttypmod=%d (rel,att)=(%d,%d)\n", new_field_name, new_adtid, new_adtsize, new_atttypmod, new_relid, new_attid);
CI_set_field_info(QR_get_fields(self), lf, new_field_name, new_adtid, new_adtsize, new_atttypmod, new_relid, new_attid);
QR_set_rstatus(self, PORES_FIELDS_OK);
self->num_fields = CI_get_num_fields(QR_get_fields(self));
if (QR_haskeyset(self))
self->num_fields -= self->num_key_fields;
if (stmt && conn)
{
num_io_params = CountParameters(stmt, NULL, &dummy1, &dummy2);
if (stmt->proc_return > 0 ||
num_io_params > 0)
{
ipdopts = SC_get_IPDF(stmt);
extend_iparameter_bindings(ipdopts, stmt->num_params);
for (i = 0, cidx = 0; i < stmt->num_params; i++)
{
if (i < stmt->proc_return)
ipdopts->parameters[i].paramType = SQL_PARAM_OUTPUT;
paramType =ipdopts->parameters[i].paramType;
if (SQL_PARAM_OUTPUT == paramType ||
SQL_PARAM_INPUT_OUTPUT == paramType)
{
MYLOG(DETAIL_LOG_LEVEL, "[%d].PGType %u->%u\n", i, PIC_get_pgtype(ipdopts->parameters[i]), CI_get_oid(QR_get_fields(self), cidx));
PIC_set_pgtype(ipdopts->parameters[i], CI_get_oid(QR_get_fields(self), cidx));
cidx++;
}
}
}
}
}
/* Then, get the data itself */
num_cached_rows = self->num_cached_rows;
if (!QR_read_tuples_from_pgres(self, pgres))
return FALSE;
MYLOG(DETAIL_LOG_LEVEL, "!!%p->cursTup=" FORMAT_LEN " total_read=" FORMAT_ULEN "\n", self, self->cursTuple, self->num_total_read);
if (!QR_once_reached_eof(self) && self->cursTuple >= (Int4) self->num_total_read)
self->num_total_read = self->cursTuple + 1;
/* EOF is 'fetched < fetch requested' */
if (self->num_cached_rows - num_cached_rows < self->cmd_fetch_size)
{
MYLOG(0, "detect EOF " FORMAT_ULEN " - %d < " FORMAT_ULEN "\n", self->num_cached_rows, num_cached_rows, self->cmd_fetch_size);
reached_eof_now = TRUE;
QR_set_reached_eof(self);
}
if (reached_eof_now && self->cursTuple < (Int4) self->num_total_read)
self->cursTuple = self->num_total_read;
if (NULL != conn)
{
/* Force a read to occur in next_tuple */
QR_set_next_in_cache(self, (SQLLEN) 0);
QR_set_rowstart_in_cache(self, 0);
self->key_base = 0;
}
/*
* Also fill in command tag. (Typically, it's SELECT, but can also be
* a FETCH.)
*/
QR_set_command(self, PQcmdStatus(*pgres));
QR_set_cursor(self, cursor);
if (NULL == cursor)
QR_set_reached_eof(self);
return TRUE;
}
/*
* Procedure needed when closing cursors.
*/
void
QR_on_close_cursor(QResultClass *self)
{
QR_set_cursor(self, NULL);
}
/*
* Close the cursor and end the transaction (if no cursors left)
* We only close the cursor if other cursors are used.
*/
int
QR_close(QResultClass *self)
{
ConnectionClass *conn;
QResultClass *res;
int ret = TRUE;
conn = QR_get_conn(self);
if (self && QR_get_cursor(self))
{
if (CC_is_in_error_trans(conn))
{
if (QR_is_withhold(self))
CC_mark_a_object_to_discard(conn, 'p', QR_get_cursor(self));
}
else
{
BOOL does_commit = FALSE;
unsigned int flag = 0;
char buf[64];
flag = READ_ONLY_QUERY;
if (QR_needs_survival_check(self))
flag |= (ROLLBACK_ON_ERROR | IGNORE_ABORT_ON_CONN);
SPRINTF_FIXED(buf, "close \"%s\"", QR_get_cursor(self));
/* End the transaction if there are no cursors left on this conn */
if (CC_is_in_trans(conn) &&
CC_does_autocommit(conn) &&
CC_cursor_count(conn) <= 1)
{
MYLOG(0, "End transaction on conn=%p\n", conn);
if ((ROLLBACK_ON_ERROR & flag) == 0)
{
STRCAT_FIXED(buf, ";commit");
flag |= END_WITH_COMMIT;
QR_set_cursor(self, NULL);
}
else
does_commit = TRUE;
}
MYLOG(DETAIL_LOG_LEVEL, " Case I CC_send_query %s flag=%x\n", buf, flag);
res = CC_send_query(conn, buf, NULL, flag, NULL);
QR_Destructor(res);
if (does_commit)
{
if (!CC_commit(conn))
{
QR_set_rstatus(self, PORES_FATAL_ERROR);
QR_set_message(self, "Error ending transaction on autocommit.");
ret = FALSE;
}
}
}
QR_on_close_cursor(self);
if (!ret)
return ret;
#ifdef NOT_USED
/* End the transaction if there are no cursors left on this conn */
if (CC_does_autocommit(conn) && CC_cursor_count(conn) == 0)
{
MYLOG(0, "End transaction on conn=%p\n", conn);
if (!CC_commit(conn))
{
QR_set_rstatus(self, PORES_FATAL_ERROR);
QR_set_message(self, "Error ending transaction.");
ret = FALSE;
}
}
#endif /* NOT_USED */
}
return ret;
}
/*
* Allocate memory for receiving next tuple.
*/
static BOOL
QR_prepare_for_tupledata(QResultClass *self)
{
BOOL haskeyset = QR_haskeyset(self);
SQLULEN num_total_rows = QR_get_num_total_tuples(self);
MYLOG(DETAIL_LOG_LEVEL, "entering %p->num_fields=%d\n", self, self->num_fields);
if (!QR_get_cursor(self))
{
if (self->num_fields > 0 &&
num_total_rows >= self->count_backend_allocated)
{
SQLLEN tuple_size = self->count_backend_allocated;
MYLOG(0, "REALLOC: old_count = " FORMAT_LEN ", size = " FORMAT_SIZE_T "\n", tuple_size, (size_t) (self->num_fields * sizeof(TupleField) * tuple_size));
if (tuple_size < 1)
tuple_size = TUPLE_MALLOC_INC;
else
tuple_size *= 2;
QR_REALLOC_return_with_error(self->backend_tuples, TupleField, tuple_size * self->num_fields * sizeof(TupleField), self, "Out of memory while reading tuples.", FALSE);
self->count_backend_allocated = tuple_size;
}
if (haskeyset &&
self->num_cached_keys >= self->count_keyset_allocated)
{
SQLLEN tuple_size = self->count_keyset_allocated;
if (tuple_size < 1)
tuple_size = TUPLE_MALLOC_INC;
else
tuple_size *= 2;
QR_REALLOC_return_with_error(self->keyset, KeySet, sizeof(KeySet) * tuple_size, self, "Out of mwmory while allocating keyset", FALSE);
memset(&self->keyset[self->count_keyset_allocated],
0,
(tuple_size - self->count_keyset_allocated) * sizeof(KeySet));
self->count_keyset_allocated = tuple_size;
}
}
return TRUE;
}
static SQLLEN enlargeKeyCache(QResultClass *self, SQLLEN add_size, const char *message)
{
size_t alloc, alloc_req;
Int4 num_fields = self->num_fields;
BOOL curs = (NULL != QR_get_cursor(self));
if (add_size <= 0)
return self->count_keyset_allocated;
alloc = self->count_backend_allocated;
if (num_fields > 0 && ((alloc_req = (Int4)self->num_cached_rows + add_size) > alloc || !self->backend_tuples))
{
if (1 > alloc)
{
if (curs)
alloc = alloc_req;
else
alloc = (alloc_req > TUPLE_MALLOC_INC ? alloc_req : TUPLE_MALLOC_INC);
}
else
{
do
{
alloc *= 2;
} while (alloc < alloc_req);
}
self->count_backend_allocated = 0;
QR_REALLOC_return_with_error(self->backend_tuples, TupleField, num_fields * sizeof(TupleField) * alloc, self, message, -1);
self->count_backend_allocated = alloc;
}
alloc = self->count_keyset_allocated;
if (QR_haskeyset(self) && ((alloc_req = (Int4)self->num_cached_keys + add_size) > alloc || !self->keyset))
{
if (1 > alloc)
{
if (curs)
alloc = alloc_req;
else
alloc = (alloc_req > TUPLE_MALLOC_INC ? alloc_req : TUPLE_MALLOC_INC);
}
else
{
do
{
alloc *= 2;
} while (alloc < alloc_req);
}
self->count_keyset_allocated = 0;
QR_REALLOC_return_with_error(self->keyset, KeySet, sizeof(KeySet) * alloc, self, message, -1);
self->count_keyset_allocated = alloc;
}
return alloc;
}
SQLLEN QR_move_cursor_to_last(QResultClass *self, StatementClass *stmt)
{
char movecmd[64];
QResultClass *res;
SQLLEN moved;
ConnectionClass *conn = SC_get_conn(stmt);
if (!QR_get_cursor(self))
return 0;
if (QR_once_reached_eof(self) &&
self->cursTuple >= self->num_total_read)
return 0;
SPRINTF_FIXED(movecmd,
"move all in \"%s\"", QR_get_cursor(self));
res = CC_send_query(conn, movecmd, NULL, READ_ONLY_QUERY, stmt);
if (!QR_command_maybe_successful(res))
{
QR_Destructor(res);
SC_set_error(stmt, STMT_EXEC_ERROR, "move error occured", __FUNCTION__);
return (-1);
}
moved = (-1);
if (sscanf(res->command, "MOVE " FORMAT_ULEN, &moved) > 0)
{
moved++;
self->cursTuple += moved;
if (!QR_once_reached_eof(self))
{
self->num_total_read = self->cursTuple;
QR_set_reached_eof(self);
}
}
QR_Destructor(res);
return moved;
}
/* This function is called by fetch_tuples() AND SQLFetch() */
int
QR_next_tuple(QResultClass *self, StatementClass *stmt)
{
CSTR func = "QR_next_tuple";
int ret = TRUE;
/* Speed up access */
SQLLEN fetch_number = self->fetch_number, cur_fetch = 0;
SQLLEN num_total_rows;
SQLLEN num_backend_rows = self->num_cached_rows, num_rows_in;
Int4 num_fields = self->num_fields, fetch_size, req_size;
SQLLEN offset = 0, end_tuple;
char boundary_adjusted = FALSE;
TupleField *the_tuples = self->backend_tuples;
QResultClass *res;
/* QR_set_command() dups this string so doesn't need static */
char fetch[128];
QueryInfo qi;
ConnectionClass *conn;
ConnInfo *ci;
BOOL reached_eof_now = FALSE, curr_eof; /* detecting EOF is pretty important */
MYLOG(DETAIL_LOG_LEVEL, "Oh %p->fetch_number=" FORMAT_LEN "\n", self, self->fetch_number);
MYLOG(DETAIL_LOG_LEVEL, "in total_read=" FORMAT_ULEN " cursT=" FORMAT_LEN " currT=" FORMAT_LEN " ad=%d total=" FORMAT_ULEN " rowsetSize=%d\n", self->num_total_read, self->cursTuple, stmt->currTuple, self->ad_count, QR_get_num_total_tuples(self), self->rowset_size_include_ommitted);
num_total_rows = QR_get_num_total_tuples(self);
conn = QR_get_conn(self);
curr_eof = FALSE;
req_size = QR_get_reqsize(self);
/* Determine the optimum cache size. */
ci = &(conn->connInfo);
fetch_size = ci->drivers.fetch_max;
if ((Int4)req_size > fetch_size)
fetch_size = req_size;
if (QR_once_reached_eof(self) && self->cursTuple >= (Int4) QR_get_num_total_read(self))
curr_eof = TRUE;
#define return DONT_CALL_RETURN_FROM_HERE???
#define RETURN(code) { ret = code; goto cleanup;}
ENTER_CONN_CS(conn);
if (0 != self->move_offset)
{
char movecmd[256];
QResultClass *mres = NULL;
SQLULEN movement, moved;
movement = self->move_offset;
if (QR_is_moving_backward(self))
{
if (fetch_size > req_size)
{
SQLLEN incr_move = fetch_size - (req_size < 0 ? 1 : req_size);
movement += incr_move;
if (movement > (UInt4)(self->cursTuple + 1))
movement = self->cursTuple + 1;