Session/Driver/DBI.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 168 SCAR 58.3
CC
18
Cov
0%
store
30.0
table_name
20.0
retrieve
20.0
CRAP 342.0
1 package CGI::Session::Driver::DBI;
2  
3 # $Id: DBI.pm 216 2005-09-01 10:52:26Z sherzodr $
4  
5 use strict;
6  
7 use DBI;
8 use Carp;
9 use CGI::Session::Driver;
10  
11 @CGI::Session::Driver::DBI::ISA = ( "CGI::Session::Driver" );
12 $CGI::Session::Driver::DBI::VERSION = "1.1";
13  
14  
15 sub init {
16 my $self = shift;
17 unless ( defined $self->{Handle} ) {
18 $self->{Handle} = DBI->connect(
19 $self->{DataSource}, $self->{User}, $self->{Password},
20 { RaiseError=>0, PrintError=>0, AutoCommit=>1 }
21 );
22 unless ( $self->{Handle} ) {
23 return $self->set_error( "init(): couldn't connect to database: " . DBI->errstr );
24 }
25 $self->{_disconnect} = 1;
26 }
27 }
28  
29 # A setter/accessor method for the table name, defaulting to 'sessions'
30  
31 sub table_name {
32 my $self = shift;
33 my $class = ref( $self ) || $self;
34  
35 if ( (@_ == 0) && ref($self) && ($self->{TableName}) ) {
36 return $self->{TableName};
37 }
38  
39 no strict 'refs';
40 if ( @_ ) {
41 my $new_name = shift;
42 $self->{TableName} = $new_name;
43 ${ $class . "::TABLE_NAME" } = $new_name;
44 }
45  
46 unless (defined $self->{TableName}) {
47 $self->{TableName} = "sessions";
48 }
49  
50 return $self->{TableName};
51 }
52  
53  
54 sub retrieve {
55 my $self = shift;
56 my ($sid) = @_;
57 croak "retrieve(): usage error" unless $sid;
58  
59  
60 my $dbh = $self->{Handle};
61 my $sth = $dbh->prepare_cached("SELECT a_session FROM " . $self->table_name . " WHERE id=?", undef, 3);
62 unless ( $sth ) {
63 return $self->set_error( "retrieve(): DBI->prepare failed with error message " . $dbh->errstr );
64 }
65 $sth->execute( $sid ) or return $self->set_error( "retrieve(): \$sth->execute failed with error message " . $sth->errstr);
66  
67 my ($row) = $sth->fetchrow_array();
68 return 0 unless $row;
69 return $row;
70 }
71  
72  
73 sub store {
74 # die;
75 my $self = shift;
76 my ($sid, $datastr) = @_;
77 croak "store(): usage error" unless $sid && $datastr;
78  
79  
80 my $dbh = $self->{Handle};
81 my $sth = $dbh->prepare_cached("SELECT id FROM " . $self->table_name . " WHERE id=?", undef, 3);
82 unless ( defined $sth ) {
83 return $self->set_error( "store(): \$dbh->prepare failed with message " . $sth->errstr );
84 }
85  
86 $sth->execute( $sid ) or return $self->set_error( "store(): \$sth->execute failed with message " . $sth->errstr );
87 my $action_sth;
88 if ( $sth->fetchrow_array ) {
89 $action_sth = $dbh->prepare_cached("UPDATE " . $self->table_name . " SET a_session=? WHERE id=?", undef, 3);
90 } else {
91 $action_sth = $dbh->prepare_cached("INSERT INTO " . $self->table_name . " (a_session, id) VALUES(?, ?)", undef, 3);
92 }
93
94 unless ( defined $action_sth ) {
95 return $self->set_error( "store(): \$dbh->prepare failed with message " . $dbh->errstr );
96 }
97 $action_sth->execute($datastr, $sid)
98 or return $self->set_error( "store(): \$action_sth->execute failed " . $action_sth->errstr );
99 return 1;
100 }
101  
102  
103 sub remove {
104 my $self = shift;
105 my ($sid) = @_;
106 croak "remove(): usage error" unless $sid;
107  
108 my $dbh = $self->{Handle};
109 my $sql = sprintf("DELETE FROM %s WHERE id='%s'", $self->table_name, $sid);
110 unless ( $dbh->do($sql) ) {
111 croak "remove(): \$dbh->do failed!";
112 }
113
114 return 1;
115 }
116  
117  
118 sub DESTROY {
119 my $self = shift;
120  
121 unless ( $self->{Handle}->{AutoCommit} ) {
122 $self->{Handle}->commit();
123 }
124 if ( $self->{_disconnect} ) {
125 $self->{Handle}->disconnect();
126 }
127 }
128  
129  
130 sub traverse {
131 my $self = shift;
132 my ($coderef) = @_;
133  
134 unless ( $coderef && ref( $coderef ) && (ref $coderef eq 'CODE') ) {
135 croak "traverse(): usage error";
136 }
137  
138 my $tablename = $self->table_name();
139 my $sth = $self->{Handle}->prepare_cached("SELECT id FROM $tablename", undef, 3)
140 or return $self->set_error("traverse(): couldn't prepare SQL statement. " . $self->{Handle}->errstr);
141 $sth->execute() or return $self->set_error("traverse(): couldn't execute statement $sth->{Statement}. " . $sth->errstr);
142  
143 while ( my ($sid) = $sth->fetchrow_array ) {
144 $coderef->($sid);
145 }
146 return 1;
147 }
148  
149  
150 1;
151  
152 =pod
153  
154 =head1 NAME
155  
156 CGI::Session::Driver::DBI - Base class for native DBI-related CGI::Session drivers
157  
158 =head1 SYNOPSIS
159  
160 require CGI::Session::Driver::DBI;
161 @ISA = qw( CGI::Session::Driver::DBI );
162  
163 =head1 DESCRIPTION
164  
165 In most cases you can create a new DBI-driven CGI::Session driver by simply creating an empty driver file that inherits from CGI::Session::Driver::DBI. That's exactly what L<sqlite|CGI::Session::Driver::sqlite> does. The only reason why this class doesn't suit for a valid driver is its name isn't in lowercase. I'm serious!
166  
167 =head2 NOTES
168  
169 CGI::Session::Driver::DBI defines init() method, which makes DBI handle available for drivers in I<Handle> - object attribute regardless of what C<\%dsn_args> were used in creating session object. Should your driver require non-standard initialization you have to re-define init() method in your F<.pm> file, but make sure to set 'Handle' - object attribute to database handle (returned by DBI->connect(...)) if you wish to inherit any of the methods from CGI::Session::Driver::DBI.
170  
171 =head1 STORAGE
172  
173 Before you can use any DBI-based session drivers you need to make sure compatible database table is created for CGI::Session to work with. Following command will produce minimal requirements in most SQL databases:
174  
175 CREATE TABLE sessions (
176 id CHAR(32) NOT NULL PRIMARY KEY,
177 a_session TEXT NOT NULL
178 );
179  
180 Your session table can define additional columns, but the above two are required. Name of the session table is expected to be I<sessions> by default. You may use a different name if you wish. To do this you have to pass I<TableName> as part of your C< \%dsn_args >:
181  
182 $s = new CGI::Session("driver:sqlite", undef, {TableName=>'my_sessions'});
183 $s = new CGI::Session("driver:mysql", undef, {
184 TableName=>'my_sessions',
185 DataSource=>'dbi:mysql:shopping_cart'});
186  
187 =head1 DRIVER ARGUMENTS
188  
189 Following driver arguments are supported:
190  
191 =over 4
192  
193 =item DataSource
194  
195 First argument to be passed to L<DBI|DBI>->L<connect()|DBI/connect()>.
196  
197 =item User
198  
199 User privileged to connect to the database defined in I<DataSource>.
200  
201 =item Password
202  
203 Password of the I<User> privileged to connect to the database defined in I<DataSource>
204  
205 =item Handle
206  
207 To set existing database handle object ($dbh) returned by DBI->connect(). I<Handle> will override all the
208 above arguments, if any present.
209  
210 =item TableName
211  
212 Name of the table session data will be stored in.
213  
214 =back
215  
216 =head1 LICENSING
217  
218 For support and licensing information see L<CGI::Session|CGI::Session>
219  
220 =cut
221