package Common::DB::Connection;

use strict;
use warnings;

use lib '/app/tools/common/lib';

use base 'Class::Accessor';

use Common::DB::Connect;

__PACKAGE__->mk_accessors(qw/dbh dsn rps dtmv misc/);

sub new {
    my $class = shift;
    my $conn  = shift;
    die __PACKAGE__ . '::new requies db conneciton variable' if not defined $conn;
    my $self = {};
    bless $self, $class;
    my $dsn = Common::DB::Connect->dsn($conn);
    $self->__init__($dsn);
    $self;
}

sub __init__ {
    my $self = shift;
    my $dsn  = shift;
    my $dbh  = DBI->connect( @{$dsn} ) || die "Conn: $DBI::errstr";
    my $rows = $dbh->selectcol_arrayref('show databases');
    foreach my $row ( @{$rows} ) {
        if ( $row =~ m/^C\_/ ) {
            push @{ $self->{'rps'} }, $row;
        } elsif ( $row =~ m/^BP\_/i ) {
            push @{ $self->{'dtmv'} }, $row;
        } else {
            push @{ $self->{'misc'} }, $row;
        }
    }
    $self->dbh($dbh);
    $self->dsn($dsn);
}

1;
