File Coverage

blib/lib/Apache2/ASP/ApplicationStateManager.pm
Criterion Covered Total %
statement 27 73 36.9
branch 0 6 0.0
condition 0 2 0.0
subroutine 9 16 56.2
pod 1 6 16.6
total 37 103 35.9


line stmt bran cond sub pod time code
1              
2             package Apache2::ASP::ApplicationStateManager;
3              
4 23     23   129 use strict;
  23         34  
  23         653  
5 23     23   92 use warnings 'all';
  23         32  
  23         722  
6 23     23   93 use Storable qw( freeze thaw );
  23         27  
  23         1079  
7 23     23   111 use DBI;
  23         47  
  23         734  
8 23     23   96 use Scalar::Util 'weaken';
  23         32  
  23         926  
9 23     23   90 use base 'Ima::DBI';
  23         33  
  23         1528  
10 23     23   106 use Digest::MD5 'md5_hex';
  23         38  
  23         7994  
11              
12             #==============================================================================
13             sub new
14             {
15 0     0 0   my ($class, %args) = @_;
16            
17 0           my $s = bless { }, $class;
18              
19 0           my $conn = $s->context->config->data_connections->application;
20 0           local $^W = 0;
21 0           __PACKAGE__->set_db('Main', $conn->dsn,
22             $conn->username,
23             $conn->password
24             );
25            
26 0 0         if( my $res = $s->retrieve )
27             {
28 0           return $res;
29             }
30             else
31             {
32 0           return $s->create;
33             }# end if()
34             }# end new()
35              
36              
37             #==============================================================================
38             sub context
39             {
40 0     0 0   $Apache2::ASP::HTTPContext::ClassName->current;
41             }# end context()
42              
43              
44             #==============================================================================
45             sub create
46             {
47 0     0 0   my $s = shift;
48            
49 0           local $s->db_Main->{AutoCommit} = 1;
50 0           my $sth = $s->db_Main->prepare(<<"");
51             INSERT INTO asp_applications (
52             application_id,
53             application_data
54             )
55             VALUES (
56             ?, ?
57             )
58              
59 0           $sth->execute(
60             $s->context->config->web->application_name,
61             freeze( {} )
62             );
63 0           $sth->finish();
64            
65 0           return $s->retrieve();
66             }# end create()
67              
68              
69             #==============================================================================
70             sub retrieve
71             {
72 0     0 0   my $s = shift;
73            
74 0           my $sth = $s->dbh->prepare_cached(<<"");
75             SELECT application_data
76             FROM asp_applications
77             WHERE application_id = ?
78              
79 0           $sth->execute( $s->context->config->web->application_name );
80 0           my ($data) = $sth->fetchrow;
81 0           $sth->finish();
82            
83 0 0         return unless $data;
84            
85 0   0       $data = thaw($data) || {};
86 0           undef(%$s);
87 0           $s = bless $data, ref($s);
88            
89 23     23   123 no warnings 'uninitialized';
  23         33  
  23         2886  
90             $s->{__signature} = md5_hex(
91             join ":",
92 0           map { "$_:$s->{$_}" }
93 0           grep { $_ ne '__signature' } sort keys(%$s)
  0            
94             );
95            
96 0           return $s;
97             }# end retrieve()
98              
99              
100             #==============================================================================
101             sub save
102             {
103 0     0 1   my $s = shift;
104              
105 23     23   111 no warnings 'uninitialized';
  23         34  
  23         5889  
106             return if $s->{__signature} eq md5_hex(
107             join ":",
108 0           map { "$_:$s->{$_}" }
109 0 0         grep { $_ ne '__signature' } sort keys(%$s)
  0            
110             );
111             $s->{__signature} = md5_hex(
112             join ":",
113 0           map { "$_:$s->{$_}" }
114 0           grep { $_ ne '__signature' } sort keys(%$s)
  0            
115             );
116            
117 0           local $s->db_Main->{AutoCommit} = 1;
118 0           my $sth = $s->db_Main->prepare_cached(<<"");
119             UPDATE asp_applications SET
120             application_data = ?
121             WHERE application_id = ?
122              
123 0           my $data = { %$s };
124 0           delete($data->{__signature} );
125 0           $sth->execute(
126             freeze( $data ),
127             $s->context->config->web->application_name
128             );
129 0           $sth->finish();
130            
131 0           1;
132             }# end save()
133              
134              
135             #==============================================================================
136             sub dbh
137             {
138 0     0 0   my $s = shift;
139 0           return $s->db_Main;
140             }# end dbh()
141              
142              
143             #==============================================================================
144             sub DESTROY
145             {
146 0     0     my $s = shift;
147            
148 0           delete($s->{$_}) foreach keys(%$s);
149             }# end DESTROY()
150              
151             1;# return true:
152              
153             __END__