1 package C4::Database; #asummes C4/Database
3 #requires DBI.pm to be installed
9 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
11 # set the version for version checking
15 @EXPORT = qw(&C4Connect &sqlinsert &sqlupdate &getmax &makelist
17 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
19 # your exported package globals go here,
20 # as well as any optionally exported functions
22 @EXPORT_OK = qw($Var1 %Hashit);
25 # non-exported package globals go here
26 use vars qw(@more $stuff);
28 # initalize package globals, first exported ones
33 # then the others (which are still accessible as $Some::Module::stuff)
37 # all file-scoped lexicals must be created before
38 # the functions below that use them.
40 # file-private lexicals go here
44 # here's a file-private function as a closure,
45 # callable as &$priv_func; it cannot be prototyped.
50 # make all your functions, whether exported or not;
56 my ($database,$hostname,$user,$pass,%configfile);
57 open (KC, "/etc/koha.conf");
61 if (/(.*)\s*=\s*(.*)/) {
64 # Clean up white space at beginning and end
69 $configfile{$variable}=$value;
72 $database=$configfile{'database'};
73 $hostname=$configfile{'hostname'};
74 $user=$configfile{'user'};
75 $pass=$configfile{'pass'};
77 my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
83 # my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "chris", "");
84 my $database='c4test';
85 my $hostname='localhost';
88 my $dbh=DBI->connect("DBI:mysql:$database:$hostname",$user,$pass);
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,";
102 $query=$query." VALUES (";
103 while (my ($key,$value) = each %data){
104 if ($key ne 'type' && $key ne 'updtype'){
105 $query=$query."'$value',";
110 my $sth=$dbh->prepare($query);
117 my ($table,$keyfld,$keyval,%data)=@_;
119 my $query="UPDATE $table SET ";
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."' ";
130 my $fsets = join(",", @sets);
131 $query=$query.$fsets." WHERE $keyarr[0] = '$keyvalarr[0]'";
134 while ($i < $numkeys) {
135 $query=$query." AND $keyarr[$i] = '$keyvalarr[$i]'";
139 # $query=~ s/\,$/\)/;
141 my $sth=$dbh->prepare($query);
149 my ($table,$item)=@_;
151 my $sth=$dbh->prepare("Select max($item) from $table");
153 my $data=$sth->fetchrow_hashref;
160 my ($table,$kfld,$dfld)=@_;
163 my $sth=$dbh->prepare("Select $kfld,$dfld from $table order by $dfld");
165 while (my $drec=$sth->fetchrow_hashref) {
166 $data = $data."\t".$drec->{$kfld}."\t".$drec->{$dfld};
172 END { } # module clean-up code here (global destructor)