Added sorting, +tests. documentation is needed.
[idzebra-moved-to-github.git] / perl / lib / IDZebra / Resultset.pm
1 # $Id: Resultset.pm,v 1.7 2003-03-03 18:27:25 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.7 $ =~ /\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 #    print STDERR "Destroy RS\n";
66     # Deleteresultset?
67     
68     my $stats = 0;
69     if ($self->{session}{zh}) { 
70         my $r = IDZebra::deleteResultSet($self->{session}{zh},
71                                          0, #Z_DeleteRequest_list,
72                                          1,[$self->{name}],
73                                          $stats);
74     }
75
76     if ($self->{odr_stream}) {
77         IDZebra::odr_reset($self->{odr_stream});
78         IDZebra::odr_destroy($self->{odr_stream});
79         $self->{odr_stream} = undef;  
80     }
81
82     delete($self->{session});
83 }
84 # -----------------------------------------------------------------------------
85 sub records {
86     my ($self, %args) = @_;
87
88     unless ($self->{session}{zh}) { 
89         croak ("Session is closed or out of scope");
90     }
91     my $from = $args{from} ? $args{from} : 1;
92     my $to   = $args{to}   ? $args{to}   : $self->{recordCount};
93
94     my $elementSet   = $args{elementSet}   ? $args{elementSet}    : 'R';
95     my $schema       = $args{schema}       ? $args{schema}        : '';
96     my $recordSyntax = $args{recordSyntax} ? $args{recordSyntax}  : '';
97     
98     my $class        = $args{class}        ? $args{class}         : '';
99     
100
101     my $ro = IDZebra::RetrievalObj->new();
102     IDZebra::records_retrieve($self->{session}{zh},
103                               $self->{odr_stream},
104                               $self->{name},
105                               $elementSet,
106                               $schema,
107                               $recordSyntax,
108                               $from,
109                               $to,
110                               $ro);
111
112
113     my @res = ();
114
115     for (my $i=$from; $i<=$to; $i++) {
116         my $rec = IDZebra::RetrievalRecord->new();
117         IDZebra::record_retrieve($ro, $self->{odr_stream}, $rec, $i-$from+1);
118         if ($class) {
119             
120         } else {
121             push (@res, $rec);
122         }
123     }
124
125     IDZebra::odr_reset($self->{odr_stream});
126
127     return (@res);
128 }
129
130 # ============================================================================
131 sub sort {
132     my ($self, $sortspec, $setname) = @_;
133
134     unless ($self->{session}{zh}) { 
135         croak ("Session is closed or out of scope");
136     }
137
138     unless ($setname) {
139         return ($_[0] = $self->{session}->sortResultsets($sortspec, 
140                                                  $self->{session}->_new_setname, ($self)));
141         return ($_[0]);
142     } else {
143         return ($self->{session}->sortResultsets($sortspec, 
144                                                  $setname, ($self)));
145     }
146 }
147
148 # ============================================================================
149 __END__
150
151 =head1 NAME
152
153 IDZebra::Resultset - Representation of Zebra search results
154
155 =head1 SYNOPSIS
156
157   $count = $rs->count;
158
159   printf ("RS Status is %d (%s)\n", $rs->errCode, $rs->errString);
160
161   my @recs = $rs->records(from => 1,
162                           to   => 10);
163
164 =head1 DESCRIPTION
165
166 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.
167
168 =head1 PROPERTIES
169
170 The folowing properties are available, trough object methods and the object hash reference:
171
172 =over 4
173
174 =item B<errCode>
175
176 The error code returned from search, resulting the Resultset object.
177
178 =item B<errString>
179
180 The optional error string
181
182 =item B<recordCount>
183
184 The number of hits (records available) in the resultset
185
186 =item B<count>
187
188 Just the synonym for I<recordCount>
189
190 =back
191
192 =head1 RETRIEVING RECORDS
193
194 In order to retrieve records, use the I<records> method:
195
196   my @recs = $rs->records();
197
198 By default this is going to return an array of IDZebra::RetrievalRecord objects. The possible arguments are:
199
200 =over 4
201
202 =item B<from>
203
204 Retrieve records from the given position. The first record corresponds to position 1. If not specified, retrieval starts from the first record.
205
206 =item B<to>
207
208 The last record position to be fetched. If not specified, all records are going to be fetched, starting from position I<from>.
209
210 =item B<elementSet>
211
212 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).
213
214 =item B<schema>
215
216 The schema used for retrieval. The default is "".
217
218 =item B<recordSyntax>
219
220 The record syntax for retrieval. The default is SUTRS.
221
222 =back
223
224 =head1 SORTING
225
226
227
228 =head1 COPYRIGHT
229
230 Fill in
231
232 =head1 AUTHOR
233
234 Peter Popovics, pop@technomat.hu
235
236 =head1 SEE ALSO
237
238 IDZebra, IDZebra::Data1, Zebra documentation
239
240 =cut