#!/usr/bin/perl -w package DB; use strict; use DBI; use SharedVariable qw ($action $dir $dirLang $dirError $imgdir $session_dir $session_id $can_do_gzip $current_ip $lang $LANG %ARTICLE %SESSION %SERVER %USER $CGISESSID %LABEL %ERROR %VALUE $COMMANDID %COMMAND %DATE %PAYPALL $INDEX %LINK $query $session $host $t0 $client); our $dbh = DBI->connect( "DBI:mysql:recordz:localhost", "root", "password",{ RaiseError => 1, AutoCommit => 1 } ); sub new { my $class = shift; my ($opts)= @_; my $self = {}; return bless $self, $class; } sub sqlConnect { local our $dbname = shift || ''; local our $dbusername = shift || ''; local our $dbpassword = shift || ''; $dbh = DBI->connect($dbname, $dbusername, $dbpassword); if (!$dbh) { } kill 9, $$ unless $dbh; } sub sqlSelect { local our $select = shift || ''; local our $from = shift || ''; local our $where = shift || ''; local our $other = shift || ''; local our $sql="SELECT $select "; $sql.="FROM $from " if $from; $sql.="WHERE $where " if $where; $sql.="$other" if $other; #$sql = $dbh->quote ($sql); #print "Content-Type: text/html\n\n"; #print "sql : $sql\n"; local our ($c)=$dbh->prepare($sql) or die "Sql has gone to hell\n"; if(not ($c->execute())) { local our $err=$dbh->errstr; return undef; } local our (@r)=$c->fetchrow(); $c->finish(); return @r; } sub sqlInsert { local our ($table,%data)=@_; local our ($names,$values); $dbh||=sqlConnect(); foreach (keys %data) { if (/^-/) {$values.="\n ".$data{$_}.","; s/^-//;} else { $values.="\n ".$dbh->quote($data{$_}).","; } $names.="$_,"; } chop($names); chop($values); local our $sql="INSERT INTO $table ($names) VALUES($values)\n"; #$sql = $dbh->quote ($sql); #print "Content-Type: text/html\n\n"; #print "$sql
"; if(!$dbh->do($sql)) { local our $err=$dbh->errstr; } } sub sqlUpdate { local our ($table, $where, %data)=@_; local our $sql="UPDATE $table SET"; foreach (keys %data) { if (/^-/) { s/^-//; $sql.=" $_ = $data{-$_} " . ","; } else { $sql.=" $_ = ".$dbh->quote($data{$_}).","; } } chop($sql); $sql.=" WHERE $where "; # $sql = $dbh->quote ($sql); # print "Content-Type: text/html\n\n"; # print "$sql"; if(!$dbh->do($sql)) { local our $err=$dbh->errstr; } } sub sqlSelectMany { local our $select = shift || ''; local our $from = shift || ''; local our $where = shift || ''; local our $other = shift || ''; local our $sql="SELECT $select "; $sql.="FROM $from " if $from; $sql.="WHERE $where " if $where; $sql.="$other" if $other; #print "Content-Type: text/html\n\n"; #print "$sql"; #$sql = $dbh->quote ($sql); local our $c=$dbh->prepare($sql); if($c->execute()) { return $c; } else { $c->finish(); local our $err=$dbh->errstr; return undef; kill 9,$$ } } sub sqlDelete { local our $fromtable = shift || ''; local our $condition = shift || ''; local our $sql = ''; if ($condition) { $sql = "DELETE from $fromtable WHERE $condition"; } else { $sql = "DELETE from $fromtable"; } #print "Content-Type: text/html\n\n"; #print "$sql"; #$sql = $dbh->quote ($sql); if (!$dbh->do($sql)) { local our $err=$dbh->errstr; } } BEGIN { use Exporter (); @DB::ISA = qw(Exporter); @DB::EXPORT = qw(); @DB::EXPORT_OK = qw(new sqlConnect sqlSelect sqlInsert sqlUpdate sqlSelectMany sqlDelete); } 1;