Perl Filter and Perl API
[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 Scalar::Util;
13
14 our @ISA = qw(IDZebra::Logger);
15
16 1;
17 # -----------------------------------------------------------------------------
18 # Class constructors, destructor
19 # -----------------------------------------------------------------------------
20 sub new {
21     my ($proto,$service) = @_;
22     my $class = ref($proto) || $proto;
23     my $self = {};
24     $self->{service} = $service;
25     $self->{sessionID} = $service->{sessc};
26     bless ($self, $class);
27     return ($self);
28 }
29
30 sub open {
31     my ($proto,$service) = @_;
32     my $self = {};
33     if (ref($proto)) { $self = $proto; } else { 
34         $self = $proto->new($service);
35     }
36     unless (defined($self->{zh})) {
37         $self->{zh}=IDZebra::open($self->{service}{zs}) if ($self->{service}); 
38     }   
39     $self->Repository(); # Make a dummy record group
40
41     $self->{odr_input} = IDZebra::odr_createmem($IDZebra::ODR_DECODE);
42     $self->{odr_output} = IDZebra::odr_createmem($IDZebra::ODR_ENCODE);
43
44     return ($self);
45 }
46
47 sub close {
48     my ($self) = @_;
49
50     if ($self->{zh}) {
51         IDZebra::close($self->{zh});
52         $self->{zh} = undef;
53     }
54
55     if ($self->{odr_input}) {
56         IDZebra::odr_destroy($self->{odr_input});
57         $self->{odr_input} = undef;  
58     }
59
60     if ($self->{odr_output}) {
61         IDZebra::odr_destroy($self->{odr_output});
62         $self->{odr_output} = undef;  
63     }
64
65     delete($self->{service}{sessions}{$self->{sessionID}});
66     delete($self->{service});
67 }
68
69 sub DESTROY {
70     my ($self) = @_;
71     print STDERR "Destroy_session\n";
72     $self->close; 
73 }
74 # -----------------------------------------------------------------------------
75 # Error handling
76 # -----------------------------------------------------------------------------
77 sub errCode {
78     my ($self) = @_;
79     return(IDZebra::errCode($self->{zh}));
80 }
81
82 sub errString {
83     my ($self) = @_;
84     return(IDZebra::errString($self->{zh}));
85 }
86
87 sub errAdd {
88     my ($self) = @_;
89     return(IDZebra::errAdd($self->{zh}));
90 }
91
92 # -----------------------------------------------------------------------------
93 # Transaction stuff
94 # -----------------------------------------------------------------------------
95 sub begin_trans {
96     my ($self) = @_;
97     unless ($self->{trans_started}) {
98         $self->{trans_started} = 1;
99         IDZebra::begin_trans($self->{zh});
100     }
101 }
102
103 sub end_trans {
104     my ($self) = @_;
105     if ($self->{trans_started}) {
106         $self->{trans_started} = 0;
107         IDZebra::end_trans($self->{zh});
108     }
109 }
110
111 sub shadow_enable {
112     my ($self, $value) = @_;
113     if ($#_ > 0) { IDZebra::set_shadow_enable($self->{zh},$value); }
114     return (IDZebra::get_shadow_enable($self->{zh}));
115 }
116
117 sub commit {
118     my ($self) = @_;
119     if ($self->shadow_enable) {
120         return(IDZebra::commit($self->{zh}));
121     }
122 }
123
124 # -----------------------------------------------------------------------------
125 # We don't really need that...
126 # -----------------------------------------------------------------------------
127 sub odr_reset {
128     my ($self, $name) = @_;
129     if ($name !~/^(input|output)$/) {
130         croak("Undefined ODR '$name'");
131     }
132   IDZebra::odr_reset($self->{"odr_$name"});
133 }
134
135 # -----------------------------------------------------------------------------
136 # Init/compact
137 # -----------------------------------------------------------------------------
138 sub init {
139     my ($self) = @_;
140     return(IDZebra::init($self->{zh}));
141 }
142
143 sub compact {
144     my ($self) = @_;
145     return(IDZebra::compact($self->{zh}));
146 }
147
148 # -----------------------------------------------------------------------------
149 # Repository stuff
150 # -----------------------------------------------------------------------------
151 sub Repository {
152     my ($self, %args) = @_;
153     if (!$self->{rep}) {
154         $self->{rep} = IDZebra::Repository->new($self, %args);
155     } else {
156         $self->{rep}->modify(%args);
157     }
158
159     return ($self->{rep});
160 }
161
162 # -----------------------------------------------------------------------------
163 # Search and retrieval
164 # -----------------------------------------------------------------------------
165 sub select_databases {
166     my ($self, @databases) = @_;
167     return (IDZebra::select_databases($self->{zh}, 
168                                       ($#databases + 1), 
169                                       \@databases));
170 }
171
172 sub begin_read {
173     my ($self) =@_;
174     return(IDZebra::begin_read($self->{zh}));
175 }
176
177 sub end_read {
178     my ($self) =@_;
179     IDZebra::end_read($self->{zh});
180 }
181
182 sub search_pqf {
183     my ($self, $query, $setname) = @_;
184     return (IDZebra::search_PQF($self->{zh},
185                                 $self->{odr_input},
186                                 $self->{odr_output},
187                                 $query,
188                                 $setname));
189 }
190
191 __END__
192
193 =head1 NAME
194
195 IDZebra::Session - 
196
197 =head1 SYNOPSIS
198
199 =head1 DESCRIPTION
200
201 =head1 COPYRIGHT
202
203 Fill in
204
205 =head1 AUTHOR
206
207 Peter Popovics, pop@technomat.hu
208
209 =head1 SEE ALSO
210
211 IDZebra, IDZebra::Data1, Zebra documentation
212
213 =cut