templating modifications to rspect finlay structure
[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 use strict;
24 require Exporter;
25 use DBI;
26 use vars qw($VERSION @ISA @EXPORT);
27   
28 $VERSION = 0.01;
29     
30 @ISA = qw(Exporter);
31 @EXPORT = qw(
32         &C4Connect &requireDBI &configfile
33 );
34
35 sub configfile {
36     my $configfile;
37     open (KC, "/etc/koha.conf");
38     while (<KC>) {
39         chomp;
40         (next) if (/^\s*#/);
41         if (/(.*)\s*=\s*(.*)/) {
42             my $variable=$1;
43             my $value=$2;
44             # Clean up white space at beginning and end
45             $variable=~s/^\s*//g;
46             $variable=~s/\s*$//g;
47             $value=~s/^\s*//g;
48             $value=~s/\s*$//g;
49             $configfile->{$variable}=$value;
50         }
51     }
52     return $configfile;
53 }
54
55 sub C4Connect  {
56   my $dbname="c4";
57    my ($database,$hostname,$user,$pass,%configfile);
58    open (KC, "/etc/koha.conf");
59    while (<KC>) {
60      chomp;
61      (next) if (/^\s*#/);
62      if (/(.*)\s*=\s*(.*)/) {
63        my $variable=$1;
64        my $value=$2;
65        # Clean up white space at beginning and end
66        $variable=~s/^\s*//g;
67        $variable=~s/\s*$//g;
68        $value=~s/^\s*//g;
69        $value=~s/\s*$//g;
70        $configfile{$variable}=$value;
71      }
72    }
73    $database=$configfile{'database'};
74    $hostname=$configfile{'hostname'};
75    $user=$configfile{'user'};
76    $pass=$configfile{'pass'};
77     
78    my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
79   return $dbh;
80 } # sub C4Connect
81
82 =item requireDBI
83
84   &requireDBI($dbh, $functionnname);
85
86 Verifies that C<$dbh> is a valid DBI::db database handle (presumably
87 to the Koha database). If it isn't, the function dies.
88
89 C<$functionname> is the name of the calling function, which will be
90 used in error messages.
91
92 =cut
93 #'
94 #------------------
95 # Helper subroutine to make sure database handle was passed properly
96 sub requireDBI {
97     my (
98         $dbh,
99         $subrname,      # name of calling subroutine
100                         # FIXME - Ought to get this with 'caller',
101                         # instead of requiring developers to always
102                         # get it right. Plus, it'd give the line
103                         # number.
104     )=@_;
105
106     unless ( ref($dbh) =~ /DBI::db/ ) {
107         print "<pre>\nERROR: Subroutine $subrname called without proper DBI handle.\n" .
108                 "Please contact system administrator.\n</pre>\n";
109         die "ERROR: Subroutine $subrname called without proper DBI handle.\n";
110     }
111 } # sub requireDBI
112
113
114 END { }
115
116 1;
117 __END__
118 =back
119
120 =head1 SEE ALSO
121
122 L<DBI(3)|DBI>
123
124 =cut