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

View of /FigKernelPackages/DBrtns.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (download) (as text) (annotate)
Thu Mar 2 22:19:45 2006 UTC (13 years, 8 months ago) by overbeek
Branch: MAIN
CVS Tags: mgrast_dev_08112011, rast_rel_2009_05_18, mgrast_dev_08022011, rast_rel_2014_0912, rast_rel_2008_06_18, myrast_rel40, rast_rel_2008_06_16, mgrast_dev_05262011, rast_rel_2008_12_18, mgrast_dev_04082011, rast_rel_2008_07_21, rast_rel_2010_0928, rast_2008_0924, mgrast_version_3_2, mgrast_dev_12152011, rast_rel_2008_04_23, mgrast_dev_06072011, rast_rel_2008_09_30, rast_rel_2009_0925, rast_rel_2010_0526, rast_rel_2014_0729, mgrast_dev_02212011, rast_rel_2010_1206, mgrast_release_3_0, mgrast_dev_03252011, rast_rel_2010_0118, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, rast_rel_2009_02_05, rast_rel_2011_0119, mgrast_rel_2008_0625, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, rast_rel_2008_10_09, mgrast_dev_04012011, rast_release_2008_09_29, mgrast_rel_2008_0806, mgrast_rel_2008_0923, mgrast_rel_2008_0919, rast_rel_2009_07_09, rast_rel_2010_0827, mgrast_rel_2008_1110, myrast_33, rast_rel_2011_0928, rast_rel_2008_09_29, mgrast_rel_2008_0917, rast_rel_2008_10_29, mgrast_dev_04052011, mgrast_dev_02222011, rast_rel_2009_03_26, mgrast_dev_10262011, rast_rel_2008_11_24, rast_rel_2008_08_07, HEAD
Changes since 1.26: +9 -5 lines
Fix extra-args option to go at teh start of the list.

#
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
# 
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License. 
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#

package DBrtns;

    # Inherit the DBKernel methods. We must do this BEFORE the "use strict".
    use DBKernel;
    @ISA = qw(DBKernel);

use strict;
use POSIX;
use DBI;
use FIG_Config;

use Data::Dumper;
use Carp;

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

    $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;
    $dbport = defined($dbport) ? $dbport : $FIG_Config::dbport;
    $dbhost = defined($dbhost) ? $dbhost : $FIG_Config::dbhost;
    $dbsock = defined($dbsock) ? $dbsock : $FIG_Config::dbsock;

    return DBKernel::new($class, $dbms, $dbname, $dbuser, $dbpass, $dbport, $dbhost, $dbsock);
}

=head1 get_inserted_id

Return the last ID of a row inserted into an autonumber/serial-containing table.

=cut

sub get_inserted_id {
    my($self, $table, $sth, $id_column) = @_;

    $id_column = 'id' unless defined($id_column);
    if ($self->{_dbms} eq "Pg") {
        my $oid = $sth->{pg_oid_status};
        my $ret = $self->SQL("select $id_column from $table where oid = ?", undef, $oid);
        return $ret->[0]->[0];
    } elsif ($self->{_dbms} eq "mysql") {
        my $id = $self->{_dbh}->{mysql_insertid};
        # print "mysql got $id\n";
        return $id;
    }
    else
    {
	confess "Attempting get_inserted_id on unsupported database $self->{_dbms}\n";
    }
}

#
# Following are database administration routines. They create an instance of a ServerAdmin class
# for the appropriate server type (in order to eliminate the if mysql / if pg / etc stuff).
#

sub get_server_admin
{
    if ($FIG_Config::dbms eq "mysql")
    {
	return MysqlAdmin->new();
    }
    elsif ($FIG_Config::dbms eq "Pg")
    {
	return new PostgresAdmin();
    }
    else
    {
	warn "Unknown server type $FIG_Config::dbms\n";
	return undef;
    }
}
package MysqlAdmin;

use POSIX;
use DBI;

sub new
{
    my($class) = @_;

    my $self = {};

    return bless($self, $class);
}

