Apache/Session/DBIBase64Store.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 105 CC 7 SCAR 20.5
CC
7
Cov
0%
get_handle
20.0
materialize
12.0
DESTROY
6.0
CRAP 7.8
1 #
2 # DBIBase64Store.pm - Session store implemented with DBI database.
3 #
4 # This file is part of Apache::iNcom.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 # Based on Apache::Session:DBIStore from Jeffrey William Baker
8 #
9 # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations
10 # Parts Copyright(c) 1998, 1999 Jeffrey William Baker (jeffrey@kathyandjeffrey.net)
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms same terms as perl itself.
14 #
15  
16 package Apache::Session::DBIBase64Store;
17  
18 use strict;
19  
20 use DBI;
21 use Storable qw( nfreeze thaw );
22 use MIME::Base64;
23  
24 use vars qw($VERSION);
25  
26 BEGIN {
27 ($VERSION) = '$Revision: 1.2 $' =~ /Revision: ([\d.]+)/ ;
28 }
29  
30 sub new {
31 my $class = shift;
32  
33 return bless {}, $class;
34 }
35  
36 sub get_handle {
37 my ($self,$session) = @_;
38  
39 my $dbh = $session->{args}{dbh} || $self->{dbh};
40 unless ( $dbh ) {
41 my $dsn = $session->{args}{DataSource};
42 my $username = $session->{args}{UserName};
43 my $password = $session->{args}{Password};
44  
45 die "No opened database connection and no DSN\n" unless $dsn;
46 $dbh = DBI->connect( $dsn, $username, $password, { AutoCommit => 0,
47 RaiseError => 1,
48 } );
49  
50 # Save it for future use
51 $self->{dbh} = $dbh;
52 }
53 die "No opened database connection\n" unless $dbh;
54  
55 return $dbh;
56 }
57  
58 sub insert {
59 my $self = shift;
60 my $session = shift;
61  
62 my $dbh = $self->get_handle( $session );
63  
64 my $sth =$dbh->prepare(qq{ INSERT INTO sessions (id, created, last_update,
65 length, a_session)
66 VALUES (?,'now()', 'now()', ?, ? ) } );
67  
68 my $serialized = encode_base64( nfreeze $session->{data} );
69  
70 $sth->execute ( $session->{data}{_session_id}, length $serialized,
71 $serialized );
72 $sth->finish;
73 $dbh->commit;
74 }
75  
76  
77 sub update {
78 my $self = shift;
79 my $session = shift;
80  
81 my $dbh = $self->get_handle( $session );
82  
83 my $sth = $dbh->prepare( qq{ UPDATE sessions
84 SET length = ?, a_session = ?,
85 last_update = now()
86 WHERE id = ?} );
87  
88 my $serialized = encode_base64( nfreeze $session->{data} );
89  
90 $sth->execute( length $serialized, $serialized,
91 $session->{data}{_session_id} );
92 $sth->finish;
93 $dbh->commit;
94 }
95  
96 sub materialize {
97 my $self = shift;
98 my $session = shift;
99  
100 my $dbh = $self->get_handle( $session );
101  
102 my $sth = $dbh->prepare( qq{ SELECT a_session FROM sessions WHERE id = ?});
103 $sth->execute( $session->{data}{_session_id} );
104  
105 my $results = $sth->fetchrow_arrayref;
106 die "Object does not exist in the data store" unless defined $results;
107  
108 $sth->finish;
109  
110 $session->{data} = thaw decode_base64( $results->[0] );
111  
112 die "Session is corrupted\n" unless $session->{data};
113  
114 $dbh->commit;
115 }
116  
117 sub remove {
118 my $self = shift;
119 my $session = shift;
120  
121 my $dbh = $self->get_handle( $session );
122  
123 my $sth = $dbh->prepare( qq{ DELETE FROM sessions WHERE id = ?} );
124 $sth->execute( $session->{data}{_session_id} );
125 $sth->finish;
126  
127 $dbh->commit;
128 }
129  
130 sub DESTROY {
131 my $self = shift;
132  
133 if ( $self->{dbh} ) {
134 # Close connection to the database if we opened
135 # it. If it is not ours, it is up to the owner
136 # to dispose of it.
137 $self->{dbh}->disconnect;
138 }
139 }
140  
141 1;
142 __END__