-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbdimp.ic
2376 lines (2067 loc) · 67.3 KB
/
dbdimp.ic
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
/* Copyright (c) 1999-2024 H.Merijn Brand <[email protected]>
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the Perl README file.
*
* Large parts of this file are shamelesly copied from other DBD drivers,
* after which they are formatted to be readable, stripped and modified
* to reflect the way UNIFY can work with it.
*
* Much effort has been put in keeping this driver as clean as possible.
* It consists entirely of E/SQL statements. ufchmsg () in the sqlError ()
* function is the *ONLY* HLI call.
*
* Main sources were Oracle (1.03), FreeTDS (0.02) and Ingres (0.24, 0.25)
* whose writers seem to have copied from other sources too ;-)
*
* Thanks to Tim Bunce for valuable input in his tutorials on O'Reilly's
* Open Source Conference 2000 in Monterey, and his code review.
*
* Thanks to all other DBD writers for making DBI such a success ;-)
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
/* Get this from previously installed DBI module ...
* Makefile.PL will find it's include path
*/
#define NEED_DBIXS_VERSION 7
#include <DBIXS.h>
#include <config.h> /* perl's config, I hope */
#include <dbd_xsh.h>
#define NEED_sv_2pv_flags
#define UEXTERN static
#define UINIT0 =0
#include "dbdimp.h"
DBISTATE_DECLARE;
/* Unify stuff off here */
#include <sqle_usr.h>
#include <sqlerr.h>
#include <dbtypes.h>
#include <rhli.h>
#include <rhlierr.h>
#ifndef SQLCURRENCY
# define SQLCURRENCY -18
# endif
#ifndef SQLDATETIME
# define SQLDATETIME 0xDEADBEAF
# endif
extern int rangchk (int, int);
extern int sqlalcr (char *, SQLCRCB *, SQLDBGST *);
extern int sqlald2 (SQLDACB *, int, int, SQLDBGST *);
extern int sqlcldb ( SQLDBGST *);
extern int sqlcls (CRSCB *, SQLDBGST *);
extern SQLCRCB *sqlcrnm (char *, int, SQLDBGST *);
extern SQLDACB *sqldanm (char *, int, SQLDBGST *);
extern int sqldcls (SQLCRCB *, SQLDBGST *);
extern int sqldesc (SQLDSCRB, SQLSTCB *, SQLDACB *, SQLDBGST *);
extern int sqldlda (SQLDACB *, SQLDBGST *);
extern int sqldfch (SQLCRCB *, int, HVDS *, SQLFCHDIR, int, SQLDBGST *);
extern int sqldlst (SQLSTCB *, SQLDBGST *);
extern int sqldopn (SQLCRCB *, int, HVDS *, int, SQLDBGST *);
extern int sqldxec (SQLSTCB *, int, HVDS *, int, HVDS *, SQLDBGST *);
extern int sqlfch (CRSCB *, SQLFCHDIR, int, SQLDBGST *);
extern int sqlgtd2 (SQLDACB *, int, int, int, long, int *,
void *, int, SQLDBGST *);
extern int sqlopdb (char *, SQLDBGST *);
extern int sqlopn (CRSCB *, SQLDBGST *);
extern int sqlprep (char *, SQLSTCB *, SQLDBGST *);
extern int sqlstcd (int);
extern int sqlstd2 (SQLDACB *, int, int, SQLDA_FIELD *,
HVDS *, int, SQLDBGST *);
extern SQLSTCB *sqlstnm (char *, int, SQLDBGST *);
extern int sqltxop (int, int, SQLDBGST *);
extern int sqlxec (CRSCB *, SQLDBGST *);
extern char *basename (const char *path);
extern int uallcgp (UTXID, UTID, int, int*, UCID **, USTATUS *);
extern int uinfcgp (UTXID, int, UCID *, UOPTS, UCGPINF *, USTATUS *, USTATUS *);
#define Max(a,b) ((a)>=(b)?(a):(b))
static char *pgm = NULL;
static byte *sth_id_on;
static short n_sth_id = 4; /* Start with max 32 $sth's */
static int dbd_verbose = 0;
/* The data dictionary */
HV *h_dd = NULL;
/* forward references */
int dbd_st_finish (SV *, imp_sth_t *);
int dbd_st_rows (SV *, imp_sth_t *);
/* Check source for use: these defines cannot be used inside EXEC SQL ! */
#define MAX_SQL_LEN 4096
#define MAX_NM_LEN 20
EXEC SQL BEGIN DECLARE SECTION;
char u_sql_do[4096];
char u_sql_st[4096];
char u_sql_nm[20];
char c_sql_nm[20];
char o_sql_nm[20]; /* Output descriptor area */
char i_sql_nm[20]; /* Input descriptor area */
int n_sql_st;
int fix, fln, fic, ftp, fpr, fsc, fnl;
char fnm[48], fdC[1028];
utxtptr fdB;
ubinptr fdX;
short fdS;
int fdL;
float fdF;
double fdD;
utime fdT;
udate fdDT;
uhdate fdHDT;
char fdDTTM[24];
EXEC SQL END DECLARE SECTION;
#define DBI_debug (dbis->debug & DBIc_TRACE_LEVEL_MASK)
/* For more info about error handling, read
* https://metacpan.org/pod/DBI::DBD#The-dbd_drv_error-method
*/
#ifdef I_STDARG
static void dbg (int level, char *fmt, ...)
#endif
#ifdef I_VARARGS
/* VARARGS2 */
static void dbg (level, fmt, va_alist)
int level;
char *fmt;
va_dcl
#endif
{
auto va_list args;
if (level > Max (dbd_verbose, DBI_debug))
return;
#ifdef I_STDARG
va_start (args, fmt);
#endif
#ifdef I_VARARGS
va_start (args);
#endif
/* DBILOGFP should ideally be replaced with DBIc_LOGPIO (imp_xxh)
* but dbg doesn't get a handle (yet) */
(void)PerlIO_vprintf (DBILOGFP, fmt, args);
(void)PerlIO_flush (DBILOGFP);
va_end (args);
} /* dbg */
#ifdef I_STDARG
static void st_dbg (int level, imp_sth_t *sth, char *fmt, ...)
#endif
#ifdef I_VARARGS
/* VARARGS2 */
static void st_dbg (level, sth, fmt, va_alist)
int level;
imp_sth_t *sth;
char *fmt;
va_dcl
#endif
{
auto va_list args;
if (level > Max (sth->dbd_verbose, Max (dbd_verbose, DBI_debug)))
return;
#ifdef I_STDARG
va_start (args, fmt);
#endif
#ifdef I_VARARGS
va_start (args);
#endif
(void)PerlIO_vprintf (DBIc_LOGPIO (sth), fmt, args);
(void)PerlIO_flush (DBIc_LOGPIO (sth));
va_end (args);
} /* st_dbg */
/* ##### Unify misc stuff ################################################## */
static void NYI (char *func) {
auto char die_msg[128];
(void)sprintf (die_msg, "DBD::UNIFY::%s () is not (yet) implemented", func);
die (die_msg);
} /* NYI */
void dbd_init (dbistate_t *dbistate) {
dTHX;
DBIS = dbistate;
(void)memset (fnm, 0, sizeof (fnm));
/* dbis->debug = 9; */
} /* dbd_init */
/* Error */
static void error (SV *h, int error_num, char *text) {
D_imp_xxh (h);
DBIh_SET_ERR_CHAR (h, imp_xxh, NULL, error_num, text, SQLSTATE, NULL);
} /* error */
/* Warning */
static void warning (SV *h, int error_num, char *text) {
D_imp_xxh (h);
DBIh_SET_ERR_CHAR (h, imp_xxh, "0", error_num, text, SQLSTATE, NULL);
} /* error */
/* Success with information */
static void info (SV *h, int error_num, char *text) {
D_imp_xxh (h);
DBIh_SET_ERR_CHAR (h, imp_xxh, "", error_num, text, SQLSTATE, NULL);
} /* error */
static int sqlError (SV *h) {
auto USTATUS status;
if (SQLCODE >= 0) {
if (SQLWARN < 0) {
dbg (4, "DBD::Unify::sqlError: SQLWARN = %d", SQLWARN);
warning (h, SQLWARN, ufchmsg (SQLWARN, &status));
}
return (1);
}
dbg (4, "DBD::Unify::sqlError: SQLCODE = %d", SQLCODE);
error (h, SQLCODE, ufchmsg (SQLCODE, &status));
dbg (4, ", returning\n");
return (0);
} /* sqlError */
/* ##### Unify DB stuff #################################################### */
int dbd_db_login (SV *dbh, imp_dbh_t *imp_dbh,
char *dbname, char *user, char *auth) {
EXEC SQL BEGIN DECLARE SECTION;
char statement[128];
EXEC SQL END DECLARE SECTION;
dTHX;
char *opt;
#ifndef PERL_USE_SAFE_PUTENV
PL_use_safe_putenv = 1;
#endif
if ((opt = getenv ("DBD_TRACE"))) {
auto int i = 0;
while (*opt) {
if (isdigit (*opt)) {
i = 10 * i + *opt - '0';
}
else {
i = -100;
}
opt++;
}
if (i >= 0 && i <= 99) {
dbd_verbose = i;
dbg (2, "Set DBD_VERBOSE = %d\n", dbd_verbose);
}
}
if ((opt = getenv ("DBD_VERBOSE"))) {
auto int i = 0;
while (*opt) {
unless (isdigit (*opt))
break;
i = 10 * i + *opt - '0';
opt++;
}
if (!*opt && i >= 0 && i <= 99) {
dbd_verbose = i;
dbg (2, "Set DBD_VERBOSE = %d\n", dbd_verbose);
}
}
dbg (3, "DBD::Unify::db_login: dbname: %s\n", dbname);
/* CONNECT [db_name];
*
* db_name: [[dbhost]:[dbuser]:][dbpath] [dbname]
* $DBHOST, $DBUSER, DBPATH, $DBNAME
*
* Users are implicitly checked by grants
*
* SET CURRENT SCHEMA TO 'USCHEMA';
*
* $USCHEMA (passed as $auth)
*/
opt = dbname;
/* look for options in dbname. Syntax: dbname;options */
while (*opt && *opt != ';')
++opt;
if (*opt == ';') {
*opt = 0; /* terminate dbname */
opt++; /* point to options */
}
if (user && *user && *user != '/') {
/* we have a username */
dbg (4, " user = '%s', opt = '%s' (ignored)\n", user, opt);
}
if (dbname && *dbname) {
(void)sprintf (statement, "DBPATH=%s", dbname);
(void)putenv (statement);
}
unless (pgm) {
/* Register program to monitor system, must be done BEFORE connect */
USTATUS ustatus;
pgm = basename (SvPV_nolen (get_sv ("0", 0)));
(void)uinimsg (pgm, &ustatus);
dbg (4, " After uinimsg ('%s'), status = %ld\n", pgm, ustatus);
}
EXEC SQL
CONNECT;
dbg (4, " After connect, sqlcode = %d\n", SQLCODE);
/* Problem number 22960: 2nd Connect to same database fails */
if (SQLCODE == -254) SQLCODE = 0;
unless (sqlError (dbh))
return (0);
DBIc_IMPSET_on (imp_dbh); /* imp_dbh set up now */
DBIc_ACTIVE_on (imp_dbh); /* call disconnect before freeing */
DBIc_set (imp_dbh, DBIcf_AutoCommit, 0);
DBIc_set (imp_dbh, DBIcf_ChopBlanks, 1);
imp_dbh->id = n_dbh++;
imp_dbh->children = (imp_sth_t **)0;
imp_dbh->nchildren = 0;
imp_dbh->unicode = 0;
unless (auth && *auth)
auth = getenv ("USCHEMA");
if ((!user || !*user) && auth && *auth) {
(void)sprintf (statement, "set current schema to \"%s\"", auth);
dbg (3, " %s\n", statement);
EXEC SQL
EXECUTE IMMEDIATE :statement;
dbg (4, " After schema, sqlcode = %d\n", SQLCODE);
unless (sqlError (dbh))
return (0);
}
unless (sth_id_on || (sth_id_on = (byte *)calloc (n_sth_id, 8))) {
error (dbh, errno, "Cannot allocate space for STH's");
return (0);
}
return (1);
} /* dbd_db_login */
static char *u_err (USTATUS s) {
USTATUS status;
static char e[2048];
char *msg = ufchmsg (s, &status);
sprintf (e, "%04d: %s", s, msg ? msg : "Unknown error");
return (e);
} /* u_err */
/* Fetch DB info and store in hash
%db{AUTH}[4] = { = $db{s}{"SYS"}
AID => 4,
NAME => "SYS,
TABLES => [ 77, ...],
],
$db{TABLE}[77] = { = $db{t}{"SYS.HASH_INDEXES"}
TID => 77,
NAME => "HASH_INDEXES",
OPTIONS => 0x12,
DIRECTKEY => 0,
SCATTERED => 0,
FIXEDSIZE => 0,
PKEYED => 0,
COLUMNS => [ 323, ...],
},
$db{COLUMN}[323] = {
TID => 77,
TNAME => "
CID => 323,
NAME => "OWNR",
TYPE => 5, # CHAR
LENGTH => 18,
SCALE => 0,
NULLABLE => 0,
DSP_LEN => 18,
DSP_SCL => 0,
DSP_PICT => "",
OPTIONS => 0x10,
PKEY => 0,
RDONLY => 0,
UNIQUE => 0,
LINK => -1, # CID
REFS = [],
},
$db{TYPE}[5] = [ 5, "CHAR", "CHAR" ],
*/
static void _db_dict (int refresh) {
int n, err;
pid_t pid;
UDBID dbid;
UAID *aidl;
UCID *cidl;
ULID *lidl;
UTID *tidl;
UTXID txid;
USTATUS status = -123;
UCID *a_lnk;
AV *a_typ;
AV *a_sch;
SV **A_anm = NULL;
AV **A_tbl;
char *A_typ[128];
if (h_dd) {
unless (refresh)
return;
dbg (9, "REFRESH DD DICT CACHE (%d)\n", refresh);
sv_free ((SV *)h_dd);
}
pid = getpid ();
unless (uopndb ((char *)0, DB_NOLOCK, &dbid, &status)) {
unless (status == -151) /* Database already opened. (-151) */
warn ("Cannot connect to database: %s\n", u_err (status));
return;
}
unless (sqlebtx (&txid, &status)) {
warn ("Cannot start a transaction: %s\n", u_err (status));
return;
}
h_dd = newHV ();
a_typ = newAV ();
a_sch = newAV ();
A_typ[U_INT] = "INTEGER";
A_typ[U_HINT] = "HUGE INTEGER";
A_typ[U_FLT] = "FLOAT";
A_typ[U_DBL] = "DOUBLE";
A_typ[U_REAL] = "REAL";
A_typ[U_AMT] = "AMOUNT";
A_typ[U_HAMT] = "HUGE AMOUNT";
A_typ[U_DATE] = "DATE";
A_typ[U_HDATE] = "HUGE DATE";
A_typ[U_TIME] = "TIME";
A_typ[U_STR] = "CHAR";
A_typ[U_VTXT] = "TEXT";
A_typ[U_VBIN] = "BINARY";
A_typ[U_BYT] = "BYTE";
(void)av_store (a_typ, U_INT, newSVpv (A_typ[U_INT], 0));
(void)av_store (a_typ, U_HINT, newSVpv (A_typ[U_HINT], 0));
(void)av_store (a_typ, U_FLT, newSVpv (A_typ[U_FLT], 0));
(void)av_store (a_typ, U_DBL, newSVpv (A_typ[U_DBL], 0));
(void)av_store (a_typ, U_REAL, newSVpv (A_typ[U_REAL], 0));
(void)av_store (a_typ, U_AMT, newSVpv (A_typ[U_AMT], 0));
(void)av_store (a_typ, U_HAMT, newSVpv (A_typ[U_HAMT], 0));
(void)av_store (a_typ, U_DATE, newSVpv (A_typ[U_DATE], 0));
(void)av_store (a_typ, U_HDATE, newSVpv (A_typ[U_HDATE], 0));
(void)av_store (a_typ, U_TIME, newSVpv (A_typ[U_TIME], 0));
(void)av_store (a_typ, U_STR, newSVpv (A_typ[U_STR], 0));
(void)av_store (a_typ, U_VTXT, newSVpv (A_typ[U_VTXT], 0));
(void)av_store (a_typ, U_VBIN, newSVpv (A_typ[U_VBIN], 0));
(void)av_store (a_typ, U_BYT, newSVpv (A_typ[U_BYT], 0));
#ifdef H_BOOL
A_typ[U_BOOL] = "BOOL";
(void)av_store (a_typ, U_BOOL, newSVpv (A_typ[U_BOOL], 0));
#endif
#ifdef H_DEC
A_typ[U_DEC] = "DECIMAL";
(void)av_store (a_typ, U_DEC, newSVpv (A_typ[U_DEC], 0));
#endif
#ifdef H_GINT
A_typ[U_GINT] = "GIANT NUMERIC";
(void)av_store (a_typ, U_GINT, newSVpv (A_typ[U_GINT], 0));
#endif
#ifdef H_GAMT
A_typ[U_GAMT] = "GIANT AMOUNT";
(void)av_store (a_typ, U_GAMT, newSVpv (A_typ[U_GAMT], 0));
#endif
#ifdef H_DATETIME
A_typ[U_DATETIME] = "DATETIME";
(void)av_store (a_typ, U_DATETIME, newSVpv (A_typ[U_DATETIME], 0));
#endif
(void)hv_store (h_dd, "TYPE", 4, newRV_noinc ((SV *)a_typ), 0);
/* Fetch SCHEMA's (AUTH) */
if (uallath (txid, USLCK, &n, &aidl, &status)) {
UATHINF *al = (UATHINF *)calloc (n, sizeof (UATHINF));
USTATUS *asl = (USTATUS *)calloc (n, sizeof (USTATUS));
if (al && asl && uinfath (txid, n, aidl, UALLINFO, al, asl, &status)) {
int i, x = 0;
for (i = 0; i < n; i++) {
if (al[i].aid > x) x = al[i].aid;
}
A_anm = (SV **)calloc (x + 1, sizeof (SV *));
A_tbl = (AV **)calloc (x + 1, sizeof (AV *));
(void)hv_store (h_dd, "AUTH", 4, newRV_noinc ((SV *)a_sch), 0);
for (i = 0; i < n; i++) {
UAID aid = al[i].aid;
HV *h_dba = newHV ();
AV *a_tbl = newAV ();
A_anm[aid] = newSVpv (al[i].authnm, 0);
(void)hv_store (h_dba, "AID", 3, newSViv (aid), 0);
(void)hv_store (h_dba, "NAME", 4, A_anm[aid], 0);
(void)hv_store (h_dba, "TABLES", 6, newRV_noinc ((SV *)a_tbl), 0);
(void)av_store (a_sch, aid, newRV_noinc ((SV *)h_dba));
A_tbl[aid] = a_tbl;
}
}
if (al) free (al);
if (asl) free (asl);
if (n) free (aidl);
}
/* Fetch links: only one-to-one links are supported */
if (ualllnk (txid, UNULLTID, USLCK, &n, &lidl, &status)) {
ULNKINF *ll = (ULNKINF *)calloc (n, sizeof (ULNKINF));
USTATUS *lsl = (USTATUS *)calloc (n, sizeof (USTATUS));
if (ll && lsl && uinflnk (txid, n, lidl, UALLINFO, ll, lsl, &status)) {
int i, j, x = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < ll[i].count; j++) {
if (ll[i].cloc[j] > x) x = ll[i].cloc[j];
}
}
a_lnk = (UCID *)calloc (x + 1, sizeof (UCID));
a_lnk[0] = x;
for (i = 0; i < n; i++) {
if (ll[i].count == 1)
a_lnk[ll[i].cloc[0]] = ll[i].ploc[0];
}
}
if (ll) free (ll);
if (lsl) free (lsl);
if (n) free (lidl);
}
else {
warn ("Call to fetch all links failed with code %d: %s\n", err, u_err (status));
}
err = ualltbl (txid, UNULLAID, USLCK, &n, &tidl, &status);
if (err != USUCCESS || n <= 0) {
warn ("Call to fetch all tables failed with code %d: %s\n", err, u_err (status));
}
else {
UTBLINF *tinfl = NULL;
USTATUS *tsl = NULL;
if ((tinfl = (UTBLINF *)calloc (n, sizeof (UTBLINF))) != NULL &&
(tsl = (USTATUS *)calloc (n, sizeof (USTATUS))) != NULL &&
uinftbl (txid, n, tidl, UALLINFO, tinfl, tsl, &status)) {
int i, c, g;
AV *a_tbl = newAV ();
AV *a_fld = newAV ();
(void)hv_store (h_dd, "TABLE", 5, newRV_noinc ((SV *)a_tbl), 0);
(void)hv_store (h_dd, "COLUMN", 6, newRV_noinc ((SV *)a_fld), 0);
for (i = 0; i < n; i++) {
UTID tid = tidl[i];
UTBLINF *ti = &tinfl[i];
UAID aid = ti->aid;
UCID *cl = ti->ucidlst;
USTATUS *csl = NULL;
UCOLINF *cinfl = NULL;
HV *h_dbt = newHV ();
AV *a_dbc = newAV ();
AV *a_dbk = newAV ();
AV *a_grp = newAV ();
SV *t_nm;
char *tname = "?";
char **tnames = NULL;
int nn;
UOPTS tops = ti->tblopts;
if (ufchtnm (txid, tid, &nn, &tnames, &status))
tname = tnames[0];
t_nm = newSVpv (tname, 0);
(void)hv_store (h_dbt, "AID", 3, newSViv (aid), 0);
(void)hv_store (h_dbt, "ANAME", 5, SvREFCNT_inc (A_anm[aid]), 0);
(void)hv_store (h_dbt, "TID", 3, newSViv (tid), 0);
(void)hv_store (h_dbt, "NAME", 4, t_nm, 0);
(void)hv_store (h_dbt, "OPTIONS", 7, newSViv (tops), 0);
(void)hv_store (h_dbt, "DIRECTKEY", 9, newSViv (tops & DB_DIRECT ? 1 : 0), 0);
(void)hv_store (h_dbt, "SCATTERED", 9, newSViv (tops & DB_SCATTR ? 1 : 0), 0);
(void)hv_store (h_dbt, "FIXEDSIZE", 9, newSViv (tops & DB_FIXSIZE ? 1 : 0), 0);
(void)hv_store (h_dbt, "EXPNUM", 6, newSViv (ti->expnum), 0);
(void)hv_store (h_dbt, "PKEYED", 6, newSViv (tops & DB_KEYED ? 1 : 0), 0);
(void)hv_store (h_dbt, "COLUMNS", 7, newRV_noinc ((SV *)a_dbc), 0);
(void)hv_store (h_dbt, "KEY", 3, newRV_noinc ((SV *)a_dbk), 0);
(void)hv_store (h_dbt, "CGRP", 4, newRV_noinc ((SV *)a_grp), 0);
/* do I want to keep the ti->tbldesc as DESCRIPTION ? */
/* No need here to store nvol, nbtree, nhsh, and nlnk yet */
(void)av_store (a_tbl, tid, newRV_noinc ((SV *)h_dbt));
if (A_tbl[aid]) av_push (A_tbl[aid], newSViv (tid));
for (c = 0; c < ti->nkey; c++)
av_push (a_dbk, newSViv (ti->keycidl[c]));
if ((cinfl = (UCOLINF *)calloc (ti->ncol, sizeof (UCOLINF))) != NULL &&
(csl = (USTATUS *)calloc (ti->ncol, sizeof (USTATUS))) != NULL &&
uinfcol (txid, ti->ncol, cl, UALLINFO, cinfl, csl, &status)) {
for (c = 0; c < ti->ncol; c++) {
UCID cid = cl[c];
UCOLINF *ci = &cinfl[c];
HV *h_dbc = newHV ();
char *cname = "?";
char **cnames = NULL;
UOPTS cops = ci->colopts;
int ctyp = ci->coltyp;
int pkey = cops & DB_COLKEY ? 1 : 0;
AV *a_plnk = newAV ();
if (ufchcnm (txid, cid, &nn, &cnames, &status))
cname = cnames[0];
if (ti->nkey > 1) { /* Combined keys */
int kc;
for (kc = 0; kc < ti->nkey; kc++) {
if (ti->keycidl[kc] == cid) pkey++;
}
}
av_push (a_dbc, newSViv (cid));
(void)hv_store (h_dbc, "TID", 3, newSViv (tid), 0);
(void)hv_store (h_dbc, "TNAME", 5, SvREFCNT_inc (t_nm), 0);
(void)hv_store (h_dbc, "CID", 3, newSViv (cid), 0);
(void)hv_store (h_dbc, "NAME", 4, newSVpv (cname, 0), 0);
(void)hv_store (h_dbc, "TYPE", 4, newSViv (ctyp), 0);
(void)hv_store (h_dbc, "LENGTH", 6, newSViv (ci->collen), 0);
(void)hv_store (h_dbc, "SCALE", 5, newSViv (ci->colscl), 0);
(void)hv_store (h_dbc, "NULLABLE", 8, newSViv (cops &
(DB_NONULL | DB_COLKEY) ? 0 : 1), 0);
(void)hv_store (h_dbc, "DSP_LEN", 7, newSViv (ci->dsplen), 0);
(void)hv_store (h_dbc, "DSP_SCL", 7, newSViv (ci->dspscl), 0);
(void)hv_store (h_dbc, "DSP_PICT", 8, newSVpv (ci->dsppict, 0), 0);
(void)hv_store (h_dbc, "OPTIONS", 7, newSViv (cops), 0);
(void)hv_store (h_dbc, "PKEY", 4, newSViv (pkey), 0);
(void)hv_store (h_dbc, "RDONLY", 6, newSViv (cops & DB_RDONLY ? 1 : 0), 0);
(void)hv_store (h_dbc, "UNIQUE", 6, newSViv (cops & DB_UNIQUE ? 1 : 0), 0);
(void)hv_store (h_dbc, "LINK", 4, newSViv (a_lnk && cid <= a_lnk[0]
&& a_lnk[cid] > 0 ? a_lnk[cid] : -1), 0);
(void)hv_store (h_dbc, "REFS", 4, newRV_noinc ((SV *)a_plnk), 0);
(void)hv_store (h_dbc, "NBTREE", 6, newSViv (ci->nbt), 0);
(void)hv_store (h_dbc, "NHASH", 5, newSViv (ci->nhsh), 0);
(void)hv_store (h_dbc, "NPLINK", 6, newSViv (ci->nplnk), 0);
(void)hv_store (h_dbc, "NCLINK", 6, newSViv (ci->nclnk), 0);
(void)av_store (a_fld, cid, newRV_noinc ((SV *)h_dbc));
if (ci->nplnk && a_lnk && a_lnk[0]) {
int lc;
for (lc = 1; lc < a_lnk[0]; lc++) {
if (a_lnk[lc] == cid)
av_push (a_plnk, newSViv (lc));
}
}
if (cnames) free (cnames);
}
free (cinfl);
}
/* Fetch col-groups */
if (uallcgp (txid, tid, USLCK, &g, &cidl, &status)) {
UCGPINF *lcg = (UCGPINF *)calloc (g, sizeof (UCGPINF));
USTATUS *lsl = (USTATUS *)calloc (g, sizeof (USTATUS));
if (g && lcg && lsl && uinfcgp (txid, g, cidl, UNOCLASS, lcg, lsl, &status)) {
int i, j, x = 0;
for (i = 0; i < g; i++) {
if (lcg[i].ncol > 0) {
HV *h_grp = newHV ();
AV *a_cid = newAV ();
for (j = 0; j < lcg[i].ncol; j++)
(void)av_store (a_cid, j, newSViv (lcg[i].grpcidl[j]));
(void)hv_store (h_grp, "CID", 3, newSViv (lcg[i].cid), 0);
(void)hv_store (h_grp, "TYPE", 4, newSViv (lcg[i].coltyp), 0);
(void)hv_store (h_grp, "COLUMNS", 7, newRV_noinc ((SV *)a_cid), 0);
(void)av_store (a_grp, x++, newRV_noinc ((SV *)h_grp));
}
}
}
if (lcg) free (lcg);
if (lsl) free (lsl);
if (g) free (cidl);
}
if (csl) free (csl);
if (tnames) free (tnames);
}
free (tinfl);
}
if (tsl) free (tsl);
free (tidl);
}
free (A_tbl);
if (a_lnk) free (a_lnk);
if (A_anm) free (A_anm);
/* Committing and disconnecting disables all subsequent actions
unless (ucmttx (txid, &status))
warn ("Cannot commit transaction: %s\n", u_err (status));
unless (uclsdb (dbid, DB_NOLOCK, &status)) {
warn ("Cannot close database connection: %s\n", u_err (status));
return;
}
*/
} /* _db_dict */
void dbd_st_destroy (SV *, imp_sth_t *); /* Forward ref */
/* Until those babys are able to change their own dirty nappies ... */
static void change_offspring (SV *dbh, imp_dbh_t *imp_dbh) {
imp_sth_t **children;
int i, n;
/* Make this function extremely precautious ;-P */
unless (imp_dbh) return;
unless (children = imp_dbh->children) return;
unless ((n = imp_dbh->nchildren) > 0) return;
for (i = 0; i < n; i++) {
imp_sth_t *imp_sth = children[i];
if (!imp_sth || imp_sth->stat > ST_STAT_OPEN
|| imp_sth->stat & ST_STAT_OPEN) continue;
if (2 > DBIc_TRACE_LEVEL (imp_sth) && 2 > dbd_verbose) {
dbg (3, "-- %03d/%03d 0x%08X %02X",
i + 1, n, imp_sth, imp_sth ? imp_sth->stat : 0);
if (imp_sth && imp_sth->statement)
dbg (3, " '%s'", imp_sth->statement);
dbg (3, "\n");
}
dbd_st_destroy (dbh, imp_sth);
}
} /* change_offspring */
static void dbd_st_diaper (imp_dbh_t *imp_dbh, imp_sth_t *imp_sth) {
imp_sth_t **children = imp_dbh->children;
int i, n = imp_dbh->nchildren;
for (i = 0; i < n; i++) {
if (children[i]) continue;
children[i] = imp_sth;
return;
}
if (n) imp_dbh->children = (imp_sth_t **)realloc ((void *)imp_dbh->children, (imp_dbh->nchildren + 1) * sizeof (imp_sth_t *));
else imp_dbh->children = (imp_sth_t **) malloc (sizeof (imp_sth_t *));
if (imp_dbh->children) imp_dbh->children[imp_dbh->nchildren++] = imp_sth;
else imp_dbh->nchildren = 0;
} /* dbd_st_diaper */
static void dbd_st_growup (imp_dbh_t *imp_dbh, imp_sth_t *imp_sth) {
imp_sth_t **children = imp_dbh->children;
int i, n = imp_dbh->nchildren;
for (i = 0; i <= n; i++) {
unless (children[i] == imp_sth) continue;
imp_dbh->children[i] = 0;
return;
}
} /* dbd_st_growup */
int dbd_db_commit (SV *dbh, imp_dbh_t *imp_dbh) {
dTHX;
dbg (3, "DBD::Unify::db_commit\n");
unless (DBIc_ACTIVE (imp_dbh))
return (0);
change_offspring (dbh, imp_dbh);
/* Check for commit () being called whilst refs to cursors
* still exists. This needs some more thought.
*/
if (DBIc_ACTIVE_KIDS (imp_dbh) && DBIc_WARN (imp_dbh) && !PL_dirty) {
warn ("DBD::Unify::db_commit (%s) invalidates %d active cursor(s)",
SvPV_nolen (dbh), (int)DBIc_ACTIVE_KIDS (imp_dbh));
}
EXEC SQL
COMMIT WORK;
return (sqlError (dbh));
} /* dbd_db_commit */
int dbd_db_rollback (SV *dbh, imp_dbh_t *imp_dbh) {
dTHX;
dbg (3, "DBD::Unify::db_rollback\n");
unless (DBIc_ACTIVE (imp_dbh))
return (0);
change_offspring (dbh, imp_dbh);
/* Check for rollback () being called whilst refs to cursors
* still exists. See dbd_db_commit ()
*/
if (DBIc_ACTIVE_KIDS (imp_dbh) && DBIc_WARN (imp_dbh) && !PL_dirty) {
warn ("DBD::Unify::db_rollback (%s) invalidates %d active cursor(s)",
SvPV_nolen (dbh), (int)DBIc_ACTIVE_KIDS (imp_dbh));
}
EXEC SQL
ROLLBACK WORK;
return (sqlError (dbh));
} /* dbd_db_rollback */
int dbd_db_dict (SV *dbh, int reload) {
dTHX;
D_imp_dbh (dbh);
dbg (3, "DBD::Unify::db_dict (%d)\n", reload);
_db_dict (reload);
sv_setsv (DEFSV, newRV_noinc ((SV *)h_dd)); /* $_ = \%db */
return (1);
} /* dbd_db_dict */
int dbd_db_do (SV *dbh, char *statement) {
dTHX;
D_imp_dbh (dbh);
dbg (3, "DBD::Unify::db_do (\"%s\")\n", statement);
unless (DBIc_ACTIVE (imp_dbh))
return (0);
if (strlen (statement) >= MAX_SQL_LEN) {
warn ("DBD::Unify::db_do (\"%.40s ...\") statement too long", statement);
return (0);
}
(void)strcpy (u_sql_do, statement);
EXEC SQL
EXECUTE IMMEDIATE :u_sql_do;
dbg (4, " After execute, sqlcode = %d\n", SQLCODE);
unless (sqlError (dbh))
return (0);
return (1);
} /* dbd_db_do */
int dbd_db_disconnect (SV *dbh, imp_dbh_t *imp_dbh) {
dTHX;
dbg (3, "DBD::Unify::db_disconnect\n");
unless (DBIc_ACTIVE (imp_dbh))
return (0);
change_offspring (dbh, imp_dbh);
if (imp_dbh->nchildren) {
if (imp_dbh->children) free ((void *)imp_dbh->children);
imp_dbh->children = (imp_sth_t **)0;
imp_dbh->nchildren = 0;
}
if (DBIc_ACTIVE_KIDS (imp_dbh) && DBIc_WARN (imp_dbh) && !PL_dirty) {
warn ("DBD::Unify::db_disconnect (%s) invalidates %d active cursor(s)",
SvPV_nolen (dbh), (int)DBIc_ACTIVE_KIDS (imp_dbh));
}
DBIc_ACTIVE_off (imp_dbh);
EXEC SQL
DISCONNECT;
dbg (4, " After disconn, sqlcode = %d\n", SQLCODE);
imp_dbh->id = 0;
/* We assume that disconnect will always work
* since most errors imply already disconnected.
*/
return (sqlError (dbh));
} /* dbd_db_disconnect */
int dbd_discon_all (SV *drh, imp_drh_t *imp_drh) {
dTHX;
if (!PL_dirty && !SvTRUE (perl_get_sv ("DBI::PERL_ENDING", 0))) {
sv_setiv (DBIc_ERR (imp_drh), (IV)1);
sv_setpv (DBIc_ERRSTR (imp_drh), "disconnect_all not implemented");
(void)DBIh_EVENT2 (drh, ERROR_event, DBIc_ERR (imp_drh), DBIc_ERRSTR (imp_drh));
return (FALSE);
}
if (PL_perl_destruct_level)
PL_perl_destruct_level = 0;
return (FALSE);
} /* dbd_discon_all */
void dbd_db_destroy (SV *dbh, imp_dbh_t *imp_dbh) {
dTHX;
dbg (3, "DBD::Unify::db_destroy\n");
if (DBIc_ACTIVE (imp_dbh))
dbd_db_disconnect (dbh, imp_dbh);
DBIc_IMPSET_off (imp_dbh);
/* No, share it among all DB handles
(void)free (sth_id_on);
*/
} /* dbd_db_destroy */
int dbd_db_STORE_attrib (SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv) {
dTHX;
STRLEN kl;
char *key = SvPV (keysv, kl);
unless (DBIc_ACTIVE (imp_dbh))
return (0);
if (kl == 11 && (strEQ (key, "uni_verbose") || strEQ (key, "dbd_verbose"))) {
dbd_verbose = SvIV (valuesv); /* dbd_verbose in DBD::Oracle since 1.22 :) */
dbg (2, "Set DBD_VERBOSE = %d\n", dbd_verbose);
return (TRUE);
}
if (kl == 10 && strEQ (key, "AutoCommit")) {
DBIc_set (imp_dbh, DBIcf_AutoCommit, 0); /* Allways off */
return (TRUE);
}
if (kl == 11 && strEQ (key, "uni_unicode")) {
imp_dbh->unicode = SvOK (valuesv) && SvTRUE (valuesv) ? 1 : 0;
return (TRUE);
}
if ((kl == 13 && strEQ (key, "uni_scanlevel")) ||
(kl == 9 && strEQ (key, "ScanLevel"))) {
auto int val = SvIV (valuesv);
dbg (3, "DBD::Unify::dbd_db_STORE (ScanLevel = %d)\n", val);
if (val < 1 || val > 16)
return (FALSE);
(void)sprintf (u_sql_do, "set transaction scan level %d", val);
EXEC SQL
EXECUTE IMMEDIATE :u_sql_do;
dbg (4, " After SCANLVL, sqlcode = %d\n", SQLCODE);
unless (sqlError (dbh))
return (FALSE);
return (TRUE);
}
return (FALSE);
} /* dbd_db_STORE_attrib */
SV *dbd_db_FETCH_attrib (SV *dbh, imp_dbh_t *imp_dbh, SV *keysv) {
dTHX;
STRLEN kl;
char *key = SvPV (keysv, kl);
unless (DBIc_ACTIVE (imp_dbh))
return (NULL);
if (kl == 11 && (strEQ (key, "uni_verbose") || strEQ (key, "dbd_verbose")))
return (newSViv (dbd_verbose));
if (kl == 11 && strEQ (key, "uni_unicode"))