File Coverage

blib/lib/Dancer2/Plugin/SessionDatabase.pm
Criterion Covered Total %
statement 16 39 41.0
branch 0 10 0.0
condition 0 3 0.0
subroutine 5 11 45.4
pod 0 4 0.0
total 21 67 31.3


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::SessionDatabase;
2              
3 2     2   841467 use Modern::Perl;
  2         10  
  2         16  
4 2     2   889 use Dancer2::Plugin;
  2         213400  
  2         20  
5 2     2   45768 use Carp qw(croak);
  2         6  
  2         93  
6 2     2   732 use Data::Dumper;
  2         6883  
  2         1030  
7              
8             =head1 NAME
9              
10             Dancer2::Plugin::SessionDatabase - Hook Loader For Dancer2::Session::DatabasePlugin
11              
12             =head1 DESCRIPTION
13              
14             Hook loader for Dancer2::Session::DatabasePlugin.
15              
16             =cut
17              
18             our $DBH;
19              
20             sub reset_session {
21 0     0 0 0 %{$Dancer2::Session::DatabasePlugin::CACHE}=();
  0         0  
22 0         0 $DBH=undef;
23             }
24              
25             sub DBC {
26 0     0 0 0 my ($self,$conn)=@_;
27 0         0 my $db=$self->find_plugin('Dancer2::Plugin::Database');
28 0         0 my $dbh=$db->database($conn);
29              
30 0 0 0     0 if(defined($DBH) && $dbh ne $DBH) {
31 0         0 %{$Dancer2::Session::DatabasePlugin::CACHE}=();
  0         0  
32             }
33              
34 0         0 return $DBH=$dbh;
35             }
36              
37             sub db_check {
38 0     0 0 0 my ($self,$dbh)=@_;
39 0 0       0 return 0 unless defined($DBH);
40 0 0       0 return 0 unless $DBH eq $dbh;
41 0         0 return 1;
42             }
43              
44             # This method runs after the new constructor
45             sub BUILD {
46 1     1 0 2111 my ($self)=@_;
47              
48             $self->app->add_hook(
49             Dancer2::Core::Hook->new(
50             name=>"engine.session.before_db",
51             code=>sub {
52 0     0   0 my ($session)=@_;
53 0         0 my $dbh=$self->DBC($session->connection);
54 0         0 $session->dbh($dbh);
55             }
56             )
57 1         31 );
58             $self->app->add_hook(
59             Dancer2::Core::Hook->new(
60             name=>"database_connection_lost",
61             code=>sub {
62 0     0   0 my ($dbh)=@_;
63 0 0       0 return unless $self->db_check($dbh);
64 0         0 $self->reset_session;
65             }
66             )
67 1         151882 );
68             $self->app->add_hook(
69             Dancer2::Core::Hook->new(
70             name=>"database_error",
71             code=>sub {
72 0     0     my ($err,$dbh)=@_;
73 0 0         return unless $self->db_check($dbh);
74 0           $self->reset_session;
75             }
76             )
77 1         556 );
78             }
79              
80             =head1 AUTHOR
81              
82             Michael Shipper AKALINUX@CPAN.ORG
83              
84             =cut
85              
86             1;