User Tools

Site Tools


Old code

pgmaster.pl
package PGmaster::Controller;
...
 
sub storeProfile {
    my ($self, $id)  = @_;
    return undef unless $id;
    my $dbi = DBI->connect($self->dsn, $self->dbuser, $self->dbpasswd)
        or return undef;
    my $query = "select * from store where id = $id limit 1;";
    my $sth = $dbi->prepare($query);
    my $rows = $sth->execute;
    my $row = $sth->fetchrow_hashref;
    $sth->finish;
    $dbi->disconnect;
    return $row if $row;
    return undef;
}
 
sub storeInfo {
    my ($self, $id)  = @_;
    return undef unless $id;
    my $dbi = DBI->connect($self->dsn, $self->dbuser, $self->dbpasswd)
        or return undef;
    my $query = "select sum(d.size) as sum , count(d.name) as count
                    from store s, data d
                    where s.id = d.storeid and s.id = $id;";
    my $sth = $dbi->prepare($query);
    my $rows = $sth->execute;
    my $row = $sth->fetchrow_hashref;
    $sth->finish;
    $dbi->disconnect;
    $row->{'sum'} ||= 0;
    return $row if $row;
    return undef;
 
...
}

Code with ::DB

pgmaster.pl
package PGmaster::Controller;
...
sub store_profile {
    my ($self, $id)  = @_;
    return undef unless $id;
    my $query = "select * from store where id = $id limit 1";
    $self->db->exec1($query);
}
 
sub store_info {
    my ($self, $id)  = @_;
    return undef unless $id;
    my $query = "select sum(d.size) as sum , count(d.name) as count
                    from store s, data d
                    where s.id = d.storeid and s.id = $id limit 1";
    my $row = $self->db->exec1($query);
    return undef unless $row;
    $row->{'sum'} ||= 0;
    $row;
}
...

DB object

::DB
package PGmaster::DB;
 
use strict;
use warnings;
use DBI;
 
sub new {
    my ($class, %args) = @_;
    my $self = {
        hostname => $args{hostname},
        username => $args{username},
        password => $args{password},
        database => $args{database},
        engine => 'Pg',
        error => ''
    };
    bless $self, $class;
    return $self;
}
 
sub exec {
    my ($self, $query) = @_;
    return undef unless $query;
 
    my $dsn = 'dbi:'.$self->engine.
                ':dbname='.$self->database.
                ';host='.$self->hostname;
    my $dbi;
    eval {
        $dbi = DBI->connect($dsn, $self->username, $self->password, { 
            RaiseError => 1,
            PrintError => 0,
            AutoCommit => 1 
        });
    };
    $self->error($@);
    return undef if $@;
 
    my $sth;
    eval {
        $sth = $dbi->prepare($query);
    };
    $self->error($@);
    return undef if $@;
 
    my $rows = $sth->execute;
    my @list;
 
    while (my $row = $sth->fetchrow_hashref) {
        push @list, $row;
    }
    $sth->finish;
    $dbi->disconnect;
    \@list;
}
 
sub exec1 {
    my ($self, $query) = @_;
    return undef unless $query;
 
    my $dsn = 'dbi:'.$self->engine.
                ':dbname='.$self->database.
                ';host='.$self->hostname;
    my $dbi;
    eval {
        $dbi = DBI->connect($dsn, $self->username, $self->password, { 
            RaiseError => 1,
            PrintError => 0,
            AutoCommit => 1 
        });
    };
    $self->error($@);
    return undef if $@;
 
    my $sth;
    eval {
        $sth = $dbi->prepare($query);
    };
    $self->error($@);
    return undef if $@;
 
    my $rows = $sth->execute;
    my $row = $sth->fetchrow_hashref;
 
    $sth->finish;
    $dbi->disconnect;
    $row;
}
 
sub do {
    my ($self, $query) = @_;
    return undef unless $query;
    my $dsn = 'dbi:'.$self->engine.
                ':dbname='.$self->database.
                ';host='.$self->hostname;
    my $dbi;
    eval {
        $dbi = DBI->connect($dsn, $self->username, $self->password, { 
            RaiseError => 1,
            PrintError => 0,
            AutoCommit => 1 
        });
    };
    $self->error($@);
    return undef if $@;
    my $rows;
    eval {
        $rows = $dbi->do($query) or return undef;
    };
    $self->error($@);
    return undef if $@;
 
    $dbi->disconnect;
    $rows*1;
}
 
sub username {
    my ($self, $username) = @_; 
    return $self->{username} unless $username;
    $self->{username} = $username;
    $self;
}
 
sub password {
    my ($self, $password) = @_; 
    return $self->{password} unless $password;
    $self->{password} = $password;
    $self;
}
 
sub hostname {
    my ($self, $hostname) = @_; 
    return $self->{hostname} unless $hostname;
    $self->{hostname} = $hostname;
    $self;
}
 
sub database {
    my ($self, $database) = @_; 
    return $self->{database} unless $database;
    $self->{database} = $database;
    $self;
}
 
sub error {
    my ($self, $error) = @_; 
    return $self->{error} unless $error;
    $self->{error} = $error;
    $self;
}
 
sub engine {
    my ($self, $engine) = @_; 
    return $self->{engine} unless $engine;
    $self->{engine} = $engine;
    $self;
}
 
1;