| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | package Rex::Helper::DBI; | ||
| 6 | |||
| 7 | use v5.14.4; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | our $VERSION = '1.16.1'; # VERSION | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | use Rex::Require; | ||
| 14 | DBI->require; | ||
| 15 | } | ||
| 16 | |||
| 17 | my %db_connections; | ||
| 18 | |||
| 19 | sub perform_request { | ||
| 20 | |||
| 21 | my ( $dsn, $user, $pass, $req ) = @_; | ||
| 22 | |||
| 23 | $user ||= ""; | ||
| 24 | $pass ||= ""; | ||
| 25 | |||
| 26 | my $con_key = "$dsn-$user-$pass"; | ||
| 27 | |||
| 28 | if ( !exists $db_connections{$dsn} ) { | ||
| 29 | $db_connections{$con_key} = DBI->connect( $dsn, $user, $pass ) | ||
| 30 | or die("Cannot connect: $DBI::errstr\n"); | ||
| 31 | } | ||
| 32 | |||
| 33 | my %res; | ||
| 34 | |||
| 35 | my $sth = $db_connections{$con_key}->prepare($req); | ||
| 36 | $sth->execute(); | ||
| 37 | |||
| 38 | my $i = 1; | ||
| 39 | while ( my $ref = $sth->fetchrow_hashref() ) { | ||
| 40 | $res{$i} = $ref; | ||
| 41 | $i++; | ||
| 42 | } | ||
| 43 | |||
| 44 | return \%res; | ||
| 45 | } | ||
| 46 | |||
| 47 | 1; |