<commit_before>#!/usr/bin/env perl

use strict;
use warnings;

    # Finding out own path in order to reference own components (including own modules):
use Cwd            ();
use File::Basename ();
BEGIN {
    $ENV{'EHIVE_ROOT_DIR'} ||= File::Basename::dirname( File::Basename::dirname( Cwd::realpath($0) ) );
    unshift @INC, $ENV{'EHIVE_ROOT_DIR'}.'/modules';
}

use Getopt::Long;

use Bio::EnsEMBL::Hive::DBSQL::DBConnection;
use Bio::EnsEMBL::Hive::Utils ('script_usage', 'report_versions');


sub main {
    my ($reg_conf, $reg_type, $reg_alias, $executable, $url, @prepend, @append, $sqlcmd, $verbose, $help, $report_versions);

    GetOptions(
                # connect to the database:
            'reg_conf=s'        => \$reg_conf,
            'reg_type=s'        => \$reg_type,
            'reg_alias=s'       => \$reg_alias,

            'exec|executable=s' => \$executable,
            'url=s'             => \$url,
            'prepend=s@'        => \@prepend,
            'append|extra=s@'   => \@append,
            'sqlcmd=s'          => \$sqlcmd,

            'verbose!'          => \$verbose,
            'help!'             => \$help,
            'v|versions!'       => \$report_versions,
    );

    my $dbc;

    if($help) {

        script_usage(0);

    } elsif($report_versions) {

        report_versions();
        exit(0);

    } elsif($reg_alias) {
        script_usage(1) if $url;

        require Bio::EnsEMBL::Registry;
        Bio::EnsEMBL::Registry->load_all($reg_conf);

        my $species = Bio::EnsEMBL::Registry->get_alias($reg_alias)
            || die "Could not solve the alias '$reg_alias'".($reg_conf ? " via the registry file '$reg_conf'" : "");

        my $dba;
        if ($reg_type) {
            $dba = Bio::EnsEMBL::Registry->get_DBAdaptor($species, $reg_type)
                || die "Could not find any database for '$species' (alias: '$reg_alias') with the type '$reg_type'".($reg_conf ? " via the registry file '$reg_conf'" : "");

        } else {

            my $dbas = Bio::EnsEMBL::Registry->get_all_DBAdaptors(-species => $species);
            if (scalar(@$dbas) == 0) {
                # I think this case cannot happen: if there are no databases, the alias does not exist and get_alias() should have failed
                die "Could not find any database for '$species' (alias: '$reg_alias')".($reg_conf ? " via the registry file '$reg_conf'" : "");

            } elsif (scalar(@$dbas) >= 2) {
                die "There are several databases for '$species' (alias: '$reg_alias'). Please set -reg_type to one of: ".join(", ", map {$_->group} @$dbas);
            };
            $dba = $dbas->[0];
        }

        $dbc = $dba->dbc();

    } elsif($url) {
        $dbc = Bio::EnsEMBL::Hive::DBSQL::DBConnection->new( -url => $url );
    } else {
        script_usage(1);
    }

    if (@append) {
        warn qq{In db_cmd.pl, final arguments don't have to be declared with --append any more. All the remaining arguments are considered to be appended.\n};
    }

    my @cmd = @{ $dbc->to_cmd( $executable, \@prepend, [@append, @ARGV], $sqlcmd ) };

    if( $verbose ) {
        my $flat_cmd = join(' ', map { ($_=~/^-?\w+$/) ? $_ : "\"$_\"" } @cmd);

        warn "\nThe actual command I am running is:\n\t$flat_cmd\n\n";
    }

    exec(@cmd);
}


main();

__DATA__

=pod

=head1 NAME<commit_msg>bugfix: make sure ->to_cmd() method works for non-Hive DBConnections<commit_after>#!/usr/bin/env perl

use strict;
use warnings;

    # Finding out own path in order to reference own components (including own modules):
use Cwd            ();
use File::Basename ();
BEGIN {
    $ENV{'EHIVE_ROOT_DIR'} ||= File::Basename::dirname( File::Basename::dirname( Cwd::realpath($0) ) );
    unshift @INC, $ENV{'EHIVE_ROOT_DIR'}.'/modules';
}

use Getopt::Long;

use Bio::EnsEMBL::Hive::DBSQL::DBConnection;
use Bio::EnsEMBL::Hive::Utils ('script_usage', 'report_versions');


sub main {
    my ($reg_conf, $reg_type, $reg_alias, $executable, $url, @prepend, @append, $sqlcmd, $verbose, $help, $report_versions);

    GetOptions(
                # connect to the database:
            'reg_conf=s'        => \$reg_conf,
            'reg_type=s'        => \$reg_type,
            'reg_alias=s'       => \$reg_alias,

            'exec|executable=s' => \$executable,
            'url=s'             => \$url,
            'prepend=s@'        => \@prepend,
            'append|extra=s@'   => \@append,
            'sqlcmd=s'          => \$sqlcmd,

            'verbose!'          => \$verbose,
            'help!'             => \$help,
            'v|versions!'       => \$report_versions,
    );

    my $dbc;

    if($help) {

        script_usage(0);

    } elsif($report_versions) {

        report_versions();
        exit(0);

    } elsif($reg_alias) {
        script_usage(1) if $url;

        require Bio::EnsEMBL::Registry;
        Bio::EnsEMBL::Registry->load_all($reg_conf);

        my $species = Bio::EnsEMBL::Registry->get_alias($reg_alias)
            || die "Could not solve the alias '$reg_alias'".($reg_conf ? " via the registry file '$reg_conf'" : "");

        my $dba;
        if ($reg_type) {
            $dba = Bio::EnsEMBL::Registry->get_DBAdaptor($species, $reg_type)
                || die "Could not find any database for '$species' (alias: '$reg_alias') with the type '$reg_type'".($reg_conf ? " via the registry file '$reg_conf'" : "");

        } else {

            my $dbas = Bio::EnsEMBL::Registry->get_all_DBAdaptors(-species => $species);
            if (scalar(@$dbas) == 0) {
                # I think this case cannot happen: if there are no databases, the alias does not exist and get_alias() should have failed
                die "Could not find any database for '$species' (alias: '$reg_alias')".($reg_conf ? " via the registry file '$reg_conf'" : "");

            } elsif (scalar(@$dbas) >= 2) {
                die "There are several databases for '$species' (alias: '$reg_alias'). Please set -reg_type to one of: ".join(", ", map {$_->group} @$dbas);
            };
            $dba = $dbas->[0];
        }

        $dbc = bless $dba->dbc, 'Bio::EnsEMBL::Hive::DBSQL::DBConnection';

    } elsif($url) {
        $dbc = Bio::EnsEMBL::Hive::DBSQL::DBConnection->new( -url => $url );
    } else {
        script_usage(1);
    }

    if (@append) {
        warn qq{In db_cmd.pl, final arguments don't have to be declared with --append any more. All the remaining arguments are considered to be appended.\n};
    }

    my @cmd = @{ $dbc->to_cmd( $executable, \@prepend, [@append, @ARGV], $sqlcmd ) };

    if( $verbose ) {
        my $flat_cmd = join(' ', map { ($_=~/^-?\w+$/) ? $_ : "\"$_\"" } @cmd);

        warn "\nThe actual command I am running is:\n\t$flat_cmd\n\n";
    }

    exec(@cmd);
}


main();

__DATA__

=pod

=head1 NAME
{'fraction_non_alphanumeric': 0.17144948755490483, 'fraction_numerical': 0.0026354319180087846, 'mean_word_length': 2.2920481927710843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '575556', 'n_tokens_mistral': 2350, 'n_tokens_neox': 2236, 'n_words': 545}