burak / CPAN-PHP-Session-DBI

Interface to PHP DataBase Sessions (Perl)

Changed (Δ233 bytes):

raw changeset »

Build.PL (4 lines added, 1 lines removed)

Changes (3 lines added, 0 lines removed)

README (2 lines added, 2 lines removed)

SPEC (3 lines added, 2 lines removed)

lib/PHP/Session/DBI.pm (33 lines added, 27 lines removed)

t/03-simple.t (1 lines added, 0 lines removed)

Up to file-list Build.PL:

1
1
use strict;
2
use warnings;
2
3
use lib qw( builder );
3
4
use Build;
4
5
5
6
my $mb = Build->new;
6
7
$mb->change_versions(1);
7
$mb->copyright_first_year( 2007 );
8
$mb->copyright_first_year( '2007' );
8
9
$mb->add_pod_author_copyright_license(1);
9
10
$mb->create_build_script;
11
12
1;

Up to file-list Changes:

@@ -2,6 +2,9 @@ Revision history for Perl extension PHP:
2
2
3
3
Time zone is GMT+2.
4
4
5
0.30 Fri Oct  2 06:45:20 2009
6
    => Perl::Critic refactoring.
7
5
8
0.24 Thu Sep  3 01:57:43 2009
6
9
    => Pod & distro fixes.
7
10

Up to file-list README:

@@ -36,8 +36,8 @@ All are available on CPAN.
36
36
37
37
COPYRIGHT AND LICENCE
38
38
39
39
40
40
41
41
This library is free software; you can redistribute it and/or modify 
42
it under the same terms as Perl itself, either Perl version 5.8.8 or, 
42
it under the same terms as Perl itself, either Perl version 5.10.1 or, 
43
43
at your option, any later version of Perl 5 you may have available.

Up to file-list SPEC:

1
1
{
2
2
    module_name    => 'PHP::Session::DBI',
3
3
    requires       => {
4
         'PHP::Session' => '0.26',
5
         'DBI'          => 0,
4
        'PHP::Session' => '0.26',
5
        'DBI'          => 0,
6
        ( $] < 5.006 ? ( 'warnings::compat' => 0 ) : ()),
6
7
    },
