Initial revision
[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 $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "chris", "");
57    my $database='c4test';
58    my $hostname='localhost';
59    my $user='hdl';
60    my $pass='testing';
61    my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
62   return $dbh;
63 }    
64
65 sub Opaconnect  {
66   my $dbname="c4"; 
67 #  my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "chris", "");
68    my $database='c4test';
69    my $hostname='localhost';
70    my $user='hdl';
71    my $pass='testing';
72    my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
73   return $dbh;
74 }    
75
76 sub sqlinsert {
77   my ($table,%data)=@_;
78   my $dbh=C4Connect;
79   my $query="INSERT INTO $table \(";
80   while (my ($key,$value) = each %data){
81     if ($key ne 'type' && $key ne 'updtype'){
82       $query=$query."$key,";
83     }
84   }
85   $query=~ s/\,$/\)/;
86   $query=$query." VALUES (";
87   while (my ($key,$value) = each %data){
88     if ($key ne 'type' && $key ne 'updtype'){
89       $query=$query."'$value',";
90     }
91   }
92   $query=~ s/\,$/\)/;
93   print $query;
94   my $sth=$dbh->prepare($query);
95   $sth->execute;
96   $sth->finish;
97   $dbh->disconnect;
98 }
99
100 sub sqlupdate {
101   my ($table,$keyfld,$keyval,%data)=@_;
102   my $dbh=C4Connect;
103   my $query="UPDATE $table SET ";
104   my @sets;
105   my @keyarr = split("\t",$keyfld);
106   my @keyvalarr = split("\t",$keyval);
107   my $numkeys = @keyarr;
108   while (my ($key,$value) = each %data){
109     if (($key ne 'type')&&($key ne 'updtype')){
110       my $temp = " ".$key."='".$value."' "; 
111       push(@sets,$temp);
112     }
113   }
114   my $fsets = join(",", @sets);
115   $query=$query.$fsets." WHERE $keyarr[0] = '$keyvalarr[0]'";
116   if ($numkeys > 1) {
117     my $i = 1;
118     while ($i < $numkeys) {
119       $query=$query." AND $keyarr[$i] = '$keyvalarr[$i]'";
120       $i++;
121     }
122   }  
123 #  $query=~ s/\,$/\)/;
124   print $query;
125   my $sth=$dbh->prepare($query);
126   $sth->execute;
127   $sth->finish;
128   $dbh->disconnect;
129 }
130
131
132 sub getmax {
133   my ($table,$item)=@_;
134   my $dbh=C4Connect;
135   my $sth=$dbh->prepare("Select max($item) from $table");
136   $sth->execute;
137   my $data=$sth->fetchrow_hashref;
138   $sth->finish;
139   $dbh->disconnect;
140   return($data);
141 }
142
143 sub makelist {
144   my ($table,$kfld,$dfld)=@_;
145   my $data;
146   my $dbh=C4Connect;
147   my $sth=$dbh->prepare("Select $kfld,$dfld from $table order by $dfld");
148   $sth->execute;
149   while (my $drec=$sth->fetchrow_hashref) {
150     $data = $data."\t".$drec->{$kfld}."\t".$drec->{$dfld};
151   }     
152   $sth->finish;
153   $dbh->disconnect;
154   return($data);
155 }
156 END { }       # module clean-up code here (global destructor)