OSDN Git Service

改行コードをLFに統一。
[fswiki/fswiki.git] / lib / CGI / Session / PostgreSQL.pm
index f3616d4..e52552b 100644 (file)
-# CGI::Session::PostgreSQL - PostgreSQL driver for CGI::Session\r
-#\r
-# Copyright (C) 2001-2002 Sherzod Ruzmetov, sherzodr@cpan.org\r
-#\r
-# Copyright (C) 2002 Cosimo Streppone, cosimo@cpan.org\r
-# This module is based on CGI::Session::MySql module\r
-# by Sherzod Ruzmetov, original author of CGI::Session modules\r
-# and CGI::Session::MySQL driver.\r
-#\r
-# 2002/12/08 cosimo@cpan.org\r
-#            Initial release\r
-# 2003/03/01 cosimo@cpan.org\r
-#            Added `FOR UPDATE' sql clauses to enable database row lock management\r
-#\r
-# $Id: PostgreSQL.pm,v 1.1.1.1 2003/08/02 23:39:34 takezoe Exp $\r
-\r
-package CGI::Session::PostgreSQL;\r
-\r
-use strict;\r
-# Inheriting necessary functionalities from the\r
-# following libraries. Do not change it unless you know\r
-# what you are doing\r
-use base qw(\r
-    CGI::Session\r
-    CGI::Session::ID::MD5\r
-    CGI::Session::Serialize::Default\r
-);\r
-\r
-\r
-# driver specific libraries should go below\r
-\r
-use vars qw($VERSION $TABLE_NAME);\r
-\r
-($VERSION) = '$Revision: 1.1.1.1 $' =~ m/Revision:\s*(\S+)/;\r
-$TABLE_NAME = 'sessions';\r
-\r
-########################\r
-# Driver methods follow\r
-########################\r
-\r
-\r
-# stores the serialized data. Returns 1 for sucess, undef otherwise\r
-sub store {\r
-\r
-       my ($self, $sid, $options, $data) = @_;\r
-       my $dbh = $self->PostgreSQL_dbh($options);\r
-       my $db_data;\r
-\r
-       eval {\r
-\r
-               ($db_data) = $dbh->selectrow_array(\r
-                       ' SELECT a_session FROM '.$TABLE_NAME.\r
-                       ' WHERE id = '.$dbh->quote($sid).' FOR UPDATE'\r
-               );\r
-\r
-       };\r
-\r
-       if( $@ ) {\r
-               $self->error("Couldn't acquire data on id '$sid'");\r
-               return undef;\r
-       }\r
-\r
-       eval {\r
-\r
-               if( $db_data ) {\r
-\r
-#warn('do update sid='.$sid.' data='.$self->freeze($data));\r
-\r
-                       $dbh->do(\r
-                               ' UPDATE '.$TABLE_NAME.\r
-                               ' SET a_session='.$dbh->quote($self->freeze($data)).\r
-                               ' WHERE id='.$dbh->quote($sid)\r
-                       );\r
-\r
-               } else {\r
-\r
-#warn('do insert sid='.$sid.' data='.$self->freeze($data));\r
-\r
-                       $dbh->do(\r
-                               'INSERT INTO '.$TABLE_NAME.' (id,a_session) '.\r
-                               'VALUES ('.$dbh->quote($sid).', '.$dbh->quote($self->freeze($data)).')'\r
-                       );\r
-\r
-               }\r
-\r
-       };\r
-\r
-       if( $@ ) {\r
-               $self->error("Error in session update on id '$sid'. $@");\r
-               warn("Error in session update on id '$sid'. $@");\r
-               return undef;\r
-       }\r
-\r
-       return 1;\r
-}\r
-\r
-\r
-\r
-# retrieves the serialized data and deserializes it\r
-sub retrieve {\r
-    my ($self, $sid, $options) = @_;\r
-\r
-    # after you get the data, deserialize it using\r
-    # $self->thaw(), and return it\r
-    my $dbh = $self->PostgreSQL_dbh($options);\r
-       my $data;\r
-    eval {\r
-       $data = $dbh->selectrow_array(\r
-               ' SELECT a_session FROM '.$TABLE_NAME.\r
-                       ' WHERE id = '.$dbh->quote($sid)\r
-           );\r
-       };\r
-       if( $@ ) {\r
-        $self->error("Couldn't acquire data on id '$sid'");\r
-        return undef;\r
-    }\r
-    return $self->thaw($data);\r
-}\r
-\r
-\r
-# removes the given data and all the disk space associated with it\r
-sub remove {\r
-    my ($self, $sid, $options) = @_;\r
-\r
-    my $dbh = $self->PostgreSQL_dbh($options);\r
-    my $data;\r
-    eval {\r
-       $data = $dbh->selectrow_array(\r
-               ' SELECT a_session FROM '.$TABLE_NAME.\r
-                       ' WHERE id = '.$dbh->quote($sid).' FOR UPDATE'\r
-           );\r
-       };\r
-       if( $@ ) {\r
-        $self->error("Couldn't acquire data on id '$sid'");\r
-        return undef;\r
-    }\r
-\r
-       eval {\r
-               $dbh->do(\r
-                       'DELETE FROM '.$TABLE_NAME.' WHERE id = '.$dbh->quote($sid)\r
-               );\r
-       };\r
-       if( $@ ) {\r
-               $self->error("Couldn't release lock of '$sid'");\r
-               return undef;\r
-       }\r
-\r
-    return 1;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-# Called right before the object is destroyed to do cleanup\r
-sub teardown {\r
-       my ($self, $sid, $options) = @_;\r
-\r
-       my $dbh = $self->PostgreSQL_dbh($options);\r
-\r
-       # Call commit if it isn't meant to be autocommited!\r
-       unless ( $dbh->{AutoCommit} ) {\r
-               $dbh->commit();\r
-       }\r
-\r
-       if ( $self->{PostgreSQL_disconnect} ) {\r
-               $dbh->disconnect();\r
-       }\r
-\r
-       return 1;\r
-}\r
-\r
-\r
-sub PostgreSQL_dbh {\r
-    my ($self, $options) = @_;\r
-\r
-    my $args = $options->[1] || {};\r
-\r
-    if ( defined $self->{PostgreSQL_dbh} ) {\r
-        return $self->{PostgreSQL_dbh};\r
-\r
-    }\r
-\r
-       if ( defined $args->{TableName} ) {\r
-               $TABLE_NAME = $args->{TableName};\r
-       }\r
-\r
-    require DBI;\r
-\r
-    $self->{PostgreSQL_dbh} = $args->{Handle} || DBI->connect(\r
-                    $args->{DataSource},\r
-                    $args->{User}       || undef,\r
-                    $args->{Password}   || undef,\r
-                    { RaiseError=>1, PrintError=>1, AutoCommit=>1 } );\r
-\r
-    # If we're the one established the connection,\r
-    # we should be the one who closes it\r
-    $args->{Handle} or $self->{PostgreSQL_disconnect} = 1;\r
-\r
-    return $self->{PostgreSQL_dbh};\r
-\r
-}\r
-\r
-\r
-\r
-\r
-# $Id: PostgreSQL.pm,v 1.1.1.1 2003/08/02 23:39:34 takezoe Exp $\r
-\r
-1;\r
-\r
-=pod\r
-\r
-=head1 NAME\r
-\r
-CGI::Session::PostgreSQL - PostgreSQL driver for CGI::Session\r
-\r
-=head1 SYNOPSIS\r
-\r
-    use CGI::Session;\r
-    $session = new CGI::Session("driver:PostgreSQL", undef, {Handle=>$dbh});\r
-\r
-For more examples, consult L<CGI::Session> manual\r
-\r
-=head1 DESCRIPTION\r
-\r
-CGI::Session::PostgreSQL is a CGI::Session driver to store session data in a PostgreSQL table.\r
-To write your own drivers for B<CGI::Session> refere L<CGI::Session> manual.\r
-\r
-=head1 STORAGE\r
-\r
-To store session data in PostgreSQL database, you first need\r
-to create a suitable table for it with the following command:\r
-\r
-    CREATE TABLE sessions (\r
-        id CHAR(32) NOT NULL,\r
-        a_session TEXT NOT NULL\r
-    );\r
-\r
-\r
-You can also add any number of additional columns to the table,\r
-but the above "id" and "a_session" are required.\r
-If you want to store the session data in other table than "sessions",\r
-you will also need to specify B<TableName> attribute as the\r
-first argument to new():\r
-\r
-    use CGI::Session;\r
-\r
-    $session = new CGI::Session("driver:PostgreSQL", undef,\r
-                                               {Handle=>$dbh, TableName=>'my_sessions'});\r
-\r
-Every write access to session records is done through PostgreSQL own row locking mechanism,\r
-enabled by `FOR UPDATE' clauses in SELECTs or implicitly enabled in UPDATEs and DELETEs.\r
-\r
-=head1 COPYRIGHT\r
-\r
-Copyright (C) 2002 Cosimo Streppone. All rights reserved.\r
-\r
-This library is free software and can be modified and distributed\r
-under the same terms as Perl itself.\r
-\r
-=head1 AUTHOR\r
-\r
-Cosimo Streppone <cosimo@cpan.org>, heavily based on the CGI::Session::MySQL\r
-driver by Sherzod Ruzmetov, original author of CGI::Session.\r
-\r
-=head1 SEE ALSO\r
-\r
-=over 4\r
-\r
-=item *\r
-\r
-L<CGI::Session|CGI::Session> - CGI::Session manual\r
-\r
-=item *\r
-\r
-L<CGI::Session::Tutorial|CGI::Session::Tutorial> - extended CGI::Session manual\r
-\r
-=item *\r
-\r
-L<CGI::Session::CookBook|CGI::Session::CookBook> - practical solutions for real life problems\r
-\r
-=item *\r
-\r
-B<RFC 2965> - "HTTP State Management Mechanism" found at ftp://ftp.isi.edu/in-notes/rfc2965.txt\r
-\r
-=item *\r
-\r
-L<CGI|CGI> - standard CGI library\r
-\r
-=item *\r
-\r
-L<Apache::Session|Apache::Session> - another fine alternative to CGI::Session\r
-\r
-=back\r
-\r
-=cut\r
+# CGI::Session::PostgreSQL - PostgreSQL driver for CGI::Session
+#
+# Copyright (C) 2001-2002 Sherzod Ruzmetov, sherzodr@cpan.org
+#
+# Copyright (C) 2002 Cosimo Streppone, cosimo@cpan.org
+# This module is based on CGI::Session::MySql module
+# by Sherzod Ruzmetov, original author of CGI::Session modules
+# and CGI::Session::MySQL driver.
+#
+# 2002/12/08 cosimo@cpan.org
+#            Initial release
+# 2003/03/01 cosimo@cpan.org
+#            Added `FOR UPDATE' sql clauses to enable database row lock management
+#
+# $Id: PostgreSQL.pm,v 1.1.1.1 2003/08/02 23:39:34 takezoe Exp $
+
+package CGI::Session::PostgreSQL;
+
+use strict;
+# Inheriting necessary functionalities from the
+# following libraries. Do not change it unless you know
+# what you are doing
+use base qw(
+    CGI::Session
+    CGI::Session::ID::MD5
+    CGI::Session::Serialize::Default
+);
+
+
+# driver specific libraries should go below
+
+use vars qw($VERSION $TABLE_NAME);
+
+($VERSION) = '$Revision: 1.1.1.1 $' =~ m/Revision:\s*(\S+)/;
+$TABLE_NAME = 'sessions';
+
+########################
+# Driver methods follow
+########################
+
+
+# stores the serialized data. Returns 1 for sucess, undef otherwise
+sub store {
+
+       my ($self, $sid, $options, $data) = @_;
+       my $dbh = $self->PostgreSQL_dbh($options);
+       my $db_data;
+
+       eval {
+
+               ($db_data) = $dbh->selectrow_array(
+                       ' SELECT a_session FROM '.$TABLE_NAME.
+                       ' WHERE id = '.$dbh->quote($sid).' FOR UPDATE'
+               );
+
+       };
+
+       if( $@ ) {
+               $self->error("Couldn't acquire data on id '$sid'");
+               return undef;
+       }
+
+       eval {
+
+               if( $db_data ) {
+
+#warn('do update sid='.$sid.' data='.$self->freeze($data));
+
+                       $dbh->do(
+                               ' UPDATE '.$TABLE_NAME.
+                               ' SET a_session='.$dbh->quote($self->freeze($data)).
+                               ' WHERE id='.$dbh->quote($sid)
+                       );
+
+               } else {
+
+#warn('do insert sid='.$sid.' data='.$self->freeze($data));
+
+                       $dbh->do(
+                               'INSERT INTO '.$TABLE_NAME.' (id,a_session) '.
+                               'VALUES ('.$dbh->quote($sid).', '.$dbh->quote($self->freeze($data)).')'
+                       );
+
+               }
+
+       };
+
+       if( $@ ) {
+               $self->error("Error in session update on id '$sid'. $@");
+               warn("Error in session update on id '$sid'. $@");
+               return undef;
+       }
+
+       return 1;
+}
+
+
+
+# retrieves the serialized data and deserializes it
+sub retrieve {
+    my ($self, $sid, $options) = @_;
+
+    # after you get the data, deserialize it using
+    # $self->thaw(), and return it
+    my $dbh = $self->PostgreSQL_dbh($options);
+       my $data;
+    eval {
+       $data = $dbh->selectrow_array(
+               ' SELECT a_session FROM '.$TABLE_NAME.
+                       ' WHERE id = '.$dbh->quote($sid)
+           );
+       };
+       if( $@ ) {
+        $self->error("Couldn't acquire data on id '$sid'");
+        return undef;
+    }
+    return $self->thaw($data);
+}
+
+
+# removes the given data and all the disk space associated with it
+sub remove {
+    my ($self, $sid, $options) = @_;
+
+    my $dbh = $self->PostgreSQL_dbh($options);
+    my $data;
+    eval {
+       $data = $dbh->selectrow_array(
+               ' SELECT a_session FROM '.$TABLE_NAME.
+                       ' WHERE id = '.$dbh->quote($sid).' FOR UPDATE'
+           );
+       };
+       if( $@ ) {
+        $self->error("Couldn't acquire data on id '$sid'");
+        return undef;
+    }
+
+       eval {
+               $dbh->do(
+                       'DELETE FROM '.$TABLE_NAME.' WHERE id = '.$dbh->quote($sid)
+               );
+       };
+       if( $@ ) {
+               $self->error("Couldn't release lock of '$sid'");
+               return undef;
+       }
+
+    return 1;
+
+}
+
+
+
+
+# Called right before the object is destroyed to do cleanup
+sub teardown {
+       my ($self, $sid, $options) = @_;
+
+       my $dbh = $self->PostgreSQL_dbh($options);
+
+       # Call commit if it isn't meant to be autocommited!
+       unless ( $dbh->{AutoCommit} ) {
+               $dbh->commit();
+       }
+
+       if ( $self->{PostgreSQL_disconnect} ) {
+               $dbh->disconnect();
+       }
+
+       return 1;
+}
+
+
+sub PostgreSQL_dbh {
+    my ($self, $options) = @_;
+
+    my $args = $options->[1] || {};
+
+    if ( defined $self->{PostgreSQL_dbh} ) {
+        return $self->{PostgreSQL_dbh};
+
+    }
+
+       if ( defined $args->{TableName} ) {
+               $TABLE_NAME = $args->{TableName};
+       }
+
+    require DBI;
+
+    $self->{PostgreSQL_dbh} = $args->{Handle} || DBI->connect(
+                    $args->{DataSource},
+                    $args->{User}       || undef,
+                    $args->{Password}   || undef,
+                    { RaiseError=>1, PrintError=>1, AutoCommit=>1 } );
+
+    # If we're the one established the connection,
+    # we should be the one who closes it
+    $args->{Handle} or $self->{PostgreSQL_disconnect} = 1;
+
+    return $self->{PostgreSQL_dbh};
+
+}
+
+
+
+
+# $Id: PostgreSQL.pm,v 1.1.1.1 2003/08/02 23:39:34 takezoe Exp $
+
+1;
+
+=pod
+
+=head1 NAME
+
+CGI::Session::PostgreSQL - PostgreSQL driver for CGI::Session
+
+=head1 SYNOPSIS
+
+    use CGI::Session;
+    $session = new CGI::Session("driver:PostgreSQL", undef, {Handle=>$dbh});
+
+For more examples, consult L<CGI::Session> manual
+
+=head1 DESCRIPTION
+
+CGI::Session::PostgreSQL is a CGI::Session driver to store session data in a PostgreSQL table.
+To write your own drivers for B<CGI::Session> refere L<CGI::Session> manual.
+
+=head1 STORAGE
+
+To store session data in PostgreSQL database, you first need
+to create a suitable table for it with the following command:
+
+    CREATE TABLE sessions (
+        id CHAR(32) NOT NULL,
+        a_session TEXT NOT NULL
+    );
+
+
+You can also add any number of additional columns to the table,
+but the above "id" and "a_session" are required.
+If you want to store the session data in other table than "sessions",
+you will also need to specify B<TableName> attribute as the
+first argument to new():
+
+    use CGI::Session;
+
+    $session = new CGI::Session("driver:PostgreSQL", undef,
+                                               {Handle=>$dbh, TableName=>'my_sessions'});
+
+Every write access to session records is done through PostgreSQL own row locking mechanism,
+enabled by `FOR UPDATE' clauses in SELECTs or implicitly enabled in UPDATEs and DELETEs.
+
+=head1 COPYRIGHT
+
+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.
+
+=head1 AUTHOR
+
+Cosimo Streppone <cosimo@cpan.org>, heavily based on the CGI::Session::MySQL
+driver by Sherzod Ruzmetov, original author of CGI::Session.
+
+=head1 SEE ALSO
+
+=over 4
+
+=item *
+
+L<CGI::Session|CGI::Session> - CGI::Session manual
+
+=item *
+
+L<CGI::Session::Tutorial|CGI::Session::Tutorial> - extended CGI::Session manual
+
+=item *
+
+L<CGI::Session::CookBook|CGI::Session::CookBook> - practical solutions for real life problems
+
+=item *
+
+B<RFC 2965> - "HTTP State Management Mechanism" found at ftp://ftp.isi.edu/in-notes/rfc2965.txt
+
+=item *
+
+L<CGI|CGI> - standard CGI library
+
+=item *
+
+L<Apache::Session|Apache::Session> - another fine alternative to CGI::Session
+
+=back
+
+=cut