| 1 | package Apache::Session::Store::MariaDB; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Apache::Session::Store::DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use base 'Apache::Session::Store::DBI'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | $Apache::Session::Store::MariaDB::DataSource = undef; | |||||||||||||||||||
| 11 | 1 | $Apache::Session::Store::MariaDB::UserName = undef; | |||||||||||||||||||
| 12 | 1 | $Apache::Session::Store::MariaDB::Password = undef; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 0 | ▶ | sub connection { | ||||||||||||||||||
|
Subroutine
connection: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||
| 15 | 0 | my $self = shift; | |||||||||||||||||||
| 16 | 0 | my $session = shift; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 0 | ▶ | return if ( defined $self->{dbh} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | $self->{'table_name'} = $session->{args}->{TableName} || $Apache::Session::Store::DBI::TableName; | ||||||||||||||||||
|
Condition0%0/3
$session->{'args'}{'TableName'} || $Apache::Session::Store::DBI::TableName
MC/DC0%0/2
$session->{'args'}{'TableName'} || $Apache::Session::Store::DBI::TableName
$session->{'args'}{'TableName'}
$Apache::Session::Store::DBI::TableName
Truth table0%0/3
A || B
A= $session->{'args'}{'TableName'}B= $Apache::Session::Store::DBI::TableName
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 0 | ▶ | if ( exists $session->{args}->{Handle} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 0 | $self->{dbh} = $session->{args}->{Handle}; | |||||||||||||||||||
| 24 | 0 | return; | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | my $datasource = $session->{args}->{DataSource} | ||||||||||||||||||||
| 28 | 0 | ▶ | || $Apache::Session::Store::MariaDB::DataSource; | ||||||||||||||||||
|
Condition0%0/3
$session->{'args'}{'DataSource'} || $Apache::Session::Store::MariaDB::DataSource
MC/DC0%0/2
$session->{'args'}{'DataSource'} || $Apache::Session::Store::MariaDB::DataSource
$session->{'args'}{'DataSource'}
$Apache::Session::Store::MariaDB::DataSource
Truth table0%0/3
A || B
A= $session->{'args'}{'DataSource'}B= $Apache::Session::Store::MariaDB::DataSource
| |||||||||||||||||||||
| 29 | my $username = $session->{args}->{UserName} | ||||||||||||||||||||
| 30 | 0 | ▶ | || $Apache::Session::Store::MariaDB::UserName; | ||||||||||||||||||
|
Condition0%0/3
$session->{'args'}{'UserName'} || $Apache::Session::Store::MariaDB::UserName
MC/DC0%0/2
$session->{'args'}{'UserName'} || $Apache::Session::Store::MariaDB::UserName
$session->{'args'}{'UserName'}
$Apache::Session::Store::MariaDB::UserName
Truth table0%0/3
A || B
A= $session->{'args'}{'UserName'}B= $Apache::Session::Store::MariaDB::UserName
| |||||||||||||||||||||
| 31 | my $password = $session->{args}->{Password} | ||||||||||||||||||||
| 32 | 0 | ▶ | || $Apache::Session::Store::MariaDB::Password; | ||||||||||||||||||
|
Condition0%0/3
$session->{'args'}{'Password'} || $Apache::Session::Store::MariaDB::Password
MC/DC0%0/2
$session->{'args'}{'Password'} || $Apache::Session::Store::MariaDB::Password
$session->{'args'}{'Password'}
$Apache::Session::Store::MariaDB::Password
Truth table0%0/3
A || B
A= $session->{'args'}{'Password'}B= $Apache::Session::Store::MariaDB::Password
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | ▶ | $self->{dbh} = DBI->connect( $datasource, $username, $password, { RaiseError => 1, AutoCommit => 1 } ) | ||||||||||||||||||
|
Condition0%0/2
'DBI'->connect($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1}) || die($DBI::errstr)
MC/DC0%0/1
'DBI'->connect($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1}) || die($DBI::errstr)
'DBI'->connect($datasource, $username, $password, {'RaiseError', 1, 'AutoCommit', 1})
Truth table0%0/2
A
A= '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 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 43 | 0 | my $self = shift; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | if ( $self->{disconnect} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | $self->{dbh}->disconnect; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # DBD::MariaDB requires to explicitly indicate a_session is a binary field | ||||||||||||||||||||
| 51 | 0 | ▶ | sub insert { | ||||||||||||||||||
|
Subroutine
insert: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 52 | 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 | ▶ | if ( !defined $self->{insert_sth} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub update { | ||||||||||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 76 | 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 | ▶ | if ( !defined $self->{update_sth} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 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 E<lt>modules@bestpractical.comE<gt> | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Jeffrey William Baker E<lt>jwbaker@acm.orgE<gt> | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head1 SEE ALSO | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | L<Apache::Session> | ||||||||||||||||||||