File Coverage

blib/lib/CGI/Application/Plugin/DBIC/Schema.pm
Criterion Covered Total %
statement 12 50 24.0
branch 0 26 0.0
condition 0 10 0.0
subroutine 4 7 57.1
pod 3 3 100.0
total 19 96 19.7


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::DBIC::Schema;
2              
3 1     1   65610 use strict;
  1         3  
  1         39  
4 1     1   6 use vars qw($VERSION @ISA @EXPORT_OK);
  1         1  
  1         55  
5 1     1   5 use Carp;
  1         6  
  1         95  
6             require Exporter;
7             @ISA = qw(Exporter);
8              
9             # "Items to export into callers namespace by default. Note: do not export
10             # names by default without a very good reason. Use EXPORT_OK instead.
11             # Do not simply export all your public functions/methods/constants."
12             # -- quote from M. Stosberg in ::Plugin::DBH
13             @EXPORT_OK = qw(
14             schema
15             dbic_config
16             resultset
17             rs
18             );
19              
20             $VERSION = '0.3';
21              
22             #
23             # This name will be used for 'default' schema (i.e., when no name is supplied.)
24             #
25 1     1   4 use constant DEFAULT_CONFIG_NAME => "__cgi_application_plugin_dbic_schema";
  1         1  
  1         466  
26              
27             sub schema {
28 0     0 1   my $self = shift;
29 0           my $name = shift;
30              
31             # Establish a default config name for the cgi::app
32 0   0       $self->{__DBIC_SCHEMA_DEFAULT_NAME} ||= DEFAULT_CONFIG_NAME;
33              
34             # Use the default name if no name was supplied.
35 0   0       $name ||= $self->{__DBIC_SCHEMA_DEFAULT_NAME}; # Unamed handle case.
36              
37 0 0         croak "must call dbic_config() before calling schema()."
38             unless $self->{__DBIC_SCHEMA_CONFIG}{$name};
39              
40 0 0         unless ( defined( $self->{__DBIC_SCHEMA}{$name} ) ) {
41 0           my $schema = $self->{__DBIC_SCHEMA_CONFIG}{$name}->{schema};
42              
43             #TODO Allow use of a DBI handle in place of a config
44 0           my @con_info = @{ $self->{__DBIC_SCHEMA_CONFIG}{$name}->{connect_info} };
  0            
45 0 0         eval { "require $schema;" } or die "Cannot require $schema: $@";
  0            
46 0           $self->{__DBIC_SCHEMA}{$name} = $schema->connect(@con_info);
47             }
48              
49 0           return $self->{__DBIC_SCHEMA}{$name};
50             }
51              
52             sub dbic_config {
53 0     0 1   my $self = shift;
54              
55 0 0         croak "too many parameters passed to dbic_config." if ( @_ > 2 );
56 0           my ( $name, $config );
57 0 0         if ( @_ == 2 ) {
    0          
58 0           ( $name, $config ) = @_;
59             }
60             elsif ( @_ == 1 ) {
61 0           $config = shift;
62             }
63             else {
64 0           croak "no config passed to dbic_config";
65             }
66              
67             # TODO Allow config to be a DBI::db handle as alternative
68 0 0         croak "config must be hashref" unless ref $config eq 'HASH';
69              
70 0   0       $self->{__DBIC_SCHEMA_DEFAULT_NAME} ||= DEFAULT_CONFIG_NAME; # First use case.
71 0   0       $name ||= $self->{__DBIC_SCHEMA_DEFAULT_NAME}; # Unamed handle case.
72              
73 0 0         croak "Calling dbic_config after the dbic has already been created"
74             if ( defined $self->{__DBIC_SCHEMA}{$name} );
75              
76 0           $self->{__DBIC_SCHEMA_CONFIG}{$name} = $config;
77              
78             }
79              
80             sub resultset {
81 0     0 1   my $c = shift;
82              
83 0           my $param_count = scalar(@_);
84 0 0         croak "Too many parameters passed to resultset" if ( $param_count > 2 );
85 0 0         croak "Too few parameters passed to resultset" if ( $param_count == 0 );
86              
87 0           my ( $config_name, $resultset_name, $result );
88 0 0         if ( $param_count == 2 ) {
89 0           ( $config_name, $resultset_name ) = @_;
90              
91             # allow undef config name (use default), but require result class name
92 0 0         croak "resultset class name must be defined"
93             if ( !defined($resultset_name) );
94              
95 0           $result = $c->schema($config_name)->resultset($resultset_name);
96             }
97             else {
98 0           $resultset_name = shift;
99 0 0         croak "resultset class name must be defined"
100             if ( !defined($resultset_name) );
101 0           $result = $c->schema()->resultset($resultset_name);
102             }
103 0           return $result;
104             }
105              
106             # short form
107             *rs = \&resultset;
108              
109             1;
110             __END__