New class to represent a retrieval context. Created as a result of
[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     if ($self->{odr_stream}) {
46         IDZebra::odr_reset($self->{odr_stream});
47         IDZebra::odr_destroy($self->{odr_stream});
48         $self->{odr_stream} = undef;  
49     }
50
51     delete($self->{ro});
52     delete($self->{session});
53 }
54 # -----------------------------------------------------------------------------
55 sub records {
56     my ($self, %args) = @_;
57
58     my $from = $args{from} ? $args{from} : 1;
59     my $to   = $args{to}   ? $args{to}   : $self->{recordCount};
60
61     my $elementSet   = $args{elementSet}   ? $args{elementSet}    : 'R';
62     my $schema       = $args{schema}       ? $args{schema}        : '';
63     my $recordSyntax = $args{recordSyntax} ? $args{recordSyntax}  : '';
64     
65
66     my $ro = IDZebra::RetrievalObj->new();
67     IDZebra::records_retrieve($self->{session}{zh},
68                               $self->{odr_stream},
69                               $self->{name},
70                               $elementSet,
71                               $schema,
72                               $recordSyntax,
73                               $from,
74                               $to,
75                               $ro);
76
77
78     my @res = ();
79
80     for (my $i=$from; $i<=$to; $i++) {
81         my $rec = IDZebra::RetrievalRecord->new();
82         IDZebra::record_retrieve($ro, $self->{odr_stream}, $rec, $i-$from+1);
83         push (@res, $rec);
84     }
85
86     IDZebra::odr_reset($self->{odr_stream});
87
88     return (@res);
89 }
90
91 sub sort {
92     my ($self, $sortspec, $setname) = @_;
93     unless ($setname) {
94         $_[0] = $self->{session}->sortResultsets($sortspec, 
95                                                  $self->{name}, ($self));
96         return ($_[0]);
97     } else {
98         return ($self->{session}->sortResultsets($sortspec, 
99                                                  $setname, ($self)));
100     }
101 }
102
103 __END__
104
105 =head1 NAME
106
107 IDZebra::Session - 
108
109 =head1 SYNOPSIS
110
111 =head1 DESCRIPTION
112
113 =head1 COPYRIGHT
114
115 Fill in
116
117 =head1 AUTHOR
118
119 Peter Popovics, pop@technomat.hu
120
121 =head1 SEE ALSO
122
123 IDZebra, IDZebra::Data1, Zebra documentation
124
125 =cut