View | Details | Raw Unified | Return to bug 36149
Collapse All | Expand All

(-)a/Koha/Database.pm (-76 / +1 lines)
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);
(-)a/t/db_dependent/Koha_Database.t (-7 / +1 lines)
Lines 4-10 Link Here
4
use Modern::Perl;
4
use Modern::Perl;
5
use utf8;
5
use utf8;
6
6
7
use Test::More tests => 12;
7
use Test::More tests => 9;
8
8
9
BEGIN {
9
BEGIN {
10
    use_ok('Koha::Database');
10
    use_ok('Koha::Database');
Lines 24-34 is( $another_schema->storage->_conn_pid, $schema->storage->_conn_pid, 'Getting a Link Here
24
$another_schema = Koha::Database->new->schema();
24
$another_schema = Koha::Database->new->schema();
25
is( $another_schema->storage->_conn_pid, $schema->storage->_conn_pid, 'Getting another schema should return the same one, it has correctly been cached' );
25
is( $another_schema->storage->_conn_pid, $schema->storage->_conn_pid, 'Getting another schema should return the same one, it has correctly been cached' );
26
26
27
my $new_schema;
28
ok( $new_schema = $database->new_schema(), 'Try to get a new schema' );
29
ok( $database->set_schema($new_schema), 'Switch to new schema' );
30
ok( $database->restore_schema(),        'Switch back' );
31
32
# run in a transaction
27
# run in a transaction
33
$schema->storage->txn_begin();
28
$schema->storage->txn_begin();
34
29
35
- 

Return to bug 36149