ASP/ApplicationStateManager.pm untested

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