Session/Driver/postgresql.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 97 SCAR 45.0
CC
9
Cov
0%
store
30.0
init
20.0
__ex_and_ret
6.0
CRAP 90.0
1 package CGI::Session::Driver::postgresql;
2  
3 # $Id: postgresql.pm 221 2005-09-06 02:55:32Z markstos $
4  
5 # CGI::Session::Driver::postgresql - PostgreSQL driver for CGI::Session
6 #
7 # Copyright (C) 2002 Cosimo Streppone, cosimo@cpan.org
8 # This module is based on CGI::Session::Driver::mysql module
9 # by Sherzod Ruzmetov, original author of CGI::Session modules
10 # and CGI::Session::Driver::mysql driver.
11  
12 use strict;
13 use Carp "croak";
14  
15 use CGI::Session::Driver::DBI;
16 use DBD::Pg qw(PG_BYTEA PG_TEXT);
17  
18 $CGI::Session::Driver::postgresql::VERSION = '2.1';
19 @CGI::Session::Driver::postgresql::ISA = qw( CGI::Session::Driver::DBI );
20  
21  
22 sub init {
23 my $self = shift;
24 my $ret = $self->SUPER::init(@_);
25 if (defined $self->{ColumnType}) {
26 no warnings "numeric";
27 return $ret if $self->{ColumnType} == PG_BYTEA || $self->{ColumnType} == PG_TEXT;
28 $self->{ColumnType} = lc(substr($self->{ColumnType},0,1)) eq 'b' ? PG_BYTEA : PG_TEXT;
29 } else {
30 $self->{ColumnType} = PG_TEXT;
31 }
32 return $ret;
33 }
34  
35 sub store {
36 my $self = shift;
37 my ($sid, $datastr) = @_;
38 croak "store(): usage error" unless $sid && $datastr;
39  
40 my $dbh = $self->{Handle};
41 my $type = $self->{ColumnType};
42  
43 if ($type == PG_TEXT && $datastr =~ tr/\x00//) {
44 croak "Unallowed characters used in session data. Please see CGI::Session::Driver::postgresql ".
45 "for more information about null characters in text columns.";
46 }
47  
48 my $sth = $dbh->prepare("SELECT id FROM " . $self->table_name . " WHERE id=?");
49 unless ( defined $sth ) {
50 return $self->set_error( "store(): \$sth->prepare failed with message " . $dbh->errstr );
51 }
52  
53 $sth->execute( $sid ) or return $self->set_error( "store(): \$sth->execute failed with message " . $dbh->errstr );
54 if ( $sth->fetchrow_array ) {
55 __ex_and_ret($dbh,"UPDATE " . $self->table_name . " SET a_session=? WHERE id=?",$datastr,$sid, $type)
56 or return $self->set_error( "store(): serialize to db failed " . $dbh->errstr );
57 } else {
58 __ex_and_ret($dbh,"INSERT INTO " . $self->table_name . " (a_session,id) VALUES(?, ?)",$datastr, $sid, $type)
59 or return $self->set_error( "store(): serialize to db failed " . $dbh->errstr );
60 }
61 return 1;
62 }
63  
64 sub __ex_and_ret {
65 my ($dbh,$sql,$datastr,$sid,$type) = @_;
66 eval {
67 my $sth = $dbh->prepare($sql) or return 0;
68 $sth->bind_param(1,$datastr,{ pg_type => $type }) or return 0;
69 $sth->bind_param(2,$sid) or return 0;
70 $sth->execute() or return 0;
71 };
72 return 0 if $@;
73 return 1;
74 }
75  
76 1;
77  
78 =pod
79  
80 =head1 NAME
81  
82 CGI::Session::Driver::postgresql - PostgreSQL driver for CGI::Session
83  
84 =head1 SYNOPSIS
85  
86 use CGI::Session;
87 $session = new CGI::Session("driver:PostgreSQL", undef, {Handle=>$dbh});
88  
89 =head1 DESCRIPTION
90  
91 CGI::Session::PostgreSQL is a L<CGI::Session|CGI::Session> driver to store session data in a PostgreSQL table.
92  
93 =head1 STORAGE
94  
95 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:
96  
97 CREATE TABLE sessions (
98 id CHAR(32) NOT NULL PRIMARY KEY,
99 a_session BYTEA NOT NULL
100 );
101  
102 and within your code use:
103  
104 use CGI::Session;
105 $session = new CGI::Session("driver:PostgreSQL", undef, {Handle=>$dbh, ColumnType=>"binary"});
106  
107 Please note the I<ColumnType> argument. PostgreSQL's text type has problems when trying to hold a null character. (Known as C<"\0"> in Perl, not to be confused with SQL I<NULL>). If you know there is no chance of ever having a null character in the serialized data, you can leave off the I<ColumnType> attribute. Using a I<BYTEA> column type and C<< ColumnType => 'binary' >> is recommended when using L<Storable|CGI::Session::Serialize::storable> as the serializer or if there's any possibility that a null value will appear in any of the serialized data.
108  
109 For more details see L<CGI::Session::Driver::DBI|CGI::Session::Driver::DBI>, parent class.
110  
111 Also see L<sqlite driver|CGI::Session::Driver::sqlite>, which exercises different method for dealing with binary data.
112  
113 =head1 COPYRIGHT
114  
115 Copyright (C) 2002 Cosimo Streppone. All rights reserved. This library is free software and can be modified and distributed under the same terms as Perl itself.
116  
117 =head1 AUTHORS
118  
119 Cosimo Streppone <cosimo@cpan.org>, heavily based on the CGI::Session::MySQL driver by Sherzod Ruzmetov, original author of CGI::Session.
120  
121 Matt LeBlanc contributed significant updates for the 4.0 release.
122  
123 =head1 LICENSING
124  
125 For additional support and licensing see L<CGI::Session|CGI::Session>
126  
127 =cut