-
Notifications
You must be signed in to change notification settings - Fork 5
/
ARS.xs
8265 lines (7545 loc) · 251 KB
/
ARS.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
/*
$Header: /cvsroot/arsperl/ARSperl/ARS.xs,v 1.127 2011/07/29 13:05:27 tstapff Exp $
ARSperl - An ARS v2 - v5 / Perl5 Integration Kit
Copyright (C) 1995-2003
Joel Murphy, jmurphy@acsu.buffalo.edu
Jeff Murphy, jcmurphy@acsu.buffalo.edu
This program is free software; you can redistribute it and/or modify
it under the terms as Perl itself.
Refer to the file called "Artistic" that accompanies the source
distribution of ARSperl (or the one that accompanies the source
distribution of Perl itself) for a full description.
Comments to: arsperl@arsperl.org
(this is a *mailing list* and you must be
a subscriber before posting)
http://www.arsperl.org
*/
#include "support.h"
#include "supportrev.h"
#include "supportrev_generated.h"
#if AR_EXPORT_VERSION < 3
#define AR_LIST_SCHEMA_ALL 1
#endif
#if defined(ARSPERL_UNDEF_MALLOC) && defined(malloc)
#undef malloc
#undef calloc
#undef realloc
#undef free
#endif
MODULE = ARS PACKAGE = ARS PREFIX = ARS
PROTOTYPES: ENABLE
int
isa_int(...)
CODE:
{
if (items != 1)
croak("usage: isa_int(value)");
RETVAL = SvIOKp(ST(0));
}
OUTPUT:
RETVAL
int
isa_float(...)
CODE:
{
if (items != 1)
croak("usage: isa_int(value)");
RETVAL = SvNOKp(ST(0));
}
OUTPUT:
RETVAL
int
isa_string(...)
CODE:
{
if (items != 1)
croak("usage: isa_int(value)");
RETVAL = SvPOKp(ST(0));
}
OUTPUT:
RETVAL
HV *
ars_perl_qualifier(ctrl, in)
ARControlStruct * ctrl
ARQualifierStruct * in
CODE:
{
RETVAL = perl_qualifier(ctrl, in);
}
OUTPUT:
RETVAL
ARQualifierStruct *
ars_qualifier_ptr(ctrl, in)
ARControlStruct * ctrl
SV * in
CODE:
{
ARQualifierStruct *qual;
HV *h_dummy;
HV *h;
int rv = 0;
AMALLOCNN(qual, 1, ARQualifierStruct);
(void) ARError_reset();
if( SvTYPE(SvRV(in)) != SVt_PVHV ){
ARError_add( AR_RETURN_ERROR, AP_ERR_GENERAL, "rev_ARQualifierStruct: not a hash value" );
RETVAL = NULL;
goto ars_qualifier_ptr_end;
}
h = (HV* ) SvRV((SV*) in);
if( ! SvTRUE(hv_scalar(h)) ){
RETVAL = qual;
goto ars_qualifier_ptr_end;
}
h_dummy = newHV();
SvREFCNT_inc( in );
hv_store( h_dummy, "_", 1, in, 0 );
rv += rev_ARQualifierStruct( ctrl, h_dummy, "_", qual );
hv_undef( h_dummy );
if( rv == 0 ){
RETVAL = qual;
}else{
ARError_add( AR_RETURN_ERROR, AP_ERR_PREREVFAIL );
RETVAL = NULL;
FreeARQualifierStruct(qual, TRUE);
}
ars_qualifier_ptr_end:;
}
OUTPUT:
RETVAL
ARQualifierStruct *
ars_LoadQualifier(ctrl,schema,qualstring,displayTag=NULL)
ARControlStruct * ctrl
char * schema
char * qualstring
char * displayTag
CODE:
{
int ret = 0;
ARStatusList status;
ARQualifierStruct *qual;
AMALLOCNN(qual, 1, ARQualifierStruct);
Zero(&status, 1, ARStatusList);
(void) ARError_reset();
ret = ARLoadARQualifierStruct(ctrl, schema, displayTag, qualstring, qual, &status);
#ifdef PROFILE
((ars_ctrl *)ctrl)->queries++;
#endif
if (! ARError( ret, status)) {
RETVAL = qual;
} else {
RETVAL = NULL;
FreeARQualifierStruct(qual, TRUE);
}
}
OUTPUT:
RETVAL
void
__ars_Termination()
CODE:
{
#if AR_EXPORT_VERSION <= 3
int ret;
#endif
ARStatusList status;
Zero(&status, 1, ARStatusList);
(void) ARError_reset();
#if AR_EXPORT_VERSION <= 3
ret = ARTermination(&status);
if (ARError( ret, status)) {
warn("failed in ARTermination\n");
}
#else
(void) ARError_add(AR_RETURN_ERROR, AP_ERR_DEPRECATED, "__ars_Termination() is only available when compiled against ARS <= 3.2");
#endif
}
void
__ars_init()
CODE:
{
#if AR_EXPORT_VERSION <= 3
int ret;
#endif
ARStatusList status;
Zero(&status, 1, ARStatusList);
(void) ARError_reset();
#if AR_EXPORT_VERSION <= 3
ret = ARInitialization(&status);
if (ARError( ret, status)) {
croak("unable to initialize ARS module");
}
#else
(void) ARError_add(AR_RETURN_ERROR, AP_ERR_DEPRECATED, "__ars_init() is only available when compiled against ARS <= 3.2");
#endif
}
int
ars_APIVersion()
CODE:
{
RETVAL = AR_CURRENT_API_VERSION;
}
OUTPUT:
RETVAL
int
ars_SetServerPort(ctrl, name, port, progNum)
ARControlStruct * ctrl
char * name
int port
int progNum
CODE:
{
int ret = 0;
ARStatusList status;
RETVAL = 0;
Zero(&status, 1, ARStatusList);
(void) ARError_reset();
#if AR_EXPORT_VERSION >= 4
ret = ARSetServerPort(ctrl, name, port, progNum, &status);
if (! ARError(ret, status)) {
RETVAL = 1;
}
#else
(void) ARError_add( AR_RETURN_ERROR, AP_ERR_DEPRECATED,
"ars_SetServerPort() is only available in ARS >= 4.x");
#endif
}
OUTPUT:
RETVAL
ARControlStruct *
ars_Login(server, username, password, lang=NULL, authString=NULL, tcpport=0, rpcnumber=0, ...)
char * server
char * username
char * password
char * lang
char * authString
unsigned int tcpport
unsigned int rpcnumber
CODE:
{
int ret = 0, s_ok = 1;
int staticParams = 7;
ARStatusList status;
ARServerNameList serverList;
ARControlStruct *ctrl;
#ifdef PROFILE
struct timeval tv;
#endif
DBG( ("ars_Login(%s, %s, %s, %s, %s, %d, %d)\n",
SAFEPRT(server),
SAFEPRT(username),
SAFEPRT(password),
SAFEPRT(lang),
SAFEPRT(authString),
tcpport,
rpcnumber)
);
RETVAL = NULL;
Zero(&status, 1, ARStatusList);
Zero(&serverList, 1, ARServerNameList);
(void) ARError_reset();
#ifdef PROFILE
/* XXX
This is something of a hack... a safemalloc will always
complain about differing structures. However, it's
pretty deep into the code. Perhaps a static would be cleaner?
*/
ctrl = (ARControlStruct *)MALLOCNN(sizeof(ars_ctrl));
Zero(ctrl, 1, ars_ctrl);
((ars_ctrl *)ctrl)->queries = 0;
((ars_ctrl *)ctrl)->startTime = 0;
((ars_ctrl *)ctrl)->endTime = 0;
#else
DBG( ("safemalloc ARControlStruct\n") );
ctrl = (ARControlStruct *)safemalloc(sizeof(ARControlStruct));
/* DBG( ("malloc ARControlStruct\n") );
ctrl = (ARControlStruct *)MALLOCNN(sizeof(ARControlStruct)); */
Zero(ctrl, 1, ARControlStruct);
#endif
#ifdef PROFILE
if (gettimeofday(&tv, 0) != -1)
((ars_ctrl *)ctrl)->startTime = tv.tv_sec;
else
perror("gettimeofday");
#endif
ctrl->cacheId = 0;
#if AR_EXPORT_VERSION >= 4
ctrl->sessionId = 0;
#endif
ctrl->operationTime = 0;
strncpy(ctrl->user, username, sizeof(ctrl->user));
ctrl->user[sizeof(ctrl->user)-1] = 0;
strncpy(ctrl->password, password, sizeof(ctrl->password));
ctrl->password[sizeof(ctrl->password)-1] = 0;
#ifndef AR_MAX_LOCALE_SIZE
/* 6.0.1 and 6.3 are AR_EXPORT_VERSION = 8L but 6.3 does not
* contain the language field
*/
ctrl->language[0] = 0;
if ( CVLD(lang) ) {
strncpy(ctrl->language, lang, AR_MAX_LANG_SIZE);
}
#else
ctrl->localeInfo.locale[0] = 0;
if ( CVLD(lang) ) {
strncpy(ctrl->localeInfo.locale, lang, AR_MAX_LANG_SIZE);
}
if( items > staticParams ){
int i;
HV *h;
if( (items - staticParams) % 2 ){
(void) ARError_add( AR_RETURN_ERROR, AP_ERR_BAD_ARGS);
#ifdef PROFILE
AP_FREE(ctrl); /* invalid, cleanup */
#else
safefree(ctrl);
#endif
goto ar_login_end;
}
h = newHV();
for( i = staticParams; i < items; i+=2 ){
hv_store_ent( h, newSVsv(ST(i)), newSVsv(ST(i+1)), 0 );
}
if( hv_exists(h,"charSet",7) ){
strncpy( ctrl->localeInfo.charSet, SvPV_nolen( *(hv_fetch(h,"charSet",7,0)) ), AR_MAX_LANG_SIZE );
}
if( hv_exists(h,"timeZone",8) ){
strncpy( ctrl->localeInfo.timeZone, SvPV_nolen( *(hv_fetch(h,"timeZone",8,0)) ), AR_MAX_LOCALE_SIZE );
}
if( hv_exists(h,"customDateFormat",16) ){
strncpy( ctrl->localeInfo.customDateFormat, SvPV_nolen( *(hv_fetch(h,"customDateFormat",16,0)) ), AR_MAX_FORMAT_SIZE );
}
if( hv_exists(h,"customTimeFormat",16) ){
strncpy( ctrl->localeInfo.customTimeFormat, SvPV_nolen( *(hv_fetch(h,"customTimeFormat",16,0)) ), AR_MAX_FORMAT_SIZE );
}
if( hv_exists(h,"separators",10) ){
strncpy( ctrl->localeInfo.separators, SvPV_nolen( *(hv_fetch(h,"separators",10,0)) ), AR_MAX_LANG_SIZE );
}
hv_undef( h );
}
#endif
#if AR_EXPORT_VERSION >= 7L
ctrl->authString[0] = 0;
if ( CVLD(authString) ) {
strncpy(ctrl->authString, authString, AR_MAX_NAME_SIZE);
}
#endif
#if AR_EXPORT_VERSION >= 4
/* call ARInitialization */
ret = ARInitialization(ctrl, &status);
if(ARError(ret, status)) {
DBG( ("ARInitialization failed %d\n", ret) );
ARTermination(ctrl, &status);
ARError(ret, status);
#ifdef PROFILE
AP_FREE(ctrl);
#else
safefree(ctrl);
#endif
goto ar_login_end;
}
/*
printf( "ctrl->localeInfo.customDateFormat <%s>\n", ctrl->localeInfo.customDateFormat );
printf( "ctrl->localeInfo.separators <%s>\n", ctrl->localeInfo.separators );
*/
#endif
if (!server || !*server) {
DBG( ("no server given. picking one.\n") );
#if AR_EXPORT_VERSION >= 4
ret = ARGetListServer(ctrl, &serverList, &status);
#else
ret = ARGetListServer(&serverList, &status);
#endif
if (ARError( ret, status)) {
ARTermination(ctrl, &status);
ARError(ret, status);
#ifdef PROFILE
AP_FREE(ctrl); /* invalid, cleanup */
#else
safefree(ctrl);
#endif
DBG( ("ARGetListServer failed %d\n", ret) );
goto ar_login_end;
}
status.numItems = 0;
if (serverList.numItems == 0) {
DBG( ("serverList is empty.\n") );
(void) ARError_add( AR_RETURN_ERROR, AP_ERR_NO_SERVERS);
ARTermination(ctrl, &status);
ARError(ret, status);
#ifdef PROFILE
AP_FREE(ctrl); /* invalid, cleanup */
#else
safefree(ctrl);
#endif
goto ar_login_end;
}
server = serverList.nameList[0];
DBG( ("changing s_ok to 0, picked server %s\n",
SAFEPRT(server)) );
s_ok = 0;
}
strncpy(ctrl->server, server, sizeof(ctrl->server));
ctrl->server[sizeof(ctrl->server)-1] = 0;
/* set the tcp/rpc port if given */
ret = ARSetServerPort(ctrl, ctrl->server, tcpport, rpcnumber,
&status);
if (ARError(ret, status)) {
DBG( ("ARSetServerPort failed %d\n", ret) );
ARTermination(ctrl, &status);
ARError(ret, status);
#ifdef PROFILE
AP_FREE(ctrl);
#else
safefree(ctrl);
#endif
RETVAL = NULL;
goto ar_login_end;
}
/* finally, check to see if the user id is valid */
ret = ARVerifyUser(ctrl, NULL, NULL, NULL, &status);
if(ARError( ret, status)) {
DBG( ("ARVerifyUser failed %d\n", ret) );
ARTermination(ctrl, &status);
ARError(ret, status);
#ifdef PROFILE
AP_FREE(ctrl); /* invalid, cleanup */
#else
safefree(ctrl);
#endif
RETVAL = NULL;
} else {
RETVAL = ctrl; /* valid, return ctrl struct */
}
if(s_ok == 0) {
DBG( ("s_ok == 0, cleaning ServerNameList\n") );
FreeARServerNameList(&serverList, FALSE);
}
ar_login_end:;
DBG( ("finished.\n") );
}
OUTPUT:
RETVAL
HV*
ars_VerifyUser(ctrl)
ARControlStruct * ctrl
CODE:
{
int ret = 0;
ARBoolean adminFlag = 0,
subAdminFlag = 0,
customFlag = 0;
ARStatusList status;
(void) ARError_reset();
Zero(&status, 1, ARStatusList);
ret = ARVerifyUser( ctrl, &adminFlag, &subAdminFlag, &customFlag, &status );
/* printf( "ret = %d, adminFlag = %d, subAdminFlag = %d, customFlag = %d\n",
ret, adminFlag, subAdminFlag, customFlag ); */
if(! ARError(ret, status)) {
RETVAL = newHV();
sv_2mortal( (SV*) RETVAL );
hv_store( RETVAL, "adminFlag", strlen("adminFlag"), newSViv(adminFlag), 0);
hv_store( RETVAL, "subAdminFlag", strlen("subAdminFlag"), newSViv(subAdminFlag), 0);
hv_store( RETVAL, "customFlag", strlen("customFlag"), newSViv(customFlag), 0);
}else{
XSRETURN_UNDEF;
}
}
OUTPUT:
RETVAL
void
ars_GetControlStructFields(ctrl)
ARControlStruct * ctrl
PPCODE:
{
(void) ARError_reset();
if(!ctrl) return;
XPUSHs(sv_2mortal(newSViv(ctrl->cacheId)));
XPUSHs(sv_2mortal(newSViv(ctrl->operationTime)));
XPUSHs(sv_2mortal(newSVpv(ctrl->user, 0)));
XPUSHs(sv_2mortal(newSVpv(ctrl->password, 0)));
#ifndef AR_MAX_LOCALE_SIZE
XPUSHs(sv_2mortal(newSVpv(ctrl->language, 0)));
#else
XPUSHs(sv_2mortal(newSVpv(ctrl->localeInfo.locale, 0)));
#endif
XPUSHs(sv_2mortal(newSVpv(ctrl->server, 0)));
XPUSHs(sv_2mortal(newSViv(ctrl->sessionId)));
#if AR_EXPORT_VERSION >= 7
XPUSHs(sv_2mortal(newSVpv(ctrl->authString, 0)));
#endif
}
SV *
ars_GetCurrentServer(ctrl)
ARControlStruct * ctrl
CODE:
{
RETVAL = NULL;
(void) ARError_reset();
if(ctrl && ctrl->server) {
RETVAL = newSVpv( ctrl->server, strlen(ctrl->server) );
}
}
OUTPUT:
RETVAL
HV *
ars_GetProfileInfo(ctrl)
ARControlStruct * ctrl
CODE:
{
RETVAL = newHV();
sv_2mortal( (SV*) RETVAL );
(void) ARError_reset();
#ifdef PROFILE
hv_store(RETVAL, "queries", strlen("queries") ,
newSViv(((ars_ctrl *)ctrl)->queries), 0);
hv_store(RETVAL, "startTime", strlen("startTime") ,
newSViv(((ars_ctrl *)ctrl)->startTime), 0);
#else /* profiling not compiled in */
(void) ARError_add( AR_RETURN_ERROR, AP_ERR_OPT_NA,
"Optional profiling code not compiled into this build of ARSperl");
#endif
}
OUTPUT:
RETVAL
void
ars_Logoff(ctrl)
ARControlStruct * ctrl
CODE:
{
int ret = 0;
ARStatusList status;
Zero(&status, 1, ARStatusList);
(void) ARError_reset();
if (!ctrl) return;
#if AR_EXPORT_VERSION >= 4
ret = ARTermination(ctrl, &status);
#else
ret = ARTermination(&status);
#endif
(void) ARError( ret, status);
/*AP_FREE(ctrl); let DESTROY free it*/
}
void
ars_GetListField(control,schema,changedsince=0,fieldType=AR_FIELD_TYPE_ALL)
ARControlStruct * control
char * schema
unsigned long changedsince
unsigned long fieldType
PPCODE:
{
ARInternalIdList idlist;
ARStatusList status;
int ret = 0;
unsigned int i = 0;
(void) ARError_reset();
Zero(&idlist, 1, ARInternalIdList);
Zero(&status, 1, ARStatusList);
#if AR_EXPORT_VERSION >= 3
ret = ARGetListField(control,schema,
fieldType,
changedsince,
#if AR_CURRENT_API_VERSION >= 17
NULL, /* &objPropList (undocumented by BMC) */
#endif
&idlist,
&status);
#else
ret = ARGetListField(control,schema,changedsince,&idlist,&status);
#endif
#ifdef PROFILE
((ars_ctrl *)control)->queries++;
#endif
if (!ARError( ret,status)) {
for (i=0; i<idlist.numItems; i++)
XPUSHs(sv_2mortal(newSViv(idlist.internalIdList[i])));
FreeARInternalIdList(&idlist,FALSE);
}
}
void
ars_GetFieldByName(control,schema,field_name)
ARControlStruct * control
char * schema
char * field_name
PPCODE:
{
int ret = 0;
unsigned int loop = 0;
ARInternalIdList idList;
ARStatusList status;
ARNameType fieldName;
(void) ARError_reset();
Zero(&idList, 1, ARInternalIdList);
Zero(&status, 1, ARStatusList);
ret = ARGetListField(control, schema,
AR_FIELD_TYPE_ALL,
(ARTimestamp)0,
#if AR_CURRENT_API_VERSION >= 17
NULL, /* &objPropList (undocumented by BMC) */
#endif
&idList,
&status);
#ifdef PROFILE
((ars_ctrl *)control)->queries++;
#endif
if (! ARError( ret, status)) {
for (loop=0; loop<idList.numItems; loop++) {
#if AR_CURRENT_API_VERSION >= 17
ret = ARGetFieldCached(control, schema, idList.internalIdList[loop], fieldName, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &status);
#elif AR_CURRENT_API_VERSION >= 12
ret = ARGetFieldCached(control, schema, idList.internalIdList[loop], fieldName, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &status);
#else
ret = ARGetFieldCached(control, schema, idList.internalIdList[loop], fieldName, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &status);
#endif
if (ARError( ret, status))
break;
if (strcmp(field_name, fieldName) == 0){
XPUSHs(sv_2mortal(newSViv(idList.internalIdList[loop])));
break;
}
}
FreeARInternalIdList(&idList, FALSE);
}
}
void
ars_GetFieldTable(ctrl,schema)
ARControlStruct * ctrl
char * schema
PPCODE:
{
int ret = 0;
unsigned int loop = 0;
HV *fields, *h;
char *hkey;
SV *hval, **hvalName;
I32 klen;
(void) ARError_reset();
fields = fieldcache_get_schema_fields( ctrl, schema, TRUE );
if( ! fields ){
goto get_fieldtable_end;
}
hv_iterinit( fields );
while( (hval = hv_iternextsv(fields,&hkey,&klen)) ){
if( strcmp(hkey,"0") == 0 ) continue;
h = (HV* ) SvRV(hval);
hvalName = hv_fetch( h, "name", 4, 0 );
XPUSHs( sv_2mortal(newSVsv(*hvalName)) );
XPUSHs( sv_2mortal(newSVpv(hkey,0)) );
}
get_fieldtable_end:;
if( ! fields ){
XSRETURN_UNDEF;
}
}
SV *
ars_CreateEntry(ctrl,schema,...)
ARControlStruct * ctrl
char * schema
CODE:
{
int a = 0,
i = 0,
c = (items - 2) / 2;
AREntryIdType entryId;
ARFieldValueList fieldList;
ARInternalIdList getFieldIds;
ARStatusList status;
int ret = 0, rv = 0;
unsigned int dataType = 0, j = 0;
HV *cacheFields;
RETVAL=NULL;
(void) ARError_reset();
Zero(&entryId, 1, AREntryIdType);
Zero(&fieldList, 1, ARFieldValueList);
Zero(&getFieldIds, 1, ARInternalIdList);
Zero(&status, 1, ARStatusList);
if (((items - 2) % 2) || c < 1) {
(void) ARError_add( AR_RETURN_ERROR, AP_ERR_BAD_ARGS);
} else {
cacheFields = fieldcache_get_schema_fields( ctrl, schema, FALSE );
if( ! cacheFields ){
goto create_entry_end;
}
fieldList.numItems = c;
AMALLOCNN(fieldList.fieldValueList,c,ARFieldValueStruct);
getFieldIds.numItems = 0;
getFieldIds.internalIdList = NULL;
/* try to get data type from field cache, collect fieldIds which are not cached */
for (i=0; i<c; ++i) {
ARInternalId fieldId;
a = i*2+2;
fieldId = fieldList.fieldValueList[i].fieldId = SvIV(ST(a));
dataType = fieldcache_get_data_type( cacheFields, fieldId );
if (dataType <= AR_DATA_TYPE_MAX_TYPE) {
/* printf( "%s [%d] found in cache\n", schema, fieldId ); fflush(stdout); */ /* _DEBUG_ */
if (sv_to_ARValue(ctrl, ST(a+1), dataType, &fieldList.fieldValueList[i].value) < 0) {
goto create_entry_end;
}
}else{
if( getFieldIds.numItems == 0 ){
AMALLOCNN(getFieldIds.internalIdList,c,ARInternalId);
}
/* printf( "%s [%d] collect for loading\n", schema, fieldId ); fflush(stdout); */ /* _DEBUG_ */
getFieldIds.internalIdList[getFieldIds.numItems] = fieldId;
++getFieldIds.numItems;
}
}
/* load missing fields into cache */
if( getFieldIds.numItems > 0 ){
/* printf( "--- load missing fields ---\n" ); fflush(stdout); */ /* _DEBUG_ */
/* if( fieldcache_load_schema(ctrl,schema,&getFieldIds,NULL) != AR_RETURN_OK ){ */
if( fieldcache_load_schema(ctrl,schema,&getFieldIds,NULL) > AR_RETURN_WARNING ){
goto create_entry_end;
}
}
/* now get data type from the freshly cached fields */
i = 0;
for (j=0; j<getFieldIds.numItems; ++j) {
ARInternalId fieldId = getFieldIds.internalIdList[j];
while(fieldId != fieldList.fieldValueList[i].fieldId) ++i;
a = i*2+2;
dataType = fieldcache_get_data_type( cacheFields, fieldId );
if (dataType <= AR_DATA_TYPE_MAX_TYPE) {
/* printf( "%s [%d] freshly loaded\n", schema, fieldId ); fflush(stdout); */ /* _DEBUG_ */
if (sv_to_ARValue(ctrl, ST(a+1), dataType, &fieldList.fieldValueList[i].value) < 0) {
goto create_entry_end;
}
}else{
char errTxt[256];
sprintf( errTxt, "Failed to fetch field %d from hash", fieldId );
ARError_add(AR_RETURN_ERROR, AP_ERR_FIELD_TYPE);
ARError_add(AR_RETURN_ERROR, AP_ERR_CONTINUE, errTxt );
goto create_entry_end;
}
}
/* printf( "--------------------\n" ); fflush(stdout); */ /* _DEBUG_ */
ret = ARCreateEntry(ctrl, schema, &fieldList, entryId, &status);
#ifdef PROFILE
((ars_ctrl *)ctrl)->queries++;
#endif
if (! ARError( ret, status)) rv = 1;
create_entry_end:;
if(rv == 0) {
RETVAL = newSVsv(&PL_sv_undef);
} else {
RETVAL = newSVpv( entryId, strlen(entryId) );
}
AP_FREE(fieldList.fieldValueList);
if( getFieldIds.internalIdList != NULL ) AP_FREE(getFieldIds.internalIdList);
}
}
OUTPUT:
RETVAL
int
ars_DeleteEntry(ctrl,schema,entry_id)
ARControlStruct * ctrl
char * schema
char * entry_id
CODE:
{
int ret = 0;
ARStatusList status;
#if AR_EXPORT_VERSION >= 3
AREntryIdList entryList;
RETVAL = 0; /* assume error */
(void) ARError_reset();
Zero(&status, 1, ARStatusList);
if(perl_BuildEntryList(ctrl, &entryList, entry_id) != 0)
goto delete_fail;
ret = ARDeleteEntry(ctrl, schema, &entryList, 0, &status);
if (entryList.entryIdList) AP_FREE(entryList.entryIdList);
#else /* ARS 2 */
RETVAL = 0; /* assume error */
if(!entry_id || !*entry_id) {
ARError_add( AR_RETURN_ERROR, AP_ERR_BAD_EID);
goto delete_fail;
}
ret = ARDeleteEntry(ctrl, schema, entry_id, &status);
#endif
#ifdef PROFILE
((ars_ctrl *)ctrl)->queries++;
#endif
if (ARError(ret, status))
RETVAL = 0;
else
RETVAL = 1;
delete_fail:;
}
OUTPUT:
RETVAL
void
ars_GetEntryBLOB(ctrl,schema,entry_id,field_id,locType,locFile=NULL)
ARControlStruct * ctrl
char * schema
char * entry_id
ARInternalId field_id
int locType
char * locFile
PPCODE:
{
ARStatusList status;
AREntryIdList entryList;
#if AR_EXPORT_VERSION >= 4
ARLocStruct loc;
#endif
int ret = 0;
(void) ARError_reset();
Zero(&entryList, 1, AREntryIdList);
Zero(&status, 1, ARStatusList);
#if AR_EXPORT_VERSION >= 4
/* build entryList */
ret = perl_BuildEntryList(ctrl, &entryList, entry_id);
if(ret)
goto get_entryblob_end;
switch(locType) {
case AR_LOC_FILENAME:
if(locFile == NULL) {
ARError_add(AR_RETURN_ERROR,
AP_ERR_USAGE,
"locFile parameter required when specifying AR_LOC_FILENAME");
goto get_entryblob_end;
}
loc.locType = AR_LOC_FILENAME;
loc.u.filename = strdup(locFile); /* strdup(locFile) ? i'm not completely sure
which to use. will FreeARLocStruct call
free(loc.locType)? i'm assuming it will.
Purify doesnt complain, so i'm going
to leave this alone. */
break;
case AR_LOC_BUFFER:
loc.locType = AR_LOC_BUFFER;
loc.u.buf.bufSize = 0;
break;
default:
ARError_add(AR_RETURN_ERROR,
AP_ERR_USAGE,
"locType parameter is required.");
goto get_entryblob_end;
break;
}
ret = ARGetEntryBLOB(ctrl, schema, &entryList, field_id,
&loc, &status);
if(!ARError(ret, status)) {
if(locType == AR_LOC_BUFFER)
#if PERL_PATCHLEVEL_IS >= 6
XPUSHs(sv_2mortal(newSVpvn((const char *)
loc.u.buf.buffer,
loc.u.buf.bufSize)));
#else
XPUSHs(sv_2mortal(newSVpvn(
loc.u.buf.buffer,
loc.u.buf.bufSize)));
#endif
else
XPUSHs(sv_2mortal(newSViv(1)));
} else
XPUSHs(&PL_sv_undef);
if (entryList.entryIdList) AP_FREE(entryList.entryIdList);
switch (loc.locType)
{
case AR_LOC_FILENAME:
AP_FREE(loc.u.filename);
break;
case AR_LOC_BUFFER:
FreeARLocStruct(&loc, FALSE);
break;
}
#else /* pre ARS-4.0 */
(void) ARError_add(AR_RETURN_ERROR, AP_ERR_DEPRECATED,
"ars_GetEntryBLOB() is only available > ARS4.x");
XPUSHs(&PL_sv_undef);
#endif
get_entryblob_end:;
}
void
ars_GetEntry(ctrl,schema,entry_id,...)
ARControlStruct * ctrl
char * schema
char * entry_id
PPCODE:
{
int ret = 0;
unsigned int c = items - 3, i;
ARInternalIdList idList;
ARFieldValueList fieldList;
ARStatusList status;
#if AR_EXPORT_VERSION >= 3
AREntryIdList entryList;
#endif
(void) ARError_reset();
Zero(&idList, 1, ARInternalIdList);
Zero(&fieldList, 1, ARFieldValueList);
Zero(&status, 1, ARStatusList);
if (c < 1) {
idList.numItems = 0; /* get all fields */
} else {
idList.numItems = c;
idList.internalIdList = MALLOCNN(sizeof(ARInternalId) * c);
if (!idList.internalIdList)
goto get_entry_end;
for (i=0; i<c; i++)
idList.internalIdList[i] = SvIV(ST(i+3));
}
#if AR_EXPORT_VERSION >= 3
/* build entryList */
if(perl_BuildEntryList(ctrl, &entryList, entry_id) != 0)
goto get_entry_end;
ret = ARGetEntry(ctrl, schema, &entryList, &idList, &fieldList, &status);
if (entryList.entryIdList) AP_FREE(entryList.entryIdList);
#else /* ARS 2 */
if(!entry_id || !*entry_id) {
ARError_add( AR_RETURN_ERROR, AP_ERR_BAD_EID);
goto get_entry_cleanup;
}
ret = ARGetEntry(ctrl, schema, entry_id, &idList, &fieldList, &status);
#endif
#ifdef PROFILE
((ars_ctrl *)ctrl)->queries++;
#endif
if (ARError( ret, status)) {
goto get_entry_cleanup;
}
if(fieldList.numItems < 1) {
goto get_entry_cleanup;
}
for (i=0; i<fieldList.numItems; i++) {
XPUSHs(sv_2mortal(newSViv(fieldList.fieldValueList[i].fieldId)));
XPUSHs(sv_2mortal(perl_ARValueStruct(ctrl,
&fieldList.fieldValueList[i].value)));
}
FreeARFieldValueList(&fieldList,FALSE);
get_entry_cleanup:;
if (idList.internalIdList) AP_FREE(idList.internalIdList);
get_entry_end:;
}
void