Shifted Database connection variables out into a configuration file
[koha.git] / C4 / Database.pm
1 package C4::Database; #asummes C4/Database
2
3 #requires DBI.pm to be installed
4 #uses DBD:Pg
5
6 use strict;
7 require Exporter;
8 use DBI;
9 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
10   
11 # set the version for version checking
12 $VERSION = 0.01;
13     
14 @ISA = qw(Exporter);
15 @EXPORT = qw(&C4Connect &sqlinsert &sqlupdate &getmax &makelist
16 &OpacConnect);
17 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
18                   
19 # your exported package globals go here,
20 # as well as any optionally exported functions
21
22 @EXPORT_OK   = qw($Var1 %Hashit);
23
24
25 # non-exported package globals go here
26 use vars qw(@more $stuff);
27         
28 # initalize package globals, first exported ones
29
30 my $Var1   = '';
31 my %Hashit = ();
32                     
33 # then the others (which are still accessible as $Some::Module::stuff)
34 my $stuff  = '';
35 my @more   = ();
36         
37 # all file-scoped lexicals must be created before
38 # the functions below that use them.
39                 
40 # file-private lexicals go here
41 my $priv_var    = '';
42 my %secret_hash = ();
43                             
44 # here's a file-private function as a closure,
45 # callable as &$priv_func;  it cannot be prototyped.
46 my $priv_func = sub {
47   # stuff goes here.
48 };
49                                                     
50 # make all your functions, whether exported or not;
51
52
53
54 sub C4Connect  {
55   my $dbname="c4"; 
56    my ($database,$hostname,$user,$pass,%configfile);
57    open (KC, "/etc/koha.conf");
58    while (<KC>) {
59      chomp;
60      (next) if (/^\s*#/);
61      if (/(.*)\s*=\s*(.*)/) {
62        my $variable=$1;
63        my $value=$2;
64        # Clean up white space at beginning and end
65        $variable=~s/^\s*//g;
66        $variable=~s/\s*$//g;
67        $value=~s/^\s*//g;
68        $value=~s/\s*$//g;
69        $configfile{$variable}=$value;
70      }
71    }
72    $database=$configfile{'database'};
73    $hostname=$configfile{'hostname'};
74    $user=$configfile{'user'};
75    $pass=$configfile{'pass'};
76     
77    my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
78   return $dbh;
79 }    
80
81 sub Opaconnect  {
82   my $dbname="c4"; 
83 #  my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "chris", "");
84    my $database='c4test';
85    my $hostname='localhost';
86    my $user='hdl';
87    my $pass='testing';
88    my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
89   return $dbh;
90 }    
91
92 sub sqlinsert {
93   my ($table,%data)=@_;
94   my $dbh=C4Connect;
95   my $query="INSERT INTO $table \(";
96   while (my ($key,$value) = each %data){
97     if ($key ne 'type' && $key ne 'updtype'){
98       $query=$query."$key,";
99     }
100   }
101   $query=~ s/\,$/\)/;
102   $query=$query." VALUES (";
103   while (my ($key,$value) = each %data){
104     if ($key ne 'type' && $key ne 'updtype'){
105       $query=$query."'$value',";
106     }
107   }
108   $query=~ s/\,$/\)/;
109   print $query;
110   my $sth=$dbh->prepare($query);
111   $sth->execute;
112   $sth->finish;
113   $dbh->disconnect;
114 }
115
116 sub sqlupdate {
117   my ($table,$keyfld,$keyval,%data)=@_;
118   my $dbh=C4Connect;
119   my $query="UPDATE $table SET ";
120   my @sets;
121   my @keyarr = split("\t",$keyfld);
122   my @keyvalarr = split("\t",$keyval);
123   my $numkeys = @keyarr;
124   while (my ($key,$value) = each %data){
125     if (($key ne 'type')&&($key ne 'updtype')){
126       my $temp = " ".$key."='".$value."' "; 
127       push(@sets,$temp);
128     }
129   }
130   my $fsets = join(",", @sets);
131   $query=$query.$fsets." WHERE $keyarr[0] = '$keyvalarr[0]'";
132   if ($numkeys > 1) {
133     my $i = 1;
134     while ($i < $numkeys) {
135       $query=$query." AND $keyarr[$i] = '$keyvalarr[$i]'";
136       $i++;
137     }
138   }  
139 #  $query=~ s/\,$/\)/;
140   print $query;
141   my $sth=$dbh->prepare($query);
142   $sth->execute;
143   $sth->finish;
144   $dbh->disconnect;
145 }
146
147
148 sub getmax {
149   my ($table,$item)=@_;
150   my $dbh=C4Connect;
151   my $sth=$dbh->prepare("Select max($item) from $table");
152   $sth->execute;
153   my $data=$sth->fetchrow_hashref;
154   $sth->finish;
155   $dbh->disconnect;
156   return($data);
157 }
158
159 sub makelist {
160   my ($table,$kfld,$dfld)=@_;
161   my $data;
162   my $dbh=C4Connect;
163   my $sth=$dbh->prepare("Select $kfld,$dfld from $table order by $dfld");
164   $sth->execute;
165   while (my $drec=$sth->fetchrow_hashref) {
166     $data = $data."\t".$drec->{$kfld}."\t".$drec->{$dfld};
167   }     
168   $sth->finish;
169   $dbh->disconnect;
170   return($data);
171 }
172 END { }       # module clean-up code here (global destructor)