File Coverage

blib/lib/Apache/Session/Store/DBMS.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             #############################################################################
2             #
3             # Apache::Session::Store::DBMS
4             # Implements session object storage via DBMS module
5             # Copyright(c) 2005 Asemantics S.r.l.
6             # Alberto Reggiori (alberto@asemantics.com)
7             # Distribute under a BSD license (see LICENSE file in main dir)
8             #
9             ############################################################################
10              
11             package Apache::Session::Store::DBMS;
12              
13 2     2   11 use strict;
  2         4  
  2         75  
14 2     2   10 use vars qw($VERSION);
  2         4  
  2         71  
15 2     2   5898 use DBMS;
  0            
  0            
16              
17             $VERSION = '0.1';
18              
19             $Apache::Session::Store::DBMS::DataSource = 'sessions';
20             $Apache::Session::Store::DBMS::Host = 'localhost';
21             $Apache::Session::Store::DBMS::Port = 1234;
22             $Apache::Session::Store::DBMS::Mode = &DBMS::XSMODE_CREAT;
23             $Apache::Session::Store::DBMS::Bt_compare = 0; #none
24              
25             sub connection {
26             my $self = shift;
27             my $session = shift;
28              
29             return
30             if(defined $self->{dbh});
31              
32             if( (exists $session->{args}->{Handle}) &&
33             (UNIVERSAL::isa( $session->{args}->{Handle}, "DBMS" )) ) {
34             $self->{dbh} = $session->{args}->{Handle};
35             return;
36             };
37              
38             my $mode = $session->{args}->{Mode} ||
39             $Apache::Session::Store::DBMS::Mode;
40             my $bt_compare = $session->{args}->{Bt_compare} ||
41             $Apache::Session::Store::DBMS::Bt_compare;
42              
43             my ($datasource, $host, $port);
44             if( $session->{isObjectPerKey} ) {
45             if( $session->{data}->{_session_id} =~ m|^\s*dbms://([^:]+):(\d+)/([^\s]+)| ) {
46             $host = $1;
47             $port = $2;
48             $datasource = $3;
49             } elsif( $session->{data}->{_session_id} =~ m|^\s*dbms://([^/]+)/([^\s]+)| ) {
50             $host = $1;
51             $port = $session->{args}->{Port} ||
52             $Apache::Session::Store::DBMS::Port;
53             $datasource = $2;
54             } else {
55             die "Invalid session identifier ".$session->{data}->{_session_id};
56             };
57             } elsif( ( exists $session->{args}->{DataSource} ) &&
58             ( $session->{args}->{DataSource} =~ m|^\s*dbms://| ) ) {
59             if( $session->{args}->{DataSource} =~ m|^\s*dbms://([^:]+):(\d+)/([^\s]+)| ) {
60             $host = $1;
61             $port = $2;
62             $datasource = $3;
63             } elsif( $session->{args}->{DataSource} =~ m|^\s*dbms://([^/]+)/([^\s]+)| ) {
64             $host = $1;
65             $port = $session->{args}->{Port} ||
66             $Apache::Session::Store::DBMS::Port;
67             $datasource = $2;
68             } else {
69             die "Invalid session identifier ".$session->{data}->{_session_id};
70             };
71             } else {
72             $datasource = $session->{args}->{DataSource} ||
73             $Apache::Session::Store::DBMS::DataSource;
74             $host = $session->{args}->{Host} ||
75             $Apache::Session::Store::DBMS::Host;
76             $port = $session->{args}->{Port} ||
77             $Apache::Session::Store::DBMS::Port;
78             };
79              
80             #print "TIE: $datasource, $mode, $bt_compare, $host, $port (session_id=".$session->{data}->{_session_id}.")\n";
81              
82             $self->{dbh} = tie %{$self->{dbms}}, 'DBMS', $datasource, $mode, $bt_compare, $host, $port
83             or die $DBMS::ERROR."\n";
84             };
85              
86             sub new {
87             my $class = shift;
88              
89             return bless {dbms => {}}, $class;
90             };
91              
92             sub insert {
93             my $self = shift;
94             my $session = shift;
95            
96             return
97             if( $session->{isObjectPerKey} );
98              
99             $self->connection($session);
100              
101             if (exists $self->{dbms}->{$session->{data}->{_session_id}}) {
102             die "Object already exists in the data store";
103             };
104              
105             $self->{dbms}->{$session->{data}->{_session_id}} = $session->{serialized}; # single session-id object
106             };
107              
108             sub update {
109             my $self = shift;
110             my $session = shift;
111            
112             return
113             if( $session->{isObjectPerKey} );
114              
115             $self->connection($session);
116              
117             $self->{dbms}->{$session->{data}->{_session_id}} = $session->{serialized}
118             if(defined $session->{data}->{_session_id});
119             };
120              
121             sub materialize {
122             my $self = shift;
123             my $session = shift;
124            
125             return
126             if( $session->{isObjectPerKey} );
127              
128             $self->connection($session);
129              
130             $session->{serialized} = $self->{dbms}->{$session->{data}->{_session_id}};
131              
132             if (!defined $session->{serialized}) {
133             die "Object does not exist in data store";
134             };
135             };
136              
137             sub remove {
138             my $self = shift;
139             my $session = shift;
140            
141             return
142             if( $session->{isObjectPerKey} );
143              
144             $self->connection($session);
145              
146             delete $self->{dbms}->{$session->{data}->{_session_id}};
147             };
148              
149             sub DESTROY {
150             my $self = shift;
151              
152             if(defined $self->{dbh}) {
153             delete $self->{dbh};
154             untie %{$self->{dbms}};
155             };
156             };
157              
158             1;
159              
160             =pod
161              
162             =head1 NAME
163              
164             Apache::Session::Store::DBMS - Use DBMS to store persistent objects
165              
166             =head1 SYNOPSIS
167              
168             use Apache::Session::Store::DBMS;
169            
170             my $store = new Apache::Session::Store::DBMS;
171            
172             $store->insert($ref);
173             $store->update($ref);
174             $store->materialize($ref);
175             $store->remove($ref);
176              
177             =head1 DESCRIPTION
178              
179             This module fulfills the storage interface of Apache::Session. The serialized
180             objects are stored in a remote hashed Berkeley DB store using the DBMS Perl module.
181              
182             =head1 OPTIONS
183              
184             This module requires...
185              
186             =head1 AUTHOR
187              
188             This module was written by Alberto Reggiori
189              
190             =head1 SEE ALSO
191              
192             L, L
193             http://rdfstore.sf.net/dbms.html