Re [CDBI] session handling in cdbi

Matt S Trout dbix-class at trout.me.uk
Tue Oct 4 19:57:27 BST 2005


On Tue, Oct 04, 2005 at 08:31:58PM +0200, Rolf Schaufelberger wrote:
> >Hi!
> 
> >I am trying to figure out a smart way of handling permissions in
> >Class::DBI. The idea is having a kind of session object and pass that
> >around. It is simple of you're only one user using the base class, but
> >is really hard if you want to pass around different session
> >objects. Has anybody here done that kind of thing? I have been
> >searching through the archives and the Wiki but found noting.
> 
> >Thanks,
> >Patrik
> 
> I'm usig a postres DB with several schemas in it and different connections. 
> Each connection has different rights and "sees" different schemas.
> This works under Apache/mod_perl/Mason and with scripts started from shell.
> 
> The main step is described in the WIKI with something like "running CDBI under 
> mod_perl..", it means you must have an own db_main method in your base class. 
> The next step I did is to feed this base class with a hash containing the 
> different connection parameters. 
> 
> So my base cdbi class looks like:
> 
> ...
> my $db_options = {
>     RaiseError         => 1,
>     AutoCommit         => 0,
>     FetchHashKeyName   => 'NAME_lc',
>     ShowErrorStatement => 1,
>     ChopBlanks         => 1,
>     RootClass          => 'DBIx::ContextualFetch',
>     pg_server_prepare => 0
> };
> 
> # Default connection parametes
> my $app = {   DbDSN  => 'dbi:Pg:dbname=baz',
>               DbUser => 'foo',
>               DbPass => 'bar',
>               DbSearchPath => '', # I need this for Postgres schemas
>           };
> 
> sub app {
>   my $class= shift;
>   $app = shift;
> }
> 
> sub db_Main {
>   my $self=shift;
>   my $dbh;
> 
>   if ( $ENV{'MOD_PERL'} and !$Apache::ServerStarting ) {
>     $dbh = Apache->request()->pnotes('dbh');
>   }
>   if ( !$dbh ) {
>     # $app is my config hash
>      $dbh = DBI->connect_cached ( $app->{DbDSN},
>                                  $app->{DbUser},
>                                  $app->{DbPass},
>                                  $db_options );
> 
>     if ( $ENV{'MOD_PERL'} and !$Apache::ServerStarting ) {
>       Apache->request()->pnotes( 'dbh', $dbh );
>     }
>   }
> ...
> 
> And I call it either from my mod_perl/Mason handler or from my standalone 
> script 
> LJ::CDBI->app( $app );

This actually looks remarkably similar to what we implemented in
DBIx::Class::Schema (see compose_connection)

http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Schema.pm

If anybody fancies back-porting this approach to Class::DBI I'd be happy
to help out (this goes for the rest of DBIx::Class too, although most of it
would probably be much more work than this would be).

-- 
     Matt S Trout       Specialists in perl consulting, web development, and
  Technical Director    UNIX/Linux systems architecture and automation. Mail
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

 + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +




More information about the ClassDBI mailing list