Search now returns a Resultset object, for retrieval purposes.
[idzebra-moved-to-github.git] / perl / lib / IDZebra / Session.pm
1 #!/usr/bin/perl
2 # ============================================================================
3 # Zebra perl API header
4 # =============================================================================
5 use strict;
6 use Carp;
7 # ============================================================================
8 package IDZebra::Session;
9 use IDZebra;
10 use IDZebra::Logger qw(:flags :calls);
11 use IDZebra::Repository;
12 use IDZebra::Resultset;
13 use Scalar::Util;
14
15 our @ISA = qw(IDZebra::Logger);
16
17 1;
18 # -----------------------------------------------------------------------------
19 # Class constructors, destructor
20 # -----------------------------------------------------------------------------
21 sub new {
22     my ($proto,$service) = @_;
23     my $class = ref($proto) || $proto;
24     my $self = {};
25     $self->{service} = $service;
26     $self->{sessionID} = $service->{sessc};
27     bless ($self, $class);
28     return ($self);
29 }
30
31 sub open {
32     my ($proto,$service) = @_;
33     my $self = {};
34     if (ref($proto)) { $self = $proto; } else { 
35         $self = $proto->new($service);
36     }
37     unless (defined($self->{zh})) {
38         $self->{zh}=IDZebra::open($self->{service}{zs}) if ($self->{service}); 
39     }   
40     $self->Repository(); # Make a dummy record group
41
42     $self->{odr_input} = IDZebra::odr_createmem($IDZebra::ODR_DECODE);
43     $self->{odr_output} = IDZebra::odr_createmem($IDZebra::ODR_ENCODE);
44
45     return ($self);
46 }
47
48 sub close {
49     my ($self) = @_;
50
51     if ($self->{zh}) {
52         IDZebra::close($self->{zh});
53         $self->{zh} = undef;
54     }
55     
56     if ($self->{odr_input}) {
57         IDZebra::odr_reset($self->{odr_input});
58         IDZebra::odr_destroy($self->{odr_input});
59         $self->{odr_input} = undef;  
60     }
61
62     if ($self->{odr_output}) {
63         IDZebra::odr_reset($self->{odr_output});
64         IDZebra::odr_destroy($self->{odr_output});
65         $self->{odr_output} = undef;  
66     }
67
68     delete($self->{service}{sessions}{$self->{sessionID}});
69     delete($self->{service});
70 }
71
72 sub DESTROY {
73     my ($self) = @_;
74     print STDERR "Destroy_session\n";
75     $self->close; 
76 }
77 # -----------------------------------------------------------------------------
78 # Error handling
79 # -----------------------------------------------------------------------------
80 sub errCode {
81     my ($self) = @_;
82     return(IDZebra::errCode($self->{zh}));
83 }
84
85 sub errString {
86     my ($self) = @_;
87     return(IDZebra::errString($self->{zh}));
88 }
89
90 sub errAdd {
91     my ($self) = @_;
92     return(IDZebra::errAdd($self->{zh}));
93 }
94
95 # -----------------------------------------------------------------------------
96 # Transaction stuff
97 # -----------------------------------------------------------------------------
98 sub begin_trans {
99     my ($self) = @_;
100     unless ($self->{trans_started}) {
101         $self->{trans_started} = 1;
102         IDZebra::begin_trans($self->{zh});
103     }
104 }
105
106 sub end_trans {
107     my ($self) = @_;
108     if ($self->{trans_started}) {
109         $self->{trans_started} = 0;
110         IDZebra::end_trans($self->{zh});
111     }
112 }
113
114 sub begin_read {
115     my ($self) =@_;
116     return(IDZebra::begin_read($self->{zh}));
117 }
118
119 sub end_read {
120     my ($self) =@_;
121     IDZebra::end_read($self->{zh});
122 }
123
124 sub shadow_enable {
125     my ($self, $value) = @_;
126     if ($#_ > 0) { IDZebra::set_shadow_enable($self->{zh},$value); }
127     return (IDZebra::get_shadow_enable($self->{zh}));
128 }
129
130 sub commit {
131     my ($self) = @_;
132     if ($self->shadow_enable) {
133         return(IDZebra::commit($self->{zh}));
134     }
135 }
136
137 # -----------------------------------------------------------------------------
138 # We don't really need that...
139 # -----------------------------------------------------------------------------
140 sub odr_reset {
141     my ($self, $name) = @_;
142     if ($name !~/^(input|output)$/) {
143         croak("Undefined ODR '$name'");
144     }
145   IDZebra::odr_reset($self->{"odr_$name"});
146 }
147
148 # -----------------------------------------------------------------------------
149 # Init/compact
150 # -----------------------------------------------------------------------------
151 sub init {
152     my ($self) = @_;
153     return(IDZebra::init($self->{zh}));
154 }
155
156 sub compact {
157     my ($self) = @_;
158     return(IDZebra::compact($self->{zh}));
159 }
160
161 # -----------------------------------------------------------------------------
162 # Repository stuff
163 # -----------------------------------------------------------------------------
164 sub Repository {
165     my ($self, %args) = @_;
166     if (!$self->{rep}) {
167         $self->{rep} = IDZebra::Repository->new($self, %args);
168     } else {
169         $self->{rep}->modify(%args);
170     }
171
172     return ($self->{rep});
173 }
174
175 # -----------------------------------------------------------------------------
176 # Search 
177 # -----------------------------------------------------------------------------
178 sub select_databases {
179     my ($self, @databases) = @_;
180     return (IDZebra::select_databases($self->{zh}, 
181                                       ($#databases + 1), 
182                                       \@databases));
183 }
184
185 sub search_pqf {
186     my ($self, $query, $setname) = @_;
187     my $hits = IDZebra::search_PQF($self->{zh},
188                                    $self->{odr_input},
189                                    $self->{odr_output},
190                                    $query,
191                                    $setname);
192
193     my $rs  = IDZebra::Resultset->new($self,
194                                       name        => $setname,
195                                       recordCount => $hits,
196                                       errCode     => $self->errCode,
197                                       errString   => $self->errString);
198     return($rs);
199 }
200
201 # -----------------------------------------------------------------------------
202 # Sort
203 #
204 # Sorting of multiple result sets is not supported by zebra...
205 # -----------------------------------------------------------------------------
206
207 sub sortResultsets {
208     my ($self, $sortspec, $setname, @sets) = @_;
209
210     my @setnames;
211     my $count = 0;
212     foreach my $rs (@sets) {
213         push (@setnames, $rs->{name});
214         $count += $rs->{recordCount};  # is this really sure ??? It doesn't 
215                                        # matter now...
216     }
217
218     my $status = IDZebra::sort($self->{zh},
219                                $self->{odr_output},
220                                $sortspec,
221                                $setname,
222                                \@setnames);
223
224     my $errCode = $self->errCode;
225     my $errString = $self->errString;
226
227     if ($status || $errCode) {$count = 0;}
228
229     my $rs  = IDZebra::Resultset->new($self,
230                                       name        => $setname,
231                                       recordCount => $count,
232                                       errCode     => $errCode,
233                                       errString   => $errString);
234     
235     return ($rs);
236 }
237
238
239 __END__
240
241 =head1 NAME
242
243 IDZebra::Session - 
244
245 =head1 SYNOPSIS
246
247 =head1 DESCRIPTION
248
249 =head1 COPYRIGHT
250
251 Fill in
252
253 =head1 AUTHOR
254
255 Peter Popovics, pop@technomat.hu
256
257 =head1 SEE ALSO
258
259 IDZebra, IDZebra::Data1, Zebra documentation
260
261 =cut