-
Notifications
You must be signed in to change notification settings - Fork 15
/
statement.c
3312 lines (2996 loc) · 85.5 KB
/
statement.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: statement.c
*
* Description: This module contains functions related to creating
* and manipulating a statement.
*
* Classes: StatementClass (Functions prefix: "SC_")
*
* API functions: SQLAllocStmt, SQLFreeStmt
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
#ifdef WIN_MULTITHREAD_SUPPORT
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif /* _WIN32_WINNT */
#endif /* WIN_MULTITHREAD_SUPPORT */
#include "statement.h"
#include "misc.h" // strncpy_null
#include "bind.h"
#include "connection.h"
#include "multibyte.h"
#include "qresult.h"
#include "convert.h"
#include "environ.h"
#include "loadlib.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "pgapifunc.h"
/* Map sql commands to statement types */
static const struct
{
int type;
char *s;
} Statement_Type[] =
{
{
STMT_TYPE_SELECT, "SELECT"
}
,{
STMT_TYPE_INSERT, "INSERT"
}
,{
STMT_TYPE_UPDATE, "UPDATE"
}
,{
STMT_TYPE_DELETE, "DELETE"
}
,{
STMT_TYPE_PROCCALL, "CALL"
}
,{
STMT_TYPE_PROCCALL, "{"
}
,{
STMT_TYPE_SET, "SET"
}
,{
STMT_TYPE_RESET, "RESET"
}
,{
STMT_TYPE_CREATE, "CREATE"
}
,{
STMT_TYPE_DECLARE, "DECLARE"
}
,{
STMT_TYPE_FETCH, "FETCH"
}
,{
STMT_TYPE_MOVE, "MOVE"
}
,{
STMT_TYPE_CLOSE, "CLOSE"
}
,{
STMT_TYPE_PREPARE, "PREPARE"
}
,{
STMT_TYPE_EXECUTE, "EXECUTE"
}
,{
STMT_TYPE_DEALLOCATE, "DEALLOCATE"
}
,{
STMT_TYPE_DROP, "DROP"
}
,{
STMT_TYPE_START, "BEGIN"
}
,{
STMT_TYPE_START, "START"
}
,{
STMT_TYPE_TRANSACTION, "SAVEPOINT"
}
,{
STMT_TYPE_TRANSACTION, "RELEASE"
}
,{
STMT_TYPE_TRANSACTION, "COMMIT"
}
,{
STMT_TYPE_TRANSACTION, "END"
}
,{
STMT_TYPE_TRANSACTION, "ROLLBACK"
}
,{
STMT_TYPE_TRANSACTION, "ABORT"
}
,{
STMT_TYPE_LOCK, "LOCK"
}
,{
STMT_TYPE_ALTER, "ALTER"
}
,{
STMT_TYPE_GRANT, "GRANT"
}
,{
STMT_TYPE_REVOKE, "REVOKE"
}
,{
STMT_TYPE_COPY, "COPY"
}
,{
STMT_TYPE_ANALYZE, "ANALYZE"
}
,{
STMT_TYPE_NOTIFY, "NOTIFY"
}
,{
STMT_TYPE_EXPLAIN, "EXPLAIN"
}
/*
* Special-commands that cannot be run in a transaction block. This isn't
* as granular as it could be. VACUUM can never be run in a transaction
* block, but some variants of REINDEX and CLUSTER can be. CHECKPOINT
* doesn't throw an error if you do, but it cannot be rolled back so
* there's no point in beginning a new transaction for it.
*/
,{
STMT_TYPE_SPECIAL, "VACUUM"
}
,{
STMT_TYPE_SPECIAL, "REINDEX"
}
,{
STMT_TYPE_SPECIAL, "CLUSTER"
}
,{
STMT_TYPE_SPECIAL, "CHECKPOINT"
}
,{
STMT_TYPE_WITH, "WITH"
}
,{
0, NULL
}
};
static QResultClass *libpq_bind_and_exec(StatementClass *stmt);
static void SC_set_errorinfo(StatementClass *self, QResultClass *res, int errkind);
static void SC_set_error_if_not_set(StatementClass *self, int errornumber, const char *errmsg, const char *func);
RETCODE SQL_API
PGAPI_AllocStmt(HDBC hdbc,
HSTMT * phstmt, UDWORD flag)
{
CSTR func = "PGAPI_AllocStmt";
ConnectionClass *conn = (ConnectionClass *) hdbc;
StatementClass *stmt;
ARDFields *ardopts;
MYLOG(0, "entering...\n");
if (!conn)
{
CC_log_error(func, "", NULL);
return SQL_INVALID_HANDLE;
}
stmt = SC_Constructor(conn);
MYLOG(0, "**** : hdbc = %p, stmt = %p\n", hdbc, stmt);
if (!stmt)
{
CC_set_error(conn, CONN_STMT_ALLOC_ERROR, "No more memory to allocate a further SQL-statement", func);
*phstmt = SQL_NULL_HSTMT;
return SQL_ERROR;
}
if (!CC_add_statement(conn, stmt))
{
CC_set_error(conn, CONN_STMT_ALLOC_ERROR, "Maximum number of statements exceeded.", func);
SC_Destructor(stmt);
*phstmt = SQL_NULL_HSTMT;
return SQL_ERROR;
}
*phstmt = (HSTMT) stmt;
stmt->iflag = flag;
/* Copy default statement options based from Connection options */
if (0 != (PODBC_INHERIT_CONNECT_OPTIONS & flag))
{
stmt->options = stmt->options_orig = conn->stmtOptions;
stmt->ardi.ardf = conn->ardOptions;
}
else
{
InitializeStatementOptions(&stmt->options_orig);
stmt->options = stmt->options_orig;
InitializeARDFields(&stmt->ardi.ardf);
}
ardopts = SC_get_ARDF(stmt);
ARD_AllocBookmark(ardopts);
/* Save the handle for later */
stmt->phstmt = phstmt;
return SQL_SUCCESS;
}
RETCODE SQL_API
PGAPI_FreeStmt(HSTMT hstmt,
SQLUSMALLINT fOption)
{
CSTR func = "PGAPI_FreeStmt";
StatementClass *stmt = (StatementClass *) hstmt;
MYLOG(0, "entering...hstmt=%p, fOption=%hi\n", hstmt, fOption);
if (!stmt)
{
SC_log_error(func, "", NULL);
return SQL_INVALID_HANDLE;
}
SC_clear_error(stmt);
if (fOption == SQL_DROP)
{
ConnectionClass *conn = stmt->hdbc;
/* Remove the statement from the connection's statement list */
if (conn)
{
QResultClass *res;
if (STMT_EXECUTING == stmt->status)
{
SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Statement is currently executing a transaction.", func);
return SQL_ERROR; /* stmt may be executing a transaction */
}
if (conn->unnamed_prepared_stmt == stmt)
conn->unnamed_prepared_stmt = NULL;
/*
* Free any cursors and discard any result info.
* Don't detach the statement from the connection
* before freeing the associated cursors. Otherwise
* CC_cursor_count() would get wrong results.
*/
if (stmt->parsed)
{
QR_Destructor(stmt->parsed);
stmt->parsed = NULL;
}
res = SC_get_Result(stmt);
QR_Destructor(res);
SC_init_Result(stmt);
if (!CC_remove_statement(conn, stmt))
{
SC_set_error(stmt, STMT_SEQUENCE_ERROR, "Statement is currently executing a transaction.", func);
return SQL_ERROR; /* stmt may be executing a
* transaction */
}
}
if (stmt->execute_delegate)
{
PGAPI_FreeStmt(stmt->execute_delegate, SQL_DROP);
stmt->execute_delegate = NULL;
}
if (stmt->execute_parent)
stmt->execute_parent->execute_delegate = NULL;
/* Destroy the statement and free any results, cursors, etc. */
/* if the connection was already closed return error */
if (SC_Destructor(stmt) == FALSE)
return SQL_ERROR;
}
else if (fOption == SQL_UNBIND)
SC_unbind_cols(stmt);
else if (fOption == SQL_CLOSE)
{
/*
* this should discard all the results, but leave the statement
* itself in place (it can be executed again)
*/
stmt->transition_status = STMT_TRANSITION_ALLOCATED;
if (stmt->execute_delegate)
{
PGAPI_FreeStmt(stmt->execute_delegate, SQL_DROP);
stmt->execute_delegate = NULL;
}
if (!SC_recycle_statement(stmt))
{
return SQL_ERROR;
}
SC_set_Curres(stmt, NULL);
}
else if (fOption == SQL_RESET_PARAMS)
SC_free_params(stmt, STMT_FREE_PARAMS_ALL);
else
{
SC_set_error(stmt, STMT_OPTION_OUT_OF_RANGE_ERROR, "Invalid option passed to PGAPI_FreeStmt.", func);
return SQL_ERROR;
}
return SQL_SUCCESS;
}
/*
* StatementClass implementation
*/
void
InitializeStatementOptions(StatementOptions *opt)
{
memset(opt, 0, sizeof(StatementOptions));
opt->scroll_concurrency = SQL_CONCUR_READ_ONLY;
opt->cursor_type = SQL_CURSOR_FORWARD_ONLY;
opt->retrieve_data = SQL_RD_ON;
opt->use_bookmarks = SQL_UB_OFF;
opt->metadata_id = SQL_FALSE;
}
static void SC_clear_parse_status(StatementClass *self, ConnectionClass *conn)
{
self->parse_status = STMT_PARSE_NONE;
}
static void SC_init_discard_output_params(StatementClass *self)
{
ConnectionClass *conn = SC_get_conn(self);
if (!conn) return;
self->discard_output_params = 0;
if (!conn->connInfo.use_server_side_prepare)
self->discard_output_params = 1;
}
static void SC_init_parse_method(StatementClass *self)
{
ConnectionClass *conn = SC_get_conn(self);
self->parse_method = 0;
if (!conn) return;
if (0 == (PODBC_EXTERNAL_STATEMENT & self->iflag)) return;
if (self->catalog_result) return;
if (conn->connInfo.drivers.parse)
SC_set_parse_forced(self);
}
StatementClass *
SC_Constructor(ConnectionClass *conn)
{
StatementClass *rv;
rv = (StatementClass *) malloc(sizeof(StatementClass));
if (rv)
{
rv->hdbc = conn;
rv->phstmt = NULL;
rv->rhold.first = rv->rhold.last = NULL;
rv->curres = NULL;
rv->parsed = NULL;
rv->catalog_result = FALSE;
rv->prepare = NON_PREPARE_STATEMENT;
rv->prepared = NOT_YET_PREPARED;
rv->status = STMT_ALLOCATED;
rv->external = FALSE;
rv->iflag = 0;
rv->plan_name = NULL;
rv->transition_status = STMT_TRANSITION_UNALLOCATED;
rv->multi_statement = -1; /* unknown */
rv->num_params = -1; /* unknown */
rv->processed_statements = NULL;
rv->__error_message = NULL;
rv->__error_number = 0;
rv->pgerror = NULL;
rv->statement = NULL;
rv->stmt_with_params = NULL;
rv->load_statement = NULL;
rv->statement_type = STMT_TYPE_UNKNOWN;
rv->currTuple = -1;
rv->rowset_start = 0;
SC_set_rowset_start(rv, -1, FALSE);
rv->current_col = -1;
rv->bind_row = 0;
rv->from_pos = rv->load_from_pos = rv->where_pos = -1;
rv->last_fetch_count = rv->last_fetch_count_include_ommitted = 0;
rv->save_rowset_size = -1;
rv->data_at_exec = -1;
rv->current_exec_param = -1;
rv->exec_start_row = -1;
rv->exec_end_row = -1;
rv->exec_current_row = -1;
rv->put_data = FALSE;
rv->ref_CC_error = FALSE;
rv->join_info = 0;
SC_init_parse_method(rv);
rv->lobj_fd = -1;
INIT_NAME(rv->cursor_name);
/* Parse Stuff */
rv->ti = NULL;
rv->ntab = 0;
rv->num_key_fields = -1; /* unknown */
SC_clear_parse_status(rv, conn);
rv->proc_return = -1;
SC_init_discard_output_params(rv);
rv->cancel_info = 0;
/* Clear Statement Options -- defaults will be set in AllocStmt */
memset(&rv->options, 0, sizeof(StatementOptions));
InitializeEmbeddedDescriptor((DescriptorClass *)&(rv->ardi),
rv, SQL_ATTR_APP_ROW_DESC);
InitializeEmbeddedDescriptor((DescriptorClass *)&(rv->apdi),
rv, SQL_ATTR_APP_PARAM_DESC);
InitializeEmbeddedDescriptor((DescriptorClass *)&(rv->irdi),
rv, SQL_ATTR_IMP_ROW_DESC);
InitializeEmbeddedDescriptor((DescriptorClass *)&(rv->ipdi),
rv, SQL_ATTR_IMP_PARAM_DESC);
rv->miscinfo = 0;
rv->execinfo = 0;
rv->rb_or_tc = 0;
SC_reset_updatable(rv);
rv->diag_row_count = 0;
rv->stmt_time = 0;
rv->execute_delegate = NULL;
rv->execute_parent = NULL;
rv->allocated_callbacks = 0;
rv->num_callbacks = 0;
rv->callbacks = NULL;
GetDataInfoInitialize(SC_get_GDTI(rv));
PutDataInfoInitialize(SC_get_PDTI(rv));
rv->use_server_side_prepare = conn->connInfo.use_server_side_prepare;
rv->lock_CC_for_rb = FALSE;
// for batch execution
memset(&rv->stmt_deffered, 0, sizeof(rv->stmt_deffered));
if ((rv->batch_size = conn->connInfo.batch_size) < 1)
rv->batch_size = 1;
rv->exec_type = DIRECT_EXEC;
rv->count_of_deffered = 0;
rv->has_notice = 0;
INIT_STMT_CS(rv);
}
return rv;
}
char
SC_Destructor(StatementClass *self)
{
char cRet = TRUE;
CSTR func = "SC_Destructor";
QResultClass *res = SC_get_Result(self);
MYLOG(0, "entering self=%p, self->result=%p, self->hdbc=%p\n", self, res, self->hdbc);
SC_clear_error(self);
if (STMT_EXECUTING == self->status)
{
SC_set_error(self, STMT_SEQUENCE_ERROR, "Statement is currently executing a transaction.", func);
return FALSE;
}
if (res)
{
if (!self->hdbc)
res->conn = NULL; /* prevent any dbase activity */
QR_Destructor(res);
}
if (self->parsed)
{
QR_Destructor(self->parsed);
self->parsed = NULL;
}
SC_initialize_stmts(self, TRUE);
if(self->hdbc && !self->hdbc->pqconn)
{
SC_set_error(self, STMT_COMMUNICATION_ERROR, "connection error.", func);
cRet = FALSE;
}
/* Free the parsed table information */
SC_initialize_cols_info(self, FALSE, TRUE);
NULL_THE_NAME(self->cursor_name);
/* Free the parsed field information */
DC_Destructor((DescriptorClass *) SC_get_ARDi(self));
DC_Destructor((DescriptorClass *) SC_get_APDi(self));
DC_Destructor((DescriptorClass *) SC_get_IRDi(self));
DC_Destructor((DescriptorClass *) SC_get_IPDi(self));
GDATA_unbind_cols(SC_get_GDTI(self), TRUE);
PDATA_free_params(SC_get_PDTI(self), STMT_FREE_PARAMS_ALL);
if (self->__error_message)
free(self->__error_message);
if (self->pgerror)
ER_Destructor(self->pgerror);
cancelNeedDataState(self);
if (self->callbacks)
free(self->callbacks);
if (!PQExpBufferDataBroken(self->stmt_deffered))
termPQExpBuffer(&self->stmt_deffered);
DELETE_STMT_CS(self);
free(self);
MYLOG(0, "leaving\n");
return cRet;
}
void
SC_init_Result(StatementClass *self)
{
self->rhold.first = self->rhold.last = NULL;
self->curres = NULL;
MYLOG(0, "leaving(%p)\n", self);
}
void
SC_set_Result(StatementClass *self, QResultClass *first)
{
if (first != self->rhold.first)
{
QResultClass *last = NULL, *res;
MYLOG(0, "(%p, %p)\n", self, first);
QR_Destructor(self->parsed);
self->parsed = NULL;
QR_Destructor(self->rhold.first);
for (res = first; res; res = QR_nextr(res))
last = res;
self->curres = first;
self->rhold.first = first;
self->rhold.last = last;
}
}
void
SC_set_ResultHold(StatementClass *self, QResultHold rhold)
{
if (rhold.first != self->rhold.first)
{
MYLOG(0, "(%p, {%p, %p})\n", self, rhold.first, rhold.last);
QR_Destructor(self->parsed);
self->parsed = NULL;
QR_Destructor(self->rhold.first);
self->curres = rhold.first;
self->rhold = rhold;
}
else if (rhold.last != self->rhold.last)
{
self->rhold.last = rhold.last;
if (QR_nextr(rhold.last) != NULL)
SC_set_error(self, STMT_INTERNAL_ERROR, "last Result is not the last result", __FUNCTION__);
}
}
/*
* Free parameters and free the memory from the
* data-at-execution parameters that was allocated in SQLPutData.
*/
void
SC_free_params(StatementClass *self, char option)
{
if (option != STMT_FREE_PARAMS_DATA_AT_EXEC_ONLY)
{
APD_free_params(SC_get_APDF(self), option);
IPD_free_params(SC_get_IPDF(self), option);
}
PDATA_free_params(SC_get_PDTI(self), option);
self->data_at_exec = -1;
self->current_exec_param = -1;
self->put_data = FALSE;
if (option == STMT_FREE_PARAMS_ALL)
{
self->exec_start_row = -1;
self->exec_end_row = -1;
self->exec_current_row = -1;
}
}
int
statement_type(const char *statement)
{
int i;
/* ignore leading whitespace in query string */
while (*statement && (isspace((UCHAR) *statement) || *statement == '('))
statement++;
for (i = 0; Statement_Type[i].s; i++)
if (!strnicmp(statement, Statement_Type[i].s, strlen(Statement_Type[i].s)))
return Statement_Type[i].type;
return STMT_TYPE_OTHER;
}
void
SC_set_planname(StatementClass *stmt, const char *plan_name)
{
if (stmt->plan_name)
free(stmt->plan_name);
if (plan_name && plan_name[0])
stmt->plan_name = strdup(plan_name);
else
stmt->plan_name = NULL;
}
void
SC_set_rowset_start(StatementClass *stmt, SQLLEN start, BOOL valid_base)
{
QResultClass *res = SC_get_Curres(stmt);
SQLLEN incr = start - stmt->rowset_start;
MYLOG(DETAIL_LOG_LEVEL, "%p->SC_set_rowstart " FORMAT_LEN "->" FORMAT_LEN "(%s) ", stmt, stmt->rowset_start, start, valid_base ? "valid" : "unknown");
if (res != NULL)
{
BOOL valid = QR_has_valid_base(res);
MYPRINTF(DETAIL_LOG_LEVEL, ":(%p)QR is %s", res, QR_has_valid_base(res) ? "valid" : "unknown");
if (valid)
{
if (valid_base)
QR_inc_rowstart_in_cache(res, incr);
else
QR_set_no_valid_base(res);
}
else if (valid_base)
{
QR_set_has_valid_base(res);
if (start < 0)
QR_set_rowstart_in_cache(res, -1);
else
QR_set_rowstart_in_cache(res, start);
}
if (!QR_get_cursor(res))
res->key_base = start;
MYPRINTF(DETAIL_LOG_LEVEL, ":(%p)QR result=" FORMAT_LEN "(%s)", res, QR_get_rowstart_in_cache(res), QR_has_valid_base(res) ? "valid" : "unknown");
}
stmt->rowset_start = start;
MYPRINTF(DETAIL_LOG_LEVEL, ":stmt result=" FORMAT_LEN "\n", stmt->rowset_start);
}
void
SC_inc_rowset_start(StatementClass *stmt, SQLLEN inc)
{
SQLLEN start = stmt->rowset_start + inc;
SC_set_rowset_start(stmt, start, TRUE);
}
int
SC_set_current_col(StatementClass *stmt, int col)
{
if (col == stmt->current_col)
return col;
if (col >= 0)
reset_a_getdata_info(SC_get_GDTI(stmt), col + 1);
stmt->current_col = col;
return stmt->current_col;
}
void
SC_set_prepared(StatementClass *stmt, int prepared)
{
if (prepared == stmt->prepared)
;
else if (NOT_YET_PREPARED == prepared && PREPARED_PERMANENTLY == stmt->prepared)
{
ConnectionClass *conn = SC_get_conn(stmt);
if (conn)
{
ENTER_CONN_CS(conn);
if (CONN_CONNECTED == conn->status)
{
if (CC_is_in_error_trans(conn))
{
CC_mark_a_object_to_discard(conn, 's', stmt->plan_name);
}
else
{
QResultClass *res;
char dealloc_stmt[128];
SPRINTF_FIXED(dealloc_stmt, "DEALLOCATE \"%s\"", stmt->plan_name);
res = CC_send_query(conn, dealloc_stmt, NULL, IGNORE_ABORT_ON_CONN | ROLLBACK_ON_ERROR, NULL);
QR_Destructor(res);
}
}
LEAVE_CONN_CS(conn);
}
}
if (NOT_YET_PREPARED == prepared)
SC_set_planname(stmt, NULL);
stmt->prepared = prepared;
}
/*
* Initialize stmt_with_params and load_statement member pointer
* deallocating corresponding prepared plan. Also initialize
* statement member pointer if specified.
*/
RETCODE
SC_initialize_stmts(StatementClass *self, BOOL initializeOriginal)
{
ProcessedStmt *pstmt;
ProcessedStmt *next_pstmt;
ConnectionClass *conn = SC_get_conn(self);
if (self->lock_CC_for_rb)
{
if (conn)
LEAVE_CONN_CS(conn);
self->lock_CC_for_rb = FALSE;
}
if (initializeOriginal)
{
if (self->statement)
{
free(self->statement);
self->statement = NULL;
}
pstmt = self->processed_statements;
while (pstmt)
{
if (pstmt->query)
free(pstmt->query);
next_pstmt = pstmt->next;
free(pstmt);
pstmt = next_pstmt;
}
self->processed_statements = NULL;
self->prepare = NON_PREPARE_STATEMENT;
SC_set_prepared(self, NOT_YET_PREPARED);
self->statement_type = STMT_TYPE_UNKNOWN; /* unknown */
self->multi_statement = -1; /* unknown */
self->num_params = -1; /* unknown */
self->proc_return = -1; /* unknown */
self->join_info = 0;
SC_init_parse_method(self);
SC_init_discard_output_params(self);
if (conn)
self->use_server_side_prepare = conn->connInfo.use_server_side_prepare;
}
if (self->stmt_with_params)
{
free(self->stmt_with_params);
self->stmt_with_params = NULL;
}
if (self->load_statement)
{
free(self->load_statement);
self->load_statement = NULL;
}
self->has_notice = 0;
return 0;
}
BOOL SC_opencheck(StatementClass *self, const char *func)
{
QResultClass *res;
if (!self)
return FALSE;
if (self->status == STMT_EXECUTING)
{
SC_set_error(self, STMT_SEQUENCE_ERROR, "Statement is currently executing a transaction.", func);
return TRUE;
}
/*
* We can dispose the result of Describe-only any time.
*/
if (self->prepare && self->status == STMT_DESCRIBED)
{
MYLOG(0, "self->prepare && self->status == STMT_DESCRIBED\n");
return FALSE;
}
if (res = SC_get_Curres(self), NULL != res)
{
if (QR_command_maybe_successful(res) && res->backend_tuples)
{
SC_set_error(self, STMT_SEQUENCE_ERROR, "The cursor is open.", func);
return TRUE;
}
}
return FALSE;
}
RETCODE
SC_initialize_and_recycle(StatementClass *self)
{
SC_initialize_stmts(self, TRUE);
if (!SC_recycle_statement(self))
return SQL_ERROR;
return SQL_SUCCESS;
}
void
SC_reset_result_for_rerun(StatementClass *self)
{
QResultClass *res;
ColumnInfoClass *flds;
if (!self) return;
if (res = SC_get_Result(self), NULL == res)
return;
flds = QR_get_fields(res);
if (NULL == flds ||
0 == CI_get_num_fields(flds))
SC_set_Result(self, NULL);
else
{
QR_reset_for_re_execute(res);
SC_set_Curres(self, NULL);
}
}
/*
* Called from SQLPrepare if STMT_PREMATURE, or
* from SQLExecute if STMT_FINISHED, or
* from SQLFreeStmt(SQL_CLOSE)
*/
char
SC_recycle_statement(StatementClass *self)
{
CSTR func = "SC_recycle_statement";
ConnectionClass *conn;
MYLOG(0, "entering self=%p\n", self);
SC_clear_error(self);
/* This would not happen */
if (self->status == STMT_EXECUTING)
{
SC_set_error(self, STMT_SEQUENCE_ERROR, "Statement is currently executing a transaction.", func);
return FALSE;
}
if (SC_get_conn(self)->unnamed_prepared_stmt == self)
SC_get_conn(self)->unnamed_prepared_stmt = NULL;
conn = SC_get_conn(self);
switch (self->status)
{
case STMT_ALLOCATED:
/* this statement does not need to be recycled */
return TRUE;
case STMT_READY:
break;
case STMT_DESCRIBED:
break;
case STMT_FINISHED:
break;
default:
SC_set_error(self, STMT_INTERNAL_ERROR, "An internal error occurred while recycling statements", func);
return FALSE;
}
switch (self->prepared)
{
case NOT_YET_PREPARED:
case PREPARED_TEMPORARILY:
/* Free the parsed table/field information */
SC_initialize_cols_info(self, TRUE, TRUE);
MYLOG(DETAIL_LOG_LEVEL, "SC_clear_parse_status\n");
SC_clear_parse_status(self, conn);
break;
}
/* Free any cursors */
if (SC_get_Result(self))
SC_set_Result(self, NULL);
QR_Destructor(self->parsed);
self->parsed = NULL;
self->miscinfo = 0;
self->execinfo = 0;
/* self->rbonerr = 0; Never clear the bits here */
/*
* Reset only parameters that have anything to do with results
*/
self->status = STMT_READY;
self->catalog_result = FALSE; /* not very important */
self->currTuple = -1;
SC_set_rowset_start(self, -1, FALSE);
SC_set_current_col(self, -1);
self->bind_row = 0;
MYLOG(DETAIL_LOG_LEVEL, "statement=%p ommitted=0\n", self);
self->last_fetch_count = self->last_fetch_count_include_ommitted = 0;
self->__error_message = NULL;
self->__error_number = 0;
self->lobj_fd = -1;
SC_free_params(self, STMT_FREE_PARAMS_DATA_AT_EXEC_ONLY);
SC_initialize_stmts(self, FALSE);
cancelNeedDataState(self);
self->cancel_info = 0;
/*
* reset the current attr setting to the original one.
*/
self->options.scroll_concurrency = self->options_orig.scroll_concurrency;
self->options.cursor_type = self->options_orig.cursor_type;
self->options.keyset_size = self->options_orig.keyset_size;
self->options.maxLength = self->options_orig.maxLength;
self->options.maxRows = self->options_orig.maxRows;
return TRUE;
}
/*
* Scan the query wholly or partially (if the next_cmd param specified).
* Also count the number of parameters respectviely.
*/
void
SC_scanQueryAndCountParams(const char *query, const ConnectionClass *conn,
ssize_t *next_cmd, SQLSMALLINT * pcpar,
po_ind_t *multi_st, po_ind_t *proc_return)
{
const char *tstr, *tag = NULL;
size_t taglen = 0;
char tchar, bchar, escape_in_literal = '\0';
char in_literal = FALSE, in_ident_keyword = FALSE,
in_dquote_identifier = FALSE,
in_dollar_quote = FALSE, in_escape = FALSE,
in_line_comment = FALSE, del_found = FALSE;
int comment_level = 0;
po_ind_t multi = FALSE;
SQLSMALLINT num_p;
encoded_str encstr;
MYLOG(0, "entering...\n");
num_p = 0;
if (proc_return)
*proc_return = 0;
if (next_cmd)
*next_cmd = -1;
tstr = query;
make_encoded_str(&encstr, conn, tstr);
for (bchar = '\0', tchar = encoded_nextchar(&encstr); tchar; tchar = encoded_nextchar(&encstr))
{
if (MBCS_NON_ASCII(encstr)) /* multibyte char */
{
if ((UCHAR) tchar >= 0x80)
bchar = tchar;
if (in_dquote_identifier ||
in_literal ||