Added documentation and test for the IDZebra::Resultset object
[idzebra-moved-to-github.git] / perl / lib / IDZebra / Resultset.pm
1 # $Id: Resultset.pm,v 1.6 2003-03-03 12:14:27 pop Exp $
2
3 # Zebra perl API header
4 # =============================================================================
5 package IDZebra::Resultset;
6
7 use strict;
8 use warnings;
9
10 BEGIN {
11     use IDZebra;
12     use IDZebra::Logger qw(:flags :calls);
13     use Scalar::Util qw(weaken);
14     use Carp;
15     our $VERSION = do { my @r = (q$Revision: 1.6 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; 
16     our @ISA = qw(IDZebra::Logger);
17 }
18
19 1;
20 # -----------------------------------------------------------------------------
21 # Class constructors, destructor
22 # -----------------------------------------------------------------------------
23 sub new {
24     my ($proto,$session, %args) = @_;
25     my $class = ref($proto) || $proto;
26     my $self = {};
27     bless ($self, $class);
28
29     $self->{session} = $session;
30     weaken ($self->{session});
31
32     $self->{odr_stream} = IDZebra::odr_createmem($IDZebra::ODR_DECODE);
33
34     $self->{name}        = $args{name};
35     $self->{recordCount} = $args{recordCount};
36     $self->{errCode}     = $args{errCode};
37     $self->{errString}   = $args{errString};
38
39     return ($self);
40 }
41
42 sub recordCount {
43     my ($self) = @_;
44     return ($self->{recordCount});
45 }
46 sub count {
47     my ($self) = @_;
48     return ($self->{recordCount});
49 }
50
51 sub errCode {
52     my ($self) = @_;
53     return ($self->{errCode});
54 }
55
56 sub errString {
57     my ($self) = @_;
58     return ($self->{errCode});
59 }
60
61 # =============================================================================
62 sub DESTROY {
63     my $self = shift;
64
65     # Deleteresultset?
66     
67     my $stats = 0;
68     if ($self->{session}{zh}) { 
69         my $r = IDZebra::deleteResultSet($self->{session}{zh},
70                                          0, #Z_DeleteRequest_list,
71                                          1,[$self->{name}],
72                                          $stats);
73     }
74
75     if ($self->{odr_stream}) {
76         IDZebra::odr_reset($self->{odr_stream});
77         IDZebra::odr_destroy($self->{odr_stream});
78         $self->{odr_stream} = undef;  
79     }
80
81     delete($self->{session});
82 }
83 # -----------------------------------------------------------------------------
84 sub records {
85     my ($self, %args) = @_;
86
87     unless ($self->{session}{zh}) { 
88         croak ("Session is closed or out of scope");
89     }
90     my $from = $args{from} ? $args{from} : 1;
91     my $to   = $args{to}   ? $args{to}   : $self->{recordCount};
92
93     my $elementSet   = $args{elementSet}   ? $args{elementSet}    : 'R';
94     my $schema       = $args{schema}       ? $args{schema}        : '';
95     my $recordSyntax = $args{recordSyntax} ? $args{recordSyntax}  : '';
96     
97     my $class        = $args{class}        ? $args{class}         : '';
98     
99
100     my $ro = IDZebra::RetrievalObj->new();
101     IDZebra::records_retrieve($self->{session}{zh},
102                               $self->{odr_stream},
103                               $self->{name},
104                               $elementSet,
105                               $schema,
106                               $recordSyntax,
107                               $from,
108                               $to,
109                               $ro);
110
111
112     my @res = ();
113
114     for (my $i=$from; $i<=$to; $i++) {
115         my $rec = IDZebra::RetrievalRecord->new();
116         IDZebra::record_retrieve($ro, $self->{odr_stream}, $rec, $i-$from+1);
117         if ($class) {
118             
119         } else {
120             push (@res, $rec);
121         }
122     }
123
124     IDZebra::odr_reset($self->{odr_stream});
125
126     return (@res);
127 }
128
129 # ============================================================================
130 sub sort {
131     my ($self, $sortspec, $setname) = @_;
132
133     unless ($self->{session}{zh}) { 
134         croak ("Session is closed or out of scope");
135     }
136
137     unless ($setname) {
138         $_[0] = $self->{session}->sortResultsets($sortspec, 
139                                                  $self->{name}, ($self));
140         return ($_[0]);
141     } else {
142         return ($self->{session}->sortResultsets($sortspec, 
143                                                  $setname, ($self)));
144     }
145 }
146
147 # ============================================================================
148 __END__
149
150 =head1 NAME
151
152 IDZebra::Resultset - Representation of Zebra search results
153
154 =head1 SYNOPSIS
155
156   $count = $rs->count;
157
158   printf ("RS Status is %d (%s)\n", $rs->errCode, $rs->errString);
159
160   my @recs = $rs->records(from => 1,
161                           to   => 10);
162
163 =head1 DESCRIPTION
164
165 The I<Resultset> object represents results of a Zebra search. Contains number of hits, search status, and can be used to sort and retrieve the records.
166
167 =head1 PROPERTIES
168
169 The folowing properties are available, trough object methods and the object hash reference:
170
171 =over 4
172
173 =item B<errCode>
174
175 The error code returned from search, resulting the Resultset object.
176
177 =item B<errString>
178
179 The optional error string
180
181 =item B<recordCount>
182
183 The number of hits (records available) in the resultset
184
185 =item B<count>
186
187 Just the synonym for I<recordCount>
188
189 =back
190
191 =head1 RETRIEVING RECORDS
192
193 In order to retrieve records, use the I<records> method:
194
195   my @recs = $rs->records();
196
197 By default this is going to return an array of IDZebra::RetrievalRecord objects. The possible arguments are:
198
199 =over 4
200
201 =item B<from>
202
203 Retrieve records from the given position. The first record corresponds to position 1. If not specified, retrieval starts from the first record.
204
205 =item B<to>
206
207 The last record position to be fetched. If not specified, all records are going to be fetched, starting from position I<from>.
208
209 =item B<elementSet>
210
211 The element set used for retrieval. If not specified 'I<R>' is used, which will return the "record" in the original format (ie.: without extraction, just as the original file, or data buffer in the update call).
212
213 =item B<schema>
214
215 The schema used for retrieval. The default is "".
216
217 =item B<recordSyntax>
218
219 The record syntax for retrieval. The default is SUTRS.
220
221 =back
222
223 =head1 SORTING
224
225
226
227 =head1 COPYRIGHT
228
229 Fill in
230
231 =head1 AUTHOR
232
233 Peter Popovics, pop@technomat.hu
234
235 =head1 SEE ALSO
236
237 IDZebra, IDZebra::Data1, Zebra documentation
238
239 =cut