The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::Helper::DBI;

use strict;
use warnings;

our $VERSION = '1.1.0'; # VERSION

BEGIN {
  use Rex::Require;
  DBI->require;
}

my %db_connections;

sub perform_request {

  my ( $dsn, $user, $pass, $req ) = @_;

  $user ||= "";
  $pass ||= "";

  my $con_key = "$dsn-$user-$pass";

  if ( !exists $db_connections{$dsn} ) {
    $db_connections{$con_key} = DBI->connect( $dsn, $user, $pass )
      or die("Cannot connect: $DBI::errstr\n");
  }

  my %res;

  my $sth = $db_connections{$con_key}->prepare($req);
  $sth->execute();

  my $i = 1;
  while ( my $ref = $sth->fetchrow_hashref() ) {
    $res{$i} = $ref;
    $i++;
  }

  return \%res;
}

1;