forked from postgresql-interfaces/psqlodbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.c
1775 lines (1607 loc) · 44.6 KB
/
execute.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: execute.c
*
* Description: This module contains routines related to
* preparing and executing an SQL statement.
*
* Classes: n/a
*
* API functions: SQLPrepare, SQLExecute, SQLExecDirect, SQLTransact,
* SQLCancel, SQLNativeSql, SQLParamData, SQLPutData
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
#include "psqlodbc.h"
#include "misc.h"
#include <stdio.h>
#include <string.h>
#ifndef WIN32
#include <ctype.h>
#endif /* WIN32 */
#include "environ.h"
#include "connection.h"
#include "statement.h"
#include "qresult.h"
#include "convert.h"
#include "bind.h"
#include "pgtypes.h"
#include "lobj.h"
#include "pgapifunc.h"
/* Perform a Prepare on the SQL statement */
RETCODE SQL_API
PGAPI_Prepare(HSTMT hstmt,
const SQLCHAR * szSqlStr,
SQLINTEGER cbSqlStr)
{
CSTR func = "PGAPI_Prepare";
StatementClass *self = (StatementClass *) hstmt;
RETCODE retval = SQL_SUCCESS;
BOOL prepared;
MYLOG(0, "entering...\n");
#define return DONT_CALL_RETURN_FROM_HERE???
/* StartRollbackState(self); */
/*
* According to the ODBC specs it is valid to call SQLPrepare multiple
* times. In that case, the bound SQL statement is replaced by the new
* one
*/
prepared = self->prepared;
SC_set_prepared(self, NOT_YET_PREPARED);
switch (self->status)
{
case STMT_DESCRIBED:
MYLOG(0, "**** STMT_DESCRIBED, recycle\n");
SC_recycle_statement(self); /* recycle the statement, but do
* not remove parameter bindings */
break;
case STMT_FINISHED:
MYLOG(0, "**** STMT_FINISHED, recycle\n");
SC_recycle_statement(self); /* recycle the statement, but do
* not remove parameter bindings */
break;
case STMT_ALLOCATED:
MYLOG(0, "**** STMT_ALLOCATED, copy\n");
self->status = STMT_READY;
break;
case STMT_READY:
MYLOG(0, "**** STMT_READY, change SQL\n");
if (NOT_YET_PREPARED != prepared)
SC_recycle_statement(self); /* recycle the statement */
break;
case STMT_EXECUTING:
MYLOG(0, "**** STMT_EXECUTING, error!\n");
SC_set_error(self, STMT_SEQUENCE_ERROR, "PGAPI_Prepare(): The handle does not point to a statement that is ready to be executed", func);
retval = SQL_ERROR;
goto cleanup;
default:
SC_set_error(self, STMT_INTERNAL_ERROR, "An Internal Error has occured -- Unknown statement status.", func);
retval = SQL_ERROR;
goto cleanup;
}
SC_initialize_stmts(self, TRUE);
if (!szSqlStr)
{
SC_set_error(self, STMT_NO_MEMORY_ERROR, "the query is NULL", func);
retval = SQL_ERROR;
goto cleanup;
}
if (!szSqlStr[0])
self->statement = strdup("");
else
self->statement = make_string(szSqlStr, cbSqlStr, NULL, 0);
if (!self->statement)
{
SC_set_error(self, STMT_NO_MEMORY_ERROR, "No memory available to store statement", func);
retval = SQL_ERROR;
goto cleanup;
}
self->prepare = PREPARE_STATEMENT;
self->statement_type = statement_type(self->statement);
/* Check if connection is onlyread (only selects are allowed) */
if (CC_is_onlyread(SC_get_conn(self)) && STMT_UPDATE(self))
{
SC_set_error(self, STMT_EXEC_ERROR, "Connection is readonly, only select statements are allowed.", func);
retval = SQL_ERROR;
goto cleanup;
}
cleanup:
#undef return
MYLOG(DETAIL_LOG_LEVEL, "leaving %d\n", retval);
return retval;
}
/* Performs the equivalent of SQLPrepare, followed by SQLExecute. */
RETCODE SQL_API
PGAPI_ExecDirect(HSTMT hstmt,
const SQLCHAR * szSqlStr,
SQLINTEGER cbSqlStr,
UWORD flag)
{
StatementClass *stmt = (StatementClass *) hstmt;
RETCODE result;
CSTR func = "PGAPI_ExecDirect";
const ConnectionClass *conn = SC_get_conn(stmt);
MYLOG(0, "entering...%x\n", flag);
if (result = SC_initialize_and_recycle(stmt), SQL_SUCCESS != result)
return result;
/*
* keep a copy of the un-parametrized statement, in case they try to
* execute this statement again
*/
stmt->statement = make_string(szSqlStr, cbSqlStr, NULL, 0);
MYLOG(DETAIL_LOG_LEVEL, "a2\n");
if (!stmt->statement)
{
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "No memory available to store statement", func);
return SQL_ERROR;
}
MYLOG(0, "**** hstmt=%p, statement='%s'\n", hstmt, stmt->statement);
if (0 != (flag & PODBC_WITH_HOLD))
SC_set_with_hold(stmt);
if (0 != (flag & PODBC_RDONLY))
SC_set_readonly(stmt);
/*
* If an SQLPrepare was performed prior to this, but was left in the
* described state because an error occurred prior to SQLExecute then
* set the statement to finished so it can be recycled.
*/
if (stmt->status == STMT_DESCRIBED)
stmt->status = STMT_FINISHED;
stmt->statement_type = statement_type(stmt->statement);
/* Check if connection is onlyread (only selects are allowed) */
if (CC_is_onlyread(conn) && STMT_UPDATE(stmt))
{
SC_set_error(stmt, STMT_EXEC_ERROR, "Connection is readonly, only select statements are allowed.", func);
return SQL_ERROR;
}
MYLOG(0, "calling PGAPI_Execute...\n");
result = PGAPI_Execute(hstmt, flag);
MYLOG(0, "leaving %hd\n", result);
return result;
}
static int
inquireHowToPrepare(const StatementClass *stmt)
{
ConnectionClass *conn;
ConnInfo *ci;
int ret = 0;
conn = SC_get_conn(stmt);
ci = &(conn->connInfo);
if (!stmt->use_server_side_prepare)
{
/* Do prepare operations by the driver itself */
return PREPARE_BY_THE_DRIVER;
}
if (NOT_YET_PREPARED == stmt->prepared)
{
SQLSMALLINT num_params;
if (STMT_TYPE_DECLARE == stmt->statement_type &&
PG_VERSION_LT(conn, 8.0))
{
return PREPARE_BY_THE_DRIVER;
}
if (stmt->multi_statement < 0)
PGAPI_NumParams((StatementClass *) stmt, &num_params);
if (stmt->multi_statement > 0)
{
/*
* divide the query into multiple commands and apply V3 parse
* requests for each of them
*/
ret = PARSE_REQ_FOR_INFO;
}
else
{
if (SC_may_use_cursor(stmt))
{
if (ci->drivers.use_declarefetch)
return PARSE_REQ_FOR_INFO;
else if (SQL_CURSOR_FORWARD_ONLY != stmt->options.cursor_type)
ret = PARSE_REQ_FOR_INFO;
else
ret = PARSE_TO_EXEC_ONCE;
}
else
ret = PARSE_TO_EXEC_ONCE;
}
}
if (SC_is_prepare_statement(stmt) && (PARSE_TO_EXEC_ONCE == ret))
ret = NAMED_PARSE_REQUEST;
return ret;
}
int
decideHowToPrepare(StatementClass *stmt, BOOL force)
{
int method = SC_get_prepare_method(stmt);
if (0 != method) /* a method was already determined */
return method;
switch (stmt->prepare)
{
case NON_PREPARE_STATEMENT: /* not a prepare statement */
if (!force)
return method;
break;
}
method = inquireHowToPrepare(stmt);
stmt->prepare |= method;
if (PREPARE_BY_THE_DRIVER == method)
stmt->discard_output_params = 1;
return method;
}
/* dont/should/can send Parse request ? */
enum {
doNothing = 0
,allowParse
,preferParse
,shouldParse
,usingCommand
};
#define ONESHOT_CALL_PARSE allowParse
#define NOPARAM_ONESHOT_CALL_PARSE doNothing
static
int HowToPrepareBeforeExec(StatementClass *stmt, BOOL checkOnly)
{
SQLSMALLINT num_params = stmt->num_params;
ConnectionClass *conn = SC_get_conn(stmt);
ConnInfo *ci = &(conn->connInfo);
int nCallParse = doNothing, how_to_prepare = 0;
BOOL bNeedsTrans = FALSE;
if (num_params < 0)
PGAPI_NumParams(stmt, &num_params);
how_to_prepare = decideHowToPrepare(stmt, checkOnly);
if (checkOnly)
{
if (num_params <= 0)
return doNothing;
}
else
{
switch (how_to_prepare)
{
case NAMED_PARSE_REQUEST:
return shouldParse;
case PARSE_TO_EXEC_ONCE:
switch (stmt->prepared)
{
case PREPARED_TEMPORARILY:
nCallParse = preferParse;
break;
default:
if (num_params <= 0)
nCallParse = NOPARAM_ONESHOT_CALL_PARSE;
else
nCallParse = ONESHOT_CALL_PARSE;
}
break;
default:
return doNothing;
}
}
if (num_params > 0)
{
int param_number = -1;
ParameterInfoClass *apara;
ParameterImplClass *ipara;
OID pgtype;
while (TRUE)
{
SC_param_next(stmt, ¶m_number, &apara, &ipara);
if (!ipara || !apara)
break;
pgtype = PIC_get_pgtype(*ipara);
if (checkOnly)
{
switch (ipara->SQLType)
{
case SQL_LONGVARBINARY:
if (0 == pgtype)
{
if (ci->bytea_as_longvarbinary &&
0 != conn->lobj_type)
nCallParse = shouldParse;
}
break;
case SQL_CHAR:
if (ci->cvt_null_date_string)
nCallParse = shouldParse;
break;
case SQL_VARCHAR:
if (ci->drivers.bools_as_char &&
PG_WIDTH_OF_BOOLS_AS_CHAR == ipara->column_size)
nCallParse = shouldParse;
break;
}
}
else
{
BOOL bBytea = FALSE;
switch (ipara->SQLType)
{
case SQL_LONGVARBINARY:
if (conn->lobj_type == pgtype || PG_TYPE_OID == pgtype)
bNeedsTrans = TRUE;
else if (PG_TYPE_BYTEA == pgtype)
bBytea = TRUE;
else if (0 == pgtype)
{
if (ci->bytea_as_longvarbinary)
bBytea = TRUE;
else
bNeedsTrans = TRUE;
}
if (bBytea)
if (nCallParse < preferParse)
nCallParse = preferParse;
break;
}
}
}
}
if (bNeedsTrans &&
PARSE_TO_EXEC_ONCE == how_to_prepare)
{
if (!CC_is_in_trans(conn) && CC_does_autocommit(conn))
nCallParse = doNothing;
}
return nCallParse;
}
static
const char *GetSvpName(const ConnectionClass *conn, char *wrk, int wrksize)
{
snprintf(wrk, wrksize, "_EXEC_SVP_%p", conn);
return wrk;
}
/*
* The execution after all parameters were resolved.
*/
#define INVALID_EXPBUFFER PQExpBufferDataBroken(stmt->stmt_deffered)
#define VALID_EXPBUFFER (!PQExpBufferDataBroken(stmt->stmt_deffered))
static
void param_status_batch_update(IPDFields *ipdopts, RETCODE retval, SQLLEN target_row, int count_of_deffered)
{
int i, j;
if (NULL != ipdopts->param_status_ptr)
{
for (i = target_row, j = 0; i >= 0 && j <= count_of_deffered; i--)
{
if (SQL_PARAM_UNUSED != ipdopts->param_status_ptr[i])
{
ipdopts->param_status_ptr[i] = retval;
j++;
}
}
}
}
static
RETCODE Exec_with_parameters_resolved(StatementClass *stmt, EXEC_TYPE exec_type, BOOL *exec_end)
{
CSTR func = "Exec_with_parameters_resolved";
RETCODE retval;
SQLLEN start_row, end_row;
SQLINTEGER cursor_type, scroll_concurrency;
ConnectionClass *conn;
QResultClass *res;
APDFields *apdopts;
IPDFields *ipdopts;
BOOL prepare_before_exec = FALSE;
char *stmt_with_params;
SQLLEN status_row = stmt->exec_current_row;
int count_of_deffered;
*exec_end = FALSE;
conn = SC_get_conn(stmt);
MYLOG(0, "copying statement params: trans_status=%d, len=" FORMAT_SIZE_T ", stmt='%s'\n", conn->transact_status, strlen(stmt->statement), stmt->statement);
#define return DONT_CALL_RETURN_FROM_HERE???
#define RETURN(code) { retval = code; goto cleanup; }
ENTER_CONN_CS(conn);
/* save the cursor's info before the execution */
cursor_type = stmt->options.cursor_type;
scroll_concurrency = stmt->options.scroll_concurrency;
/* Prepare the statement if possible at backend side */
if (HowToPrepareBeforeExec(stmt, FALSE) >= allowParse)
prepare_before_exec = TRUE;
MYLOG(DETAIL_LOG_LEVEL, "prepare_before_exec=%d srv=%d\n", prepare_before_exec, stmt->use_server_side_prepare);
/* Create the statement with parameters substituted. */
stmt_with_params = stmt->stmt_with_params;
if (LAST_EXEC == exec_type)
{
if (NULL != stmt_with_params)
{
free(stmt_with_params);
stmt_with_params = stmt->stmt_with_params = NULL;
}
if (INVALID_EXPBUFFER ||
!stmt->stmt_deffered.data[0])
RETURN(SQL_SUCCESS);
}
else
{
retval = copy_statement_with_parameters(stmt, prepare_before_exec);
stmt->current_exec_param = -1;
if (retval != SQL_SUCCESS)
{
stmt->exec_current_row = -1;
*exec_end = TRUE;
RETURN(retval) /* error msg is passed from the above */
}
stmt_with_params = stmt->stmt_with_params;
if (!stmt_with_params) // Extended Protocol
exec_type = DIRECT_EXEC;
}
MYLOG(0, " stmt_with_params = '%s'\n", stmt->stmt_with_params);
/*
* The real execution.
*/
MYLOG(0, "about to begin SC_execute exec_type=%d\n", exec_type);
ipdopts = SC_get_IPDF(stmt);
apdopts = SC_get_APDF(stmt);
if (start_row = stmt->exec_start_row, start_row < 0)
start_row = 0;
if (end_row = stmt->exec_end_row, end_row < 0)
{
end_row = (SQLINTEGER) apdopts->paramset_size - 1;
if (end_row < 0)
end_row = 0;
}
if (LAST_EXEC == exec_type &&
NULL != ipdopts->param_status_ptr)
{
int i;
for (i = end_row; i >= start_row; i--)
{
if (SQL_PARAM_UNUSED != ipdopts->param_status_ptr[i])
{
status_row = i;
break;
}
}
}
count_of_deffered = stmt->count_of_deffered;
if (DIRECT_EXEC == exec_type)
{
retval = SC_execute(stmt);
stmt->count_of_deffered = 0;
}
else if (DEFFERED_EXEC == exec_type &&
stmt->exec_current_row < end_row &&
stmt->count_of_deffered + 1 < stmt->batch_size)
{
if (INVALID_EXPBUFFER)
initPQExpBuffer(&stmt->stmt_deffered);
if (INVALID_EXPBUFFER)
{
retval = SQL_ERROR;
SC_set_error(stmt, STMT_NO_MEMORY_ERROR, "Out of memory", __FUNCTION__);
}
else
{
if (NULL != stmt_with_params)
{
if (stmt->stmt_deffered.data[0])
appendPQExpBuffer(&stmt->stmt_deffered, ";%s", stmt_with_params);
else
printfPQExpBuffer(&stmt->stmt_deffered, "%s", stmt_with_params);
}
if (NULL != ipdopts->param_status_ptr)
ipdopts->param_status_ptr[stmt->exec_current_row] = SQL_PARAM_SUCCESS; // set without exec
stmt->count_of_deffered++;
stmt->exec_current_row++;
RETURN(SQL_SUCCESS);
}
}
else
{
if (VALID_EXPBUFFER)
{
if (NULL != stmt_with_params)
appendPQExpBuffer(&stmt->stmt_deffered, ";%s", stmt_with_params);
stmt->stmt_with_params = stmt->stmt_deffered.data;
}
retval = SC_execute(stmt);
stmt->stmt_with_params = stmt_with_params;
stmt->count_of_deffered = 0;
if (VALID_EXPBUFFER)
resetPQExpBuffer(&stmt->stmt_deffered);
}
if (retval == SQL_ERROR)
{
MYLOG(0, "count_of_deffered=%d\n", count_of_deffered);
param_status_batch_update(ipdopts, SQL_PARAM_ERROR, stmt->exec_current_row, count_of_deffered);
stmt->exec_current_row = -1;
*exec_end = TRUE;
RETURN(retval)
}
res = SC_get_Result(stmt);
/* special handling of result for keyset driven cursors */
if (SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type &&
SQL_CONCUR_READ_ONLY != stmt->options.scroll_concurrency)
{
QResultClass *kres;
if (kres = QR_nextr(res), kres)
{
QR_set_fields(kres, QR_get_fields(res));
QR_set_fields(res, NULL);
kres->num_fields = res->num_fields;
QR_detach(res);
SC_set_Result(stmt, kres);
res = kres;
}
}
ipdopts = SC_get_IPDF(stmt);
if (ipdopts->param_status_ptr)
{
switch (retval)
{
case SQL_SUCCESS:
ipdopts->param_status_ptr[status_row] = SQL_PARAM_SUCCESS;
break;
case SQL_SUCCESS_WITH_INFO:
MYLOG(0, "count_of_deffered=%d has_notice=%d\n", count_of_deffered, stmt->has_notice);
param_status_batch_update(ipdopts, (count_of_deffered > 0 && !stmt->has_notice) ? SQL_PARAM_SUCCESS : SQL_PARAM_SUCCESS_WITH_INFO, status_row, count_of_deffered);
break;
default:
param_status_batch_update(ipdopts, SQL_PARAM_ERROR, status_row, count_of_deffered);
break;
}
}
stmt->has_notice = 0;
if (stmt->exec_current_row >= end_row)
{
*exec_end = TRUE;
stmt->exec_current_row = -1;
}
else
stmt->exec_current_row++;
if (res)
{
EnvironmentClass *env = (EnvironmentClass *) CC_get_env(conn);
const char *cmd = QR_get_command(res);
if (retval == SQL_SUCCESS &&
NULL != cmd &&
start_row >= end_row &&
NULL != env &&
EN_is_odbc3(env))
{
int count;
if (sscanf(cmd , "UPDATE %d", &count) == 1)
;
else if (sscanf(cmd , "DELETE %d", &count) == 1)
;
else
count = -1;
if (0 == count)
retval = SQL_NO_DATA;
}
stmt->diag_row_count = res->recent_processed_row_count;
}
/*
* The cursor's info was changed ?
*/
if (retval == SQL_SUCCESS &&
(stmt->options.cursor_type != cursor_type ||
stmt->options.scroll_concurrency != scroll_concurrency))
{
SC_set_error(stmt, STMT_OPTION_VALUE_CHANGED, "cursor updatability changed", func);
retval = SQL_SUCCESS_WITH_INFO;
}
cleanup:
#undef RETURN
#undef return
LEAVE_CONN_CS(conn);
return retval;
}
int
StartRollbackState(StatementClass *stmt)
{
int ret;
ConnectionClass *conn;
ConnInfo *ci = NULL;
MYLOG(DETAIL_LOG_LEVEL, "entering %p->external=%d\n", stmt, stmt->external);
conn = SC_get_conn(stmt);
if (conn)
ci = &conn->connInfo;
if (!ci || ci->rollback_on_error < 0) /* default */
{
if (conn && PG_VERSION_GE(conn, 8.0))
ret = 2; /* statement rollback */
else
ret = 1; /* transaction rollback */
}
else
{
ret = ci->rollback_on_error;
if (2 == ret && PG_VERSION_LT(conn, 8.0))
ret = 1;
}
switch (ret)
{
case 1:
SC_start_tc_stmt(stmt);
break;
case 2:
SC_start_rb_stmt(stmt);
break;
}
return ret;
}
int
GenerateSvpCommand(ConnectionClass *conn, int type, char *cmd, int buflen)
{
char esavepoint[50];
int rtn = -1;
cmd[0] = '\0';
switch (type)
{
case INTERNAL_SAVEPOINT_OPERATION: /* savepoint */
#ifdef _RELEASE_INTERNAL_SAVEPOINT
if (conn->internal_svp)
rtn = snprintf(cmd, buflen, "RELEASE %s;", GetSvpName(conn, esavepoint, sizeof(esavepoint)));
#endif /* _RELEASE_INTERNAL_SAVEPOINT */
rtn = snprintfcat(cmd, buflen, "SAVEPOINT %s", GetSvpName(conn, esavepoint, sizeof(esavepoint)));
break;
case INTERNAL_ROLLBACK_OPERATION: /* rollback */
if (conn->internal_svp)
rtn = snprintf(cmd, buflen, "ROLLBACK TO %s", GetSvpName(conn, esavepoint, sizeof(esavepoint)));
else
rtn = snprintf(cmd, buflen, "ROLLBACK");
break;
}
return rtn;
}
/*
* Must be in a transaction or the subsequent execution
* invokes a transaction.
*/
RETCODE
SetStatementSvp(StatementClass *stmt, unsigned int option)
{
CSTR func = "SetStatementSvp";
char cmd[128];
ConnectionClass *conn = SC_get_conn(stmt);
QResultClass *res;
RETCODE ret = SQL_SUCCESS_WITH_INFO;
if (NULL == conn->pqconn)
{
SC_set_error(stmt, STMT_COMMUNICATION_ERROR, "The connection has been lost", __FUNCTION__);
return SQL_ERROR;
}
if (CC_is_in_error_trans(conn))
return ret;
if (!stmt->lock_CC_for_rb)
{
ENTER_CONN_CS(conn);
stmt->lock_CC_for_rb = TRUE;
}
MYLOG(DETAIL_LOG_LEVEL, " %p->accessed=%d opt=%u in_progress=%u prev=%u\n", conn, CC_accessed_db(conn), option, conn->opt_in_progress, conn->opt_previous);
conn->opt_in_progress &= option;
switch (stmt->statement_type)
{
case STMT_TYPE_SPECIAL:
case STMT_TYPE_TRANSACTION:
return ret;
}
/* If rbpoint is not yet started and the previous statement was not read-only */
if (!CC_started_rbpoint(conn) && 0 == (conn->opt_previous & SVPOPT_RDONLY))
{
BOOL need_savep = FALSE;
if (SC_is_rb_stmt(stmt))
{
if (CC_is_in_trans(conn)) /* needless to issue SAVEPOINT before the 1st command */
{
need_savep = TRUE;
}
}
if (need_savep)
{
if (0 != (option & SVPOPT_REDUCE_ROUNDTRIP))
{
conn->internal_op = PREPEND_IN_PROGRESS;
CC_set_accessed_db(conn);
return ret;
}
GenerateSvpCommand(conn, INTERNAL_SAVEPOINT_OPERATION, cmd, sizeof(cmd));
conn->internal_op = SAVEPOINT_IN_PROGRESS;
res = CC_send_query(conn, cmd, NULL, 0, NULL);
conn->internal_op = 0;
if (QR_command_maybe_successful(res))
ret = SQL_SUCCESS;
else
{
SC_set_error(stmt, STMT_INTERNAL_ERROR, "internal SAVEPOINT failed", func);
ret = SQL_ERROR;
}
QR_Destructor(res);
}
}
CC_set_accessed_db(conn);
MYLOG(DETAIL_LOG_LEVEL, "leaving %p->accessed=%d\n", conn, CC_accessed_db(conn));
return ret;
}
RETCODE
DiscardStatementSvp(StatementClass *stmt, RETCODE ret, BOOL errorOnly)
{
CSTR func = "DiscardStatementSvp";
ConnectionClass *conn = SC_get_conn(stmt);
BOOL start_stmt = FALSE;
MYLOG(DETAIL_LOG_LEVEL, "entering %p->accessed=%d is_in=%d is_rb=%d is_tc=%d\n", conn, CC_accessed_db(conn),
CC_is_in_trans(conn), SC_is_rb_stmt(stmt), SC_is_tc_stmt(stmt));
if (stmt->lock_CC_for_rb)
MYLOG(0, "in_progress=%u previous=%d\n", conn->opt_in_progress, conn->opt_previous);
switch (ret)
{
case SQL_NEED_DATA:
break;
case SQL_ERROR:
start_stmt = TRUE;
break;
default:
if (!errorOnly)
start_stmt = TRUE;
break;
}
if (!CC_accessed_db(conn) || !CC_is_in_trans(conn))
goto cleanup;
if (!SC_is_rb_stmt(stmt) && !SC_is_tc_stmt(stmt))
goto cleanup;
if (SQL_ERROR == ret)
{
if (CC_started_rbpoint(conn) && conn->internal_svp)
{
int cmd_success = CC_internal_rollback(conn, PER_STATEMENT_ROLLBACK, FALSE);
if (!cmd_success)
{
SC_set_error(stmt, STMT_INTERNAL_ERROR, "internal ROLLBACK failed", func);
goto cleanup;
}
}
else
{
CC_abort(conn);
goto cleanup;
}
}
else if (errorOnly)
return ret;
MYLOG(DETAIL_LOG_LEVEL, "\tret=%d\n", ret);
cleanup:
#ifdef NOT_USED
if (!SC_is_prepare_statement(stmt) && ONCE_DESCRIBED == stmt->prepared)
SC_set_prepared(stmt, NOT_YET_PREPARED);
#endif
if (start_stmt || SQL_ERROR == ret)
{
stmt->execinfo = 0;
if (SQL_ERROR != ret && CC_accessed_db(conn))
{
conn->opt_previous = conn->opt_in_progress;
CC_init_opt_in_progress(conn);
}
if (stmt->lock_CC_for_rb)
{
stmt->lock_CC_for_rb = FALSE;
LEAVE_CONN_CS(conn);
MYLOG(DETAIL_LOG_LEVEL, " release conn_lock\n");
}
CC_start_stmt(conn);
}
MYLOG(DETAIL_LOG_LEVEL, "leaving %d\n", ret);
return ret;
}
/*
* Given a SQL statement, see if it is an INSERT INTO statement and extract
* the name of the table (with schema) of the table that was inserted to.
* (It is needed to resolve any @@identity references in the future.)
*/
void
SC_setInsertedTable(StatementClass *stmt, RETCODE retval)
{
const char *cmd = stmt->statement;
ConnectionClass *conn;
size_t len;
if (STMT_TYPE_INSERT != stmt->statement_type)
return;
if (!SQL_SUCCEEDED(retval))
return;
conn = SC_get_conn(stmt);
#ifdef NOT_USED /* give up the use of lastval() */
if (PG_VERSION_GE(conn, 8.1)) /* lastval() is available */
return;
#endif /* NOT_USED */
/*if (!CC_fake_mss(conn))
return;*/
/*
* Parse a statement that was just executed. If it looks like an INSERT INTO
* statement, try to extract the table name (and schema) of the table that
* we inserted into.
*
* This is by no means fool-proof, we don't implement the whole backend
* lexer and grammar here, but should handle most simple INSERT statements.
*/
while (isspace((UCHAR) *cmd)) cmd++;
if (!*cmd)
return;
len = 6;
if (strnicmp(cmd, "insert", len))
return;
cmd += len;
while (isspace((UCHAR) *(++cmd)));
if (!*cmd)
return;
len = 4;
if (strnicmp(cmd, "into", len))
return;
cmd += len;
while (isspace((UCHAR) *cmd)) cmd++;
if (!*cmd)
return;
NULL_THE_NAME(conn->schemaIns);
NULL_THE_NAME(conn->tableIns);
eatTableIdentifiers((const UCHAR *) cmd, conn->ccsc, &conn->tableIns, &conn->schemaIns);
if (!NAME_IS_VALID(conn->tableIns))
NULL_THE_NAME(conn->schemaIns);
}
/* Execute a prepared SQL statement */
RETCODE SQL_API
PGAPI_Execute(HSTMT hstmt, UWORD flag)
{
CSTR func = "PGAPI_Execute";
StatementClass *stmt = (StatementClass *) hstmt;
RETCODE retval = SQL_SUCCESS;
ConnectionClass *conn;
APDFields *apdopts;
IPDFields *ipdopts;
SQLLEN i, start_row, end_row;
BOOL exec_end = FALSE, recycled = FALSE, recycle = TRUE;
SQLSMALLINT num_params;
MYLOG(0, "entering...%x %p status=%d\n", flag, stmt, stmt->status);
stmt->has_notice = 0;
conn = SC_get_conn(stmt);
apdopts = SC_get_APDF(stmt);
/*
* If the statement was previously described, just recycle the old result
* set that contained just the column information.
*/
if ((stmt->prepare && stmt->status == STMT_DESCRIBED) ||
(stmt->status == STMT_FINISHED && 0 != (flag & PODBC_RECYCLE_STATEMENT)))
{
stmt->exec_current_row = -1;
SC_recycle_statement(stmt);
}
MYLOG(0, "clear errors...\n");
SC_clear_error(stmt);
if (!stmt->statement)
{
SC_set_error(stmt, STMT_NO_STMTSTRING, "This handle does not have a SQL statement stored in it", func);
MYLOG(0, "problem with handle\n");
return SQL_ERROR;
}
#define return DONT_CALL_RETURN_FROM_HERE???
if (stmt->exec_current_row > 0)
{
/*
* executing an array of parameters.
* Don't recycle the statement.
*/
recycle = FALSE;
}
else if (PREPARED_PERMANENTLY == stmt->prepared ||
PREPARED_TEMPORARILY == stmt->prepared)
{
/*
* re-executing an prepared statement.
* Don't recycle the statement but
* discard the old result.
*/
recycle = FALSE;
SC_reset_result_for_rerun(stmt);
}
/*
* If SQLExecute is being called again, recycle the statement. Note
* this should have been done by the application in a call to
* SQLFreeStmt(SQL_CLOSE) or SQLCancel.
*/
else if (stmt->status == STMT_FINISHED)
{
MYLOG(0, "recycling statement (should have been done by app)...\n");
/******** Is this really NEEDED ? ******/
SC_recycle_statement(stmt);
recycled = TRUE;
}
/* Check if the statement is in the correct state */
else if ((stmt->prepare && stmt->status != STMT_READY) ||
(stmt->status != STMT_ALLOCATED && stmt->status != STMT_READY))
{