Perl filters now can optionally reach data via a virtual filehandle.
[idzebra-moved-to-github.git] / perl / lib / IDZebra / Resultset.pm
1 #!/usr/bin/perl
2 # ============================================================================
3 # Zebra perl API header
4 # =============================================================================
5 use strict;
6 use Carp;
7 # ============================================================================
8 package IDZebra::Resultset;
9 use IDZebra;
10 use IDZebra::Logger qw(:flags :calls);
11 use IDZebra::Repository;
12 use Scalar::Util qw(weaken);
13
14 our @ISA = qw(IDZebra::Logger);
15
16 1;
17 # -----------------------------------------------------------------------------
18 # Class constructors, destructor
19 # -----------------------------------------------------------------------------
20 sub new {
21     my ($proto,$session, %args) = @_;
22     my $class = ref($proto) || $proto;
23     my $self = {};
24     $self->{session} = $session;
25     weaken ($self->{session});
26
27     # Retrieval object
28     $self->{ro} = IDZebra::RetrievalObj->new();
29     $self->{odr_stream} = IDZebra::odr_createmem($IDZebra::ODR_DECODE);
30
31     $self->{name}        = $args{name};
32     $self->{recordCount} = $args{recordCount};
33     $self->{errCode}     = $args{errCode};
34     $self->{errString}   = $args{errString};
35
36     bless ($self, $class);
37     return ($self);
38 }
39
40 sub DESTROY {
41     my ($self) = @_;
42
43 #    print STDERR "Destroy RS\n";
44
45     # Deleteresultset?
46
47     if ($self->{odr_stream}) {
48         IDZebra::odr_reset($self->{odr_stream});
49         IDZebra::odr_destroy($self->{odr_stream});
50         $self->{odr_stream} = undef;  
51     }
52
53     delete($self->{ro});
54     delete($self->{session});
55 }
56 # -----------------------------------------------------------------------------
57 sub records {
58     my ($self, %args) = @_;
59
60     my $from = $args{from} ? $args{from} : 1;
61     my $to   = $args{to}   ? $args{to}   : $self->{recordCount};
62
63     my $elementSet   = $args{elementSet}   ? $args{elementSet}    : 'R';
64     my $schema       = $args{schema}       ? $args{schema}        : '';
65     my $recordSyntax = $args{recordSyntax} ? $args{recordSyntax}  : '';
66     
67
68     my $ro = IDZebra::RetrievalObj->new();
69     IDZebra::records_retrieve($self->{session}{zh},
70                               $self->{odr_stream},
71                               $self->{name},
72                               $elementSet,
73                               $schema,
74                               $recordSyntax,
75                               $from,
76                               $to,
77                               $ro);
78
79
80     my @res = ();
81
82     for (my $i=$from; $i<=$to; $i++) {
83         my $rec = IDZebra::RetrievalRecord->new();
84         IDZebra::record_retrieve($ro, $self->{odr_stream}, $rec, $i-$from+1);
85         push (@res, $rec);
86     }
87
88     IDZebra::odr_reset($self->{odr_stream});
89
90     return (@res);
91 }
92
93 sub sort {
94     my ($self, $sortspec, $setname) = @_;
95     unless ($setname) {
96         $_[0] = $self->{session}->sortResultsets($sortspec, 
97                                                  $self->{name}, ($self));
98         return ($_[0]);
99     } else {
100         return ($self->{session}->sortResultsets($sortspec, 
101                                                  $setname, ($self)));
102     }
103 }
104
105 __END__
106
107 =head1 NAME
108
109 IDZebra::Session - 
110
111 =head1 SYNOPSIS
112
113 =head1 DESCRIPTION
114
115 =head1 COPYRIGHT
116
117 Fill in
118
119 =head1 AUTHOR
120
121 Peter Popovics, pop@technomat.hu
122
123 =head1 SEE ALSO
124
125 IDZebra, IDZebra::Data1, Zebra documentation
126
127 =cut