File Coverage

blib/lib/Prancer/Database/Driver/Pg.pm
Criterion Covered Total %
statement 21 48 43.8
branch 0 14 0.0
condition 0 3 0.0
subroutine 7 10 70.0
pod 0 1 0.0
total 28 76 36.8


line stmt bran cond sub pod time code
1             package Prancer::Database::Driver::Pg;
2              
3 4     4   2441 use strict;
  4         4  
  4         117  
4 4     4   10 use warnings FATAL => 'all';
  4         2  
  4         93  
5              
6 4     4   8 use Prancer::Database::Driver;
  4         5  
  4         61  
7 4     4   9 use parent qw(Prancer::Database::Driver);
  4         4  
  4         14  
8              
9 4     4   172 use Carp;
  4         4  
  4         160  
10 4     4   11 use Try::Tiny;
  4         2  
  4         164  
11 4     4   9 use Prancer qw(logger);
  4         4  
  4         1315  
12              
13             sub new {
14 0     0 0       my $class = shift;
15 0               my $self = bless($class->SUPER::new(@_), $class);
16              
17                 try {
18 0     0             require DBD::Pg;
19                 } catch {
20 0 0   0             my $error = (defined($_) ? $_ : "unknown");
21 0                   logger->fatal("could not initialize database connection '${\$self->{'_connection'}}': could not load DBD::Pg: ${error}");
  0            
22 0                   croak;
23 0               };
24              
25 0               my $database = $self->{'_database'};
26 0               my $username = $self->{'_username'};
27 0               my $password = $self->{'_password'};
28 0               my $hostname = $self->{'_hostname'};
29 0               my $port = $self->{'_port'};
30 0               my $charset = $self->{'_charset'};
31              
32             # if autocommit isn't configured then enable it by default
33 0 0             my $autocommit = (defined($self->{'_autocommit'}) ? ($self->{'_autocommit'} =~ /^(1|true|yes)$/ix ? 1 : 0) : 1);
    0          
34 0 0             logger->debug("auto commit is not enabled on database connection '${\$self->{'_connection'}}'") unless $autocommit;
  0            
35              
36 0               my $dsn = "dbi:Pg:dbname=${database}";
37 0 0             $dsn .= ";host=${hostname}" if defined($hostname);
38 0 0             $dsn .= ";port=${port}" if defined($port);
39              
40 0               my $params = {
41                     'AutoCommit' => $autocommit,
42                     'RaiseError' => 1,
43                     'PrintError' => 0,
44                 };
45 0 0 0           if ($charset && $charset =~ /^utf8$/xi) {
46 0                   $params->{'pg_enable_utf8'} = 1;
47                 }
48              
49 0               $self->{'_dsn'} = [$dsn, $username, $password, $params];
50 0               logger->debug("database connection '${\$self->{'_connection'}}' dsn: ${dsn}");
  0            
51              
52 0               return $self;
53             }
54              
55             1;
56