minor fixes
[sgn.git] / lib / SGN / Role / Site / DBIC.pm
blob7949060e4d6b21ede8906c273ec5df4c6f2ac09b
1 package SGN::Role::Site::DBIC;
2 use 5.10.0;
4 use Moose::Role;
5 use namespace::autoclean;
7 use Carp;
9 use Class::Load ':all';
11 requires
12 'dbc_profile',
13 'ensure_dbh_search_path_is_set',
17 =head2 dbic_schema
19 Usage: my $schema = $c->dbic_schema( 'Schema::Package', 'connection_name' );
20 Desc : get a L<DBIx::Class::Schema> with the proper connection
21 parameters for the given connection name
22 Args : L<DBIx::Class> schema package name,
23 (optional) connection name to use
24 Ret : schema object
25 Side Effects: dies on failure
27 =cut
29 sub dbic_schema {
30 my ( $class, $schema_name, $profile_name ) = @_;
31 $class = ref $class if ref $class;
33 $schema_name or croak "must provide a schema package name to dbic_schema";
34 #Class::MOP::load_class( $schema_name );
35 load_class( $schema_name );
36 state %schema_cache;
37 return $schema_cache{$class}{$profile_name || ''}{$schema_name} ||= do {
38 my $profile = $class->dbc_profile( $profile_name );
39 $schema_name->connect(
40 @{$profile}{qw| dsn user password attributes |},
41 { on_connect_call => sub { $class->ensure_dbh_search_path_is_set( shift->dbh ) } },