sub init_db
{
    my($self, $db_dir) = @_;

    if (!$db_dir)
    {
	warn "init_db failed: db_dir must be provided\n";
	return;
    }

    if (-d "$db_dir/mysql")
    {
	warn "init_db: mysql data directory already exists\n";
	return;
    }

    my $exe = "$FIG_Config::ext_bin/mysql_install_db";
    if (! -x $exe)
    {
	$exe = "mysql_install_db";
    }


    my @opts;

    push(@opts, "--datadir=$db_dir");
    push(@opts, "--user=$FIG_Config::dbuser");

    if (not $FIG_Config::use_system_mysql)
    {
	push(@opts, "--basedir=$FIG_Config::common_runtime")
    }


    my $rc = system($exe, @opts);
    if ($rc != 0)
    {
	my $err = $?;
	if (WIFEXITED($err))
	{
	    my $exitstat = WEXITSTATUS($err);
	    warn "init_db failed: $exe returned result code $exitstat\n";
	}
	else
	{
	    warn "init_db failed: $exe died with signal ", WTERMSIG($err), "\n";
	}
	return;
    }

    return 1;
}

sub create_database
{
    my($self, $db_name) = @_;

    my $drh = DBI->install_driver("mysql");

    my @dbs = DBI->data_sources("mysql", { host => $FIG_Config::dbhost,
					       user => $FIG_Config::dbuser,
					       password => $FIG_Config::dbpass });
    if (grep { $_ eq $db_name } @dbs)
    {
	warn "Database $db_name already exists\n";
	return;
    }

    my $rc = $drh->func('createdb', $db_name, $FIG_Config::dbhost,
			$FIG_Config::dbuser, $FIG_Config::dbpass, 'admin');


    if (!$rc)
    {
	warn "create_database: createdb call failed: $DBI::errstr\n";
	return;
    }

    return 1;
}

sub start_server
{
    my($self, $dont_fork) = @_;

    print "Starting mysql server\n";

    my(@opts);

    my $cnf = "$FIG_Config::fig_disk/config/my.cnf";

    if ($FIG_Config::use_system_mysql)
    {
	#
	# This has to be first in the argument list.
	#
	push(@opts, "--defaults-extra-file=$cnf");
    }

    #
    # Put this first, so  config can put --defaults-extra-file here 
    # and have it show up first.
    #
    if (@FIG_Config::db_server_startup_options)
    {
	push(@opts, @FIG_Config::db_server_startup_options)
    }

    push(@opts, "--port=$FIG_Config::dbport");
    #
    # Don't do this; dbuser isn't the unix uid that we are using.
    #
    #push(@opts, "--user=$FIG_Config::dbuser");

    push(@opts, "--datadir=$FIG_Config::db_datadir");

    if ($FIG_Config::use_system_mysql)
    {
	push(@opts, "--err-log=$FIG_Config::temp/mysql.log");
	push(@opts, "--socket=$FIG_Config::dbsock");

	#
	# Feh. You can't actually override the socket that /etc/my.cnf 
	# sets up, so we need to set up a config/my.cnf with the socket in it.
	#

	if (! -f $cnf) 
	{
	    if (open(F, ">$cnf"))
	    {
		print F <<END;
[mysqld]
socket=$FIG_Config::dbsock
END
		close(F);
	    }
	}
    }
    else
    {
	push(@opts, "--basedir=$FIG_Config::common_runtime");
	push(@opts, "--ledir=$FIG_Config::common_runtime/libexec");
    }

    if (not $FIG_Config::mysql_v3)
    {
	push(@opts, "--old-password");
	push(@opts, "--max-allowed-packet=128M");
    }

    #
    # Use InnoDB for large-table support and allegedly better performance.
    #
    
    #push(@opts, "--default-table-type=innodb");
    
    #
    # Oddly, this doesn't seem to work. need to set the environment variable.
    #
    #push(@opts, "--port=$FIG_Config::dbport");

    #
    # We are going to assume that if mysql has shipped with this release, we'll use it. Otherwise
    # try to use a system one.
    #

    my $exe;
    if ($FIG_Config::mysql_v3)
    {
	$exe = "safe_mysqld";
    }
    else
    {
	$exe = "mysqld_safe";
    }

    if (-x "$FIG_Config::ext_bin/$exe")
    {
	$exe = "$FIG_Config::ext_bin/$exe";
    }

    print "Start $exe @opts\n";

    if ($dont_fork)
    {
	$ENV{MYSQL_TCP_PORT} = $FIG_Config::dbport;
	exec $exe, @opts;
    }
    else
    {
	my $pid = fork;

	if ($pid == 0)
	{
	    POSIX::setsid();

	    $ENV{MYSQL_TCP_PORT} = $FIG_Config::dbport;
	    exec $exe, @opts;
	}
	print "Forked db server $pid\n";
    }

}

1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3