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