###################################################################### # # Package: MyDBI # MyDBI::db # MyDBI::st # # Purpose: Subclassing the DBI module # ###################################################################### package vgapas::MyDBI; @ISA = qw(DBI); # inherit connect etc package vgapas::MyDBI::db; @ISA = qw(DBI::db); sub prepare { my($dbh, @args) = @_; my $sth = $dbh->SUPER::prepare(@args); return $sth; } package vgapas::MyDBI::st; @ISA = qw(DBI::st); sub fetch { my($sth, @args) = @_; my $row = $sth->SUPER::fetch(@args); return $row; } ###################################################################### # # Package: vgapas::GenericDB # # Purpose: A class for cgi field retrieval and storage # ###################################################################### package vgapas::GenericDB; @ISA = qw(vgapas::MyDBI); $VERSION = 1.00; use Carp; use Carp qw(cluck); use Data::Dumper; sub new { my ($class, @argz) = @_; # Eval connection block and croak on failure my $dbh = eval { vgapas::MyDBI->connect("DBI:mysql::localhost:3306", '', '', {PrintError => 0, RaiseError => 0} ); }; if ($dbh->err) {croak "Error connecting to db: $dbh->errstr\n";} bless {_dbh=>$dbh}, $class; } sub fetchall { my($self, $sql, @args) = @_; my $dbh = $self->{_dbh}; my @row_data; eval { my $sth = $dbh->prepare($sql); $sth->execute(); while ((my @row) = $sth->fetchrow()) { push(@row_data, \@row); } }; if ($@) {croak "fetchall failed".join("\n", $@)."\n$sql\n";} return \@row_data; } # other methods for retrieving data here