Commit 24f2295c authored by Matthieu Muffato's avatar Matthieu Muffato
Browse files

dbc_hash_to_cmd moved from db_cmd.pl to Utils.pm

It now takes a DBConnection object instead of a dbc_hash
parent 973b577f
......@@ -61,7 +61,7 @@ use Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;
#use Bio::EnsEMBL::Hive::DBSQL::DBConnection; # causes warnings that all exported functions have been redefined
use Exporter 'import';
our @EXPORT_OK = qw(stringify destringify dir_revhash parse_cmdline_options find_submodules load_file_or_module script_usage url2dbconn_hash go_figure_dbc report_versions throw);
our @EXPORT_OK = qw(stringify destringify dir_revhash parse_cmdline_options find_submodules load_file_or_module script_usage url2dbconn_hash go_figure_dbc report_versions throw dbc_to_cmd);
no warnings ('once'); # otherwise the next line complains about $Carp::Internal being used just once
$Carp::Internal{ (__PACKAGE__) }++;
......@@ -352,5 +352,81 @@ sub throw {
}
sub dbc_to_cmd {
my ($dbc, $executable, $prepend, $append, $sqlcmd, $to_params) = @_;
my $driver = $dbc->driver || 'mysql';
my $dbname = $dbc->dbname;
if($sqlcmd) {
if($sqlcmd =~ /(DROP\s+DATABASE(?:\s+IF\s+EXISTS)?\s*?)(?:\s+(\w+))?/i) {
$dbname = $2 if $2;
if($driver eq 'sqlite') {
return ['rm', '-f', $dbname];
} elsif(!$2) {
$sqlcmd = "$1 $dbname";
$dbname = '';
}
} elsif($sqlcmd =~ /(CREATE\s+DATABASE\s*?)(?:\s+(\w+))?/i ) {
$dbname = $2 if $2;
if($driver eq 'sqlite') {
return ['touch', $dbname];
} elsif(!$2) {
my %limits = ( 'mysql' => 64, 'pgsql' => 63 );
if (length($dbname) > $limits{$driver}) {
die "Database name '$dbname' is too long (> $limits{$driver}). Cannot create the database\n";
}
$sqlcmd = "$1 $dbname";
$dbname = '';
}
}
}
my @cmd;
if($driver eq 'mysql') {
$executable ||= 'mysql';
push @cmd, $executable unless $to_params;
push @cmd, @$prepend if ($prepend && @$prepend);
push @cmd, '-h'.$dbc->host if $dbc->host;
push @cmd, '-P'.$dbc->port if $dbc->port;
push @cmd, '-u'.$dbc->username if $dbc->username;
push @cmd, '-p'.$dbc->password if $dbc->password;
push @cmd, ('-e', $sqlcmd) if $sqlcmd;
push @cmd, $dbname if $dbname;
push @cmd, @$append if ($append && @$append);
} elsif($driver eq 'pgsql') {
$executable ||= 'psql';
push @cmd, ('env', "PGPASSWORD='$dbc->pass'") if ($to_params && $dbc->pass);
push @cmd, $executable unless $to_params;
push @cmd, @$prepend if ($prepend && @$prepend);
push @cmd, ('-h', $dbc->host) if defined($dbc->host);
push @cmd, ('-p', $dbc->port) if defined($dbc->port);
push @cmd, ('-U', $dbc->username) if defined($dbc->username);
push @cmd, ('-c', $sqlcmd) if $sqlcmd;
push @cmd, @$append if ($append && @$append);
push @cmd, $dbname if defined($dbname);
} elsif($driver eq 'sqlite') {
$executable ||= 'sqlite3';
die "sqlite requires a database (file) name\n" unless $dbname;
push @cmd, $executable unless $to_params;
push @cmd, @$prepend if ($prepend && @$prepend);
push @cmd, @$append if ($append && @$append);
push @cmd, $dbname;
push @cmd, $sqlcmd if $sqlcmd;
}
return \@cmd;
}
1;
......@@ -12,8 +12,9 @@ BEGIN {
}
use Getopt::Long;
use Bio::EnsEMBL::Hive::Utils::URL;
use Bio::EnsEMBL::Hive::Utils ('script_usage', 'report_versions');
use Bio::EnsEMBL::Hive::DBSQL::DBConnection;
use Bio::EnsEMBL::Hive::Utils ('script_usage', 'report_versions', 'dbc_to_cmd');
sub main {
......@@ -37,7 +38,7 @@ sub main {
'v|versions!' => \$report_versions,
);
my $dbc_hash;
my $dbc;
if($help) {
......@@ -75,24 +76,15 @@ sub main {
$dba = $dbas->[0];
}
my $dbc = $dba->dbc();
$dbc = $dba->dbc();
$dbc_hash = {
'driver' => $dbc->driver,
'host' => $dbc->host,
'port' => $dbc->port,
'user' => $dbc->username,
'pass' => $dbc->password,
'dbname' => $dbc->dbname,
};
} elsif($url) {
$dbc_hash = Bio::EnsEMBL::Hive::Utils::URL::parse( $url )
|| die "Could not parse URL '$url'";
$dbc = Bio::EnsEMBL::Hive::DBSQL::DBConnection->new( -url => $url );
} else {
script_usage(1);
}
my @cmd = @{ dbc_hash_to_cmd( $dbc_hash, $executable, \@prepend, \@append, $sqlcmd, $to_params ) };
my @cmd = @{ dbc_to_cmd( $dbc, $executable, \@prepend, \@append, $sqlcmd, $to_params ) };
my $flat_cmd = join(' ', map { ($_=~/^-?\w+$/) ? $_ : "\"$_\"" } @cmd);
......@@ -105,80 +97,6 @@ sub main {
}
}
sub dbc_hash_to_cmd {
my ($dbc_hash, $executable, $prepend, $append, $sqlcmd, $to_params) = @_;
my $driver = $dbc_hash->{'driver'} || 'mysql';
if($sqlcmd) {
if($sqlcmd =~ /(DROP\s+DATABASE(?:\s+IF\s+EXISTS)?\s*?)(?:\s+(\w+))?/i) {
my $dbname = $2 || $dbc_hash->{dbname};
if($driver eq 'sqlite') {
return ['rm', '-f', $dbname];
} elsif(!$2) {
$sqlcmd = "$1 $dbname";
$dbc_hash->{dbname} = '';
}
} elsif($sqlcmd =~ /(CREATE\s+DATABASE\s*?)(?:\s+(\w+))?/i ) {
my $dbname = $2 || $dbc_hash->{dbname};
if($driver eq 'sqlite') {
return ['touch', $dbname];
} elsif(!$2) {
my %limits = ( 'mysql' => 64, 'pgsql' => 63 );
if (length($dbname) > $limits{$driver}) {
die "Database name '$dbname' is too long (> $limits{$driver}). Cannot create the database\n";
}
$sqlcmd = "$1 $dbname";
$dbc_hash->{dbname} = '';
}
}
}
my @cmd;
if($driver eq 'mysql') {
$executable ||= 'mysql';
push @cmd, $executable unless $to_params;
push @cmd, @$prepend if ($prepend && @$prepend);
push @cmd, "-h$dbc_hash->{'host'}" if $dbc_hash->{'host'};
push @cmd, "-P$dbc_hash->{'port'}" if $dbc_hash->{'port'};
push @cmd, "-u$dbc_hash->{'user'}" if $dbc_hash->{'user'};
push @cmd, "-p$dbc_hash->{'pass'}" if $dbc_hash->{'pass'};
push @cmd, ('-e', $sqlcmd) if $sqlcmd;
push @cmd, $dbc_hash->{'dbname'} if $dbc_hash->{'dbname'};
push @cmd, @$append if ($append && @$append);
} elsif($driver eq 'pgsql') {
$executable ||= 'psql';
push @cmd, ('env', "PGPASSWORD='$dbc_hash->{'pass'}'") if ($to_params && $dbc_hash->{'pass'});
push @cmd, $executable unless $to_params;
push @cmd, @$prepend if ($prepend && @$prepend);
push @cmd, ('-h', $dbc_hash->{'host'}) if defined($dbc_hash->{'host'});
push @cmd, ('-p', $dbc_hash->{'port'}) if defined($dbc_hash->{'port'});
push @cmd, ('-U', $dbc_hash->{'user'}) if defined($dbc_hash->{'user'});
push @cmd, ('-c', $sqlcmd) if $sqlcmd;
push @cmd, @$append if ($append && @$append);
push @cmd, $dbc_hash->{'dbname'} if defined($dbc_hash->{'dbname'});
} elsif($driver eq 'sqlite') {
$executable ||= 'sqlite3';
die "sqlite requires a database (file) name\n" unless $dbc_hash->{dbname};
push @cmd, $executable unless $to_params;
push @cmd, @$prepend if ($prepend && @$prepend);
push @cmd, @$append if ($append && @$append);
push @cmd, $dbc_hash->{'dbname'};
push @cmd, $sqlcmd if $sqlcmd;
}
return \@cmd;
}
main();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment