[Bio] / FigKernelPackages / DBrtns.pm Repository:
ViewVC logotype

View of /FigKernelPackages/DBrtns.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (as text) (annotate)
Tue Jan 6 11:52:31 2004 UTC (15 years, 11 months ago) by overbeek
Branch: MAIN
Changes since 1.1: +3 -2 lines
translate auto_assignments

package DBrtns;

use strict;
use DBI;
use FIG_Config;

use Data::Dumper;
use Carp;

sub new {
    my($class,$dbms,$dbname,$dbuser,$dbpass,$dbport) = @_;

    $dbms   = defined($dbms)   ? $dbms   : $FIG_Config::dbms;
    $dbname = defined($dbname) ? $dbname : $FIG_Config::db;
    $dbuser = defined($dbuser) ? $dbuser : $FIG_Config::dbuser;
    $dbpass = defined($dbpass) ? $dbpass : $FIG_Config::dbpass;
    $dbpass = defined($dbpass) ? $dbpass : $FIG_Config::dbpass;
    $dbport = defined($dbport) ? $dbport : $FIG_Config::dbport;

    my $data_source = "DBI:$dbms(AutoCommit => 1):dbname=$dbname;port=$dbport";    
    my $dbh         = DBI->connect( $data_source, $dbuser, $dbpass )
	|| die "ERROR: Could not connect to $data_source; ", DBI->errstr, "\n"; 
    $dbh->{PrintError} = 1;
    $dbh->{RaiseError} = 0;
    if ($dbms eq "Pg")
    {
        $dbh->do(qq(SET "ENABLE_SEQSCAN" TO "OFF"));
        $dbh->do(qq(SET DATESTYLE TO Postgres,US));
    }

    bless {
	_dbh => $dbh,
        _dbms => $dbms,
	}, $class;
}

sub SQL {
    my($self,$sql,$verbose) = @_;
    my($dbh,$sth,$rc,$tmp);

    if ($verbose)
    {
	print STDERR "running: $sql\n";
    }

    $dbh  = $self->{_dbh};
    
    if (($sql =~ /^select/i) && ($tmp = $dbh->selectall_arrayref($sql)))
    {
	return $tmp;
    }
    else
    {
        return $dbh->do($sql);
#	$sth = $dbh->prepare($sql)
#	    or die "prepare failed: $DBI::errstr";
#	$sth->execute()
#	    or warn "execute failed: $DBI::errstr";
#	return 1;
    }
    return undef;
}

sub drop_table {
    my $self = shift @_;
    my %arg  = @_;
    my $tbl  = $arg{tbl};
    my $dbh  = $self->{_dbh};
    my $dbms = $self->{_dbms};
    my $cmd;
    if ($dbms eq "mysql")  { $cmd = "DROP TABLE IF EXISTS $tbl;" ; }
    else                   { $cmd = "DROP TABLE $tbl;" ; }
    $dbh->do($cmd);
}

sub create_table {
    my $self = shift @_;
    my %arg  = @_;
    my $tbl  = $arg{tbl};
    my $flds = $arg{flds};
    my $dbh  = $self->{_dbh};
    my $dbms = $self->{_dbms};
    $dbh->do("CREATE TABLE $tbl ( $flds );");
}

sub load_table {
    my $self     = shift @_;
    my %defaults = ( delim => "\t" );
    my %arg      = (%defaults, @_);
    my $file     = $arg{file};
    my $tbl      = $arg{tbl};
    my $delim    = $arg{delim};
    my $dbh  = $self->{_dbh};
    my $dbms = $self->{_dbms};

    if ($file)
    {
        if ($dbms eq "mysql")
        {
            $dbh->do("LOAD DATA LOCAL INFILE '$file' REPLACE INTO TABLE $tbl;");
        }
        elsif ($dbms eq "Pg")
        {
            $dbh->do("COPY $tbl FROM '$file' USING DELIMITERS '$delim';");
        }
    }
}

sub create_index {
    my $self = shift @_;
    my %arg  = @_;
    my $tbl  = $arg{tbl};
    my $idx  = $arg{idx};
    my $flds = $arg{flds};
    my $type = $arg{type};
    my $dbh  = $self->{_dbh};
    my $dbms = $self->{_dbms};
    my $cmd  = "CREATE INDEX $idx ON $tbl ";
    if ($type && $dbms eq "Pg")
    {
        $cmd .= " USING $type ";
    }
    $cmd .= " ( $flds );";
    $dbh->do($cmd);
}

sub DESTROY {
    my($self) = @_;

    my($dbh);
    if ($dbh = $self->{_dbh})
    {
	$dbh->disconnect;
    }
}

sub vacuum_it {
    my($self,@tables) = @_;
    my($table);

    my $dbh  = $self->{_dbh};
    my $dbms = $self->{_dbms};
    if ($dbms eq "mysql")
    {
        return;
    }

    # this chunk is for Pg  (Postgres)
    if (@tables == 0)
    {
	$self->SQL("VACUUM ANALYZE");
    }
    else
    {
	foreach $table (@tables)
	{
	    $self->SQL("VACUUM ANALYZE $table");
	}
    }
}

1

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3