7
8
    recommends     => {
8
9
         'DBD::mysql'   => 0,

Up to file-list lib/PHP/Session/DBI.pm:

1
1
package PHP::Session::DBI;
2
2
use strict;
3
use warnings;
3
4
use vars qw($VERSION);
4
5
use base qw(PHP::Session);
5
6
use Carp qw(croak);
6
7
7
$VERSION = '0.24';
8
$VERSION = '0.30';
8
9
9
10
sub new {
10
11
   my($class, $sid, $opt) = @_;
11
   croak "OPTIONS must be present and must be a HASHref" if ! _ishash($opt);
12
   my $db_handle = delete($opt->{db_handle}) || croak "db_handle option is missing";
13
   my $db_table  = delete($opt->{db_table})  || croak "db_table option is missing";
14
   my $db_schema = delete($opt->{db_schema}) || croak "db_schema option is missing";
12
   croak 'OPTIONS must be present and must be a HASHref' if ! _ishash($opt);
13
   my $db_handle = delete($opt->{db_handle}) || croak 'db_handle option is missing';
14
   my $db_table  = delete($opt->{db_table})  || croak 'db_table option is missing';
15
   my $db_schema = delete($opt->{db_schema}) || croak 'db_schema option is missing';
15
16
   my $self      = $class->SUPER::new($sid, $opt);
16
17
   # inject our keys into session object
17
18
   $self->{db_handle}    = $db_handle;
@@ -23,64 +24,68 @@ sub new {
23
24
   return $self;
24
25
}
25
26
26
sub dbh { shift->{db_handle} }
27
sub dbh { return shift->{db_handle} }
27
28
28
29
sub save {
29
30
   my $self    = shift;
30
   my $encoded = $self->encode($self->{_data}) || '';
31
   my $encoded = $self->encode($self->{_data}) || q{};
31
32
   my %schema  = $self->_db_schema;
32
33
   my($SQL, @params);
33
34
   if ($self->{_db_create}) {
34
      $SQL = qq(
35
      $SQL = <<"SQL";
35
36
         INSERT INTO $self->{db_table}
36
37
                ( $schema{date}, $schema{data}, $schema{id} )
37
38
         VALUES (             ?,             ?,           ? )
38
      );
39
SQL
39
40
      @params = (time, $encoded, $self->id);
40
41
   }
41
42
   else {
42
43
      if ($schema{update_date}) {
43
         $SQL = qq(
44
         $SQL = <<"SQL";
44
45
            UPDATE $self->{db_table}
45
46
            SET    $schema{date} = ?,
46
47
                   $schema{data} = ?
47
48
            WHERE  $schema{id}   = ?
48
         );
49
SQL
49
50
         @params = (time, $encoded, $self->id);
50
51
      }
51
52
      else {
52
         $SQL = qq(
53
         $SQL = <<"SQL";
53
54
            UPDATE $self->{db_table}
54
55
            SET    $schema{data} = ?
55
56
            WHERE  $schema{id}   = ?
56
         );
57
SQL
57
58
         @params = ($encoded, $self->id);
58
59
      }
59
60
   }
60
61
   $self->dbh->do( $SQL, undef, @params )
61
      or croak( "Can't update database: " . $self->dbh->errstr );
62
      or croak( q{Can't update database: } . $self->dbh->errstr );
62
63
   $self->{_changed} = 0; # init
64
   return;
63
65
}
64
66
65
67
sub destroy {
66
68
   my $self   = shift;
67
69
   my %schema = $self->_db_schema;
68
70
   my $SQL    = qq(DELETE FROM $self->{db_table} WHERE $schema{id} = ?);
69
   $self->dbh->do( $SQL, undef, $self->id )
70
      || croak("Can't delete session from database: " . $self->dbh->errstr);
71
   return $self->dbh->do( $SQL, undef, $self->id )
72
            || croak( q{Can't delete session from database: } . $self->dbh->errstr);
71
73
}
72
74
73
75
# private methods
74
76
75
sub _ishash { $_[0] && ref($_[0]) && ref($_[0]) eq 'HASH' }
77
sub _ishash {
78
   my $x = shift;
79
   return $x && ref $x && ref $x eq 'HASH';
80
}
76
81
77
82
sub _db_schema {
78
83
   my $self = shift;
79
   my $test = $self->{db_schema} || croak("Database session are enabled, but db_schema is missing");
80
   croak "db_schema must be a HASHref" if ! _ishash($test);
81
   $test->{id}   || croak("id parameter in db_schema is missing");
82
   $test->{data} || croak("data parameter in db_schema is missing");
83
   $test->{date} || croak("date parameter in db_schema is missing");
84
   my $test = $self->{db_schema} || croak('Database session are enabled, but db_schema is missing');
85
   croak 'db_schema must be a HASHref' if ! _ishash($test);
86
   $test->{id}   || croak('id parameter in db_schema is missing');
87
   $test->{data} || croak('data parameter in db_schema is missing');
88
   $test->{date} || croak('date parameter in db_schema is missing');
84
89
   return %{ $test };
85
90
}
86
91
@@ -92,16 +97,17 @@ sub _parse_session_real {
92
97
   my $cont = $self->_slurp_content;
93
98
   if (!$cont && !$self->{create}) {
94
99
      $self->{_db_create} = 0;
95
      my $error = $self->dbh ? $self->dbh->errstr : '';
96
      my $id    = $self->id || "<unknown sid>";
100
      my $error = $self->dbh ? $self->dbh->errstr : q{};
101
      my $id    = $self->id || '<unknown sid>';
97
102
      # $cont might be empty string, if this is a fresh session
98
103
      # in that case, _last_update must have a value
99
104
      if (! $self->{_last_update}) {
100
         croak("DBH($id)", ": ", $error || "entry does not exist in the database");
105
         croak("DBH($id)", ': ', $error || 'entry does not exist in the database');
101
106
      }
102
107
   }
103
108
   $self->{_changed}++ if !$cont;
104
109
   $self->{_data} = $self->decode($cont);
110
   return;
105
111
}
106
112
107
113
sub _slurp_content {
@@ -109,8 +115,8 @@ sub _slurp_content {
109
115
   my $sid    = $self->id || return;
110
116
   my %schema = $self->_db_schema;
111
117
   my $SQL    = qq(SELECT * FROM $self->{db_table} WHERE $schema{id} = ?);
112
   my $sth    = $self->dbh->prepare($SQL) || croak("sth error: "         . $self->dbh->errstr);
113
      $sth->execute( $sid )               || croak("sth execute error: " . $self->dbh->errstr);
118
   my $sth    = $self->dbh->prepare($SQL) || croak('sth error: '         . $self->dbh->errstr);
119
      $sth->execute( $sid )               || croak('sth execute error: ' . $self->dbh->errstr);
114
120
   # SID does not exist
115
121
   my $session = $sth->fetchrow_hashref || do { $self->{_db_create}++; return };
116
122
      $sth->finish;

Up to file-list t/03-simple.t:

1
1
#!/usr/bin/env perl -w
2
2
# Simple test. Just try to use the module.
3
3
use strict;
4
use warnings;
4
5
use Test::More qw( no_plan );
5
6
BEGIN {
6
7
    use_ok('PHP::Session::DBI');