Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / C4 / Database.pm
1 package C4::Database; #assumes C4/Database
2
3 #requires DBI.pm to be installed
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 # NOTE:
24 # C4::Database::C4Connect has been superseded by C4::Context->dbh;
25
26 # FIXME
27 # If C4::Biblio::OLD_MAY_BE_DELETED_newcompletebiblioitem can, in
28 # fact, be deleted, then it should be. Then C4::Biblio::getoraddbiblio
29 # won't be used anywhere, and it can be deleted too. That'll make two
30 # fewer functions that use C4::Database::requireDBI.
31
32 use strict;
33 require Exporter;
34 use DBI;
35 use vars qw($VERSION @ISA @EXPORT);
36   
37 $VERSION = 0.01;
38     
39 @ISA = qw(Exporter);
40 @EXPORT = qw(
41         &C4Connect &requireDBI
42 );
43
44 sub C4Connect  {
45   my $dbname="c4";
46    my ($database,$hostname,$user,$pass,%configfile);
47    open (KC, "/etc/koha.conf");
48    while (<KC>) {
49      chomp;
50      (next) if (/^\s*#/);
51      if (/(.*)\s*=\s*(.*)/) {
52        my $variable=$1;
53        my $value=$2;
54        # Clean up white space at beginning and end
55        $variable=~s/^\s*//g;
56        $variable=~s/\s*$//g;
57        $value=~s/^\s*//g;
58        $value=~s/\s*$//g;
59        $configfile{$variable}=$value;
60      }
61    }
62    $database=$configfile{'database'};
63    $hostname=$configfile{'hostname'};
64    $user=$configfile{'user'};
65    $pass=$configfile{'pass'};
66     
67    my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
68   return $dbh;
69 } # sub C4Connect
70
71 =item requireDBI
72
73   &requireDBI($dbh, $functionnname);
74
75 Verifies that C<$dbh> is a valid DBI::db database handle (presumably
76 to the Koha database). If it isn't, the function dies.
77
78 C<$functionname> is the name of the calling function, which will be
79 used in error messages.
80
81 =cut
82 #'
83 #------------------
84 # Helper subroutine to make sure database handle was passed properly
85 sub requireDBI {
86     my (
87         $dbh,
88         $subrname,      # name of calling subroutine
89                         # FIXME - Ought to get this with 'caller',
90                         # instead of requiring developers to always
91                         # get it right. Plus, it'd give the line
92                         # number.
93     )=@_;
94
95     unless ( ref($dbh) =~ /DBI::db/ ) {
96         print "<pre>\nERROR: Subroutine $subrname called without proper DBI handle.\n" .
97                 "Please contact system administrator.\n</pre>\n";
98         die "ERROR: Subroutine $subrname called without proper DBI handle.\n";
99     }
100 } # sub requireDBI
101
102
103 END { }
104
105 1;
106 __END__
107 =back
108
109 =head1 SEE ALSO
110
111 L<DBI(3)|DBI>
112
113 =cut