-
Notifications
You must be signed in to change notification settings - Fork 5
/
ifx_connection.ec
2730 lines (2376 loc) · 65.4 KB
/
ifx_connection.ec
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
/*-------------------------------------------------------------------------
*
* ifx_fdw.c
* foreign-data wrapper for IBM INFORMIX databases
*
* NOTES:
*
* One word about datum conversion routines: The various getter
* and setter functions vary in their usage of the specified
* Informix attribute number: getter functions expect the attribute
* number to match it's offset into the table, whereas setter functions
* need to know the attribute number into the sqlvar struct to bind
* any new values (thus, the order of the parametrized columns in the
* DML query).
*
* For all setter functions it is also absolutely mandatory to set
* valid options into the specific stmt_info->ifxAttrDefs array item,
* otherwise the functions won't be able to set correct values into
* the specific sqlvar struct item. For example, IfxAttrDef.indicator
* need to be set to a valid value to reflect wether the column will get
* a NULL or NOT NULL datum.
*
* Copyright (c) 2012, credativ GmbH
*
* IDENTIFICATION
* informix_fdw/ifx_connection.ec
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "ifx_type_compat.h"
EXEC SQL include sqltypes;
EXEC SQL include sqlda;
EXEC SQL include "int8.h";
EXEC SQL include "decimal.h";
EXEC SQL include "varchar.h";
/* Don't trap errors. Default, but force it explicitely */
EXEC SQL WHENEVER SQLERROR CONTINUE;
/*
* Number of current transactions
* in progress per backend.
*/
unsigned int ifxXactInProgress = 0;
static void ifxSetEnv(IfxConnectionInfo *coninfo);
static inline IfxIndicatorValue ifxSetIndicator(IfxAttrDef *def,
struct sqlvar_struct *ifx_value);
static void ifxReleaseSavepoint(int level);
static void ifxRollbackSavepoint(int level);
static void ifxSavepoint(IfxPGCachedConnection *cached,
IfxConnectionInfo *coninfo);
/*
* Establish a named INFORMIX database connection with transactions
*
* This also initializes various database properties of the
* coninfo structure, e.g. wether the database supports transactions.
*/
void ifxCreateConnectionXact(IfxConnectionInfo *coninfo)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifxdsn;
char *ifxconname;
char *ifxuser;
char *ifxpass;
EXEC SQL END DECLARE SECTION;
ifxdsn = coninfo->dsn;
ifxconname = coninfo->conname;
ifxuser = coninfo->username;
ifxpass = coninfo->password;
/*
* Set specific Informix environment
*/
ifxSetEnv(coninfo);
EXEC SQL CONNECT TO :ifxdsn AS :ifxconname
USER :ifxuser USING :ifxpass WITH CONCURRENT TRANSACTION;
if (ifxGetSQLCAWarn(SQLCA_WARN_SET) == 'W') {
if (ifxGetSQLCAWarn(SQLCA_WARN_TRANSACTIONS) == 'W')
{
/* save state into connection info */
coninfo->tx_enabled = 1;
}
if (ifxGetSQLCAWarn(SQLCA_WARN_ANSI) == 'W') {
/* ANSI compliant database */
coninfo->db_ansi = 1;
}
if (ifxGetSQLCAWarn(SQLCA_WARN_NO_IFX_SE) == 'W') {
/* connected to an non-SE instance */
coninfo->is_obsolete = 0;
} else {
/* SQLCA_WARN_NO_IFX_SE is not set, assume SE instance */
coninfo->is_obsolete = 1;
}
} else {
/*
* If no warning was set, we implicitely assume an Informix SE
* instance
*/
coninfo->is_obsolete = 1;
}
}
int ifxStartTransaction(IfxPGCachedConnection *cached, IfxConnectionInfo *coninfo)
{
/*
* No-op if non-logged database or parent transaction
* already in progress...
*/
if (coninfo->tx_enabled == 1)
{
/*
* If we already have started a transaction, we use a
* SAVEPOINT instead.
*/
if (cached->tx_in_progress < 1)
{
EXEC SQL BEGIN WORK;
EXEC SQL SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
if (ifxGetSqlStateClass() != IFX_ERROR)
{
cached->tx_in_progress = 1;
++ifxXactInProgress;
}
else
return -1;
}
/*
* If the nested level didn't change, this is a no-op. No further
* nested transaction started so far.
*/
if (cached->tx_in_progress == coninfo->xact_level)
return 0;
/*
* Okay, looks like this connection already has established
* a parent transaction and a new nested subtransaction.
* Since the xact level increased in the meantime, we need
* to create a corresponding SAVEPOINT for the
* current nest level on the remote server as well.
*/
while(cached->tx_in_progress < coninfo->xact_level)
{
ifxSavepoint(cached, coninfo);
if (ifxGetSqlStateClass() != IFX_ERROR)
{
cached->tx_in_progress = coninfo->xact_level;
++ifxXactInProgress;
}
else
{
/*
* Indicate something went wrong, leave it up
* to the caller to inspect SQLSTATE
*/
return -1;
}
}
}
/* no-op treated as success! */
return 0;
}
/*
* ifxReleaseSavepoint()
*
* Release the given savepoint identified by level.
*
* NOTE: This function isn't intended to be called directly, the caller
* needs to make sure the transaction state is actually
* adjusted in the cached connection handle. See ifxCommitTransaction()
* or ifxRollbackTransaction() for details.
*/
static void ifxReleaseSavepoint(int level)
{
EXEC SQL BEGIN DECLARE SECTION;
char ifx_sql[256];
EXEC SQL END DECLARE SECTION;
memset(ifx_sql, '\0', sizeof(ifx_sql));
snprintf(ifx_sql, sizeof(ifx_sql), "RELEASE SAVEPOINT ifxfdw_svpt%d",
level);
EXEC SQL EXECUTE IMMEDIATE :ifx_sql;
}
/*
* ifxRollbackSavepoint()
*
* Rollback the specified savepoint identified by level.
*
* NOTE: This function isn't intended to be called directly, the caller
* needs to make sure the transaction state is actually
* adjusted in the cached connection handle. See ifxCommitTransaction()
* or ifxRollbackTransaction() for details.
*/
static void ifxRollbackSavepoint(int level)
{
EXEC SQL BEGIN DECLARE SECTION;
char ifx_sql[256];
EXEC SQL END DECLARE SECTION;
memset(ifx_sql, '\0', sizeof(ifx_sql));
snprintf(ifx_sql, sizeof(ifx_sql), "ROLLBACK TO SAVEPOINT ifxfdw_svpt%d",
level);
EXEC SQL EXECUTE IMMEDIATE :ifx_sql;
}
/*
* ifxSavepoint()
*
* Creates a savepoint within the current nested xact level.
*
* NOTE: This function isn't intended to be called directly, the caller
* needs to make sure the transaction state is actually
* adjusted in the cached connection handle. See ifxCommitTransaction()
* or ifxRollbackTransaction() for details.
*/
static void ifxSavepoint(IfxPGCachedConnection *cached, IfxConnectionInfo *coninfo)
{
EXEC SQL BEGIN DECLARE SECTION;
char ifx_sql[256];
EXEC SQL END DECLARE SECTION;
/*
* Generate the SAVEPOINT SQL command sent to the Informix server.
* NOTE: Since we rely on the caller, we don't increment the
* nest level here directly and leave this duty to the caller.
*/
memset(ifx_sql, '\0', sizeof(ifx_sql));
snprintf(ifx_sql, sizeof(ifx_sql), "SAVEPOINT ifxfdw_svpt%d",
cached->tx_in_progress + 1);
EXEC SQL EXECUTE IMMEDIATE :ifx_sql;
}
/*
* Rollback a parent transaction or SAVEPOINT.
*
* If subXactLevel is specified and the current cached connection handle
* is within a nested xact level, the transaction is rolled back to the
* SAVEPOINT identified by subXactLevel.
*
* Specify 0 within subXactLevel to ROLLBACK the whole parent transaction.
*/
int ifxRollbackTransaction(IfxPGCachedConnection *cached, int subXactLevel)
{
/*
* No-op, if no transaction in progress.
* We don't treat this as an error currently, since we
* might talk with a remote Informix database with no logging.
*/
if (cached->tx_in_progress <= 0)
return 0;
/*
* Rollback transaction.
*/
if ((cached->tx_in_progress == 1)
|| (cached->tx_in_progress > 1 && subXactLevel == 0))
{
EXEC SQL ROLLBACK WORK;
if (ifxGetSqlStateClass() != IFX_ERROR)
{
--cached->tx_in_progress;
--ifxXactInProgress;
++cached->tx_num_rollback;
}
else
{
/*
* Indicate something went wrong, leave it up
* to the caller to inspect SQLSTATE
*/
return -1;
}
}
else
{
/*
* If tx_in_progress is larger than 1, then we know that
* we are currently nested within several subtransactions
* locally. Rollback to the savepoint specified by the
* subXactLevel parameter...
*/
ifxRollbackSavepoint(subXactLevel);
if (ifxGetSqlStateClass() == IFX_ERROR)
return -1;
/*
* ...and finally release the savepoint.
*/
ifxReleaseSavepoint(subXactLevel);
if (ifxGetSqlStateClass() != IFX_ERROR)
{
/* decrease the nest level */
--cached->tx_in_progress;
--ifxXactInProgress;
/*
* NOTE: We count parent transactions in the stats
* only!
*/
}
else
{
/*
* Indicate something went wrong, leave it up
* to the caller to inspect SQLSTATE.
*/
return -1;
}
}
/* only reached in case of success */
return 0;
}
/*
* Commit a transaction or release a SAVEPOINT identified by
* subXactLevel.
*/
int ifxCommitTransaction(IfxPGCachedConnection *cached, int subXactLevel)
{
if (cached->tx_in_progress == 1)
{
EXEC SQL COMMIT WORK;
if (ifxGetSqlStateClass() != IFX_ERROR)
{
cached->tx_in_progress = 0;
--ifxXactInProgress;
++cached->tx_num_commit;
}
else
{
/*
* Indicate something went wrong, leave it up
* to the caller to inspect SQLSTATE
*/
return -1;
}
}
else if (cached->tx_in_progress > 1)
{
/*
* Commit the current savepoint.
*/
ifxReleaseSavepoint(subXactLevel);
if (ifxGetSqlStateClass() != IFX_ERROR)
{
--cached->tx_in_progress;
--ifxXactInProgress;
/*
* NOTE: We count parent transactions in the stats
* only!
*/
}
}
/* no-op treated as success! */
return 0;
}
void ifxDisconnectConnection(char *conname)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_conname;
EXEC SQL END DECLARE SECTION;
ifx_conname = conname;
EXEC SQL DISCONNECT :ifx_conname;
}
int ifxGetSQLCAErrd(signed short ca)
{
return sqlca.sqlerrd[ca];
}
char ifxGetSQLCAWarn(signed short int warn)
{
switch (warn)
{
case 0:
return SQLCA_WARN(0);
case 1:
return SQLCA_WARN(1);
case 2:
return SQLCA_WARN(2);
case 3:
return SQLCA_WARN(3);
case 4:
return SQLCA_WARN(4);
case 5:
return SQLCA_WARN(5);
case 6:
return SQLCA_WARN(6);
case 7:
return SQLCA_WARN(7);
default:
return -1;
}
}
void ifxSetEnv(IfxConnectionInfo *coninfo)
{
setenv("INFORMIXDIR", coninfo->informixdir, 1);
setenv("INFORMIXSERVER", coninfo->servername, 1);
/*
* Set output format for DATE and DATETIME.
*/
setenv("GL_DATE", coninfo->gl_date, 1);
setenv("GL_DATETIME", coninfo->gl_datetime, 1);
/*
* Set CLIENT_LOCALE or leave it empty in case
* no value is specified.
*/
if (coninfo->client_locale != NULL)
setenv("CLIENT_LOCALE", coninfo->client_locale, 1);
/*
* Set DB_LOCALE or leave it empty in case
* no value is specified.
*/
if (coninfo->db_locale != NULL)
setenv("DB_LOCALE", coninfo->db_locale, 1);
/*
* Set DBMONEY or leave it empty in case
* no value is specified.
*/
if (coninfo->db_monetary != NULL)
setenv("DBMONEY", coninfo->db_monetary, 1);
/*
* Set DELIMIDENT accordingly. This will determine
* wether we use quoted identifier for tables names or not.
*/
if (coninfo->delimident)
setenv("DELIMIDENT", "1", 1);
else
/* will probably set errno to EINVAL in case not set */
unsetenv("DELIMIDENT");
}
/*
* Sets the connection specified by conname.
*/
int ifxSetConnectionIdent(char *conname)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifxconname;
EXEC SQL END DECLARE SECTION;
ifxconname = conname;
EXEC SQL SET CONNECTION :ifxconname;
/*
* In case we can't make this connection current abort
* immediately, but let the caller know that something went
* wrong by returning -1. It's up to him to examine SQLSTATE
* then. Note that we only react on an SQLSTATE representing an
* ERROR, warnings or other SQLSTATE classes are ignored.
*/
if (ifxGetSqlStateClass() == IFX_ERROR)
return -1;
else
return 0;
}
void ifxSetConnection(IfxConnectionInfo *coninfo)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifxconname;
EXEC SQL END DECLARE SECTION;
/*
* Set specific Informix environment
*/
ifxSetEnv(coninfo);
ifxconname = coninfo->conname;
EXEC SQL SET CONNECTION :ifxconname;
if (ifxGetSQLCAWarn(SQLCA_WARN_SET) == 'W') {
if (ifxGetSQLCAWarn(SQLCA_WARN_TRANSACTIONS) == 'W')
{
/* save state into connection info */
coninfo->tx_enabled = 1;
}
if (ifxGetSQLCAWarn(SQLCA_WARN_ANSI) == 'W') {
/* ANSI compliant database */
coninfo->db_ansi = 1;
}
if (ifxGetSQLCAWarn(SQLCA_WARN_NO_IFX_SE) == 'W') {
/* if 'W' was set, an non-SE instance was detected */
coninfo->is_obsolete = 0;
} else {
/* SQLCA_WARN_NO_IFX_SE is not set, assume SE instance */
coninfo->is_obsolete = 1;
}
} else {
/*
* If no warning was set, we implicitely assume an Informix SE
* instance
*/
coninfo->is_obsolete = 1;
}
}
void ifxPrepareQuery(char *query, char *stmt_name)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_query;
char *ifx_stmt_name;
EXEC SQL END DECLARE SECTION;
ifx_query = query;
ifx_stmt_name = stmt_name;
EXEC SQL PREPARE :ifx_stmt_name FROM :ifx_query;
}
void ifxCloseCursor(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_cursor_name;
EXEC SQL END DECLARE SECTION;
ifx_cursor_name = state->cursor_name;
EXEC SQL CLOSE :ifx_cursor_name;
}
int ifxFreeResource(IfxStatementInfo *state,
int stackentry)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_id;
EXEC SQL END DECLARE SECTION;
switch (stackentry)
{
case IFX_STACK_PREPARE:
ifx_id = state->stmt_name;
break;
case IFX_STACK_DECLARE:
ifx_id = state->cursor_name;
break;
default:
/* should not happen */
return -1;
}
EXEC SQL FREE :ifx_id;
return stackentry;
}
void ifxAllocateDescriptor(char *descr_name, int num_items)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_descriptor;
int ifx_max;
EXEC SQL END DECLARE SECTION;
ifx_descriptor = descr_name;
ifx_max = num_items;
EXEC SQL ALLOCATE DESCRIPTOR :ifx_descriptor WITH MAX :ifx_max;
}
void ifxDeallocateDescriptor(char *descr_name)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_descr_name;
EXEC SQL END DECLARE SECTION;
ifx_descr_name = descr_name;
EXEC SQL DEALLOCATE DESCRIPTOR :ifx_descr_name;
}
void ifxDescribeAllocatorByName(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_stmt_name;
EXEC SQL END DECLARE SECTION;
struct sqlda *ifx_sqlda;
ifx_stmt_name = state->stmt_name;
EXEC SQL DESCRIBE :ifx_stmt_name
INTO ifx_sqlda;
state->sqlda = (void *)ifx_sqlda;
}
void ifxDescribeStmtInput(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_stmt_name;
EXEC SQL END DECLARE SECTION;
struct sqlda *ifx_sqlda;
ifx_stmt_name = state->stmt_name;
EXEC SQL DESCRIBE INPUT :ifx_stmt_name INTO ifx_sqlda;
state->sqlda = (void *) ifx_sqlda;
}
void ifxSetDescriptorCount(char *descr_name, int count)
{
EXEC SQL BEGIN DECLARE SECTION;
int ifx_count;
char *ifx_descriptor;
EXEC SQL END DECLARE SECTION;
ifx_count = count;
ifx_descriptor = descr_name;
EXEC SQL SET DESCRIPTOR :ifx_descriptor COUNT = :ifx_count;
}
int ifxDescriptorColumnCount(IfxStatementInfo *state)
{
struct sqlda *sqptr = (struct sqlda *)state->sqlda;
return sqptr->sqld;
}
void ifxOpenCursorForPrepared(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_cursor_name;
EXEC SQL END DECLARE SECTION;
ifx_cursor_name = state->cursor_name;
EXEC SQL OPEN :ifx_cursor_name;
}
/*
* Execute a prepared statement assigned to the
* specified execution state without a given
* SQLDA structure.
*/
void ifxExecuteStmt(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_stmt_name;
EXEC SQL END DECLARE SECTION;
ifx_stmt_name = state->stmt_name;
EXEC SQL EXECUTE :ifx_stmt_name;
}
/*
* Execute a prepared statement assigned to the
* specified execution state with the attached SQLDA
* structure. The caller is responsible to make sure
* the state is a valid execution state with a fully
* initialized SQLDA structure attached to it.
*/
void ifxExecuteStmtSqlda(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_stmt_name;
EXEC SQL END DECLARE SECTION;
struct sqlda *sqptr = (struct sqlda *)state->sqlda;
ifx_stmt_name = state->stmt_name;
EXEC SQL EXECUTE :ifx_stmt_name USING DESCRIPTOR sqptr;
}
void ifxPutValuesInPrepared(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_cursor_name;
EXEC SQL END DECLARE SECTION;
struct sqlda *sqptr = (struct sqlda *)state->sqlda;
ifx_cursor_name = state->cursor_name;
EXEC SQL PUT :ifx_cursor_name USING DESCRIPTOR sqptr;
}
void ifxFlushCursor(IfxStatementInfo *info)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_cursor_name;
EXEC SQL END DECLARE SECTION;
ifx_cursor_name = info->cursor_name;
EXEC SQL FLUSH :ifx_cursor_name;
}
void ifxDeclareCursorForPrepared(char *stmt_name, char *cursor_name,
IfxCursorUsage cursorType)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_stmt_name;
char *ifx_cursor_name;
EXEC SQL END DECLARE SECTION;
/*
* Check if cursor support is really available.
*/
if (cursorType == IFX_NO_CURSOR)
return;
ifx_stmt_name = stmt_name;
ifx_cursor_name = cursor_name;
if (cursorType == IFX_SCROLL_CURSOR)
{
EXEC SQL DECLARE :ifx_cursor_name
SCROLL CURSOR FOR :ifx_stmt_name;
}
else
{
/*
* IFX_UPDATE_CURSOR,
* IFX_DEFAULT_CURSOR,
* IFX_INSERT_CURSOR
*/
EXEC SQL DECLARE :ifx_cursor_name
CURSOR FOR :ifx_stmt_name;
}
}
void ifxDestroyConnection(char *conname)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifxconname;
EXEC SQL END DECLARE SECTION;
ifxconname = conname;
EXEC SQL DISCONNECT :ifxconname;
}
void ifxFetchRowFromCursor(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_cursor_name;
EXEC SQL END DECLARE SECTION;
struct sqlda *ifx_sqlda;
ifx_sqlda = (struct sqlda *)state->sqlda;
ifx_cursor_name = state->cursor_name;
EXEC SQL FETCH NEXT :ifx_cursor_name USING DESCRIPTOR ifx_sqlda;
}
void ifxFetchFirstRowFromCursor(IfxStatementInfo *state)
{
EXEC SQL BEGIN DECLARE SECTION;
char *ifx_cursor_name;
EXEC SQL END DECLARE SECTION;
struct sqlda *ifx_sqlda;
ifx_sqlda = (struct sqlda *)state->sqlda;
ifx_cursor_name = state->cursor_name;
EXEC SQL FETCH FIRST :ifx_cursor_name USING DESCRIPTOR ifx_sqlda;
}
/*
* Returns the class of the current SQLSTATE value.
*/
IfxSqlStateClass ifxGetSqlStateClass(void)
{
IfxSqlStateClass errclass = IFX_RT_ERROR;
if (SQLSTATE[0] == '0')
{
/* We'd like to see here the following exception
* classes:
*
* = 00: IFX_SUCCESS
* = 01: IFX_WARNING
* = 02: IFX_NOT_FOUND
* > 02: IFX_ERROR
*/
switch (SQLSTATE[1])
{
case '0':
errclass = IFX_SUCCESS;
break;
case '1':
errclass = IFX_WARNING;
break;
case '2':
errclass = IFX_NOT_FOUND;
break;
default:
/* error */
errclass = IFX_ERROR;
break;
}
}
else if (SQLSTATE[0] == '4')
{
/*
* Map SQL errors
*
* Currently we pay special attention to
* SQLSTATE 42000 (syntax error) and 4040
* (invalid transaction state).
*/
if ((SQLSTATE[1] == '0')
|| (SQLSTATE[1] == '2'))
{
errclass = IFX_ERROR;
}
}
else if (SQLSTATE[0] == 'S')
{
/*
* S0 SQLSTATE messages carry object errors
* or conflicts, such as tables already existing
* or missing or invalid names.
* Trap them accordingly and map them
* to a specific error code.
*/
if (strncmp(SQLSTATE, "S0002", 5) == 0)
errclass = IFX_ERROR_TABLE_NOT_FOUND;
if (strncmp(SQLSTATE, "S0000", 5) == 0)
errclass = IFX_ERROR_INVALID_NAME;
}
/*
* XXX: Note that a failed GET DIAGNOSTICS command
* will always set SQLSTATE to IX000 or IX001.
* In this case it is reasonable to return
* a IFX_RT_ERROR code, to indicate a generic
* runtime error for now.
*/
return errclass;
}
/*
* Callback error handler.
*
* This function is intended to be called directly
* after an ESQL call to check for an SQLSTATE error
* condition. Once SQLSTATE is
* set to an error code, the function returns the
* SQLSTATE string with the statement info structure.
*/
IfxSqlStateClass ifxSetException(IfxStatementInfo *state)
{
IfxSqlStateClass errclass = IFX_RT_ERROR;
/*
* Save the SQLSTATE
*/
strncpy(state->sqlstate, SQLSTATE, 5);
/*
* Examine in which category the current
* SQLSTATE belongs.
*/
errclass = ifxGetSqlStateClass();
/*
* Save number of possible sub-exceptions, so
* they can be retrieved additionally.
*/
state->exception_count = ifxExceptionCount();
/* and we're done */
return errclass;
}
/*
* ifxGetSqlStateMessage
*
* Returns the specified SQLSTATE id within
* the current informix connection context.
*/
void ifxGetSqlStateMessage(int id, IfxSqlStateMessage *message)
{
EXEC SQL BEGIN DECLARE SECTION;
int ifx_msg_id;
int ifx_msg_len;
char ifx_message[255];
char ifx_class_origin[255];
char ifx_subclass_origin[255];
EXEC SQL END DECLARE SECTION;
ifx_msg_id = id;
memset(ifx_message, '\0', 255);
memset(ifx_class_origin, '\0', 255);
memset(ifx_subclass_origin, '\0', 255);
memset(message->text, '\0', 255);
memset(message->class_origin, '\0', 255);
memset(message->subclass_origin, '\0', 255);
ifx_msg_len = message->len = 0;
/* Save current SQLCODE and SQLSTATE */
message->sqlcode = ifxGetSqlCode();
strncpy(message->sqlstate, SQLSTATE, 6);
/* Obtain error message */
EXEC SQL GET DIAGNOSTICS EXCEPTION :ifx_msg_id
:ifx_message = MESSAGE_TEXT,
:ifx_msg_len = MESSAGE_LENGTH,
:ifx_subclass_origin = SUBCLASS_ORIGIN,
:ifx_class_origin = CLASS_ORIGIN;
/* copy fields to struct and we're done */
message->id = id;
message->len = ifx_msg_len;
strncpy(message->text, ifx_message, ifx_msg_len);
strncpy(message->subclass_origin,
ifx_subclass_origin,
sizeof(message->subclass_origin));
strncpy(message->class_origin,
ifx_class_origin,
sizeof(message->class_origin));
}
/*
* ifxCatchConnectionError
*
* This function can be used to obtain connection
* errors or warnings after a CONNECT or SET CONNECTION.
*/
IfxSqlStateClass ifxConnectionStatus()
{
/*
* Informix might set SQLSTATE to class '01'
* after CONNECT or SET CONNECTION to tell
* some specific connection characteristics.
*
* There's also the '08' explicit connection
* exception class which needs to be handled
* accordingly. This always should be handled
* as an ERROR within pgsql backends.
*
* Handle Informix exception class 'IX' accordingly
* in case someone has installation errors with his
* CSDK (e.g. no INFORMIXDIR set).
*/
if ((strncmp(SQLSTATE, "08", 2) == 0)
|| (strncmp(SQLSTATE, "IX", 2) == 0))
return IFX_CONNECTION_ERROR;
if (strncmp(SQLSTATE, "01", 2) == 0)
return IFX_CONNECTION_WARN;