Re [CDBI] session handling in cdbi

Rolf Schaufelberger rs at plusw.de
Tue Oct 4 19:31:58 BST 2005


>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 );

I've moved this into a general Application-Config-setup subroutine,
which itself takes the parameters from a parameter class. 
So all my standalone script start with 

use MyApp::Param; 
use MyApp;
my $conf = MyApp::Param;
Myapp->init($conf)

and from init I call LJ::CDBI->app($config->app);
and the only thing I chane is using different Param classes
like MyApp::SpecialApp::Param  etc.

and within Mason I do this at the beginning of each request in a custom 
mod_perl handler (which is subclassed from MasonX::WebApp).
Doesn't look very beautiful , but getting all initalized at the right time 
wasn't so easy since ( my Apache Session and my Locale Maketext all use CDBI 
and they 'use' CDBI very "early" )

Hope that helps

Rolf Schaufelberger
rs at plusw.de




More information about the ClassDBI mailing list