-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterBase.xs
1397 lines (1248 loc) · 42.9 KB
/
InterBase.xs
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
/*
$Id: InterBase.xs 394 2008-01-08 05:29:19Z edpratomo $
Copyright (c) 1999-2008 Edwin Pratomo
Portions Copyright (c) 2001-2005 Daniel Ritz
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file,
with the exception that it cannot be placed on a CD-ROM or similar media
for commercial distribution without the prior approval of the author.
*/
/* vim: set noai ts=4 et sw=4: */
#include "InterBase.h"
DBISTATE_DECLARE;
static int _cancel_callback(SV *dbh, IB_EVENT *ev)
{
ISC_STATUS status[ISC_STATUS_LENGTH];
D_imp_dbh(dbh);
int ret = 0;
if (ev->exec_cb)
croak("Can't be called from inside a callback");
if (ev->perl_cb) {
ev->state = INACTIVE;
SvREFCNT_dec(ev->perl_cb);
ev->perl_cb = (SV*)NULL;
isc_cancel_events(status, &(imp_dbh->db), &(ev->id));
if (ib_error_check(dbh, status))
ret = 0;
else
ret = 1;
} else
croak("No callback found for this event handle. Have you called ib_register_callback?");
return ret;
}
static int _call_perlsub(IB_EVENT ISC_FAR *ev, short length,
#if defined(INCLUDE_TYPES_PUB_H)
const ISC_UCHAR *updated
#else
char ISC_FAR *updated
#endif
)
{
int retval = 1;
#if defined(USE_THREADS) || defined(USE_ITHREADS) || defined(MULTIPLICITY)
/* save context, set context from dbh */
void *context = PERL_GET_CONTEXT;
PERL_SET_CONTEXT(ev->dbh->context);
{
#else
void *context = PERL_GET_CONTEXT;
PerlInterpreter *cb_perl = perl_alloc();
PERL_SET_CONTEXT(cb_perl);
{
#endif
dSP;
int i, count;
SV **svp;
HV *posted_events = newHV();
ISC_ULONG ecount[15];
#if defined(INCLUDE_TYPES_PUB_H)
ISC_UCHAR *result = ev->result_buffer;
#else
char ISC_FAR *result = ev->result_buffer;
#endif
while (length--)
*result++ = *updated++;
isc_event_counts(ecount, ev->epb_length, ev->event_buffer,
ev->result_buffer);
for (i = 0; i < ev->num; i++)
{
if (ecount[i])
{
svp = hv_store(posted_events, *(ev->names + i), strlen(*(ev->names + i)),
newSViv(ecount[i]), 0);
if (svp == NULL)
croak("Bad: key '%s' not stored", *(ev->names + i));
}
}
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newRV_noinc((SV*)posted_events)));
PUTBACK;
count = perl_call_sv(ev->perl_cb, G_SCALAR);
SPAGAIN;
if (count > 0)
retval = POPi;
PUTBACK;
FREETMPS;
LEAVE;
#if defined(USE_THREADS) || defined(USE_ITHREADS) || defined(MULTIPLICITY)
}
/* restore old context*/
PERL_SET_CONTEXT(context);
#else
}
PERL_SET_CONTEXT(context);
perl_free(cb_perl);
#endif
return retval;
}
/* callback function for events, called by InterBase */
/* static isc_callback _async_callback(IB_EVENT ISC_FAR *ev, short length, char ISC_FAR *updated) */
static ISC_EVENT_CALLBACK _async_callback(IB_EVENT ISC_FAR *ev,
#if defined(INCLUDE_TYPES_PUB_H)
ISC_USHORT length, const ISC_UCHAR *updated
#else
short length, char ISC_FAR *updated
#endif
)
{
ISC_STATUS status[ISC_STATUS_LENGTH];
switch (ev->state) {
case INACTIVE:
break;
case ACTIVE:
ev->exec_cb = 1;
if (_call_perlsub(ev, length, updated) == 0) {
ev->state = INACTIVE;
ev->exec_cb = 0;
break;
}
ev->exec_cb = 0;
isc_que_events(
status,
&(ev->dbh->db),
&(ev->id),
ev->epb_length,
ev->event_buffer,
(ISC_EVENT_CALLBACK)_async_callback,
ev
);
}
return (0);
}
MODULE = DBD::InterBase PACKAGE = DBD::InterBase
INCLUDE: InterBase.xsi
MODULE = DBD::InterBase PACKAGE = DBD::InterBase::db
void
_do(dbh, statement, attr=Nullsv)
SV * dbh
SV * statement
SV * attr
PROTOTYPE: $$;$@
CODE:
{
D_imp_dbh(dbh);
ISC_STATUS status[ISC_STATUS_LENGTH]; /* isc api status vector */
STRLEN slen;
int retval;
char *sbuf = SvPV(statement, slen);
DBI_TRACE_imp_xxh(imp_dbh, 1, (DBIc_LOGPIO(imp_dbh), "db::_do\n" "Executing : %s\n", sbuf));
/* we need an open transaction */
if (!imp_dbh->tr)
{
DBI_TRACE_imp_xxh(imp_dbh, 1, (DBIc_LOGPIO(imp_dbh), "starting new transaction..\n"));
if (!ib_start_transaction(dbh, imp_dbh))
{
retval = -2;
XST_mUNDEF(0); /* <= -2 means error */
return;
}
DBI_TRACE_imp_xxh(imp_dbh, 1, (DBIc_LOGPIO(imp_dbh), "new transaction started.\n"));
}
/* we need to count the DDL statement whether in soft / hard commit */
#if 0
/* only execute_immediate statment if NOT in soft commit mode */
if (!(imp_dbh->soft_commit))
{
isc_dsql_execute_immediate(status, &(imp_dbh->db), &(imp_dbh->tr), 0,
sbuf, imp_dbh->sqldialect, NULL);
if (ib_error_check(dbh, status))
retval = -2;
else
retval = -1 ;
}
else
#endif
/* count DDL statements is necessary for ib_commit_transaction to work properly */
{
isc_stmt_handle stmt = 0L; /* temp statment handle */
static char stmt_info[] = { isc_info_sql_stmt_type };
char info_buffer[20]; /* statment info buffer */
retval = -2;
do
{
/* init statement handle */
if (isc_dsql_alloc_statement2(status, &(imp_dbh->db), &stmt))
break;
/* prepare statement */
isc_dsql_prepare(status, &(imp_dbh->tr), &stmt, 0, sbuf,
imp_dbh->sqldialect, NULL);
if (ib_error_check(dbh, status))
break;
/* get statement type */
if (!isc_dsql_sql_info(status, &stmt, sizeof(stmt_info), stmt_info,
sizeof(info_buffer), info_buffer))
{
/* need to count DDL statments */
short l = (short) isc_vax_integer((char *) info_buffer + 1, 2);
if (isc_vax_integer((char *) info_buffer + 3, l) == isc_info_sql_stmt_ddl)
imp_dbh->sth_ddl++;
}
else
break;
/* exec the statement */
isc_dsql_execute(status, &(imp_dbh->tr), &stmt, imp_dbh->sqldialect, NULL);
if (!ib_error_check(dbh, status))
retval = -1;
} while (0);
/* close statement */
if (stmt)
isc_dsql_free_statement(status, &stmt, DSQL_drop);
if (retval != -2) retval = -1;
}
/* for AutoCommit: commit */
if (DBIc_has(imp_dbh, DBIcf_AutoCommit))
{
if (!ib_commit_transaction(dbh, imp_dbh))
retval = -2;
}
if (retval < -1)
XST_mUNDEF(0);
else
XST_mIV(0, retval); /* typically 1, rowcount or -1 */
}
void
_ping(dbh)
SV * dbh
CODE:
{
int ret;
ret = dbd_db_ping(dbh);
if (ret == 0)
XST_mUNDEF(0);
else
XST_mIV(0, ret);
}
#define TX_INFOBUF(name, len) \
if (strEQ(item, #name)) { \
*p++ = (char) isc_info_tra_##name; \
res_len += len + 3; \
item_buf_len++; \
continue; \
}
#define TX_RESBUF_CASE(name) \
case isc_info_tra_##name:\
{\
keyname = #name;\
/* PerlIO_printf(PerlIO_stderr(), "Got %s\n", keyname); */\
p++;\
length = isc_vax_integer (p, 2);\
p += 2;\
(void)hv_store(RETVAL, keyname, strlen(keyname), \
newSViv(isc_vax_integer(p, (short) length)), 0);\
p += length;\
break;\
}
HV*
ib_tx_info(dbh)
SV* dbh
PREINIT:
char* p;
char* result = NULL;
short result_length = 0;
ISC_STATUS status[ISC_STATUS_LENGTH];
CODE:
{
D_imp_dbh(dbh);
char request[] = {
isc_info_tra_id,
#if defined(FB_API_VER) && FB_API_VER >= 20
/* FB 2.0: */
isc_info_tra_oldest_interesting,
isc_info_tra_oldest_active,
isc_info_tra_oldest_snapshot,
isc_info_tra_lock_timeout,
isc_info_tra_isolation,
isc_info_tra_access,
#endif
isc_info_end
};
RETVAL = newHV();
if (!RETVAL) {
if (result) {
Safefree(result);
}
do_error(dbh, 2, "unable to allocate hash return value");
XSRETURN_UNDEF;
}
if (!imp_dbh->tr) {
do_error(dbh, 2, "No active transaction");
XSRETURN_UNDEF;
}
/* calc required result buffer size */
for (p = request; *p != isc_info_end; p++) {
result_length++; /* identifier (1 byte)*/
switch (*p) {
#if defined(FB_API_VER) && FB_API_VER >= 20
case isc_info_tra_isolation:
/* result:
length (2 bytes) + first content (1 byte) +
length (2 bytes) + second content (2 bytes max)
*/
result_length += 7;
break;
case isc_info_tra_access:
/* result:
length (2 bytes) + content (1 byte)
*/
result_length += 3;
break;
#endif
default:
result_length += 2; /* length (2 bytes) */
result_length += 4; /* pessimistic */
}
}
result_length += 1; /* add 1 byte for isc_info_end */
/* try insufficient result_length:
result_length = 40;
*/
try_alloc_result_buffer:
Newxz(result, result_length, char);
/* PerlIO_printf(PerlIO_stderr(), "result_length: %d\n", result_length); */
/* call */
isc_transaction_info(status, &(imp_dbh->tr),
sizeof(request), request,
result_length, result);
if (ib_error_check(dbh, status)) {
XSRETURN_UNDEF;
} else {
/* detect truncation */
for (p = result + result_length - 1; p > result; p--) {
if (*p != 0) {
break;
}
}
if (p > result) {
/* PerlIO_printf(PerlIO_stderr(), "First non-null byte found at: %d\n", (p - result)); */
if (*p == isc_info_truncated) {
/* PerlIO_printf(PerlIO_stderr(), "Truncation detected.\n"); */
/* increase result_length, retry allocation */
result_length += 10;
Safefree(result);
result = NULL;
goto try_alloc_result_buffer;
}
}
/* parse result */
for (p = result; p < result + result_length; ) {
char *keyname;
short length;
if (*p == isc_info_end) {
/* PerlIO_printf(PerlIO_stderr(), "isc_info_end encountered at byte: %d\n", (p - result)); */
break;
}
switch (*p) {
TX_RESBUF_CASE(id)
#if defined(FB_API_VER) && FB_API_VER >= 20
TX_RESBUF_CASE(oldest_interesting)
TX_RESBUF_CASE(oldest_active)
TX_RESBUF_CASE(oldest_snapshot)
TX_RESBUF_CASE(lock_timeout)
case isc_info_tra_isolation:
{
keyname = "isolation";
HV* reshv;
/* PerlIO_printf(PerlIO_stderr(), "Got 'isolation' at byte: %d\n", (p - result)); */
++p;
short length = isc_vax_integer(p, 2);
p += 2;
/* PerlIO_printf(PerlIO_stderr(), "Content length: %d\n", length); */
if (*p == isc_info_tra_consistency) {
(void)hv_store(RETVAL, keyname, strlen(keyname), newSVpv("consistency", 0), 0);
} else if (*p == isc_info_tra_concurrency) {
(void)hv_store(RETVAL, keyname, strlen(keyname), newSVpv("snapshot (concurrency)", 0), 0);
} else if (*p == isc_info_tra_read_committed) {
/* warn("got 'read_committed'"); */
reshv = newHV();
if (!reshv) {
if (result) {
Safefree(result);
}
do_error(dbh, 2, "unable to allocate hash for read_committed rec/no_rec version");
XSRETURN_UNDEF;
}
if (*(p + 1) == isc_info_tra_no_rec_version) {
(void)hv_store(reshv, "read_committed", 14, newSVpv("no_rec_version", 0), 0);
} else if (*(p + 1) == isc_info_tra_rec_version) {
(void)hv_store(reshv, "read_committed", 14, newSVpv("rec_version", 0), 0);
} else {
warn("unrecognized byte");
continue;
}
(void)hv_store(RETVAL, keyname, strlen(keyname),
newRV_noinc((SV*) reshv), 0);
} else {
PerlIO_printf(PerlIO_stderr(), "+2: got unrecognized byte: %d\n", *((char*)p));
}
p += length;
break;
}
case isc_info_tra_access: {
keyname = "access";
/* PerlIO_printf(PerlIO_stderr(), "Got 'access' at byte: %d\n", (p - result)); */
p++;
short length = isc_vax_integer(p, 2);
p += 2;
if (*p == isc_info_tra_readonly) {
(void)hv_store(RETVAL, keyname, strlen(keyname), newSVpvn("readonly", 8), 0);
} else if (*p == isc_info_tra_readwrite) {
(void)hv_store(RETVAL, keyname, strlen(keyname), newSVpvn("readwrite", 9), 0);
}
p += length;
break;
}
#endif
default:
/* PerlIO_printf(PerlIO_stderr(), "now at byte: %d\n", (p - result)); */
p++;
}
}
}
}
OUTPUT:
RETVAL
CLEANUP:
SvREFCNT_dec(RETVAL);
#undef TX_INFOBUF
#undef TX_RESBUF_CASE
int
ib_set_tx_param(dbh, ...)
SV *dbh
ALIAS:
set_tx_param = 1
PREINIT:
STRLEN len;
char *tx_key, *tx_val, *tpb, *tmp_tpb;
int i, rc = 0;
int tpb_len;
char am_set = 0, il_set = 0, ls_set = 0;
I32 j;
AV *av;
HV *hv;
SV *sv, *sv_value;
HE *he;
CODE:
{
D_imp_dbh(dbh);
#ifdef PERL_UNUSED_VAR
PERL_UNUSED_VAR(ix); /* -Wall */
#endif
/* if no params or first parameter = 0 or undef -> reset TPB to NULL */
if (items < 3)
{
if ((items == 1) || !(SvTRUE(ST(1))))
{
tpb = NULL;
tmp_tpb = NULL;
tpb_len = 0;
goto do_set_tpb;
}
}
/* we need to know the max. size of TBP, (buffer overflow problem) */
/* mem usage: -access_mode: max. 1 byte */
/* -isolation_level: max. 2 bytes */
/* -lock_resolution: max. 1 byte */
/* -reserving: max. 4 bytes + strlen(tablename) */
tpb_len = 5; /* 4 + 1 for tpb_version */
/* we need to add the length of each table name + 4 bytes */
for (i = 1; i < items-1; i += 2)
{
sv_value = ST(i + 1);
if (strEQ(SvPV_nolen(ST(i)), "-reserving"))
if (SvROK(sv_value) && SvTYPE(SvRV(sv_value)) == SVt_PVHV)
{
hv = (HV *)SvRV(sv_value);
hv_iterinit(hv);
while ((he = hv_iternext(hv)))
{
/* retrieve the size of table name(s) */
HePV(he, len);
tpb_len += len + 4;
}
}
}
/* alloc it */
Newx(tmp_tpb, tpb_len, char);
/* do set TPB values */
tpb = tmp_tpb;
*tpb++ = isc_tpb_version3;
for (i = 1; i < items; i += 2)
{
tx_key = SvPV_nolen(ST(i));
sv_value = ST(i + 1);
/* value specified? */
if (i >= items - 1)
{
Safefree(tmp_tpb);
croak("You must specify parameter => value pairs, but theres no value for %s", tx_key);
}
/**********************************************************************/
if (strEQ(tx_key, "-access_mode"))
{
if (am_set)
{
warn("-access_mode already set; ignoring second try!");
continue;
}
tx_val = SvPV_nolen(sv_value);
if (strEQ(tx_val, "read_write"))
*tpb++ = isc_tpb_write;
else if (strEQ(tx_val, "read_only"))
*tpb++ = isc_tpb_read;
else
{
Safefree(tmp_tpb);
croak("Unknown -access_mode value %s", tx_val);
}
am_set = 1; /* flag */
}
/**********************************************************************/
else if (strEQ(tx_key, "-isolation_level"))
{
if (il_set)
{
warn("-isolation_level already set; ignoring second try!");
continue;
}
if (SvROK(sv_value) && SvTYPE(SvRV(sv_value)) == SVt_PVAV)
{
av = (AV *)SvRV(sv_value);
/* sanity check */
for (j = 0; (j <= av_len(av)) && !rc; j++)
{
sv = *av_fetch(av, j, FALSE);
if (strEQ(SvPV_nolen(sv), "read_committed"))
{
rc = 1;
*tpb++ = isc_tpb_read_committed;
}
}
if (!rc)
{
Safefree(tmp_tpb);
croak("Invalid -isolation_level value");
}
for (j = 0; j <= av_len(av); j++)
{
tx_val = SvPV_nolen(*(av_fetch(av, j, FALSE)));
if (strEQ(tx_val, "record_version"))
{
*tpb++ = isc_tpb_rec_version;
break;
}
else if (strEQ(tx_val, "no_record_version"))
{
*tpb++ = isc_tpb_no_rec_version;
break;
}
else if (!strEQ(tx_val, "read_committed"))
{
Safefree(tmp_tpb);
croak("Unknown -isolation_level value %s", tx_val);
}
}
}
else
{
tx_val = SvPV_nolen(sv_value);
if (strEQ(tx_val, "read_committed"))
*tpb++ = isc_tpb_read_committed;
else if (strEQ(tx_val, "snapshot"))
*tpb++ = isc_tpb_concurrency;
else if (strEQ(tx_val, "snapshot_table_stability"))
*tpb++ = isc_tpb_consistency;
else
{
Safefree(tmp_tpb);
croak("Unknown -isolation_level value %s", tx_val);
}
}
il_set = 1; /* flag */
}
/**********************************************************************/
else if (strEQ(tx_key, "-lock_resolution"))
{
if (ls_set)
{
warn("-lock_resolution already set; ignoring second try!");
continue;
}
if (SvROK(sv_value) && SvTYPE(SvRV(sv_value)) == SVt_PVHV) {
#if defined(FB_API_VER) && FB_API_VER >= 20
hv = (HV *)SvRV(sv_value);
if (hv_exists(hv, "wait", 4)) {
*tpb++ = isc_tpb_wait;
sv = *hv_fetch(hv, "wait", 4, FALSE);
if (SvIOK(sv)) {
IV lock_timeout = SvIV(sv);
if (lock_timeout < 0) {
do_error(dbh, 2, "Wait timeout value must be positive integer");
XSRETURN_UNDEF;
} else if (lock_timeout > 0) {
*tpb++ = isc_tpb_lock_timeout;
*tpb++ = sizeof(ISC_LONG); /* length = 4 bytes */
*(ISC_LONG*)tpb = lock_timeout; /* infinite timeout */
tpb += sizeof(ISC_LONG);
}
} else {
do_error(dbh, 2, "Wait timeout value must be positive integer");
XSRETURN_UNDEF;
}
} else {
do_error(dbh, 2, "The only valid key is 'wait'");
XSRETURN_UNDEF;
}
#else
do_error(dbh, 2, "Hashref unsupported. Must be compiled with Firebird 2.0 client library");
XSRETURN_UNDEF;
#endif
} else {
tx_val = SvPV_nolen(sv_value);
if (strEQ(tx_val, "wait"))
*tpb++ = isc_tpb_wait;
else if (strEQ(tx_val, "no_wait"))
*tpb++ = isc_tpb_nowait;
else
{
Safefree(tmp_tpb);
croak("Unknown transaction parameter %s", tx_val);
}
}
ls_set = 1; /* flag */
}
/**********************************************************************/
else if (strEQ(tx_key, "-reserving"))
{
if (SvROK(sv_value) && SvTYPE(SvRV(sv_value)) == SVt_PVHV)
{
char *table_name;
HV *table_opts;
hv = (HV *)SvRV(sv_value);
hv_iterinit(hv);
while ((he = hv_iternext(hv)))
{
/* check val type */
if (SvROK(HeVAL(he)) && SvTYPE(SvRV(HeVAL(he))) == SVt_PVHV)
{
table_opts = (HV*)SvRV(HeVAL(he));
if (hv_exists(table_opts, "access", 6))
{
/* access is optional */
sv = *hv_fetch(table_opts, "access", 6, FALSE);
if (strnEQ(SvPV_nolen(sv), "shared", 6))
*tpb++ = isc_tpb_shared;
else if (strnEQ(SvPV_nolen(sv), "protected", 9))
*tpb++ = isc_tpb_protected;
else
{
Safefree(tmp_tpb);
croak("Invalid -reserving access value");
}
}
if (hv_exists(table_opts, "lock", 4))
{
/* lock is required */
sv = *hv_fetch(table_opts, "lock", 4, FALSE);
if (strnEQ(SvPV_nolen(sv), "read", 4))
*tpb++ = isc_tpb_lock_read;
else if (strnEQ(SvPV_nolen(sv), "write", 5))
*tpb++ = isc_tpb_lock_write;
else
{
Safefree(tmp_tpb);
croak("Invalid -reserving lock value");
}
}
else /* lock */
{
Safefree(tmp_tpb);
croak("Lock value is required in -reserving");
}
/* add the table name to TPB */
table_name = HePV(he, len);
*tpb++ = len + 1;
{
unsigned int k;
for (k = 0; k < len; k++)
*tpb++ = toupper(*table_name++);
}
*tpb++ = 0;
} /* end hashref check*/
else
{
Safefree(tmp_tpb);
croak("Reservation for a given table must be hashref.");
}
} /* end of while() */
}
else
{
Safefree(tmp_tpb);
croak("Invalid -reserving value. Must be hashref.");
}
} /* end table reservation */
else
{
Safefree(tmp_tpb);
croak("Unknown transaction parameter %s", tx_key);
}
}
/* an ugly label... */
do_set_tpb:
Safefree(imp_dbh->tpb_buffer);
imp_dbh->tpb_buffer = tmp_tpb;
imp_dbh->tpb_length = tpb - imp_dbh->tpb_buffer;
/* for AutoCommit: commit current transaction */
if (DBIc_has(imp_dbh, DBIcf_AutoCommit))
{
imp_dbh->sth_ddl++;
ib_commit_transaction(dbh, imp_dbh);
}
RETVAL = 1;
}
OUTPUT:
RETVAL
#*******************************************************************************
# only for use within database_info!
#define DB_INFOBUF(name, len) \
if (strEQ(item, #name)) { \
*p++ = (char) isc_info_##name; \
res_len += len + 3; \
item_buf_len++; \
continue; \
}
#define DB_RESBUF_CASEHDR(name) \
case isc_info_##name:\
keyname = #name;
HV *
ib_database_info(dbh, ...)
SV *dbh
PREINIT:
unsigned int i, count;
char item_buf[30], *p, *old_p;
char *res_buf;
short item_buf_len, res_len;
AV *av;
ISC_STATUS status[ISC_STATUS_LENGTH];
CODE:
{
D_imp_dbh(dbh);
/* process input params, count max. result buffer length */
p = item_buf;
res_len = 0;
item_buf_len = 0;
/* array or array ref? */
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV)
{
av = (AV *)SvRV(ST(1));
count = av_len(av) + 1;
}
else
{
av = NULL;
count = items;
}
/* loop thru all elements */
for (i = 0; i < count; i++)
{
char *item;
/* fetch from array or array ref? */
if (av)
item = SvPV_nolen(*av_fetch(av, i, FALSE));
else
item = SvPV_nolen(ST(i + 1));
/* database characteristics */
DB_INFOBUF(allocation, 4);
DB_INFOBUF(base_level, 2);
DB_INFOBUF(db_id, 513);
DB_INFOBUF(implementation, 3);
DB_INFOBUF(no_reserve, 1);
#ifdef IB_API_V6
DB_INFOBUF(db_read_only, 1);
#endif
DB_INFOBUF(ods_minor_version, 1);
DB_INFOBUF(ods_version, 1);
DB_INFOBUF(page_size, 4);
DB_INFOBUF(version, 257);
#ifdef IB_API_V6
DB_INFOBUF(db_sql_dialect, 1);
#endif
/* environmental characteristics */
DB_INFOBUF(current_memory, 4);
DB_INFOBUF(forced_writes, 1);
DB_INFOBUF(max_memory, 4);
DB_INFOBUF(num_buffers, 4);
DB_INFOBUF(sweep_interval, 4);
DB_INFOBUF(user_names, 1024); /* can be more, can be less */
/* performance statistics */
DB_INFOBUF(fetches, 4);
DB_INFOBUF(marks, 4);
DB_INFOBUF(reads, 4);
DB_INFOBUF(writes, 4);
#if defined(FB_API_VER) && FB_API_VER >= 20
/* FB 2.0 */
DB_INFOBUF(active_tran_count, 4);
DB_INFOBUF(creation_date, sizeof(ISC_TIMESTAMP)); /* 2 x 4 bytes */
#endif
/* database operation counts */
/* XXX - not implemented (complicated: returns a descriptor for _each_
table...how to fetch / store this??) but do we really need these? */
}
/* the end marker */
*p++ = isc_info_end;
item_buf_len++;
/* allocate the result buffer */
res_len += 256; /* add some safety...just in case */
Newx(res_buf, res_len, char);
/* call the function */
isc_database_info(status, &(imp_dbh->db), item_buf_len, item_buf,
res_len, res_buf);
if (ib_error_check(dbh, status))
{
Safefree(res_buf);
XSRETURN_UNDEF; // croak("isc_database_info failed!");
}
/* fill hash with key/value pairs */
RETVAL = newHV();
for (p = res_buf; *p != isc_info_end; )
{
char *keyname;
char item = *p++;
int length = isc_vax_integer (p, 2);
p += 2;
old_p = p;
switch (item)
{
/******************************************************************/
/* database characteristics */
DB_RESBUF_CASEHDR(allocation)
(void)hv_store(RETVAL, keyname, strlen(keyname),
newSViv(isc_vax_integer(p, (short) length)), 0);
break;
DB_RESBUF_CASEHDR(base_level)
(void)hv_store(RETVAL, keyname, strlen(keyname),
newSViv(isc_vax_integer(++p, 1)), 0);
break;
DB_RESBUF_CASEHDR(db_id)
{
HV *reshv = newHV();
ISC_LONG slen;
(void)hv_store(reshv, "connection", 10,
(isc_vax_integer(p++, 1) == 2)?
newSVpv("local", 0):
newSVpv("remote", 0),
0);
slen = isc_vax_integer(p++, 1);
(void)hv_store(reshv, "database", 8, newSVpvn(p, slen), 0);
p += slen;
slen = isc_vax_integer(p++, 1);
(void)hv_store(reshv, "site", 8, newSVpvn(p, slen), 0);
(void)hv_store(RETVAL, keyname, strlen(keyname),
newRV_noinc((SV *) reshv), 0);
break;
}
DB_RESBUF_CASEHDR(implementation)
{
HV *reshv = newHV();
(void)hv_store(reshv, "implementation", 14,
newSViv(isc_vax_integer(++p, 1)), 0);
(void)hv_store(reshv, "class", 5,
newSViv(isc_vax_integer(++p, 1)), 0);
(void)hv_store(RETVAL, keyname, strlen(keyname),
newRV_noinc((SV *) reshv), 0);
break;
}
DB_RESBUF_CASEHDR(no_reserve)
(void)hv_store(RETVAL, keyname, strlen(keyname),
newSViv(isc_vax_integer(p, (short) length)), 0);
break;
#ifdef IB_API_V6
DB_RESBUF_CASEHDR(db_read_only)
(void)hv_store(RETVAL, keyname, strlen(keyname),
newSViv(isc_vax_integer(p, (short) length)), 0);
break;
#endif
DB_RESBUF_CASEHDR(ods_minor_version)
(void)hv_store(RETVAL, keyname, strlen(keyname),
newSViv(isc_vax_integer(p, (short) length)), 0);
break;
DB_RESBUF_CASEHDR(ods_version)
(void)hv_store(RETVAL, keyname, strlen(keyname),
newSViv(isc_vax_integer(p, (short) length)), 0);
break;
DB_RESBUF_CASEHDR(page_size)