-
Notifications
You must be signed in to change notification settings - Fork 31
/
dbinfx.ec
1599 lines (1439 loc) · 39.9 KB
/
dbinfx.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
/*********************************************************************
dbinfx.ec: C-level interface to SQL.
This is a layer above esql/c,
since embedded SQL is often difficult to use, especially for new programmers.
Most SQL queries are relatively simple, whence the esql API is overkill.
Why mess with cryptic $directives when you can write:
sql_select("select this, that from table1, table2 where keycolumn = %d",
27, &this, &that);
More important, this API automatically aborts (or longjumps) if an error
occurs, unless that error has been specifically trapped by the program.
This minimizes application-level error-leg programming,
thereby reducing the code by as much as 1/3.
To accomplish this, the errorPrint() function,
supplied by the application, must never return.
We assume it passes the error message
to stderr and to a logfile,
and then exits, or longjumps to a recovery point.
Note that this API works within the context of our own C programming
environment.
Note that dbapi.h does NOT include the Informix header files.
That would violate the spirit of this layer,
which attempts to sheild the application from the details of the SQL API.
If the application needed to see anything in the Informix header files,
we would be doing something wrong.
*********************************************************************/
/* bring in the necessary Informix headers */
$include sqlca;
$include sqltypes;
$include sqlda;
$include locator;
#include "eb.h"
#include "dbapi.h"
#define CACHELIMIT 10000 /* number of cached lines */
#define ENGINE_ERRCODE sqlca.sqlcode
/*********************************************************************
The status variable ENGINE_ERRCODE holds the return code from an Informix call.
This is then used by the function errorTrap() below.
If ENGINE_ERRCODE != 0, errorTrap() aborts the program, or performs
a recovery longjmp, as directed by the generic error function errorPrint().
errorTrap() returns true if an SQL error occurred, but that error
was trapped by the application.
In this case the calling routine should clean up as best it can and return.
*********************************************************************/
static const char *stmt_text = 0; /* text of the SQL statement */
static const short *exclist; /* list of error codes trapped by the application */
static short translevel;
static eb_bool badtrans;
/* Through globals, make error info available to the application. */
int rv_lastStatus, rv_stmtOffset;
long rv_vendorStatus;
char *rv_badToken;
static void debugStatement(void)
{
if(sql_debug && stmt_text)
appendFileNF(sql_debuglog, stmt_text);
} /* debugStatement */
static void debugExtra(const char *s)
{
if(sql_debug)
appendFileNF(sql_debuglog, s);
} /* debugExtra */
/* Append the SQL statement to the debug log. This is not strictly necessary
* if sql_debug is set, since the statement has already been appended. */
static void showStatement(void)
{
if(!sql_debug && stmt_text)
appendFileNF(sql_debuglog, stmt_text);
} /* showStatement */
/* application sets the exception list */
void sql_exclist(const short *list) { exclist = list; }
void sql_exception(int errnum)
{
static short list[2];
list[0] = errnum;
exclist = list;
} /* sql_exception */
/* map Informix errors to our own exception codes, as defined in dbapi.h. */
static const struct ERRORMAP {
short infcode;
short excno;
} errormap[] = {
{200, EXCSYNTAX},
{201, EXCSYNTAX},
{202, EXCSYNTAX},
{203, EXCSYNTAX},
{204, EXCSYNTAX},
{205, EXCROWIDUSE},
{206, EXCNOTABLE},
/* 207 */
{208, EXCRESOURCE},
{209, EXCDBCORRUPT},
{210, EXCFILENAME},
{211, EXCDBCORRUPT},
{212, EXCRESOURCE},
{213, EXCINTERRUPT},
{214, EXCDBCORRUPT},
{215, EXCDBCORRUPT},
{216, EXCDBCORRUPT},
{217, EXCNOCOLUMN},
{218, EXCNOSYNONYM},
{219, EXCCONVERT},
{220, EXCSYNTAX},
{221, EXCRESOURCE},
{222, EXCRESOURCE},
{223, EXCAMBTABLE},
{224, EXCRESOURCE},
{225, EXCRESOURCE},
{226, EXCRESOURCE},
{227, EXCROWIDUSE},
{228, EXCROWIDUSE},
{229, EXCRESOURCE},
{230, EXCDBCORRUPT},
{231, EXCAGGREGATEUSE},
{232, EXCSERIAL},
{233, EXCITEMLOCK},
{234, EXCAMBCOLUMN},
{235, EXCCONVERT},
{236, EXCSYNTAX},
{237, EXCMANAGETRANS},
{238, EXCMANAGETRANS},
{239, EXCDUPKEY},
{240, EXCDBCORRUPT},
{241, EXCMANAGETRANS},
{249, EXCAMBCOLUMN},
{250, EXCDBCORRUPT},
{251, EXCSYNTAX},
{252, EXCITEMLOCK},
{253, EXCSYNTAX},
{255, EXCNOTINTRANS},
{256, EXCMANAGETRANS},
{257, EXCRESOURCE},
{258, EXCDBCORRUPT},
{259, EXCNOCURSOR},
{260, EXCNOCURSOR},
{261, EXCRESOURCE},
{262, EXCNOCURSOR},
{263, EXCRESOURCE},
{264, EXCRESOURCE},
{265, EXCNOTINTRANS},
{266, EXCNOCURSOR},
{267, EXCNOCURSOR},
{268, EXCDUPKEY},
{269, EXCNOTNULLCOLUMN},
{270, EXCDBCORRUPT},
{271, EXCDBCORRUPT},
{272, EXCPERMISSION},
{273, EXCPERMISSION},
{274, EXCPERMISSION},
{275, EXCPERMISSION},
{276, EXCNOCURSOR},
{277, EXCNOCURSOR},
{278, EXCRESOURCE},
{281, EXCTEMPTABLEUSE},
{282, EXCSYNTAX},
{283, EXCSYNTAX},
{284, EXCMANYROW},
{285, EXCNOCURSOR},
{286, EXCNOTNULLCOLUMN},
{287, EXCSERIAL},
{288, EXCITEMLOCK},
{289, EXCITEMLOCK},
{290, EXCNOCURSOR},
{292, EXCNOTNULLCOLUMN},
{293, EXCSYNTAX},
{294, EXCAGGREGATEUSE},
{295, EXCCROSSDB},
{296, EXCNOTABLE},
{297, EXCNOKEY},
{298, EXCPERMISSION},
{299, EXCPERMISSION},
{300, EXCRESOURCE},
{301, EXCRESOURCE},
{302, EXCPERMISSION},
{ 303, EXCAGGREGATEUSE},
{304, EXCAGGREGATEUSE},
{305, EXCSUBSCRIPT},
{306, EXCSUBSCRIPT},
{307, EXCSUBSCRIPT},
{308, EXCCONVERT},
{309, EXCAMBCOLUMN},
{310, EXCDUPTABLE},
{311, EXCDBCORRUPT},
{312, EXCDBCORRUPT},
{313, EXCPERMISSION},
{314, EXCDUPTABLE},
{315, EXCPERMISSION},
{316, EXCDUPINDEX},
{317, EXCUNION},
{318, EXCFILENAME},
{319, EXCNOINDEX},
{320, EXCPERMISSION},
{321, EXCAGGREGATEUSE},
{323, EXCTEMPTABLEUSE},
{324, EXCAMBCOLUMN},
{325, EXCFILENAME},
{326, EXCRESOURCE},
{327, EXCITEMLOCK},
{328, EXCDUPCOLUMN},
{329, EXCNOCONNECT},
{330, EXCRESOURCE},
{331, EXCDBCORRUPT},
{332, EXCTRACE},
{333, EXCTRACE},
{334, EXCTRACE},
{335, EXCTRACE},
{336, EXCTEMPTABLEUSE},
{337, EXCTEMPTABLEUSE},
{338, EXCTRACE},
{339, EXCFILENAME},
{340, EXCTRACE},
{341, EXCTRACE},
{342, EXCREMOTE},
{343, EXCTRACE},
{344, EXCTRACE},
{345, EXCTRACE},
{346, EXCDBCORRUPT},
{347, EXCITEMLOCK},
{348, EXCDBCORRUPT},
{349, EXCNODB},
{350, EXCDUPINDEX},
{352, EXCNOCOLUMN},
{353, EXCNOTABLE},
{354, EXCSYNTAX},
{355, EXCDBCORRUPT},
{356, EXCCONVERT},
{361, EXCRESOURCE},
{362, EXCSERIAL},
{363, EXCNOCURSOR},
{365, EXCNOCURSOR},
{366, EXCCONVERT},
{367, EXCAGGREGATEUSE},
{368, EXCDBCORRUPT},
{369, EXCSERIAL},
{370, EXCAMBCOLUMN},
{371, EXCDUPKEY},
{372, EXCTRACE},
{373, EXCFILENAME},
{374, EXCSYNTAX},
{375, EXCMANAGETRANS},
{376, EXCMANAGETRANS},
{377, EXCMANAGETRANS},
{378, EXCITEMLOCK},
{382, EXCSYNTAX},
{383, EXCAGGREGATEUSE},
{384, EXCVIEWUSE},
{385, EXCCONVERT},
{386, EXCNOTNULLCOLUMN},
{387, EXCPERMISSION},
{388, EXCPERMISSION},
{389, EXCPERMISSION},
{390, EXCDUPSYNONYM},
{391, EXCNOTNULLCOLUMN},
{392, EXCDBCORRUPT},
{393, EXCWHERECLAUSE},
{394, EXCNOTABLE},
{395, EXCWHERECLAUSE},
{396, EXCWHERECLAUSE},
{397, EXCDBCORRUPT},
{398, EXCNOTINTRANS},
{399, EXCMANAGETRANS},
{400, EXCNOCURSOR},
{401, EXCNOCURSOR},
{404, EXCNOCURSOR},
{406, EXCRESOURCE},
{407, EXCDBCORRUPT},
{408, EXCDBCORRUPT},
{409, EXCNOCONNECT},
{410, EXCNOCURSOR},
{413, EXCNOCURSOR},
{414, EXCNOCURSOR},
{415, EXCCONVERT},
{417, EXCNOCURSOR},
{420, EXCREMOTE},
{421, EXCREMOTE},
{422, EXCNOCURSOR},
{423, EXCNOROW},
{424, EXCDUPCURSOR},
{425, EXCITEMLOCK},
{430, EXCCONVERT},
{431, EXCCONVERT},
{432, EXCCONVERT},
{433, EXCCONVERT},
{434, EXCCONVERT},
{439, EXCREMOTE},
{451, EXCRESOURCE},
{452, EXCRESOURCE},
{453, EXCDBCORRUPT},
{454, EXCDBCORRUPT},
{455, EXCRESOURCE},
{457, EXCREMOTE},
{458, EXCLONGTRANS},
{459, EXCREMOTE},
{460, EXCRESOURCE},
{465, EXCRESOURCE},
{468, EXCNOCONNECT},
{472, EXCCONVERT},
{473, EXCCONVERT},
{474, EXCCONVERT},
{482, EXCNOCURSOR},
{484, EXCFILENAME},
{500, EXCDUPINDEX},
{501, EXCDUPINDEX},
{502, EXCNOINDEX},
{503, EXCRESOURCE},
{504, EXCVIEWUSE},
{505, EXCSYNTAX},
{506, EXCPERMISSION},
{507, EXCNOCURSOR},
{508, EXCTEMPTABLEUSE},
{509, EXCTEMPTABLEUSE},
{510, EXCTEMPTABLEUSE},
{512, EXCPERMISSION},
{514, EXCPERMISSION},
{515, EXCNOCONSTRAINT},
{517, EXCRESOURCE},
{518, EXCNOCONSTRAINT},
{519, EXCCONVERT},
{521, EXCITEMLOCK},
{522, EXCNOTABLE},
{524, EXCNOTINTRANS},
{525, EXCREFINT},
{526, EXCNOCURSOR},
{528, EXCRESOURCE},
{529, EXCNOCONNECT},
{530, EXCCHECK},
{531, EXCDUPCOLUMN},
{532, EXCTEMPTABLEUSE},
{534, EXCITEMLOCK},
{535, EXCMANAGETRANS},
{536, EXCSYNTAX},
{537, EXCNOCONSTRAINT},
{538, EXCDUPCURSOR},
{539, EXCRESOURCE},
{540, EXCDBCORRUPT},
{541, EXCPERMISSION},
{543, EXCAMBCOLUMN},
{543, EXCSYNTAX},
{544, EXCAGGREGATEUSE},
{545, EXCPERMISSION},
{548, EXCTEMPTABLEUSE},
{549, EXCNOCOLUMN},
{550, EXCRESOURCE},
{551, EXCRESOURCE},
{554, EXCSYNTAX},
{559, EXCDUPSYNONYM},
{560, EXCDBCORRUPT},
{561, EXCAGGREGATEUSE},
{562, EXCCONVERT},
{536, EXCITEMLOCK},
{564, EXCRESOURCE},
{565, EXCRESOURCE},
{566, EXCRESOURCE},
{567, EXCRESOURCE},
{568, EXCCROSSDB},
{569, EXCCROSSDB},
{570, EXCCROSSDB},
{571, EXCCROSSDB},
{573, EXCMANAGETRANS},
{574, EXCAMBCOLUMN},
{576, EXCTEMPTABLEUSE},
{577, EXCDUPCONSTRAINT},
{578, EXCSYNTAX},
{579, EXCPERMISSION},
{580, EXCPERMISSION},
{582, EXCMANAGETRANS},
{583, EXCPERMISSION},
{586, EXCDUPCURSOR},
{589, EXCREMOTE},
{590, EXCDBCORRUPT},
{591, EXCCONVERT},
{592, EXCNOTNULLCOLUMN},
{593, EXCSERIAL},
{594, EXCBLOBUSE},
{595, EXCAGGREGATEUSE},
{597, EXCDBCORRUPT},
{598, EXCNOCURSOR},
{599, EXCSYNTAX},
{600, EXCMANAGEBLOB},
{601, EXCMANAGEBLOB},
{602, EXCMANAGEBLOB},
{603, EXCMANAGEBLOB},
{604, EXCMANAGEBLOB},
{605, EXCMANAGEBLOB},
{606, EXCMANAGEBLOB},
{607, EXCSUBSCRIPT},
{608, EXCCONVERT},
{610, EXCBLOBUSE},
{611, EXCBLOBUSE},
{612, EXCBLOBUSE},
{613, EXCBLOBUSE},
{614, EXCBLOBUSE},
{615, EXCBLOBUSE},
{616, EXCBLOBUSE},
{617, EXCBLOBUSE},
{618, EXCMANAGEBLOB},
{622, EXCNOINDEX},
{623, EXCNOCONSTRAINT},
{625, EXCDUPCONSTRAINT},
{628, EXCMANAGETRANS},
{629, EXCMANAGETRANS},
{630, EXCMANAGETRANS},
{631, EXCBLOBUSE},
{635, EXCPERMISSION},
{636, EXCRESOURCE},
{638, EXCBLOBUSE},
{639, EXCBLOBUSE},
{640, EXCDBCORRUPT},
{649, EXCFILENAME},
{650, EXCRESOURCE},
{651, EXCRESOURCE},
/* I'm not about to map all possible compile/runtime SPL errors. */
/* Here's a few. */
{655, EXCSYNTAX},
{667, EXCSYNTAX},
{673, EXCDUPSPROC},
{674, EXCNOSPROC},
{678, EXCSUBSCRIPT},
{681, EXCDUPCOLUMN},
{686, EXCMANYROW},
{690, EXCREFINT},
{691, EXCREFINT},
{692, EXCREFINT},
{702, EXCITEMLOCK},
{703, EXCNOTNULLCOLUMN},
{704, EXCDUPCONSTRAINT},
{706, EXCPERMISSION},
{707, EXCBLOBUSE},
{722, EXCRESOURCE},
{958, EXCDUPTABLE},
{1214, EXCCONVERT},
{1262, EXCCONVERT},
{1264, EXCCONVERT},
{25553, EXCNOCONNECT},
{25587, EXCNOCONNECT},
{25588, EXCNOCONNECT},
{25596, EXCNOCONNECT},
{0, 0}
}; /* ends of list */
static int errTranslate(int code)
{
const struct ERRORMAP *e;
for(e=errormap; e->infcode; ++e) {
if(e->infcode == code)
return e->excno;
}
return EXCSQLMISC;
} /* errTranslate */
static eb_bool errorTrap(void)
{
short i;
/* innocent until proven guilty */
rv_lastStatus = 0;
rv_vendorStatus = 0;
rv_stmtOffset = 0;
rv_badToken = 0;
if(ENGINE_ERRCODE >= 0) return eb_false; /* no problem */
/* log the SQL statement that elicitted the error */
showStatement();
rv_vendorStatus = -ENGINE_ERRCODE;
rv_lastStatus = errTranslate(rv_vendorStatus);
rv_stmtOffset = sqlca.sqlerrd[4];
rv_badToken = sqlca.sqlerrm;
if(!rv_badToken[0]) rv_badToken = 0;
/* if the application didn't trap for this exception, blow up! */
if(exclist)
for(i=0; exclist[i]; ++i)
if(exclist[i] == rv_lastStatus) {
exclist = 0; /* we've spent that exception */
return eb_true;
}
/* Remember, errorPrint() should not return. */
errorPrint("2SQL error %d, %s", rv_vendorStatus, sqlErrorList[rv_lastStatus]);
return eb_true; /* make the compiler happy */
} /* errorTrap */
/*********************************************************************
The OCURS structure given below maintains an open SQL cursor.
A static array of these structures allows multiple cursors
to be opened simultaneously.
*********************************************************************/
static struct OCURS {
char sname[8]; /* statement name */
char cname[8]; /* cursor name */
struct sqlda *desc;
char rv_type[NUMRETS];
long rownum;
short cid; /* cursor ID */
char flag;
char numRets;
} ocurs[NUMCURSORS];
/* values for struct OCURS.flag */
#define CURSOR_NONE 0
#define CURSOR_PREPARED 1
#define CURSOR_OPENED 2
/* find a free cursor structure */
static struct OCURS *findNewCursor(void)
{
struct OCURS *o;
short i;
for(o=ocurs, i=0; i<NUMCURSORS; ++i, ++o) {
if(o->flag != CURSOR_NONE) continue;
sprintf(o->cname, "c%u", i);
sprintf(o->sname, "s%u", i);
o->cid = 6000+i;
return o;
}
errorPrint("2more than %d cursors opend concurrently", NUMCURSORS);
return 0; /* make the compiler happy */
} /* findNewCursor */
/* dereference an existing cursor */
static struct OCURS *findCursor(int cid)
{
struct OCURS *o;
if(cid < 6000 || cid >= 6000+NUMCURSORS)
errorPrint("2cursor number %d is out of range", cid);
cid -= 6000;
o = ocurs+cid;
if(o->flag == CURSOR_NONE)
errorPrint("2cursor %d is not currently active", cid);
rv_numRets = o->numRets;
memcpy(rv_type, o->rv_type, NUMRETS);
return o;
} /* findCursor */
/* This doesn't close/free anything; it simply puts variables in an initial state. */
/* part of the disconnect() procedure */
static void clearAllCursors(void)
{
int i, j;
struct OCURS *o;
for(i=0, o=ocurs; i<NUMCURSORS; ++i, ++o) {
if(o->flag == CURSOR_NONE) continue;
o->flag = CURSOR_NONE;
o->rownum = 0;
} /* loop over cursors */
translevel = 0;
badtrans = eb_false;
} /* clearAllCursors */
/*********************************************************************
Connect and disconect to SQL databases.
*********************************************************************/
void sql_connect(const char *db, const char *login, const char *pw)
{
$char *dblocal = (char*)db;
login = pw = 0; /* not used here, so make the compiler happy */
if(isnullstring(dblocal)) {
dblocal = getenv("DBNAME");
if(isnullstring(dblocal))
errorPrint("2sql_connect receives no database, check $DBNAME");
}
if(sql_database) {
stmt_text = "disconnect";
debugStatement();
$disconnect current;
clearAllCursors();
sql_database = 0;
}
stmt_text = "connect";
debugStatement();
$connect to :dblocal;
if(errorTrap()) return;
sql_database = dblocal;
/* set default lock mode and isolation level for transaction management */
stmt_text = "lock isolation";
debugStatement();
$ set lock mode to wait;
if(errorTrap()) {
abort:
sql_disconnect();
return;
}
$ set isolation to committed read;
if(errorTrap()) goto abort;
exclist = 0;
} /* sql_connect */
void sql_disconnect(void)
{
if(sql_database) {
stmt_text = "disconnect";
debugStatement();
$disconnect current;
clearAllCursors();
sql_database = 0;
}
exclist = 0;
} /* sql_disconnect */
/* make sure we're connected to a database */
static void checkConnect(void)
{
if(!sql_database)
errorPrint("2SQL command issued, but no database selected");
} /* checkConnect */
/*********************************************************************
Begin, commit, and abort transactions.
SQL does not permit nested transactions; this API does, to a limited degree.
An inner transaction cannot fail while an outer one succeeds;
that would require SQL support which is not forthcoming.
However, as long as all transactions succeed, or the outer most fails,
everything works properly.
The static variable transLevel holds the number of nested transactions.
*********************************************************************/
/* begin a transaction */
void sql_begTrans(void)
{
rv_lastStatus = 0;
checkConnect();
stmt_text = 0;
/* count the nesting level of transactions. */
if(!translevel) {
badtrans = eb_false;
stmt_text = "begin work";
debugStatement();
$begin work;
if(errorTrap()) return;
}
++translevel;
exclist = 0;
} /* sql_begTrans */
/* end a transaction */
static void endTrans(eb_bool commit)
{
rv_lastStatus = 0;
checkConnect();
stmt_text = 0;
if(translevel == 0)
errorPrint("2end transaction without a matching begTrans()");
--translevel;
if(commit) {
stmt_text = "commit work";
debugStatement();
if(badtrans)
errorPrint("2Cannot commit a transaction around an aborted transaction");
if(translevel == 0) {
$commit work;
if(ENGINE_ERRCODE) ++translevel;
errorTrap();
}
} else { /* success or failure */
stmt_text = "rollback work";
debugStatement();
badtrans = eb_true;
if(!translevel) { /* bottom level */
$rollback work;
if(ENGINE_ERRCODE) --translevel;
errorTrap();
badtrans = eb_false;
}
} /* success or failure */
/* At this point I will make a bold assumption --
* that all cursors are declared with hold.
* Hence they remain valid after the transaction is closed,
* and we don't have to change any of the OCURS structures. */
exclist = 0;
} /* endTrans */
void sql_commitWork(void) { endTrans(eb_true); }
void sql_rollbackWork(void) { endTrans(eb_false); }
void sql_deferConstraints(void)
{
if(!translevel)
errorPrint("2Cannot defer constraints unless inside a transaction");
stmt_text = "defer constraints";
debugStatement();
$set constraints all deferred;
errorTrap();
exclist = 0;
} /* sql_deferConstraints */
/*********************************************************************
Blob management routines, a somewhat awkward interface.
Global variables tell SQL where to unload the next fetched blob:
either a file (truncate or append) or an allocated chunk of memory.
This assumes each fetch or select statement retrieves at most one blob.
Since there is no %blob directive in lineFormat(),
one cannot simply slip a blob in with the rest of the data as a row is
updated or inserted. Instead the row must be created first,
then the blob is entered separately, using blobInsert().
This means every blob column must permit nulls, at least within the schema.
Also, what use to be an atomic insert might become a multi-statement
transaction if data integrity is important.
Future versions of our line formatting software may support a %blob directive,
which makes sense only when the formatted string is destined for SQL.
*********************************************************************/
static loc_t blobstruct; /* Informix structure to manage the blob */
/* insert a blob into the database */
void sql_blobInsert(const char *tabname, const char *colname, int rowid,
const char *filename, void *offset, int length)
{
$char blobcmd[100];
$loc_t insblob;
/* basic sanity checks */
checkConnect();
if(isnullstring(tabname)) errorPrint("2blobInsert, missing table name");
if(isnullstring(colname)) errorPrint("2blobInsert, missing column name");
if(rowid <= 0) errorPrint("2invalid rowid in blobInsert");
if(length < 0) errorPrint("2invalid length in blobInsert");
if(strlen(tabname) + strlen(colname) + 42 >= sizeof(blobcmd))
errorPrint("2internal blobInsert command too long");
/* set up the blob structure */
memset(&insblob, 0, sizeof(insblob));
if(!filename) {
insblob.loc_loctype = LOCMEMORY;
if(offset) {
if(length == 0) offset = 0;
}
if(!offset) length = -1;
insblob.loc_buffer = offset;
insblob.loc_bufsize = length;
insblob.loc_size = length;
if(!offset) insblob.loc_indicator = -1;
} else {
insblob.loc_loctype = LOCFNAME;
insblob.loc_fname = (char*)filename;
insblob.loc_oflags = LOC_RONLY;
insblob.loc_size = -1;
}
/* set up the blob insert command, using one host variable */
sprintf(blobcmd, "update %s set %s = ? where rowid = %d",
tabname, colname, rowid);
stmt_text = blobcmd;
debugStatement();
$prepare blobinsert from :blobcmd;
if(errorTrap()) return;
$execute blobinsert using :insblob;
errorTrap();
rv_lastNrows = sqlca.sqlerrd[2];
rv_lastRowid = sqlca.sqlerrd[5];
if(sql_debug) appendFile(sql_debuglog, "%d rows affected", rv_lastNrows);
exclist = 0;
} /* sql_blobInsert */
/*********************************************************************
When an SQL statement is prepared, the engine tells us the types and lengths
of the columns. Use this information to "normalize" the sqlda
structure, so that columns are fetched using our preferred formats.
For instance, smallints and ints both map into int variables,
varchars become chars, dates map into strings (so that we can convert
them into our own vendor-independent binary representations later), etc.
We assume the number and types of returns have been established.
Once retsSetup has "normalized" the sqlda structure,
run the select or fetch, and then call retsCleanup to post-process the data.
This will, for example, turn dates, fetched into strings,
into our own 4-byte representations.
The same for time intervals, money, etc.
*********************************************************************/
/* Temp area to read the Informix values, as strings */
static char retstring[NUMRETS][STRINGLEN+4];
static va_list sqlargs;
static void retsSetup(struct sqlda *desc)
{
short i;
eb_bool blobpresent = eb_false;
struct sqlvar_struct *v;
for(i=0; (unsigned)i< NUMRETS; ++i) {
rv_data[i].l = nullint;
retstring[i][0] = 0;
rv_name[i][0] = 0;
}
if(!desc) return;
for(i=0,v=desc->sqlvar; i<rv_numRets; ++i,++v ) {
strncpy(rv_name[i], v->sqlname, COLNAMELEN);
switch(rv_type[i]) {
case 'S':
case 'C':
case 'D':
case 'I':
v->sqltype = CCHARTYPE;
v->sqllen = STRINGLEN+2;
v->sqldata = retstring[i];
rv_data[i].ptr = retstring[i];
break;
case 'N':
v->sqltype = CINTTYPE;
v->sqllen = 4;
v->sqldata = (char *) &rv_data[i].l;
break;
case 'F':
case 'M':
v->sqltype = CDOUBLETYPE;
v->sqllen = 8;
v->sqldata = (char*) &rv_data[i].f;
rv_data[i].f = nullfloat;
break;
case 'B':
case 'T':
if(blobpresent)
errorPrint("2Cannot select more than one blob at a time");
blobpresent = eb_true;
v->sqltype = CLOCATORTYPE;
v->sqllen = sizeof(blobstruct);
v->sqldata = (char*) &blobstruct;
memset(&blobstruct, 0, sizeof(blobstruct));
if(!rv_blobFile) {
blobstruct.loc_loctype = LOCMEMORY;
blobstruct.loc_mflags = LOC_ALLOC;
blobstruct.loc_bufsize = -1;
} else {
blobstruct.loc_loctype = LOCFNAME;
blobstruct.loc_fname = (char*)rv_blobFile;
blobstruct.lc_union.lc_file.lc_mode = 0600;
blobstruct.loc_oflags =
(rv_blobAppend ? LOC_WONLY|LOC_APPEND : LOC_WONLY);
}
break;
default:
errorPrint("@bad character %c in retsSetup", rv_type[i]);
} /* switch */
} /* loop over fetched columns */
} /* retsSetup */
/* clean up fetched values, eg. convert date to our proprietary format. */
static void retsCleanup(void)
{
short i, l;
eb_bool yearfirst;
/* no blobs unless proven otherwise */
rv_blobLoc = 0;
rv_blobSize = nullint;
for(i=0; i<rv_numRets; ++i) {
clipString(retstring[i]);
switch(rv_type[i]) {
case 'D':
yearfirst = eb_false;
if(retstring[i][4] == '-') yearfirst = eb_true;
rv_data[i].l = stringDate(retstring[i],yearfirst);
break;
case 'I':
/* thanks to stringTime(), this works for either hh:mm or hh:mm:ss */
if(retstring[i][0] == 0) rv_data[i].l = nullint;
else {
/* convert space to 0 */
if(retstring[i][1] == ' ') retstring[i][1] = '0';
/* skip the leading space that is produced when Informix converts interval to string */
rv_data[i].l = stringTime(retstring[i]+1);
}
break;
case 'C':
rv_data[i].l = retstring[i][0];
break;
case 'M':
case 'F':
/* null floats look different from null dates and ints. */
if(rv_data[i].l == 0xffffffff) {
rv_data[i].f = nullfloat;
if(rv_type[i] == 'M') rv_data[i].l = nullint;
break;
}
/* represent monitary amounts as an integer number of pennies. */
if(rv_type[i] == 'M')
rv_data[i].l = rv_data[i].f * 100.0 + 0.5;
break;
case 'S':
/* map the empty string into the null string */
l = strlen(retstring[i]);
if(!l) rv_data[i].ptr = 0;
if(l > STRINGLEN) errorPrint("2fetched string is too long, limit %d chars", STRINGLEN);
break;
case 'B':
case 'T':
if(blobstruct.loc_indicator >= 0) { /* not null blob */
rv_blobSize = blobstruct.loc_size;
if(!rv_blobFile) rv_blobLoc = blobstruct.loc_buffer;
if(rv_blobSize == 0) { /* turn empty blob into null blob */
nzFree(rv_blobLoc);
rv_blobLoc = 0;
rv_blobSize = nullint;
}
}
rv_data[i].l = rv_blobSize;
break;
case 'N':
/* Convert from Informix null to our nullint */
if(rv_data[i].l == 0x80000000) rv_data[i].l = nullint;
break;
default:
errorPrint("@bad character %c in retsCleanup", rv_type[i]);
} /* switch on datatype */
} /* loop over columsn fetched */
} /* retsCleanup */
void retsCopy(eb_bool allstrings, void *first, ...)
{
void *q;
int i;
if(!rv_numRets)
errorPrint("@calling retsCopy() with no returns pending");
for(i=0; i<rv_numRets; ++i) {
if(first) {
q = first;
va_start(sqlargs, first);
first = 0;
} else {
q = va_arg(sqlargs, void*);
}
if(!q) break;
if((int)q < 1000 && (int)q > -1000)
errorPrint("2retsCopy, pointer too close to 0");
if(allstrings) *(char*)q = 0;
if(rv_type[i] == 'S') {
*(char*)q = 0;
if(rv_data[i].ptr)
strcpy(q, rv_data[i].ptr);
} else if(rv_type[i] == 'C') {
*(char *)q = rv_data[i].l;
if(allstrings) ((char*)q)[1] = 0;
} else if(rv_type[i] == 'F') {
if(allstrings) {
if(isnotnullfloat(rv_data[i].f)) sprintf(q, "%lf", rv_data[i].f);
} else {
*(double *)q = rv_data[i].f;
}
} else if(allstrings) {
char type = rv_type[i];
long l = rv_data[i].l;
if(isnotnull(l)) {
if(type == 'D') {
strcpy(q, dateString(l, DTDELIMIT));
} else if(type == 'I') {
strcpy(q, timeString(l, DTDELIMIT));
} else if(type == 'M') {
sprintf(q, "%ld.%02d", l/100, l%100);
} else sprintf(q, "%ld", l);
}