From: Masahiro Nagano Date: Wed, 23 Mar 2011 07:24:35 +0000 (+0900) Subject: support get_dbh callback X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4804dac361810f70fd6c930d518c227ee64c8fb9;hp=39d453e492b78344e141165baf24ed5dee957f95;p=catagits%2FWeb-Session.git support get_dbh callback --- diff --git a/lib/Plack/Session/Store/DBI.pm b/lib/Plack/Session/Store/DBI.pm index 74db5ad..28f0b30 100644 --- a/lib/Plack/Session/Store/DBI.pm +++ b/lib/Plack/Session/Store/DBI.pm @@ -12,13 +12,13 @@ use Storable (); use parent 'Plack::Session::Store'; -use Plack::Util::Accessor qw[ dbh table_name serializer deserializer ]; +use Plack::Util::Accessor qw[ dbh get_dbh table_name serializer deserializer ]; sub new { my ($class, %params) = @_; - if (! $params{dbh} ) { - die "DBI instance was not available in the argument list"; + if (! $params{dbh} && ! $params{get_dbh}) { + die "DBI instance or a callback was not available in the argument list"; } $params{table_name} ||= 'sessions'; @@ -32,7 +32,8 @@ sub new { } sub _dbh { - shift->{dbh}; + my $self =shift; + ( exists $self->{get_dbh} ) ? $self->{get_dbh}->() : $self->{dbh}; } sub fetch { @@ -107,6 +108,16 @@ Plack::Session::Store::DBI - DBI-based session store $app; }; + # set get_dbh callback for ondemand + + builder { + enable 'Session', + store => Plack::Session::Store::DBI->new( + get_dbh => sub { DBI->connect( @connect_args ) } + ); + $app; + }; + # with custom serializer/deserializer builder { diff --git a/t/006_basic_w_dbi_store.t b/t/006_basic_w_dbi_store.t index 7d5b384..2b7b03d 100644 --- a/t/006_basic_w_dbi_store.t +++ b/t/006_basic_w_dbi_store.t @@ -42,6 +42,24 @@ t::lib::TestSession::run_all_tests( }, ); +t::lib::TestSession::run_all_tests( + store => Plack::Session::Store::DBI->new( get_dbh => sub { $dbh } ), + state => Plack::Session::State->new, + env_cb => sub { + open my $in, '<', \do { my $d }; + my $env = { + 'psgi.version' => [ 1, 0 ], + 'psgi.input' => $in, + 'psgi.errors' => *STDERR, + 'psgi.url_scheme' => 'http', + SERVER_PORT => 80, + REQUEST_METHOD => 'GET', + QUERY_STRING => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}}, + }; + }, +); + + $dbh->disconnect; -done_testing; \ No newline at end of file +done_testing;