Main Koha release repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.1 KiB

package Koha::Database;
# Copyright 2013 Catalyst IT
# This file is part of Koha.
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
=head1 NAME
use Koha::Database;
my $database = Koha::Database->new();
my $schema = $database->schema();
use Modern::Perl;
use Carp;
use C4::Context;
use base qw(Class::Accessor);
use vars qw($database);
__PACKAGE__->mk_accessors(qw( ));
# _new_schema
# Internal helper function (not a method!). This creates a new
# database connection from the data given in the current context, and
# returns it.
sub _new_schema {
require Koha::Schema;
my $context = C4::Context->new();
# we are letting C4::Context->dbh not set the RaiseError handle attribute
# for now for compatbility purposes
my $schema = Koha::Schema->connect( sub { C4::Context->dbh }, { unsafe => 1 } );
return $schema;
=head2 schema
$schema = $database->schema;
Returns a database handle connected to the Koha database for the
current context. If no connection has yet been made, this method
creates one, and connects to the database.
This database handle is cached for future use: if you call
C<$database-E<gt>schema> twice, you will get the same handle both
times. If you need a second database handle, use C<&new_schema> and
possibly C<&set_schema>.
sub schema {
my $self = shift;
my $sth;
if ( defined( $database->{schema} ) and $database->{schema}->storage->connected() ) {
return $database->{schema};
# No database handle or it died . Create one.
$database->{schema} = &_new_schema();
return $database->{schema};
=head2 new_schema
$schema = $database->new_schema;
Creates a new connection to the Koha database for the current context,
and returns the database handle (a C<DBI::db> object).
The handle is not saved anywhere: this method is strictly a
convenience function; the point is that it knows which database to
connect to so that the caller doesn't have to know.
sub new_schema {
my $self = shift;
return &_new_schema();
=head2 set_schema
$my_schema = $database->new_schema;
C<&set_schema> and C<&restore_schema> work in a manner analogous to
C<&set_context> and C<&restore_context>.
C<&set_schema> saves the current database handle on a stack, then sets
the current database handle to C<$my_schema>.
C<$my_schema> is assumed to be a good database handle.
sub set_schema {
my $self = shift;
my $new_schema = shift;
# Save the current database handle on the handle stack.
# We assume that $new_schema is all good: if the caller wants to
# screw himself by passing an invalid handle, that's fine by
# us.
push @{ $database->{schema_stack} }, $database->{schema};
$database->{schema} = $new_schema;
=head2 restore_schema
Restores the database handle saved by an earlier call to
sub restore_schema {
my $self = shift;
if ( $#{ $database->{schema_stack} } < 0 ) {
# Stack underflow
die "SCHEMA stack underflow";
# Pop the old database handle and set it.
$database->{schema} = pop @{ $database->{schema_stack} };
# FIXME - If it is determined that restore_context should
# return something, then this function should, too.
=head2 EXPORT
None by default.
=head1 AUTHOR
Chris Cormack, E<lt><gt>