File Coverage

blib/lib/Apache/Session/Store/MariaDB.pm
Criterion Covered Total %
statement 12 47 25.5
branch 0 10 0.0
condition 0 14 0.0
subroutine 4 8 50.0
pod 0 3 0.0
total 16 82 19.5


line stmt bran cond sub pod time code
1             package Apache::Session::Store::MariaDB;
2              
3 1     1   5 use strict;
  1         1  
  1         30  
4              
5 1     1   4 use DBI;
  1         2  
  1         31  
6 1     1   519 use Apache::Session::Store::DBI;
  1         617  
  1         56  
7              
8 1     1   8 use base 'Apache::Session::Store::DBI';
  1         2  
  1         728  
9              
10             $Apache::Session::Store::MariaDB::DataSource = undef;
11             $Apache::Session::Store::MariaDB::UserName = undef;
12             $Apache::Session::Store::MariaDB::Password = undef;
13              
14             sub connection {
15 0     0 0   my $self = shift;
16 0           my $session = shift;
17              
18 0 0         return if ( defined $self->{dbh} );
19              
20 0   0       $self->{'table_name'} = $session->{args}->{TableName} || $Apache::Session::Store::DBI::TableName;
21              
22 0 0         if ( exists $session->{args}->{Handle} ) {
23 0           $self->{dbh} = $session->{args}->{Handle};
24 0           return;
25             }
26              
27             my $datasource = $session->{args}->{DataSource}
28 0   0       || $Apache::Session::Store::MariaDB::DataSource;
29             my $username = $session->{args}->{UserName}
30 0   0       || $Apache::Session::Store::MariaDB::UserName;
31             my $password = $session->{args}->{Password}
32 0   0       || $Apache::Session::Store::MariaDB::Password;
33              
34 0   0       $self->{dbh} = DBI->connect( $datasource, $username, $password, { RaiseError => 1, AutoCommit => 1 } )
35             || die $DBI::errstr;
36              
37              
38             #If we open the connection, we close the connection
39 0           $self->{disconnect} = 1;
40             }
41              
42             sub DESTROY {
43 0     0     my $self = shift;
44              
45 0 0         if ( $self->{disconnect} ) {
46 0           $self->{dbh}->disconnect;
47             }
48             }
49              
50             # DBD::MariaDB requires to explicitly indicate a_session is a binary field
51             sub insert {
52 0     0 0   my $self = shift;
53 0           my $session = shift;
54              
55 0           $self->connection($session);
56              
57 0           local $self->{dbh}->{RaiseError} = 1;
58              
59 0 0         if ( !defined $self->{insert_sth} ) {
60             $self->{insert_sth} = $self->{dbh}->prepare_cached(
61 0           qq{
62             INSERT INTO $self->{'table_name'} (id, a_session) VALUES (?,?)}
63             );
64             }
65              
66 0           $self->{insert_sth}->bind_param( 1, $session->{data}->{_session_id} );
67 0           $self->{insert_sth}->bind_param( 2, $session->{serialized}, DBI::SQL_BLOB );
68              
69 0           $self->{insert_sth}->execute;
70              
71 0           $self->{insert_sth}->finish;
72             }
73              
74              
75             sub update {
76 0     0 0   my $self = shift;
77 0           my $session = shift;
78              
79 0           $self->connection($session);
80              
81 0           local $self->{dbh}->{RaiseError} = 1;
82              
83 0 0         if ( !defined $self->{update_sth} ) {
84             $self->{update_sth} = $self->{dbh}->prepare_cached(
85 0           qq{
86             UPDATE $self->{'table_name'} SET a_session = ? WHERE id = ?}
87             );
88             }
89              
90 0           $self->{update_sth}->bind_param( 1, $session->{serialized}, DBI::SQL_BLOB );
91 0           $self->{update_sth}->bind_param( 2, $session->{data}->{_session_id} );
92              
93 0           $self->{update_sth}->execute;
94              
95 0           $self->{update_sth}->finish;
96             }
97              
98             1;
99              
100             =pod
101              
102             =head1 NAME
103              
104             Apache::Session::Store::MariaDB - Store persistent data in a MariaDB database
105              
106             =head1 SYNOPSIS
107              
108             use Apache::Session::Store::MariaDB;
109              
110             my $store = new Apache::Session::Store::MariaDB;
111              
112             $store->insert($ref);
113             $store->update($ref);
114             $store->materialize($ref);
115             $store->remove($ref);
116              
117             =head1 DESCRIPTION
118              
119             Apache::Session::Store::MariaDB fulfills the storage interface of .
120             Apache::Session Session data is stored in a MariaDB database .
121              
122             =head1 SCHEMA
123              
124             To use this module, you will need at least these columns in a table called
125             'sessions', or another table name if you provide the TableName argument:
126              
127             id char(32) # or however long your session IDs are.
128             a_session blob # or longblob if you plan to use a big session (>64k after serialization)
129              
130             To create this schema, you can execute this command using the MariaDB
131             program:
132              
133             CREATE TABLE sessions (
134             id char(32) not null primary key,
135             a_session blob
136             );
137              
138             If you use some other command, ensure that there is a unique index on the
139             table's id column.
140              
141             =head1 CONFIGURATION
142              
143             The module must know what datasource, username, and password to use when
144             connecting to the database. These values can be set using the options hash
145             (see Apache::Session documentation). The options are:
146              
147             =over 4
148              
149             =item DataSource
150              
151             =item UserName
152              
153             =item Password
154              
155             =item TableName
156              
157             =item Handle
158              
159             =back
160              
161             Example:
162              
163             tie %hash, 'Apache::Session::MariaDB', $id, {
164             DataSource => 'dbi:MariaDB:database',
165             UserName => 'database_user',
166             Password => 'K00l',
167             TableName => 'sessions'
168             };
169              
170             Instead, you may pass in an already-opened DBI handle to your database.
171              
172             tie %hash, 'Apache::Session::MariaDB', $id, {
173             Handle => $dbh
174             };
175              
176             =head1 AUTHOR
177              
178             Best Practical Solutions, LLC Emodules@bestpractical.comE
179              
180             Jeffrey William Baker Ejwbaker@acm.orgE
181              
182             =head1 SEE ALSO
183              
184             L