File Coverage

blib/lib/DBIx/QuickORM/DB.pm
Criterion Covered Total %
statement 40 40 100.0
branch 9 14 64.2
condition 9 16 56.2
subroutine 9 9 100.0
pod 0 4 0.0
total 67 83 80.7


line stmt bran cond sub pod time code
1             package DBIx::QuickORM::DB;
2 24     24   187 use strict;
  24         61  
  24         1106  
3 24     24   177 use warnings;
  24         66  
  24         2447  
4              
5             our $VERSION = '0.000019';
6              
7 24     24   174 use Carp qw/croak confess/;
  24         52  
  24         2094  
8 24     24   185 use Scalar::Util qw/blessed/;
  24         102  
  24         7335  
9              
10 24         283 use DBIx::QuickORM::Util::HashBase qw{
11             <name
12             +connect
13             <attributes
14             +db_name
15             +dsn
16             <host
17             <port
18             <socket
19             <user
20             <pass
21             <created
22             <compiled
23             <dialect
24             <dbi_driver
25 24     24   218 };
  24         67  
26              
27 25   33 25 0 401 sub db_name { $_[0]->{+DB_NAME} // $_[0]->{+NAME} }
28              
29             sub init {
30 35     35 0 102 my $self = shift;
31              
32 35 50       287 croak "'dialect' is a required attribute" unless $self->{+DIALECT};
33              
34 35 100       144 delete $self->{+NAME} unless defined $self->{+NAME};
35              
36 35   100     341 $self->{+ATTRIBUTES} //= {};
37 35   50     225 $self->{+ATTRIBUTES}->{RaiseError} //= 1;
38 35   50     284 $self->{+ATTRIBUTES}->{PrintError} //= 1;
39 35   50     191 $self->{+ATTRIBUTES}->{AutoCommit} //= 1;
40 35   50     215 $self->{+ATTRIBUTES}->{AutoInactiveDestroy} //= 1;
41              
42 35 50 66     188 croak "Cannot provide both a socket and a host" if $self->{+SOCKET} && $self->{+HOST};
43             }
44              
45             sub dsn {
46 2     2 0 5 my $self = shift;
47 2 50       9 return $self->{+DSN} if $self->{+DSN};
48 2         26 return $self->{+DSN} = $self->{+DIALECT}->dsn($self);
49             }
50              
51             sub new_dbh {
52 23     23 0 62 my $self = shift;
53 23         78 my (%params) = @_;
54              
55 23         129 my $attrs = $self->attributes;
56              
57 23         62 my $dbh;
58 23 50       57 eval {
59 23 100       142 if ($self->{+CONNECT}) {
60 21         139 $dbh = $self->{+CONNECT}->();
61             }
62             else {
63 2         15 require DBI;
64 2         9 $dbh = DBI->connect($self->dsn, $self->user, $self->pass, $self->attributes);
65             }
66              
67 23         57598 1;
68             } or confess $@;
69              
70 23 50       279 $dbh->{AutoInactiveDestroy} = 1 if $attrs->{AutoInactiveDestroy};
71              
72 23         210 return $dbh;
73             }
74              
75             1;