Lines 166-173
creates one, and connects to the database.
Link Here
|
166 |
|
166 |
|
167 |
This database handle is cached for future use: if you call |
167 |
This database handle is cached for future use: if you call |
168 |
C<$database-E<gt>schema> twice, you will get the same handle both |
168 |
C<$database-E<gt>schema> twice, you will get the same handle both |
169 |
times. If you need a second database handle, use C<&new_schema> and |
169 |
times. |
170 |
possibly C<&set_schema>. |
|
|
171 |
|
170 |
|
172 |
=cut |
171 |
=cut |
173 |
|
172 |
|
Lines 182-261
sub schema {
Link Here
|
182 |
return $database->{schema}; |
181 |
return $database->{schema}; |
183 |
} |
182 |
} |
184 |
|
183 |
|
185 |
=head2 new_schema |
|
|
186 |
|
187 |
$schema = $database->new_schema; |
188 |
|
189 |
Creates a new connection to the Koha database for the current context, |
190 |
and returns the database handle (a C<DBI::db> object). |
191 |
|
192 |
The handle is not saved anywhere: this method is strictly a |
193 |
convenience function; the point is that it knows which database to |
194 |
connect to so that the caller doesn't have to know. |
195 |
|
196 |
=cut |
197 |
|
198 |
#' |
199 |
sub new_schema { |
200 |
my $self = shift; |
201 |
|
202 |
return &_new_schema(); |
203 |
} |
204 |
|
205 |
=head2 set_schema |
206 |
|
207 |
$my_schema = $database->new_schema; |
208 |
$database->set_schema($my_schema); |
209 |
... |
210 |
$database->restore_schema; |
211 |
|
212 |
C<&set_schema> and C<&restore_schema> work in a manner analogous to |
213 |
C<&set_context> and C<&restore_context>. |
214 |
|
215 |
C<&set_schema> saves the current database handle on a stack, then sets |
216 |
the current database handle to C<$my_schema>. |
217 |
|
218 |
C<$my_schema> is assumed to be a good database handle. |
219 |
|
220 |
=cut |
221 |
|
222 |
sub set_schema { |
223 |
my $self = shift; |
224 |
my $new_schema = shift; |
225 |
|
226 |
# Save the current database handle on the handle stack. |
227 |
# We assume that $new_schema is all good: if the caller wants to |
228 |
# screw himself by passing an invalid handle, that's fine by |
229 |
# us. |
230 |
push @{ $database->{schema_stack} }, $database->{schema}; |
231 |
$database->{schema} = $new_schema; |
232 |
} |
233 |
|
234 |
=head2 restore_schema |
235 |
|
236 |
$database->restore_schema; |
237 |
|
238 |
Restores the database handle saved by an earlier call to |
239 |
C<$database-E<gt>set_schema>. |
240 |
|
241 |
=cut |
242 |
|
243 |
sub restore_schema { |
244 |
my $self = shift; |
245 |
|
246 |
if ( $#{ $database->{schema_stack} } < 0 ) { |
247 |
|
248 |
# Stack underflow |
249 |
die "SCHEMA stack underflow"; |
250 |
} |
251 |
|
252 |
# Pop the old database handle and set it. |
253 |
$database->{schema} = pop @{ $database->{schema_stack} }; |
254 |
|
255 |
# FIXME - If it is determined that restore_context should |
256 |
# return something, then this function should, too. |
257 |
} |
258 |
|
259 |
=head2 db_scheme2dbi |
184 |
=head2 db_scheme2dbi |
260 |
|
185 |
|
261 |
my $dbd_driver_name = Koha::Database::db_scheme2dbi($scheme); |
186 |
my $dbd_driver_name = Koha::Database::db_scheme2dbi($scheme); |